@tscircuit/capacity-autorouter 0.0.34 → 0.0.36

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/index.d.ts CHANGED
@@ -105,6 +105,11 @@ declare class BaseSolver {
105
105
  _step(): void;
106
106
  solve(): void;
107
107
  visualize(): GraphicsObject;
108
+ /**
109
+ * A lightweight version of the visualize method that can be used to stream
110
+ * progress
111
+ */
112
+ preview(): GraphicsObject;
108
113
  }
109
114
 
110
115
  declare class CapacityMeshEdgeSolver extends BaseSolver {
@@ -465,13 +470,6 @@ interface HighDensityHyperParameters {
465
470
  FLIP_TRACE_ALIGNMENT_DIRECTION: boolean;
466
471
  }
467
472
 
468
- type FutureConnection = {
469
- connectionName: string;
470
- points: {
471
- x: number;
472
- y: number;
473
- }[];
474
- };
475
473
  type Node = {
476
474
  x: number;
477
475
  y: number;
@@ -481,6 +479,32 @@ type Node = {
481
479
  f: number;
482
480
  parent: Node | null;
483
481
  };
482
+ declare class SingleRouteCandidatePriorityQueue {
483
+ private heap;
484
+ constructor(nodes: Node[]);
485
+ private getLeftChildIndex;
486
+ private getRightChildIndex;
487
+ private getParentIndex;
488
+ private hasLeftChild;
489
+ private hasRightChild;
490
+ private hasParent;
491
+ private leftChild;
492
+ private rightChild;
493
+ private parent;
494
+ private swap;
495
+ dequeue(): Node | null;
496
+ enqueue(item: Node): void;
497
+ heapifyUp(): void;
498
+ heapifyDown(): void;
499
+ }
500
+
501
+ type FutureConnection = {
502
+ connectionName: string;
503
+ points: {
504
+ x: number;
505
+ y: number;
506
+ }[];
507
+ };
484
508
  declare class SingleHighDensityRouteSolver extends BaseSolver {
485
509
  obstacleRoutes: HighDensityIntraNodeRoute[];
486
510
  bounds: {
@@ -519,7 +543,7 @@ declare class SingleHighDensityRouteSolver extends BaseSolver {
519
543
  VIA_PENALTY_FACTOR: number;
520
544
  CELL_SIZE_FACTOR: number;
521
545
  exploredNodes: Set<string>;
522
- candidates: Node[];
546
+ candidates: SingleRouteCandidatePriorityQueue;
523
547
  connectionName: string;
524
548
  solvedPath: HighDensityIntraNodeRoute | null;
525
549
  futureConnections: FutureConnection[];
@@ -1428,6 +1452,16 @@ declare class CapacityMeshSolver extends BaseSolver {
1428
1452
  _step(): void;
1429
1453
  getCurrentPhase(): string;
1430
1454
  visualize(): GraphicsObject;
1455
+ /**
1456
+ * A lightweight version of the visualize method that can be used to stream
1457
+ * progress
1458
+ *
1459
+ * We return the most relevant graphic for the stage:
1460
+ * 1. netToPointPairs output
1461
+ * 2. Capacity Planning Output
1462
+ * 3. High Density Route Solver Output, max 200 lines
1463
+ */
1464
+ preview(): GraphicsObject;
1431
1465
  /**
1432
1466
  * Get original connection name from connection name with MST suffix
1433
1467
  * @param mstConnectionName The MST-suffixed connection name (e.g. "connection1_mst0")
package/dist/index.js CHANGED
@@ -83,6 +83,18 @@ var BaseSolver = class {
83
83
  circles: []
84
84
  };
85
85
  }
86
+ /**
87
+ * A lightweight version of the visualize method that can be used to stream
88
+ * progress
89
+ */
90
+ preview() {
91
+ return {
92
+ lines: [],
93
+ points: [],
94
+ rects: [],
95
+ circles: []
96
+ };
97
+ }
86
98
  };
87
99
 
88
100
  // node_modules/@babel/runtime/helpers/esm/extends.js
@@ -1925,6 +1937,88 @@ function distance(p1, p2) {
1925
1937
  return Math.sqrt(dx * dx + dy * dy);
1926
1938
  }
1927
1939
 
1940
+ // lib/data-structures/SingleRouteCandidatePriorityQueue.ts
1941
+ var SingleRouteCandidatePriorityQueue = class {
1942
+ heap = [];
1943
+ constructor(nodes) {
1944
+ this.heap = [];
1945
+ for (const node of nodes) {
1946
+ this.enqueue(node);
1947
+ }
1948
+ }
1949
+ getLeftChildIndex(parentIndex) {
1950
+ return 2 * parentIndex + 1;
1951
+ }
1952
+ getRightChildIndex(parentIndex) {
1953
+ return 2 * parentIndex + 2;
1954
+ }
1955
+ getParentIndex(childIndex) {
1956
+ return Math.floor((childIndex - 1) / 2);
1957
+ }
1958
+ hasLeftChild(index) {
1959
+ return this.getLeftChildIndex(index) < this.heap.length;
1960
+ }
1961
+ hasRightChild(index) {
1962
+ return this.getRightChildIndex(index) < this.heap.length;
1963
+ }
1964
+ hasParent(index) {
1965
+ return this.getParentIndex(index) >= 0;
1966
+ }
1967
+ leftChild(index) {
1968
+ return this.heap[this.getLeftChildIndex(index)];
1969
+ }
1970
+ rightChild(index) {
1971
+ return this.heap[this.getRightChildIndex(index)];
1972
+ }
1973
+ parent(index) {
1974
+ return this.heap[this.getParentIndex(index)];
1975
+ }
1976
+ swap(i, j) {
1977
+ const temp = this.heap[i];
1978
+ this.heap[i] = this.heap[j];
1979
+ this.heap[j] = temp;
1980
+ }
1981
+ // Removing an element will remove the
1982
+ // top element with highest priority then
1983
+ // heapifyDown will be called
1984
+ dequeue() {
1985
+ if (this.heap.length === 0) {
1986
+ return null;
1987
+ }
1988
+ const item = this.heap[0];
1989
+ this.heap[0] = this.heap[this.heap.length - 1];
1990
+ this.heap.pop();
1991
+ this.heapifyDown();
1992
+ return item;
1993
+ }
1994
+ enqueue(item) {
1995
+ this.heap.push(item);
1996
+ this.heapifyUp();
1997
+ }
1998
+ heapifyUp() {
1999
+ let index = this.heap.length - 1;
2000
+ while (this.hasParent(index) && this.parent(index).f > this.heap[index].f) {
2001
+ this.swap(this.getParentIndex(index), index);
2002
+ index = this.getParentIndex(index);
2003
+ }
2004
+ }
2005
+ heapifyDown() {
2006
+ let index = 0;
2007
+ while (this.hasLeftChild(index)) {
2008
+ let smallerChildIndex = this.getLeftChildIndex(index);
2009
+ if (this.hasRightChild(index) && this.rightChild(index).f < this.leftChild(index).f) {
2010
+ smallerChildIndex = this.getRightChildIndex(index);
2011
+ }
2012
+ if (this.heap[index].f < this.heap[smallerChildIndex].f) {
2013
+ break;
2014
+ } else {
2015
+ this.swap(index, smallerChildIndex);
2016
+ }
2017
+ index = smallerChildIndex;
2018
+ }
2019
+ }
2020
+ };
2021
+
1928
2022
  // lib/solvers/HighDensitySolver/SingleHighDensityRouteSolver.ts
1929
2023
  var SingleHighDensityRouteSolver = class extends BaseSolver {
1930
2024
  obstacleRoutes;
@@ -1979,7 +2073,7 @@ var SingleHighDensityRouteSolver = class extends BaseSolver {
1979
2073
  this.obstacleMargin = opts.obstacleMargin ?? 0.2;
1980
2074
  this.layerCount = opts.layerCount ?? 2;
1981
2075
  this.exploredNodes = /* @__PURE__ */ new Set();
1982
- this.candidates = [
2076
+ this.candidates = new SingleRouteCandidatePriorityQueue([
1983
2077
  {
1984
2078
  ...opts.A,
1985
2079
  z: opts.A.z ?? 0,
@@ -1988,7 +2082,7 @@ var SingleHighDensityRouteSolver = class extends BaseSolver {
1988
2082
  f: 0,
1989
2083
  parent: null
1990
2084
  }
1991
- ];
2085
+ ]);
1992
2086
  this.straightLineDistance = distance(this.A, this.B);
1993
2087
  this.futureConnections = opts.futureConnections ?? [];
1994
2088
  this.MAX_ITERATIONS = 5e3;
@@ -2208,17 +2302,18 @@ var SingleHighDensityRouteSolver = class extends BaseSolver {
2208
2302
  );
2209
2303
  }
2210
2304
  _step() {
2211
- this.candidates.sort((a, b) => b.f - a.f);
2212
- let currentNode = this.candidates.pop();
2213
- while (currentNode && this.exploredNodes.has(this.getNodeKey(currentNode))) {
2214
- currentNode = this.candidates.pop();
2305
+ let currentNode = this.candidates.dequeue();
2306
+ let currentNodeKey = currentNode ? this.getNodeKey(currentNode) : void 0;
2307
+ while (currentNode && currentNodeKey && this.exploredNodes.has(currentNodeKey)) {
2308
+ currentNode = this.candidates.dequeue();
2309
+ currentNodeKey = currentNode ? this.getNodeKey(currentNode) : void 0;
2215
2310
  }
2216
- if (!currentNode) {
2311
+ if (!currentNode || !currentNodeKey) {
2217
2312
  this.failed = true;
2218
2313
  return;
2219
2314
  }
2220
- this.exploredNodes.add(this.getNodeKey(currentNode));
2221
- this.debug_exploredNodesOrdered.push(this.getNodeKey(currentNode));
2315
+ this.exploredNodes.add(currentNodeKey);
2316
+ this.debug_exploredNodesOrdered.push(currentNodeKey);
2222
2317
  const goalDist = distance(currentNode, this.B);
2223
2318
  this.progress = this.computeProgress(
2224
2319
  currentNode,
@@ -2231,7 +2326,7 @@ var SingleHighDensityRouteSolver = class extends BaseSolver {
2231
2326
  }
2232
2327
  const neighbors = this.getNeighbors(currentNode);
2233
2328
  for (const neighbor of neighbors) {
2234
- this.candidates.push(neighbor);
2329
+ this.candidates.enqueue(neighbor);
2235
2330
  }
2236
2331
  }
2237
2332
  visualize() {
@@ -8003,6 +8098,50 @@ var CapacityMeshSolver = class extends BaseSolver {
8003
8098
  ].filter(Boolean);
8004
8099
  return combineVisualizations(...visualizations);
8005
8100
  }
8101
+ /**
8102
+ * A lightweight version of the visualize method that can be used to stream
8103
+ * progress
8104
+ *
8105
+ * We return the most relevant graphic for the stage:
8106
+ * 1. netToPointPairs output
8107
+ * 2. Capacity Planning Output
8108
+ * 3. High Density Route Solver Output, max 200 lines
8109
+ */
8110
+ preview() {
8111
+ if (this.highDensityRouteSolver) {
8112
+ const lines = [];
8113
+ for (let i = this.highDensityRouteSolver.routes.length - 1; i >= 0; i--) {
8114
+ const route = this.highDensityRouteSolver.routes[i];
8115
+ lines.push({
8116
+ points: route.route.map((n) => ({
8117
+ x: n.x,
8118
+ y: n.y
8119
+ })),
8120
+ strokeColor: this.colorMap[route.connectionName]
8121
+ });
8122
+ if (lines.length > 200) break;
8123
+ }
8124
+ return { lines };
8125
+ }
8126
+ if (this.pathingSolver) {
8127
+ const lines = [];
8128
+ for (const connection of this.pathingSolver.connectionsWithNodes) {
8129
+ if (!connection.path) continue;
8130
+ lines.push({
8131
+ points: connection.path.map((n) => ({
8132
+ x: n.center.x,
8133
+ y: n.center.y
8134
+ })),
8135
+ strokeColor: this.colorMap[connection.connection.name]
8136
+ });
8137
+ }
8138
+ return { lines };
8139
+ }
8140
+ if (this.netToPointPairsSolver) {
8141
+ return this.netToPointPairsSolver?.visualize();
8142
+ }
8143
+ return {};
8144
+ }
8006
8145
  /**
8007
8146
  * Get original connection name from connection name with MST suffix
8008
8147
  * @param mstConnectionName The MST-suffixed connection name (e.g. "connection1_mst0")