Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/assets/e2e4layer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"layerCount": 4,
"minTraceWidth": 0.15,
"obstacles": [],
"connections": [
{
"name": "conn1",
"pointsToConnect": [
{ "x": 1, "y": 1, "layer": "top", "pcb_port_id": "p1" },
{ "x": 9, "y": 9, "layer": "bottom", "pcb_port_id": "p2" }
]
},
{
"name": "conn2",
"pointsToConnect": [
{ "x": 9, "y": 1, "layer": "top", "pcb_port_id": "p3" },
{ "x": 1, "y": 9, "layer": "bottom", "pcb_port_id": "p4" }
]
}
],
"bounds": { "minX": 0, "maxX": 10, "minY": 0, "maxY": 10 }
}
4 changes: 2 additions & 2 deletions lib/data-structures/FlatbushIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class FlatbushIndex<T> implements ISpatialIndex<T> {
private currentIndex = 0

constructor(numItems: number) {
this.index = new Flatbush(numItems)
this.index = new Flatbush(Math.max(numItems, 1))
}

insert(item: T, minX: number, minY: number, maxX: number, maxY: number) {
Expand All @@ -30,6 +30,6 @@ export class FlatbushIndex<T> implements ISpatialIndex<T> {

clear() {
this.items = []
this.index = new Flatbush(0)
this.index = new Flatbush(1)
}
}
9 changes: 7 additions & 2 deletions lib/data-structures/ObstacleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export class ObstacleSpatialHashIndex {
implementation: "native" | "rbush" | "flatbush" = "native",
obstacles: Obstacle[] = [],
) {
if (implementation === "flatbush") {
if (implementation === "flatbush" && obstacles.length > 0) {
this.idx = new FlatbushIndex<Obstacle>(obstacles.length)
} else if (implementation === "flatbush" && obstacles.length === 0) {
// flatbush cannot index zero items, fall back to rbush
this.idx = new RbushIndex<Obstacle>()
} else if (implementation === "rbush") {
this.idx = new RbushIndex<Obstacle>()
} else {
Expand Down Expand Up @@ -50,7 +53,9 @@ export class ObstacleSpatialHashIndex {

// bulk-load initial obstacles
obstacles.forEach((o) => this.insert(o))
if (implementation === "flatbush") this.idx.finish?.()
if (implementation === "flatbush" && obstacles.length > 0) {
this.idx.finish?.()
}
}

insert(o: Obstacle) {
Expand Down
91 changes: 91 additions & 0 deletions tests/__snapshots__/e2e3.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/core3.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ test("core3 - 0402 columns", async () => {
expect(convertCircuitJsonToPcbSvg(circuitJson)).toMatchSvgSnapshot(
import.meta.path,
)
})
}, 20_000)
18 changes: 18 additions & 0 deletions tests/e2e3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "bun:test"
import { CapacityMeshSolver } from "../lib"
import type { SimpleRouteJson } from "lib/types"
import { convertSrjToGraphicsObject } from "./fixtures/convertSrjToGraphicsObject"
import e2e4layer from "../examples/assets/e2e4layer.json"

// Simple end-to-end test for 4 layer routing

test("should solve a 4-layer board", async () => {
const srj: SimpleRouteJson = e2e4layer as any
const solver = new CapacityMeshSolver(srj)
await solver.solve()
const result = solver.getOutputSimpleRouteJson()
expect(result.layerCount).toBe(4)
expect(convertSrjToGraphicsObject(result)).toMatchGraphicsSvg(
import.meta.path,
)
})
2 changes: 1 addition & 1 deletion tests/fixtures/convertSrjToGraphicsObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const convertSrjToGraphicsObject = (srj: SimpleRouteJson) => {
const points: Point[] = []

const colorMap: Record<string, string> = getColorMap(srj)
const layerCount = 2
const layerCount = srj.layerCount

// Add points for each connection's pointsToConnect
if (srj.connections) {
Expand Down