forked from tscircuit/schematic-trace-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchematicTraceSingleLineSolver_long_trace.test.ts
More file actions
158 lines (133 loc) · 4.79 KB
/
SchematicTraceSingleLineSolver_long_trace.test.ts
File metadata and controls
158 lines (133 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { expect, test } from "bun:test"
import { calculateElbow } from "calculate-elbow"
import { SchematicTraceSingleLineSolver } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver/SchematicTraceSingleLineSolver"
import type { InputChip, InputProblem } from "lib/types/InputProblem"
type XY = { x: number; y: number }
const toSvgPoint = ({ x, y }: XY): XY => ({ x, y: -y })
const formatNumber = (value: number): string => {
const normalized = Math.abs(value) < 1e-9 ? 0 : value
const formatted = normalized.toFixed(3)
return formatted.replace(/\.?0+$/, "")
}
const renderTraceSnapshot = ({
chips,
path,
}: {
chips: InputChip[]
path: XY[]
}): string => {
const worldPoints: XY[] = [...path]
for (const chip of chips) {
const left = chip.center.x - chip.width / 2
const right = chip.center.x + chip.width / 2
const top = chip.center.y + chip.height / 2
const bottom = chip.center.y - chip.height / 2
worldPoints.push({ x: left, y: bottom })
worldPoints.push({ x: right, y: top })
for (const pin of chip.pins) {
worldPoints.push({ x: pin.x, y: pin.y })
}
}
const svgPoints = worldPoints.map(toSvgPoint)
const xs = svgPoints.map((p) => p.x)
const ys = svgPoints.map((p) => p.y)
const minX = Math.min(...xs)
const maxX = Math.max(...xs)
const minY = Math.min(...ys)
const maxY = Math.max(...ys)
const margin = 1
const viewBoxX = minX - margin
const viewBoxY = minY - margin
const viewBoxWidth = maxX - minX + margin * 2
const viewBoxHeight = maxY - minY + margin * 2
const pixelScale = 40
const svgWidth = Math.round(viewBoxWidth * pixelScale)
const svgHeight = Math.round(viewBoxHeight * pixelScale)
const chipRectElements = chips
.map((chip) => {
const x = chip.center.x - chip.width / 2
const y = -(chip.center.y + chip.height / 2)
return ` <rect x="${formatNumber(x)}" y="${formatNumber(y)}" width="${formatNumber(chip.width)}" height="${formatNumber(chip.height)}" />`
})
.join("\n")
const pinCircleElements = chips
.flatMap((chip) => chip.pins)
.map((pin) => {
const { x, y } = toSvgPoint({ x: pin.x, y: pin.y })
return ` <circle cx="${formatNumber(x)}" cy="${formatNumber(y)}" r="0.12" />`
})
.join("\n")
const polylinePoints = path
.map((point) => {
const { x, y } = toSvgPoint(point)
return `${formatNumber(x)},${formatNumber(y)}`
})
.join(" ")
return [
`<svg width="${svgWidth}" height="${svgHeight}" viewBox="${formatNumber(viewBoxX)} ${formatNumber(viewBoxY)} ${formatNumber(viewBoxWidth)} ${formatNumber(viewBoxHeight)}" xmlns="http://www.w3.org/2000/svg">`,
" <rect width=\"100%\" height=\"100%\" fill=\"white\" />",
" <g stroke=\"#888\" stroke-width=\"0.03\" fill=\"rgba(200,200,200,0.2)\">",
chipRectElements,
" </g>",
` <polyline points="${polylinePoints}" fill="none" stroke="#2E8B57" stroke-width="0.08" stroke-linecap="round" stroke-linejoin="round" />`,
" <g fill=\"#1f77b4\">",
pinCircleElements,
" </g>",
"</svg>",
].join("\n")
}
const buildChip = (
chipId: string,
center: { x: number; y: number },
width: number,
height: number,
pins: Array<{ pinId: string; x: number; y: number }>,
): InputChip => ({
chipId,
center,
width,
height,
pins,
})
test("allows long traces when restricted center line is far away", async () => {
const chipA = buildChip("A", { x: 0, y: 0 }, 0.4, 0.4, [
{ pinId: "A1", x: 0, y: 0 },
])
const chipB = buildChip("B", { x: 10, y: 0 }, 0.4, 0.4, [
{ pinId: "B1", x: 10, y: 0 },
])
const chipC = buildChip("C", { x: 5, y: 10 }, 1, 1, [
{ pinId: "C1", x: 4.5, y: 10 },
{ pinId: "C2", x: 5.5, y: 10 },
])
const inputProblem: InputProblem = {
chips: [chipA, chipB, chipC],
directConnections: [
{ pinIds: ["A1", "C1"], netId: "N1" },
{ pinIds: ["B1", "C2"], netId: "N1" },
],
netConnections: [],
availableNetLabelOrientations: {},
}
const pins = [
{ pinId: "A1", x: 0, y: 0, chipId: "A", _facingDirection: "x+" as const },
{ pinId: "B1", x: 10, y: 0, chipId: "B", _facingDirection: "x-" as const },
]
const solver = new SchematicTraceSingleLineSolver({
pins: pins as any,
guidelines: [],
inputProblem,
chipMap: { A: chipA, B: chipB, C: chipC },
})
solver.solve()
expect(solver.solved).toBe(true)
expect(solver.failed).toBe(false)
const baseElbow = calculateElbow(
{ x: pins[0].x, y: pins[0].y, facingDirection: pins[0]._facingDirection },
{ x: pins[1].x, y: pins[1].y, facingDirection: pins[1]._facingDirection },
{ overshoot: 0.2 },
)
expect(solver.solvedTracePath).toEqual(baseElbow)
const svg = renderTraceSnapshot({ chips: inputProblem.chips, path: solver.solvedTracePath! })
await expect(svg).toMatchSvgSnapshot(import.meta.path)
})