@multiplatform.one/node-graph 9.0.1 → 9.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/edges/edgeRoute.cjs +348 -204
- package/dist/cjs/edges/edgeRoute.native.js +473 -334
- package/dist/cjs/edges/edgeRoute.native.js.map +1 -1
- package/dist/esm/edges/edgeRoute.mjs +348 -204
- package/dist/esm/edges/edgeRoute.mjs.map +1 -1
- package/dist/esm/edges/edgeRoute.native.js +473 -334
- package/dist/esm/edges/edgeRoute.native.js.map +1 -1
- package/dist/jsx/edges/edgeRoute.mjs +348 -204
- package/dist/jsx/edges/edgeRoute.mjs.map +1 -1
- package/dist/jsx/edges/edgeRoute.native.js +473 -334
- package/dist/jsx/edges/edgeRoute.native.js.map +1 -1
- package/package.json +6 -6
- package/src/edges/edgeRoute.spec.ts +252 -0
- package/src/edges/edgeRoute.ts +445 -218
- package/types/edges/edgeRoute.d.ts.map +1 -1
|
@@ -77,66 +77,69 @@ function samplePath(d) {
|
|
|
77
77
|
points.push(at);
|
|
78
78
|
command = "L";
|
|
79
79
|
} else if (command === "L") {
|
|
80
|
-
|
|
80
|
+
at = sampleLine(points, at, {
|
|
81
81
|
x: take(),
|
|
82
82
|
y: take()
|
|
83
|
-
};
|
|
84
|
-
const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
85
|
-
for (let step = 1; step <= steps; step += 1) {
|
|
86
|
-
const t = step / steps;
|
|
87
|
-
points.push({
|
|
88
|
-
x: at.x + (to.x - at.x) * t,
|
|
89
|
-
y: at.y + (to.y - at.y) * t
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
at = to;
|
|
83
|
+
});
|
|
93
84
|
} else if (command === "Q") {
|
|
94
|
-
|
|
85
|
+
at = sampleQuadratic(points, at, {
|
|
95
86
|
x: take(),
|
|
96
87
|
y: take()
|
|
97
|
-
}
|
|
98
|
-
const to = {
|
|
88
|
+
}, {
|
|
99
89
|
x: take(),
|
|
100
90
|
y: take()
|
|
101
|
-
};
|
|
102
|
-
for (let step = 1; step <= 8; step += 1) {
|
|
103
|
-
const t = step / 8;
|
|
104
|
-
const u = 1 - t;
|
|
105
|
-
points.push({
|
|
106
|
-
x: u * u * at.x + 2 * u * t * control.x + t * t * to.x,
|
|
107
|
-
y: u * u * at.y + 2 * u * t * control.y + t * t * to.y
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
at = to;
|
|
91
|
+
});
|
|
111
92
|
} else if (command === "C") {
|
|
112
|
-
|
|
93
|
+
at = sampleCubic(points, at, {
|
|
113
94
|
x: take(),
|
|
114
95
|
y: take()
|
|
115
|
-
}
|
|
116
|
-
const second = {
|
|
96
|
+
}, {
|
|
117
97
|
x: take(),
|
|
118
98
|
y: take()
|
|
119
|
-
}
|
|
120
|
-
const to = {
|
|
99
|
+
}, {
|
|
121
100
|
x: take(),
|
|
122
101
|
y: take()
|
|
123
|
-
};
|
|
124
|
-
const steps = Math.max(16, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
125
|
-
for (let step = 1; step <= steps; step += 1) {
|
|
126
|
-
const t = step / steps;
|
|
127
|
-
const u = 1 - t;
|
|
128
|
-
points.push({
|
|
129
|
-
x: u * u * u * at.x + 3 * u * u * t * first.x + 3 * u * t * t * second.x + t * t * t * to.x,
|
|
130
|
-
y: u * u * u * at.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * to.y
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
at = to;
|
|
102
|
+
});
|
|
134
103
|
} else {
|
|
135
104
|
return points;
|
|
136
105
|
}
|
|
137
106
|
}
|
|
138
107
|
return points;
|
|
139
108
|
}
|
|
109
|
+
function sampleLine(points, at, to) {
|
|
110
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
111
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
112
|
+
const t = step / steps;
|
|
113
|
+
points.push({
|
|
114
|
+
x: at.x + (to.x - at.x) * t,
|
|
115
|
+
y: at.y + (to.y - at.y) * t
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return to;
|
|
119
|
+
}
|
|
120
|
+
function sampleQuadratic(points, at, control, to) {
|
|
121
|
+
for (let step = 1; step <= 8; step += 1) {
|
|
122
|
+
const t = step / 8;
|
|
123
|
+
const u = 1 - t;
|
|
124
|
+
points.push({
|
|
125
|
+
x: u * u * at.x + 2 * u * t * control.x + t * t * to.x,
|
|
126
|
+
y: u * u * at.y + 2 * u * t * control.y + t * t * to.y
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return to;
|
|
130
|
+
}
|
|
131
|
+
function sampleCubic(points, at, first, second, to) {
|
|
132
|
+
const steps = Math.max(16, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
|
|
133
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
134
|
+
const t = step / steps;
|
|
135
|
+
const u = 1 - t;
|
|
136
|
+
points.push({
|
|
137
|
+
x: u * u * u * at.x + 3 * u * u * t * first.x + 3 * u * t * t * second.x + t * t * t * to.x,
|
|
138
|
+
y: u * u * u * at.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * to.y
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return to;
|
|
142
|
+
}
|
|
140
143
|
function distanceToBox(point, box) {
|
|
141
144
|
const dx = Math.max(box.x - point.x, 0, point.x - (box.x + box.width));
|
|
142
145
|
const dy = Math.max(box.y - point.y, 0, point.y - (box.y + box.height));
|
|
@@ -287,79 +290,146 @@ function directionIndex(side) {
|
|
|
287
290
|
return stepX.findIndex((dx, index) => dx === dir.x && stepY[index] === dir.y);
|
|
288
291
|
}
|
|
289
292
|
function orthogonalRoute(start, startDirection, goal, goalDirection, boxes, clearance, area) {
|
|
293
|
+
const grid = routeGrid(start, goal, boxes, clearance, area);
|
|
294
|
+
const startX = gridIndex(grid.xs, start.x);
|
|
295
|
+
const startY = gridIndex(grid.ys, start.y);
|
|
296
|
+
const goalX = gridIndex(grid.xs, goal.x);
|
|
297
|
+
const goalY = gridIndex(grid.ys, goal.y);
|
|
298
|
+
if (startX < 0 || startY < 0 || goalX < 0 || goalY < 0) return null;
|
|
299
|
+
if (!gridFree(grid, startX, startY) || !gridFree(grid, goalX, goalY)) return null;
|
|
300
|
+
const search = {
|
|
301
|
+
grid,
|
|
302
|
+
goal,
|
|
303
|
+
goalX,
|
|
304
|
+
goalY,
|
|
305
|
+
goalDirection,
|
|
306
|
+
turn: clearance * 4,
|
|
307
|
+
best: /* @__PURE__ */new Map(),
|
|
308
|
+
from: /* @__PURE__ */new Map(),
|
|
309
|
+
heap: new Heap()
|
|
310
|
+
};
|
|
311
|
+
const first = gridKey(grid, startX, startY, startDirection);
|
|
312
|
+
const reached = searchRoute(search, first, startX, startY);
|
|
313
|
+
if (reached === void 0) return null;
|
|
314
|
+
return traceRoute(grid, search.from, reached);
|
|
315
|
+
}
|
|
316
|
+
function routeGrid(start, goal, boxes, clearance, area) {
|
|
290
317
|
const rects = boxes.map(box => inflate(box, clearance));
|
|
291
318
|
const xs = lines([start.x, goal.x, area.x, area.x + area.width, ...rects.flatMap(r => [r.left, r.right])]);
|
|
292
319
|
const ys = lines([start.y, goal.y, area.y, area.y + area.height, ...rects.flatMap(r => [r.top, r.bottom])]);
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
320
|
+
return {
|
|
321
|
+
xs,
|
|
322
|
+
ys,
|
|
323
|
+
rects
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function gridIndex(values, value) {
|
|
327
|
+
return values.indexOf(Math.round(value * 1e3) / 1e3);
|
|
328
|
+
}
|
|
329
|
+
function gridFree(grid, ix, iy) {
|
|
330
|
+
return grid.rects.every(rect => !strictlyInside(grid.xs[ix], grid.ys[iy], rect));
|
|
331
|
+
}
|
|
332
|
+
function gridOpen(grid, ax, ay, bx, by) {
|
|
333
|
+
const {
|
|
334
|
+
xs,
|
|
335
|
+
ys
|
|
336
|
+
} = grid;
|
|
337
|
+
const minX = Math.min(xs[ax], xs[bx]);
|
|
338
|
+
const maxX = Math.max(xs[ax], xs[bx]);
|
|
339
|
+
const minY = Math.min(ys[ay], ys[by]);
|
|
340
|
+
const maxY = Math.max(ys[ay], ys[by]);
|
|
341
|
+
return grid.rects.every(rect => {
|
|
342
|
+
if (minY === maxY) return !(minY > rect.top && minY < rect.bottom && maxX > rect.left && minX < rect.right);
|
|
343
|
+
return !(minX > rect.left && minX < rect.right && maxY > rect.top && minY < rect.bottom);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function canStep(grid, ix, iy, nx, ny) {
|
|
347
|
+
if (nx < 0 || ny < 0 || nx >= grid.xs.length || ny >= grid.ys.length) return false;
|
|
348
|
+
return gridFree(grid, nx, ny) && gridOpen(grid, ix, iy, nx, ny);
|
|
349
|
+
}
|
|
350
|
+
function gridKey(grid, ix, iy, dir) {
|
|
351
|
+
return (iy * grid.xs.length + ix) * 4 + dir;
|
|
352
|
+
}
|
|
353
|
+
function gridCell(grid, key) {
|
|
354
|
+
const columns = grid.xs.length;
|
|
355
|
+
const dir = key % 4;
|
|
356
|
+
const cell = (key - dir) / 4;
|
|
357
|
+
const ix = cell % columns;
|
|
358
|
+
return {
|
|
359
|
+
ix,
|
|
360
|
+
iy: (cell - ix) / columns,
|
|
361
|
+
dir
|
|
310
362
|
};
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
363
|
+
}
|
|
364
|
+
function routeHeuristic(search, ix, iy) {
|
|
365
|
+
return Math.abs(search.grid.xs[ix] - search.goal.x) + Math.abs(search.grid.ys[iy] - search.goal.y);
|
|
366
|
+
}
|
|
367
|
+
function bendCost(search, dir, next, nx, ny) {
|
|
368
|
+
const atGoal = nx === search.goalX && ny === search.goalY;
|
|
369
|
+
return (next === dir ? 0 : search.turn) + (atGoal && next !== search.goalDirection ? search.turn : 0);
|
|
370
|
+
}
|
|
371
|
+
function searchRoute(search, first, startX, startY) {
|
|
372
|
+
const {
|
|
373
|
+
best,
|
|
374
|
+
heap
|
|
375
|
+
} = search;
|
|
319
376
|
best.set(first, 0);
|
|
320
|
-
heap.push(first,
|
|
321
|
-
let reached;
|
|
377
|
+
heap.push(first, routeHeuristic(search, startX, startY));
|
|
322
378
|
let budget = 2e5;
|
|
323
379
|
while (heap.size > 0 && budget > 0) {
|
|
324
380
|
budget -= 1;
|
|
325
381
|
const current = heap.pop();
|
|
326
382
|
if (!current) break;
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
383
|
+
const {
|
|
384
|
+
ix,
|
|
385
|
+
iy,
|
|
386
|
+
dir
|
|
387
|
+
} = gridCell(search.grid, current.key);
|
|
331
388
|
const cost = best.get(current.key) ?? Infinity;
|
|
332
|
-
if (current.cost -
|
|
333
|
-
if (ix === goalX && iy === goalY)
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
389
|
+
if (current.cost - routeHeuristic(search, ix, iy) > cost + 1e-6) continue;
|
|
390
|
+
if (ix === search.goalX && iy === search.goalY) return current.key;
|
|
391
|
+
relaxNeighbours(search, {
|
|
392
|
+
key: current.key,
|
|
393
|
+
ix,
|
|
394
|
+
iy,
|
|
395
|
+
dir,
|
|
396
|
+
cost
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
return void 0;
|
|
400
|
+
}
|
|
401
|
+
function relaxNeighbours(search, node) {
|
|
402
|
+
const {
|
|
403
|
+
grid,
|
|
404
|
+
best,
|
|
405
|
+
from,
|
|
406
|
+
heap
|
|
407
|
+
} = search;
|
|
408
|
+
for (let next = 0; next < 4; next += 1) {
|
|
409
|
+
if (next === (node.dir + 2) % 4) continue;
|
|
410
|
+
const nx = node.ix + stepX[next];
|
|
411
|
+
const ny = node.iy + stepY[next];
|
|
412
|
+
if (!canStep(grid, node.ix, node.iy, nx, ny)) continue;
|
|
413
|
+
const length = Math.abs(grid.xs[nx] - grid.xs[node.ix]) + Math.abs(grid.ys[ny] - grid.ys[node.iy]);
|
|
414
|
+
const nextKey = gridKey(grid, nx, ny, next);
|
|
415
|
+
const nextCost = node.cost + length + bendCost(search, node.dir, next, nx, ny);
|
|
416
|
+
if (nextCost < (best.get(nextKey) ?? Infinity)) {
|
|
417
|
+
best.set(nextKey, nextCost);
|
|
418
|
+
from.set(nextKey, node.key);
|
|
419
|
+
heap.push(nextKey, nextCost + routeHeuristic(search, nx, ny));
|
|
353
420
|
}
|
|
354
421
|
}
|
|
355
|
-
|
|
422
|
+
}
|
|
423
|
+
function traceRoute(grid, from, reached) {
|
|
356
424
|
const route = [];
|
|
357
425
|
for (let at = reached; at !== void 0; at = from.get(at)) {
|
|
358
|
-
const
|
|
359
|
-
|
|
426
|
+
const {
|
|
427
|
+
ix,
|
|
428
|
+
iy
|
|
429
|
+
} = gridCell(grid, at);
|
|
360
430
|
route.push({
|
|
361
|
-
x: xs[ix],
|
|
362
|
-
y: ys[
|
|
431
|
+
x: grid.xs[ix],
|
|
432
|
+
y: grid.ys[iy]
|
|
363
433
|
});
|
|
364
434
|
}
|
|
365
435
|
return route.reverse();
|
|
@@ -633,7 +703,7 @@ function virtualPoint(open, ideal, goal) {
|
|
|
633
703
|
function linkKey(link) {
|
|
634
704
|
return `${link.source}\0${link.target}`;
|
|
635
705
|
}
|
|
636
|
-
function
|
|
706
|
+
function layersOf(boxes, side, clearance) {
|
|
637
707
|
const frame = frames[side];
|
|
638
708
|
const spans = new Map(boxes.map(box => [box.id, localSpan(box, frame)]));
|
|
639
709
|
const ranks = ranksOf([...spans.values()]);
|
|
@@ -641,121 +711,189 @@ function planTracks(links, boxes, side, clearance, sharing) {
|
|
|
641
711
|
ranks.forEach((rank, index) => {
|
|
642
712
|
for (const span of rank.spans) rankOf.set(span, index);
|
|
643
713
|
});
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
714
|
+
return {
|
|
715
|
+
spans,
|
|
716
|
+
ranks,
|
|
717
|
+
rankOf,
|
|
718
|
+
open: ranks.map(rank => channels(rank, clearance))
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
function forwardEnds(link, layers) {
|
|
722
|
+
const source = layers.spans.get(link.source);
|
|
723
|
+
const target = layers.spans.get(link.target);
|
|
724
|
+
if (!source || !target) return void 0;
|
|
725
|
+
const rs = layers.rankOf.get(source);
|
|
726
|
+
const rt = layers.rankOf.get(target);
|
|
727
|
+
if (rt <= rs) return void 0;
|
|
728
|
+
return {
|
|
729
|
+
source,
|
|
730
|
+
target,
|
|
731
|
+
rs,
|
|
732
|
+
rt
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function virtualCourse(id, {
|
|
736
|
+
source,
|
|
737
|
+
target,
|
|
738
|
+
rs,
|
|
739
|
+
rt
|
|
740
|
+
}, open) {
|
|
741
|
+
const sourceU = (source.u0 + source.u1) / 2;
|
|
742
|
+
const targetU = (target.u0 + target.u1) / 2;
|
|
743
|
+
const us = [sourceU];
|
|
744
|
+
const passes = [];
|
|
745
|
+
for (let rank = rs + 1; rank < rt; rank += 1) {
|
|
746
|
+
const ideal = sourceU + (targetU - sourceU) * (rank - rs) / (rt - rs);
|
|
747
|
+
const at = virtualPoint(open[rank], ideal, targetU);
|
|
748
|
+
us.push(at.u);
|
|
749
|
+
passes.push({
|
|
750
|
+
link: id,
|
|
751
|
+
rank,
|
|
752
|
+
slot: us.length - 1,
|
|
753
|
+
...at
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
us.push(targetU);
|
|
757
|
+
return {
|
|
758
|
+
us,
|
|
759
|
+
passes
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
function groupsAt(gaps, gap) {
|
|
763
|
+
let groups = gaps.get(gap);
|
|
764
|
+
if (!groups) {
|
|
765
|
+
groups = /* @__PURE__ */new Map();
|
|
766
|
+
gaps.set(gap, groups);
|
|
767
|
+
}
|
|
768
|
+
return groups;
|
|
769
|
+
}
|
|
770
|
+
function widenGroup(groups, key, lone, a, b) {
|
|
771
|
+
const group = groups.get(key);
|
|
772
|
+
if (group) {
|
|
773
|
+
group.u0 = Math.min(group.u0, a, b);
|
|
774
|
+
group.u1 = Math.max(group.u1, a, b);
|
|
775
|
+
} else groups.set(key, {
|
|
776
|
+
key,
|
|
777
|
+
lone,
|
|
778
|
+
u0: Math.min(a, b),
|
|
779
|
+
u1: Math.max(a, b)
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
function placePieces(gaps, link, id, us, {
|
|
783
|
+
rs,
|
|
784
|
+
rt
|
|
785
|
+
}, sharing) {
|
|
786
|
+
const own = [];
|
|
787
|
+
for (let gap = rs; gap < rt; gap += 1) {
|
|
788
|
+
const last = gap === rt - 1;
|
|
789
|
+
const key = last ? trackKey(sharing, link) : `${id}\0${gap}`;
|
|
790
|
+
own.push({
|
|
791
|
+
gap,
|
|
792
|
+
key
|
|
793
|
+
});
|
|
794
|
+
widenGroup(groupsAt(gaps, gap), key, rt - rs === 1, us[gap - rs], us[gap - rs + 1]);
|
|
795
|
+
}
|
|
796
|
+
return own;
|
|
797
|
+
}
|
|
798
|
+
function collectPieces(links, layers, sharing) {
|
|
799
|
+
const plan = {
|
|
800
|
+
gaps: /* @__PURE__ */new Map(),
|
|
801
|
+
pieces: /* @__PURE__ */new Map(),
|
|
802
|
+
passing: [],
|
|
803
|
+
virtual: /* @__PURE__ */new Map(),
|
|
804
|
+
channelOf: /* @__PURE__ */new Map()
|
|
805
|
+
};
|
|
650
806
|
for (const link of links) {
|
|
651
|
-
const
|
|
652
|
-
|
|
653
|
-
if (!source || !target) continue;
|
|
654
|
-
const rs = rankOf.get(source);
|
|
655
|
-
const rt = rankOf.get(target);
|
|
656
|
-
if (rt <= rs) continue;
|
|
807
|
+
const ends = forwardEnds(link, layers);
|
|
808
|
+
if (!ends) continue;
|
|
657
809
|
const id = linkKey(link);
|
|
658
|
-
if (pieces.has(id)) continue;
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
link: id,
|
|
668
|
-
rank,
|
|
669
|
-
slot: us.length - 1,
|
|
670
|
-
...at
|
|
671
|
-
});
|
|
672
|
-
}
|
|
673
|
-
us.push(targetU);
|
|
674
|
-
virtual.set(id, us);
|
|
675
|
-
channelOf.set(id, passing.filter(pass => pass.link === id).map(pass => open[pass.rank][pass.channel]));
|
|
676
|
-
const own = [];
|
|
677
|
-
for (let gap = rs; gap < rt; gap += 1) {
|
|
678
|
-
const last = gap === rt - 1;
|
|
679
|
-
const key = last ? trackKey(sharing, link) : `${id}\0${gap}`;
|
|
680
|
-
own.push({
|
|
681
|
-
gap,
|
|
682
|
-
key
|
|
683
|
-
});
|
|
684
|
-
let groups = gaps.get(gap);
|
|
685
|
-
if (!groups) {
|
|
686
|
-
groups = /* @__PURE__ */new Map();
|
|
687
|
-
gaps.set(gap, groups);
|
|
688
|
-
}
|
|
689
|
-
const a = us[gap - rs];
|
|
690
|
-
const b = us[gap - rs + 1];
|
|
691
|
-
const group = groups.get(key);
|
|
692
|
-
if (group) {
|
|
693
|
-
group.u0 = Math.min(group.u0, a, b);
|
|
694
|
-
group.u1 = Math.max(group.u1, a, b);
|
|
695
|
-
} else groups.set(key, {
|
|
696
|
-
key,
|
|
697
|
-
lone: rt - rs === 1,
|
|
698
|
-
u0: Math.min(a, b),
|
|
699
|
-
u1: Math.max(a, b)
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
pieces.set(id, own);
|
|
810
|
+
if (plan.pieces.has(id)) continue;
|
|
811
|
+
const {
|
|
812
|
+
us,
|
|
813
|
+
passes
|
|
814
|
+
} = virtualCourse(id, ends, layers.open);
|
|
815
|
+
plan.passing.push(...passes);
|
|
816
|
+
plan.virtual.set(id, us);
|
|
817
|
+
plan.channelOf.set(id, passes.map(pass => layers.open[pass.rank][pass.channel]));
|
|
818
|
+
plan.pieces.set(id, placePieces(plan.gaps, link, id, us, ends, sharing));
|
|
703
819
|
}
|
|
820
|
+
return plan;
|
|
821
|
+
}
|
|
822
|
+
function spreadChannel(group, open, virtual, clearance) {
|
|
823
|
+
if (group.length < 2) return;
|
|
824
|
+
const [lo, hi] = open[group[0].rank][group[0].channel];
|
|
825
|
+
const sorted = [...group].sort((a, b) => a.u - b.u || a.link.localeCompare(b.link));
|
|
826
|
+
const crowded = sorted.some((pass, index) => index > 0 && pass.u - sorted[index - 1].u < clearance);
|
|
827
|
+
if (!crowded) return;
|
|
828
|
+
const width = Number.isFinite(lo) && Number.isFinite(hi) ? hi - lo : clearance * (group.length + 1);
|
|
829
|
+
const base = Number.isFinite(lo) ? lo : Number.isFinite(hi) ? hi - width : sorted[0].u;
|
|
830
|
+
const pitch = width / (group.length + 1);
|
|
831
|
+
sorted.forEach((pass, index) => {
|
|
832
|
+
virtual.get(pass.link)[pass.slot] = base + pitch * (index + 1);
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
function spreadChannels(passing, open, virtual, clearance) {
|
|
704
836
|
const byChannel = /* @__PURE__ */new Map();
|
|
705
837
|
for (const pass of passing) {
|
|
706
838
|
const key = `${pass.rank}:${pass.channel}`;
|
|
707
839
|
byChannel.set(key, [...(byChannel.get(key) ?? []), pass]);
|
|
708
840
|
}
|
|
709
|
-
for (const group of byChannel.values())
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
841
|
+
for (const group of byChannel.values()) spreadChannel(group, open, virtual, clearance);
|
|
842
|
+
}
|
|
843
|
+
function conflictSets(list, clearance) {
|
|
844
|
+
const parent = list.map((_, index) => index);
|
|
845
|
+
const root = index => {
|
|
846
|
+
let at = index;
|
|
847
|
+
while (parent[at] !== at) at = parent[at];
|
|
848
|
+
return at;
|
|
849
|
+
};
|
|
850
|
+
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);
|
|
851
|
+
const sets = /* @__PURE__ */new Map();
|
|
852
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
853
|
+
const at = root(index);
|
|
854
|
+
sets.set(at, [...(sets.get(at) ?? []), list[index]]);
|
|
721
855
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
};
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
const pitch = (hi - lo) / (ends.length + 1);
|
|
754
|
-
for (const [key, index] of lane) levels.set(`${gap}\0${key}`, lo + pitch * (index + 1));
|
|
755
|
-
}
|
|
856
|
+
return [...sets.values()];
|
|
857
|
+
}
|
|
858
|
+
function lanesOf(members, clearance) {
|
|
859
|
+
const sorted = [...members].sort((a, b) => a.u0 - b.u0 || a.key.localeCompare(b.key));
|
|
860
|
+
const ends = [];
|
|
861
|
+
const lane = /* @__PURE__ */new Map();
|
|
862
|
+
for (const group of sorted) {
|
|
863
|
+
let free = ends.findIndex(end => end + clearance < group.u0);
|
|
864
|
+
if (free < 0) {
|
|
865
|
+
free = ends.length;
|
|
866
|
+
ends.push(group.u1);
|
|
867
|
+
} else ends[free] = group.u1;
|
|
868
|
+
lane.set(group.key, free);
|
|
869
|
+
}
|
|
870
|
+
return {
|
|
871
|
+
lane,
|
|
872
|
+
count: ends.length
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
function levelGap(gap, groups, ranks, clearance, levels) {
|
|
876
|
+
const lo = ranks[gap].v1 + clearance;
|
|
877
|
+
const hi = ranks[gap + 1].v0 - clearance;
|
|
878
|
+
if (lo >= hi) return;
|
|
879
|
+
for (const members of conflictSets([...groups.values()], clearance)) {
|
|
880
|
+
const {
|
|
881
|
+
lane,
|
|
882
|
+
count
|
|
883
|
+
} = lanesOf(members, clearance);
|
|
884
|
+
if (count < 2 && members.every(group => group.lone)) continue;
|
|
885
|
+
const pitch = (hi - lo) / (count + 1);
|
|
886
|
+
for (const [key, index] of lane) levels.set(`${gap}\0${key}`, lo + pitch * (index + 1));
|
|
756
887
|
}
|
|
888
|
+
}
|
|
889
|
+
function levelsOf(gaps, ranks, clearance) {
|
|
890
|
+
const levels = /* @__PURE__ */new Map();
|
|
891
|
+
for (const [gap, groups] of gaps) levelGap(gap, groups, ranks, clearance, levels);
|
|
892
|
+
return levels;
|
|
893
|
+
}
|
|
894
|
+
function coursesOf(plan, levels) {
|
|
757
895
|
const courses = /* @__PURE__ */new Map();
|
|
758
|
-
for (const [id, own] of pieces) {
|
|
896
|
+
for (const [id, own] of plan.pieces) {
|
|
759
897
|
const vs = own.map(({
|
|
760
898
|
gap,
|
|
761
899
|
key
|
|
@@ -763,12 +901,18 @@ function planTracks(links, boxes, side, clearance, sharing) {
|
|
|
763
901
|
if (vs.some(v => v === void 0)) continue;
|
|
764
902
|
courses.set(id, {
|
|
765
903
|
vs,
|
|
766
|
-
us: virtual.get(id).slice(1, -1),
|
|
767
|
-
open: channelOf.get(id) ?? []
|
|
904
|
+
us: plan.virtual.get(id).slice(1, -1),
|
|
905
|
+
open: plan.channelOf.get(id) ?? []
|
|
768
906
|
});
|
|
769
907
|
}
|
|
770
908
|
return courses;
|
|
771
909
|
}
|
|
910
|
+
function planTracks(links, boxes, side, clearance, sharing) {
|
|
911
|
+
const layers = layersOf(boxes, side, clearance);
|
|
912
|
+
const plan = collectPieces(links, layers, sharing);
|
|
913
|
+
spreadChannels(plan.passing, layers.open, plan.virtual, clearance);
|
|
914
|
+
return coursesOf(plan, levelsOf(plan.gaps, layers.ranks, clearance));
|
|
915
|
+
}
|
|
772
916
|
const plans = /* @__PURE__ */new WeakMap();
|
|
773
917
|
function tracksFor(obstacles, side) {
|
|
774
918
|
const {
|