diff --git a/inire/router/_perimeter.py b/inire/router/_perimeter.py index 67a2bc1..606f110 100644 --- a/inire/router/_perimeter.py +++ b/inire/router/_perimeter.py @@ -250,11 +250,18 @@ def _return_detours( 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} + min_departure = start.x + radius + max_arrival = target.x - radius + pivots = {left_edge, right_edge, min_departure + 1, max_arrival - 1} for left, _, right, _ in obstacles: 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) + departures = sorted(x for x in pivots if min_departure <= x <= right_edge) + arrivals = sorted((x for x in pivots if left_edge <= x <= max_arrival), reverse=True) + # Keep the first pivot pair and try bends that meet the ports directly next. + if min_departure <= right_edge and min_departure not in departures: + departures.insert(1, min_departure) + if left_edge <= max_arrival and max_arrival not in arrivals: + arrivals.insert(1, max_arrival) # 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)) diff --git a/inire/tests/test_perimeter.py b/inire/tests/test_perimeter.py index 61c810a..d8d4666 100644 --- a/inire/tests/test_perimeter.py +++ b/inire/tests/test_perimeter.py @@ -126,6 +126,21 @@ 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_can_fit_without_straights_at_either_port() -> None: + net = NetSpec("return", Port(100, 50, 0), Port(20, 50, 0)) + obstacles = (box(111.2, 38, 125, 62), box(0, 38, 8.8, 62)) + problem = RoutingProblem((0, 0, 125, 100), (net,), obstacles, clearance=0) + + run = route(problem, options=OPTIONS) + + assert run.automatic.selected_planner == "perimeter" + result = run.results_by_net[net.net_id] + assert result.is_valid + segments = result.as_seed().segments + assert isinstance(segments[0], Bend90Seed) + assert isinstance(segments[-1], Bend90Seed) + + @pytest.mark.parametrize( ("start", "target", "obstacles", "direction"), [