@multiplatform.one/node-graph 8.2.2 → 8.4.0
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/dist/cjs/NodeGraph.native.js.map +1 -1
- package/dist/cjs/bench/frameSampler.native.js.map +1 -1
- package/dist/cjs/bench/generateGraph.native.js.map +1 -1
- package/dist/cjs/diagnostics/GraphDiagnostics.native.js.map +1 -1
- package/dist/cjs/diagnostics/LiveGraphDiagnostics.native.js.map +1 -1
- package/dist/cjs/diagnostics/fromWire.native.js.map +1 -1
- package/dist/cjs/diagnostics/lastError.native.js.map +1 -1
- package/dist/cjs/diagnostics/types.native.js.map +1 -1
- package/dist/cjs/diagnostics/useGraphDiagnostics.native.js.map +1 -1
- package/dist/cjs/dom/GraphDomLayout.native.js.map +1 -1
- package/dist/cjs/dom/boot.native.js.map +1 -1
- package/dist/cjs/dom/wire.native.js.map +1 -1
- package/dist/cjs/edges/GraphEdge.cjs +21 -50
- package/dist/cjs/edges/GraphEdge.native.js +23 -50
- package/dist/cjs/edges/GraphEdge.native.js.map +1 -1
- package/dist/cjs/edges/edgeKinds.native.js.map +1 -1
- package/dist/cjs/edges/edgePath.cjs +94 -0
- package/dist/cjs/edges/edgePath.native.js +98 -0
- package/dist/cjs/edges/edgePath.native.js.map +1 -0
- package/dist/cjs/edges/edgeRoute.cjs +886 -0
- package/dist/cjs/edges/edgeRoute.native.js +1399 -0
- package/dist/cjs/edges/edgeRoute.native.js.map +1 -0
- package/dist/cjs/edges/edgeStyle.native.js.map +1 -1
- package/dist/cjs/edges/useEdgeObstacles.cjs +78 -0
- package/dist/cjs/edges/useEdgeObstacles.native.js +110 -0
- package/dist/cjs/edges/useEdgeObstacles.native.js.map +1 -0
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/interaction/lassoIntercept.native.js.map +1 -1
- package/dist/cjs/interaction/legalTargets.native.js.map +1 -1
- package/dist/cjs/interaction/snapAlignment.native.js.map +1 -1
- package/dist/cjs/interaction/snapChanges.native.js.map +1 -1
- package/dist/cjs/interaction/spatialHash.native.js.map +1 -1
- package/dist/cjs/interaction/useLassoSelect.native.js.map +1 -1
- package/dist/cjs/knobs/KnobEnvelopeProvider.native.js.map +1 -1
- package/dist/cjs/knobs/envelope.native.js.map +1 -1
- package/dist/cjs/knobs/graphKnobs.native.js.map +1 -1
- package/dist/cjs/knobs/useKnobEnvelope.native.js.map +1 -1
- package/dist/cjs/layout/dagre.native.js.map +1 -1
- package/dist/cjs/layout/layout.native.js.map +1 -1
- package/dist/cjs/layout/runner.native.js.map +1 -1
- package/dist/cjs/layout/structuredCloneShim.native.js.map +1 -1
- package/dist/cjs/layout/types.native.js.map +1 -1
- package/dist/cjs/layout/worker.entry.native.js.map +1 -1
- package/dist/cjs/layout/worker.generated.native.js.map +1 -1
- package/dist/cjs/model/commands.native.js.map +1 -1
- package/dist/cjs/model/connect.native.js.map +1 -1
- package/dist/cjs/model/document.native.js.map +1 -1
- package/dist/cjs/model/history.native.js.map +1 -1
- package/dist/cjs/model/index.native.js.map +1 -1
- package/dist/cjs/model/schema.native.js.map +1 -1
- package/dist/cjs/model/select.native.js.map +1 -1
- package/dist/cjs/model/serialize.native.js.map +1 -1
- package/dist/cjs/native/DomOnlyStory.native.js.map +1 -1
- package/dist/cjs/native/GraphToolbar.native.js.map +1 -1
- package/dist/cjs/native/domEntryRegistry.native.js.map +1 -1
- package/dist/cjs/native/graphChildren.native.js.map +1 -1
- package/dist/cjs/nodes/GraphNode.native.js.map +1 -1
- package/dist/cjs/nodes/GraphPort.native.js.map +1 -1
- package/dist/cjs/nodes/handleGeometry.native.js.map +1 -1
- package/dist/cjs/nodes/lod.native.js.map +1 -1
- package/dist/cjs/nodes/nodeMeasure.native.js.map +1 -1
- package/dist/cjs/nodes/overviewNode.native.js.map +1 -1
- package/dist/cjs/react-flow/ConnectionPuck.native.js.map +1 -1
- package/dist/cjs/react-flow/GraphActions.native.js.map +1 -1
- package/dist/cjs/react-flow/GraphCommandBridge.native.js.map +1 -1
- package/dist/cjs/react-flow/GraphNodeHost.native.js.map +1 -1
- package/dist/cjs/react-flow/GraphPaintProbe.native.js.map +1 -1
- package/dist/cjs/react-flow/GraphZoomVar.native.js.map +1 -1
- package/dist/cjs/react-flow/LassoOverlay.native.js.map +1 -1
- package/dist/cjs/react-flow/NodeGraphWeb.native.js.map +1 -1
- package/dist/cjs/react-flow/SnapGuides.native.js.map +1 -1
- package/dist/cjs/react-flow/demoControlBody.native.js.map +1 -1
- package/dist/cjs/react-flow/demoNodeBody.native.js.map +1 -1
- package/dist/cjs/react-flow/fixtures.native.js.map +1 -1
- package/dist/cjs/react-flow/flowAdapter.native.js.map +1 -1
- package/dist/cjs/react-flow/graphHandle.native.js.map +1 -1
- package/dist/cjs/react-flow/nodeCensus.native.js.map +1 -1
- package/dist/cjs/react-flow/useGraphController.native.js.map +1 -1
- package/dist/cjs/react-flow/xy.cjs +4 -3
- package/dist/cjs/react-flow/xy.native.js +4 -3
- package/dist/cjs/react-flow/xy.native.js.map +1 -1
- package/dist/cjs/react-flow/xyPaths.cjs +31 -0
- package/dist/cjs/react-flow/xyPaths.native.js +34 -0
- package/dist/cjs/react-flow/xyPaths.native.js.map +1 -0
- package/dist/cjs/theme/GraphFaces.native.js.map +1 -1
- package/dist/cjs/theme/GraphVarsStyle.native.js.map +1 -1
- package/dist/cjs/theme/faces.generated.native.js.map +1 -1
- package/dist/cjs/theme/faces.native.js.map +1 -1
- package/dist/cjs/theme/graphVars.native.js.map +1 -1
- package/dist/esm/edges/GraphEdge.mjs +23 -52
- package/dist/esm/edges/GraphEdge.mjs.map +1 -1
- package/dist/esm/edges/GraphEdge.native.js +25 -52
- package/dist/esm/edges/GraphEdge.native.js.map +1 -1
- package/dist/esm/edges/edgePath.mjs +68 -0
- package/dist/esm/edges/edgePath.mjs.map +1 -0
- package/dist/esm/edges/edgePath.native.js +69 -0
- package/dist/esm/edges/edgePath.native.js.map +1 -0
- package/dist/esm/edges/edgeRoute.mjs +857 -0
- package/dist/esm/edges/edgeRoute.mjs.map +1 -0
- package/dist/esm/edges/edgeRoute.native.js +1367 -0
- package/dist/esm/edges/edgeRoute.native.js.map +1 -0
- package/dist/esm/edges/useEdgeObstacles.mjs +52 -0
- package/dist/esm/edges/useEdgeObstacles.mjs.map +1 -0
- package/dist/esm/edges/useEdgeObstacles.native.js +81 -0
- package/dist/esm/edges/useEdgeObstacles.native.js.map +1 -0
- package/dist/esm/react-flow/xy.mjs +2 -1
- package/dist/esm/react-flow/xy.mjs.map +1 -1
- package/dist/esm/react-flow/xy.native.js +2 -1
- package/dist/esm/react-flow/xy.native.js.map +1 -1
- package/dist/esm/react-flow/xyPaths.mjs +3 -0
- package/dist/esm/react-flow/xyPaths.mjs.map +1 -0
- package/dist/esm/react-flow/xyPaths.native.js +3 -0
- package/dist/esm/react-flow/xyPaths.native.js.map +1 -0
- package/dist/jsx/edges/GraphEdge.mjs +23 -52
- package/dist/jsx/edges/GraphEdge.mjs.map +1 -1
- package/dist/jsx/edges/GraphEdge.native.js +23 -50
- package/dist/jsx/edges/GraphEdge.native.js.map +1 -1
- package/dist/jsx/edges/edgePath.mjs +68 -0
- package/dist/jsx/edges/edgePath.mjs.map +1 -0
- package/dist/jsx/edges/edgePath.native.js +98 -0
- package/dist/jsx/edges/edgePath.native.js.map +1 -0
- package/dist/jsx/edges/edgeRoute.mjs +857 -0
- package/dist/jsx/edges/edgeRoute.mjs.map +1 -0
- package/dist/jsx/edges/edgeRoute.native.js +1399 -0
- package/dist/jsx/edges/edgeRoute.native.js.map +1 -0
- package/dist/jsx/edges/useEdgeObstacles.mjs +52 -0
- package/dist/jsx/edges/useEdgeObstacles.mjs.map +1 -0
- package/dist/jsx/edges/useEdgeObstacles.native.js +110 -0
- package/dist/jsx/edges/useEdgeObstacles.native.js.map +1 -0
- package/dist/jsx/react-flow/xy.mjs +2 -1
- package/dist/jsx/react-flow/xy.mjs.map +1 -1
- package/dist/jsx/react-flow/xy.native.js +4 -3
- package/dist/jsx/react-flow/xy.native.js.map +1 -1
- package/dist/jsx/react-flow/xyPaths.mjs +3 -0
- package/dist/jsx/react-flow/xyPaths.mjs.map +1 -0
- package/dist/jsx/react-flow/xyPaths.native.js +34 -0
- package/dist/jsx/react-flow/xyPaths.native.js.map +1 -0
- package/package.json +6 -6
- package/src/edges/GraphEdge.tsx +18 -61
- package/src/edges/edgePath.ts +84 -0
- package/src/edges/edgeRoute.spec.ts +631 -0
- package/src/edges/edgeRoute.ts +980 -0
- package/src/edges/useEdgeObstacles.ts +71 -0
- package/src/react-flow/NodeGraphWeb.stories.tsx +80 -0
- package/src/react-flow/xy.ts +1 -3
- package/src/react-flow/xyPaths.ts +9 -0
- package/src/theme/graphVars.spec.ts +1 -0
- package/types/edges/GraphEdge.d.ts +1 -16
- package/types/edges/GraphEdge.d.ts.map +1 -1
- package/types/edges/edgePath.d.ts +25 -0
- package/types/edges/edgePath.d.ts.map +1 -0
- package/types/edges/edgeRoute.d.ts +77 -0
- package/types/edges/edgeRoute.d.ts.map +1 -0
- package/types/edges/useEdgeObstacles.d.ts +45 -0
- package/types/edges/useEdgeObstacles.d.ts.map +1 -0
- package/types/react-flow/xy.d.ts +2 -1
- package/types/react-flow/xy.d.ts.map +1 -1
- package/types/react-flow/xyPaths.d.ts +9 -0
- package/types/react-flow/xyPaths.d.ts.map +1 -0
|
@@ -0,0 +1,980 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps an edge out of every node box (FZ-9208).
|
|
3
|
+
*
|
|
4
|
+
* React Flow's path builders see the two port points and nothing else, so a
|
|
5
|
+
* backward edge (target left of its source) bends out and cuts back across
|
|
6
|
+
* whatever sits between, and a smoothstep's middle run lands on whichever node
|
|
7
|
+
* border happens to sit at the midpoint. Edges paint under nodes, so both read
|
|
8
|
+
* as a line that vanishes into one box and reappears out of another.
|
|
9
|
+
*
|
|
10
|
+
* The router keeps React Flow's own path whenever it stays `clearance` away
|
|
11
|
+
* from every box outside the port stubs, so the common forward edge looks
|
|
12
|
+
* exactly as before. Only a path that intrudes is replaced, by an orthogonal
|
|
13
|
+
* route found with A* over the gaps between the inflated boxes, drawn in the
|
|
14
|
+
* kind's own corner style.
|
|
15
|
+
*
|
|
16
|
+
* Pure: no DOM, no React, no `@xyflow`, so it runs in vitest under Node.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { Rect } from "../model/document";
|
|
20
|
+
import { buildSpatialHash } from "../interaction/spatialHash";
|
|
21
|
+
import type { SpatialHash } from "../interaction/spatialHash";
|
|
22
|
+
import type { EdgePathKind } from "./edgeStyle";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Flow units between an edge and any node box it passes. Main's rows are 24
|
|
26
|
+
* apart, so this leaves an 8-unit channel down the middle of the tightest gap
|
|
27
|
+
* the demo graphs lay out.
|
|
28
|
+
*/
|
|
29
|
+
export const edgeClearance = 8;
|
|
30
|
+
|
|
31
|
+
export interface EdgeObstacle extends Rect {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A graph edge by its two node ids. */
|
|
36
|
+
export interface EdgeLink {
|
|
37
|
+
readonly source: string;
|
|
38
|
+
readonly target: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface EdgeObstacles {
|
|
42
|
+
readonly boxes: readonly EdgeObstacle[];
|
|
43
|
+
readonly clearance: number;
|
|
44
|
+
/**
|
|
45
|
+
* Every edge in the graph. With them, edges that cross the same gap between
|
|
46
|
+
* two ranks get tracks of their own instead of all taking the gap's middle
|
|
47
|
+
* (FZ-9291). Without them each edge is routed as if it were alone.
|
|
48
|
+
*/
|
|
49
|
+
readonly links?: readonly EdgeLink[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type EdgeSide = "left" | "right" | "top" | "bottom";
|
|
53
|
+
|
|
54
|
+
export interface EdgeEnds {
|
|
55
|
+
readonly sourceX: number;
|
|
56
|
+
readonly sourceY: number;
|
|
57
|
+
readonly targetX: number;
|
|
58
|
+
readonly targetY: number;
|
|
59
|
+
readonly sourceSide: EdgeSide;
|
|
60
|
+
readonly targetSide: EdgeSide;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface Point {
|
|
64
|
+
readonly x: number;
|
|
65
|
+
readonly y: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const directions: Record<EdgeSide, Point> = {
|
|
69
|
+
left: { x: -1, y: 0 },
|
|
70
|
+
right: { x: 1, y: 0 },
|
|
71
|
+
top: { x: 0, y: -1 },
|
|
72
|
+
bottom: { x: 0, y: 1 },
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** Corner radius per path shape, so a detour still reads as its kind. */
|
|
76
|
+
const cornerRadius: Record<EdgePathKind, number> = {
|
|
77
|
+
bezier: 24,
|
|
78
|
+
smoothstep: 5,
|
|
79
|
+
step: 0,
|
|
80
|
+
straight: 0,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const indexes = new WeakMap<readonly EdgeObstacle[], SpatialHash>();
|
|
84
|
+
|
|
85
|
+
function indexFor(boxes: readonly EdgeObstacle[]): SpatialHash {
|
|
86
|
+
let index = indexes.get(boxes);
|
|
87
|
+
if (!index) {
|
|
88
|
+
index = buildSpatialHash(boxes.map((box) => ({ id: box.id, rect: box })));
|
|
89
|
+
indexes.set(boxes, index);
|
|
90
|
+
}
|
|
91
|
+
return index;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function boxesNear(obstacles: EdgeObstacles, area: Rect): EdgeObstacle[] {
|
|
95
|
+
const byId = new Map(obstacles.boxes.map((box) => [box.id, box]));
|
|
96
|
+
const found: EdgeObstacle[] = [];
|
|
97
|
+
for (const item of indexFor(obstacles.boxes).query(area)) {
|
|
98
|
+
const box = byId.get(item.id);
|
|
99
|
+
if (box) found.push(box);
|
|
100
|
+
}
|
|
101
|
+
return found;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function tokens(d: string): (string | number)[] {
|
|
105
|
+
const raw = d.match(/[MLQCZ]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? [];
|
|
106
|
+
return raw.map((token) => (/^[MLQCZ]$/i.test(token) ? token.toUpperCase() : Number(token)));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Points along an absolute M/L/Q/C path, the commands React Flow writes. */
|
|
110
|
+
export function samplePath(d: string): Point[] {
|
|
111
|
+
const list = tokens(d);
|
|
112
|
+
const points: Point[] = [];
|
|
113
|
+
let at: Point = { x: 0, y: 0 };
|
|
114
|
+
let command = "M";
|
|
115
|
+
let index = 0;
|
|
116
|
+
const take = (): number => {
|
|
117
|
+
const value = list[index];
|
|
118
|
+
index += 1;
|
|
119
|
+
return typeof value === "number" ? value : Number.NaN;
|
|
120
|
+
};
|
|
121
|
+
while (index < list.length) {
|
|
122
|
+
if (typeof list[index] === "string") {
|
|
123
|
+
command = list[index] as string;
|
|
124
|
+
index += 1;
|
|
125
|
+
if (command === "Z") continue;
|
|
126
|
+
}
|
|
127
|
+
if (command === "M") {
|
|
128
|
+
at = { x: take(), y: take() };
|
|
129
|
+
points.push(at);
|
|
130
|
+
command = "L";
|
|
131
|
+
} else if (command === "L") {
|
|
132
|
+
const to = { x: take(), y: take() };
|
|
133
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
134
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
135
|
+
const t = step / steps;
|
|
136
|
+
points.push({ x: at.x + (to.x - at.x) * t, y: at.y + (to.y - at.y) * t });
|
|
137
|
+
}
|
|
138
|
+
at = to;
|
|
139
|
+
} else if (command === "Q") {
|
|
140
|
+
const control = { x: take(), y: take() };
|
|
141
|
+
const to = { x: take(), y: take() };
|
|
142
|
+
for (let step = 1; step <= 8; step += 1) {
|
|
143
|
+
const t = step / 8;
|
|
144
|
+
const u = 1 - t;
|
|
145
|
+
points.push({
|
|
146
|
+
x: u * u * at.x + 2 * u * t * control.x + t * t * to.x,
|
|
147
|
+
y: u * u * at.y + 2 * u * t * control.y + t * t * to.y,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
at = to;
|
|
151
|
+
} else if (command === "C") {
|
|
152
|
+
const first = { x: take(), y: take() };
|
|
153
|
+
const second = { x: take(), y: take() };
|
|
154
|
+
const to = { x: take(), y: take() };
|
|
155
|
+
const steps = Math.max(16, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
156
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
157
|
+
const t = step / steps;
|
|
158
|
+
const u = 1 - t;
|
|
159
|
+
points.push({
|
|
160
|
+
x:
|
|
161
|
+
u * u * u * at.x +
|
|
162
|
+
3 * u * u * t * first.x +
|
|
163
|
+
3 * u * t * t * second.x +
|
|
164
|
+
t * t * t * to.x,
|
|
165
|
+
y:
|
|
166
|
+
u * u * u * at.y +
|
|
167
|
+
3 * u * u * t * first.y +
|
|
168
|
+
3 * u * t * t * second.y +
|
|
169
|
+
t * t * t * to.y,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
at = to;
|
|
173
|
+
} else {
|
|
174
|
+
return points;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return points;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function distanceToBox(point: Point, box: Rect): number {
|
|
181
|
+
const dx = Math.max(box.x - point.x, 0, point.x - (box.x + box.width));
|
|
182
|
+
const dy = Math.max(box.y - point.y, 0, point.y - (box.y + box.height));
|
|
183
|
+
return Math.hypot(dx, dy);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The box the port sits in or on, if any: a port is drawn on its own node. */
|
|
187
|
+
function ownBox(point: Point, boxes: readonly EdgeObstacle[]): EdgeObstacle | undefined {
|
|
188
|
+
let best: EdgeObstacle | undefined;
|
|
189
|
+
let bestDistance = 0.5;
|
|
190
|
+
for (const box of boxes) {
|
|
191
|
+
const distance = distanceToBox(point, box);
|
|
192
|
+
if (distance <= bestDistance) {
|
|
193
|
+
best = box;
|
|
194
|
+
bestDistance = distance;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return best;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** How far the port point sits inside its own frame, measured along its side. */
|
|
201
|
+
function depth(point: Point, side: EdgeSide, box: EdgeObstacle | undefined): number {
|
|
202
|
+
if (!box) return 0;
|
|
203
|
+
switch (side) {
|
|
204
|
+
case "right":
|
|
205
|
+
return Math.max(0, box.x + box.width - point.x);
|
|
206
|
+
case "left":
|
|
207
|
+
return Math.max(0, point.x - box.x);
|
|
208
|
+
case "bottom":
|
|
209
|
+
return Math.max(0, box.y + box.height - point.y);
|
|
210
|
+
case "top":
|
|
211
|
+
return Math.max(0, point.y - box.y);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function bounds(points: readonly Point[], pad: number): Rect {
|
|
216
|
+
let minX = Infinity;
|
|
217
|
+
let minY = Infinity;
|
|
218
|
+
let maxX = -Infinity;
|
|
219
|
+
let maxY = -Infinity;
|
|
220
|
+
for (const point of points) {
|
|
221
|
+
minX = Math.min(minX, point.x);
|
|
222
|
+
minY = Math.min(minY, point.y);
|
|
223
|
+
maxX = Math.max(maxX, point.x);
|
|
224
|
+
maxY = Math.max(maxY, point.y);
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
x: minX - pad,
|
|
228
|
+
y: minY - pad,
|
|
229
|
+
width: maxX - minX + pad * 2,
|
|
230
|
+
height: maxY - minY + pad * 2,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* True when a sample outside either port stub comes within `clearance` of a
|
|
236
|
+
* box. The stub is the stretch from the port to `clearance` past its own frame.
|
|
237
|
+
*/
|
|
238
|
+
export function pathIntrudes(d: string, ends: EdgeEnds, obstacles: EdgeObstacles): boolean {
|
|
239
|
+
const { clearance } = obstacles;
|
|
240
|
+
const from = { x: ends.sourceX, y: ends.sourceY };
|
|
241
|
+
const to = { x: ends.targetX, y: ends.targetY };
|
|
242
|
+
const points = samplePath(d);
|
|
243
|
+
if (points.length === 0) return false;
|
|
244
|
+
const near = boxesNear(obstacles, bounds(points, clearance));
|
|
245
|
+
const fromStub = depth(from, ends.sourceSide, ownBox(from, near)) + clearance;
|
|
246
|
+
const toStub = depth(to, ends.targetSide, ownBox(to, near)) + clearance;
|
|
247
|
+
for (const point of points) {
|
|
248
|
+
if (Math.hypot(point.x - from.x, point.y - from.y) <= fromStub) continue;
|
|
249
|
+
if (Math.hypot(point.x - to.x, point.y - to.y) <= toStub) continue;
|
|
250
|
+
for (const box of near) {
|
|
251
|
+
if (distanceToBox(point, box) < clearance - 0.25) return true;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
interface Inflated {
|
|
258
|
+
readonly left: number;
|
|
259
|
+
readonly right: number;
|
|
260
|
+
readonly top: number;
|
|
261
|
+
readonly bottom: number;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function inflate(box: Rect, by: number): Inflated {
|
|
265
|
+
return {
|
|
266
|
+
left: box.x - by,
|
|
267
|
+
right: box.x + box.width + by,
|
|
268
|
+
top: box.y - by,
|
|
269
|
+
bottom: box.y + box.height + by,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function strictlyInside(x: number, y: number, rect: Inflated): boolean {
|
|
274
|
+
return x > rect.left && x < rect.right && y > rect.top && y < rect.bottom;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Sorted unique values plus the midpoint of every gap, so a run can take the middle of a channel. */
|
|
278
|
+
function lines(values: number[]): number[] {
|
|
279
|
+
const sorted = [...new Set(values.map((value) => Math.round(value * 1000) / 1000))].sort(
|
|
280
|
+
(a, b) => a - b,
|
|
281
|
+
);
|
|
282
|
+
const out: number[] = [];
|
|
283
|
+
for (let index = 0; index < sorted.length; index += 1) {
|
|
284
|
+
out.push(sorted[index]);
|
|
285
|
+
const next = sorted[index + 1];
|
|
286
|
+
if (next !== undefined && next - sorted[index] > 1) out.push((sorted[index] + next) / 2);
|
|
287
|
+
}
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Exit point: `clearance` past the port's own frame, straight out of its side. */
|
|
292
|
+
function stubEnd(
|
|
293
|
+
point: Point,
|
|
294
|
+
side: EdgeSide,
|
|
295
|
+
box: EdgeObstacle | undefined,
|
|
296
|
+
clearance: number,
|
|
297
|
+
): Point {
|
|
298
|
+
const reach = depth(point, side, box) + clearance;
|
|
299
|
+
const dir = directions[side];
|
|
300
|
+
return { x: point.x + dir.x * reach, y: point.y + dir.y * reach };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
class Heap {
|
|
304
|
+
private readonly items: { key: number; cost: number }[] = [];
|
|
305
|
+
get size(): number {
|
|
306
|
+
return this.items.length;
|
|
307
|
+
}
|
|
308
|
+
push(key: number, cost: number): void {
|
|
309
|
+
const items = this.items;
|
|
310
|
+
items.push({ key, cost });
|
|
311
|
+
let index = items.length - 1;
|
|
312
|
+
while (index > 0) {
|
|
313
|
+
const parent = (index - 1) >> 1;
|
|
314
|
+
if (items[parent].cost <= items[index].cost) break;
|
|
315
|
+
[items[parent], items[index]] = [items[index], items[parent]];
|
|
316
|
+
index = parent;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
pop(): { key: number; cost: number } | undefined {
|
|
320
|
+
const items = this.items;
|
|
321
|
+
const top = items[0];
|
|
322
|
+
const last = items.pop();
|
|
323
|
+
if (items.length > 0 && last) {
|
|
324
|
+
items[0] = last;
|
|
325
|
+
let index = 0;
|
|
326
|
+
for (;;) {
|
|
327
|
+
const left = index * 2 + 1;
|
|
328
|
+
const right = left + 1;
|
|
329
|
+
let smallest = index;
|
|
330
|
+
if (left < items.length && items[left].cost < items[smallest].cost) smallest = left;
|
|
331
|
+
if (right < items.length && items[right].cost < items[smallest].cost) smallest = right;
|
|
332
|
+
if (smallest === index) break;
|
|
333
|
+
[items[smallest], items[index]] = [items[index], items[smallest]];
|
|
334
|
+
index = smallest;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return top;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const stepX = [1, 0, -1, 0];
|
|
342
|
+
const stepY = [0, 1, 0, -1];
|
|
343
|
+
|
|
344
|
+
function directionIndex(side: EdgeSide): number {
|
|
345
|
+
const dir = directions[side];
|
|
346
|
+
return stepX.findIndex((dx, index) => dx === dir.x && stepY[index] === dir.y);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The orthogonal route from one stub end to the other through `boxes`
|
|
351
|
+
* inflated by `clearance`, fewest turns weighed against length. `null` when
|
|
352
|
+
* the region holds no such route.
|
|
353
|
+
*/
|
|
354
|
+
function orthogonalRoute(
|
|
355
|
+
start: Point,
|
|
356
|
+
startDirection: number,
|
|
357
|
+
goal: Point,
|
|
358
|
+
goalDirection: number,
|
|
359
|
+
boxes: readonly EdgeObstacle[],
|
|
360
|
+
clearance: number,
|
|
361
|
+
area: Rect,
|
|
362
|
+
): Point[] | null {
|
|
363
|
+
const rects = boxes.map((box) => inflate(box, clearance));
|
|
364
|
+
const xs = lines([
|
|
365
|
+
start.x,
|
|
366
|
+
goal.x,
|
|
367
|
+
area.x,
|
|
368
|
+
area.x + area.width,
|
|
369
|
+
...rects.flatMap((r) => [r.left, r.right]),
|
|
370
|
+
]);
|
|
371
|
+
const ys = lines([
|
|
372
|
+
start.y,
|
|
373
|
+
goal.y,
|
|
374
|
+
area.y,
|
|
375
|
+
area.y + area.height,
|
|
376
|
+
...rects.flatMap((r) => [r.top, r.bottom]),
|
|
377
|
+
]);
|
|
378
|
+
const columns = xs.length;
|
|
379
|
+
const rows = ys.length;
|
|
380
|
+
const startX = xs.indexOf(Math.round(start.x * 1000) / 1000);
|
|
381
|
+
const startY = ys.indexOf(Math.round(start.y * 1000) / 1000);
|
|
382
|
+
const goalX = xs.indexOf(Math.round(goal.x * 1000) / 1000);
|
|
383
|
+
const goalY = ys.indexOf(Math.round(goal.y * 1000) / 1000);
|
|
384
|
+
if (startX < 0 || startY < 0 || goalX < 0 || goalY < 0) return null;
|
|
385
|
+
|
|
386
|
+
const free = (ix: number, iy: number): boolean =>
|
|
387
|
+
rects.every((rect) => !strictlyInside(xs[ix], ys[iy], rect));
|
|
388
|
+
const open = (ax: number, ay: number, bx: number, by: number): boolean => {
|
|
389
|
+
const minX = Math.min(xs[ax], xs[bx]);
|
|
390
|
+
const maxX = Math.max(xs[ax], xs[bx]);
|
|
391
|
+
const minY = Math.min(ys[ay], ys[by]);
|
|
392
|
+
const maxY = Math.max(ys[ay], ys[by]);
|
|
393
|
+
return rects.every((rect) => {
|
|
394
|
+
if (minY === maxY)
|
|
395
|
+
return !(minY > rect.top && minY < rect.bottom && maxX > rect.left && minX < rect.right);
|
|
396
|
+
return !(minX > rect.left && minX < rect.right && maxY > rect.top && minY < rect.bottom);
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
if (!free(startX, startY) || !free(goalX, goalY)) return null;
|
|
400
|
+
|
|
401
|
+
const turn = clearance * 4;
|
|
402
|
+
const key = (ix: number, iy: number, dir: number) => (iy * columns + ix) * 4 + dir;
|
|
403
|
+
const best = new Map<number, number>();
|
|
404
|
+
const from = new Map<number, number>();
|
|
405
|
+
const heap = new Heap();
|
|
406
|
+
const heuristic = (ix: number, iy: number) =>
|
|
407
|
+
Math.abs(xs[ix] - goal.x) + Math.abs(ys[iy] - goal.y);
|
|
408
|
+
const first = key(startX, startY, startDirection);
|
|
409
|
+
best.set(first, 0);
|
|
410
|
+
heap.push(first, heuristic(startX, startY));
|
|
411
|
+
let reached: number | undefined;
|
|
412
|
+
let budget = 200_000;
|
|
413
|
+
while (heap.size > 0 && budget > 0) {
|
|
414
|
+
budget -= 1;
|
|
415
|
+
const current = heap.pop();
|
|
416
|
+
if (!current) break;
|
|
417
|
+
const dir = current.key % 4;
|
|
418
|
+
const cell = (current.key - dir) / 4;
|
|
419
|
+
const ix = cell % columns;
|
|
420
|
+
const iy = (cell - ix) / columns;
|
|
421
|
+
const cost = best.get(current.key) ?? Infinity;
|
|
422
|
+
if (current.cost - heuristic(ix, iy) > cost + 1e-6) continue;
|
|
423
|
+
if (ix === goalX && iy === goalY) {
|
|
424
|
+
reached = current.key;
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
for (let next = 0; next < 4; next += 1) {
|
|
428
|
+
if (next === (dir + 2) % 4) continue;
|
|
429
|
+
const nx = ix + stepX[next];
|
|
430
|
+
const ny = iy + stepY[next];
|
|
431
|
+
if (nx < 0 || ny < 0 || nx >= columns || ny >= rows) continue;
|
|
432
|
+
if (!free(nx, ny) || !open(ix, iy, nx, ny)) continue;
|
|
433
|
+
const atGoal = nx === goalX && ny === goalY;
|
|
434
|
+
const length = Math.abs(xs[nx] - xs[ix]) + Math.abs(ys[ny] - ys[iy]);
|
|
435
|
+
const bend = (next === dir ? 0 : turn) + (atGoal && next !== goalDirection ? turn : 0);
|
|
436
|
+
const nextKey = key(nx, ny, next);
|
|
437
|
+
const nextCost = cost + length + bend;
|
|
438
|
+
if (nextCost < (best.get(nextKey) ?? Infinity)) {
|
|
439
|
+
best.set(nextKey, nextCost);
|
|
440
|
+
from.set(nextKey, current.key);
|
|
441
|
+
heap.push(nextKey, nextCost + heuristic(nx, ny));
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (reached === undefined) return null;
|
|
446
|
+
const route: Point[] = [];
|
|
447
|
+
for (let at: number | undefined = reached; at !== undefined; at = from.get(at)) {
|
|
448
|
+
const cell = (at - (at % 4)) / 4;
|
|
449
|
+
const ix = cell % columns;
|
|
450
|
+
route.push({ x: xs[ix], y: ys[(cell - ix) / columns] });
|
|
451
|
+
}
|
|
452
|
+
return route.reverse();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function simplify(points: readonly Point[]): Point[] {
|
|
456
|
+
const out: Point[] = [];
|
|
457
|
+
for (const point of points) {
|
|
458
|
+
const last = out[out.length - 1];
|
|
459
|
+
if (last && Math.abs(last.x - point.x) < 1e-6 && Math.abs(last.y - point.y) < 1e-6) continue;
|
|
460
|
+
const before = out[out.length - 2];
|
|
461
|
+
if (before && last) {
|
|
462
|
+
const sameX = Math.abs(before.x - last.x) < 1e-6 && Math.abs(last.x - point.x) < 1e-6;
|
|
463
|
+
const sameY = Math.abs(before.y - last.y) < 1e-6 && Math.abs(last.y - point.y) < 1e-6;
|
|
464
|
+
if (sameX || sameY) out.pop();
|
|
465
|
+
}
|
|
466
|
+
out.push(point);
|
|
467
|
+
}
|
|
468
|
+
return out;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function format(value: number): string {
|
|
472
|
+
return String(Math.round(value * 1000) / 1000);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
interface Corner {
|
|
476
|
+
readonly enter: Point;
|
|
477
|
+
readonly corner: Point;
|
|
478
|
+
readonly leave: Point;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** Corner `index` of `points` rounded by `radius`, capped at half of either leg. */
|
|
482
|
+
function roundCorner(points: readonly Point[], index: number, radius: number): Corner | null {
|
|
483
|
+
const before = points[index - 1];
|
|
484
|
+
const corner = points[index];
|
|
485
|
+
const after = points[index + 1];
|
|
486
|
+
const inLength = Math.hypot(corner.x - before.x, corner.y - before.y);
|
|
487
|
+
const outLength = Math.hypot(after.x - corner.x, after.y - corner.y);
|
|
488
|
+
const r = Math.min(radius, inLength / 2, outLength / 2);
|
|
489
|
+
if (r <= 0) return null;
|
|
490
|
+
return {
|
|
491
|
+
enter: {
|
|
492
|
+
x: corner.x - ((corner.x - before.x) / inLength) * r,
|
|
493
|
+
y: corner.y - ((corner.y - before.y) / inLength) * r,
|
|
494
|
+
},
|
|
495
|
+
corner,
|
|
496
|
+
leave: {
|
|
497
|
+
x: corner.x + ((after.x - corner.x) / outLength) * r,
|
|
498
|
+
y: corner.y + ((after.y - corner.y) / outLength) * r,
|
|
499
|
+
},
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function cornerPath({ enter, corner, leave }: Corner): string {
|
|
504
|
+
return `M${format(enter.x)},${format(enter.y)} Q${format(corner.x)},${format(corner.y)} ${format(leave.x)},${format(leave.y)}`;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* The largest radius up to `radius` that rounds corner `index` without the
|
|
509
|
+
* curve coming inside the clearance, so a tight corridor gets a tighter bend.
|
|
510
|
+
*/
|
|
511
|
+
function fittedRadius(
|
|
512
|
+
points: readonly Point[],
|
|
513
|
+
index: number,
|
|
514
|
+
radius: number,
|
|
515
|
+
ends: EdgeEnds,
|
|
516
|
+
obstacles: EdgeObstacles,
|
|
517
|
+
): number {
|
|
518
|
+
const fits = (r: number): boolean => {
|
|
519
|
+
const corner = roundCorner(points, index, r);
|
|
520
|
+
return !corner || !pathIntrudes(cornerPath(corner), ends, obstacles);
|
|
521
|
+
};
|
|
522
|
+
if (fits(radius)) return radius;
|
|
523
|
+
let low = 0;
|
|
524
|
+
let high = radius;
|
|
525
|
+
for (let step = 0; step < 12; step += 1) {
|
|
526
|
+
const middle = (low + high) / 2;
|
|
527
|
+
if (fits(middle)) low = middle;
|
|
528
|
+
else high = middle;
|
|
529
|
+
}
|
|
530
|
+
return low;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** A polyline with corner `index` rounded by `radii[index]`. */
|
|
534
|
+
function polylinePath(points: readonly Point[], radii: readonly number[]): string {
|
|
535
|
+
let d = `M${format(points[0].x)},${format(points[0].y)}`;
|
|
536
|
+
for (let index = 1; index < points.length - 1; index += 1) {
|
|
537
|
+
const corner = roundCorner(points, index, radii[index] ?? 0);
|
|
538
|
+
if (!corner) {
|
|
539
|
+
d += ` L${format(points[index].x)},${format(points[index].y)}`;
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
d += ` L${format(corner.enter.x)},${format(corner.enter.y)} Q${format(corner.corner.x)},${format(corner.corner.y)} ${format(corner.leave.x)},${format(corner.leave.y)}`;
|
|
543
|
+
}
|
|
544
|
+
const last = points[points.length - 1];
|
|
545
|
+
return `${d} L${format(last.x)},${format(last.y)}`;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function midpoint(points: readonly Point[]): Point {
|
|
549
|
+
let total = 0;
|
|
550
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
551
|
+
total += Math.hypot(
|
|
552
|
+
points[index].x - points[index - 1].x,
|
|
553
|
+
points[index].y - points[index - 1].y,
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
let left = total / 2;
|
|
557
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
558
|
+
const a = points[index - 1];
|
|
559
|
+
const b = points[index];
|
|
560
|
+
const length = Math.hypot(b.x - a.x, b.y - a.y);
|
|
561
|
+
if (length >= left && length > 0) {
|
|
562
|
+
const t = left / length;
|
|
563
|
+
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
|
|
564
|
+
}
|
|
565
|
+
left -= length;
|
|
566
|
+
}
|
|
567
|
+
return points[0];
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* A route around the boxes, as `[d, labelX, labelY]`, or `null` when there is
|
|
572
|
+
* none in reach, in which case the caller keeps React Flow's path.
|
|
573
|
+
*/
|
|
574
|
+
export function routeAroundBoxes(
|
|
575
|
+
path: EdgePathKind,
|
|
576
|
+
ends: EdgeEnds,
|
|
577
|
+
obstacles: EdgeObstacles,
|
|
578
|
+
): [d: string, labelX: number, labelY: number] | null {
|
|
579
|
+
const { clearance } = obstacles;
|
|
580
|
+
const from = { x: ends.sourceX, y: ends.sourceY };
|
|
581
|
+
const to = { x: ends.targetX, y: ends.targetY };
|
|
582
|
+
const largest = obstacles.boxes.reduce((size, box) => Math.max(size, box.width, box.height), 0);
|
|
583
|
+
for (const reach of [1, 3]) {
|
|
584
|
+
const area = bounds([from, to], (largest + clearance * 2) * reach);
|
|
585
|
+
const near = boxesNear(obstacles, area);
|
|
586
|
+
const start = stubEnd(from, ends.sourceSide, ownBox(from, near), clearance);
|
|
587
|
+
const goal = stubEnd(to, ends.targetSide, ownBox(to, near), clearance);
|
|
588
|
+
const inner = orthogonalRoute(
|
|
589
|
+
start,
|
|
590
|
+
directionIndex(ends.sourceSide),
|
|
591
|
+
goal,
|
|
592
|
+
(directionIndex(ends.targetSide) + 2) % 4,
|
|
593
|
+
near,
|
|
594
|
+
clearance,
|
|
595
|
+
area,
|
|
596
|
+
);
|
|
597
|
+
if (!inner) continue;
|
|
598
|
+
const points = simplify([from, ...inner, to]);
|
|
599
|
+
const label = midpoint(points);
|
|
600
|
+
// A rounded corner cuts inside its turn, toward the box it turns around,
|
|
601
|
+
// so each corner gets the largest bend that keeps the clearance.
|
|
602
|
+
const radius = cornerRadius[path];
|
|
603
|
+
const radii = points.map((_, index) =>
|
|
604
|
+
index === 0 || index === points.length - 1
|
|
605
|
+
? 0
|
|
606
|
+
: fittedRadius(points, index, radius, ends, obstacles),
|
|
607
|
+
);
|
|
608
|
+
return [polylinePath(points, radii), label.x, label.y];
|
|
609
|
+
}
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* What one track across a gap carries (FZ-9291). `target`: every edge into
|
|
615
|
+
* the same node shares a trunk, so a run leads to exactly one node. `edge`:
|
|
616
|
+
* every edge gets a lane of its own.
|
|
617
|
+
*/
|
|
618
|
+
type TrackSharing = "target" | "edge";
|
|
619
|
+
|
|
620
|
+
const trackSharing: TrackSharing = "target";
|
|
621
|
+
|
|
622
|
+
/** Edge coordinates turned so the source side points down: `u` across the gap, `v` from source to target. */
|
|
623
|
+
interface Frame {
|
|
624
|
+
readonly local: (point: Point) => Point;
|
|
625
|
+
readonly world: (point: Point) => Point;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const frames: Record<EdgeSide, Frame> = {
|
|
629
|
+
bottom: {
|
|
630
|
+
local: ({ x, y }) => ({ x, y }),
|
|
631
|
+
world: ({ x, y }) => ({ x, y }),
|
|
632
|
+
},
|
|
633
|
+
top: {
|
|
634
|
+
local: ({ x, y }) => ({ x, y: -y }),
|
|
635
|
+
world: ({ x, y }) => ({ x, y: -y }),
|
|
636
|
+
},
|
|
637
|
+
right: {
|
|
638
|
+
local: ({ x, y }) => ({ x: y, y: x }),
|
|
639
|
+
world: ({ x, y }) => ({ x: y, y: x }),
|
|
640
|
+
},
|
|
641
|
+
left: {
|
|
642
|
+
local: ({ x, y }) => ({ x: y, y: -x }),
|
|
643
|
+
world: ({ x, y }) => ({ x: -y, y: x }),
|
|
644
|
+
},
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
const opposite: Record<EdgeSide, EdgeSide> = {
|
|
648
|
+
left: "right",
|
|
649
|
+
right: "left",
|
|
650
|
+
top: "bottom",
|
|
651
|
+
bottom: "top",
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
interface Span {
|
|
655
|
+
readonly u0: number;
|
|
656
|
+
readonly u1: number;
|
|
657
|
+
readonly v0: number;
|
|
658
|
+
readonly v1: number;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function localSpan(box: Rect, frame: Frame): Span {
|
|
662
|
+
const a = frame.local({ x: box.x, y: box.y });
|
|
663
|
+
const b = frame.local({ x: box.x + box.width, y: box.y + box.height });
|
|
664
|
+
return {
|
|
665
|
+
u0: Math.min(a.x, b.x),
|
|
666
|
+
u1: Math.max(a.x, b.x),
|
|
667
|
+
v0: Math.min(a.y, b.y),
|
|
668
|
+
v1: Math.max(a.y, b.y),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function trackKey(sharing: TrackSharing, link: EdgeLink): string {
|
|
673
|
+
return sharing === "target" ? link.target : `${link.source}\u0000${link.target}`;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
interface Rank {
|
|
677
|
+
readonly v0: number;
|
|
678
|
+
readonly v1: number;
|
|
679
|
+
readonly spans: Span[];
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/** Boxes whose `v` extents overlap form one rank, ordered from source side to target side. */
|
|
683
|
+
function ranksOf(spans: readonly Span[]): Rank[] {
|
|
684
|
+
const sorted = [...spans].sort((a, b) => a.v0 - b.v0);
|
|
685
|
+
const ranks: { v0: number; v1: number; spans: Span[] }[] = [];
|
|
686
|
+
for (const span of sorted) {
|
|
687
|
+
const last = ranks[ranks.length - 1];
|
|
688
|
+
if (last && span.v0 < last.v1) {
|
|
689
|
+
last.v1 = Math.max(last.v1, span.v1);
|
|
690
|
+
last.spans.push(span);
|
|
691
|
+
} else ranks.push({ v0: span.v0, v1: span.v1, spans: [span] });
|
|
692
|
+
}
|
|
693
|
+
for (const rank of ranks) rank.spans.sort((a, b) => a.u0 - b.u0);
|
|
694
|
+
return ranks;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/** The open `u` intervals a vertical run can take through a rank and keep the clearance. */
|
|
698
|
+
function channels(rank: Rank, clearance: number): [number, number][] {
|
|
699
|
+
const open: [number, number][] = [];
|
|
700
|
+
let from = Number.NEGATIVE_INFINITY;
|
|
701
|
+
for (const span of rank.spans) {
|
|
702
|
+
const to = span.u0 - clearance;
|
|
703
|
+
if (to > from) open.push([from, to]);
|
|
704
|
+
from = Math.max(from, span.u1 + clearance);
|
|
705
|
+
}
|
|
706
|
+
open.push([from, Number.POSITIVE_INFINITY]);
|
|
707
|
+
return open;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Where a link crosses one rank it skips: held at `goal` when its channel
|
|
712
|
+
* reaches the target's line, which saves a bend, else the channel nearest
|
|
713
|
+
* `ideal`. The source's line isn't held: a port can sit off its box's centre,
|
|
714
|
+
* so holding it would put a jog at the source.
|
|
715
|
+
*/
|
|
716
|
+
function virtualPoint(
|
|
717
|
+
open: readonly [number, number][],
|
|
718
|
+
ideal: number,
|
|
719
|
+
goal: number,
|
|
720
|
+
): { u: number; channel: number } {
|
|
721
|
+
const held = open.findIndex(([lo, hi]) => lo <= goal && goal <= hi);
|
|
722
|
+
if (held >= 0) return { u: goal, channel: held };
|
|
723
|
+
let best = { u: ideal, channel: 0, cost: Number.POSITIVE_INFINITY };
|
|
724
|
+
open.forEach(([lo, hi], channel) => {
|
|
725
|
+
const u = Math.min(Math.max(ideal, lo), hi);
|
|
726
|
+
const cost = Math.abs(u - ideal);
|
|
727
|
+
if (cost < best.cost) best = { u, channel, cost };
|
|
728
|
+
});
|
|
729
|
+
return { u: best.u, channel: best.channel };
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/** A link's way across the gaps, in the source side's frame: one `v` per gap and the `u` it holds through each rank it skips. */
|
|
733
|
+
interface Course {
|
|
734
|
+
readonly vs: number[];
|
|
735
|
+
readonly us: number[];
|
|
736
|
+
readonly open: [number, number][];
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
interface Group {
|
|
740
|
+
readonly key: string;
|
|
741
|
+
readonly lone: boolean;
|
|
742
|
+
u0: number;
|
|
743
|
+
u1: number;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function linkKey(link: EdgeLink): string {
|
|
747
|
+
return `${link.source}\u0000${link.target}`;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Every link that shares a gap with another, or skips a rank, with its course.
|
|
752
|
+
* A link that crosses its gap alone gets none and keeps the ordinary route.
|
|
753
|
+
*
|
|
754
|
+
* Like a layered layout's dummy nodes, a link that skips ranks gets a virtual
|
|
755
|
+
* point in each rank it crosses, in the open channel between that rank's
|
|
756
|
+
* boxes, so it never runs through a node; each piece then crosses one gap
|
|
757
|
+
* like any other edge. In each gap, groups whose spans overlap are one
|
|
758
|
+
* conflict set; interval colouring gives the fewest tracks, evenly spaced.
|
|
759
|
+
*/
|
|
760
|
+
function planTracks(
|
|
761
|
+
links: readonly EdgeLink[],
|
|
762
|
+
boxes: readonly EdgeObstacle[],
|
|
763
|
+
side: EdgeSide,
|
|
764
|
+
clearance: number,
|
|
765
|
+
sharing: TrackSharing,
|
|
766
|
+
): Map<string, Course> {
|
|
767
|
+
const frame = frames[side];
|
|
768
|
+
const spans = new Map(boxes.map((box) => [box.id, localSpan(box, frame)]));
|
|
769
|
+
const ranks = ranksOf([...spans.values()]);
|
|
770
|
+
const rankOf = new Map<Span, number>();
|
|
771
|
+
ranks.forEach((rank, index) => {
|
|
772
|
+
for (const span of rank.spans) rankOf.set(span, index);
|
|
773
|
+
});
|
|
774
|
+
const open = ranks.map((rank) => channels(rank, clearance));
|
|
775
|
+
const gaps = new Map<number, Map<string, Group>>();
|
|
776
|
+
const pieces = new Map<string, { gap: number; key: string }[]>();
|
|
777
|
+
const passing: { link: string; rank: number; slot: number; u: number; channel: number }[] = [];
|
|
778
|
+
const virtual = new Map<string, number[]>();
|
|
779
|
+
const channelOf = new Map<string, [number, number][]>();
|
|
780
|
+
for (const link of links) {
|
|
781
|
+
const source = spans.get(link.source);
|
|
782
|
+
const target = spans.get(link.target);
|
|
783
|
+
if (!source || !target) continue;
|
|
784
|
+
const rs = rankOf.get(source)!;
|
|
785
|
+
const rt = rankOf.get(target)!;
|
|
786
|
+
if (rt <= rs) continue;
|
|
787
|
+
const id = linkKey(link);
|
|
788
|
+
if (pieces.has(id)) continue;
|
|
789
|
+
const sourceU = (source.u0 + source.u1) / 2;
|
|
790
|
+
const targetU = (target.u0 + target.u1) / 2;
|
|
791
|
+
const us = [sourceU];
|
|
792
|
+
for (let rank = rs + 1; rank < rt; rank += 1) {
|
|
793
|
+
const ideal = sourceU + ((targetU - sourceU) * (rank - rs)) / (rt - rs);
|
|
794
|
+
const at = virtualPoint(open[rank], ideal, targetU);
|
|
795
|
+
us.push(at.u);
|
|
796
|
+
passing.push({ link: id, rank, slot: us.length - 1, ...at });
|
|
797
|
+
}
|
|
798
|
+
us.push(targetU);
|
|
799
|
+
virtual.set(id, us);
|
|
800
|
+
channelOf.set(
|
|
801
|
+
id,
|
|
802
|
+
passing.filter((pass) => pass.link === id).map((pass) => open[pass.rank][pass.channel]),
|
|
803
|
+
);
|
|
804
|
+
const own: { gap: number; key: string }[] = [];
|
|
805
|
+
for (let gap = rs; gap < rt; gap += 1) {
|
|
806
|
+
const last = gap === rt - 1;
|
|
807
|
+
const key = last ? trackKey(sharing, link) : `${id}\u0000${gap}`;
|
|
808
|
+
own.push({ gap, key });
|
|
809
|
+
let groups = gaps.get(gap);
|
|
810
|
+
if (!groups) {
|
|
811
|
+
groups = new Map();
|
|
812
|
+
gaps.set(gap, groups);
|
|
813
|
+
}
|
|
814
|
+
const a = us[gap - rs];
|
|
815
|
+
const b = us[gap - rs + 1];
|
|
816
|
+
const group = groups.get(key);
|
|
817
|
+
if (group) {
|
|
818
|
+
group.u0 = Math.min(group.u0, a, b);
|
|
819
|
+
group.u1 = Math.max(group.u1, a, b);
|
|
820
|
+
} else groups.set(key, { key, lone: rt - rs === 1, u0: Math.min(a, b), u1: Math.max(a, b) });
|
|
821
|
+
}
|
|
822
|
+
pieces.set(id, own);
|
|
823
|
+
}
|
|
824
|
+
// Links through one channel of one rank take evenly spaced lanes in it.
|
|
825
|
+
const byChannel = new Map<string, typeof passing>();
|
|
826
|
+
for (const pass of passing) {
|
|
827
|
+
const key = `${pass.rank}:${pass.channel}`;
|
|
828
|
+
byChannel.set(key, [...(byChannel.get(key) ?? []), pass]);
|
|
829
|
+
}
|
|
830
|
+
for (const group of byChannel.values()) {
|
|
831
|
+
if (group.length < 2) continue;
|
|
832
|
+
const [lo, hi] = open[group[0].rank][group[0].channel];
|
|
833
|
+
const sorted = [...group].sort((a, b) => a.u - b.u || a.link.localeCompare(b.link));
|
|
834
|
+
const crowded = sorted.some(
|
|
835
|
+
(pass, index) => index > 0 && pass.u - sorted[index - 1].u < clearance,
|
|
836
|
+
);
|
|
837
|
+
if (!crowded) continue;
|
|
838
|
+
const width =
|
|
839
|
+
Number.isFinite(lo) && Number.isFinite(hi) ? hi - lo : clearance * (group.length + 1);
|
|
840
|
+
const base = Number.isFinite(lo) ? lo : Number.isFinite(hi) ? hi - width : sorted[0].u;
|
|
841
|
+
const pitch = width / (group.length + 1);
|
|
842
|
+
sorted.forEach((pass, index) => {
|
|
843
|
+
virtual.get(pass.link)![pass.slot] = base + pitch * (index + 1);
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
const levels = new Map<string, number>();
|
|
847
|
+
for (const [gap, groups] of gaps) {
|
|
848
|
+
const lo = ranks[gap].v1 + clearance;
|
|
849
|
+
const hi = ranks[gap + 1].v0 - clearance;
|
|
850
|
+
if (lo >= hi) continue;
|
|
851
|
+
const list = [...groups.values()];
|
|
852
|
+
const parent = list.map((_, index) => index);
|
|
853
|
+
const root = (index: number): number => {
|
|
854
|
+
let at = index;
|
|
855
|
+
while (parent[at] !== at) at = parent[at];
|
|
856
|
+
return at;
|
|
857
|
+
};
|
|
858
|
+
for (let a = 0; a < list.length; a += 1)
|
|
859
|
+
for (let b = a + 1; b < list.length; b += 1)
|
|
860
|
+
if (list[a].u0 <= list[b].u1 + clearance && list[b].u0 <= list[a].u1 + clearance)
|
|
861
|
+
parent[root(b)] = root(a);
|
|
862
|
+
const sets = new Map<number, Group[]>();
|
|
863
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
864
|
+
const at = root(index);
|
|
865
|
+
sets.set(at, [...(sets.get(at) ?? []), list[index]]);
|
|
866
|
+
}
|
|
867
|
+
for (const members of sets.values()) {
|
|
868
|
+
const sorted = [...members].sort((a, b) => a.u0 - b.u0 || a.key.localeCompare(b.key));
|
|
869
|
+
const ends: number[] = [];
|
|
870
|
+
const lane = new Map<string, number>();
|
|
871
|
+
for (const group of sorted) {
|
|
872
|
+
let free = ends.findIndex((end) => end + clearance < group.u0);
|
|
873
|
+
if (free < 0) {
|
|
874
|
+
free = ends.length;
|
|
875
|
+
ends.push(group.u1);
|
|
876
|
+
} else ends[free] = group.u1;
|
|
877
|
+
lane.set(group.key, free);
|
|
878
|
+
}
|
|
879
|
+
if (ends.length < 2 && members.every((group) => group.lone)) continue;
|
|
880
|
+
const pitch = (hi - lo) / (ends.length + 1);
|
|
881
|
+
for (const [key, index] of lane) levels.set(`${gap}\u0000${key}`, lo + pitch * (index + 1));
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
const courses = new Map<string, Course>();
|
|
885
|
+
for (const [id, own] of pieces) {
|
|
886
|
+
const vs = own.map(({ gap, key }) => levels.get(`${gap}\u0000${key}`));
|
|
887
|
+
if (vs.some((v) => v === undefined)) continue;
|
|
888
|
+
courses.set(id, {
|
|
889
|
+
vs: vs as number[],
|
|
890
|
+
us: virtual.get(id)!.slice(1, -1),
|
|
891
|
+
open: channelOf.get(id) ?? [],
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
return courses;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const plans = new WeakMap<
|
|
898
|
+
readonly EdgeLink[],
|
|
899
|
+
WeakMap<readonly EdgeObstacle[], Map<string, Map<string, Course>>>
|
|
900
|
+
>();
|
|
901
|
+
|
|
902
|
+
function tracksFor(obstacles: EdgeObstacles, side: EdgeSide): Map<string, Course> | undefined {
|
|
903
|
+
const { links, boxes, clearance } = obstacles;
|
|
904
|
+
if (!links || links.length === 0) return undefined;
|
|
905
|
+
let byBoxes = plans.get(links);
|
|
906
|
+
if (!byBoxes) {
|
|
907
|
+
byBoxes = new WeakMap();
|
|
908
|
+
plans.set(links, byBoxes);
|
|
909
|
+
}
|
|
910
|
+
let bySide = byBoxes.get(boxes);
|
|
911
|
+
if (!bySide) {
|
|
912
|
+
bySide = new Map();
|
|
913
|
+
byBoxes.set(boxes, bySide);
|
|
914
|
+
}
|
|
915
|
+
const key = `${side}:${clearance}`;
|
|
916
|
+
let tracks = bySide.get(key);
|
|
917
|
+
if (!tracks) {
|
|
918
|
+
tracks = planTracks(links, boxes, side, clearance, trackSharing);
|
|
919
|
+
bySide.set(key, tracks);
|
|
920
|
+
}
|
|
921
|
+
return tracks;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* A step or smoothstep edge on its own tracks across the gaps it crosses, as
|
|
926
|
+
* `[d, labelX, labelY]`, through a virtual point in each rank it skips.
|
|
927
|
+
* `null` when the edge crosses its gap alone, has no course, or a run would
|
|
928
|
+
* come inside the clearance, in which case the caller routes it as before.
|
|
929
|
+
*/
|
|
930
|
+
export function routeOnTrack(
|
|
931
|
+
path: EdgePathKind,
|
|
932
|
+
ends: EdgeEnds,
|
|
933
|
+
obstacles: EdgeObstacles,
|
|
934
|
+
): [d: string, labelX: number, labelY: number] | null {
|
|
935
|
+
if (path !== "smoothstep" && path !== "step") return null;
|
|
936
|
+
if (ends.targetSide !== opposite[ends.sourceSide]) return null;
|
|
937
|
+
const courses = tracksFor(obstacles, ends.sourceSide);
|
|
938
|
+
if (!courses || courses.size === 0) return null;
|
|
939
|
+
const from = { x: ends.sourceX, y: ends.sourceY };
|
|
940
|
+
const to = { x: ends.targetX, y: ends.targetY };
|
|
941
|
+
const source = ownBox(from, boxesNear(obstacles, bounds([from], 1)));
|
|
942
|
+
const target = ownBox(to, boxesNear(obstacles, bounds([to], 1)));
|
|
943
|
+
if (!source || !target) return null;
|
|
944
|
+
const course = courses.get(linkKey({ source: source.id, target: target.id }));
|
|
945
|
+
if (!course) return null;
|
|
946
|
+
const frame = frames[ends.sourceSide];
|
|
947
|
+
const a = frame.local(from);
|
|
948
|
+
const b = frame.local(to);
|
|
949
|
+
const { vs, us } = course;
|
|
950
|
+
const { clearance } = obstacles;
|
|
951
|
+
if (!(a.y < vs[0] && vs[vs.length - 1] < b.y)) return null;
|
|
952
|
+
if (us.length === 0 && Math.abs(a.x - b.x) < 0.5) return null;
|
|
953
|
+
// A port can sit off its box's centre. A virtual point planned a hair away
|
|
954
|
+
// from the port's own line takes that line when its channel allows, so the
|
|
955
|
+
// edge doesn't jog just before it leaves or lands.
|
|
956
|
+
const snapped = us.map((u, index) => {
|
|
957
|
+
const [lo, hi] = course.open[index] ?? [u, u];
|
|
958
|
+
for (const line of [index === us.length - 1 ? b.x : undefined, index === 0 ? a.x : undefined])
|
|
959
|
+
if (line !== undefined && Math.abs(line - u) < clearance * 2 && lo <= line && line <= hi)
|
|
960
|
+
return line;
|
|
961
|
+
return u;
|
|
962
|
+
});
|
|
963
|
+
const local = [a];
|
|
964
|
+
const holds = [a.x, ...snapped, b.x];
|
|
965
|
+
vs.forEach((v, gap) => {
|
|
966
|
+
local.push({ x: holds[gap], y: v }, { x: holds[gap + 1], y: v });
|
|
967
|
+
});
|
|
968
|
+
local.push(b);
|
|
969
|
+
const points = simplify(local.map(frame.world));
|
|
970
|
+
const radius = cornerRadius[path];
|
|
971
|
+
const radii = points.map((_, index) =>
|
|
972
|
+
index === 0 || index === points.length - 1
|
|
973
|
+
? 0
|
|
974
|
+
: fittedRadius(points, index, radius, ends, obstacles),
|
|
975
|
+
);
|
|
976
|
+
const d = polylinePath(points, radii);
|
|
977
|
+
if (pathIntrudes(d, ends, obstacles)) return null;
|
|
978
|
+
const label = midpoint(points);
|
|
979
|
+
return [d, label.x, label.y];
|
|
980
|
+
}
|