283 lines
13 KiB
Python
283 lines
13 KiB
Python
"""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.geometry.primitives import rotate_cardinal
|
|
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 residual_dogleg_candidates(problem: RoutingProblem, search: SearchOptions) -> Iterator[PathSeed]:
|
|
"""Try four-turn routes through interior gaps with the incumbent geometry locked."""
|
|
if len(problem.nets) != 1 or problem.initial_paths:
|
|
return
|
|
(net,) = problem.nets
|
|
if net.start.r != net.target.r:
|
|
return
|
|
heading = int(net.start.r)
|
|
|
|
def project(x: float, y: float) -> tuple[float, float]:
|
|
return rotate_cardinal(x, y, -heading)
|
|
|
|
def physical(x: int, y: int) -> XY:
|
|
return rotate_cardinal(x, y, heading)
|
|
|
|
def extent(bounds: tuple[float, float, float, float]) -> tuple[float, float, float, float]:
|
|
x0, y0, x1, y1 = bounds
|
|
a, b = project(x0, y0), project(x1, y1)
|
|
return min(a[0], b[0]), min(a[1], b[1]), max(a[0], b[0]), max(a[1], b[1])
|
|
|
|
start_x, start_y = map(int, project(net.start.x, net.start.y))
|
|
end_x, end_y = map(int, project(net.target.x, net.target.y))
|
|
if end_x <= start_x:
|
|
return
|
|
half_width = net.width / 2
|
|
pad = problem.clearance + half_width
|
|
_, floor_y, _, ceiling_y = extent(problem.bounds)
|
|
obstacles = []
|
|
for shape in problem._blocking_polygons:
|
|
if shape.is_empty:
|
|
continue
|
|
a, b, c, d = extent(shape.bounds)
|
|
if c + pad >= start_x and a - pad <= end_x:
|
|
obstacles.append((a - pad, b - pad, c + pad, d + pad))
|
|
|
|
def straight_clear(a: int, b: int, c: int, d: int) -> bool:
|
|
left, right = sorted((a, c))
|
|
bottom, top = sorted((b, d))
|
|
return all(right <= x0 or left >= x1 or top <= y0 or bottom >= y1 for x0, y0, x1, y1 in obstacles)
|
|
|
|
lanes = {ceil(floor_y + half_width), floor(ceiling_y - half_width)}
|
|
for _, bottom, _, top in obstacles:
|
|
lanes.update((floor(bottom), ceil(top)))
|
|
lanes = {y for y in lanes if floor_y + half_width <= y <= ceiling_y - half_width}
|
|
ordered_lanes = sorted(lanes, key=lambda y: (abs(y - start_y) + abs(y - end_y), y))
|
|
minimum, maximum = max(0.0, search.min_straight_length), search.max_straight_length
|
|
remaining = 16384
|
|
for radius in sorted({int(r) for r in search.bend_radii if isfinite(r) and r > 0 and r == int(r)}):
|
|
first, last = ceil(start_x + radius + minimum), floor(end_x - radius - minimum)
|
|
if last - first < 2 * radius:
|
|
continue
|
|
pivots = {first, last}
|
|
for left, _, right, _ in obstacles:
|
|
pivots.update((floor(left - radius), ceil(right + radius)))
|
|
pivots = sorted(x for x in pivots if first <= x <= last)
|
|
entrances = [x for x in pivots if straight_clear(start_x, start_y, x - radius, start_y)]
|
|
exits = [x for x in reversed(pivots) if straight_clear(x + radius, end_y, end_x, end_y)]
|
|
for y in ordered_lanes:
|
|
rises = [sorted((port_y, y)) for port_y in (start_y, end_y)]
|
|
if any(high - low < 2 * radius + minimum for low, high in rises):
|
|
continue
|
|
lower, upper = rises
|
|
departures = [x for x in entrances if straight_clear(x, lower[0] + radius, x, lower[1] - radius)]
|
|
arrivals = [x for x in exits if straight_clear(x, upper[0] + radius, x, upper[1] - radius)]
|
|
for a, b in product(departures, arrivals):
|
|
if not remaining:
|
|
return
|
|
remaining -= 1
|
|
if not 2 * radius + minimum <= b - a <= 2 * radius + maximum:
|
|
continue
|
|
if not straight_clear(a + radius, y, b - radius, y):
|
|
continue
|
|
points = tuple(physical(x, h) for x, h in ((start_x, start_y), (a, start_y), (a, y), (b, y), (b, end_y), (end_x, end_y)))
|
|
seed, end = compile_waypoints(points, net.start, radius)
|
|
if end == net.target and all(
|
|
not isinstance(move, StraightSeed) or minimum <= move.length <= maximum for move in seed.segments
|
|
):
|
|
yield seed
|
|
|
|
|
|
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,
|
|
bounds=problem.bounds,
|
|
)
|
|
return tuple((round(x), round(y)) for x, y in export_waypoints(path or ()))
|
|
|
|
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._blocking_polygons 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
|