Check actual keepouts on residual dogleg bounds hits

This commit is contained in:
Jan Petykiewicz 2026-11-30 12:41:20 -08:00
commit 2bc8cead6e
2 changed files with 34 additions and 4 deletions

View file

@ -3,12 +3,14 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import replace from dataclasses import replace
from functools import lru_cache
from itertools import combinations, islice, pairwise, product from itertools import combinations, islice, pairwise, product
from math import ceil, floor, isfinite from math import ceil, floor, isfinite
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from inire.geometry.components import bend90_uses_circular_geometry, bend_fits_width from inire.geometry.collision import RoutingWorld
from inire.geometry.primitives import rotate_cardinal from inire.geometry.components import Straight, bend90_uses_circular_geometry, bend_fits_width
from inire.geometry.primitives import Port, rotate_cardinal
from inire.model import resolve_bend_geometry from inire.model import resolve_bend_geometry
from inire.router._seed_materialization import materialize_path_seed from inire.router._seed_materialization import materialize_path_seed
from inire.router._waypoints import _direction, compile_waypoints, export_waypoints from inire.router._waypoints import _direction, compile_waypoints, export_waypoints
@ -62,10 +64,25 @@ def residual_dogleg_candidates(problem: RoutingProblem, search: SearchOptions, *
if c + pad >= start_x and a - pad <= end_x: if c + pad >= start_x and a - pad <= end_x:
obstacles.append((a - pad, b - pad, c + pad, d + pad)) obstacles.append((a - pad, b - pad, c + pad, d + pad))
world: RoutingWorld | None = None
@lru_cache(maxsize=4096)
def straight_clear(a: int, b: int, c: int, d: int) -> bool: def straight_clear(a: int, b: int, c: int, d: int) -> bool:
nonlocal world
length = abs(c - a) + abs(d - b)
if not length:
return True
left, right = sorted((a, c)) left, right = sorted((a, c))
bottom, top = sorted((b, d)) 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) if all(right < x0 or left > x1 or top < y0 or bottom > y1 for x0, y0, x1, y1 in obstacles):
return True
if world is None:
world = RoutingWorld.from_problem(problem)
start, end = physical(a, b), physical(c, d)
direction = _direction(start, end)
assert direction is not None
straight = Straight.generate(Port(*start, direction), length, net.width)
return not world.check_move_static(straight)
lanes = {ceil(floor_y + half_width), floor(ceiling_y - half_width)} lanes = {ceil(floor_y + half_width), floor(ceiling_y - half_width)}
for _, bottom, _, top in obstacles: for _, bottom, _, top in obstacles:

View file

@ -1,7 +1,7 @@
from dataclasses import replace from dataclasses import replace
import pytest import pytest
from shapely.geometry import box from shapely.geometry import Polygon, box
from inire import ( from inire import (
Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, StraightSeed, route, Bend90Seed, CongestionOptions, NetSpec, Port, RefinementOptions, RoutingOptions, RoutingProblem, SearchOptions, StraightSeed, route,
@ -135,6 +135,19 @@ def test_residual_doglegs_spend_attempts_only_on_radii_that_fit_the_trace() -> N
assert {move.radius for move in segments if isinstance(move, Bend90Seed)} == {20} assert {move.radius for move in segments if isinstance(move, Bend90Seed)} == {20}
def test_residual_dogleg_can_cross_clear_space_inside_a_keepout_bounds() -> None:
trunk = NetSpec("trunk", Port(20, 60, 0), Port(120, 60, 0))
north = NetSpec("north", Port(10, 70, 90), Port(10, 105, 90))
rail = NetSpec("rail", Port(115, 100, 0), Port(145, 100, 0))
concave = Polygon(((12, 5), (90, 5), (90, 8), (15, 8), (15, 75), (12, 75)))
problem = RoutingProblem((0, 0, 150, 120), (trunk, north, rail), (concave, box(58, 56, 65, 64)), clearance=0)
run = route(problem, options=OPTIONS)
assert run.automatic.selected_planner == "residual-dogleg"
assert all(result.is_valid for result in run.results_by_net.values())
@pytest.mark.parametrize("requires_reservation", [False, True]) @pytest.mark.parametrize("requires_reservation", [False, True])
def test_paired_perimeter_plans_peer_reserved_detours_only_as_a_fallback(monkeypatch, requires_reservation) -> None: def test_paired_perimeter_plans_peer_reserved_detours_only_as_a_fallback(monkeypatch, requires_reservation) -> None:
line = NetSpec("line", Port(5, 75, 0), Port(195, 75, 0)) line = NetSpec("line", Port(5, 75, 0), Port(195, 75, 0))