inire/inire/router/_capacity.py

85 lines
3.9 KiB
Python

"""Geometric upper bounds on automatic routing coverage."""
from __future__ import annotations
from itertools import combinations, pairwise
from math import floor, isfinite
from typing import TYPE_CHECKING
from shapely.geometry import box
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:
bounds = tuple(tuple(shape.bounds) for shape in problem.static_obstacles)
if any(shape.is_empty or not shape.equals(box(*extent)) for shape, extent in zip(problem.static_obstacles, bounds, strict=True)):
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()
for ports in aligned.values():
for (a, y1, w1), (b, y2, w2) in combinations(ports, 2):
if a == b or a in matched or b in matched:
continue
gap = abs(y1 - y2) - (w1 + w2) / 2
if gap < problem.clearance - TOLERANCE_LINEAR:
pairs.append((a, b))
matched.update((a, b))
return tuple(pairs)