2026-11-26 19:58:46 -08:00
|
|
|
"""Geometric upper bounds on automatic routing coverage."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-11-28 20:13:00 -08:00
|
|
|
from itertools import pairwise
|
2026-11-26 19:58:46 -08:00
|
|
|
from math import floor, isfinite
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
from shapely.geometry import box
|
2026-11-28 20:13:00 -08:00
|
|
|
from shapely.strtree import STRtree
|
2026-11-26 19:58:46 -08:00
|
|
|
|
|
|
|
|
from inire.constants import TOLERANCE_LINEAR
|
|
|
|
|
from inire.geometry.intervals import open_intervals
|
|
|
|
|
from inire.model import resolve_bend_geometry
|
|
|
|
|
from inire.results import CapacityLimit
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from inire.model import RoutingOptions, RoutingProblem
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _rectangular_obstacle_bounds(problem: RoutingProblem) -> tuple[tuple[float, float, float, float], ...] | None:
|
2026-11-29 00:12:59 -08:00
|
|
|
bounds = tuple(tuple(shape.bounds) for shape in problem._blocking_polygons)
|
|
|
|
|
if any(shape.is_empty or not shape.equals(box(*extent)) for shape, extent in zip(problem._blocking_polygons, bounds, strict=True)):
|
2026-11-26 19:58:46 -08:00
|
|
|
return None
|
|
|
|
|
return bounds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cut_capacity(problem: RoutingProblem, options: RoutingOptions) -> CapacityLimit | None:
|
|
|
|
|
"""Bound routes across vertical cuts through truly rectangular obstacles."""
|
|
|
|
|
_, physical_geometry = resolve_bend_geometry(options.search)
|
|
|
|
|
obstacles = _rectangular_obstacle_bounds(problem)
|
|
|
|
|
if physical_geometry != "arc" or not obstacles or not problem.nets:
|
|
|
|
|
return None
|
|
|
|
|
# The narrowest guide gives an optimistic upper bound for mixed widths.
|
|
|
|
|
width = min(net.width for net in problem.nets)
|
|
|
|
|
if not isfinite(width) or width <= 0 or problem.clearance < 0:
|
|
|
|
|
return None
|
|
|
|
|
pitch = width + problem.clearance
|
|
|
|
|
limit = None
|
|
|
|
|
edges = sorted({x for left, _, right, _ in obstacles for x in (left, right)})
|
|
|
|
|
for left, right in pairwise(edges):
|
|
|
|
|
x = (left + right) / 2
|
|
|
|
|
crossing = sum(min(net.start.x, net.target.x) + width / 2 < x < max(net.start.x, net.target.x) - width / 2 for net in problem.nets)
|
|
|
|
|
if not crossing:
|
|
|
|
|
continue
|
|
|
|
|
solid = [(bottom, top) for start, bottom, end, top in obstacles if start < x < end]
|
|
|
|
|
if not solid:
|
|
|
|
|
continue
|
|
|
|
|
ymin, ymax = problem.bounds[1], problem.bounds[3]
|
|
|
|
|
tracks = 0
|
|
|
|
|
for low, high in open_intervals(solid, ymin, ymax):
|
|
|
|
|
bottom = low + width / 2 + (problem.clearance if low != ymin else 0)
|
|
|
|
|
top = high - width / 2 - (problem.clearance if high != ymax else 0)
|
|
|
|
|
room = top - bottom + 2 * TOLERANCE_LINEAR
|
|
|
|
|
if room >= 0:
|
|
|
|
|
tracks += 1 + floor(room / max(TOLERANCE_LINEAR, pitch - TOLERANCE_LINEAR))
|
|
|
|
|
max_routed = len(problem.nets) - crossing + tracks
|
|
|
|
|
if tracks < crossing and (limit is None or max_routed < limit.max_routed):
|
|
|
|
|
limit = CapacityLimit(x, crossing, tracks, max_routed)
|
|
|
|
|
return limit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def port_conflicts(problem: RoutingProblem, options: RoutingOptions) -> tuple[tuple[str, str], ...]:
|
|
|
|
|
"""Find disjoint fixed-heading terminal pairs with insufficient optical clearance."""
|
|
|
|
|
_, physical_geometry = resolve_bend_geometry(options.search)
|
|
|
|
|
if physical_geometry != "arc" or problem.clearance <= TOLERANCE_LINEAR:
|
|
|
|
|
return ()
|
|
|
|
|
aligned: dict[tuple[int, int], list[tuple[str, float, float]]] = {}
|
|
|
|
|
for net in problem.nets:
|
|
|
|
|
for port in (net.start, net.target):
|
|
|
|
|
axis = port.r % 180
|
|
|
|
|
longitudinal, lateral = (port.x, port.y) if axis == 0 else (port.y, port.x)
|
|
|
|
|
aligned.setdefault((axis, longitudinal), []).append((net.net_id, lateral, net.width))
|
|
|
|
|
# Each disjoint conflict pair forces at least one route to be omitted.
|
|
|
|
|
# A terminal's physical cross-section is fixed by its heading and width.
|
|
|
|
|
pairs = []
|
|
|
|
|
matched = set()
|
2026-11-28 20:13:00 -08:00
|
|
|
separation = problem.clearance - TOLERANCE_LINEAR
|
|
|
|
|
for bank in aligned.values():
|
|
|
|
|
ports = [port for port in bank if port[0] not in matched]
|
|
|
|
|
if len(ports) < 2:
|
|
|
|
|
continue
|
|
|
|
|
pairs.extend(_match_bank(ports, separation, matched))
|
2026-11-26 19:58:46 -08:00
|
|
|
return tuple(pairs)
|
2026-11-28 20:13:00 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _match_bank(ports: list[tuple[str, float, float]], separation: float, matched: set[str]) -> list[tuple[str, str]]:
|
|
|
|
|
spans = [box(y - (width + separation) / 2, 0, y + (width + separation) / 2, 1) for _, y, width in ports]
|
|
|
|
|
index = STRtree(spans)
|
|
|
|
|
|
|
|
|
|
def conflicts(i: int, j: int) -> bool:
|
|
|
|
|
a, y1, w1 = ports[i]
|
|
|
|
|
b, y2, w2 = ports[j]
|
|
|
|
|
return a != b and b not in matched and abs(y1 - y2) - (w1 + w2) / 2 < separation
|
|
|
|
|
|
|
|
|
|
pairs = []
|
|
|
|
|
for i, (a, _, _) in enumerate(ports):
|
|
|
|
|
if a in matched:
|
|
|
|
|
continue
|
|
|
|
|
# Dense banks usually match consecutive terminals in input order.
|
|
|
|
|
j = i + 1 if i + 1 < len(ports) and conflicts(i, i + 1) else min(
|
|
|
|
|
(j for j in index.query(spans[i]) if j > i and conflicts(i, j)), default=None,
|
|
|
|
|
)
|
|
|
|
|
if j is None:
|
|
|
|
|
continue
|
|
|
|
|
b = ports[j][0]
|
|
|
|
|
pairs.append((a, b))
|
|
|
|
|
matched.update((a, b))
|
|
|
|
|
return pairs
|