dsh-model-everything 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +524 -140
- package/lib/render/paint.js +668 -207
- package/lib/render/svg.js +3 -0
- package/lib/render/trunk.js +213 -0
- package/lib/types/render/paint.d.ts +120 -6
- package/lib/types/render/place.d.ts +8 -3
- package/lib/types/render/trunk.d.ts +104 -0
- package/package.json +1 -1
- package/skills/model-everything-diagram/SKILL.md +1 -0
- package/src/render/paint.ts +769 -205
- package/src/render/place.ts +8 -3
- package/src/render/svg.ts +4 -0
- package/src/render/trunk.ts +275 -0
package/src/render/paint.ts
CHANGED
|
@@ -11,17 +11,27 @@
|
|
|
11
11
|
* route, not picked by comparing the boxes' centres: a route between two rows arrives vertically, so the endpoint
|
|
12
12
|
* is snapped onto the top or bottom perimeter in the direction the route approaches. Where along that side it sits
|
|
13
13
|
* follows the symbol: a rectangle spreads its ports along the side, a cylinder's top port sits on its cap curve, and a
|
|
14
|
-
* diamond or a circle has four legal points and no side to spread along at all.
|
|
14
|
+
* diamond or a circle has four legal points and no side to spread along at all. A side that carries one port is
|
|
15
|
+
* centred on it, and a side that carries several spreads them symmetrically about the middle; the bend budget comes
|
|
16
|
+
* first, so a port that is the end of a straight run or of a lane keeps the coordinate its route asked for.
|
|
15
17
|
*
|
|
16
18
|
* A symbol with discrete points gives each of its edges a point of its own: the point whose outward normal faces the
|
|
17
19
|
* box at the other end is the one that edge takes, and an edge only shares a point when all four are spoken for. A
|
|
18
20
|
* node with a run on its side keeps doing what it always did, because there the ports are places on the run and not
|
|
19
21
|
* points at all. Sharing is the fallback, not the rule: two labelled branches of a decision leave by two vertices.
|
|
20
22
|
*
|
|
21
|
-
* An edge's name is drawn on the edge, not beside it: it is anchored at the midpoint of the edge's
|
|
22
|
-
*
|
|
23
|
-
* something is in the way.
|
|
24
|
-
* whole
|
|
23
|
+
* An edge's name is drawn on the edge, not beside it: it is anchored at the midpoint of one of the edge's own straight
|
|
24
|
+
* runs, over a rectangle in the page colour that masks the line behind the text, and it slides along that run when
|
|
25
|
+
* something is in the way. It tries every run - the longest first - because on a dense drawing the longest one can be
|
|
26
|
+
* crowded for its whole length while a shorter run of the same edge is clear. A label that can clear nothing still
|
|
27
|
+
* stays on its edge, takes the place with the fewest collisions, and records what it could not avoid. Labels are
|
|
28
|
+
* placed after every edge is drawn, because what a label has to avoid is the whole picture.
|
|
29
|
+
*
|
|
30
|
+
* Every route is checked before it is drawn, and the assembled drawing is checked once more: a segment may touch no
|
|
31
|
+
* box except the two the edge terminates at. A route is a proposal - a channel run, a lane, a trunk branch, a wrapped
|
|
32
|
+
* column's sideways read - and any proposal that would cross a box is handed to the router instead. When even the
|
|
33
|
+
* router cannot clear a box, the failure is reported on the canvas rather than hidden: a drawing that says which edge
|
|
34
|
+
* could not be routed is worth more than one that quietly draws it through a box.
|
|
25
35
|
*
|
|
26
36
|
* Pure: a diagram in, shapes out, every decision assertable.
|
|
27
37
|
*/
|
|
@@ -29,6 +39,7 @@
|
|
|
29
39
|
import { MARGIN, place, textOf, type Box } from './place.js'
|
|
30
40
|
import { PORT_INSET } from './channels.js'
|
|
31
41
|
import { anchor, approachSides, crossesBox, normalOf, ROUTE_STUB, routeEdge, simplify, type Ports, type Side } from './route.js'
|
|
42
|
+
import { planTrunks, type TrunkEnd, type TrunkGroup, type TrunkPlan } from './trunk.js'
|
|
32
43
|
import { cylinderApex, cylinderCaps, hasDiscretePorts, hexagonCut, stadiumRadius } from './symbol.js'
|
|
33
44
|
import { textWidth } from './text.js'
|
|
34
45
|
import { palette, roleForVerdict, type ColourRole, type Palette, type PaletteOptions } from './palette.js'
|
|
@@ -54,6 +65,23 @@ export interface BoxShape {
|
|
|
54
65
|
initial: boolean
|
|
55
66
|
}
|
|
56
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Why a label sits where it does.
|
|
70
|
+
*
|
|
71
|
+
* A clear label records `collisions: 0` and the run it chose. A label that could clear nothing records the collisions
|
|
72
|
+
* it could not avoid, so a reader of the drawing (or a test) can tell a crowded drawing from a placement bug. The
|
|
73
|
+
* label never leaves its own edge to earn any of these numbers.
|
|
74
|
+
*/
|
|
75
|
+
export interface LabelPlacement {
|
|
76
|
+
/** The straight run it sits on, 0 being the edge's longest. */
|
|
77
|
+
run: number
|
|
78
|
+
/** Everything its mask overlaps: the other labels, boxes and edges it would have had to move off its line to clear. */
|
|
79
|
+
collisions: number
|
|
80
|
+
boxes: number
|
|
81
|
+
labels: number
|
|
82
|
+
edges: number
|
|
83
|
+
}
|
|
84
|
+
|
|
57
85
|
export interface LinkShape {
|
|
58
86
|
kind: 'link'
|
|
59
87
|
x1: number
|
|
@@ -69,6 +97,8 @@ export interface LinkShape {
|
|
|
69
97
|
dashed?: boolean
|
|
70
98
|
/** The arrow the source edge stated. Undefined means the plain arrow every edge has been drawn with. */
|
|
71
99
|
arrow?: EdgeArrow
|
|
100
|
+
/** Why the label sits where it does. Undefined on an edge with no label. */
|
|
101
|
+
labelPlacement?: LabelPlacement
|
|
72
102
|
}
|
|
73
103
|
|
|
74
104
|
export interface PathShape {
|
|
@@ -81,6 +111,23 @@ export interface PathShape {
|
|
|
81
111
|
colour: ColourRole
|
|
82
112
|
dashed?: boolean
|
|
83
113
|
arrow?: EdgeArrow
|
|
114
|
+
/** Why the label sits where it does. Undefined on an edge with no label. */
|
|
115
|
+
labelPlacement?: LabelPlacement
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A junction dot: the place one branch of a merged bundle meets the bundle's trunk.
|
|
120
|
+
*
|
|
121
|
+
* The dot is not decoration. Two lines that meet at a right angle read as a crossing unless one of them is marked as
|
|
122
|
+
* joining the other, and a bundle whose branches are not marked reads as a set of broken lines rather than a merge.
|
|
123
|
+
*/
|
|
124
|
+
export interface JunctionShape {
|
|
125
|
+
kind: 'junction'
|
|
126
|
+
x: number
|
|
127
|
+
y: number
|
|
128
|
+
colour: ColourRole
|
|
129
|
+
/** The trunk the dot belongs to: one dot per branch, one trunk id per bundle. */
|
|
130
|
+
trunk: number
|
|
84
131
|
}
|
|
85
132
|
|
|
86
133
|
/** A boundary around several states: a package, a layer, a subgraph. */
|
|
@@ -112,7 +159,7 @@ export interface TitleShape {
|
|
|
112
159
|
colour: ColourRole
|
|
113
160
|
}
|
|
114
161
|
|
|
115
|
-
export type Shape = GroupShape | BoxShape | LinkShape | PathShape | CaptionShape | TitleShape
|
|
162
|
+
export type Shape = GroupShape | BoxShape | LinkShape | PathShape | JunctionShape | CaptionShape | TitleShape
|
|
116
163
|
|
|
117
164
|
/** Which side of each box an edge leaves by and arrives at, and, for a long edge, the lane x it meets there. */
|
|
118
165
|
interface Facing {
|
|
@@ -121,10 +168,33 @@ interface Facing {
|
|
|
121
168
|
at?: number
|
|
122
169
|
}
|
|
123
170
|
|
|
171
|
+
/** One segment of a drawn route that could not be kept out of a box it does not terminate at. */
|
|
172
|
+
export interface RoutingFailure {
|
|
173
|
+
/** The edge's index in the diagram. */
|
|
174
|
+
edge: number
|
|
175
|
+
from: string
|
|
176
|
+
to: string
|
|
177
|
+
/** The box the segment passes through. */
|
|
178
|
+
box: string
|
|
179
|
+
x1: number
|
|
180
|
+
y1: number
|
|
181
|
+
x2: number
|
|
182
|
+
y2: number
|
|
183
|
+
}
|
|
184
|
+
|
|
124
185
|
export interface Canvas {
|
|
125
186
|
width: number
|
|
126
187
|
height: number
|
|
127
188
|
shapes: Shape[]
|
|
189
|
+
/**
|
|
190
|
+
* Every place a drawn segment still passes through a box it does not terminate at, after every route was checked.
|
|
191
|
+
*
|
|
192
|
+
* A drawing should carry none. The field exists for the case where no stage and no router could find a way around:
|
|
193
|
+
* a route that cannot be drawn honestly is reported here rather than silently drawn through a box.
|
|
194
|
+
*/
|
|
195
|
+
routingFailures: RoutingFailure[]
|
|
196
|
+
/** How many edge labels could not be placed clear of every box, label and other edge. */
|
|
197
|
+
labelCollisions: number
|
|
128
198
|
}
|
|
129
199
|
|
|
130
200
|
/** Geometry constants in one place: a container with different proportions changes these, not the rules. */
|
|
@@ -179,6 +249,23 @@ function packPorts(wanted: number[], low: number, high: number): number[] {
|
|
|
179
249
|
return placed
|
|
180
250
|
}
|
|
181
251
|
|
|
252
|
+
/**
|
|
253
|
+
* Centre a row of ports on the side it sits on.
|
|
254
|
+
*
|
|
255
|
+
* The row keeps its order, its gaps and its separation, and is translated as a whole so its outer two ports sit
|
|
256
|
+
* symmetrically about the middle of the span: two ports become the middle less and plus half their gap, and a longer
|
|
257
|
+
* row is the same run of places the routing asked for, moved to where the side has room for it. A row that cannot
|
|
258
|
+
* travel the whole way stops where its last port reaches the end of the span - the alternative is a port off the box.
|
|
259
|
+
*/
|
|
260
|
+
function centreRun(placed: number[], low: number, high: number): number[] {
|
|
261
|
+
if (placed.length === 0) return placed
|
|
262
|
+
const first = placed[0] as number
|
|
263
|
+
const last = placed[placed.length - 1] as number
|
|
264
|
+
const middle = (low + high) / 2
|
|
265
|
+
const shift = Math.min(high - last, Math.max(low - first, middle - (first + last) / 2))
|
|
266
|
+
return shift === 0 ? placed : placed.map((value) => value + shift)
|
|
267
|
+
}
|
|
268
|
+
|
|
182
269
|
/** Which colour a box earns from its role. One mapping, used by every container. */
|
|
183
270
|
export function roleOfNode(node: DiagramNode): ColourRole {
|
|
184
271
|
if (node.role === 'error') return 'fail'
|
|
@@ -246,26 +333,58 @@ interface LabelSpot {
|
|
|
246
333
|
height: number
|
|
247
334
|
}
|
|
248
335
|
|
|
249
|
-
/**
|
|
336
|
+
/** A straight run of an edge: one of the pieces a label may sit on. */
|
|
337
|
+
interface Run {
|
|
338
|
+
from: Point
|
|
339
|
+
to: Point
|
|
340
|
+
/** The run is axis-aligned, so its Manhattan length is its length. */
|
|
341
|
+
length: number
|
|
342
|
+
/** Where the run sits in the route, so two equal runs keep the order they are drawn in. */
|
|
343
|
+
index: number
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** The runs of one edge, longest first. A label prefers the run with the most room. */
|
|
347
|
+
function runsOf(points: Point[]): Run[] {
|
|
348
|
+
const runs: Run[] = []
|
|
349
|
+
for (let index = 0; index + 1 < points.length; index += 1) {
|
|
350
|
+
const from = points[index]
|
|
351
|
+
const to = points[index + 1]
|
|
352
|
+
const length = Math.abs(to.x - from.x) + Math.abs(to.y - from.y)
|
|
353
|
+
if (length < 1) continue
|
|
354
|
+
runs.push({ from, to, length, index })
|
|
355
|
+
}
|
|
356
|
+
return runs.sort((left, right) => right.length - left.length || left.index - right.index)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* The point an edge's label is drawn at.
|
|
361
|
+
*
|
|
362
|
+
* A label is not furniture parked beside the line: it names the transition, so it sits on the line - anchored on one
|
|
363
|
+
* of its own straight runs - and the writer masks the stretch of line it covers. Text beside a line can be read as
|
|
364
|
+
* belonging to whatever it happens to sit next to; text that interrupts its own line reads as part of it.
|
|
365
|
+
*
|
|
366
|
+
* The anchor then has to be earned. A mask over a box hides a border, over another label it makes both unreadable,
|
|
367
|
+
* and over a third edge's line it hides that line. So the label walks along a run in small steps, in both directions
|
|
368
|
+
* and bounded so its rectangle stays on that run, and takes the first place that is clear. It tries the longest run
|
|
369
|
+
* first and then the shorter ones: on a dense drawing the longest run can be crowded for its whole length while a
|
|
370
|
+
* shorter run of the same edge is empty, and a label that gives up after one run leaves the drawing with a collision
|
|
371
|
+
* it did not have to accept.
|
|
250
372
|
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
373
|
+
* When no run offers a clear place, the label takes the place with the fewest collisions - boxes and other labels
|
|
374
|
+
* first, because text over a border hides the border and text over text is unreadable, then other edges, which a mask
|
|
375
|
+
* merely interrupts. It never leaves its own edge: a label moved onto a neighbouring edge is a label that names the
|
|
376
|
+
* wrong thing. The choice is returned with the label, so a crowded drawing can be told from a placement bug.
|
|
255
377
|
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
* steps, in both directions and bounded so its rectangle stays on that run, and takes the first place that is
|
|
259
|
-
* clear. It never leaves the run: a label moved onto a neighbouring edge is a label that names the wrong thing.
|
|
378
|
+
* Exported because the choice is pure and worth asserting without a layout: a test can hand it one polyline, one box
|
|
379
|
+
* and one other route, and read which run it took and what it could not clear.
|
|
260
380
|
*/
|
|
261
|
-
function placeLabel(
|
|
381
|
+
export function placeLabel(
|
|
262
382
|
label: string,
|
|
263
|
-
|
|
264
|
-
to: Point,
|
|
383
|
+
points: Point[],
|
|
265
384
|
boxes: Iterable<Box>,
|
|
266
385
|
placed: LabelSpot[],
|
|
267
|
-
|
|
268
|
-
): LabelSpot {
|
|
386
|
+
otherRoutes: Segment[][],
|
|
387
|
+
): LabelPlacement & LabelSpot {
|
|
269
388
|
const width = textWidth(label)
|
|
270
389
|
const height = LABEL_HEIGHT
|
|
271
390
|
const spotAt = (x: number, y: number): LabelSpot => ({ x, y, width, height })
|
|
@@ -286,42 +405,60 @@ function placeLabel(
|
|
|
286
405
|
spot.x - spot.width / 2 < Math.max(segment.x1, segment.x2) + LABEL_PAD &&
|
|
287
406
|
spot.y + spot.height / 2 > Math.min(segment.y1, segment.y2) - LABEL_PAD &&
|
|
288
407
|
spot.y - spot.height / 2 < Math.max(segment.y1, segment.y2) + LABEL_PAD
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
408
|
+
/** Everything one candidate place overlaps, counted by the thing it covers rather than by the segment that draws it. */
|
|
409
|
+
const collisionsAt = (spot: LabelSpot, run: number): LabelPlacement => {
|
|
410
|
+
let boxHits = 0
|
|
411
|
+
let labelHits = 0
|
|
412
|
+
let edgeHits = 0
|
|
413
|
+
for (const box of boxes) if (hitsBox(spot, box)) boxHits += 1
|
|
414
|
+
for (const earlier of placed) if (hitsSpot(spot, earlier)) labelHits += 1
|
|
415
|
+
for (const route of otherRoutes) {
|
|
416
|
+
for (const segment of route) {
|
|
417
|
+
if (!hitsSegment(spot, segment)) continue
|
|
418
|
+
edgeHits += 1
|
|
419
|
+
break
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return { run, collisions: boxHits + labelHits + edgeHits, boxes: boxHits, labels: labelHits, edges: edgeHits }
|
|
293
423
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
424
|
+
|
|
425
|
+
const runs = runsOf(points)
|
|
426
|
+
if (runs.length === 0) {
|
|
427
|
+
// A route with no straight piece has no line to sit on: keep the label at the route's own point rather than move
|
|
428
|
+
// it into the air. Only a degenerate route reaches this, and the writer still draws its mask.
|
|
429
|
+
const point = points[0] ?? { x: 0, y: 0 }
|
|
430
|
+
return { ...spotAt(point.x, point.y), run: -1, collisions: 0, boxes: 0, labels: 0, edges: 0 }
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
let best: (LabelPlacement & LabelSpot) | undefined
|
|
434
|
+
for (const [runIndex, run] of runs.entries()) {
|
|
435
|
+
const dx = run.to.x - run.from.x
|
|
436
|
+
const dy = run.to.y - run.from.y
|
|
437
|
+
const ux = dx / run.length
|
|
438
|
+
const uy = dy / run.length
|
|
439
|
+
const middle = { x: (run.from.x + run.to.x) / 2, y: (run.from.y + run.to.y) / 2 }
|
|
440
|
+
// The walk is bounded so the label's own rectangle stays on the run that names it: a mask that hangs off the
|
|
441
|
+
// end of its segment would be half over the next thing and would not read as sitting on this edge.
|
|
442
|
+
const along = (Math.abs(ux) * width) / 2 + (Math.abs(uy) * height) / 2
|
|
443
|
+
const bound = Math.max(0, run.length / 2 - along)
|
|
444
|
+
const walk: number[] = [0]
|
|
445
|
+
for (let step = LABEL_STEP; step <= bound + 0.001; step += LABEL_STEP) walk.push(step, -step)
|
|
446
|
+
for (const offset of walk) {
|
|
447
|
+
const spot = spotAt(middle.x + ux * offset, middle.y + uy * offset)
|
|
448
|
+
const placement = collisionsAt(spot, runIndex)
|
|
449
|
+
// A clear place is the answer, and the first one is the preferred one: the search order is already the
|
|
450
|
+
// drawing's preference - longest run, own midpoint, then either side.
|
|
451
|
+
if (placement.collisions === 0) return { ...spot, ...placement }
|
|
452
|
+
if (
|
|
453
|
+
best === undefined ||
|
|
454
|
+
placement.collisions < best.collisions ||
|
|
455
|
+
(placement.collisions === best.collisions && placement.boxes + placement.labels < best.boxes + best.labels)
|
|
456
|
+
) {
|
|
457
|
+
best = { ...spot, ...placement }
|
|
458
|
+
}
|
|
459
|
+
}
|
|
297
460
|
}
|
|
298
|
-
|
|
299
|
-
// The run is axis-aligned, so its Manhattan length is its length.
|
|
300
|
-
const dx = to.x - from.x
|
|
301
|
-
const dy = to.y - from.y
|
|
302
|
-
const length = Math.abs(dx) + Math.abs(dy)
|
|
303
|
-
const ux = dx / length
|
|
304
|
-
const uy = dy / length
|
|
305
|
-
// The walk is bounded so the label's own rectangle stays on the run that names it: a mask that hangs off the
|
|
306
|
-
// end of its segment would be half over the next thing and would not read as sitting on this edge.
|
|
307
|
-
const along = (Math.abs(ux) * width) / 2 + (Math.abs(uy) * height) / 2
|
|
308
|
-
const bound = Math.max(0, length / 2 - along)
|
|
309
|
-
const walk: number[] = [0]
|
|
310
|
-
for (let step = LABEL_STEP; step <= bound + 0.001; step += LABEL_STEP) walk.push(step, -step)
|
|
311
|
-
const at = (offset: number): LabelSpot => spotAt(middle.x + ux * offset, middle.y + uy * offset)
|
|
312
|
-
for (const offset of walk) {
|
|
313
|
-
const spot = at(offset)
|
|
314
|
-
if (clearOfBoxes(spot) && clearOfEdges(spot)) return spot
|
|
315
|
-
}
|
|
316
|
-
// Nothing on the run is clear of every other edge - two runs may lie side by side for their whole length, and
|
|
317
|
-
// then no place on one is clear of the other. Covering a neighbour's line is the least of the three: text over
|
|
318
|
-
// a line still reads, while text over a box hides the box and text over text is unreadable. The label stays
|
|
319
|
-
// on its own edge, which is the one thing it may not give up.
|
|
320
|
-
for (const offset of walk) {
|
|
321
|
-
const spot = at(offset)
|
|
322
|
-
if (clearOfBoxes(spot)) return spot
|
|
323
|
-
}
|
|
324
|
-
return at(0)
|
|
461
|
+
return best as LabelPlacement & LabelSpot
|
|
325
462
|
}
|
|
326
463
|
|
|
327
464
|
|
|
@@ -352,11 +489,35 @@ function drawnExtent(shapes: Shape[]): { width: number; height: number } {
|
|
|
352
489
|
case 'title':
|
|
353
490
|
consider(textWidth((shape as { lines?: string[]; text?: string }).lines?.[0] ?? '') + shape.x, shape.y + 12)
|
|
354
491
|
break
|
|
492
|
+
case 'junction':
|
|
493
|
+
// A dot sits on a line that already reaches further than it does: counting it would never widen the canvas.
|
|
494
|
+
break
|
|
355
495
|
}
|
|
356
496
|
}
|
|
357
497
|
return { width, height }
|
|
358
498
|
}
|
|
359
499
|
|
|
500
|
+
/**
|
|
501
|
+
* The boxes a polyline passes through other than the two it terminates at.
|
|
502
|
+
*
|
|
503
|
+
* This is the drawing's one clearance rule, and it is deliberately written once: a channel run, a lane, a trunk
|
|
504
|
+
* branch, a wrapped column's sideways step and the router's fallback all have to obey the same test. A box is skipped
|
|
505
|
+
* only when it is one of the edge's own two ends, so a port that sits inside its own symbol (a cylinder's cap apex)
|
|
506
|
+
* is allowed and a segment that ends inside somebody else's box is not.
|
|
507
|
+
*/
|
|
508
|
+
export function boxesCrossedBy(points: Point[], from: Box, to: Box, boxes: Iterable<Box>): Box[] {
|
|
509
|
+
const crossed: Box[] = []
|
|
510
|
+
for (let index = 0; index + 1 < points.length; index += 1) {
|
|
511
|
+
const a = points[index]
|
|
512
|
+
const b = points[index + 1]
|
|
513
|
+
for (const box of boxes) {
|
|
514
|
+
if (box === from || box === to) continue
|
|
515
|
+
if (crossesBox(a.x, a.y, b.x, b.y, box)) crossed.push(box)
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return crossed
|
|
519
|
+
}
|
|
520
|
+
|
|
360
521
|
export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, options: PaletteOptions = {}): { canvas: Canvas; colours: Palette } {
|
|
361
522
|
const colours = palette(overrides, options)
|
|
362
523
|
const shapes: Shape[] = []
|
|
@@ -387,8 +548,15 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
387
548
|
const drawn = new Set<string>()
|
|
388
549
|
// A Map iterator is consumed by the first pass: collision checks need a list.
|
|
389
550
|
const boxList = Array.from(boxes.values())
|
|
551
|
+
/** One drawn edge: the shape, its index in the diagram, and whether its label was fixed rather than placed. */
|
|
552
|
+
interface DrawnRoute {
|
|
553
|
+
shape: LinkShape | PathShape
|
|
554
|
+
edge: number
|
|
555
|
+
/** A self loop's label is drawn beside the loop by design; the placement pass leaves it alone. */
|
|
556
|
+
fixed: boolean
|
|
557
|
+
}
|
|
390
558
|
/** The edges drawn as lines, in paint order: their labels are placed once every one of them is known. */
|
|
391
|
-
const routes:
|
|
559
|
+
const routes: DrawnRoute[] = []
|
|
392
560
|
for (const node of diagram.nodes) {
|
|
393
561
|
// A state id may repeat in a malformed model: the first placement wins and the repeat is skipped.
|
|
394
562
|
if (drawn.has(node.id)) continue
|
|
@@ -450,32 +618,173 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
450
618
|
endsAt.set(edge.to, (endsAt.get(edge.to) ?? 0) + 1)
|
|
451
619
|
}
|
|
452
620
|
|
|
621
|
+
/**
|
|
622
|
+
* Where a port may sit on one side of one node, as the interval its coordinate may take.
|
|
623
|
+
*
|
|
624
|
+
* This is the per-symbol rule, and it is why the rule lives here: a port slot is chosen in this module, so this is
|
|
625
|
+
* where the symbol has to be known. A rectangle spreads a port along the side, and a subroutine does the same. A
|
|
626
|
+
* diamond's side meets its outline at exactly one point - the vertex - so its interval is that point, and a circle's
|
|
627
|
+
* is its cardinal point; a point is all that side has to give, so two edges on one side share it rather than take
|
|
628
|
+
* two points in the air. A hexagon's top and bottom are the flat run between the cut corners, and its left and right
|
|
629
|
+
* are the vertices. A stadium's flat run stops at the end caps, and a cylinder's flat sides are the two vertical
|
|
630
|
+
* runs, never the caps.
|
|
631
|
+
*
|
|
632
|
+
* A zero-width interval is not a failure: the packer keeps every port on it, so a symbol that offers one point on a
|
|
633
|
+
* side hands that point to every edge that wants it. Which is why a symbol with four points is given one edge per
|
|
634
|
+
* side first - the point is only shared when the four are taken.
|
|
635
|
+
*/
|
|
636
|
+
const spanOf = (boxId: string, side: Side, corner: boolean): { low: number; high: number } => {
|
|
637
|
+
const box = endBox(boxId) as Box
|
|
638
|
+
const vertical = side === 'top' || side === 'bottom'
|
|
639
|
+
const middle = vertical ? box.x + box.width / 2 : box.y + box.height / 2
|
|
640
|
+
const within = (low: number, high: number): { low: number; high: number } => ({ low, high: Math.max(low, high) })
|
|
641
|
+
switch (shapeOf(boxId)) {
|
|
642
|
+
// The four vertices of a diamond, and the four cardinal points of an ellipse, are the middles of its sides.
|
|
643
|
+
case 'diamond':
|
|
644
|
+
case 'circle':
|
|
645
|
+
return within(middle, middle)
|
|
646
|
+
case 'hexagon':
|
|
647
|
+
return vertical ? within(box.x + hexagonCut(box.width), box.x + box.width - hexagonCut(box.width)) : within(middle, middle)
|
|
648
|
+
case 'stadium': {
|
|
649
|
+
const radius = stadiumRadius(box)
|
|
650
|
+
return vertical ? within(box.x + radius, box.x + box.width - radius) : within(middle, middle)
|
|
651
|
+
}
|
|
652
|
+
case 'cylinder': {
|
|
653
|
+
if (vertical) return within(middle, middle)
|
|
654
|
+
const cap = cylinderCaps(box).cap
|
|
655
|
+
return within(box.y + cap, box.y + box.height - cap)
|
|
656
|
+
}
|
|
657
|
+
default: {
|
|
658
|
+
// A rectangle, and a subroutine's doubled side, keep the margins they have always had; `corner` relaxes them
|
|
659
|
+
// because a side that is crowded uses its corners.
|
|
660
|
+
const inset = corner ? 2 : vertical ? PORT_INSET : 8
|
|
661
|
+
return vertical ? within(box.x + inset, box.x + box.width - inset) : within(box.y + inset, box.y + box.height - inset)
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
const clampTo = (boxId: string, side: Side, value: number, corner: boolean): number => {
|
|
666
|
+
const span = spanOf(boxId, side, corner)
|
|
667
|
+
return Math.min(span.high, Math.max(span.low, value))
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* The boundary point a coordinate along one side takes.
|
|
671
|
+
*
|
|
672
|
+
* The box's rectangle is the point for every symbol whose side is the box's side. A cylinder's top and bottom are
|
|
673
|
+
* arcs, so a port there sits on the cap curve's apex - `cap / 4` inside the box, not on the box's own edge.
|
|
674
|
+
*/
|
|
675
|
+
const pointOn = (boxId: string, side: Side, along: number): Point => {
|
|
676
|
+
const box = endBox(boxId) as Box
|
|
677
|
+
if (side === 'left') return { x: box.x, y: along }
|
|
678
|
+
if (side === 'right') return { x: box.x + box.width, y: along }
|
|
679
|
+
if (side === 'top') return { x: along, y: shapeOf(boxId) === 'cylinder' ? cylinderApex(box, 'top').y : box.y }
|
|
680
|
+
return { x: along, y: shapeOf(boxId) === 'cylinder' ? cylinderApex(box, 'bottom').y : box.y + box.height }
|
|
681
|
+
}
|
|
453
682
|
/** The side each edge end's port is on, where the port is a point rather than a place along a run. */
|
|
454
683
|
const claimed = new Map<string, Side>()
|
|
455
|
-
const claimedAt = new Map<string, Side
|
|
684
|
+
const claimedAt = new Map<string, Array<{ index: number; side: Side }>>()
|
|
456
685
|
const claim = (id: string, index: number, end: 'exit' | 'entry', side: Side): void => {
|
|
457
686
|
claimed.set(endKey(index, end), side)
|
|
458
|
-
claimedAt.set(id, [...(claimedAt.get(id) ?? []), side])
|
|
687
|
+
claimedAt.set(id, [...(claimedAt.get(id) ?? []), { index, side }])
|
|
688
|
+
}
|
|
689
|
+
/** The sides of one node another edge already holds. An edge is not crowded by its own other end. */
|
|
690
|
+
const takenSides = (id: string, except: number): Side[] =>
|
|
691
|
+
(claimedAt.get(id) ?? []).filter((entry) => entry.index !== except).map((entry) => entry.side)
|
|
692
|
+
|
|
693
|
+
/** The middle of one side: the coordinate that side's point sits at when nothing has moved it. */
|
|
694
|
+
const sideMiddle = (box: Box, side: Side): number => (isVerticalSide(side) ? box.x + box.width / 2 : box.y + box.height / 2)
|
|
695
|
+
|
|
696
|
+
/** The point a side offers at its middle: a symbol's own point, or the middle of the run it spreads along. */
|
|
697
|
+
const sidePoint = (boxId: string, side: Side): Point => pointOn(boxId, side, sideMiddle(endBox(boxId) as Box, side))
|
|
698
|
+
|
|
699
|
+
/** Does this side face away from the box at the other end? A route leaving it has to come back round its own box. */
|
|
700
|
+
const facesAway = (box: Box, other: Box, side: Side): boolean => {
|
|
701
|
+
const out = normalOf(side)
|
|
702
|
+
const dx = other.x + other.width / 2 - (box.x + box.width / 2)
|
|
703
|
+
const dy = other.y + other.height / 2 - (box.y + box.height / 2)
|
|
704
|
+
return out.x * dx + out.y * dy < 0
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/** The coordinate a free side takes when the other end is pinned: the pinned port's stub, read on the free axis. */
|
|
708
|
+
const stubAlong = (pinnedId: string, pinnedSide: Side, freeSide: Side): number => {
|
|
709
|
+
const point = sidePoint(pinnedId, pinnedSide)
|
|
710
|
+
const out = normalOf(pinnedSide)
|
|
711
|
+
return isVerticalSide(freeSide) ? point.x + out.x * ROUTE_STUB : point.y + out.y * ROUTE_STUB
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** Can this side put a port at `value`? A point-side offers exactly one coordinate; a run offers an interval. */
|
|
715
|
+
const offers = (boxId: string, side: Side, value: number): boolean => {
|
|
716
|
+
const span = spanOf(boxId, side, true)
|
|
717
|
+
return value >= span.low - 0.01 && value <= span.high + 0.01
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* The bend budget a candidate (exit side, entry side) pair spends on the orthogonal route it implies, before a lane
|
|
722
|
+
* or a router is consulted. The numbers, and what each one is:
|
|
723
|
+
*
|
|
724
|
+
* 0 the two stubs share a line: both on vertical sides whose x can be the same, or both on horizontal sides
|
|
725
|
+
* whose y can be the same, with each stub pointing the way the edge travels. One straight run, no corner.
|
|
726
|
+
* 1 an L: the two sides are perpendicular and the one that can still move can be placed exactly where the pinned
|
|
727
|
+
* side's stub ends, so the route turns once. It is the rung between the straight run and the Z: an L never
|
|
728
|
+
* beats a run that shares a line, and always beats a Z.
|
|
729
|
+
* 2 a Z: the stubs are perpendicular but cannot meet at one corner, or they are parallel and offset. Out, across,
|
|
730
|
+
* in: two corners.
|
|
731
|
+
* 4 a U: a side faces away from the box at the other end, so the route has to wrap a box - out, round, back
|
|
732
|
+
* in. The canonical U has both sides facing away; one is already enough to force the wrap, and a pair that
|
|
733
|
+
* has to come back the way it went is never worth a pair that does not.
|
|
734
|
+
*
|
|
735
|
+
* "Can be the same" is the symbol's own answer (`spanOf`): a rectangle offers its whole side, a diamond one point, a
|
|
736
|
+
* cylinder's top the apex of its cap. Two rectangles facing each other therefore share a line wherever they sit; two
|
|
737
|
+
* diamonds only when their vertices line up. This is a budget, not the final polyline: the router still adds the
|
|
738
|
+
* length an obstacle demands, and the channel stage still decides which lanes exist.
|
|
739
|
+
*
|
|
740
|
+
* A side another edge already holds is charged for the L it costs: the packer has to move this pair's port at least
|
|
741
|
+
* a port separation along the side, which turns the one corner the pair counted on into a jog. A Z is not charged -
|
|
742
|
+
* it was already off the line, and a port that moves along its side does not add a corner to it.
|
|
743
|
+
*/
|
|
744
|
+
const bendBudget = (fromId: string, toId: string, exit: Side, entry: Side, index: number): number => {
|
|
745
|
+
const from = endBox(fromId) as Box
|
|
746
|
+
const to = endBox(toId) as Box
|
|
747
|
+
if (facesAway(from, to, exit) || facesAway(to, from, entry)) return 4
|
|
748
|
+
if (isVerticalSide(exit) === isVerticalSide(entry)) {
|
|
749
|
+
// Parallel: a straight run only when the two stubs travel the same way and the sides can meet on one line.
|
|
750
|
+
const out = normalOf(exit)
|
|
751
|
+
const into = normalOf(entry)
|
|
752
|
+
const sameWay = out.x === -into.x && out.y === -into.y
|
|
753
|
+
const fromSpan = spanOf(fromId, exit, true)
|
|
754
|
+
const toSpan = spanOf(toId, entry, true)
|
|
755
|
+
const meet = fromSpan.low <= toSpan.high + 0.01 && toSpan.low <= fromSpan.high + 0.01
|
|
756
|
+
return sameWay && meet ? 0 : 2
|
|
757
|
+
}
|
|
758
|
+
// Perpendicular: one corner when the vertical side can be placed at the horizontal side's stub end.
|
|
759
|
+
const horizontalId = isVerticalSide(exit) ? toId : fromId
|
|
760
|
+
const horizontalSide = isVerticalSide(exit) ? entry : exit
|
|
761
|
+
const verticalId = isVerticalSide(exit) ? fromId : toId
|
|
762
|
+
const verticalSide = isVerticalSide(exit) ? exit : entry
|
|
763
|
+
if (!offers(verticalId, verticalSide, stubAlong(horizontalId, horizontalSide, verticalSide))) return 2
|
|
764
|
+
const crowded = takenSides(fromId, index).includes(exit) || takenSides(toId, index).includes(entry)
|
|
765
|
+
return crowded ? 2 : 1
|
|
459
766
|
}
|
|
460
767
|
|
|
461
768
|
/**
|
|
462
|
-
* The side a discrete port takes: the
|
|
769
|
+
* The side a discrete port takes: the point whose pair with the edge's other end bends least.
|
|
463
770
|
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
*
|
|
771
|
+
* `partner` is the side the other end holds - the side it was claimed, or, until it is claimed, the side the route
|
|
772
|
+
* approaches by. The bend budget comes first. Equal budgets fall back to the rule that was here before: the cosine
|
|
773
|
+
* of the outward normal against the direction to the other box, then the point nearest that box, then the ground
|
|
774
|
+
* between the two points - the small length term. So a case that already read well does not move, and a point that
|
|
775
|
+
* costs an extra corner loses even when it faces the target more squarely.
|
|
469
776
|
*/
|
|
470
|
-
const bestSide = (id: string,
|
|
777
|
+
const bestSide = (id: string, otherId: string, end: 'exit' | 'entry', partner: Side, index: number): Side | undefined => {
|
|
471
778
|
const box = endBox(id) as Box
|
|
779
|
+
const other = endBox(otherId) as Box
|
|
472
780
|
const centre = { x: box.x + box.width / 2, y: box.y + box.height / 2 }
|
|
473
781
|
const toward = { x: other.x + other.width / 2 - centre.x, y: other.y + other.height / 2 - centre.y }
|
|
474
782
|
const reach = Math.hypot(toward.x, toward.y)
|
|
475
|
-
const taken =
|
|
476
|
-
let best: { side: Side; score: number; near: number } | undefined
|
|
783
|
+
const taken = takenSides(id, index)
|
|
784
|
+
let best: { side: Side; bends: number; score: number; near: number; ground: number } | undefined
|
|
477
785
|
for (const side of PORT_SIDES) {
|
|
478
786
|
if (taken.includes(side)) continue
|
|
787
|
+
const bends = end === 'exit' ? bendBudget(id, otherId, side, partner, index) : bendBudget(otherId, id, partner, side, index)
|
|
479
788
|
const normal = normalOf(side)
|
|
480
789
|
const score = reach === 0 ? 0 : (normal.x * toward.x + normal.y * toward.y) / reach
|
|
481
790
|
// How far the point is from the other box's rectangle: the nearest port is the one the edge reaches first.
|
|
@@ -484,15 +793,69 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
484
793
|
Math.max(other.x - point.x, 0, point.x - (other.x + other.width)),
|
|
485
794
|
Math.max(other.y - point.y, 0, point.y - (other.y + other.height)),
|
|
486
795
|
)
|
|
796
|
+
const partnerPoint = anchor(other, partner)
|
|
797
|
+
const ground = Math.abs(point.x - partnerPoint.x) + Math.abs(point.y - partnerPoint.y)
|
|
487
798
|
const better =
|
|
488
799
|
best === undefined ||
|
|
489
|
-
|
|
490
|
-
(
|
|
491
|
-
|
|
800
|
+
bends < best.bends ||
|
|
801
|
+
(bends === best.bends &&
|
|
802
|
+
(score > best.score + 1e-9 ||
|
|
803
|
+
(Math.abs(score - best.score) <= 1e-9 &&
|
|
804
|
+
(near < best.near - 1e-9 || (Math.abs(near - best.near) <= 1e-9 && ground < best.ground - 1e-9)))))
|
|
805
|
+
if (better) best = { side, bends, score, near, ground }
|
|
492
806
|
}
|
|
493
807
|
return best?.side
|
|
494
808
|
}
|
|
495
809
|
|
|
810
|
+
/**
|
|
811
|
+
* The sides an edge leaves and enters by when no claimed point decides either end: the pair the budget likes best.
|
|
812
|
+
*
|
|
813
|
+
* Only the two same-kind pairs are offered. Both ends here spread their ports along a side, so the choice is which
|
|
814
|
+
* side of the box the edge travels on; a pair that turns the corner one side further is the case `bestEnd` decides,
|
|
815
|
+
* where a claimed point has already fixed the near end. A pair strictly cheaper than the one the route approaches by
|
|
816
|
+
* wins; an equal pair keeps the approach, which leaves a drawing that was already right exactly as it was.
|
|
817
|
+
*/
|
|
818
|
+
const bestPair = (fromId: string, toId: string, index: number): { exit: Side; entry: Side } => {
|
|
819
|
+
const approach = approachSides(endBox(fromId) as Box, endBox(toId) as Box)
|
|
820
|
+
const pairs: Array<{ exit: Side; entry: Side }> = [
|
|
821
|
+
{ exit: 'bottom', entry: 'top' },
|
|
822
|
+
{ exit: 'top', entry: 'bottom' },
|
|
823
|
+
{ exit: 'right', entry: 'left' },
|
|
824
|
+
{ exit: 'left', entry: 'right' },
|
|
825
|
+
]
|
|
826
|
+
let best = approach
|
|
827
|
+
let bestBends = bendBudget(fromId, toId, approach.exit, approach.entry, index)
|
|
828
|
+
for (const pair of pairs) {
|
|
829
|
+
const bends = bendBudget(fromId, toId, pair.exit, pair.entry, index)
|
|
830
|
+
if (bends >= bestBends) continue
|
|
831
|
+
best = pair
|
|
832
|
+
bestBends = bends
|
|
833
|
+
}
|
|
834
|
+
return best
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* The side one end takes when the other end has already been decided: the cheapest of the four.
|
|
839
|
+
*
|
|
840
|
+
* The side the route approaches by wins a tie, for the same reason as `bestPair` - a drawing that already read well
|
|
841
|
+
* does not move for a pair the budget cannot tell apart.
|
|
842
|
+
*/
|
|
843
|
+
const bestEnd = (id: string, otherId: string, end: 'exit' | 'entry', partner: Side, index: number): Side => {
|
|
844
|
+
const approach =
|
|
845
|
+
end === 'exit'
|
|
846
|
+
? approachSides(endBox(id) as Box, endBox(otherId) as Box).exit
|
|
847
|
+
: approachSides(endBox(otherId) as Box, endBox(id) as Box).entry
|
|
848
|
+
let best = approach
|
|
849
|
+
let bestBends = end === 'exit' ? bendBudget(id, otherId, approach, partner, index) : bendBudget(otherId, id, partner, approach, index)
|
|
850
|
+
for (const side of PORT_SIDES) {
|
|
851
|
+
const bends = end === 'exit' ? bendBudget(id, otherId, side, partner, index) : bendBudget(otherId, id, partner, side, index)
|
|
852
|
+
if (bends >= bestBends) continue
|
|
853
|
+
best = side
|
|
854
|
+
bestBends = bends
|
|
855
|
+
}
|
|
856
|
+
return best
|
|
857
|
+
}
|
|
858
|
+
|
|
496
859
|
// A side the drawing has already decided is claimed first, because a point there would point the route away from
|
|
497
860
|
// the corridor it has to run in: a long edge leaves by the side its lane starts from, and an edge read across two
|
|
498
861
|
// wrapped columns leaves and arrives on the sides the reading order turns at.
|
|
@@ -527,7 +890,12 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
527
890
|
const id = end === 'exit' ? edge.from : edge.to
|
|
528
891
|
if ((endsAt.get(id) ?? 0) < 2) continue
|
|
529
892
|
if (!hasDiscretePorts(shapeOf(id))) continue
|
|
530
|
-
|
|
893
|
+
// The side the other end holds, or will hold if nothing claims it first.
|
|
894
|
+
const approached = approachSides(source, target)
|
|
895
|
+
const partner =
|
|
896
|
+
claimed.get(endKey(index, end === 'exit' ? 'entry' : 'exit')) ??
|
|
897
|
+
(end === 'exit' ? approached.entry : approached.exit)
|
|
898
|
+
const side = bestSide(id, end === 'exit' ? edge.to : edge.from, end, partner, index)
|
|
531
899
|
if (side === undefined) continue
|
|
532
900
|
claim(id, index, end, side)
|
|
533
901
|
}
|
|
@@ -539,23 +907,25 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
539
907
|
if (source === undefined || target === undefined || edge.from === edge.to) return undefined
|
|
540
908
|
// A wrapped drawing is read in columns: the edge from one column to the next leaves and arrives at the side,
|
|
541
909
|
// which is the direction that reading order travels. A column is always to the right of the one before it.
|
|
542
|
-
|
|
543
|
-
? { exit: 'right' as Side, entry: 'left' as Side }
|
|
544
|
-
: approachSides(source, target)
|
|
910
|
+
if (placement.across.has(index)) return { exit: 'right', entry: 'left' }
|
|
545
911
|
const lane = placement.lanes.get(index)
|
|
546
|
-
if (lane === undefined || lane.points.length === 0) {
|
|
547
|
-
return {
|
|
548
|
-
exit: claimed.get(endKey(index, 'exit')) ?? sides.exit,
|
|
549
|
-
entry: claimed.get(endKey(index, 'entry')) ?? sides.entry,
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
912
|
// A lane runs down the page, so its ends face the rows above and below wherever the box centres are.
|
|
553
913
|
const down = target.y > source.y
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
914
|
+
if (lane !== undefined && lane.points.length > 0) {
|
|
915
|
+
return {
|
|
916
|
+
exit: claimed.get(endKey(index, 'exit')) ?? (down ? 'bottom' : 'top'),
|
|
917
|
+
entry: claimed.get(endKey(index, 'entry')) ?? (down ? 'top' : 'bottom'),
|
|
918
|
+
at: lane.points[0].x,
|
|
919
|
+
}
|
|
558
920
|
}
|
|
921
|
+
// No claimed point and no lane: an end that was claimed keeps its point, the other one takes the side the budget
|
|
922
|
+
// likes best against it; with neither claimed, the pair is chosen together.
|
|
923
|
+
const exit = claimed.get(endKey(index, 'exit'))
|
|
924
|
+
const entry = claimed.get(endKey(index, 'entry'))
|
|
925
|
+
if (exit !== undefined && entry !== undefined) return { exit, entry }
|
|
926
|
+
if (exit !== undefined) return { exit, entry: bestEnd(edge.to, edge.from, 'entry', exit, index) }
|
|
927
|
+
if (entry !== undefined) return { exit: bestEnd(edge.from, edge.to, 'exit', entry, index), entry }
|
|
928
|
+
return bestPair(edge.from, edge.to, index)
|
|
559
929
|
})
|
|
560
930
|
// A long edge's lane is where the whole edge runs, so a fan moves the lane instead of bending the edge twice at its
|
|
561
931
|
// ends. It tries the place that keeps the set evenly spaced first, then its own place moved by the same offset; a
|
|
@@ -599,67 +969,6 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
599
969
|
|
|
600
970
|
const wanted = new Map<string, number>()
|
|
601
971
|
const slotKey = (boxId: string, side: Side, index: number): string => boxId + ':' + side + ':' + String(index)
|
|
602
|
-
/**
|
|
603
|
-
* Where a port may sit on one side of one node, as the interval its coordinate may take.
|
|
604
|
-
*
|
|
605
|
-
* This is the per-symbol rule, and it is why the rule lives here: a port slot is chosen in this module, so this is
|
|
606
|
-
* where the symbol has to be known. A rectangle spreads a port along the side, and a subroutine does the same. A
|
|
607
|
-
* diamond's side meets its outline at exactly one point - the vertex - so its interval is that point, and a circle's
|
|
608
|
-
* is its cardinal point; a point is all that side has to give, so two edges on one side share it rather than take
|
|
609
|
-
* two points in the air. A hexagon's top and bottom are the flat run between the cut corners, and its left and right
|
|
610
|
-
* are the vertices. A stadium's flat run stops at the end caps, and a cylinder's flat sides are the two vertical
|
|
611
|
-
* runs, never the caps.
|
|
612
|
-
*
|
|
613
|
-
* A zero-width interval is not a failure: the packer keeps every port on it, so a symbol that offers one point on a
|
|
614
|
-
* side hands that point to every edge that wants it. Which is why a symbol with four points is given one edge per
|
|
615
|
-
* side first - the point is only shared when the four are taken.
|
|
616
|
-
*/
|
|
617
|
-
const spanOf = (boxId: string, side: Side, corner: boolean): { low: number; high: number } => {
|
|
618
|
-
const box = endBox(boxId) as Box
|
|
619
|
-
const vertical = side === 'top' || side === 'bottom'
|
|
620
|
-
const middle = vertical ? box.x + box.width / 2 : box.y + box.height / 2
|
|
621
|
-
const within = (low: number, high: number): { low: number; high: number } => ({ low, high: Math.max(low, high) })
|
|
622
|
-
switch (shapeOf(boxId)) {
|
|
623
|
-
// The four vertices of a diamond, and the four cardinal points of an ellipse, are the middles of its sides.
|
|
624
|
-
case 'diamond':
|
|
625
|
-
case 'circle':
|
|
626
|
-
return within(middle, middle)
|
|
627
|
-
case 'hexagon':
|
|
628
|
-
return vertical ? within(box.x + hexagonCut(box.width), box.x + box.width - hexagonCut(box.width)) : within(middle, middle)
|
|
629
|
-
case 'stadium': {
|
|
630
|
-
const radius = stadiumRadius(box)
|
|
631
|
-
return vertical ? within(box.x + radius, box.x + box.width - radius) : within(middle, middle)
|
|
632
|
-
}
|
|
633
|
-
case 'cylinder': {
|
|
634
|
-
if (vertical) return within(middle, middle)
|
|
635
|
-
const cap = cylinderCaps(box).cap
|
|
636
|
-
return within(box.y + cap, box.y + box.height - cap)
|
|
637
|
-
}
|
|
638
|
-
default: {
|
|
639
|
-
// A rectangle, and a subroutine's doubled side, keep the margins they have always had; `corner` relaxes them
|
|
640
|
-
// because a side that is crowded uses its corners.
|
|
641
|
-
const inset = corner ? 2 : vertical ? PORT_INSET : 8
|
|
642
|
-
return vertical ? within(box.x + inset, box.x + box.width - inset) : within(box.y + inset, box.y + box.height - inset)
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
const clampTo = (boxId: string, side: Side, value: number, corner: boolean): number => {
|
|
647
|
-
const span = spanOf(boxId, side, corner)
|
|
648
|
-
return Math.min(span.high, Math.max(span.low, value))
|
|
649
|
-
}
|
|
650
|
-
/**
|
|
651
|
-
* The boundary point a coordinate along one side takes.
|
|
652
|
-
*
|
|
653
|
-
* The box's rectangle is the point for every symbol whose side is the box's side. A cylinder's top and bottom are
|
|
654
|
-
* arcs, so a port there sits on the cap curve's apex - `cap / 4` inside the box, not on the box's own edge.
|
|
655
|
-
*/
|
|
656
|
-
const pointOn = (boxId: string, side: Side, along: number): Point => {
|
|
657
|
-
const box = endBox(boxId) as Box
|
|
658
|
-
if (side === 'left') return { x: box.x, y: along }
|
|
659
|
-
if (side === 'right') return { x: box.x + box.width, y: along }
|
|
660
|
-
if (side === 'top') return { x: along, y: shapeOf(boxId) === 'cylinder' ? cylinderApex(box, 'top').y : box.y }
|
|
661
|
-
return { x: along, y: shapeOf(boxId) === 'cylinder' ? cylinderApex(box, 'bottom').y : box.y + box.height }
|
|
662
|
-
}
|
|
663
972
|
for (const [index, entry] of facing.entries()) {
|
|
664
973
|
if (entry === undefined) continue
|
|
665
974
|
const edge = diagram.edges[index]
|
|
@@ -691,16 +1000,16 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
691
1000
|
exitWanted = clampTo(edge.from, entry.exit, chosen, corner)
|
|
692
1001
|
entryWanted = clampTo(edge.to, entry.entry, chosen, corner)
|
|
693
1002
|
} else {
|
|
694
|
-
// The two ports sit on sides of different kinds, which
|
|
695
|
-
//
|
|
696
|
-
//
|
|
697
|
-
// instead of jogging sideways to reach a middle
|
|
1003
|
+
// The two ports sit on sides of different kinds, which a discrete point causes. The end that is fixed on its
|
|
1004
|
+
// symbol's point keeps that point's coordinate; the other one sits where the route actually arrives - one step
|
|
1005
|
+
// out of the fixed port along that port's own normal, read on the free side's axis - so the two legs of the
|
|
1006
|
+
// route meet in one corner instead of jogging sideways to reach a middle the route never passes.
|
|
698
1007
|
exitWanted = hasDiscretePorts(shapeOf(edge.from))
|
|
699
1008
|
? fromMiddle
|
|
700
|
-
: clampTo(edge.from, entry.exit,
|
|
1009
|
+
: clampTo(edge.from, entry.exit, stubAlong(edge.to, entry.entry, entry.exit), false) + offset
|
|
701
1010
|
entryWanted = hasDiscretePorts(shapeOf(edge.to))
|
|
702
1011
|
? toMiddle
|
|
703
|
-
: clampTo(edge.to, entry.entry,
|
|
1012
|
+
: clampTo(edge.to, entry.entry, stubAlong(edge.from, entry.exit, entry.entry), false) + offset
|
|
704
1013
|
}
|
|
705
1014
|
wanted.set(slotKey(edge.from, entry.exit, index), exitWanted)
|
|
706
1015
|
wanted.set(slotKey(edge.to, entry.entry, index), entryWanted)
|
|
@@ -752,6 +1061,181 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
752
1061
|
}
|
|
753
1062
|
if (!moved) break
|
|
754
1063
|
}
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* A batch of arrows into one box from one side is one bundle, not a comb: which ends may merge is the planner's
|
|
1067
|
+
* answer (`trunk.ts` carries the rules), and this module supplies what only it knows - which edges have a run of
|
|
1068
|
+
* their own, and where each port landed.
|
|
1069
|
+
*
|
|
1070
|
+
* The plan is made twice. The first pass, from the ports the packing gave, says which ends merge; the centring rule
|
|
1071
|
+
* below reads it, because a merged branch is drawn into the bundle's one port and that - not its own entry port -
|
|
1072
|
+
* is the coordinate its exit either shares a line with or does not. The second pass is made from the centred ports
|
|
1073
|
+
* and is the plan the drawing uses: the run it lays out is the run of the ports that are actually drawn.
|
|
1074
|
+
*/
|
|
1075
|
+
const trunkEnds: TrunkEnd[] = []
|
|
1076
|
+
diagram.edges.forEach((edge, index) => {
|
|
1077
|
+
const entry = facing[index]
|
|
1078
|
+
if (entry === undefined) return
|
|
1079
|
+
const runY = placement.runs.get(index)
|
|
1080
|
+
trunkEnds.push({
|
|
1081
|
+
index,
|
|
1082
|
+
from: edge.from,
|
|
1083
|
+
to: edge.to,
|
|
1084
|
+
exit: entry.exit,
|
|
1085
|
+
entry: entry.entry,
|
|
1086
|
+
...(edge.arrow === undefined ? {} : { arrow: edge.arrow }),
|
|
1087
|
+
...(runY === undefined ? {} : { runY }),
|
|
1088
|
+
})
|
|
1089
|
+
})
|
|
1090
|
+
const planFor = (exitPortOf: (index: number) => Point | undefined): TrunkPlan =>
|
|
1091
|
+
planTrunks({
|
|
1092
|
+
ends: trunkEnds,
|
|
1093
|
+
// Only a node's own box is a trunk target: an arrow into a boundary points past it, not at it.
|
|
1094
|
+
boxOf: (id) => boxes.get(id),
|
|
1095
|
+
boxes: boxList,
|
|
1096
|
+
exitPortOf,
|
|
1097
|
+
})
|
|
1098
|
+
/** Where an edge's exit port sits before the centring rule has had its say. */
|
|
1099
|
+
const packedExit = (index: number): Point | undefined => {
|
|
1100
|
+
const entry = facing[index]
|
|
1101
|
+
if (entry === undefined) return undefined
|
|
1102
|
+
const edge = diagram.edges[index] as DiagramEdge
|
|
1103
|
+
const coordinate = placedPort.get(slotKey(edge.from, entry.exit, index))
|
|
1104
|
+
return coordinate === undefined ? undefined : pointOn(edge.from, entry.exit, coordinate)
|
|
1105
|
+
}
|
|
1106
|
+
const planned = planFor(packedExit)
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* Does the channel stage's lane pin this port to its own coordinate? A port off its lane is a jog added to the run.
|
|
1110
|
+
* A merged branch has a lane the painter never follows - the branch is drawn from its exit to the trunk - so it is
|
|
1111
|
+
* not pinned by it.
|
|
1112
|
+
*/
|
|
1113
|
+
const onLane = (index: number, coordinate: number): boolean => {
|
|
1114
|
+
if (planned.groupOf.has(index)) return false
|
|
1115
|
+
const lane = placement.lanes.get(index)
|
|
1116
|
+
if (lane === undefined || lane.points.length === 0) return false
|
|
1117
|
+
return Math.abs((lane.points[0] as Point).x - coordinate) <= 0.5
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
/**
|
|
1121
|
+
* A side's ports as the centring rule reads them: where they are now, and how far the whole row would move to be
|
|
1122
|
+
* centred. The row is the side, so its ports move together and not one by one.
|
|
1123
|
+
*/
|
|
1124
|
+
interface PortRow {
|
|
1125
|
+
boxId: string
|
|
1126
|
+
side: Side
|
|
1127
|
+
/** The edge indexes in the row, in the row's own order, and the coordinate each one sits at. */
|
|
1128
|
+
indexes: number[]
|
|
1129
|
+
at: number[]
|
|
1130
|
+
/** Where each index sits in `at`, so a partner port can be read without searching. */
|
|
1131
|
+
position: Map<number, number>
|
|
1132
|
+
/** How far the whole row travels to be centred. Zero when it already is. */
|
|
1133
|
+
shift: number
|
|
1134
|
+
/** Whether it may travel at all: the fixpoint below only ever takes this away. */
|
|
1135
|
+
allowed: boolean
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
const rows: PortRow[] = []
|
|
1139
|
+
const rowOf = new Map<string, PortRow>()
|
|
1140
|
+
for (const [key, list] of sides) {
|
|
1141
|
+
const [boxId, side] = key.split(':') as [string, Side]
|
|
1142
|
+
const span = spanOf(boxId, side, true)
|
|
1143
|
+
const slots = [...list]
|
|
1144
|
+
.map((entry) => ({ index: entry.index, at: placedPort.get(slotKey(boxId, side, entry.index)) as number }))
|
|
1145
|
+
.sort((left, right) => left.at - right.at || left.index - right.index)
|
|
1146
|
+
const centred = centreRun(
|
|
1147
|
+
slots.map((slot) => slot.at),
|
|
1148
|
+
span.low,
|
|
1149
|
+
span.high,
|
|
1150
|
+
)
|
|
1151
|
+
const first = slots[0] as { index: number; at: number }
|
|
1152
|
+
const row: PortRow = {
|
|
1153
|
+
boxId,
|
|
1154
|
+
side,
|
|
1155
|
+
indexes: slots.map((slot) => slot.index),
|
|
1156
|
+
at: slots.map((slot) => slot.at),
|
|
1157
|
+
position: new Map(slots.map((slot, at) => [slot.index, at] as const)),
|
|
1158
|
+
shift: (centred[0] as number) - first.at,
|
|
1159
|
+
allowed: true,
|
|
1160
|
+
}
|
|
1161
|
+
rows.push(row)
|
|
1162
|
+
rowOf.set(key, row)
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/** Where a port of a row will sit once the rows that may move have moved. */
|
|
1166
|
+
const movedAt = (row: PortRow, at: number): number => (row.at[at] as number) + (row.allowed ? row.shift : 0)
|
|
1167
|
+
|
|
1168
|
+
/**
|
|
1169
|
+
* The coordinate the port at the other end of this edge sits at, and the row that port belongs to.
|
|
1170
|
+
*
|
|
1171
|
+
* Three ends are not a drawn port of a row: the entry of a merged branch (the bundle has one port), the shared
|
|
1172
|
+
* entry itself (the planner's `entryAlong`), and a partner on a side of the other kind (a discrete symbol's point,
|
|
1173
|
+
* on a different axis). Each is a fixed coordinate or nothing, which is all the pin test needs.
|
|
1174
|
+
*/
|
|
1175
|
+
const partnerOf = (boxId: string, side: Side, index: number): { coordinate: number; row?: PortRow; at?: number } | undefined => {
|
|
1176
|
+
const entry = facing[index]
|
|
1177
|
+
if (entry === undefined) return undefined
|
|
1178
|
+
const edge = diagram.edges[index] as DiagramEdge
|
|
1179
|
+
const isExit = edge.from === boxId && entry.exit === side
|
|
1180
|
+
const groupId = planned.groupOf.get(index)
|
|
1181
|
+
if (groupId !== undefined) {
|
|
1182
|
+
// A merged branch ends at the bundle's one port, so its own entry port is not drawn and pins nothing. Its exit
|
|
1183
|
+
// is drawn, and the line it may share is the one to that shared port.
|
|
1184
|
+
if (!isExit) return undefined
|
|
1185
|
+
const group = planned.groups[groupId] as TrunkGroup
|
|
1186
|
+
if (isVerticalSide(side) !== isVerticalSide(group.side)) return undefined
|
|
1187
|
+
return { coordinate: group.entryAlong }
|
|
1188
|
+
}
|
|
1189
|
+
const partnerId = isExit ? edge.to : edge.from
|
|
1190
|
+
const partnerSide = isExit ? entry.entry : entry.exit
|
|
1191
|
+
if (isVerticalSide(side) !== isVerticalSide(partnerSide)) return undefined
|
|
1192
|
+
const coordinate = placedPort.get(slotKey(partnerId, partnerSide, index))
|
|
1193
|
+
if (coordinate === undefined) return undefined
|
|
1194
|
+
const row = rowOf.get(partnerId + ':' + partnerSide)
|
|
1195
|
+
return { coordinate, ...(row === undefined ? {} : { row, at: row.position.get(index) as number }) }
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
/**
|
|
1199
|
+
* A side that carries one port is centred on it; a side that carries several keeps its spread, translated as a
|
|
1200
|
+
* whole so its outer ports sit symmetrically about the middle.
|
|
1201
|
+
*
|
|
1202
|
+
* A port is put where the route wants it, and a side that carries one relation has no spread to fit: the only
|
|
1203
|
+
* reason its port is not in the middle is that the route asked for a coordinate near one end - the far end of the
|
|
1204
|
+
* span, the point a lane runs at. That request is real, and the bend budget decides whether it may be overruled.
|
|
1205
|
+
* A port that shares a line with the port at the other end of its edge is the middle of a straight run, and a port
|
|
1206
|
+
* on a lane is the end of a channel: moving either adds two corners. Every other port moves to where its side has
|
|
1207
|
+
* room, which costs nothing.
|
|
1208
|
+
*
|
|
1209
|
+
* So the bend budget is a fixpoint over rows, not a test per port: a row may move unless that breaks a line it
|
|
1210
|
+
* shares with a port that is staying. Two ends of one straight line may both move when they move by the same
|
|
1211
|
+
* amount - which is what centring both sides of one pair does - so the rows are resolved together, and a row that
|
|
1212
|
+
* is refused takes the lines it shares with it.
|
|
1213
|
+
*/
|
|
1214
|
+
for (let pass = 0; pass < rows.length; pass += 1) {
|
|
1215
|
+
let refused = false
|
|
1216
|
+
for (const row of rows) {
|
|
1217
|
+
if (!row.allowed || row.shift === 0) continue
|
|
1218
|
+
const pinned = row.indexes.some((index, at) => {
|
|
1219
|
+
if (onLane(index, row.at[at] as number)) return true
|
|
1220
|
+
const partner = partnerOf(row.boxId, row.side, index)
|
|
1221
|
+
if (partner === undefined) return false
|
|
1222
|
+
// Only a line that exists now can be broken: two ports that already differ are a Z, and moving one end of a Z
|
|
1223
|
+
// along its own side changes where its corners are, not how many.
|
|
1224
|
+
if (Math.abs((row.at[at] as number) - partner.coordinate) > 0.5) return false
|
|
1225
|
+
const partnerAt = partner.row === undefined ? partner.coordinate : movedAt(partner.row, partner.at as number)
|
|
1226
|
+
return Math.abs(movedAt(row, at) - partnerAt) > 0.5
|
|
1227
|
+
})
|
|
1228
|
+
if (!pinned) continue
|
|
1229
|
+
row.allowed = false
|
|
1230
|
+
refused = true
|
|
1231
|
+
}
|
|
1232
|
+
if (!refused) break
|
|
1233
|
+
}
|
|
1234
|
+
for (const row of rows) {
|
|
1235
|
+
if (!row.allowed || row.shift === 0) continue
|
|
1236
|
+
row.indexes.forEach((index, at) => placedPort.set(slotKey(row.boxId, row.side, index), movedAt(row, at)))
|
|
1237
|
+
}
|
|
1238
|
+
|
|
755
1239
|
const portOf = (boxId: string, side: Side, edgeIndex: number): Point => {
|
|
756
1240
|
const coordinate = placedPort.get(slotKey(boxId, side, edgeIndex)) as number
|
|
757
1241
|
return pointOn(boxId, side, coordinate)
|
|
@@ -768,17 +1252,12 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
768
1252
|
})
|
|
769
1253
|
}
|
|
770
1254
|
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
const length = Math.abs(to.x - from.x) + Math.abs(to.y - from.y)
|
|
778
|
-
if (length > best.length) best = { from, to, length }
|
|
779
|
-
}
|
|
780
|
-
return best
|
|
781
|
-
}
|
|
1255
|
+
const trunks = planFor((index) => anchorsFor.get(index)?.exit)
|
|
1256
|
+
// The one port each accepted bundle enters by: the middle of the target's side, which is the port the planner
|
|
1257
|
+
// centred on `entryAlong`. A merged entry is one port, so the rule for a side with one port applies to it, and the
|
|
1258
|
+
// target carries one relation's worth of ports however many arrows point at it.
|
|
1259
|
+
const sharedEntry = new Map<number, Point>()
|
|
1260
|
+
for (const group of trunks.groups) sharedEntry.set(group.id, pointOn(group.target, group.side, group.entryAlong))
|
|
782
1261
|
|
|
783
1262
|
/** The points a drawn edge passes through, whichever of the two shapes carries it. */
|
|
784
1263
|
const pointsOfRoute = (shape: LinkShape | PathShape): Point[] =>
|
|
@@ -797,7 +1276,7 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
797
1276
|
}
|
|
798
1277
|
|
|
799
1278
|
/** Paint one edge as a polyline. Its label is placed once every edge is drawn; see the pass after the loop. */
|
|
800
|
-
const paintPath = (edge: DiagramEdge, points: Point[], colour: ColourRole): void => {
|
|
1279
|
+
const paintPath = (edgeIndex: number, edge: DiagramEdge, points: Point[], colour: ColourRole): void => {
|
|
801
1280
|
const shape: PathShape = {
|
|
802
1281
|
kind: 'path',
|
|
803
1282
|
points,
|
|
@@ -809,13 +1288,13 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
809
1288
|
...(edge.arrow === undefined ? {} : { arrow: edge.arrow }),
|
|
810
1289
|
}
|
|
811
1290
|
shapes.push(shape)
|
|
812
|
-
routes.push(shape)
|
|
1291
|
+
routes.push({ shape, edge: edgeIndex, fixed: false })
|
|
813
1292
|
}
|
|
814
1293
|
|
|
815
1294
|
/** Two points are a line and more of them are a polyline; both are an edge carrying a label. */
|
|
816
|
-
const paintRoute = (edge: DiagramEdge, points: Point[], colour: ColourRole): void => {
|
|
1295
|
+
const paintRoute = (edgeIndex: number, edge: DiagramEdge, points: Point[], colour: ColourRole): void => {
|
|
817
1296
|
if (points.length > 2) {
|
|
818
|
-
paintPath(edge, points, colour)
|
|
1297
|
+
paintPath(edgeIndex, edge, points, colour)
|
|
819
1298
|
return
|
|
820
1299
|
}
|
|
821
1300
|
const exit = points[0]
|
|
@@ -834,9 +1313,18 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
834
1313
|
...(edge.arrow === undefined ? {} : { arrow: edge.arrow }),
|
|
835
1314
|
}
|
|
836
1315
|
shapes.push(shape)
|
|
837
|
-
routes.push(shape)
|
|
1316
|
+
routes.push({ shape, edge: edgeIndex, fixed: false })
|
|
838
1317
|
}
|
|
839
1318
|
|
|
1319
|
+
/**
|
|
1320
|
+
* Is this route clear of every box it does not terminate at?
|
|
1321
|
+
*
|
|
1322
|
+
* One test, used by every stage. A run, a lane and a trunk branch are proposals made from the layout; when the
|
|
1323
|
+
* proposal meets a box - which happens when the layout reserved a run between two boundaries while the painter
|
|
1324
|
+
* draws the boxes inside them - the proposal is refused and the router gets the edge.
|
|
1325
|
+
*/
|
|
1326
|
+
const clearOfBoxes = (points: Point[], from: Box, to: Box): boolean => boxesCrossedBy(points, from, to, boxList).length === 0
|
|
1327
|
+
|
|
840
1328
|
/**
|
|
841
1329
|
* The polyline through a run: out of one port, across the strip between two rows, and into the other, square to
|
|
842
1330
|
* whatever side each port belongs to.
|
|
@@ -849,10 +1337,16 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
849
1337
|
const runRoute = (ports: Ports, runY: number): Point[] => {
|
|
850
1338
|
const out = isVerticalSide(ports.exitSide) ? 0 : normalOf(ports.exitSide).x * ROUTE_STUB
|
|
851
1339
|
const into = isVerticalSide(ports.entrySide) ? 0 : normalOf(ports.entrySide).x * ROUTE_STUB
|
|
1340
|
+
// A sideways port and a vertical one meet where the other port already is, if that is the way the stub points: the
|
|
1341
|
+
// stub is the straight run out of the port, and it is worth more than a stub to avoid the point-wide jog that
|
|
1342
|
+
// meeting at the nominal length leaves behind - a jog the edge would be charged a bend for and a reader for
|
|
1343
|
+
// nothing. Only when the other port is on the far side does the stub have to overshoot.
|
|
1344
|
+
const exitRun = out !== 0 && into === 0 && Math.sign(ports.entry.x - ports.exit.x) === Math.sign(out) ? ports.entry.x : ports.exit.x + out
|
|
1345
|
+
const entryRun = into !== 0 && out === 0 && Math.sign(ports.exit.x - ports.entry.x) === Math.sign(into) ? ports.exit.x : ports.entry.x + into
|
|
852
1346
|
const points: Point[] = [ports.exit]
|
|
853
|
-
if (out !== 0) points.push({ x:
|
|
854
|
-
points.push({ x:
|
|
855
|
-
if (into !== 0) points.push({ x:
|
|
1347
|
+
if (out !== 0) points.push({ x: exitRun, y: ports.exit.y })
|
|
1348
|
+
points.push({ x: exitRun, y: runY }, { x: entryRun, y: runY })
|
|
1349
|
+
if (into !== 0) points.push({ x: entryRun, y: ports.entry.y })
|
|
856
1350
|
points.push(ports.entry)
|
|
857
1351
|
return simplify(points)
|
|
858
1352
|
}
|
|
@@ -875,19 +1369,8 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
875
1369
|
return true
|
|
876
1370
|
}
|
|
877
1371
|
|
|
878
|
-
/**
|
|
879
|
-
const
|
|
880
|
-
for (let at = 0; at + 1 < points.length; at += 1) {
|
|
881
|
-
const a = points[at]
|
|
882
|
-
const b = points[at + 1]
|
|
883
|
-
for (const box of boxList) {
|
|
884
|
-
if (box === from || box === to) continue
|
|
885
|
-
if (crossesBox(a.x, a.y, b.x, b.y, box)) return false
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
return true
|
|
889
|
-
}
|
|
890
|
-
|
|
1372
|
+
/** Where a branch meets its trunk, one per merged edge. They are drawn after every line, so no line covers one. */
|
|
1373
|
+
const junctions: JunctionShape[] = []
|
|
891
1374
|
for (const [edgeIndex, edge] of diagram.edges.entries()) {
|
|
892
1375
|
const source = endBox(edge.from)
|
|
893
1376
|
const target = endBox(edge.to)
|
|
@@ -897,7 +1380,7 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
897
1380
|
if (edge.from === edge.to) {
|
|
898
1381
|
const x = source.x + source.width
|
|
899
1382
|
const y = source.y + source.height / 2
|
|
900
|
-
|
|
1383
|
+
const shape: PathShape = {
|
|
901
1384
|
kind: 'path',
|
|
902
1385
|
points: [
|
|
903
1386
|
{ x, y: y - 9 },
|
|
@@ -913,12 +1396,38 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
913
1396
|
colour,
|
|
914
1397
|
...(edge.dashed === undefined ? {} : { dashed: edge.dashed }),
|
|
915
1398
|
...(edge.arrow === undefined ? {} : { arrow: edge.arrow }),
|
|
916
|
-
}
|
|
1399
|
+
}
|
|
1400
|
+
shapes.push(shape)
|
|
1401
|
+
routes.push({ shape, edge: edgeIndex, fixed: true })
|
|
917
1402
|
continue
|
|
918
1403
|
}
|
|
919
1404
|
|
|
920
1405
|
const anchors = anchorsFor.get(edgeIndex)
|
|
921
1406
|
|
|
1407
|
+
// A branch of an accepted bundle: out of its own port, square onto the trunk, along the trunk to the bundle's one
|
|
1408
|
+
// port, and in. The last leg is shared with every other branch of the group, so the target is entered once; the
|
|
1409
|
+
// branch still draws its own head and its own label. The junction is recorded where the branch meets the trunk,
|
|
1410
|
+
// because a reader who is not shown the join reads the two lines as one line that was broken. A branch that would
|
|
1411
|
+
// cross a box is refused here as well: the planner checked it, but the painter is the last stage that knows every
|
|
1412
|
+
// box the drawing actually carries.
|
|
1413
|
+
const groupId = trunks.groupOf.get(edgeIndex)
|
|
1414
|
+
if (groupId !== undefined && anchors !== undefined) {
|
|
1415
|
+
const group = trunks.groups[groupId] as TrunkGroup
|
|
1416
|
+
const entry = sharedEntry.get(groupId) as Point
|
|
1417
|
+
const branch: Point[] = isVerticalSide(group.side)
|
|
1418
|
+
? [anchors.exit, { x: anchors.exit.x, y: group.at }, { x: entry.x, y: group.at }, entry]
|
|
1419
|
+
: [anchors.exit, { x: group.at, y: anchors.exit.y }, { x: group.at, y: entry.y }, entry]
|
|
1420
|
+
const points = simplify(branch)
|
|
1421
|
+
if (clearOfBoxes(points, source, target)) {
|
|
1422
|
+
paintRoute(edgeIndex, edge, points, colour)
|
|
1423
|
+
// The join itself, read off the two ports rather than off the emitted points: a branch that drops straight
|
|
1424
|
+
// into the shared port is drawn as one segment, and its junction is still where the trunk crosses it.
|
|
1425
|
+
const meet = isVerticalSide(group.side) ? { x: anchors.exit.x, y: group.at } : { x: group.at, y: anchors.exit.y }
|
|
1426
|
+
junctions.push({ kind: 'junction', x: meet.x, y: meet.y, colour, trunk: group.id })
|
|
1427
|
+
continue
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
|
|
922
1431
|
// A long edge follows the lane the channel stage gave it: down the exit stub, across to the lane's x on the run
|
|
923
1432
|
// the gap was assigned, down the lane, and across to the entry stub. The spine carries one point per gap, so the
|
|
924
1433
|
// two runs are the places where a port and the lane disagree, and everything between them is one straight drop.
|
|
@@ -936,41 +1445,93 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
936
1445
|
}
|
|
937
1446
|
if (Math.abs(previous.x - anchors.entry.x) > 0.5 && Math.abs(previous.y - anchors.entry.y) > 0.5) points.push({ x: anchors.entry.x, y: previous.y })
|
|
938
1447
|
points.push(anchors.entry)
|
|
939
|
-
|
|
940
|
-
|
|
1448
|
+
const simplified = simplify(points)
|
|
1449
|
+
if (clearOfBoxes(simplified, source, target)) {
|
|
1450
|
+
paintPath(edgeIndex, edge, simplified, colour)
|
|
1451
|
+
continue
|
|
1452
|
+
}
|
|
941
1453
|
}
|
|
942
1454
|
|
|
943
|
-
// Two rows with nothing between them: the edge is the two-bend run the channel stage stacked.
|
|
944
|
-
// empty strip
|
|
945
|
-
//
|
|
1455
|
+
// Two rows with nothing between them: the edge is the two-bend run the channel stage stacked. The run's height is
|
|
1456
|
+
// an empty strip only when the two rows it was measured between are the boxes actually drawn: a run reserved
|
|
1457
|
+
// between two boundaries is a strip outside them, while the painter's ports belong to boxes inside them. So the
|
|
1458
|
+
// proposal is checked, not trusted, and an edge whose legs would cross a box is the router's.
|
|
946
1459
|
const runY = placement.runs.get(edgeIndex)
|
|
947
1460
|
if (runY !== undefined && anchors !== undefined && runFits(anchors, runY)) {
|
|
948
1461
|
const points = runRoute(anchors, runY)
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
paintRoute(edge, points, colour)
|
|
1462
|
+
if (clearOfBoxes(points, source, target)) {
|
|
1463
|
+
paintRoute(edgeIndex, edge, points, colour)
|
|
952
1464
|
continue
|
|
953
1465
|
}
|
|
954
1466
|
}
|
|
955
1467
|
|
|
956
|
-
|
|
1468
|
+
// The router is the last stage. Its own straight-line shortcut is already refused when a box is in the way; a
|
|
1469
|
+
// search that cannot finish still returns a line, and the final pass over the assembled drawing reports it.
|
|
1470
|
+
paintRoute(edgeIndex, edge, routeEdge(source, target, boxList, anchors).points, colour)
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
// The dots last of the lines: a dot marks a join, so no line of the drawing may be drawn over it.
|
|
1474
|
+
for (const dot of junctions) shapes.push(dot)
|
|
1475
|
+
|
|
1476
|
+
// The assembled drawing, checked once: every segment of every drawn edge against every box it does not terminate
|
|
1477
|
+
// at. Each candidate route was checked before it was emitted, so this pass is the assertion that no stage slipped a
|
|
1478
|
+
// segment through a box - and the place a router that found no way round is reported rather than hidden.
|
|
1479
|
+
const boxEntries = Array.from(boxes.entries())
|
|
1480
|
+
const routingFailures: RoutingFailure[] = []
|
|
1481
|
+
for (const { shape, edge } of routes) {
|
|
1482
|
+
const diagramEdge = diagram.edges[edge] as DiagramEdge
|
|
1483
|
+
const source = endBox(diagramEdge.from)
|
|
1484
|
+
const target = endBox(diagramEdge.to)
|
|
1485
|
+
if (source === undefined || target === undefined) continue
|
|
1486
|
+
for (const segment of segmentsOfRoute(shape)) {
|
|
1487
|
+
for (const [id, box] of boxEntries) {
|
|
1488
|
+
if (box === source || box === target) continue
|
|
1489
|
+
if (!crossesBox(segment.x1, segment.y1, segment.x2, segment.y2, box)) continue
|
|
1490
|
+
routingFailures.push({
|
|
1491
|
+
edge,
|
|
1492
|
+
from: diagramEdge.from,
|
|
1493
|
+
to: diagramEdge.to,
|
|
1494
|
+
box: id,
|
|
1495
|
+
x1: segment.x1,
|
|
1496
|
+
y1: segment.y1,
|
|
1497
|
+
x2: segment.x2,
|
|
1498
|
+
y2: segment.y2,
|
|
1499
|
+
})
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
957
1502
|
}
|
|
958
1503
|
|
|
959
1504
|
// Labels last. A label has to clear every other edge, and until the last edge is drawn the painter does not know
|
|
960
1505
|
// where those are: this is why the whole pass exists rather than a decision made edge by edge.
|
|
961
|
-
const routeSegments = routes.map((shape) => segmentsOfRoute(shape))
|
|
1506
|
+
const routeSegments = routes.map(({ shape }) => segmentsOfRoute(shape))
|
|
962
1507
|
const placedLabels: LabelSpot[] = []
|
|
963
|
-
routes.forEach((shape,
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
1508
|
+
routes.forEach(({ shape, fixed }, at) => {
|
|
1509
|
+
if (fixed) {
|
|
1510
|
+
if (shape.label !== '') {
|
|
1511
|
+
placedLabels.push({ x: shape.labelX, y: shape.labelY, width: textWidth(shape.label), height: LABEL_HEIGHT })
|
|
1512
|
+
}
|
|
1513
|
+
return
|
|
1514
|
+
}
|
|
1515
|
+
if (shape.label === '') return
|
|
1516
|
+
const placement = placeLabel(
|
|
1517
|
+
shape.label,
|
|
1518
|
+
pointsOfRoute(shape),
|
|
1519
|
+
boxList,
|
|
1520
|
+
placedLabels,
|
|
1521
|
+
routeSegments.filter((_, other) => other !== at),
|
|
1522
|
+
)
|
|
1523
|
+
shape.labelX = placement.x
|
|
1524
|
+
shape.labelY = placement.y
|
|
1525
|
+
shape.labelPlacement = {
|
|
1526
|
+
run: placement.run,
|
|
1527
|
+
collisions: placement.collisions,
|
|
1528
|
+
boxes: placement.boxes,
|
|
1529
|
+
labels: placement.labels,
|
|
1530
|
+
edges: placement.edges,
|
|
1531
|
+
}
|
|
1532
|
+
placedLabels.push({ x: placement.x, y: placement.y, width: placement.width, height: placement.height })
|
|
973
1533
|
})
|
|
1534
|
+
const labelCollisions = routes.reduce((total, { shape }) => total + ((shape.labelPlacement?.collisions ?? 0) > 0 ? 1 : 0), 0)
|
|
974
1535
|
|
|
975
1536
|
shapes.push({ kind: 'title', x: MARGIN, y: MARGIN + 12, text: diagram.title, colour: 'text' })
|
|
976
1537
|
|
|
@@ -998,7 +1559,10 @@ export function paintWith(diagram: Diagram, overrides: Partial<Palette> = {}, op
|
|
|
998
1559
|
const canvasWidth = Math.max(width, extent.width + MARGIN)
|
|
999
1560
|
const canvasHeight = Math.max(height, extent.height + MARGIN)
|
|
1000
1561
|
|
|
1001
|
-
return {
|
|
1562
|
+
return {
|
|
1563
|
+
canvas: { width: Math.round(canvasWidth), height: Math.round(canvasHeight), shapes, routingFailures, labelCollisions },
|
|
1564
|
+
colours,
|
|
1565
|
+
}
|
|
1002
1566
|
}
|
|
1003
1567
|
|
|
1004
1568
|
export function paint(diagram: Diagram, overrides: Partial<Palette> = {}): Canvas {
|