@tscircuit/schematic-trace-solver 0.0.158 → 0.0.159
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 +1 -0
- package/dist/index.js +126 -3
- package/lib/solvers/LongDistancePairSolver/LongDistancePairSolver.ts +15 -8
- package/lib/solvers/MspConnectionPairSolver/MspConnectionPairSolver.ts +5 -0
- package/lib/solvers/MspConnectionPairSolver/getGroundConnectionPolicy.ts +83 -0
- package/lib/solvers/SameNetJunctionAlignmentSolver/SameNetJunctionAlignmentSolver.ts +6 -1
- package/lib/solvers/SameNetJunctionAlignmentSolver/placeGroundRailLabelsAtOuterEnd.ts +122 -0
- package/package.json +1 -1
- package/tests/bug-reports/bug-report-20260826T072956Z/__snapshots__/bug-report-20260826T072956Z.snap.svg +71 -62
- package/tests/fixtures/parallel-ground-rail.ts +53 -0
- package/tests/repros/__snapshots__/repro-mspm0l1306-capacitor-symmetry.snap.svg +34 -16
- package/tests/repros/repro-mspm0l1306-capacitor-symmetry.test.ts +40 -7
- package/tests/solvers/MspConnectionPairSolver/local-ground-branches.test.ts +105 -0
- package/tests/solvers/SameNetJunctionAlignmentSolver/ground-rail-label.test.ts +188 -0
|
@@ -19,7 +19,8 @@ test("repro: MSPM0L1306 aligned decoupling capacitor rails", async () => {
|
|
|
19
19
|
expect(solver.solved).toBe(true)
|
|
20
20
|
expect(solver.failed).toBe(false)
|
|
21
21
|
|
|
22
|
-
const traces =
|
|
22
|
+
const { traces, netLabelPlacements } =
|
|
23
|
+
solver.netLabelToTraceSolver!.getOutput()
|
|
23
24
|
const getTrace = (...pinIds: string[]) => {
|
|
24
25
|
const trace = traces.find((trace) =>
|
|
25
26
|
pinIds.every((pinId) => trace.pinIds.includes(pinId)),
|
|
@@ -43,12 +44,44 @@ test("repro: MSPM0L1306 aligned decoupling capacitor rails", async () => {
|
|
|
43
44
|
expect([start!.x, end!.x].sort()).toEqual([-3.5, -4.5].sort())
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
// C3 and C4 share the electrical GND net with the decoupling rail, but
|
|
48
|
+
// should each terminate locally instead of being wired to other capacitors.
|
|
49
|
+
for (const pinId of ["C3.2", "C4.2"]) {
|
|
50
|
+
expect(
|
|
51
|
+
traces.some(
|
|
52
|
+
(trace) => trace.pinIds.includes(pinId) && trace.pinIds.length > 1,
|
|
53
|
+
),
|
|
54
|
+
).toBe(false)
|
|
55
|
+
expect(
|
|
56
|
+
netLabelPlacements.some(
|
|
57
|
+
(label) =>
|
|
58
|
+
label.netId === "GND" &&
|
|
59
|
+
label.pinIds.includes(pinId) &&
|
|
60
|
+
label.orientation === "y-",
|
|
61
|
+
),
|
|
62
|
+
).toBe(true)
|
|
63
|
+
}
|
|
64
|
+
const railGround = netLabelPlacements.find(
|
|
65
|
+
(label) =>
|
|
66
|
+
label.netId === "GND" &&
|
|
67
|
+
label.mspConnectionPairIds.some((pairId) =>
|
|
68
|
+
traces.some(
|
|
69
|
+
(trace) =>
|
|
70
|
+
trace.mspPairId === pairId && trace.pinIds.includes("C1.2"),
|
|
71
|
+
),
|
|
72
|
+
),
|
|
73
|
+
)
|
|
74
|
+
expect(railGround).toBeDefined()
|
|
75
|
+
expect(railGround!.orientation).toBe("y-")
|
|
76
|
+
expect(railGround!.anchorPoint.x).toBeCloseTo(-4.5, 6)
|
|
77
|
+
expect(railGround!.anchorPoint.y).toBeCloseTo(0.65, 6)
|
|
78
|
+
const groundNetId =
|
|
79
|
+
solver.mspConnectionPairSolver!.globalConnMap.getNetConnectedToId("GND")
|
|
80
|
+
expect(
|
|
81
|
+
netLabelPlacements
|
|
82
|
+
.filter((label) => label.netId === "GND")
|
|
83
|
+
.every((label) => label.globalConnNetId === groundNetId),
|
|
84
|
+
).toBe(true)
|
|
52
85
|
|
|
53
86
|
await expect(solver).toMatchSolverSnapshot(import.meta.path)
|
|
54
87
|
})
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { LongDistancePairSolver } from "lib/solvers/LongDistancePairSolver/LongDistancePairSolver"
|
|
3
|
+
import { getGroundConnectionPolicy } from "lib/solvers/MspConnectionPairSolver/getGroundConnectionPolicy"
|
|
4
|
+
import { MspConnectionPairSolver } from "lib/solvers/MspConnectionPairSolver/MspConnectionPairSolver"
|
|
5
|
+
import { createParallelGroundRailProblem } from "tests/fixtures/parallel-ground-rail"
|
|
6
|
+
|
|
7
|
+
test("keeps a wired parallel ground rail but labels independent staggered branches", () => {
|
|
8
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
9
|
+
const before = structuredClone(inputProblem)
|
|
10
|
+
const solver = new MspConnectionPairSolver({ inputProblem })
|
|
11
|
+
solver.solve()
|
|
12
|
+
expect(solver.mspConnectionPairs).toHaveLength(2)
|
|
13
|
+
const routed = new Set(
|
|
14
|
+
solver.mspConnectionPairs.flatMap((pair) =>
|
|
15
|
+
pair.pins.map((pin) => pin.pinId),
|
|
16
|
+
),
|
|
17
|
+
)
|
|
18
|
+
expect([...routed].sort()).toEqual(["A.2", "B.2", "U.GND"])
|
|
19
|
+
expect(inputProblem).toEqual(before)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test("long-distance recovery does not reconnect the separated ground branches", () => {
|
|
23
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
24
|
+
const primary = new MspConnectionPairSolver({ inputProblem })
|
|
25
|
+
primary.solve()
|
|
26
|
+
const solver = new LongDistancePairSolver({
|
|
27
|
+
inputProblem,
|
|
28
|
+
primaryMspConnectionPairs: primary.mspConnectionPairs,
|
|
29
|
+
alreadySolvedTraces: [],
|
|
30
|
+
failedConnectionPairs: [],
|
|
31
|
+
})
|
|
32
|
+
solver.solve()
|
|
33
|
+
expect(solver.getOutput().newTraces).toEqual([])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test("the branch policy is symmetric in its endpoints", () => {
|
|
37
|
+
const canRoute = getGroundConnectionPolicy(createParallelGroundRailProblem())
|
|
38
|
+
expect(canRoute("B.2", "C.2")).toBe(false)
|
|
39
|
+
expect(canRoute("C.2", "B.2")).toBe(false)
|
|
40
|
+
expect(canRoute("C.2", "D.2")).toBe(false)
|
|
41
|
+
expect(canRoute("D.2", "C.2")).toBe(false)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test("explicit direct wiring overrides the local-ground preference", () => {
|
|
45
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
46
|
+
inputProblem.directConnections.push({ pinIds: ["B.2", "C.2"] })
|
|
47
|
+
const canRoute = getGroundConnectionPolicy(inputProblem)
|
|
48
|
+
expect(canRoute("B.2", "C.2")).toBe(true)
|
|
49
|
+
expect(canRoute("A.2", "C.2")).toBe(true)
|
|
50
|
+
expect(canRoute("C.2", "D.2")).toBe(false)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
for (const y of [1, 0.5, 0]) {
|
|
54
|
+
test(`keeps level or nearby ground pins at y=${y} on the rail`, () => {
|
|
55
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
56
|
+
inputProblem.chips[2]!.pins[1]!.y = y
|
|
57
|
+
const canRoute = getGroundConnectionPolicy(inputProblem)
|
|
58
|
+
expect(canRoute("B.2", "C.2")).toBe(true)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
test("preserves existing behavior without an explicitly wired parallel rail", () => {
|
|
63
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
64
|
+
inputProblem.directConnections = []
|
|
65
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(true)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test("a repeated direct-connection net ID does not invent a physical rail", () => {
|
|
69
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
70
|
+
inputProblem.directConnections = [
|
|
71
|
+
{ netId: "GND", pinIds: ["A.2", "U.GND"] },
|
|
72
|
+
{ netId: "GND", pinIds: ["B.2", "D.2"] },
|
|
73
|
+
]
|
|
74
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(true)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test("resolves a ground alias through global connectivity", () => {
|
|
78
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
79
|
+
inputProblem.netConnections[0]!.netId = "RETURN"
|
|
80
|
+
inputProblem.netConnections.push({ netId: "GND", pinIds: ["U.GND"] })
|
|
81
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(false)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test("does not apply the ground rule to power or signal nets", () => {
|
|
85
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
86
|
+
inputProblem.netConnections[0]!.netId = "VDD"
|
|
87
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(true)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test("infers ground-facing pins when explicit directions are absent", () => {
|
|
91
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
92
|
+
for (const chip of inputProblem.chips) {
|
|
93
|
+
for (const pin of chip.pins) delete pin._facingDirection
|
|
94
|
+
}
|
|
95
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(false)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test("does not isolate side-facing or multi-pin components", () => {
|
|
99
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
100
|
+
inputProblem.chips[2]!.pins[1]!._facingDirection = "x-"
|
|
101
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(true)
|
|
102
|
+
inputProblem.chips[2]!.pins[1]!._facingDirection = "y-"
|
|
103
|
+
inputProblem.chips[2]!.pins.push({ pinId: "C.3", x: 2, y: -1.6 })
|
|
104
|
+
expect(getGroundConnectionPolicy(inputProblem)("B.2", "C.2")).toBe(true)
|
|
105
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { getConnectivityMapsFromInputProblem } from "lib/solvers/MspConnectionPairSolver/getConnectivityMapFromInputProblem"
|
|
3
|
+
import type { NetLabelPlacement } from "lib/solvers/NetLabelPlacementSolver/NetLabelPlacementSolver"
|
|
4
|
+
import { placeGroundRailLabelsAtOuterEnd } from "lib/solvers/SameNetJunctionAlignmentSolver/placeGroundRailLabelsAtOuterEnd"
|
|
5
|
+
import type { SolvedTracePath } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceLinesSolver"
|
|
6
|
+
import { createParallelGroundRailProblem } from "tests/fixtures/parallel-ground-rail"
|
|
7
|
+
|
|
8
|
+
const createFixture = () => {
|
|
9
|
+
const inputProblem = createParallelGroundRailProblem()
|
|
10
|
+
const { netConnMap } = getConnectivityMapsFromInputProblem(inputProblem)
|
|
11
|
+
const globalConnNetId = netConnMap.getNetConnectedToId("GND")!
|
|
12
|
+
const pins = new Map(
|
|
13
|
+
inputProblem.chips.flatMap((chip) =>
|
|
14
|
+
chip.pins.map(
|
|
15
|
+
(pin) => [pin.pinId, { ...pin, chipId: chip.chipId }] as const,
|
|
16
|
+
),
|
|
17
|
+
),
|
|
18
|
+
)
|
|
19
|
+
const trace = (
|
|
20
|
+
mspPairId: string,
|
|
21
|
+
pinIds: [string, string],
|
|
22
|
+
path: number[][],
|
|
23
|
+
): SolvedTracePath => ({
|
|
24
|
+
mspPairId,
|
|
25
|
+
mspConnectionPairIds: [mspPairId],
|
|
26
|
+
globalConnNetId,
|
|
27
|
+
dcConnNetId: globalConnNetId,
|
|
28
|
+
userNetId: "GND",
|
|
29
|
+
pinIds,
|
|
30
|
+
pins: [pins.get(pinIds[0])!, pins.get(pinIds[1])!],
|
|
31
|
+
tracePath: path.map(([x, y]) => ({ x: x!, y: y! })),
|
|
32
|
+
})
|
|
33
|
+
const traces = [
|
|
34
|
+
trace(
|
|
35
|
+
"rail",
|
|
36
|
+
["B.2", "A.2"],
|
|
37
|
+
[
|
|
38
|
+
[1, 1],
|
|
39
|
+
[1, 0.6],
|
|
40
|
+
[0, 0.6],
|
|
41
|
+
[0, 1],
|
|
42
|
+
],
|
|
43
|
+
),
|
|
44
|
+
trace(
|
|
45
|
+
"feed",
|
|
46
|
+
["U.GND", "B.2"],
|
|
47
|
+
[
|
|
48
|
+
[2, 0.6],
|
|
49
|
+
[1, 0.6],
|
|
50
|
+
[1, 1],
|
|
51
|
+
],
|
|
52
|
+
),
|
|
53
|
+
]
|
|
54
|
+
const netLabelPlacements: NetLabelPlacement[] = [
|
|
55
|
+
{
|
|
56
|
+
globalConnNetId,
|
|
57
|
+
dcConnNetId: globalConnNetId,
|
|
58
|
+
netId: "GND",
|
|
59
|
+
mspConnectionPairIds: ["feed"],
|
|
60
|
+
pinIds: ["U.GND", "B.2"],
|
|
61
|
+
orientation: "y-",
|
|
62
|
+
anchorPoint: { x: 1, y: 0.6 },
|
|
63
|
+
center: { x: 1, y: 0.4 },
|
|
64
|
+
width: 0.4,
|
|
65
|
+
height: 0.4,
|
|
66
|
+
},
|
|
67
|
+
]
|
|
68
|
+
return { inputProblem, traces, netLabelPlacements }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (const mirror of [1, -1]) {
|
|
72
|
+
test(`places the shared GND at the outer column, mirrored=${mirror}`, () => {
|
|
73
|
+
const fixture = createFixture()
|
|
74
|
+
for (const chip of fixture.inputProblem.chips) {
|
|
75
|
+
chip.center.x *= mirror
|
|
76
|
+
for (const pin of chip.pins) pin.x *= mirror
|
|
77
|
+
}
|
|
78
|
+
for (const trace of fixture.traces) {
|
|
79
|
+
for (const point of trace.tracePath) point.x *= mirror
|
|
80
|
+
}
|
|
81
|
+
const visited = new Set()
|
|
82
|
+
for (const pin of fixture.traces.flatMap((trace) => trace.pins)) {
|
|
83
|
+
if (visited.has(pin)) continue
|
|
84
|
+
pin.x *= mirror
|
|
85
|
+
visited.add(pin)
|
|
86
|
+
}
|
|
87
|
+
fixture.netLabelPlacements[0]!.anchorPoint.x *= mirror
|
|
88
|
+
fixture.netLabelPlacements[0]!.center.x *= mirror
|
|
89
|
+
const before = structuredClone(fixture)
|
|
90
|
+
const labels = placeGroundRailLabelsAtOuterEnd(fixture)
|
|
91
|
+
expect(labels[0]!.anchorPoint.x).toBeCloseTo(0, 6)
|
|
92
|
+
expect(labels[0]!.anchorPoint.y).toBeCloseTo(0.6, 6)
|
|
93
|
+
expect(labels[0]!.mspConnectionPairIds).toEqual(["rail"])
|
|
94
|
+
expect(labels[0]!.globalConnNetId).toBe(
|
|
95
|
+
before.netLabelPlacements[0]!.globalConnNetId,
|
|
96
|
+
)
|
|
97
|
+
expect(fixture).toEqual(before)
|
|
98
|
+
expect(
|
|
99
|
+
placeGroundRailLabelsAtOuterEnd({
|
|
100
|
+
...fixture,
|
|
101
|
+
netLabelPlacements: labels,
|
|
102
|
+
}),
|
|
103
|
+
).toEqual(labels)
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
test("label placement is independent of trace and endpoint ordering", () => {
|
|
108
|
+
const fixture = createFixture()
|
|
109
|
+
fixture.traces.reverse()
|
|
110
|
+
for (const trace of fixture.traces) {
|
|
111
|
+
trace.tracePath.reverse()
|
|
112
|
+
trace.pins.reverse()
|
|
113
|
+
trace.pinIds.reverse()
|
|
114
|
+
}
|
|
115
|
+
expect(placeGroundRailLabelsAtOuterEnd(fixture)[0]!.anchorPoint).toEqual({
|
|
116
|
+
x: 0,
|
|
117
|
+
y: 0.6,
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
for (const obstacle of [
|
|
122
|
+
"chip",
|
|
123
|
+
"text",
|
|
124
|
+
"trace",
|
|
125
|
+
"same-net-trace",
|
|
126
|
+
"label",
|
|
127
|
+
"same-net-label",
|
|
128
|
+
] as const) {
|
|
129
|
+
test(`keeps the existing anchor when the outer label would hit a ${obstacle}`, () => {
|
|
130
|
+
const fixture = createFixture()
|
|
131
|
+
const rect = { center: { x: 0, y: 0.4 }, width: 0.2, height: 0.2 }
|
|
132
|
+
if (obstacle === "chip")
|
|
133
|
+
fixture.inputProblem.chips.push({ ...rect, chipId: "obstacle", pins: [] })
|
|
134
|
+
if (obstacle === "text")
|
|
135
|
+
fixture.inputProblem.textBoxes = [{ ...rect, text: "annotation" }]
|
|
136
|
+
if (obstacle === "trace" || obstacle === "same-net-trace")
|
|
137
|
+
fixture.traces.push({
|
|
138
|
+
...fixture.traces[0]!,
|
|
139
|
+
mspPairId: "obstacle",
|
|
140
|
+
globalConnNetId:
|
|
141
|
+
obstacle === "trace" ? "foreign" : fixture.traces[0]!.globalConnNetId,
|
|
142
|
+
tracePath: [
|
|
143
|
+
{ x: -0.4, y: 0.4 },
|
|
144
|
+
{ x: 0.4, y: 0.4 },
|
|
145
|
+
],
|
|
146
|
+
})
|
|
147
|
+
if (obstacle === "label" || obstacle === "same-net-label")
|
|
148
|
+
fixture.netLabelPlacements.push({
|
|
149
|
+
...fixture.netLabelPlacements[0]!,
|
|
150
|
+
...rect,
|
|
151
|
+
globalConnNetId:
|
|
152
|
+
obstacle === "label"
|
|
153
|
+
? "foreign"
|
|
154
|
+
: fixture.netLabelPlacements[0]!.globalConnNetId,
|
|
155
|
+
mspConnectionPairIds: [],
|
|
156
|
+
pinIds: [],
|
|
157
|
+
anchorPoint: { x: 0, y: 0.5 },
|
|
158
|
+
})
|
|
159
|
+
expect(placeGroundRailLabelsAtOuterEnd(fixture)).toEqual(
|
|
160
|
+
fixture.netLabelPlacements,
|
|
161
|
+
)
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
test("does not move a port-only or unrelated island's GND label", () => {
|
|
166
|
+
const fixture = createFixture()
|
|
167
|
+
fixture.netLabelPlacements[0]!.mspConnectionPairIds = []
|
|
168
|
+
expect(placeGroundRailLabelsAtOuterEnd(fixture)).toEqual(
|
|
169
|
+
fixture.netLabelPlacements,
|
|
170
|
+
)
|
|
171
|
+
fixture.netLabelPlacements[0]!.mspConnectionPairIds = ["another-island"]
|
|
172
|
+
expect(placeGroundRailLabelsAtOuterEnd(fixture)).toEqual(
|
|
173
|
+
fixture.netLabelPlacements,
|
|
174
|
+
)
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test("does not force staggered ground pins or power labels to an outer column", () => {
|
|
178
|
+
const fixture = createFixture()
|
|
179
|
+
fixture.traces[0]!.pins[0].y += 0.2
|
|
180
|
+
expect(placeGroundRailLabelsAtOuterEnd(fixture)).toEqual(
|
|
181
|
+
fixture.netLabelPlacements,
|
|
182
|
+
)
|
|
183
|
+
fixture.traces[0]!.pins[0].y -= 0.2
|
|
184
|
+
fixture.netLabelPlacements[0]!.orientation = "y+"
|
|
185
|
+
expect(placeGroundRailLabelsAtOuterEnd(fixture)).toEqual(
|
|
186
|
+
fixture.netLabelPlacements,
|
|
187
|
+
)
|
|
188
|
+
})
|