-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
145 lines (127 loc) · 4.48 KB
/
index.ts
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
import { Grid, AStarFinder, DiagonalMovement } from "pathfinding"
import { getSimpleRouteJson, type SimplifiedPcbTrace } from "solver-utils"
import type { AnyCircuitElement as AnySoupElement } from "circuit-json"
export function autoroute(soup: AnySoupElement[]): SimplifiedPcbTrace[] {
const input = getSimpleRouteJson(soup)
const gridSize = 0.1 // Assume 1 unit grid size
const bufferSize = 0
const boundsWithBuffer = {
minX: input.bounds.minX - bufferSize,
maxX: input.bounds.maxX + bufferSize,
minY: input.bounds.minY - bufferSize,
maxY: input.bounds.maxY + bufferSize,
}
const width =
Math.ceil((boundsWithBuffer.maxX - boundsWithBuffer.minX) / gridSize) + 1
const height =
Math.ceil((boundsWithBuffer.maxY - boundsWithBuffer.minY) / gridSize) + 1
// Initialize grids for each layer
const grids = Array.from(
{ length: input.layerCount },
() => new Grid(width, height),
)
// Mark obstacles
input.obstacles.forEach((obstacle) => {
const left = Math.floor(
(obstacle.center.x - obstacle.width / 2 - boundsWithBuffer.minX) /
gridSize,
)
const right = Math.ceil(
(obstacle.center.x + obstacle.width / 2 - boundsWithBuffer.minX) /
gridSize,
)
const top = Math.floor(
(obstacle.center.y - obstacle.height / 2 - boundsWithBuffer.minY) /
gridSize,
)
const bottom = Math.ceil(
(obstacle.center.y + obstacle.height / 2 - boundsWithBuffer.minY) /
gridSize,
)
for (let x = left; x <= right; x++) {
for (let y = top; y <= bottom; y++) {
grids.forEach((grid) => grid.setWalkableAt(x, y, false))
}
}
})
const finder = new AStarFinder({
diagonalMovement: DiagonalMovement.OnlyWhenNoObstacles,
})
const solution: SimplifiedPcbTrace[] = []
input.connections.forEach((connection) => {
const route: SimplifiedPcbTrace["route"] = []
let currentLayer = 0
// Create a clone of the current grid for this connection
const connectionGrid = grids[currentLayer].clone()
// Make obstacles walkable if they are connected to this trace
input.obstacles.forEach((obstacle) => {
if (obstacle.connectedTo.includes(connection.name)) {
const left = Math.floor(
(obstacle.center.x - obstacle.width / 2 - boundsWithBuffer.minX) /
gridSize,
)
const right = Math.ceil(
(obstacle.center.x + obstacle.width / 2 - boundsWithBuffer.minX) /
gridSize,
)
const top = Math.floor(
(obstacle.center.y - obstacle.height / 2 - boundsWithBuffer.minY) /
gridSize,
)
const bottom = Math.ceil(
(obstacle.center.y + obstacle.height / 2 - boundsWithBuffer.minY) /
gridSize,
)
for (let x = left; x <= right; x++) {
for (let y = top; y <= bottom; y++) {
connectionGrid.setWalkableAt(x, y, true)
}
}
}
})
for (let i = 0; i < connection.pointsToConnect.length - 1; i++) {
const start = connection.pointsToConnect[i]
const end = connection.pointsToConnect[i + 1]
const startX = Math.round((start.x - boundsWithBuffer.minX) / gridSize)
const startY = Math.round((start.y - boundsWithBuffer.minY) / gridSize)
const endX = Math.round((end.x - boundsWithBuffer.minX) / gridSize)
const endY = Math.round((end.y - boundsWithBuffer.minY) / gridSize)
const path = finder.findPath(
startX,
startY,
endX,
endY,
connectionGrid.clone(),
)
if (path.length > 0) {
path.forEach((point, index) => {
const x = point[0] * gridSize + boundsWithBuffer.minX
const y = point[1] * gridSize + boundsWithBuffer.minY
if (index > 0) {
route.push({
route_type: "wire",
x,
y,
width: 0.08, // Assuming a default width
layer: "top", // TODO create layer map for "top", "bottom", "inner1", "inner2" etc.
// layer: `layer${currentLayer + 1}`,
})
}
})
// Mark the path as occupied
path.forEach((point) => {
connectionGrid.setWalkableAt(point[0], point[1], false)
grids[currentLayer].setWalkableAt(point[0], point[1], false)
})
}
}
if (route.length > 0) {
solution.push({
type: "pcb_trace",
pcb_trace_id: `pcb_trace_for_${connection.name}`,
route,
})
}
})
return solution
}