Skip to content
Draft
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
17 changes: 16 additions & 1 deletion lib/solvers/RectDiffSeedingSolver/RectDiffSeedingSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { expandRectFromSeed } from "../../utils/expandRectFromSeed"
import { computeDefaultGridSizes } from "./computeDefaultGridSizes"
import { computeCandidates3D } from "./computeCandidates3D"
import { computeEdgeCandidates3D } from "./computeEdgeCandidates3D"
import { computeConnectionCandidates3D } from "./computeConnectionCandidates3D"
import { longestFreeSpanAroundZ } from "./longestFreeSpanAroundZ"
import { allLayerNode } from "../../utils/buildHardPlacedByLayer"
import { isFullyOccupiedAtPoint } from "../../utils/isFullyOccupiedAtPoint"
Expand Down Expand Up @@ -191,14 +192,28 @@ export class RectDiffSeedingSolver extends BaseSolver {
} else {
if (!this.edgeAnalysisDone) {
const minSize = Math.min(minSingle.width, minSingle.height)
this.candidates = computeEdgeCandidates3D({
const edgeCandidates = computeEdgeCandidates3D({
bounds: this.bounds,
minSize,
layerCount: this.layerCount,
obstacleIndexByLayer: this.input.obstacleIndexByLayer,
placedIndexByLayer: this.placedIndexByLayer,
hardPlacedByLayer: this.hardPlacedByLayer,
})
const connectionCandidates = computeConnectionCandidates3D({
bounds: this.bounds,
simpleRouteJson: this.srj,
minSize,
layerCount: this.layerCount,
obstacleIndexByLayer: this.input.obstacleIndexByLayer,
placedIndexByLayer: this.placedIndexByLayer,
hardPlacedByLayer: this.hardPlacedByLayer,
zIndexByName: this.input.zIndexByName,
obstacleClearance: this.input.obstacleClearance,
})
// Process generic edge candidates first, then let connection-point
// seeds carve back finer escape regions near pads and ports.
this.candidates = [...edgeCandidates, ...connectionCandidates]
this.edgeAnalysisDone = true
this.totalSeedsThisGrid = this.candidates.length
this.consumedSeedsThisGrid = 0
Expand Down
177 changes: 177 additions & 0 deletions lib/solvers/RectDiffSeedingSolver/computeConnectionCandidates3D.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import type { Candidate3D, XYRect } from "../../rectdiff-types"
import type { SimpleRouteJson } from "../../types/srj-types"
import { EPS, distancePointToRectEdges } from "../../utils/rectdiff-geometry"
import { isFullyOccupiedAtPoint } from "../../utils/isFullyOccupiedAtPoint"
import { padRect } from "../../utils/padRect"
import { obstacleToXYRect, obstacleZs } from "./layers"
import { longestFreeSpanAroundZ } from "./longestFreeSpanAroundZ"
import type RBush from "rbush"
import type { RTreeRect } from "../../types/capacity-mesh-types"

const quantize = (value: number, precision = 1e-6) =>
Math.round(value / precision) * precision

type ConcretePoint = NonNullable<
SimpleRouteJson["connections"]
>[number]["pointsToConnect"][number] & {
x: number
y: number
layer: string
}

const isConcretePoint = (point: unknown): point is ConcretePoint =>
!!point &&
typeof point === "object" &&
typeof (point as any).x === "number" &&
typeof (point as any).y === "number" &&
typeof (point as any).layer === "string"

const clamp = (value: number, min: number, max: number) =>
Math.min(max, Math.max(min, value))

/**
* Create precise seeds around explicit connection points so later routing can
* preserve finer-grained escape regions around ports and pads.
*/
export function computeConnectionCandidates3D(params: {
bounds: XYRect
simpleRouteJson: SimpleRouteJson
minSize: number
layerCount: number
obstacleIndexByLayer: Array<RBush<RTreeRect> | undefined>
placedIndexByLayer: Array<RBush<RTreeRect> | undefined>
hardPlacedByLayer: XYRect[][]
zIndexByName: Map<string, number>
obstacleClearance?: number
}): Candidate3D[] {
const {
bounds,
simpleRouteJson,
minSize,
layerCount,
obstacleIndexByLayer,
placedIndexByLayer,
hardPlacedByLayer,
zIndexByName,
obstacleClearance,
} = params

const out: Candidate3D[] = []
const dedup = new Set<string>()
const delta = Math.max(minSize * 0.15, EPS * 3)
const hardRectsByLayer = Array.from({ length: layerCount }, (_, z) => [
...(obstacleIndexByLayer[z]?.all() ?? []),
...(hardPlacedByLayer[z] ?? []),
])

const key = (p: { x: number; y: number; z: number }) =>
`${p.z}|${p.x.toFixed(6)}|${p.y.toFixed(6)}`

const fullyOcc = (p: { x: number; y: number }) =>
isFullyOccupiedAtPoint({
layerCount,
obstacleIndexByLayer,
placedIndexByLayer,
point: p,
})

const pushIfFree = (p: { x: number; y: number; z: number }) => {
const x = quantize(p.x)
const y = quantize(p.y)
const { z } = p

if (
x < bounds.x + EPS ||
y < bounds.y + EPS ||
x > bounds.x + bounds.width - EPS ||
y > bounds.y + bounds.height - EPS
) {
return
}

if (fullyOcc({ x, y })) return

let d = distancePointToRectEdges({ x, y }, bounds)
for (const blocker of hardRectsByLayer[z] ?? []) {
d = Math.min(d, distancePointToRectEdges({ x, y }, blocker))
}
const distance = quantize(d)

const dedupKey = key({ x, y, z })
if (dedup.has(dedupKey)) return
dedup.add(dedupKey)

const span = longestFreeSpanAroundZ({
x,
y,
z,
layerCount,
minSpan: 1,
maxSpan: undefined,
obstacleIndexByLayer,
additionalBlockersByLayer: hardPlacedByLayer,
})

out.push({
x,
y,
z,
distance,
zSpanLen: span.length,
isEdgeSeed: true,
})
}

const concretePoints = (simpleRouteJson.connections ?? [])
.flatMap((connection) => connection.pointsToConnect ?? [])
.filter(isConcretePoint)

const obstacleRectsByLayer = Array.from({ length: layerCount }, () => [])
for (const obstacle of simpleRouteJson.obstacles ?? []) {
const baseRect = obstacleToXYRect(obstacle)
const rect = baseRect ? padRect(baseRect, obstacleClearance ?? 0) : null
if (!rect) continue
for (const z of obstacleZs(obstacle, zIndexByName)) {
obstacleRectsByLayer[z]?.push(rect)
}
}

for (const point of concretePoints) {
const z = zIndexByName.get(point.layer.toLowerCase())
if (typeof z !== "number" || z < 0 || z >= layerCount) continue

const containingRects = (obstacleRectsByLayer[z] ?? []).filter(
(rect) =>
point.x >= rect.x - EPS &&
point.x <= rect.x + rect.width + EPS &&
point.y >= rect.y - EPS &&
point.y <= rect.y + rect.height + EPS,
)

if (containingRects.length === 0) {
pushIfFree({ x: point.x, y: point.y, z })
continue
}

for (const rect of containingRects) {
const clampedX = clamp(point.x, rect.x + EPS, rect.x + rect.width - EPS)
const clampedY = clamp(point.y, rect.y + EPS, rect.y + rect.height - EPS)

pushIfFree({ x: rect.x - delta, y: clampedY, z })
pushIfFree({ x: rect.x + rect.width + delta, y: clampedY, z })
pushIfFree({ x: clampedX, y: rect.y - delta, z })
pushIfFree({ x: clampedX, y: rect.y + rect.height + delta, z })
}
}

out.sort(
(a, b) =>
b.zSpanLen! - a.zSpanLen! ||
b.distance - a.distance ||
a.z - b.z ||
a.x - b.x ||
a.y - b.y,
)

return out
}
17 changes: 17 additions & 0 deletions pages/repro/port-escape-paths.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import inputProblems from "tests/solver/repros/port-escape-paths/port-escape-paths.json"
import { RectDiffPipeline } from "lib/RectDiffPipeline"
import { useMemo } from "react"
import { SolverDebugger3d } from "components/SolverDebugger3d"

export default () => {
const problem = inputProblems[0]!

const solver = useMemo(() => new RectDiffPipeline(problem), [])

return (
<SolverDebugger3d
solver={solver}
simpleRouteJson={problem.simpleRouteJson}
/>
)
}
79 changes: 79 additions & 0 deletions tests/connection-point-seeding.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { expect, test } from "bun:test"
import RBush from "rbush"
import { buildObstacleIndexesByLayer } from "../lib/solvers/RectDiffGridSolverPipeline/buildObstacleIndexes"
import { computeConnectionCandidates3D } from "../lib/solvers/RectDiffSeedingSolver/computeConnectionCandidates3D"
import type { RTreeRect } from "../lib/types/capacity-mesh-types"
import type { SimpleRouteJson } from "../lib/types/srj-types"

test("computeConnectionCandidates3D seeds around pad-connected ports", () => {
const srj: SimpleRouteJson = {
bounds: {
minX: 0,
minY: 0,
maxX: 10,
maxY: 10,
},
layerCount: 2,
minTraceWidth: 0.15,
obstacles: [
{
type: "rect",
layers: ["top"],
center: { x: 5, y: 5 },
width: 0.8,
height: 1.6,
connectedTo: ["pad-1"],
},
],
connections: [
{
name: "pad-net",
pointsToConnect: [
{
x: 5,
y: 5,
layer: "top",
pointId: "pcb_port_1",
pcb_port_id: "pcb_port_1",
},
{ $ref: "$.connections[0].pointsToConnect[0]" } as any,
],
},
],
}

const { obstacleIndexByLayer, zIndexByName } = buildObstacleIndexesByLayer({
srj,
})

const candidates = computeConnectionCandidates3D({
bounds: {
x: srj.bounds.minX,
y: srj.bounds.minY,
width: srj.bounds.maxX - srj.bounds.minX,
height: srj.bounds.maxY - srj.bounds.minY,
},
simpleRouteJson: srj,
minSize: srj.minTraceWidth * 2,
layerCount: srj.layerCount,
obstacleIndexByLayer,
placedIndexByLayer: Array.from(
{ length: srj.layerCount },
() => new RBush<RTreeRect>(),
),
hardPlacedByLayer: Array.from({ length: srj.layerCount }, () => []),
zIndexByName,
})

expect(candidates.length).toBeGreaterThanOrEqual(4)

const xs = candidates.map((candidate) => candidate.x)
const ys = candidates.map((candidate) => candidate.y)
const topLayerSeeds = candidates.filter((candidate) => candidate.z === 0)

expect(topLayerSeeds.length).toBeGreaterThanOrEqual(4)
expect(xs.some((x) => x < 5)).toBe(true)
expect(xs.some((x) => x > 5)).toBe(true)
expect(ys.some((y) => y < 5)).toBe(true)
expect(ys.some((y) => y > 5)).toBe(true)
})
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.
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.

Large diffs are not rendered by default.

Loading
Loading