Recover bounded routes around bends and through narrow corridors

This commit is contained in:
Jan Petykiewicz 2026-11-28 20:29:40 -08:00
commit af3ddfbb09

View file

@ -11,7 +11,9 @@ from typing import TYPE_CHECKING
import shapely
from shapely.geometry import LineString, Point, box
from inire.constants import TOLERANCE_LINEAR
from inire.geometry.collision import _violates_clearance
from inire.geometry.index_helpers import is_axis_aligned_rect
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
@ -96,7 +98,7 @@ def constrained_triangulation_available() -> bool:
class _Mesh:
def __init__(self, board: Polygon, obstacles: Iterable[Polygon], margin: float, clearance: float) -> None:
def __init__(self, board: Polygon, obstacles: Iterable[Polygon], margin: float, clearance: float, *, relief: float = 0.0) -> None:
if not constrained_triangulation_available():
raise RuntimeError("Topological routing requires Shapely 2.1 and GEOS 3.10 or newer")
@ -105,7 +107,11 @@ class _Mesh:
self.margin = margin
self.clearance = clearance
self.keepouts = shapely.STRtree(tuple(obstacles))
padded = shapely.union_all([shape.buffer(margin + clearance, join_style="mitre") for shape in self.keepouts.geometries])
padding = max(0.0, margin + clearance - relief)
padded = shapely.union_all([
shape.buffer(padding, join_style="mitre" if is_axis_aligned_rect(shape) else "round")
for shape in self.keepouts.geometries
])
self.inset = board.buffer(-margin)
self.free = self.inset.difference(padded)
self.boundary = self.free.boundary
@ -446,24 +452,41 @@ def plan_corridors(
board = box(*bounds)
keepouts = tuple(obstacles)
def plan_group(group: Sequence[NetSpec], width: float) -> dict[str, tuple[Corridor, ...]]:
mesh = _Mesh(board, keepouts, width / 2, clearance)
def plan_group(group: Sequence[NetSpec], width: float, *, relief: float = 0.0) -> tuple[dict[str, tuple[Corridor, ...]], BaseGeometry]:
mesh = _Mesh(board, keepouts, width / 2, clearance, relief=relief)
endpoints, leads = _endpoint_access(mesh, group, bend_radius)
assignments = _plan_lane_assignments(
mesh, group, endpoints, leads, bend_radius=bend_radius, limit=limit, joint_net_ids=joint_net_ids
)
return _build_corridors(mesh, board, group, assignments, bend_radius)
return _build_corridors(mesh, board, group, assignments, bend_radius), mesh.free
widest = max(net.width for net in nets)
guides = plan_group(nets, widest)
guides, shared_space = plan_group(nets, widest)
if joint_net_ids:
return guides
# Keep shared lane assignments and recover passages closed by the widest net's padding.
missing_by_width: dict[float, list[NetSpec]] = {}
# Keep shared assignments and add routes through passages opened by the narrower margin.
by_width: dict[float, list[NetSpec]] = {}
for net in nets:
if net.width < widest and net.net_id not in guides:
missing_by_width.setdefault(net.width, []).append(net)
for width, group in missing_by_width.items():
guides.update(plan_group(group, width))
by_width.setdefault(net.width, []).append(net)
for width, group in by_width.items():
if width < widest:
narrow, _ = plan_group(group, width)
for net_id, corridors in narrow.items():
if net_id not in guides:
guides[net_id] = corridors
continue
alternate = next(
(c for c in corridors if c.centerline.difference(shared_space).length > widest + 2 * clearance),
None,
)
if alternate is not None:
first, *rest = guides[net_id]
guides[net_id] = (first, alternate, *rest)[:limit]
missing = [net for net in group if net.net_id not in guides]
if missing and keepouts:
# Exact-clearance passages can collapse to a line before triangulation.
# This affects only candidate guidance. Move admission checks the original shapes.
recovered, _ = plan_group(missing, width, relief=8 * TOLERANCE_LINEAR)
guides.update(recovered)
return {net.net_id: guides[net.net_id] for net in nets if net.net_id in guides}