Skip to content

Commit

Permalink
Merge pull request #55 from tscircuit/fix-route-obstacles
Browse files Browse the repository at this point in the history
Add Debug SVGs for snapshot algorithm development, fix duplicated routes returned from autorouter
  • Loading branch information
seveibar authored Sep 11, 2024
2 parents bfe0c8a + e2da5d9 commit c4caeed
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions algos/infinite-grid-ijump-astar/tests/fixtures/get-debug-svg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getSimpleRouteJson } from "autorouting-dataset"
import { test, expect } from "bun:test"
import { circuitJsonToPcbSvg } from "circuit-to-svg"
import { Circuit } from "@tscircuit/core"
import { transformPCBElements } from "@tscircuit/soup-util"
import { translate } from "transformation-matrix"
import type { AnySoupElement } from "@tscircuit/soup"
import type { GeneralizedAstarAutorouter } from "algos/infinite-grid-ijump-astar/v2/lib/GeneralizedAstar"

export const getDebugSvg = (
inputCircuitJson: AnySoupElement[],
autorouter: GeneralizedAstarAutorouter,
) => {
const debugSolutions = Object.entries(autorouter.debugSolutions!).map(
([debugSolutionName, solutionCircuitJson]) => ({
debugSolutionName,
solutionCircuitJson,
}),
)

const aggCircuitJson: AnySoupElement[] = []

for (let i = 0; i < debugSolutions.length; i++) {
const { debugSolutionName, solutionCircuitJson } = debugSolutions[i]
const translatedCircuitJson = transformPCBElements(
JSON.parse(
JSON.stringify(
solutionCircuitJson.concat(inputCircuitJson).concat({
type: "pcb_fabrication_note_text",
text: debugSolutionName,
pcb_component_id: "unknown",
layer: "top",
font: "tscircuit2024",
font_size: 0.2,
anchor_position: { x: -5, y: 0 },
anchor_alignment: "center",
}),
),
),
translate(0, -2 * i),
)
aggCircuitJson.push(...translatedCircuitJson)
}

return circuitJsonToPcbSvg(aggCircuitJson)
}
36 changes: 36 additions & 0 deletions algos/infinite-grid-ijump-astar/tests/get-debug-svg.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getSimpleRouteJson } from "autorouting-dataset"
import { test, expect } from "bun:test"
import { circuitJsonToPcbSvg } from "circuit-to-svg"
import { IJumpAutorouter } from "../v2"
import { Circuit } from "@tscircuit/core"
import { transformPCBElements } from "@tscircuit/soup-util"
import { translate } from "transformation-matrix"
import type { AnySoupElement } from "@tscircuit/soup"
import { getDebugSvg } from "./fixtures/get-debug-svg"

test("ijump-astar: intersection with margin", () => {
const circuit = new Circuit()

circuit.add(
<board width="10mm" height="2mm" routingDisabled>
<resistor name="R1" resistance="1k" footprint="0402" pcbX={-3} />
<resistor name="R2" resistance="1k" footprint="0402" pcbX={3} />
<trace from=".R1 > .pin1" to=".R2 > .pin1" />
</board>,
)

const inputCircuitJson = circuit.getCircuitJson()

const input = getSimpleRouteJson(inputCircuitJson)

const autorouter = new IJumpAutorouter({
input,
debug: true,
})

const traces = autorouter.solveAndMapToTraces()

expect(getDebugSvg(inputCircuitJson, autorouter)).toMatchSvgSnapshot(
import.meta.path,
)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getSimpleRouteJson } from "autorouting-dataset"
import { test, expect } from "bun:test"
import { circuitJsonToPcbSvg } from "circuit-to-svg"
import { IJumpAutorouter } from "../v2"
import { Circuit } from "@tscircuit/core"
import { transformPCBElements } from "@tscircuit/soup-util"
import { translate } from "transformation-matrix"
import type { AnySoupElement } from "@tscircuit/soup"
import { getDebugSvg } from "./fixtures/get-debug-svg"

test("ijump-astar: intersection with margin", () => {
const circuit = new Circuit()

circuit.add(
<board width="10mm" height="2mm" routingDisabled>
<resistor name="R1" resistance="1k" footprint="0402" pcbX={-3} />
<resistor name="R2" resistance="1k" footprint="0402" pcbX={3} />
<trace from=".R1 > .pin1" to=".R2 > .pin1" />
</board>,
)

const inputCircuitJson = circuit.getCircuitJson()

const input = getSimpleRouteJson(inputCircuitJson)

const autorouter = new IJumpAutorouter({
input,
debug: true,
})

const traces = autorouter.solveAndMapToTraces()

expect(getDebugSvg(inputCircuitJson, autorouter)).toMatchSvgSnapshot(
import.meta.path,
)
})
7 changes: 6 additions & 1 deletion algos/infinite-grid-ijump-astar/v2/lib/GeneralizedAstar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ConnectionSolveResult =
export class GeneralizedAstarAutorouter {
openSet: Node[] = []
closedSet: Set<string> = new Set()
debug = false

debugSolutions?: Record<string, AnySoupElement[]>
debugMessage: string | null = null
Expand Down Expand Up @@ -57,6 +58,7 @@ export class GeneralizedAstarAutorouter {
GRID_STEP?: number
OBSTACLE_MARGIN?: number
MAX_ITERATIONS?: number
debug?: boolean
}) {
this.input = opts.input
this.allObstacles = opts.input.obstacles
Expand All @@ -65,6 +67,10 @@ export class GeneralizedAstarAutorouter {
this.GRID_STEP = opts.GRID_STEP ?? 0.1
this.OBSTACLE_MARGIN = opts.OBSTACLE_MARGIN ?? 0.15
this.MAX_ITERATIONS = opts.MAX_ITERATIONS ?? 100
this.debug = opts.debug ?? debug.enabled
if (this.debug) {
debug.enabled = true
}

if (debug.enabled) {
this.debugSolutions = {}
Expand Down Expand Up @@ -249,7 +255,6 @@ export class GeneralizedAstarAutorouter {
}

if (result.solved) {
solutions.push(result)
obstaclesFromTraces.push(
...getObstaclesFromRoute(
result.route.map((p) => ({
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@biomejs/biome": "^1.8.3",
"@timohausmann/quadtree-ts": "^2.2.2",
"@tscircuit/builder": "1.11.2",
"@tscircuit/core": "^0.0.56",
"@tscircuit/pcb-viewer": "1.4.5",
"@tscircuit/props": "^0.0.26",
"@tscircuit/soup": "^0.0.68",
Expand All @@ -30,7 +31,7 @@
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"bun-match-svg": "^0.0.3",
"circuit-to-svg": "^0.0.22",
"circuit-to-svg": "^0.0.26",
"concurrently": "^8.2.2",
"d3-delaunay": "^6.0.4",
"debug": "^4.3.6",
Expand Down

0 comments on commit c4caeed

Please sign in to comment.