from dataclasses import replace import pytest from shapely.geometry import Polygon, box from inire import CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, route from inire.results import CapacityLimit from inire.router._capacity import cut_capacity, port_conflicts from inire.tests.routing_corpus import CASE_GROUPS def test_cut_capacity_requires_rectangular_obstacles() -> None: (case,) = CASE_GROUPS["overloaded-gate"] bound = cut_capacity(case.problem, case.options) assert bound is not None assert (bound.crossing_nets, bound.tracks, bound.max_routed) == (4, 3, case.routing_bound) triangle = Polygon([(10, 10), (20, 10), (10, 20)]) changed = replace(case.problem, static_obstacles=(*case.problem.static_obstacles, triangle)) assert cut_capacity(changed, case.options) is None def test_cut_capacity_counts_overlapping_rectangles_and_strict_crossings() -> None: through = tuple(NetSpec(str(y), Port(5, y, 0), Port(95, y, 0)) for y in (15, 40, 75)) arriving = NetSpec("arriving", Port(5, 45, 0), Port(46, 45, 0)) departing = NetSpec("departing", Port(44, 50, 0), Port(95, 50, 0)) vertical = NetSpec("vertical", Port(45, 10, 90), Port(45, 90, 90)) obstacles = (box(20, 0, 50, 30), box(20, 5, 50, 15), box(40, 34, 60, 100)) problem = RoutingProblem((0, 0, 100, 100), (*through, arriving, departing, vertical), obstacles, clearance=1) assert cut_capacity(problem, RoutingOptions()) == CapacityLimit(45, 3, 1, 4) def test_fixed_port_conflicts_are_disjoint_and_depend_on_clearance() -> None: (case,) = CASE_GROUPS["lidar-clements-8x8"] pairs = port_conflicts(case.problem, case.options) assert len(case.problem.nets) - len(pairs) == case.routing_bound assert len({net_id for pair in pairs for net_id in pair}) == 2 * len(pairs) assert port_conflicts(replace(case.problem, clearance=0), case.options) == () @pytest.mark.parametrize("heading", [0, 90]) @pytest.mark.parametrize( ("clearance", "second_width", "conflict"), [(0, 1.5, False), (0, 2, True), (0, 2.5, True), (1e-7, 2 - 1e-7, True), (0.25, 1.5, False)], ids=["positive-gap", "touching", "overlapping", "tiny-clearance", "exact-clearance"], ) def test_automatic_bound_matches_terminal_face_clearance(heading, clearance, second_width, conflict) -> None: nets = tuple( NetSpec(net_id, Port(10, y, 0).rotated(heading, (50, 50)), Port(90, y, 0).rotated(heading, (50, 50)), width) for net_id, y, width in (("a", 20, 2), ("clear", 40, 2), ("b", 22, second_width)) ) problem = RoutingProblem((0, 0, 100, 100), nets, clearance=clearance) options = RoutingOptions( search=SearchOptions(node_limit=20, bend_radii=(), sbend_radii=()), congestion=CongestionOptions(max_iterations=1), refinement=RefinementOptions(enabled=False), ) run = route(problem, options=options) assert run.automatic is not None assert run.automatic.port_conflicts == ((("a", "b"),) if conflict else ()) assert run.automatic.max_routed == len(nets) - int(conflict) assert len(run.automatic.verified_net_ids) <= run.automatic.max_routed