@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,886 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
21
|
+
value: true
|
|
22
|
+
}), mod);
|
|
23
|
+
var edgeRoute_exports = {};
|
|
24
|
+
__export(edgeRoute_exports, {
|
|
25
|
+
edgeClearance: () => edgeClearance,
|
|
26
|
+
pathIntrudes: () => pathIntrudes,
|
|
27
|
+
routeAroundBoxes: () => routeAroundBoxes,
|
|
28
|
+
routeOnTrack: () => routeOnTrack,
|
|
29
|
+
samplePath: () => samplePath
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(edgeRoute_exports);
|
|
32
|
+
var import_spatialHash = require("../interaction/spatialHash.cjs");
|
|
33
|
+
const edgeClearance = 8;
|
|
34
|
+
const directions = {
|
|
35
|
+
left: {
|
|
36
|
+
x: -1,
|
|
37
|
+
y: 0
|
|
38
|
+
},
|
|
39
|
+
right: {
|
|
40
|
+
x: 1,
|
|
41
|
+
y: 0
|
|
42
|
+
},
|
|
43
|
+
top: {
|
|
44
|
+
x: 0,
|
|
45
|
+
y: -1
|
|
46
|
+
},
|
|
47
|
+
bottom: {
|
|
48
|
+
x: 0,
|
|
49
|
+
y: 1
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const cornerRadius = {
|
|
53
|
+
bezier: 24,
|
|
54
|
+
smoothstep: 5,
|
|
55
|
+
step: 0,
|
|
56
|
+
straight: 0
|
|
57
|
+
};
|
|
58
|
+
const indexes = /* @__PURE__ */new WeakMap();
|
|
59
|
+
function indexFor(boxes) {
|
|
60
|
+
let index = indexes.get(boxes);
|
|
61
|
+
if (!index) {
|
|
62
|
+
index = (0, import_spatialHash.buildSpatialHash)(boxes.map(box => ({
|
|
63
|
+
id: box.id,
|
|
64
|
+
rect: box
|
|
65
|
+
})));
|
|
66
|
+
indexes.set(boxes, index);
|
|
67
|
+
}
|
|
68
|
+
return index;
|
|
69
|
+
}
|
|
70
|
+
function boxesNear(obstacles, area) {
|
|
71
|
+
const byId = new Map(obstacles.boxes.map(box => [box.id, box]));
|
|
72
|
+
const found = [];
|
|
73
|
+
for (const item of indexFor(obstacles.boxes).query(area)) {
|
|
74
|
+
const box = byId.get(item.id);
|
|
75
|
+
if (box) found.push(box);
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
}
|
|
79
|
+
function tokens(d) {
|
|
80
|
+
const raw = d.match(/[MLQCZ]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? [];
|
|
81
|
+
return raw.map(token => /^[MLQCZ]$/i.test(token) ? token.toUpperCase() : Number(token));
|
|
82
|
+
}
|
|
83
|
+
function samplePath(d) {
|
|
84
|
+
const list = tokens(d);
|
|
85
|
+
const points = [];
|
|
86
|
+
let at = {
|
|
87
|
+
x: 0,
|
|
88
|
+
y: 0
|
|
89
|
+
};
|
|
90
|
+
let command = "M";
|
|
91
|
+
let index = 0;
|
|
92
|
+
const take = () => {
|
|
93
|
+
const value = list[index];
|
|
94
|
+
index += 1;
|
|
95
|
+
return typeof value === "number" ? value : Number.NaN;
|
|
96
|
+
};
|
|
97
|
+
while (index < list.length) {
|
|
98
|
+
if (typeof list[index] === "string") {
|
|
99
|
+
command = list[index];
|
|
100
|
+
index += 1;
|
|
101
|
+
if (command === "Z") continue;
|
|
102
|
+
}
|
|
103
|
+
if (command === "M") {
|
|
104
|
+
at = {
|
|
105
|
+
x: take(),
|
|
106
|
+
y: take()
|
|
107
|
+
};
|
|
108
|
+
points.push(at);
|
|
109
|
+
command = "L";
|
|
110
|
+
} else if (command === "L") {
|
|
111
|
+
const to = {
|
|
112
|
+
x: take(),
|
|
113
|
+
y: take()
|
|
114
|
+
};
|
|
115
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
116
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
117
|
+
const t = step / steps;
|
|
118
|
+
points.push({
|
|
119
|
+
x: at.x + (to.x - at.x) * t,
|
|
120
|
+
y: at.y + (to.y - at.y) * t
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
at = to;
|
|
124
|
+
} else if (command === "Q") {
|
|
125
|
+
const control = {
|
|
126
|
+
x: take(),
|
|
127
|
+
y: take()
|
|
128
|
+
};
|
|
129
|
+
const to = {
|
|
130
|
+
x: take(),
|
|
131
|
+
y: take()
|
|
132
|
+
};
|
|
133
|
+
for (let step = 1; step <= 8; step += 1) {
|
|
134
|
+
const t = step / 8;
|
|
135
|
+
const u = 1 - t;
|
|
136
|
+
points.push({
|
|
137
|
+
x: u * u * at.x + 2 * u * t * control.x + t * t * to.x,
|
|
138
|
+
y: u * u * at.y + 2 * u * t * control.y + t * t * to.y
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
at = to;
|
|
142
|
+
} else if (command === "C") {
|
|
143
|
+
const first = {
|
|
144
|
+
x: take(),
|
|
145
|
+
y: take()
|
|
146
|
+
};
|
|
147
|
+
const second = {
|
|
148
|
+
x: take(),
|
|
149
|
+
y: take()
|
|
150
|
+
};
|
|
151
|
+
const to = {
|
|
152
|
+
x: take(),
|
|
153
|
+
y: take()
|
|
154
|
+
};
|
|
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: u * u * u * at.x + 3 * u * u * t * first.x + 3 * u * t * t * second.x + t * t * t * to.x,
|
|
161
|
+
y: u * u * u * at.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * to.y
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
at = to;
|
|
165
|
+
} else {
|
|
166
|
+
return points;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return points;
|
|
170
|
+
}
|
|
171
|
+
function distanceToBox(point, box) {
|
|
172
|
+
const dx = Math.max(box.x - point.x, 0, point.x - (box.x + box.width));
|
|
173
|
+
const dy = Math.max(box.y - point.y, 0, point.y - (box.y + box.height));
|
|
174
|
+
return Math.hypot(dx, dy);
|
|
175
|
+
}
|
|
176
|
+
function ownBox(point, boxes) {
|
|
177
|
+
let best;
|
|
178
|
+
let bestDistance = 0.5;
|
|
179
|
+
for (const box of boxes) {
|
|
180
|
+
const distance = distanceToBox(point, box);
|
|
181
|
+
if (distance <= bestDistance) {
|
|
182
|
+
best = box;
|
|
183
|
+
bestDistance = distance;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return best;
|
|
187
|
+
}
|
|
188
|
+
function depth(point, side, box) {
|
|
189
|
+
if (!box) return 0;
|
|
190
|
+
switch (side) {
|
|
191
|
+
case "right":
|
|
192
|
+
return Math.max(0, box.x + box.width - point.x);
|
|
193
|
+
case "left":
|
|
194
|
+
return Math.max(0, point.x - box.x);
|
|
195
|
+
case "bottom":
|
|
196
|
+
return Math.max(0, box.y + box.height - point.y);
|
|
197
|
+
case "top":
|
|
198
|
+
return Math.max(0, point.y - box.y);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function bounds(points, pad) {
|
|
202
|
+
let minX = Infinity;
|
|
203
|
+
let minY = Infinity;
|
|
204
|
+
let maxX = -Infinity;
|
|
205
|
+
let maxY = -Infinity;
|
|
206
|
+
for (const point of points) {
|
|
207
|
+
minX = Math.min(minX, point.x);
|
|
208
|
+
minY = Math.min(minY, point.y);
|
|
209
|
+
maxX = Math.max(maxX, point.x);
|
|
210
|
+
maxY = Math.max(maxY, point.y);
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
x: minX - pad,
|
|
214
|
+
y: minY - pad,
|
|
215
|
+
width: maxX - minX + pad * 2,
|
|
216
|
+
height: maxY - minY + pad * 2
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function pathIntrudes(d, ends, obstacles) {
|
|
220
|
+
const {
|
|
221
|
+
clearance
|
|
222
|
+
} = obstacles;
|
|
223
|
+
const from = {
|
|
224
|
+
x: ends.sourceX,
|
|
225
|
+
y: ends.sourceY
|
|
226
|
+
};
|
|
227
|
+
const to = {
|
|
228
|
+
x: ends.targetX,
|
|
229
|
+
y: ends.targetY
|
|
230
|
+
};
|
|
231
|
+
const points = samplePath(d);
|
|
232
|
+
if (points.length === 0) return false;
|
|
233
|
+
const near = boxesNear(obstacles, bounds(points, clearance));
|
|
234
|
+
const fromStub = depth(from, ends.sourceSide, ownBox(from, near)) + clearance;
|
|
235
|
+
const toStub = depth(to, ends.targetSide, ownBox(to, near)) + clearance;
|
|
236
|
+
for (const point of points) {
|
|
237
|
+
if (Math.hypot(point.x - from.x, point.y - from.y) <= fromStub) continue;
|
|
238
|
+
if (Math.hypot(point.x - to.x, point.y - to.y) <= toStub) continue;
|
|
239
|
+
for (const box of near) {
|
|
240
|
+
if (distanceToBox(point, box) < clearance - 0.25) return true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
function inflate(box, by) {
|
|
246
|
+
return {
|
|
247
|
+
left: box.x - by,
|
|
248
|
+
right: box.x + box.width + by,
|
|
249
|
+
top: box.y - by,
|
|
250
|
+
bottom: box.y + box.height + by
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function strictlyInside(x, y, rect) {
|
|
254
|
+
return x > rect.left && x < rect.right && y > rect.top && y < rect.bottom;
|
|
255
|
+
}
|
|
256
|
+
function lines(values) {
|
|
257
|
+
const sorted = [...new Set(values.map(value => Math.round(value * 1e3) / 1e3))].sort((a, b) => a - b);
|
|
258
|
+
const out = [];
|
|
259
|
+
for (let index = 0; index < sorted.length; index += 1) {
|
|
260
|
+
out.push(sorted[index]);
|
|
261
|
+
const next = sorted[index + 1];
|
|
262
|
+
if (next !== void 0 && next - sorted[index] > 1) out.push((sorted[index] + next) / 2);
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
function stubEnd(point, side, box, clearance) {
|
|
267
|
+
const reach = depth(point, side, box) + clearance;
|
|
268
|
+
const dir = directions[side];
|
|
269
|
+
return {
|
|
270
|
+
x: point.x + dir.x * reach,
|
|
271
|
+
y: point.y + dir.y * reach
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
class Heap {
|
|
275
|
+
items = [];
|
|
276
|
+
get size() {
|
|
277
|
+
return this.items.length;
|
|
278
|
+
}
|
|
279
|
+
push(key, cost) {
|
|
280
|
+
const items = this.items;
|
|
281
|
+
items.push({
|
|
282
|
+
key,
|
|
283
|
+
cost
|
|
284
|
+
});
|
|
285
|
+
let index = items.length - 1;
|
|
286
|
+
while (index > 0) {
|
|
287
|
+
const parent = index - 1 >> 1;
|
|
288
|
+
if (items[parent].cost <= items[index].cost) break;
|
|
289
|
+
[items[parent], items[index]] = [items[index], items[parent]];
|
|
290
|
+
index = parent;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
pop() {
|
|
294
|
+
const items = this.items;
|
|
295
|
+
const top = items[0];
|
|
296
|
+
const last = items.pop();
|
|
297
|
+
if (items.length > 0 && last) {
|
|
298
|
+
items[0] = last;
|
|
299
|
+
let index = 0;
|
|
300
|
+
for (;;) {
|
|
301
|
+
const left = index * 2 + 1;
|
|
302
|
+
const right = left + 1;
|
|
303
|
+
let smallest = index;
|
|
304
|
+
if (left < items.length && items[left].cost < items[smallest].cost) smallest = left;
|
|
305
|
+
if (right < items.length && items[right].cost < items[smallest].cost) smallest = right;
|
|
306
|
+
if (smallest === index) break;
|
|
307
|
+
[items[smallest], items[index]] = [items[index], items[smallest]];
|
|
308
|
+
index = smallest;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return top;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const stepX = [1, 0, -1, 0];
|
|
315
|
+
const stepY = [0, 1, 0, -1];
|
|
316
|
+
function directionIndex(side) {
|
|
317
|
+
const dir = directions[side];
|
|
318
|
+
return stepX.findIndex((dx, index) => dx === dir.x && stepY[index] === dir.y);
|
|
319
|
+
}
|
|
320
|
+
function orthogonalRoute(start, startDirection, goal, goalDirection, boxes, clearance, area) {
|
|
321
|
+
const rects = boxes.map(box => inflate(box, clearance));
|
|
322
|
+
const xs = lines([start.x, goal.x, area.x, area.x + area.width, ...rects.flatMap(r => [r.left, r.right])]);
|
|
323
|
+
const ys = lines([start.y, goal.y, area.y, area.y + area.height, ...rects.flatMap(r => [r.top, r.bottom])]);
|
|
324
|
+
const columns = xs.length;
|
|
325
|
+
const rows = ys.length;
|
|
326
|
+
const startX = xs.indexOf(Math.round(start.x * 1e3) / 1e3);
|
|
327
|
+
const startY = ys.indexOf(Math.round(start.y * 1e3) / 1e3);
|
|
328
|
+
const goalX = xs.indexOf(Math.round(goal.x * 1e3) / 1e3);
|
|
329
|
+
const goalY = ys.indexOf(Math.round(goal.y * 1e3) / 1e3);
|
|
330
|
+
if (startX < 0 || startY < 0 || goalX < 0 || goalY < 0) return null;
|
|
331
|
+
const free = (ix, iy) => rects.every(rect => !strictlyInside(xs[ix], ys[iy], rect));
|
|
332
|
+
const open = (ax, ay, bx, by) => {
|
|
333
|
+
const minX = Math.min(xs[ax], xs[bx]);
|
|
334
|
+
const maxX = Math.max(xs[ax], xs[bx]);
|
|
335
|
+
const minY = Math.min(ys[ay], ys[by]);
|
|
336
|
+
const maxY = Math.max(ys[ay], ys[by]);
|
|
337
|
+
return rects.every(rect => {
|
|
338
|
+
if (minY === maxY) return !(minY > rect.top && minY < rect.bottom && maxX > rect.left && minX < rect.right);
|
|
339
|
+
return !(minX > rect.left && minX < rect.right && maxY > rect.top && minY < rect.bottom);
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
if (!free(startX, startY) || !free(goalX, goalY)) return null;
|
|
343
|
+
const turn = clearance * 4;
|
|
344
|
+
const key = (ix, iy, dir) => (iy * columns + ix) * 4 + dir;
|
|
345
|
+
const best = /* @__PURE__ */new Map();
|
|
346
|
+
const from = /* @__PURE__ */new Map();
|
|
347
|
+
const heap = new Heap();
|
|
348
|
+
const heuristic = (ix, iy) => Math.abs(xs[ix] - goal.x) + Math.abs(ys[iy] - goal.y);
|
|
349
|
+
const first = key(startX, startY, startDirection);
|
|
350
|
+
best.set(first, 0);
|
|
351
|
+
heap.push(first, heuristic(startX, startY));
|
|
352
|
+
let reached;
|
|
353
|
+
let budget = 2e5;
|
|
354
|
+
while (heap.size > 0 && budget > 0) {
|
|
355
|
+
budget -= 1;
|
|
356
|
+
const current = heap.pop();
|
|
357
|
+
if (!current) break;
|
|
358
|
+
const dir = current.key % 4;
|
|
359
|
+
const cell = (current.key - dir) / 4;
|
|
360
|
+
const ix = cell % columns;
|
|
361
|
+
const iy = (cell - ix) / columns;
|
|
362
|
+
const cost = best.get(current.key) ?? Infinity;
|
|
363
|
+
if (current.cost - heuristic(ix, iy) > cost + 1e-6) continue;
|
|
364
|
+
if (ix === goalX && iy === goalY) {
|
|
365
|
+
reached = current.key;
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
for (let next = 0; next < 4; next += 1) {
|
|
369
|
+
if (next === (dir + 2) % 4) continue;
|
|
370
|
+
const nx = ix + stepX[next];
|
|
371
|
+
const ny = iy + stepY[next];
|
|
372
|
+
if (nx < 0 || ny < 0 || nx >= columns || ny >= rows) continue;
|
|
373
|
+
if (!free(nx, ny) || !open(ix, iy, nx, ny)) continue;
|
|
374
|
+
const atGoal = nx === goalX && ny === goalY;
|
|
375
|
+
const length = Math.abs(xs[nx] - xs[ix]) + Math.abs(ys[ny] - ys[iy]);
|
|
376
|
+
const bend = (next === dir ? 0 : turn) + (atGoal && next !== goalDirection ? turn : 0);
|
|
377
|
+
const nextKey = key(nx, ny, next);
|
|
378
|
+
const nextCost = cost + length + bend;
|
|
379
|
+
if (nextCost < (best.get(nextKey) ?? Infinity)) {
|
|
380
|
+
best.set(nextKey, nextCost);
|
|
381
|
+
from.set(nextKey, current.key);
|
|
382
|
+
heap.push(nextKey, nextCost + heuristic(nx, ny));
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (reached === void 0) return null;
|
|
387
|
+
const route = [];
|
|
388
|
+
for (let at = reached; at !== void 0; at = from.get(at)) {
|
|
389
|
+
const cell = (at - at % 4) / 4;
|
|
390
|
+
const ix = cell % columns;
|
|
391
|
+
route.push({
|
|
392
|
+
x: xs[ix],
|
|
393
|
+
y: ys[(cell - ix) / columns]
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
return route.reverse();
|
|
397
|
+
}
|
|
398
|
+
function simplify(points) {
|
|
399
|
+
const out = [];
|
|
400
|
+
for (const point of points) {
|
|
401
|
+
const last = out[out.length - 1];
|
|
402
|
+
if (last && Math.abs(last.x - point.x) < 1e-6 && Math.abs(last.y - point.y) < 1e-6) continue;
|
|
403
|
+
const before = out[out.length - 2];
|
|
404
|
+
if (before && last) {
|
|
405
|
+
const sameX = Math.abs(before.x - last.x) < 1e-6 && Math.abs(last.x - point.x) < 1e-6;
|
|
406
|
+
const sameY = Math.abs(before.y - last.y) < 1e-6 && Math.abs(last.y - point.y) < 1e-6;
|
|
407
|
+
if (sameX || sameY) out.pop();
|
|
408
|
+
}
|
|
409
|
+
out.push(point);
|
|
410
|
+
}
|
|
411
|
+
return out;
|
|
412
|
+
}
|
|
413
|
+
function format(value) {
|
|
414
|
+
return String(Math.round(value * 1e3) / 1e3);
|
|
415
|
+
}
|
|
416
|
+
function roundCorner(points, index, radius) {
|
|
417
|
+
const before = points[index - 1];
|
|
418
|
+
const corner = points[index];
|
|
419
|
+
const after = points[index + 1];
|
|
420
|
+
const inLength = Math.hypot(corner.x - before.x, corner.y - before.y);
|
|
421
|
+
const outLength = Math.hypot(after.x - corner.x, after.y - corner.y);
|
|
422
|
+
const r = Math.min(radius, inLength / 2, outLength / 2);
|
|
423
|
+
if (r <= 0) return null;
|
|
424
|
+
return {
|
|
425
|
+
enter: {
|
|
426
|
+
x: corner.x - (corner.x - before.x) / inLength * r,
|
|
427
|
+
y: corner.y - (corner.y - before.y) / inLength * r
|
|
428
|
+
},
|
|
429
|
+
corner,
|
|
430
|
+
leave: {
|
|
431
|
+
x: corner.x + (after.x - corner.x) / outLength * r,
|
|
432
|
+
y: corner.y + (after.y - corner.y) / outLength * r
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function cornerPath({
|
|
437
|
+
enter,
|
|
438
|
+
corner,
|
|
439
|
+
leave
|
|
440
|
+
}) {
|
|
441
|
+
return `M${format(enter.x)},${format(enter.y)} Q${format(corner.x)},${format(corner.y)} ${format(leave.x)},${format(leave.y)}`;
|
|
442
|
+
}
|
|
443
|
+
function fittedRadius(points, index, radius, ends, obstacles) {
|
|
444
|
+
const fits = r => {
|
|
445
|
+
const corner = roundCorner(points, index, r);
|
|
446
|
+
return !corner || !pathIntrudes(cornerPath(corner), ends, obstacles);
|
|
447
|
+
};
|
|
448
|
+
if (fits(radius)) return radius;
|
|
449
|
+
let low = 0;
|
|
450
|
+
let high = radius;
|
|
451
|
+
for (let step = 0; step < 12; step += 1) {
|
|
452
|
+
const middle = (low + high) / 2;
|
|
453
|
+
if (fits(middle)) low = middle;else high = middle;
|
|
454
|
+
}
|
|
455
|
+
return low;
|
|
456
|
+
}
|
|
457
|
+
function polylinePath(points, radii) {
|
|
458
|
+
let d = `M${format(points[0].x)},${format(points[0].y)}`;
|
|
459
|
+
for (let index = 1; index < points.length - 1; index += 1) {
|
|
460
|
+
const corner = roundCorner(points, index, radii[index] ?? 0);
|
|
461
|
+
if (!corner) {
|
|
462
|
+
d += ` L${format(points[index].x)},${format(points[index].y)}`;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
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)}`;
|
|
466
|
+
}
|
|
467
|
+
const last = points[points.length - 1];
|
|
468
|
+
return `${d} L${format(last.x)},${format(last.y)}`;
|
|
469
|
+
}
|
|
470
|
+
function midpoint(points) {
|
|
471
|
+
let total = 0;
|
|
472
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
473
|
+
total += Math.hypot(points[index].x - points[index - 1].x, points[index].y - points[index - 1].y);
|
|
474
|
+
}
|
|
475
|
+
let left = total / 2;
|
|
476
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
477
|
+
const a = points[index - 1];
|
|
478
|
+
const b = points[index];
|
|
479
|
+
const length = Math.hypot(b.x - a.x, b.y - a.y);
|
|
480
|
+
if (length >= left && length > 0) {
|
|
481
|
+
const t = left / length;
|
|
482
|
+
return {
|
|
483
|
+
x: a.x + (b.x - a.x) * t,
|
|
484
|
+
y: a.y + (b.y - a.y) * t
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
left -= length;
|
|
488
|
+
}
|
|
489
|
+
return points[0];
|
|
490
|
+
}
|
|
491
|
+
function routeAroundBoxes(path, ends, obstacles) {
|
|
492
|
+
const {
|
|
493
|
+
clearance
|
|
494
|
+
} = obstacles;
|
|
495
|
+
const from = {
|
|
496
|
+
x: ends.sourceX,
|
|
497
|
+
y: ends.sourceY
|
|
498
|
+
};
|
|
499
|
+
const to = {
|
|
500
|
+
x: ends.targetX,
|
|
501
|
+
y: ends.targetY
|
|
502
|
+
};
|
|
503
|
+
const largest = obstacles.boxes.reduce((size, box) => Math.max(size, box.width, box.height), 0);
|
|
504
|
+
for (const reach of [1, 3]) {
|
|
505
|
+
const area = bounds([from, to], (largest + clearance * 2) * reach);
|
|
506
|
+
const near = boxesNear(obstacles, area);
|
|
507
|
+
const start = stubEnd(from, ends.sourceSide, ownBox(from, near), clearance);
|
|
508
|
+
const goal = stubEnd(to, ends.targetSide, ownBox(to, near), clearance);
|
|
509
|
+
const inner = orthogonalRoute(start, directionIndex(ends.sourceSide), goal, (directionIndex(ends.targetSide) + 2) % 4, near, clearance, area);
|
|
510
|
+
if (!inner) continue;
|
|
511
|
+
const points = simplify([from, ...inner, to]);
|
|
512
|
+
const label = midpoint(points);
|
|
513
|
+
const radius = cornerRadius[path];
|
|
514
|
+
const radii = points.map((_, index) => index === 0 || index === points.length - 1 ? 0 : fittedRadius(points, index, radius, ends, obstacles));
|
|
515
|
+
return [polylinePath(points, radii), label.x, label.y];
|
|
516
|
+
}
|
|
517
|
+
return null;
|
|
518
|
+
}
|
|
519
|
+
const trackSharing = "target";
|
|
520
|
+
const frames = {
|
|
521
|
+
bottom: {
|
|
522
|
+
local: ({
|
|
523
|
+
x,
|
|
524
|
+
y
|
|
525
|
+
}) => ({
|
|
526
|
+
x,
|
|
527
|
+
y
|
|
528
|
+
}),
|
|
529
|
+
world: ({
|
|
530
|
+
x,
|
|
531
|
+
y
|
|
532
|
+
}) => ({
|
|
533
|
+
x,
|
|
534
|
+
y
|
|
535
|
+
})
|
|
536
|
+
},
|
|
537
|
+
top: {
|
|
538
|
+
local: ({
|
|
539
|
+
x,
|
|
540
|
+
y
|
|
541
|
+
}) => ({
|
|
542
|
+
x,
|
|
543
|
+
y: -y
|
|
544
|
+
}),
|
|
545
|
+
world: ({
|
|
546
|
+
x,
|
|
547
|
+
y
|
|
548
|
+
}) => ({
|
|
549
|
+
x,
|
|
550
|
+
y: -y
|
|
551
|
+
})
|
|
552
|
+
},
|
|
553
|
+
right: {
|
|
554
|
+
local: ({
|
|
555
|
+
x,
|
|
556
|
+
y
|
|
557
|
+
}) => ({
|
|
558
|
+
x: y,
|
|
559
|
+
y: x
|
|
560
|
+
}),
|
|
561
|
+
world: ({
|
|
562
|
+
x,
|
|
563
|
+
y
|
|
564
|
+
}) => ({
|
|
565
|
+
x: y,
|
|
566
|
+
y: x
|
|
567
|
+
})
|
|
568
|
+
},
|
|
569
|
+
left: {
|
|
570
|
+
local: ({
|
|
571
|
+
x,
|
|
572
|
+
y
|
|
573
|
+
}) => ({
|
|
574
|
+
x: y,
|
|
575
|
+
y: -x
|
|
576
|
+
}),
|
|
577
|
+
world: ({
|
|
578
|
+
x,
|
|
579
|
+
y
|
|
580
|
+
}) => ({
|
|
581
|
+
x: -y,
|
|
582
|
+
y: x
|
|
583
|
+
})
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const opposite = {
|
|
587
|
+
left: "right",
|
|
588
|
+
right: "left",
|
|
589
|
+
top: "bottom",
|
|
590
|
+
bottom: "top"
|
|
591
|
+
};
|
|
592
|
+
function localSpan(box, frame) {
|
|
593
|
+
const a = frame.local({
|
|
594
|
+
x: box.x,
|
|
595
|
+
y: box.y
|
|
596
|
+
});
|
|
597
|
+
const b = frame.local({
|
|
598
|
+
x: box.x + box.width,
|
|
599
|
+
y: box.y + box.height
|
|
600
|
+
});
|
|
601
|
+
return {
|
|
602
|
+
u0: Math.min(a.x, b.x),
|
|
603
|
+
u1: Math.max(a.x, b.x),
|
|
604
|
+
v0: Math.min(a.y, b.y),
|
|
605
|
+
v1: Math.max(a.y, b.y)
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
function trackKey(sharing, link) {
|
|
609
|
+
return sharing === "target" ? link.target : `${link.source}\0${link.target}`;
|
|
610
|
+
}
|
|
611
|
+
function ranksOf(spans) {
|
|
612
|
+
const sorted = [...spans].sort((a, b) => a.v0 - b.v0);
|
|
613
|
+
const ranks = [];
|
|
614
|
+
for (const span of sorted) {
|
|
615
|
+
const last = ranks[ranks.length - 1];
|
|
616
|
+
if (last && span.v0 < last.v1) {
|
|
617
|
+
last.v1 = Math.max(last.v1, span.v1);
|
|
618
|
+
last.spans.push(span);
|
|
619
|
+
} else ranks.push({
|
|
620
|
+
v0: span.v0,
|
|
621
|
+
v1: span.v1,
|
|
622
|
+
spans: [span]
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
for (const rank of ranks) rank.spans.sort((a, b) => a.u0 - b.u0);
|
|
626
|
+
return ranks;
|
|
627
|
+
}
|
|
628
|
+
function channels(rank, clearance) {
|
|
629
|
+
const open = [];
|
|
630
|
+
let from = Number.NEGATIVE_INFINITY;
|
|
631
|
+
for (const span of rank.spans) {
|
|
632
|
+
const to = span.u0 - clearance;
|
|
633
|
+
if (to > from) open.push([from, to]);
|
|
634
|
+
from = Math.max(from, span.u1 + clearance);
|
|
635
|
+
}
|
|
636
|
+
open.push([from, Number.POSITIVE_INFINITY]);
|
|
637
|
+
return open;
|
|
638
|
+
}
|
|
639
|
+
function virtualPoint(open, ideal, goal) {
|
|
640
|
+
const held = open.findIndex(([lo, hi]) => lo <= goal && goal <= hi);
|
|
641
|
+
if (held >= 0) return {
|
|
642
|
+
u: goal,
|
|
643
|
+
channel: held
|
|
644
|
+
};
|
|
645
|
+
let best = {
|
|
646
|
+
u: ideal,
|
|
647
|
+
channel: 0,
|
|
648
|
+
cost: Number.POSITIVE_INFINITY
|
|
649
|
+
};
|
|
650
|
+
open.forEach(([lo, hi], channel) => {
|
|
651
|
+
const u = Math.min(Math.max(ideal, lo), hi);
|
|
652
|
+
const cost = Math.abs(u - ideal);
|
|
653
|
+
if (cost < best.cost) best = {
|
|
654
|
+
u,
|
|
655
|
+
channel,
|
|
656
|
+
cost
|
|
657
|
+
};
|
|
658
|
+
});
|
|
659
|
+
return {
|
|
660
|
+
u: best.u,
|
|
661
|
+
channel: best.channel
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
function linkKey(link) {
|
|
665
|
+
return `${link.source}\0${link.target}`;
|
|
666
|
+
}
|
|
667
|
+
function planTracks(links, boxes, side, clearance, sharing) {
|
|
668
|
+
const frame = frames[side];
|
|
669
|
+
const spans = new Map(boxes.map(box => [box.id, localSpan(box, frame)]));
|
|
670
|
+
const ranks = ranksOf([...spans.values()]);
|
|
671
|
+
const rankOf = /* @__PURE__ */new Map();
|
|
672
|
+
ranks.forEach((rank, index) => {
|
|
673
|
+
for (const span of rank.spans) rankOf.set(span, index);
|
|
674
|
+
});
|
|
675
|
+
const open = ranks.map(rank => channels(rank, clearance));
|
|
676
|
+
const gaps = /* @__PURE__ */new Map();
|
|
677
|
+
const pieces = /* @__PURE__ */new Map();
|
|
678
|
+
const passing = [];
|
|
679
|
+
const virtual = /* @__PURE__ */new Map();
|
|
680
|
+
const channelOf = /* @__PURE__ */new Map();
|
|
681
|
+
for (const link of links) {
|
|
682
|
+
const source = spans.get(link.source);
|
|
683
|
+
const target = spans.get(link.target);
|
|
684
|
+
if (!source || !target) continue;
|
|
685
|
+
const rs = rankOf.get(source);
|
|
686
|
+
const rt = rankOf.get(target);
|
|
687
|
+
if (rt <= rs) continue;
|
|
688
|
+
const id = linkKey(link);
|
|
689
|
+
if (pieces.has(id)) continue;
|
|
690
|
+
const sourceU = (source.u0 + source.u1) / 2;
|
|
691
|
+
const targetU = (target.u0 + target.u1) / 2;
|
|
692
|
+
const us = [sourceU];
|
|
693
|
+
for (let rank = rs + 1; rank < rt; rank += 1) {
|
|
694
|
+
const ideal = sourceU + (targetU - sourceU) * (rank - rs) / (rt - rs);
|
|
695
|
+
const at = virtualPoint(open[rank], ideal, targetU);
|
|
696
|
+
us.push(at.u);
|
|
697
|
+
passing.push({
|
|
698
|
+
link: id,
|
|
699
|
+
rank,
|
|
700
|
+
slot: us.length - 1,
|
|
701
|
+
...at
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
us.push(targetU);
|
|
705
|
+
virtual.set(id, us);
|
|
706
|
+
channelOf.set(id, passing.filter(pass => pass.link === id).map(pass => open[pass.rank][pass.channel]));
|
|
707
|
+
const own = [];
|
|
708
|
+
for (let gap = rs; gap < rt; gap += 1) {
|
|
709
|
+
const last = gap === rt - 1;
|
|
710
|
+
const key = last ? trackKey(sharing, link) : `${id}\0${gap}`;
|
|
711
|
+
own.push({
|
|
712
|
+
gap,
|
|
713
|
+
key
|
|
714
|
+
});
|
|
715
|
+
let groups = gaps.get(gap);
|
|
716
|
+
if (!groups) {
|
|
717
|
+
groups = /* @__PURE__ */new Map();
|
|
718
|
+
gaps.set(gap, groups);
|
|
719
|
+
}
|
|
720
|
+
const a = us[gap - rs];
|
|
721
|
+
const b = us[gap - rs + 1];
|
|
722
|
+
const group = groups.get(key);
|
|
723
|
+
if (group) {
|
|
724
|
+
group.u0 = Math.min(group.u0, a, b);
|
|
725
|
+
group.u1 = Math.max(group.u1, a, b);
|
|
726
|
+
} else groups.set(key, {
|
|
727
|
+
key,
|
|
728
|
+
lone: rt - rs === 1,
|
|
729
|
+
u0: Math.min(a, b),
|
|
730
|
+
u1: Math.max(a, b)
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
pieces.set(id, own);
|
|
734
|
+
}
|
|
735
|
+
const byChannel = /* @__PURE__ */new Map();
|
|
736
|
+
for (const pass of passing) {
|
|
737
|
+
const key = `${pass.rank}:${pass.channel}`;
|
|
738
|
+
byChannel.set(key, [...(byChannel.get(key) ?? []), pass]);
|
|
739
|
+
}
|
|
740
|
+
for (const group of byChannel.values()) {
|
|
741
|
+
if (group.length < 2) continue;
|
|
742
|
+
const [lo, hi] = open[group[0].rank][group[0].channel];
|
|
743
|
+
const sorted = [...group].sort((a, b) => a.u - b.u || a.link.localeCompare(b.link));
|
|
744
|
+
const crowded = sorted.some((pass, index) => index > 0 && pass.u - sorted[index - 1].u < clearance);
|
|
745
|
+
if (!crowded) continue;
|
|
746
|
+
const width = Number.isFinite(lo) && Number.isFinite(hi) ? hi - lo : clearance * (group.length + 1);
|
|
747
|
+
const base = Number.isFinite(lo) ? lo : Number.isFinite(hi) ? hi - width : sorted[0].u;
|
|
748
|
+
const pitch = width / (group.length + 1);
|
|
749
|
+
sorted.forEach((pass, index) => {
|
|
750
|
+
virtual.get(pass.link)[pass.slot] = base + pitch * (index + 1);
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
const levels = /* @__PURE__ */new Map();
|
|
754
|
+
for (const [gap, groups] of gaps) {
|
|
755
|
+
const lo = ranks[gap].v1 + clearance;
|
|
756
|
+
const hi = ranks[gap + 1].v0 - clearance;
|
|
757
|
+
if (lo >= hi) continue;
|
|
758
|
+
const list = [...groups.values()];
|
|
759
|
+
const parent = list.map((_, index) => index);
|
|
760
|
+
const root = index => {
|
|
761
|
+
let at = index;
|
|
762
|
+
while (parent[at] !== at) at = parent[at];
|
|
763
|
+
return at;
|
|
764
|
+
};
|
|
765
|
+
for (let a = 0; a < list.length; a += 1) for (let b = a + 1; b < list.length; b += 1) if (list[a].u0 <= list[b].u1 + clearance && list[b].u0 <= list[a].u1 + clearance) parent[root(b)] = root(a);
|
|
766
|
+
const sets = /* @__PURE__ */new Map();
|
|
767
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
768
|
+
const at = root(index);
|
|
769
|
+
sets.set(at, [...(sets.get(at) ?? []), list[index]]);
|
|
770
|
+
}
|
|
771
|
+
for (const members of sets.values()) {
|
|
772
|
+
const sorted = [...members].sort((a, b) => a.u0 - b.u0 || a.key.localeCompare(b.key));
|
|
773
|
+
const ends = [];
|
|
774
|
+
const lane = /* @__PURE__ */new Map();
|
|
775
|
+
for (const group of sorted) {
|
|
776
|
+
let free = ends.findIndex(end => end + clearance < group.u0);
|
|
777
|
+
if (free < 0) {
|
|
778
|
+
free = ends.length;
|
|
779
|
+
ends.push(group.u1);
|
|
780
|
+
} else ends[free] = group.u1;
|
|
781
|
+
lane.set(group.key, free);
|
|
782
|
+
}
|
|
783
|
+
if (ends.length < 2 && members.every(group => group.lone)) continue;
|
|
784
|
+
const pitch = (hi - lo) / (ends.length + 1);
|
|
785
|
+
for (const [key, index] of lane) levels.set(`${gap}\0${key}`, lo + pitch * (index + 1));
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const courses = /* @__PURE__ */new Map();
|
|
789
|
+
for (const [id, own] of pieces) {
|
|
790
|
+
const vs = own.map(({
|
|
791
|
+
gap,
|
|
792
|
+
key
|
|
793
|
+
}) => levels.get(`${gap}\0${key}`));
|
|
794
|
+
if (vs.some(v => v === void 0)) continue;
|
|
795
|
+
courses.set(id, {
|
|
796
|
+
vs,
|
|
797
|
+
us: virtual.get(id).slice(1, -1),
|
|
798
|
+
open: channelOf.get(id) ?? []
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
return courses;
|
|
802
|
+
}
|
|
803
|
+
const plans = /* @__PURE__ */new WeakMap();
|
|
804
|
+
function tracksFor(obstacles, side) {
|
|
805
|
+
const {
|
|
806
|
+
links,
|
|
807
|
+
boxes,
|
|
808
|
+
clearance
|
|
809
|
+
} = obstacles;
|
|
810
|
+
if (!links || links.length === 0) return void 0;
|
|
811
|
+
let byBoxes = plans.get(links);
|
|
812
|
+
if (!byBoxes) {
|
|
813
|
+
byBoxes = /* @__PURE__ */new WeakMap();
|
|
814
|
+
plans.set(links, byBoxes);
|
|
815
|
+
}
|
|
816
|
+
let bySide = byBoxes.get(boxes);
|
|
817
|
+
if (!bySide) {
|
|
818
|
+
bySide = /* @__PURE__ */new Map();
|
|
819
|
+
byBoxes.set(boxes, bySide);
|
|
820
|
+
}
|
|
821
|
+
const key = `${side}:${clearance}`;
|
|
822
|
+
let tracks = bySide.get(key);
|
|
823
|
+
if (!tracks) {
|
|
824
|
+
tracks = planTracks(links, boxes, side, clearance, trackSharing);
|
|
825
|
+
bySide.set(key, tracks);
|
|
826
|
+
}
|
|
827
|
+
return tracks;
|
|
828
|
+
}
|
|
829
|
+
function routeOnTrack(path, ends, obstacles) {
|
|
830
|
+
if (path !== "smoothstep" && path !== "step") return null;
|
|
831
|
+
if (ends.targetSide !== opposite[ends.sourceSide]) return null;
|
|
832
|
+
const courses = tracksFor(obstacles, ends.sourceSide);
|
|
833
|
+
if (!courses || courses.size === 0) return null;
|
|
834
|
+
const from = {
|
|
835
|
+
x: ends.sourceX,
|
|
836
|
+
y: ends.sourceY
|
|
837
|
+
};
|
|
838
|
+
const to = {
|
|
839
|
+
x: ends.targetX,
|
|
840
|
+
y: ends.targetY
|
|
841
|
+
};
|
|
842
|
+
const source = ownBox(from, boxesNear(obstacles, bounds([from], 1)));
|
|
843
|
+
const target = ownBox(to, boxesNear(obstacles, bounds([to], 1)));
|
|
844
|
+
if (!source || !target) return null;
|
|
845
|
+
const course = courses.get(linkKey({
|
|
846
|
+
source: source.id,
|
|
847
|
+
target: target.id
|
|
848
|
+
}));
|
|
849
|
+
if (!course) return null;
|
|
850
|
+
const frame = frames[ends.sourceSide];
|
|
851
|
+
const a = frame.local(from);
|
|
852
|
+
const b = frame.local(to);
|
|
853
|
+
const {
|
|
854
|
+
vs,
|
|
855
|
+
us
|
|
856
|
+
} = course;
|
|
857
|
+
const {
|
|
858
|
+
clearance
|
|
859
|
+
} = obstacles;
|
|
860
|
+
if (!(a.y < vs[0] && vs[vs.length - 1] < b.y)) return null;
|
|
861
|
+
if (us.length === 0 && Math.abs(a.x - b.x) < 0.5) return null;
|
|
862
|
+
const snapped = us.map((u, index) => {
|
|
863
|
+
const [lo, hi] = course.open[index] ?? [u, u];
|
|
864
|
+
for (const line of [index === us.length - 1 ? b.x : void 0, index === 0 ? a.x : void 0]) if (line !== void 0 && Math.abs(line - u) < clearance * 2 && lo <= line && line <= hi) return line;
|
|
865
|
+
return u;
|
|
866
|
+
});
|
|
867
|
+
const local = [a];
|
|
868
|
+
const holds = [a.x, ...snapped, b.x];
|
|
869
|
+
vs.forEach((v, gap) => {
|
|
870
|
+
local.push({
|
|
871
|
+
x: holds[gap],
|
|
872
|
+
y: v
|
|
873
|
+
}, {
|
|
874
|
+
x: holds[gap + 1],
|
|
875
|
+
y: v
|
|
876
|
+
});
|
|
877
|
+
});
|
|
878
|
+
local.push(b);
|
|
879
|
+
const points = simplify(local.map(frame.world));
|
|
880
|
+
const radius = cornerRadius[path];
|
|
881
|
+
const radii = points.map((_, index) => index === 0 || index === points.length - 1 ? 0 : fittedRadius(points, index, radius, ends, obstacles));
|
|
882
|
+
const d = polylinePath(points, radii);
|
|
883
|
+
if (pathIntrudes(d, ends, obstacles)) return null;
|
|
884
|
+
const label = midpoint(points);
|
|
885
|
+
return [d, label.x, label.y];
|
|
886
|
+
}
|