Select and verify topological and constructive routing plans

This commit is contained in:
Jan Petykiewicz 2026-11-26 11:57:43 -08:00
commit be3222e5ae

154
inire/router/_compact.py Normal file
View file

@ -0,0 +1,154 @@
"""Fit perpendicular jogs between nearby, parallel terminal columns."""
from __future__ import annotations
from itertools import islice
from math import isfinite
from typing import TYPE_CHECKING
from shapely import union_all
from shapely.geometry import box
from inire.constants import TOLERANCE_LINEAR
from inire.geometry.collision import RoutingWorld
from inire.model import resolve_bend_geometry
from inire.router._columns import _exact_spans
from inire.router._seed_materialization import materialize_path_seed
from inire.seeds import Bend90Seed, PathSeed, SBendSeed, StraightSeed
if TYPE_CHECKING:
from collections.abc import Iterator
from shapely.geometry.base import BaseGeometry
from inire.model import NetSpec, RoutingProblem, SearchOptions
from inire.seeds import PathSegmentSeed
def _path(*moves: int | PathSegmentSeed) -> PathSeed:
return PathSeed(tuple(StraightSeed(move) if isinstance(move, int) else move for move in moves if move != 0))
def _lanes(first: int, last: int) -> tuple[int, ...]:
return tuple(dict.fromkeys((*range(last, max(first - 1, last - 8), -1), *range(min(last, first + 7), first - 1, -1))))
def _shift_positions(
problem: RoutingProblem,
origin: int,
sign: int,
radius: int,
run: int,
available: int,
) -> list[int]:
positions = {i * available // 8 for i in range(9)}
for obstacle in problem.static_obstacles:
if obstacle.is_empty:
continue
low, high = sorted(sign * (y - origin) - radius for y in (obstacle.bounds[1], obstacle.bounds[3]))
positions.update((round(low - problem.clearance - run), round(high + problem.clearance), round((low + high - run) / 2)))
return sorted((y for y in positions if 0 <= y <= available), key=lambda y: (abs(2 * y - available), y))
def _candidate_jogs(
net: NetSpec,
problem: RoutingProblem,
search: SearchOptions,
radii: tuple[int, ...],
) -> Iterator[PathSeed]:
start, target = net.start, net.target
dx, dy = target.x - start.x, target.y - start.y
if dy == 0:
yield _path(dx)
return
direction = 1 if dy > 0 else -1
for radius in radii:
reach = abs(dy) - 2 * radius
if reach < 0 or dx < 2 * radius:
continue
first = Bend90Seed(radius, "CCW" if direction > 0 else "CW")
last = Bend90Seed(radius, "CW" if direction > 0 else "CCW")
turns = _lanes(start.x + radius, target.x - radius)
for x in turns:
yield _path(x - start.x - radius, first, reach, last, target.x - x - radius)
if radius not in search.sbend_radii:
continue
# Move the middle of the jog between two independently positioned
# turn lanes. The S-bend stays away from the terminal access cutouts.
for offset, span in reversed(tuple(_exact_spans(radius).items())):
if span > reach:
continue
available = reach - span
for before in _shift_positions(problem, start.y, direction, radius, span, available):
for exit_x in turns:
for lateral in (offset, -offset):
lead = exit_x + lateral - start.x - radius
if lead < 0:
continue
yield _path(
lead,
first,
before,
SBendSeed(direction * lateral, radius),
available - before,
last,
target.x - exit_x - radius,
)
def _pack_routes(
candidates: dict[str, list[tuple[PathSeed, BaseGeometry]]],
clearance: float,
) -> dict[str, PathSeed] | None:
order = sorted(candidates, key=lambda net_id: len(candidates[net_id]))
chosen: dict[str, tuple[PathSeed, BaseGeometry]] = {}
remaining = 4096
def add(index: int) -> bool:
nonlocal remaining
if index == len(order):
return True
net_id = order[index]
for seed, footprint in candidates[net_id]:
if remaining <= 0:
break
remaining -= 1
if any(footprint.distance(other) < clearance - TOLERANCE_LINEAR for _, other in chosen.values()):
continue
chosen[net_id] = (seed, footprint)
if add(index + 1):
return True
del chosen[net_id]
return False
return {net_id: value[0] for net_id, value in chosen.items()} if add(0) else None
def compact_port_seed_set(problem: RoutingProblem, search: SearchOptions) -> dict[str, PathSeed] | None:
"""Verify candidates individually and find mutually clear routes for a bank."""
_, physical_geometry = resolve_bend_geometry(search)
radii = tuple(sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)}))
if problem.initial_paths or not radii or not problem.nets or physical_geometry != "arc":
return None
if any(net.start.r != 0 or net.target.r != 0 or not 0 < net.target.x - net.start.x <= 4 * radii[-1] for net in problem.nets):
return None
world = RoutingWorld(clearance=problem.clearance, safety_zone_radius=problem.safety_zone_radius)
for obstacle in problem.static_obstacles:
world.add_static_obstacle(obstacle)
board = box(*problem.bounds)
candidates = {}
for net in problem.nets:
choices = []
for seed in islice(_candidate_jogs(net, problem, search, radii), 512):
path = materialize_path_seed(seed, start=net.start, net_width=net.width, search=search, clearance=problem.clearance)
if path[-1].end_port != net.target or not world.verify_path_report(net.net_id, path).is_valid:
continue
footprint = union_all([polygon for component in path for polygon in component.physical_geometry])
if board.covers(footprint):
choices.append((seed, footprint))
if len(choices) == 20:
break
if not choices:
return None
candidates[net.net_id] = choices
return _pack_routes(candidates, problem.clearance)