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

201
inire/router/_perimeter.py Normal file
View file

@ -0,0 +1,201 @@
"""Perimeter detours and fixed-heading returns for ordered bundles."""
from __future__ import annotations
from dataclasses import replace
from itertools import combinations, pairwise, product
from math import ceil, floor, isfinite
from typing import TYPE_CHECKING
from inire.model import resolve_bend_geometry
from inire.router._seed_materialization import materialize_path_seed
from inire.router._waypoints import _direction, compile_waypoints, export_waypoints
from inire.seeds import PathSeed, StraightSeed
if TYPE_CHECKING:
from collections.abc import Iterator
from inire.model import NetSpec, RoutingProblem, SearchOptions
from inire.router._waypoints import XY
def bundle_turnaround_candidates(
problem: RoutingProblem,
proposal: RoutingProblem,
search: SearchOptions,
) -> Iterator[RoutingProblem]:
"""Fit a final turnaround below the shelf without changing terminal headings."""
if len(problem.nets) != len(proposal.nets) or not problem.nets:
return
mismatches = [
(fixed, inferred)
for fixed, inferred in zip(problem.nets, proposal.nets, strict=True)
if (fixed.start, fixed.target) != (inferred.start, inferred.target)
]
if len(mismatches) != 1:
return
returning, inferred = mismatches[0]
if returning.start != inferred.start or returning.target.r != 0 or inferred.target.r != 180:
return
radius = int(min(search.bend_radii))
farthest = max(net.target.x for net in problem.nets)
shelf = max((net for net in problem.nets if net.target.x == farthest), key=lambda net: net.target.y)
if shelf == returning:
return
def points_for(net: NetSpec) -> tuple[XY, ...]:
path = materialize_path_seed(
proposal.initial_paths[net.net_id], start=net.start, net_width=net.width, search=search, clearance=problem.clearance
)
return tuple((round(x), round(y)) for x, y in export_waypoints(path))
shelf_points, return_points = points_for(shelf), points_for(returning)
horizontals = [i for i, (a, b) in enumerate(pairwise(shelf_points)) if a[1] == b[1]]
if len(return_points) != 6 or not horizontals:
return
launch = max(horizontals, key=lambda i: shelf_points[i][1])
drop = launch + 1
bends = [(a, b) for a, b in pairwise(shelf_points[drop:]) if a[0] != b[0] and a[1] != b[1]]
if len(bends) != 1 or drop + 1 >= len(shelf_points):
return
(left_x, left_y), (right_x, right_y) = bends[0]
drop_offset = left_y - right_y
if not 0 < drop_offset < 2 * radius:
return
min_gap = problem.clearance + (returning.width + shelf.width) / 2
# Parallel S-bends approach each other most at their largest tangent angle.
inner_gap = ceil(min_gap / (1 - drop_offset / (2 * radius)))
top = floor(problem.bounds[3] - shelf.width / 2)
first_drop = max(shelf_points[drop][0], ceil(returning.target.x + radius + min_gap))
for descent_x, gap, lead in product(
(first_drop, first_drop + radius // 2, first_drop + radius),
range(inner_gap, inner_gap + 5),
(0, 2, 4, 8),
):
shelf_candidate = list(shelf_points)
shelf_candidate[launch] = (shelf_points[launch][0], top)
shelf_candidate[drop] = (descent_x, top)
shelf_candidate[drop + 1] = (descent_x, shelf_points[drop + 1][1])
x_turn = returning.target.x - radius - lead
turn_points = (
*return_points[:4],
(return_points[4][0], right_y - gap),
(right_x, right_y - gap),
(left_x, left_y - gap),
(x_turn, left_y - gap),
(x_turn, returning.target.y),
(returning.target.x, returning.target.y),
)
try:
shelf_seed, shelf_end = compile_waypoints(tuple(shelf_candidate), shelf.start, radius)
return_seed, return_end = compile_waypoints(turn_points, returning.start, radius)
except ValueError:
continue
if shelf_end == shelf.target and return_end == returning.target:
seeds = {**proposal.initial_paths, shelf.net_id: shelf_seed, returning.net_id: return_seed}
yield replace(problem, initial_paths=seeds)
def terminal_link_seed(net: NetSpec, search: SearchOptions) -> PathSeed | None:
"""Connect aligned terminals with a straight or a single tangent bend."""
start, target = net.start, net.target
a, b = (start.x, start.y), (target.x, target.y)
if a == b:
return None
if start.r == target.r == _direction(a, b):
return PathSeed((StraightSeed(abs(b[0] - a[0]) + abs(b[1] - a[1])),))
if start.r % 180 == target.r % 180:
return None
corner = (b[0], a[1]) if start.r % 180 == 0 else (a[0], b[1])
radius = min(search.bend_radii, default=0.0)
if (
not isfinite(radius)
or radius <= 0
or corner in (a, b)
or _direction(a, corner) != start.r
or _direction(corner, b) != target.r
or min(abs(b[0] - a[0]), abs(b[1] - a[1])) < radius
):
return None
seed, end = compile_waypoints((a, corner, b), start, radius)
return seed if end == target else None
def _return_detours(
problem: RoutingProblem,
net: NetSpec,
obstacles: tuple[tuple[float, float, float, float], ...],
radius: int,
) -> Iterator[PathSeed]:
xmin, ymin, xmax, ymax = problem.bounds
half_width = net.width / 2
margin = ceil(problem.clearance + half_width + 1)
start, target = net.start, net.target
low = min(start.y, target.y) - 2 * radius
high = max(start.y, target.y) + 2 * radius
bottom = min(low, min((floor(bottom - margin) for _, bottom, _, _ in obstacles), default=low))
top = max(high, max((ceil(top + margin) for _, _, _, top in obstacles), default=high))
pivots = {xmin + half_width, xmax - half_width, start.x + radius + 1, target.x - radius - 1}
for left, _, right, _ in obstacles:
pivots.update((floor(left - margin), ceil(right + margin)))
departures = sorted(x for x in pivots if start.x + radius <= x <= xmax - half_width)
arrivals = sorted((x for x in pivots if xmin + half_width <= x <= target.x - radius), reverse=True)
for y in (bottom, top):
if not ymin + half_width <= y <= ymax - half_width:
continue
for first, last in product(departures, arrivals):
points = ((start.x, start.y), (first, start.y), (first, y), (last, y), (last, target.y), (target.x, target.y))
seed, end = compile_waypoints(points, start, radius)
if end == target:
yield seed
def perimeter_detour_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[tuple[NetSpec, PathSeed]]:
"""Try four-turn detours around obstacles or behind east-facing ports."""
_, physical_geometry = resolve_bend_geometry(search)
bend = min(search.bend_radii, default=0.0)
if problem.initial_paths or physical_geometry != "arc" or not isfinite(bend) or bend <= 0 or bend != int(bend):
return
obstacles = tuple(shape.bounds for shape in problem.static_obstacles if not shape.is_empty)
radius = int(bend)
for net in problem.nets:
if net.start.r != 0 or net.target.r != 0:
continue
if net.start.x > net.target.x:
yield from ((net, seed) for seed in _return_detours(problem, net, obstacles, radius))
continue
if net.start.x == net.target.x or not obstacles:
continue
low, high = sorted((net.start.y, net.target.y))
if all(bottom > high or top < low or right < net.start.x or left > net.target.x for left, bottom, right, top in obstacles):
continue
margin = ceil(problem.clearance + net.width / 2 + 1)
lanes = (max(ceil(top + margin) for _, _, _, top in obstacles), min(floor(bottom - margin) for _, bottom, _, _ in obstacles))
pivots = {net.start.x + radius + 1, net.target.x - radius - 1}
pivots.update(
x
for left, _, right, _ in obstacles
for x in (floor(left - radius - margin), ceil(right + radius + margin))
if net.start.x + radius <= x <= net.target.x - radius
)
for y in lanes:
if (
abs(y - net.start.y) < 2 * radius
or abs(y - net.target.y) < 2 * radius
or not problem.bounds[1] + net.width / 2 <= y <= problem.bounds[3] - net.width / 2
):
continue
for first, last in combinations(sorted(pivots), 2):
if last - first < 2 * radius:
continue
points = (
(net.start.x, net.start.y),
(first, net.start.y),
(first, y),
(last, y),
(last, net.target.y),
(net.target.x, net.target.y),
)
seed, end = compile_waypoints(points, net.start, radius)
if end == net.target:
yield net, seed