From 393c01704a221abebb0a9b11afc1929763518e82 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 30 Nov 2026 12:42:15 -0800 Subject: [PATCH] Share the return candidate budget across lanes --- inire/router/_perimeter.py | 17 +++++++++-------- inire/tests/test_perimeter.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/inire/router/_perimeter.py b/inire/router/_perimeter.py index 010b7b6..d996cf3 100644 --- a/inire/router/_perimeter.py +++ b/inire/router/_perimeter.py @@ -245,6 +245,9 @@ def _return_detours( 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)) + lanes = tuple(y for y in dict.fromkeys((bottom, top, low, high)) if ymin + half_width <= y <= ymax - half_width) + if not lanes: + return left_edge = ceil(xmin + half_width) right_edge = floor(xmax - half_width) pivots = {left_edge, right_edge, start.x + radius + 1, target.x - radius - 1} @@ -252,14 +255,12 @@ def _return_detours( pivots.update((floor(left - margin), ceil(right + margin))) departures = sorted(x for x in pivots if start.x + radius <= x <= right_edge) arrivals = sorted((x for x in pivots if left_edge <= x <= target.x - radius), reverse=True) - for y in dict.fromkeys((bottom, top, low, high)): - 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 + # Try each lane for a pivot pair before spending the budget on more pivots. + for first, last, y in product(departures, arrivals, lanes): + 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]]: diff --git a/inire/tests/test_perimeter.py b/inire/tests/test_perimeter.py index 5e16d3a..bb09414 100644 --- a/inire/tests/test_perimeter.py +++ b/inire/tests/test_perimeter.py @@ -115,6 +115,29 @@ def test_return_detours_try_inner_lanes_when_keepouts_reach_board_edges() -> Non assert run.results_by_net[net.net_id].is_valid +def test_return_detours_try_both_sides_within_the_candidate_budget(monkeypatch) -> None: + net = NetSpec("return", Port(130, 60, 0), Port(40, 60, 0)) + obstacles = (box(135, 42, 199, 48), *(box(x, 100, x + 1, 104) for x in (*range(10, 29, 3), *range(142, 181, 4)))) + problem = RoutingProblem((0, 0, 200, 130), (net,), obstacles, clearance=0) + original_detours = _automatic.perimeter_detour_candidates + generated = 0 + + def detours(candidate_problem, search): + nonlocal generated + for candidate in original_detours(candidate_problem, search): + generated += 1 + yield candidate + + monkeypatch.setattr(_automatic, "perimeter_detour_candidates", detours) + run = route(problem, options=OPTIONS) + + result = run.results_by_net[net.net_id] + assert run.automatic.selected_planner == "perimeter" + assert result.is_valid + assert next(move.direction for move in result.as_seed().segments if isinstance(move, Bend90Seed)) == "CCW" + assert 0 < generated <= 32 + + @pytest.mark.parametrize(("forward", "needs_short_straight"), [(43, True), (60, False)]) def test_residual_doglegs_prefer_the_lattice_minimum_but_can_relax_it(forward, needs_short_straight) -> None: trunk = NetSpec("trunk", Port(20, 50, 0), Port(20 + forward, 50, 0))