@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.
@@ -108,66 +108,69 @@ function samplePath(d) {
108
108
  points.push(at);
109
109
  command = "L";
110
110
  } else if (command === "L") {
111
- const to = {
111
+ at = sampleLine(points, at, {
112
112
  x: take(),
113
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;
114
+ });
124
115
  } else if (command === "Q") {
125
- const control = {
116
+ at = sampleQuadratic(points, at, {
126
117
  x: take(),
127
118
  y: take()
128
- };
129
- const to = {
119
+ }, {
130
120
  x: take(),
131
121
  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;
122
+ });
142
123
  } else if (command === "C") {
143
- const first = {
124
+ at = sampleCubic(points, at, {
144
125
  x: take(),
145
126
  y: take()
146
- };
147
- const second = {
127
+ }, {
148
128
  x: take(),
149
129
  y: take()
150
- };
151
- const to = {
130
+ }, {
152
131
  x: take(),
153
132
  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;
133
+ });
165
134
  } else {
166
135
  return points;
167
136
  }
168
137
  }
169
138
  return points;
170
139
  }
140
+ function sampleLine(points, at, to) {
141
+ const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
142
+ for (let step = 1; step <= steps; step += 1) {
143
+ const t = step / steps;
144
+ points.push({
145
+ x: at.x + (to.x - at.x) * t,
146
+ y: at.y + (to.y - at.y) * t
147
+ });
148
+ }
149
+ return to;
150
+ }
151
+ function sampleQuadratic(points, at, control, to) {
152
+ for (let step = 1; step <= 8; step += 1) {
153
+ const t = step / 8;
154
+ const u = 1 - t;
155
+ points.push({
156
+ x: u * u * at.x + 2 * u * t * control.x + t * t * to.x,
157
+ y: u * u * at.y + 2 * u * t * control.y + t * t * to.y
158
+ });
159
+ }
160
+ return to;
161
+ }
162
+ function sampleCubic(points, at, first, second, to) {
163
+ const steps = Math.max(16, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y) / 4));
164
+ for (let step = 1; step <= steps; step += 1) {
165
+ const t = step / steps;
166
+ const u = 1 - t;
167
+ points.push({
168
+ x: u * u * u * at.x + 3 * u * u * t * first.x + 3 * u * t * t * second.x + t * t * t * to.x,
169
+ y: u * u * u * at.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * to.y
170
+ });
171
+ }
172
+ return to;
173
+ }
171
174
  function distanceToBox(point, box) {
172
175
  const dx = Math.max(box.x - point.x, 0, point.x - (box.x + box.width));
173
176
  const dy = Math.max(box.y - point.y, 0, point.y - (box.y + box.height));
@@ -318,79 +321,146 @@ function directionIndex(side) {
318
321
  return stepX.findIndex((dx, index) => dx === dir.x && stepY[index] === dir.y);
319
322
  }
320
323
  function orthogonalRoute(start, startDirection, goal, goalDirection, boxes, clearance, area) {
324
+ const grid = routeGrid(start, goal, boxes, clearance, area);
325
+ const startX = gridIndex(grid.xs, start.x);
326
+ const startY = gridIndex(grid.ys, start.y);
327
+ const goalX = gridIndex(grid.xs, goal.x);
328
+ const goalY = gridIndex(grid.ys, goal.y);
329
+ if (startX < 0 || startY < 0 || goalX < 0 || goalY < 0) return null;
330
+ if (!gridFree(grid, startX, startY) || !gridFree(grid, goalX, goalY)) return null;
331
+ const search = {
332
+ grid,
333
+ goal,
334
+ goalX,
335
+ goalY,
336
+ goalDirection,
337
+ turn: clearance * 4,
338
+ best: /* @__PURE__ */new Map(),
339
+ from: /* @__PURE__ */new Map(),
340
+ heap: new Heap()
341
+ };
342
+ const first = gridKey(grid, startX, startY, startDirection);
343
+ const reached = searchRoute(search, first, startX, startY);
344
+ if (reached === void 0) return null;
345
+ return traceRoute(grid, search.from, reached);
346
+ }
347
+ function routeGrid(start, goal, boxes, clearance, area) {
321
348
  const rects = boxes.map(box => inflate(box, clearance));
322
349
  const xs = lines([start.x, goal.x, area.x, area.x + area.width, ...rects.flatMap(r => [r.left, r.right])]);
323
350
  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
- });
351
+ return {
352
+ xs,
353
+ ys,
354
+ rects
355
+ };
356
+ }
357
+ function gridIndex(values, value) {
358
+ return values.indexOf(Math.round(value * 1e3) / 1e3);
359
+ }
360
+ function gridFree(grid, ix, iy) {
361
+ return grid.rects.every(rect => !strictlyInside(grid.xs[ix], grid.ys[iy], rect));
362
+ }
363
+ function gridOpen(grid, ax, ay, bx, by) {
364
+ const {
365
+ xs,
366
+ ys
367
+ } = grid;
368
+ const minX = Math.min(xs[ax], xs[bx]);
369
+ const maxX = Math.max(xs[ax], xs[bx]);
370
+ const minY = Math.min(ys[ay], ys[by]);
371
+ const maxY = Math.max(ys[ay], ys[by]);
372
+ return grid.rects.every(rect => {
373
+ if (minY === maxY) return !(minY > rect.top && minY < rect.bottom && maxX > rect.left && minX < rect.right);
374
+ return !(minX > rect.left && minX < rect.right && maxY > rect.top && minY < rect.bottom);
375
+ });
376
+ }
377
+ function canStep(grid, ix, iy, nx, ny) {
378
+ if (nx < 0 || ny < 0 || nx >= grid.xs.length || ny >= grid.ys.length) return false;
379
+ return gridFree(grid, nx, ny) && gridOpen(grid, ix, iy, nx, ny);
380
+ }
381
+ function gridKey(grid, ix, iy, dir) {
382
+ return (iy * grid.xs.length + ix) * 4 + dir;
383
+ }
384
+ function gridCell(grid, key) {
385
+ const columns = grid.xs.length;
386
+ const dir = key % 4;
387
+ const cell = (key - dir) / 4;
388
+ const ix = cell % columns;
389
+ return {
390
+ ix,
391
+ iy: (cell - ix) / columns,
392
+ dir
341
393
  };
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);
394
+ }
395
+ function routeHeuristic(search, ix, iy) {
396
+ return Math.abs(search.grid.xs[ix] - search.goal.x) + Math.abs(search.grid.ys[iy] - search.goal.y);
397
+ }
398
+ function bendCost(search, dir, next, nx, ny) {
399
+ const atGoal = nx === search.goalX && ny === search.goalY;
400
+ return (next === dir ? 0 : search.turn) + (atGoal && next !== search.goalDirection ? search.turn : 0);
401
+ }
402
+ function searchRoute(search, first, startX, startY) {
403
+ const {
404
+ best,
405
+ heap
406
+ } = search;
350
407
  best.set(first, 0);
351
- heap.push(first, heuristic(startX, startY));
352
- let reached;
408
+ heap.push(first, routeHeuristic(search, startX, startY));
353
409
  let budget = 2e5;
354
410
  while (heap.size > 0 && budget > 0) {
355
411
  budget -= 1;
356
412
  const current = heap.pop();
357
413
  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;
414
+ const {
415
+ ix,
416
+ iy,
417
+ dir
418
+ } = gridCell(search.grid, current.key);
362
419
  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
- }
420
+ if (current.cost - routeHeuristic(search, ix, iy) > cost + 1e-6) continue;
421
+ if (ix === search.goalX && iy === search.goalY) return current.key;
422
+ relaxNeighbours(search, {
423
+ key: current.key,
424
+ ix,
425
+ iy,
426
+ dir,
427
+ cost
428
+ });
429
+ }
430
+ return void 0;
431
+ }
432
+ function relaxNeighbours(search, node) {
433
+ const {
434
+ grid,
435
+ best,
436
+ from,
437
+ heap
438
+ } = search;
439
+ for (let next = 0; next < 4; next += 1) {
440
+ if (next === (node.dir + 2) % 4) continue;
441
+ const nx = node.ix + stepX[next];
442
+ const ny = node.iy + stepY[next];
443
+ if (!canStep(grid, node.ix, node.iy, nx, ny)) continue;
444
+ const length = Math.abs(grid.xs[nx] - grid.xs[node.ix]) + Math.abs(grid.ys[ny] - grid.ys[node.iy]);
445
+ const nextKey = gridKey(grid, nx, ny, next);
446
+ const nextCost = node.cost + length + bendCost(search, node.dir, next, nx, ny);
447
+ if (nextCost < (best.get(nextKey) ?? Infinity)) {
448
+ best.set(nextKey, nextCost);
449
+ from.set(nextKey, node.key);
450
+ heap.push(nextKey, nextCost + routeHeuristic(search, nx, ny));
384
451
  }
385
452
  }
386
- if (reached === void 0) return null;
453
+ }
454
+ function traceRoute(grid, from, reached) {
387
455
  const route = [];
388
456
  for (let at = reached; at !== void 0; at = from.get(at)) {
389
- const cell = (at - at % 4) / 4;
390
- const ix = cell % columns;
457
+ const {
458
+ ix,
459
+ iy
460
+ } = gridCell(grid, at);
391
461
  route.push({
392
- x: xs[ix],
393
- y: ys[(cell - ix) / columns]
462
+ x: grid.xs[ix],
463
+ y: grid.ys[iy]
394
464
  });
395
465
  }
396
466
  return route.reverse();
@@ -664,7 +734,7 @@ function virtualPoint(open, ideal, goal) {
664
734
  function linkKey(link) {
665
735
  return `${link.source}\0${link.target}`;
666
736
  }
667
- function planTracks(links, boxes, side, clearance, sharing) {
737
+ function layersOf(boxes, side, clearance) {
668
738
  const frame = frames[side];
669
739
  const spans = new Map(boxes.map(box => [box.id, localSpan(box, frame)]));
670
740
  const ranks = ranksOf([...spans.values()]);
@@ -672,121 +742,189 @@ function planTracks(links, boxes, side, clearance, sharing) {
672
742
  ranks.forEach((rank, index) => {
673
743
  for (const span of rank.spans) rankOf.set(span, index);
674
744
  });
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();
745
+ return {
746
+ spans,
747
+ ranks,
748
+ rankOf,
749
+ open: ranks.map(rank => channels(rank, clearance))
750
+ };
751
+ }
752
+ function forwardEnds(link, layers) {
753
+ const source = layers.spans.get(link.source);
754
+ const target = layers.spans.get(link.target);
755
+ if (!source || !target) return void 0;
756
+ const rs = layers.rankOf.get(source);
757
+ const rt = layers.rankOf.get(target);
758
+ if (rt <= rs) return void 0;
759
+ return {
760
+ source,
761
+ target,
762
+ rs,
763
+ rt
764
+ };
765
+ }
766
+ function virtualCourse(id, {
767
+ source,
768
+ target,
769
+ rs,
770
+ rt
771
+ }, open) {
772
+ const sourceU = (source.u0 + source.u1) / 2;
773
+ const targetU = (target.u0 + target.u1) / 2;
774
+ const us = [sourceU];
775
+ const passes = [];
776
+ for (let rank = rs + 1; rank < rt; rank += 1) {
777
+ const ideal = sourceU + (targetU - sourceU) * (rank - rs) / (rt - rs);
778
+ const at = virtualPoint(open[rank], ideal, targetU);
779
+ us.push(at.u);
780
+ passes.push({
781
+ link: id,
782
+ rank,
783
+ slot: us.length - 1,
784
+ ...at
785
+ });
786
+ }
787
+ us.push(targetU);
788
+ return {
789
+ us,
790
+ passes
791
+ };
792
+ }
793
+ function groupsAt(gaps, gap) {
794
+ let groups = gaps.get(gap);
795
+ if (!groups) {
796
+ groups = /* @__PURE__ */new Map();
797
+ gaps.set(gap, groups);
798
+ }
799
+ return groups;
800
+ }
801
+ function widenGroup(groups, key, lone, a, b) {
802
+ const group = groups.get(key);
803
+ if (group) {
804
+ group.u0 = Math.min(group.u0, a, b);
805
+ group.u1 = Math.max(group.u1, a, b);
806
+ } else groups.set(key, {
807
+ key,
808
+ lone,
809
+ u0: Math.min(a, b),
810
+ u1: Math.max(a, b)
811
+ });
812
+ }
813
+ function placePieces(gaps, link, id, us, {
814
+ rs,
815
+ rt
816
+ }, sharing) {
817
+ const own = [];
818
+ for (let gap = rs; gap < rt; gap += 1) {
819
+ const last = gap === rt - 1;
820
+ const key = last ? trackKey(sharing, link) : `${id}\0${gap}`;
821
+ own.push({
822
+ gap,
823
+ key
824
+ });
825
+ widenGroup(groupsAt(gaps, gap), key, rt - rs === 1, us[gap - rs], us[gap - rs + 1]);
826
+ }
827
+ return own;
828
+ }
829
+ function collectPieces(links, layers, sharing) {
830
+ const plan = {
831
+ gaps: /* @__PURE__ */new Map(),
832
+ pieces: /* @__PURE__ */new Map(),
833
+ passing: [],
834
+ virtual: /* @__PURE__ */new Map(),
835
+ channelOf: /* @__PURE__ */new Map()
836
+ };
681
837
  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;
838
+ const ends = forwardEnds(link, layers);
839
+ if (!ends) continue;
688
840
  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);
841
+ if (plan.pieces.has(id)) continue;
842
+ const {
843
+ us,
844
+ passes
845
+ } = virtualCourse(id, ends, layers.open);
846
+ plan.passing.push(...passes);
847
+ plan.virtual.set(id, us);
848
+ plan.channelOf.set(id, passes.map(pass => layers.open[pass.rank][pass.channel]));
849
+ plan.pieces.set(id, placePieces(plan.gaps, link, id, us, ends, sharing));
734
850
  }
851
+ return plan;
852
+ }
853
+ function spreadChannel(group, open, virtual, clearance) {
854
+ if (group.length < 2) return;
855
+ const [lo, hi] = open[group[0].rank][group[0].channel];
856
+ const sorted = [...group].sort((a, b) => a.u - b.u || a.link.localeCompare(b.link));
857
+ const crowded = sorted.some((pass, index) => index > 0 && pass.u - sorted[index - 1].u < clearance);
858
+ if (!crowded) return;
859
+ const width = Number.isFinite(lo) && Number.isFinite(hi) ? hi - lo : clearance * (group.length + 1);
860
+ const base = Number.isFinite(lo) ? lo : Number.isFinite(hi) ? hi - width : sorted[0].u;
861
+ const pitch = width / (group.length + 1);
862
+ sorted.forEach((pass, index) => {
863
+ virtual.get(pass.link)[pass.slot] = base + pitch * (index + 1);
864
+ });
865
+ }
866
+ function spreadChannels(passing, open, virtual, clearance) {
735
867
  const byChannel = /* @__PURE__ */new Map();
736
868
  for (const pass of passing) {
737
869
  const key = `${pass.rank}:${pass.channel}`;
738
870
  byChannel.set(key, [...(byChannel.get(key) ?? []), pass]);
739
871
  }
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
- });
872
+ for (const group of byChannel.values()) spreadChannel(group, open, virtual, clearance);
873
+ }
874
+ function conflictSets(list, clearance) {
875
+ const parent = list.map((_, index) => index);
876
+ const root = index => {
877
+ let at = index;
878
+ while (parent[at] !== at) at = parent[at];
879
+ return at;
880
+ };
881
+ 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);
882
+ const sets = /* @__PURE__ */new Map();
883
+ for (let index = 0; index < list.length; index += 1) {
884
+ const at = root(index);
885
+ sets.set(at, [...(sets.get(at) ?? []), list[index]]);
752
886
  }
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
- }
887
+ return [...sets.values()];
888
+ }
889
+ function lanesOf(members, clearance) {
890
+ const sorted = [...members].sort((a, b) => a.u0 - b.u0 || a.key.localeCompare(b.key));
891
+ const ends = [];
892
+ const lane = /* @__PURE__ */new Map();
893
+ for (const group of sorted) {
894
+ let free = ends.findIndex(end => end + clearance < group.u0);
895
+ if (free < 0) {
896
+ free = ends.length;
897
+ ends.push(group.u1);
898
+ } else ends[free] = group.u1;
899
+ lane.set(group.key, free);
900
+ }
901
+ return {
902
+ lane,
903
+ count: ends.length
904
+ };
905
+ }
906
+ function levelGap(gap, groups, ranks, clearance, levels) {
907
+ const lo = ranks[gap].v1 + clearance;
908
+ const hi = ranks[gap + 1].v0 - clearance;
909
+ if (lo >= hi) return;
910
+ for (const members of conflictSets([...groups.values()], clearance)) {
911
+ const {
912
+ lane,
913
+ count
914
+ } = lanesOf(members, clearance);
915
+ if (count < 2 && members.every(group => group.lone)) continue;
916
+ const pitch = (hi - lo) / (count + 1);
917
+ for (const [key, index] of lane) levels.set(`${gap}\0${key}`, lo + pitch * (index + 1));
787
918
  }
919
+ }
920
+ function levelsOf(gaps, ranks, clearance) {
921
+ const levels = /* @__PURE__ */new Map();
922
+ for (const [gap, groups] of gaps) levelGap(gap, groups, ranks, clearance, levels);
923
+ return levels;
924
+ }
925
+ function coursesOf(plan, levels) {
788
926
  const courses = /* @__PURE__ */new Map();
789
- for (const [id, own] of pieces) {
927
+ for (const [id, own] of plan.pieces) {
790
928
  const vs = own.map(({
791
929
  gap,
792
930
  key
@@ -794,12 +932,18 @@ function planTracks(links, boxes, side, clearance, sharing) {
794
932
  if (vs.some(v => v === void 0)) continue;
795
933
  courses.set(id, {
796
934
  vs,
797
- us: virtual.get(id).slice(1, -1),
798
- open: channelOf.get(id) ?? []
935
+ us: plan.virtual.get(id).slice(1, -1),
936
+ open: plan.channelOf.get(id) ?? []
799
937
  });
800
938
  }
801
939
  return courses;
802
940
  }
941
+ function planTracks(links, boxes, side, clearance, sharing) {
942
+ const layers = layersOf(boxes, side, clearance);
943
+ const plan = collectPieces(links, layers, sharing);
944
+ spreadChannels(plan.passing, layers.open, plan.virtual, clearance);
945
+ return coursesOf(plan, levelsOf(plan.gaps, layers.ranks, clearance));
946
+ }
803
947
  const plans = /* @__PURE__ */new WeakMap();
804
948
  function tracksFor(obstacles, side) {
805
949
  const {