@tscircuit/schematic-trace-solver 0.0.17 → 0.0.19

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.19",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "start": "cosmos",
@@ -6,6 +6,24 @@ interface DownloadDropdownProps {
6
6
  className?: string
7
7
  }
8
8
 
9
+ const deepRemoveUnderscoreProperties = (obj: any): any => {
10
+ if (obj === null || typeof obj !== "object") {
11
+ return obj
12
+ }
13
+
14
+ if (Array.isArray(obj)) {
15
+ return obj.map(deepRemoveUnderscoreProperties)
16
+ }
17
+
18
+ const result: any = {}
19
+ for (const [key, value] of Object.entries(obj)) {
20
+ if (!key.startsWith("_")) {
21
+ result[key] = deepRemoveUnderscoreProperties(value)
22
+ }
23
+ }
24
+ return result
25
+ }
26
+
9
27
  export const DownloadDropdown = ({
10
28
  solver,
11
29
  className = "",
@@ -36,7 +54,9 @@ export const DownloadDropdown = ({
36
54
  return
37
55
  }
38
56
 
39
- const params = solver.getConstructorParams()
57
+ const params = deepRemoveUnderscoreProperties(
58
+ solver.getConstructorParams(),
59
+ )
40
60
  const blob = new Blob([JSON.stringify(params, null, 2)], {
41
61
  type: "application/json",
42
62
  })
@@ -56,7 +76,9 @@ export const DownloadDropdown = ({
56
76
 
57
77
  const downloadPageTsx = () => {
58
78
  try {
59
- const params = solver.getConstructorParams()
79
+ const params = deepRemoveUnderscoreProperties(
80
+ solver.getConstructorParams(),
81
+ )
60
82
  const solverName = solver.constructor.name
61
83
  const isSchematicTracePipelineSolver =
62
84
  solverName === "SchematicTracePipelineSolver"
@@ -104,19 +126,21 @@ export default () => {
104
126
 
105
127
  const downloadTestTs = () => {
106
128
  try {
107
- const params = solver.getConstructorParams()
129
+ const params = deepRemoveUnderscoreProperties(
130
+ solver.getConstructorParams(),
131
+ )
108
132
  const solverName = solver.constructor.name
109
133
 
110
- const content = `import { ${solverName} } from "lib/solvers/${solverName}/${solverName}"
134
+ const content = `import { ${solverName} } from "lib/solvers/${solverName}/${solverName}\nimport { test, expect } from "bun:test"
111
135
 
112
136
  test("${solverName} should solve problem correctly", () => {
113
137
  const input = ${JSON.stringify(params, null, 2)}
114
138
 
115
139
  const solver = new ${solverName}(input as any)
116
- const result = solver.solve()
140
+ solver.solve()
117
141
 
118
- expect(result).toBeDefined()
119
142
  // Add more specific assertions based on expected output
143
+ // expect(solver.netLabelPlacementSolver!.netLabelPlacements).toMatchInlineSnapshot()
120
144
  })
121
145
  `
122
146
 
@@ -0,0 +1,135 @@
1
+ import { PipelineDebugger } from "site/components/PipelineDebugger"
2
+ import type { InputProblem } from "lib/types/InputProblem"
3
+
4
+ const inputProblem: InputProblem = {
5
+ chips: [
6
+ {
7
+ chipId: "schematic_component_0",
8
+ center: {
9
+ x: 0,
10
+ y: 0,
11
+ },
12
+ width: 2.8,
13
+ height: 1.4,
14
+ pins: [
15
+ {
16
+ pinId: "U3.8",
17
+ x: -1.4,
18
+ y: 0.42500000000000004,
19
+ },
20
+ {
21
+ pinId: "U3.4",
22
+ x: -1.4,
23
+ y: -0.42500000000000004,
24
+ },
25
+ {
26
+ pinId: "U3.1",
27
+ x: 1.4,
28
+ y: 0.5,
29
+ },
30
+ {
31
+ pinId: "U3.6",
32
+ x: 1.4,
33
+ y: 0.30000000000000004,
34
+ },
35
+ {
36
+ pinId: "U3.5",
37
+ x: 1.4,
38
+ y: 0.10000000000000009,
39
+ },
40
+ {
41
+ pinId: "U3.2",
42
+ x: 1.4,
43
+ y: -0.09999999999999998,
44
+ },
45
+ {
46
+ pinId: "U3.3",
47
+ x: 1.4,
48
+ y: -0.3,
49
+ },
50
+ {
51
+ pinId: "U3.7",
52
+ x: 1.4,
53
+ y: -0.5,
54
+ },
55
+ ],
56
+ },
57
+ {
58
+ chipId: "schematic_component_1",
59
+ center: {
60
+ x: -2.3145833,
61
+ y: 0,
62
+ },
63
+ width: 0.5291665999999999,
64
+ height: 1.1024186000000005,
65
+ pins: [
66
+ {
67
+ pinId: "C20.1",
68
+ x: -2.3148566499999994,
69
+ y: 0.5512093000000002,
70
+ },
71
+ {
72
+ pinId: "C20.2",
73
+ x: -2.31430995,
74
+ y: -0.5512093000000002,
75
+ },
76
+ ],
77
+ },
78
+ {
79
+ chipId: "schematic_component_2",
80
+ center: {
81
+ x: 1.7577928249999983,
82
+ y: 1.7512907000000002,
83
+ },
84
+ width: 0.3155856499999966,
85
+ height: 1.1025814000000005,
86
+ pins: [
87
+ {
88
+ pinId: "R11.1",
89
+ x: 1.7580660749999977,
90
+ y: 2.3025814000000002,
91
+ },
92
+ {
93
+ pinId: "R11.2",
94
+ x: 1.757519574999999,
95
+ y: 1.2,
96
+ },
97
+ ],
98
+ },
99
+ ],
100
+ directConnections: [
101
+ {
102
+ pinIds: ["C20.1", "U3.8"],
103
+ netId: "capacitor.C20 > port.pin1 to .U3 > .VDD",
104
+ },
105
+ {
106
+ pinIds: ["C20.2", "U3.4"],
107
+ netId: "capacitor.C20 > port.pin2 to .U3 > .GND",
108
+ },
109
+ {
110
+ pinIds: ["R11.2", "U3.1"],
111
+ netId: "resistor.R11 > port.pin2 to .U3 > .N_CS",
112
+ },
113
+ ],
114
+ netConnections: [
115
+ {
116
+ netId: "V3_3",
117
+ pinIds: ["U3.8", "U3.3", "U3.7", "C20.1", "R11.1"],
118
+ },
119
+ {
120
+ netId: "GND",
121
+ pinIds: ["U3.4", "C20.2"],
122
+ },
123
+ {
124
+ netId: "FLASH_N_CS",
125
+ pinIds: ["U3.1", "R11.2"],
126
+ },
127
+ ],
128
+ availableNetLabelOrientations: {
129
+ V3_3: ["y+"],
130
+ GND: ["y-"],
131
+ },
132
+ maxMspPairDistance: 2,
133
+ }
134
+
135
+ export default () => <PipelineDebugger inputProblem={inputProblem} />
@@ -0,0 +1,140 @@
1
+ import { SchematicTracePipelineSolver } from "lib/solvers/SchematicTracePipelineSolver/SchematicTracePipelineSolver"
2
+ import { test, expect } from "bun:test"
3
+
4
+ test("SchematicTracePipelineSolver should solve problem correctly", () => {
5
+ const input = {
6
+ chips: [
7
+ {
8
+ chipId: "schematic_component_0",
9
+ center: {
10
+ x: 0,
11
+ y: 0,
12
+ },
13
+ width: 2.8,
14
+ height: 1.4,
15
+ pins: [
16
+ {
17
+ pinId: "U3.8",
18
+ x: -1.4,
19
+ y: 0.42500000000000004,
20
+ },
21
+ {
22
+ pinId: "U3.4",
23
+ x: -1.4,
24
+ y: -0.42500000000000004,
25
+ },
26
+ {
27
+ pinId: "U3.1",
28
+ x: 1.4,
29
+ y: 0.5,
30
+ },
31
+ {
32
+ pinId: "U3.6",
33
+ x: 1.4,
34
+ y: 0.30000000000000004,
35
+ },
36
+ {
37
+ pinId: "U3.5",
38
+ x: 1.4,
39
+ y: 0.10000000000000009,
40
+ },
41
+ {
42
+ pinId: "U3.2",
43
+ x: 1.4,
44
+ y: -0.09999999999999998,
45
+ },
46
+ {
47
+ pinId: "U3.3",
48
+ x: 1.4,
49
+ y: -0.3,
50
+ },
51
+ {
52
+ pinId: "U3.7",
53
+ x: 1.4,
54
+ y: -0.5,
55
+ },
56
+ ],
57
+ },
58
+ {
59
+ chipId: "schematic_component_1",
60
+ center: {
61
+ x: -2.3145833,
62
+ y: 0,
63
+ },
64
+ width: 0.5291665999999999,
65
+ height: 1.1024186000000005,
66
+ pins: [
67
+ {
68
+ pinId: "C20.1",
69
+ x: -2.3148566499999994,
70
+ y: 0.5512093000000002,
71
+ },
72
+ {
73
+ pinId: "C20.2",
74
+ x: -2.31430995,
75
+ y: -0.5512093000000002,
76
+ },
77
+ ],
78
+ },
79
+ {
80
+ chipId: "schematic_component_2",
81
+ center: {
82
+ x: 1.7577928249999983,
83
+ y: 1.7512907000000002,
84
+ },
85
+ width: 0.3155856499999966,
86
+ height: 1.1025814000000005,
87
+ pins: [
88
+ {
89
+ pinId: "R11.1",
90
+ x: 1.7580660749999977,
91
+ y: 2.3025814000000002,
92
+ },
93
+ {
94
+ pinId: "R11.2",
95
+ x: 1.757519574999999,
96
+ y: 1.2,
97
+ },
98
+ ],
99
+ },
100
+ ],
101
+ directConnections: [
102
+ {
103
+ pinIds: ["C20.1", "U3.8"],
104
+ netId: "capacitor.C20 > port.pin1 to .U3 > .VDD",
105
+ },
106
+ {
107
+ pinIds: ["C20.2", "U3.4"],
108
+ netId: "capacitor.C20 > port.pin2 to .U3 > .GND",
109
+ },
110
+ {
111
+ pinIds: ["R11.2", "U3.1"],
112
+ netId: "resistor.R11 > port.pin2 to .U3 > .N_CS",
113
+ },
114
+ ],
115
+ netConnections: [
116
+ {
117
+ netId: "V3_3",
118
+ pinIds: ["U3.8", "U3.3", "U3.7", "C20.1", "R11.1"],
119
+ },
120
+ {
121
+ netId: "GND",
122
+ pinIds: ["U3.4", "C20.2"],
123
+ },
124
+ {
125
+ netId: "FLASH_N_CS",
126
+ pinIds: ["U3.1", "R11.2"],
127
+ },
128
+ ],
129
+ availableNetLabelOrientations: {
130
+ V3_3: ["y+"],
131
+ GND: ["y-"],
132
+ },
133
+ maxMspPairDistance: 2,
134
+ }
135
+
136
+ const solver = new SchematicTracePipelineSolver(input as any)
137
+ solver.solve()
138
+
139
+ // console.log(solver.netLabelPlacementSolver!.netLabelPlacements)
140
+ })
@@ -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
+ })