Compare commits

..
Author SHA1 Message Date
rsp2k 8f006fec25 Rip every user of a shared via, and judge vias round at the outline
Two connections of a net can share a via. The grid kept one owner per via
cell next to the claim list, and the search and rip-up read only that owner:
a route of another net through the via ripped the first user and left the
second's via under the new trace. The exact check then threw one of them
out. The search and rip-up now read the claim list, and the single owner is
gone.

The exact check also tested a via against the outline as its square box,
whose corner reaches past a 45-degree chamfer where the round via does not.
The grid keeps the via's centre its radius plus the edge clearance from the
outline, as KiCad measures it; the exact check now does the same.

kitspace_minisumo_v3 seed 4: 4 rejected connections to 0, 11 failed to 7.
2026-10-03 11:03:38 -06:00
rsp2k ad10113b8b Merge fix/round-pads: route past round pads by their outline, not their bounding square 2026-10-02 04:51:51 -06:00
rsp2k dc11b4aa14 Route past round pads by their outline, not their bounding square
A circle pad was its bounding box in both the grid and the exact check, so a
4 mm mounting hole's square corners refused legal routes 0.8 mm clear of the
real copper. It is now a 16-sided polygon whose sides touch the circle, so
the router is never closer than KiCad measures. diagnose.py takes --seed.
2026-10-02 04:14:39 -06:00
rsp2k de00937084 Merge fix/roomba: grid and exact check agree on rotated pads, shared vias and wide nets 2026-09-30 14:02:54 -06:00
6 changed files with 123 additions and 27 deletions
+4 -2
View File
@@ -185,7 +185,7 @@ def _flood(router: GridRouter, pin, net_no: int, conns) -> tuple[int, Counter]:
def diagnose( def diagnose(
dsn_text: str, edge_clearance_mm: float | None, resolution: int = 2, shove: bool = False, dsn_text: str, edge_clearance_mm: float | None, resolution: int = 2, shove: bool = False,
access_cost: float = 0.0, hole_to_hole_mm: float = 0.25, access_cost: float = 0.0, hole_to_hole_mm: float = 0.25, seed: int | None = None,
) -> dict: ) -> dict:
dsn = parse_dsn(dsn_text) dsn = parse_dsn(dsn_text)
names = {i + 1: n.name for i, n in enumerate(dsn.nets)} names = {i + 1: n.name for i, n in enumerate(dsn.nets)}
@@ -201,6 +201,7 @@ def diagnose(
resolution=resolution, resolution=resolution,
shove=shove, shove=shove,
access_cost=access_cost, access_cost=access_cost,
seed=seed,
# the product's configuration: net-class widths and hole spacing # the product's configuration: net-class widths and hole spacing
net_widths=_net_widths_board(dsn, scale), net_widths=_net_widths_board(dsn, scale),
via_drill=_via_drill_board(dsn, scale), via_drill=_via_drill_board(dsn, scale),
@@ -276,12 +277,13 @@ def main(argv: list[str] | None = None) -> int:
ap.add_argument("--grid-resolution", type=int, default=2, metavar="K") ap.add_argument("--grid-resolution", type=int, default=2, metavar="K")
ap.add_argument("--shove", action="store_true", help="as mckicad routes") ap.add_argument("--shove", action="store_true", help="as mckicad routes")
ap.add_argument("--access-cost", type=float, default=0.0) ap.add_argument("--access-cost", type=float, default=0.0)
ap.add_argument("--seed", type=int, help="route in this shuffled order, as bench.py does")
ap.add_argument("--hole-to-hole", type=float, default=0.25, metavar="MM") ap.add_argument("--hole-to-hole", type=float, default=0.25, metavar="MM")
ap.add_argument("--json", type=Path, help="also write the full table here") ap.add_argument("--json", type=Path, help="also write the full table here")
args = ap.parse_args(argv) args = ap.parse_args(argv)
report = diagnose(args.dsn.read_text(), args.edge_clearance, args.grid_resolution, args.shove, report = diagnose(args.dsn.read_text(), args.edge_clearance, args.grid_resolution, args.shove,
args.access_cost, args.hole_to_hole) args.access_cost, args.hole_to_hole, args.seed)
for row in report["failures"]: for row in report["failures"]:
extra = "" extra = ""
for w in row.get("walled", []): for w in row.get("walled", []):
+32 -5
View File
@@ -12,9 +12,10 @@ model. It mirrors what FreeRouting's ``Structure.create_board`` / ``Library`` /
* padstacks x placement -> :class:`Pin` items * padstacks x placement -> :class:`Pin` items
* keepouts -> :class:`ObstacleArea` items * keepouts -> :class:`ObstacleArea` items
Pad geometry: a rectangle pad becomes an exact :class:`IntBox`; a circle pad its Pad geometry: a rectangle pad becomes an exact :class:`IntBox`; a circle pad a
bounding box (documented approximation pending an exact circle tile); a convex 16-sided :class:`Simplex` drawn just outside it (its corners stand out by 2% of
polygon pad an exact :class:`Simplex`. Every pad is placed centred on its pin the radius, where the bounding box's stood out by 41%); a convex polygon pad an
exact :class:`Simplex`. Every pad is placed centred on its pin
location, so it contains that location by construction. Component rotation is location, so it contains that location by construction. Component rotation is
applied to the pin position; the pad tile itself is not rotated (irrelevant to applied to the pin position; the pad tile itself is not rotated (irrelevant to
the containment invariant, and pads are centre-symmetric in practice). the containment invariant, and pads are centre-symmetric in practice).
@@ -249,7 +250,7 @@ def _placed_pad_tile(shape: Shape, place, dsn_pin, transform: CoordinateTransfor
r = round(transform.dsn_to_board(shape.diameter / 2)) r = round(transform.dsn_to_board(shape.diameter / 2))
cx = round(transform.dsn_to_board(centre.x)) cx = round(transform.dsn_to_board(centre.x))
cy = round(transform.dsn_to_board(centre.y)) cy = round(transform.dsn_to_board(centre.y))
return IntBox(cx - r, cy - r, cx + r, cy + r) return _circle_tile(cx, cy, r)
if isinstance(shape, Rectangle): if isinstance(shape, Rectangle):
x0, y0, x1, y1 = shape.coords[:4] x0, y0, x1, y1 = shape.coords[:4]
corners = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)] corners = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
@@ -281,6 +282,32 @@ def _placed_pad_tile(shape: Shape, place, dsn_pin, transform: CoordinateTransfor
return IntBox(min(xs), min(ys), max(xs), max(ys)) return IntBox(min(xs), min(ys), max(xs), max(ys))
_CIRCLE_SIDES = 16
def _circle_tile(cx: int, cy: int, r: int):
"""A polygon around the circle, every side touching it. A square around a
big round pad (a mounting hole) puts phantom copper in its corners that
blocks legal routes past it; this stays outside the circle, so the router
is still never closer than KiCad measures."""
if r < 8:
return IntBox(cx - r, cy - r, cx + r, cy + r)
reach = r / math.cos(math.pi / _CIRCLE_SIDES)
corners = []
for k in range(_CIRCLE_SIDES):
a = (k + 0.5) * 2 * math.pi / _CIRCLE_SIDES
dx, dy = reach * math.cos(a), reach * math.sin(a)
# round away from the centre so the polygon never cuts into the circle
corners.append(IntPoint(cx + math.copysign(math.ceil(abs(dx)), dx),
cy + math.copysign(math.ceil(abs(dy)), dy)))
corners = [IntPoint(int(p.x), int(p.y)) for p in corners]
for winding in (corners, corners[::-1]):
simplex = Simplex.from_corners(winding)
if not simplex.is_empty():
return simplex
return IntBox(cx - r, cy - r, cx + r, cy + r)
def _convex_hull(pts: list[IntPoint]) -> list[IntPoint]: def _convex_hull(pts: list[IntPoint]) -> list[IntPoint]:
"""The convex hull of ``pts`` counter-clockwise, collinear points dropped.""" """The convex hull of ``pts`` counter-clockwise, collinear points dropped."""
ps = sorted({(p.x, p.y) for p in pts}) ps = sorted({(p.x, p.y) for p in pts})
@@ -310,7 +337,7 @@ def _pad_tile(shape: Shape, transform: CoordinateTransform):
r = round(transform.dsn_to_board(shape.diameter / 2)) r = round(transform.dsn_to_board(shape.diameter / 2))
cx = round(transform.dsn_to_board(shape.center_x)) cx = round(transform.dsn_to_board(shape.center_x))
cy = round(transform.dsn_to_board(shape.center_y)) cy = round(transform.dsn_to_board(shape.center_y))
return IntBox(cx - r, cy - r, cx + r, cy + r) # bounding-box approximation return _circle_tile(cx, cy, r)
if isinstance(shape, (Polygon, Path)): if isinstance(shape, (Polygon, Path)):
coords = shape.coords coords = shape.coords
pts = [ pts = [
+12 -1
View File
@@ -207,7 +207,9 @@ def route_board_exact(
for i, (box, box_layers) in enumerate(boxes) for i, (box, box_layers) in enumerate(boxes)
for layer in box_layers for layer in box_layers
) or _end_in_rule_area(tree, wires, wire_half) or ( ) or _end_in_rule_area(tree, wires, wire_half) or (
outline is not None and not all(outline.box_clear(b, edge) for b, _ in boxes) outline is not None and not _inside_outline(
outline, boxes[:wire_count], vias, (via_half or wire_half) + edge, edge
)
): ):
rejected.append((net_no, *ends)) rejected.append((net_no, *ends))
continue # exact-clearance conflict -> drop (may be recovered by shove) continue # exact-clearance conflict -> drop (may be recovered by shove)
@@ -670,6 +672,15 @@ def _copper_boxes(wires, vias, half_width, via_layers, via_half=None):
return boxes return boxes
def _inside_outline(outline, wire_boxes, vias, via_reach, edge) -> bool:
"""Copper inside the board, ``edge`` from its outline. A via is round, as
the grid and KiCad take it: its square box would reach past a chamfered
corner where the via itself does not."""
return all(outline.box_clear(b, edge) for b, _ in wire_boxes) and all(
outline.point_clear(v, via_reach) for v in vias
)
def _is_rotated(shape) -> bool: def _is_rotated(shape) -> bool:
return isinstance(shape, Simplex) and not shape.is_int_box() return isinstance(shape, Simplex) and not shape.is_int_box()
+16 -17
View File
@@ -299,9 +299,9 @@ class GridRouter:
self._zone_cells: dict[int, list[Node]] = {} self._zone_cells: dict[int, list[Node]] = {}
# per-connection occupancy (owner = connection index) # per-connection occupancy (owner = connection index)
self._cell_owner: dict[Node, int] = {} self._cell_owner: dict[Node, int] = {}
self._via_owner: dict[Cell, int] = {}
# every connection whose via covers a cell, in claim order: a via two # every connection whose via covers a cell, in claim order: a via two
# connections share still blocks after the first of them is ripped # connections share still blocks after the first of them is ripped,
# and a route through it must rip all of them
self._via_claims: dict[Cell, list[int]] = {} self._via_claims: dict[Cell, list[int]] = {}
# trace halos: node -> indices of the connections claiming it # trace halos: node -> indices of the connections claiming it
self._halo: dict[Node, list[int]] = {} self._halo: dict[Node, list[int]] = {}
@@ -520,9 +520,7 @@ class GridRouter:
if owner is not None and conns[owner].net_no != net_no: if owner is not None and conns[owner].net_no != net_no:
blocking = owner blocking = owner
else: else:
via_owner = self._via_owner.get(cell) blocking = self._via_blocker(cell, net_no, conns)
if via_owner is not None and conns[via_owner].net_no != net_no:
blocking = via_owner
if blocking is None: if blocking is None:
for i in self._halo.get(node, ()): for i in self._halo.get(node, ()):
if conns[i].net_no != net_no: if conns[i].net_no != net_no:
@@ -537,6 +535,17 @@ class GridRouter:
return None return None
return self.rip_penalty * (1 + rip_count) return self.rip_penalty * (1 + rip_count)
def _via_others(self, cell: Cell, net_no: int, conns: list[_Connection]) -> list[int]:
"""Connections of other nets whose via covers ``cell``: all of them,
since two connections of a net can share one via."""
return [i for i in self._via_claims.get(cell, ()) if conns[i].net_no != net_no]
def _via_blocker(self, cell: Cell, net_no: int, conns: list[_Connection]) -> int | None:
"""The other net's via connection on ``cell`` hardest to rip (the
penalty and the rip cap go by it), or None."""
others = self._via_others(cell, net_no, conns)
return max(others, key=lambda i: conns[i].rip_count) if others else None
def _via_cells(self, cell: Cell) -> list[Cell]: def _via_cells(self, cell: Cell) -> list[Cell]:
"""The cell a via sits in plus the neighbours its copper reaches.""" """The cell a via sits in plus the neighbours its copper reaches."""
r = self.via_reach r = self.via_reach
@@ -927,9 +936,7 @@ class GridRouter:
if not any(g.overlaps(t) for tiles in other for t in tiles for g in grown): if not any(g.overlaps(t) for tiles in other for t in tiles for g in grown):
continue continue
owners.add(owner) owners.add(owner)
via_owner = self._via_owner.get(c) owners.update(self._via_others(c, net_no, conns))
if via_owner is not None and conns[via_owner].net_no != net_no:
owners.add(via_owner)
return owners return owners
def _end_penalty(self, pin: Pin, node: Node, net_no: int, allow_ripup: bool, conns) -> float: def _end_penalty(self, pin: Pin, node: Node, net_no: int, allow_ripup: bool, conns) -> float:
@@ -1206,7 +1213,6 @@ class GridRouter:
if conn.index not in claims: if conn.index not in claims:
claims.append(conn.index) claims.append(conn.index)
conn.via_halo.append(c) conn.via_halo.append(c)
self._via_owner.setdefault(c, conn.index)
ends = ((conn.a_end, wire_segs[0], 0), (conn.b_end, wire_segs[-1], -1)) if wire_segs else () ends = ((conn.a_end, wire_segs[0], 0), (conn.b_end, wire_segs[-1], -1)) if wire_segs else ()
conn.stubs = [] conn.stubs = []
for pin, (layer, cells), at in ends: for pin, (layer, cells), at in ends:
@@ -1259,11 +1265,6 @@ class GridRouter:
claims = self._via_claims.get(cell) claims = self._via_claims.get(cell)
if claims is not None and conn.index in claims: if claims is not None and conn.index in claims:
claims.remove(conn.index) claims.remove(conn.index)
if self._via_owner.get(cell) == conn.index:
if claims:
self._via_owner[cell] = claims[0]
else:
del self._via_owner[cell]
if claims is not None and not claims: if claims is not None and not claims:
del self._via_claims[cell] del self._via_claims[cell]
conn.via_halo = [] conn.via_halo = []
@@ -1308,9 +1309,7 @@ class GridRouter:
owner = self._cell_owner.get((cx, cy, cl)) owner = self._cell_owner.get((cx, cy, cl))
if owner is not None and conns[owner].net_no != net_no: if owner is not None and conns[owner].net_no != net_no:
owners.add(owner) owners.add(owner)
via_owner = self._via_owner.get((cx, cy)) owners.update(self._via_others((cx, cy), net_no, conns))
if via_owner is not None and conns[via_owner].net_no != net_no:
owners.add(via_owner)
for i in self._halo.get((cx, cy, cl), ()): for i in self._halo.get((cx, cy, cl), ()):
if conns[i].net_no != net_no: if conns[i].net_no != net_no:
owners.add(i) owners.add(i)
+58 -1
View File
@@ -143,11 +143,48 @@ def test_a_shared_via_keeps_its_footprint_when_one_user_is_ripped():
router._rip(first) router._rip(first)
assert second.routed and via in second.via_cells assert second.routed and via in second.via_cells
assert all(router._via_owner.get(c) == second.index for c in footprint) assert all(router._via_claims.get(c) == [second.index] for c in footprint)
# another net still keeps its distance from the via that is left # another net still keeps its distance from the via that is left
assert all(router._enter_cost_base(c, 0, sig.net_no, False, conns) is None for c in footprint) assert all(router._enter_cost_base(c, 0, sig.net_no, False, conns) is None for c in footprint)
def test_a_route_through_a_shared_via_rips_every_user():
"""Two connections of a net share a via. Another net routed through it
in rip-up mode has to rip both, or the one left keeps its via under the
new trace and the exact check throws one of them out."""
dsn = parse_dsn(_SHARED_VIA)
s = dsn.resolution.value
router = GridRouter(
build_board(dsn),
trace_width=round(_rule_width_dsn(dsn) * s),
clearance=round(_rule_clearance_dsn(dsn) * s),
via_half=_via_half_board(dsn, s),
)
conns = router._conns = router._build_connections()
gnd = [c for c in conns if len({c.a.component_ref, c.b.component_ref} & {"G1", "G2", "G3"})]
sig = next(c for c in conns if c not in gnd)
first, second = gnd
g3 = router._cell_of(next(p for p in router.board.get_pins() if p.component_ref == "G3").location)
via = (g3[0] - 10, g3[1])
def through_via(conn):
a, b = router._cell_of(conn.a.location), router._cell_of(conn.b.location)
return _line(a, via, 0) + _line(via, b, 1)
router._commit(first, through_via(first))
router._commit(second, through_via(second))
# the via's copper alone: footprint cells no trace or trace halo covers
crossing = [
node
for c in router._via_cells(via)
for node in ((*c, 0), (*c, 1))
if node not in router._cell_owner and node not in router._halo
]
assert crossing
assert router._crossed_owners(crossing, sig.net_no, conns) == {first.index, second.index}
# --- a wider net class -------------------------------------------------------- # --- a wider net class --------------------------------------------------------
_WIDE = """(pcb wide (parser) (resolution um 10) (unit um) _WIDE = """(pcb wide (parser) (resolution um 10) (unit um)
@@ -225,3 +262,23 @@ def test_a_pad_cell_check_uses_the_rotated_outline():
c = router._cell_center(cell) c = router._cell_center(cell)
square = IntBox(c.x - margin, c.y - margin, c.x + margin, c.y + margin) square = IntBox(c.x - margin, c.y - margin, c.x + margin, c.y + margin)
assert oval.shape.intersection(square).dimension() != 2 assert oval.shape.intersection(square).dimension() != 2
def test_a_via_by_a_chamfered_corner_is_judged_round():
"""The grid keeps a via's centre its radius plus the edge clearance from
the outline. The exact check used the via's square, whose corner reaches
past a 45-degree chamfer where the round via does not."""
from freeroute.board.outline import BoardOutline
from freeroute.geometry import IntBox, IntPoint
from freeroute.route.exact_router import _inside_outline
# a 10 mm square with its lower-left corner cut at 45 degrees
outline = BoardOutline([IntPoint(*p) for p in
[(30, 0), (100, 0), (100, 100), (0, 100), (0, 30)]])
half, edge = 4, 1
via = IntPoint(19, 20) # its centre 6.4 from the chamfer x + y = 30
square = IntBox(via.x - half, via.y - half, via.x + half, via.y + half)
assert not outline.box_clear(square, edge) # the square's corner: 0.7 away
assert _inside_outline(outline, [], [via], half + edge, edge)
# 4.2 from the chamfer: the round via itself is too close
assert not _inside_outline(outline, [], [IntPoint(18, 18)], half + edge, edge)
+1 -1
View File
@@ -31,7 +31,7 @@ def test_occupancy_only_holds_routed_connections(name):
router, _ = _routed_router(name) router, _ = _routed_router(name)
conns = router._conns conns = router._conns
assert all(conns[i].routed for i in router._cell_owner.values()) assert all(conns[i].routed for i in router._cell_owner.values())
assert all(conns[i].routed for i in router._via_owner.values()) assert all(conns[i].routed for claims in router._via_claims.values() for i in claims)
assert all(conns[i].routed for claims in router._halo.values() for i in claims) assert all(conns[i].routed for claims in router._halo.values() for i in claims)