Skip to content

Commit

Permalink
Merge pull request #56 from tscircuit/i8wm-fix
Browse files Browse the repository at this point in the history
Intersection with Margin Reproduction & Fix
  • Loading branch information
seveibar authored Sep 12, 2024
2 parents c4caeed + 8e02ac6 commit df17d05
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 27 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 26 additions & 6 deletions algos/infinite-grid-ijump-astar/tests/fixtures/get-debug-svg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getSimpleRouteJson } from "autorouting-dataset"
import {
getSimpleRouteJson,
type SimplifiedPcbTrace,
} from "autorouting-dataset"
import { test, expect } from "bun:test"
import { circuitJsonToPcbSvg } from "circuit-to-svg"
import { Circuit } from "@tscircuit/core"
Expand All @@ -7,10 +10,17 @@ 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,
) => {
export const getDebugSvg = ({
inputCircuitJson,
autorouter,
solution,
rowHeight = 2.5,
}: {
inputCircuitJson: AnySoupElement[]
autorouter: GeneralizedAstarAutorouter
solution?: AnySoupElement[] | SimplifiedPcbTrace[]
rowHeight?: number
}) => {
const debugSolutions = Object.entries(autorouter.debugSolutions!).map(
([debugSolutionName, solutionCircuitJson]) => ({
debugSolutionName,
Expand All @@ -37,10 +47,20 @@ export const getDebugSvg = (
}),
),
),
translate(0, -2 * i),
translate(0, -rowHeight * i),
)
aggCircuitJson.push(...translatedCircuitJson)
}

const finalCircuitJson = JSON.parse(
JSON.stringify(inputCircuitJson.concat((solution as any) ?? [])),
)
aggCircuitJson.push(
...transformPCBElements(
finalCircuitJson,
translate(0, -rowHeight * debugSolutions.length),
),
)

return circuitJsonToPcbSvg(aggCircuitJson)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test("ijump-astar: intersection with margin", () => {

const traces = autorouter.solveAndMapToTraces()

expect(getDebugSvg(inputCircuitJson, autorouter)).toMatchSvgSnapshot(
expect(getDebugSvg({ inputCircuitJson, autorouter })).toMatchSvgSnapshot(
import.meta.path,
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@ import { translate } from "transformation-matrix"
import type { AnySoupElement } from "@tscircuit/soup"
import { getDebugSvg } from "./fixtures/get-debug-svg"

const OneByOnePad = (props: { name: string; pcbX?: number; pcbY?: number }) => (
<chip name={props.name} pcbX={props.pcbX} pcbY={props.pcbY}>
<footprint>
<smtpad
pcbX={0}
pcbY={0}
shape="rect"
width="1mm"
height="1mm"
portHints={["pin1"]}
/>
</footprint>
</chip>
)

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" />
<OneByOnePad name="U1" pcbX={-3} />
<OneByOnePad name="U2" pcbX={3} />
<OneByOnePad name="U_obstacle" pcbX={0} pcbY={-0.51} />
<trace from=".U1 > .pin1" to=".U2 > .pin1" />
</board>,
)

Expand All @@ -28,9 +44,11 @@ test("ijump-astar: intersection with margin", () => {
debug: true,
})

const traces = autorouter.solveAndMapToTraces()
const solution = autorouter.solveAndMapToTraces()

expect(getDebugSvg(inputCircuitJson, autorouter)).toMatchSvgSnapshot(
import.meta.path,
)
expect(solution).toHaveLength(1)

expect(
getDebugSvg({ inputCircuitJson, autorouter, solution }),
).toMatchSvgSnapshot(import.meta.path)
})
8 changes: 6 additions & 2 deletions algos/infinite-grid-ijump-astar/v2/lib/IJumpAutorouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export class IJumpAutorouter extends GeneralizedAstarAutorouter {
}
return true
})
.map((dir) => obstacles.getOrthoDirectionCollisionInfo(node, dir))
.map((dir) =>
obstacles.getOrthoDirectionCollisionInfo(node, dir, {
margin: this.OBSTACLE_MARGIN,
}),
)
// Filter out directions that are too close to the wall
.filter((dir) => dir.wallDistance >= this.OBSTACLE_MARGIN)

Expand All @@ -77,7 +81,7 @@ export class IJumpAutorouter extends GeneralizedAstarAutorouter {
wallDir: { ...forwardDir, wallDistance: this.OBSTACLE_MARGIN },
obstacle: node.obstacleHit,
obstacles,
OBSTACLE_MARGIN: 0.15,
OBSTACLE_MARGIN: this.OBSTACLE_MARGIN,
SHOULD_DETECT_CONJOINED_OBSTACLES: true,
})
}
Expand Down
22 changes: 13 additions & 9 deletions algos/infinite-grid-ijump-astar/v2/lib/ObstacleList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,40 @@ export class ObstacleList {
getOrthoDirectionCollisionInfo(
point: Point,
dir: Direction,
{ margin = 0 }: { margin?: number } = {},
): DirectionWithCollisionInfo {
const { x, y } = point
const { dx, dy } = dir
let minDistance = Infinity
let collisionObstacle: ObstacleWithEdges | null = null

for (const obstacle of this.obstacles) {
const { left, right, top, bottom } = obstacle
const leftMargin = obstacle.left - margin
const rightMargin = obstacle.right + margin
const topMargin = obstacle.top + margin
const bottomMargin = obstacle.bottom - margin

let distance: number | null = null

if (dx === 1 && dy === 0) {
// Right
if (y >= bottom && y <= top && x < left) {
distance = left - x
if (y > bottomMargin && y < topMargin && x < obstacle.left) {
distance = obstacle.left - x
}
} else if (dx === -1 && dy === 0) {
// Left
if (y >= bottom && y <= top && x > right) {
distance = x - right
if (y > bottomMargin && y < topMargin && x > obstacle.right) {
distance = x - obstacle.right
}
} else if (dx === 0 && dy === 1) {
// Up
if (x >= left && x <= right && y < bottom) {
distance = bottom - y
if (x > leftMargin && x < rightMargin && y < obstacle.bottom) {
distance = obstacle.bottom - y
}
} else if (dx === 0 && dy === -1) {
// Down
if (x >= left && x <= right && y > top) {
distance = y - top
if (x > leftMargin && x < rightMargin && y > obstacle.top) {
distance = y - obstacle.top
}
}

Expand Down

0 comments on commit df17d05

Please sign in to comment.