From 13b2d8b7de096cf18bd633725b6015636599e43a Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 30 Nov 2026 10:42:01 -0800 Subject: [PATCH] Bound implicit exact S-bend offset discovery --- docs/user/api-reference.md | 2 ++ inire/geometry/sbend.py | 11 +++++++++++ inire/router/_columns.py | 7 +++++-- inire/router/_compact.py | 7 +++++-- inire/tests/test_columns.py | 29 +++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/docs/user/api-reference.md b/docs/user/api-reference.md index fcb706d..2e5cd22 100644 --- a/docs/user/api-reference.md +++ b/docs/user/api-reference.md @@ -99,6 +99,8 @@ A custom S-bend outline uses local coordinates. Its input face is centered at `( Match custom outline faces to their declared ports and width. `SBendOutline` warns about a mismatch at construction. For a custom 90-degree outline used in a result, `route()` warns once per radius and net width. Replay an `SBendSeed` with the same outlines and bend settings. +Constructive planners that need exact integer S-bend spans check up to 65,536 implicit offsets. When that budget would be exceeded, automatic routing tries other candidates. In explicit bundle mode, supply `sbend_offsets` to check a specific list of offsets. + Search and replay use placed custom outlines for board containment. When a declared port lies outside the board, proximity scoring samples the nearest point on the placed outline. Final verification checks the placed polygons against the board, keepouts, and other routes. For a complete custom S-bend outline, or a custom 90-degree bend with a custom polygon proxy, the radius determines the declared snapped endpoints. Use the supplied polygons to represent the process geometry. diff --git a/inire/geometry/sbend.py b/inire/geometry/sbend.py index eba72f9..a00630d 100644 --- a/inire/geometry/sbend.py +++ b/inire/geometry/sbend.py @@ -4,6 +4,12 @@ from math import ceil, isfinite, isqrt, sqrt from inire.constants import TOLERANCE_LINEAR +_MAX_IMPLICIT_OFFSET_CHECKS = 65_536 + + +class ExactSBendScanLimitError(ValueError): + """Implicit exact-span discovery exceeds the planner's offset budget.""" + def sbend_span(offset: float, radius: float) -> float: offset = abs(offset) @@ -29,6 +35,11 @@ def exact_sbend_spans( offset_limit = diameter - (isqrt(remaining - 1) + 1 if remaining > 0 else 1) if max_offset is not None: offset_limit = min(offset_limit, max_offset) + if offsets is None and offset_limit > _MAX_IMPLICIT_OFFSET_CHECKS: + raise ExactSBendScanLimitError( + f"Implicit exact S-bend discovery would check {offset_limit:,} offsets (limit {_MAX_IMPLICIT_OFFSET_CHECKS:,}). " + "Configure sbend_offsets to supply an explicit list." + ) candidates = range(1, offset_limit + 1) if offsets is None else sorted({int(abs(offset)) for offset in offsets}) spans = {} for offset in candidates: diff --git a/inire/router/_columns.py b/inire/router/_columns.py index 497a3be..5cf6d3d 100644 --- a/inire/router/_columns.py +++ b/inire/router/_columns.py @@ -13,7 +13,7 @@ from typing import TYPE_CHECKING from shapely.geometry import LineString from inire.geometry.intervals import Gap, open_intervals -from inire.geometry.sbend import exact_sbend_spans +from inire.geometry.sbend import ExactSBendScanLimitError, exact_sbend_spans from inire.model import resolve_bend_geometry from inire.router._waypoints import compile_waypoints from inire.seeds import PathSeed, SBendSeed, StraightSeed @@ -569,7 +569,10 @@ def column_seed_candidates( radius = int(bend) max_span = max(before - after - 4 for (_, after), (before, _) in pairwise(layout.spans)) max_offset = floor(problem.bounds[3]) - ceil(problem.bounds[1]) - spans = exact_sbend_spans(radius, max_span=max_span, max_offset=max_offset, offsets=search.sbend_offsets) + try: + spans = exact_sbend_spans(radius, max_span=max_span, max_offset=max_offset, offsets=search.sbend_offsets) + except ExactSBendScanLimitError: + return if not spans: return allowed_offsets = None if search.sbend_offsets is None else frozenset(search.sbend_offsets) diff --git a/inire/router/_compact.py b/inire/router/_compact.py index f7a5c64..d579bd5 100644 --- a/inire/router/_compact.py +++ b/inire/router/_compact.py @@ -11,7 +11,7 @@ from shapely.geometry import box from inire.geometry.collision import RoutingWorld, _violates_net_clearance from inire.geometry.primitives import port_local_coordinates -from inire.geometry.sbend import exact_sbend_spans, sbend_grid_span +from inire.geometry.sbend import ExactSBendScanLimitError, exact_sbend_spans, sbend_grid_span from inire.model import resolve_bend_geometry from inire.results import RoutingResult from inire.router._seed_materialization import materialize_path_seed @@ -80,7 +80,10 @@ def _candidate_jogs( continue # Move the middle of the jog between two independently positioned # turn lanes. The S-bend stays away from the terminal access cutouts. - spans = exact_sbend_spans(radius, max_span=int(reach), max_offset=max_offset, offsets=search.sbend_offsets) + try: + spans = exact_sbend_spans(radius, max_span=int(reach), max_offset=max_offset, offsets=search.sbend_offsets) + except ExactSBendScanLimitError: + continue for offset, span in reversed(tuple(spans.items())): available = reach - span for before in _shift_positions(problem, start.y, direction, radius, span, available): diff --git a/inire/tests/test_columns.py b/inire/tests/test_columns.py index a848690..851efa2 100644 --- a/inire/tests/test_columns.py +++ b/inire/tests/test_columns.py @@ -131,6 +131,35 @@ def test_exact_sbend_spans_filter_by_window_and_configured_offsets() -> None: assert exact_sbend_spans(25, max_span=40, max_offset=10, offsets=(-20, 10, 40, 0)) == {10: 30} +def test_exact_sbend_scan_budget_applies_only_to_implicit_offsets(monkeypatch) -> None: + monkeypatch.setattr(sbend, "_MAX_IMPLICIT_OFFSET_CHECKS", 10) + + with pytest.raises(sbend.ExactSBendScanLimitError, match="would check 20 offsets"): + exact_sbend_spans(25, max_span=40) + + assert exact_sbend_spans(25, max_span=40, max_offset=10) == {2: 14, 10: 30} + assert exact_sbend_spans(25, max_span=40, offsets=(-20,)) == {20: 40} + + +def test_automatic_routes_wide_tall_boards_without_a_large_implicit_scan(monkeypatch) -> None: + def unexpected_scan(_value): + raise AssertionError("The implicit offset scan exceeds the work budget") + + monkeypatch.setattr(sbend, "isqrt", unexpected_scan) + nets = tuple(NetSpec(str(y), Port(5, y, 0), Port(399_999_990, y, 0)) for y in (30, 60, 90)) + problem = RoutingProblem((0, 0, 400_000_000, 50_000_000), nets, (box(190_000_000, 0, 190_000_020, 10),), clearance=0) + options = RoutingOptions( + search=SearchOptions(node_limit=1, bend_radii=(), sbend_radii=(100_000_000,)), + congestion=CongestionOptions(max_iterations=1, warm_start_enabled=False), + refinement=RefinementOptions(enabled=False), + ) + + run = route(problem, options=options) + + assert all(run.results_by_net[net.net_id].is_valid for net in nets) + assert run.metrics.nodes_expanded == 0 + + def test_wide_column_windows_only_scan_offsets_within_the_board_height(monkeypatch) -> None: square_root = sbend.isqrt scans = 0