Choose forward detour lanes from nearby keepouts

This commit is contained in:
Jan Petykiewicz 2026-11-30 12:13:27 -08:00
commit 4537392641
2 changed files with 29 additions and 10 deletions

View file

@ -262,25 +262,30 @@ def perimeter_detour_candidates(problem: RoutingProblem, search: SearchOptions)
spare = net.target.x - net.start.x - 4 * radius spare = net.target.x - net.start.x - 4 * radius
if spare < 0 or not obstacles: if spare < 0 or not obstacles:
continue continue
padding = problem.clearance + net.width / 2
nearby = tuple(bounds for bounds in obstacles if bounds[0] <= net.target.x + padding and bounds[2] >= net.start.x - padding)
low, high = sorted((net.start.y, net.target.y)) low, high = sorted((net.start.y, net.target.y))
if all(bottom > high or top < low or right < net.start.x or left > net.target.x for left, bottom, right, top in obstacles): if all(bottom > high or top < low or right < net.start.x or left > net.target.x for left, bottom, right, top in nearby):
continue continue
margin = ceil(problem.clearance + net.width / 2 + 1) margin = ceil(padding + 1)
lanes = (max(ceil(top + margin) for _, _, _, top in obstacles), min(floor(bottom - margin) for _, bottom, _, _ in obstacles)) top = max(ceil(top + margin) for _, _, _, top in nearby)
bottom = min(floor(bottom - margin) for _, bottom, _, _ in nearby)
lanes, fallback_lanes = [], []
for y, fallback in ((top, high + 2 * radius), (bottom, low - 2 * radius)):
if min(abs(y - low), abs(y - high)) >= 2 * radius:
lanes.append(y)
else:
fallback_lanes.append(fallback)
end_slack = 1 if spare >= 2 else 0 end_slack = 1 if spare >= 2 else 0
pivots = {net.start.x + radius + end_slack, net.target.x - radius - end_slack} pivots = {net.start.x + radius + end_slack, net.target.x - radius - end_slack}
pivots.update( pivots.update(
x x
for left, _, right, _ in obstacles for left, _, right, _ in nearby
for x in (floor(left - radius - margin), ceil(right + radius + margin)) for x in (floor(left - radius - margin), ceil(right + radius + margin))
if net.start.x + radius <= x <= net.target.x - radius if net.start.x + radius <= x <= net.target.x - radius
) )
for y in lanes: for y in (*lanes, *fallback_lanes):
if ( if not problem.bounds[1] + net.width / 2 <= y <= problem.bounds[3] - net.width / 2:
abs(y - net.start.y) < 2 * radius
or abs(y - net.target.y) < 2 * radius
or not problem.bounds[1] + net.width / 2 <= y <= problem.bounds[3] - net.width / 2
):
continue continue
for first, last in combinations(sorted(pivots), 2): for first, last in combinations(sorted(pivots), 2):
if last - first < 2 * radius: if last - first < 2 * radius:

View file

@ -53,6 +53,20 @@ def test_tight_forward_perimeter_detours_use_pivots_without_extra_end_spacing(fo
assert sum(isinstance(move, Bend90Seed) for move in result.as_seed().segments) == 4 assert sum(isinstance(move, Bend90Seed) for move in result.as_seed().segments) == 4
def test_forward_perimeter_lanes_ignore_keepouts_beyond_the_target() -> None:
net = NetSpec("through", Port(20, 60, 0), Port(120, 60, 0))
problem = RoutingProblem((0, 0, 200, 120), (net,), (box(55, 30, 85, 90),), clearance=0)
distant = (box(150, 0, 160, 4), box(150, 116, 160, 120))
clean = route(problem, options=OPTIONS).results_by_net[net.net_id]
cluttered = route(replace(problem, static_obstacles=(*problem.static_obstacles, *distant)), options=OPTIONS)
result = cluttered.results_by_net[net.net_id]
assert cluttered.automatic.selected_planner == "perimeter"
assert result.is_valid
assert result.as_seed() == clean.as_seed()
@pytest.mark.parametrize("smaller_radius", [10, 17.5]) @pytest.mark.parametrize("smaller_radius", [10, 17.5])
def test_perimeter_detours_choose_a_radius_usable_by_the_planner(smaller_radius) -> None: def test_perimeter_detours_choose_a_radius_usable_by_the_planner(smaller_radius) -> None:
net = NetSpec("wide", Port(20, 70, 0), Port(220, 70, 0), width=30) net = NetSpec("wide", Port(20, 70, 0), Port(220, 70, 0), width=30)