-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpad-position-integrity.test.ts
More file actions
214 lines (195 loc) · 6.63 KB
/
pad-position-integrity.test.ts
File metadata and controls
214 lines (195 loc) · 6.63 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { test, expect } from "bun:test"
import { pack } from "../lib"
import type { PackInput } from "../lib/types"
test("pads should maintain correct offsets when rotation is constrained to [0]", () => {
const input: PackInput = {
components: [
{
componentId: "U1",
availableRotationDegrees: [0], // No rotation allowed
pads: [
{
padId: "U1_P1",
networkId: "VCC",
type: "rect",
offset: { x: -5, y: 2 }, // Should stay at center + (-5, 2)
size: { x: 1, y: 1 },
},
{
padId: "U1_P2",
networkId: "GND",
type: "rect",
offset: { x: -5, y: -2 }, // Should stay at center + (-5, -2)
size: { x: 1, y: 1 },
},
],
},
{
componentId: "U2",
availableRotationDegrees: [0], // No rotation allowed
pads: [
{
padId: "U2_P1",
networkId: "VCC",
type: "rect",
offset: { x: -5, y: 0 }, // Should stay at center + (-5, 0)
size: { x: 1, y: 1 },
},
{
padId: "U2_P2",
networkId: "GND",
type: "rect",
offset: { x: 5, y: 0 }, // Should stay at center + (5, 0)
size: { x: 1, y: 1 },
},
],
},
],
minGap: 2,
packOrderStrategy: "largest_to_smallest",
packPlacementStrategy: "minimum_sum_squared_distance_to_network",
packFirst: ["U1"],
}
const result = pack(input)
console.log("=== Pad Position Integrity Test ===")
for (const component of result.components) {
console.log(`\n${component.componentId}:`)
console.log(
` Center: (${component.center.x.toFixed(2)}, ${component.center.y.toFixed(2)})`,
)
console.log(` Rotation: ${component.ccwRotationOffsetDegrees.toFixed(1)}°`)
// Find original component to compare offsets
const originalComponent = input.components.find(
(c) => c.componentId === component.componentId,
)!
for (let i = 0; i < component.pads.length; i++) {
const resultPad = component.pads[i]
const originalPad = originalComponent.pads[i]
// Calculate expected absolute position (center + offset)
const expectedX = component.center.x + (originalPad?.offset.x ?? 0)
const expectedY = component.center.y + (originalPad?.offset.y ?? 0)
console.log(` ${resultPad?.padId}:`)
console.log(
` Original offset: (${originalPad?.offset.x}, ${originalPad?.offset.y})`,
)
console.log(
` Expected absolute: (${expectedX.toFixed(2)}, ${expectedY.toFixed(2)})`,
)
console.log(
` Actual absolute: (${resultPad?.absoluteCenter?.x.toFixed(2)}, ${resultPad?.absoluteCenter?.y.toFixed(2)})`,
)
const deltaX = Math.abs((resultPad?.absoluteCenter?.x ?? 0) - expectedX)
const deltaY = Math.abs((resultPad?.absoluteCenter?.y ?? 0) - expectedY)
console.log(` Delta: (${deltaX.toFixed(3)}, ${deltaY.toFixed(3)})`)
// With no rotation allowed, pad positions should exactly match center + offset
expect(deltaX).toBeLessThan(0.001) // Very small tolerance for floating point
expect(deltaY).toBeLessThan(0.001)
if (deltaX > 0.001 || deltaY > 0.001) {
console.log(
` ❌ MISMATCH: Pad ${resultPad?.padId} is not at expected position!`,
)
} else {
console.log(
` ✅ CORRECT: Pad ${resultPad?.padId} is at expected position`,
)
}
}
}
})
test("compare rotation=0 vs unconstrained to see the difference", () => {
const inputConstrained: PackInput = {
components: [
{
componentId: "U1",
availableRotationDegrees: [0], // No rotation
pads: [
{
padId: "U1_P1",
networkId: "VCC",
type: "rect",
offset: { x: -5, y: 2 },
size: { x: 1, y: 1 },
},
{
padId: "U1_P2",
networkId: "GND",
type: "rect",
offset: { x: -5, y: -2 },
size: { x: 1, y: 1 },
},
],
},
{
componentId: "U2",
availableRotationDegrees: [0], // No rotation
pads: [
{
padId: "U2_P1",
networkId: "VCC",
type: "rect",
offset: { x: -5, y: 0 },
size: { x: 1, y: 1 },
},
{
padId: "U2_P2",
networkId: "GND",
type: "rect",
offset: { x: 5, y: 0 },
size: { x: 1, y: 1 },
},
],
},
],
minGap: 2,
packOrderStrategy: "largest_to_smallest",
packPlacementStrategy: "minimum_sum_squared_distance_to_network",
packFirst: ["U1"],
}
const inputUnconstrained: PackInput = {
...inputConstrained,
components: inputConstrained.components.map((c) => ({
...c,
availableRotationDegrees: undefined, // Allow all rotations
})),
}
const resultConstrained = pack(inputConstrained)
const resultUnconstrained = pack(inputUnconstrained)
console.log("\n=== Constrained vs Unconstrained Comparison ===")
const u2Constrained = resultConstrained.components.find(
(c) => c.componentId === "U2",
)!
const u2Unconstrained = resultUnconstrained.components.find(
(c) => c.componentId === "U2",
)!
console.log(`Constrained U2:`)
console.log(
` Center: (${u2Constrained.center.x.toFixed(2)}, ${u2Constrained.center.y.toFixed(2)})`,
)
console.log(
` Rotation: ${u2Constrained.ccwRotationOffsetDegrees.toFixed(1)}°`,
)
console.log(
` VCC pad: (${u2Constrained.pads[0]?.absoluteCenter?.x.toFixed(2)}, ${u2Constrained.pads[0]?.absoluteCenter?.y.toFixed(2)})`,
)
console.log(
` GND pad: (${u2Constrained.pads[1]?.absoluteCenter?.x.toFixed(2)}, ${u2Constrained.pads[1]?.absoluteCenter?.y.toFixed(2)})`,
)
console.log(`Unconstrained U2:`)
console.log(
` Center: (${u2Unconstrained.center.x.toFixed(2)}, ${u2Unconstrained.center.y.toFixed(2)})`,
)
console.log(
` Rotation: ${u2Unconstrained.ccwRotationOffsetDegrees.toFixed(1)}°`,
)
console.log(
` VCC pad: (${u2Unconstrained.pads[0]?.absoluteCenter?.x.toFixed(2)}, ${u2Unconstrained.pads[0]?.absoluteCenter?.y.toFixed(2)})`,
)
console.log(
` GND pad: (${u2Unconstrained.pads[1]?.absoluteCenter?.x.toFixed(2)}, ${u2Unconstrained.pads[1]?.absoluteCenter?.y.toFixed(2)})`,
)
// They should be different if rotation constraints are working
const same =
u2Constrained.ccwRotationOffsetDegrees ===
u2Unconstrained.ccwRotationOffsetDegrees
console.log(`Same rotation: ${same}`)
})