diff --git a/algos/infinite-grid-ijump-astar/tests/__snapshots__/intersection-with-margin.snap.svg b/algos/infinite-grid-ijump-astar/tests/__snapshots__/intersection-with-margin.snap.svg new file mode 100644 index 0000000..a860000 --- /dev/null +++ b/algos/infinite-grid-ijump-astar/tests/__snapshots__/intersection-with-margin.snap.svg @@ -0,0 +1,12 @@ +X0t1_iter[0]X01t1_iter[1]X01t1_iter[2]X01t1_iter[3] \ No newline at end of file diff --git a/algos/infinite-grid-ijump-astar/tests/fixtures/get-debug-svg.ts b/algos/infinite-grid-ijump-astar/tests/fixtures/get-debug-svg.ts new file mode 100644 index 0000000..c8ffff5 --- /dev/null +++ b/algos/infinite-grid-ijump-astar/tests/fixtures/get-debug-svg.ts @@ -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) +} diff --git a/algos/infinite-grid-ijump-astar/tests/get-debug-svg.test.tsx b/algos/infinite-grid-ijump-astar/tests/get-debug-svg.test.tsx new file mode 100644 index 0000000..60fc676 --- /dev/null +++ b/algos/infinite-grid-ijump-astar/tests/get-debug-svg.test.tsx @@ -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( + + + + + , + ) + + 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, + ) +}) diff --git a/algos/infinite-grid-ijump-astar/tests/intersection-with-margin.test.tsx b/algos/infinite-grid-ijump-astar/tests/intersection-with-margin.test.tsx new file mode 100644 index 0000000..60fc676 --- /dev/null +++ b/algos/infinite-grid-ijump-astar/tests/intersection-with-margin.test.tsx @@ -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( + + + + + , + ) + + 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, + ) +}) diff --git a/algos/infinite-grid-ijump-astar/v2/lib/GeneralizedAstar.ts b/algos/infinite-grid-ijump-astar/v2/lib/GeneralizedAstar.ts index 1cbdbad..8f10758 100644 --- a/algos/infinite-grid-ijump-astar/v2/lib/GeneralizedAstar.ts +++ b/algos/infinite-grid-ijump-astar/v2/lib/GeneralizedAstar.ts @@ -26,6 +26,7 @@ export type ConnectionSolveResult = export class GeneralizedAstarAutorouter { openSet: Node[] = [] closedSet: Set = new Set() + debug = false debugSolutions?: Record debugMessage: string | null = null @@ -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 @@ -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 = {} @@ -249,7 +255,6 @@ export class GeneralizedAstarAutorouter { } if (result.solved) { - solutions.push(result) obstaclesFromTraces.push( ...getObstaclesFromRoute( result.route.map((p) => ({ diff --git a/bun.lockb b/bun.lockb index 7a58d1f..f91915e 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index cf8db67..74d3fd9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",