@tscircuit/schematic-trace-solver 0.0.17 → 0.0.18

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.js CHANGED
@@ -6682,7 +6682,18 @@ var SchematicTraceSingleLineSolver = class extends BaseSolver {
6682
6682
  guidelines: this.guidelines
6683
6683
  });
6684
6684
  this.movableSegments = movableSegments;
6685
- this.queuedCandidatePaths = [this.baseElbow, ...elbowVariants];
6685
+ const getPathLength = (pts) => {
6686
+ let len = 0;
6687
+ for (let i = 0; i < pts.length - 1; i++) {
6688
+ const dx = pts[i + 1].x - pts[i].x;
6689
+ const dy = pts[i + 1].y - pts[i].y;
6690
+ len += Math.sqrt(dx * dx + dy * dy);
6691
+ }
6692
+ return len;
6693
+ };
6694
+ this.queuedCandidatePaths = [this.baseElbow, ...elbowVariants].sort(
6695
+ (a, b) => getPathLength(a) - getPathLength(b)
6696
+ );
6686
6697
  }
6687
6698
  getConstructorParams() {
6688
6699
  return {
@@ -81,7 +81,19 @@ export class SchematicTraceSingleLineSolver extends BaseSolver {
81
81
 
82
82
  this.movableSegments = movableSegments
83
83
 
84
- this.queuedCandidatePaths = [this.baseElbow, ...elbowVariants]
84
+ const getPathLength = (pts: Point[]) => {
85
+ let len = 0
86
+ for (let i = 0; i < pts.length - 1; i++) {
87
+ const dx = pts[i + 1].x - pts[i].x
88
+ const dy = pts[i + 1].y - pts[i].y
89
+ len += Math.sqrt(dx * dx + dy * dy)
90
+ }
91
+ return len
92
+ }
93
+
94
+ this.queuedCandidatePaths = [this.baseElbow, ...elbowVariants].sort(
95
+ (a, b) => getPathLength(a) - getPathLength(b),
96
+ )
85
97
  }
86
98
 
87
99
  override getConstructorParams(): ConstructorParameters<
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tscircuit/schematic-trace-solver",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.17",
4
+ "version": "0.0.18",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "start": "cosmos",
@@ -0,0 +1,71 @@
1
+ import { test, expect } from "bun:test"
2
+ import { SchematicTraceSingleLineSolver } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver/SchematicTraceSingleLineSolver"
3
+ import { calculateElbow } from "calculate-elbow"
4
+ import { generateElbowVariants } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver/generateElbowVariants"
5
+ import type { InputChip, InputProblem } from "lib/types/InputProblem"
6
+ import type { Guideline } from "lib/solvers/GuidelinesSolver/GuidelinesSolver"
7
+
8
+ const pathLength = (pts: { x: number; y: number }[]) => {
9
+ let len = 0
10
+ for (let i = 0; i < pts.length - 1; i++) {
11
+ const dx = pts[i + 1].x - pts[i].x
12
+ const dy = pts[i + 1].y - pts[i].y
13
+ len += Math.sqrt(dx * dx + dy * dy)
14
+ }
15
+ return len
16
+ }
17
+
18
+ test("SchematicTraceSingleLineSolver chooses shortest candidate path", () => {
19
+ const chipA: InputChip = {
20
+ chipId: "A",
21
+ center: { x: 0, y: 0 },
22
+ width: 0.2,
23
+ height: 0.2,
24
+ pins: [{ pinId: "A1", x: 0, y: 0 }],
25
+ }
26
+ const chipB: InputChip = {
27
+ chipId: "B",
28
+ center: { x: 4, y: 2 },
29
+ width: 0.2,
30
+ height: 0.2,
31
+ pins: [{ pinId: "B1", x: 4, y: 2 }],
32
+ }
33
+
34
+ const pins = [
35
+ { pinId: "A1", x: 0, y: 0, _facingDirection: "x+" as const, chipId: "A" },
36
+ { pinId: "B1", x: 4, y: 2, _facingDirection: "x+" as const, chipId: "B" },
37
+ ]
38
+
39
+ const guidelines: Guideline[] = [
40
+ { orientation: "vertical", x: 4, y: undefined },
41
+ { orientation: "vertical", x: 6, y: undefined },
42
+ ]
43
+
44
+ const inputProblem: InputProblem = {
45
+ chips: [chipA, chipB],
46
+ directConnections: [],
47
+ netConnections: [],
48
+ availableNetLabelOrientations: {},
49
+ }
50
+
51
+ const solver = new SchematicTraceSingleLineSolver({
52
+ pins: pins as any,
53
+ guidelines,
54
+ inputProblem,
55
+ chipMap: { A: chipA, B: chipB },
56
+ })
57
+
58
+ solver.solve()
59
+ expect(solver.solved).toBe(true)
60
+
61
+ const baseElbow = calculateElbow(
62
+ { x: pins[0].x, y: pins[0].y, facingDirection: pins[0]._facingDirection },
63
+ { x: pins[1].x, y: pins[1].y, facingDirection: pins[1]._facingDirection },
64
+ { overshoot: 0.2 },
65
+ )
66
+ const { elbowVariants } = generateElbowVariants({ baseElbow, guidelines })
67
+ const candidates = [baseElbow, ...elbowVariants]
68
+ const shortestLength = Math.min(...candidates.map(pathLength))
69
+
70
+ expect(pathLength(solver.solvedTracePath!)).toBe(shortestLength)
71
+ })