Reuse physical bounds for rectangular board checks

This commit is contained in:
Jan Petykiewicz 2026-11-30 15:35:42 -08:00
commit 6b699e492b
5 changed files with 76 additions and 24 deletions

View file

@ -103,7 +103,8 @@ class RoutingWorld:
"clearance", "clearance",
"self_clearance", "self_clearance",
"safety_zone_radius", "safety_zone_radius",
"board", "_board",
"_board_bounds",
"grid_cell_size", "grid_cell_size",
"metrics", "metrics",
"_dynamic_paths", "_dynamic_paths",
@ -122,7 +123,8 @@ class RoutingWorld:
self.clearance = clearance self.clearance = clearance
self.self_clearance = self_clearance self.self_clearance = self_clearance
self.safety_zone_radius = safety_zone_radius self.safety_zone_radius = safety_zone_radius
self.board = floating_polygon(board) if board is not None else None self._board = floating_polygon(board) if board is not None else None
self._board_bounds: tuple[float, float, float, float] | None = None
self.grid_cell_size = 50.0 self.grid_cell_size = 50.0
self.metrics = None self.metrics = None
@ -131,13 +133,18 @@ class RoutingWorld:
@classmethod @classmethod
def from_problem(cls, problem: RoutingProblem, *, board: Polygon | None = None, use_dynamic_grid: bool = True) -> RoutingWorld: def from_problem(cls, problem: RoutingProblem, *, board: Polygon | None = None, use_dynamic_grid: bool = True) -> RoutingWorld:
outline = box(*problem.bounds) if board is None else board
world = cls( world = cls(
clearance=problem.clearance, clearance=problem.clearance,
safety_zone_radius=problem.safety_zone_radius, safety_zone_radius=problem.safety_zone_radius,
board=box(*problem.bounds) if board is None else board, board=outline,
self_clearance=problem.self_clearance, self_clearance=problem.self_clearance,
use_dynamic_grid=use_dynamic_grid, use_dynamic_grid=use_dynamic_grid,
) )
if board is None:
xmin, ymin, xmax, ymax = outline.bounds
if xmin < xmax and ymin < ymax:
world._board_bounds = (xmin, ymin, xmax, ymax)
for obstacle in problem.static_obstacles: for obstacle in problem.static_obstacles:
world.add_static_obstacle(obstacle) world.add_static_obstacle(obstacle)
for fixed in problem.fixed_routes: for fixed in problem.fixed_routes:
@ -145,6 +152,18 @@ class RoutingWorld:
world.add_static_obstacle(polygon, owner=fixed.net_id) world.add_static_obstacle(polygon, owner=fixed.net_id)
return world return world
@property
def board(self) -> Polygon | None:
return self._board
def board_covers_component(self, component: ComponentResult) -> bool:
bounds = self._board_bounds
if bounds is not None and component.standard_faces:
# For a rectangular board the physical bounds give exact containment.
left, bottom, right, top = component.total_physical_bounds
return bounds[0] <= left and bounds[1] <= bottom and right <= bounds[2] and top <= bounds[3]
return self._board is None or path_within_board((component,), self._board)
def get_static_version(self) -> int: def get_static_version(self) -> int:
return self._static_obstacles.version return self._static_obstacles.version
@ -717,7 +736,7 @@ class RoutingWorld:
static_collision_count=static_collision_count, static_collision_count=static_collision_count,
self_collision_count=count_path_self_collisions(components, self.self_clearance), self_collision_count=count_path_self_collisions(components, self.self_clearance),
total_length=total_length, total_length=total_length,
out_of_bounds=self.board is not None and not path_within_board(components, self.board), out_of_bounds=self._board is not None and not all(self.board_covers_component(component) for component in components),
) )
def verify_path_details( def verify_path_details(

View file

@ -12,7 +12,7 @@ from shapely.affinity import translate as shapely_translate
from shapely.geometry import Polygon, box from shapely.geometry import Polygon, box
from inire.constants import TOLERANCE_ANGULAR, TOLERANCE_LINEAR from inire.constants import TOLERANCE_ANGULAR, TOLERANCE_LINEAR
from inire.geometry.sbend import sbend_grid_span, sbend_span from inire.geometry.sbend import sbend_angle, sbend_centerline_length, sbend_grid_span, sbend_span
from inire.seeds import Bend90Seed, PathSegmentSeed, SBendSeed, StraightSeed from inire.seeds import Bend90Seed, PathSegmentSeed, SBendSeed, StraightSeed
from .primitives import Port, rotate_cardinal, rotation_matrix2 from .primitives import Port, rotate_cardinal, rotation_matrix2
@ -141,8 +141,10 @@ class ComponentResult:
def __post_init__(self) -> None: def __post_init__(self) -> None:
collision_geometry = tuple(self.collision_geometry) collision_geometry = tuple(self.collision_geometry)
physical_geometry = tuple(self.physical_geometry) physical_geometry = collision_geometry if self.physical_geometry is self.collision_geometry else tuple(self.physical_geometry)
dilated_collision_geometry = tuple(self.dilated_collision_geometry) dilated_collision_geometry = (
collision_geometry if self.dilated_collision_geometry is self.collision_geometry else tuple(self.dilated_collision_geometry)
)
dilated_physical_geometry = tuple(self.dilated_physical_geometry) dilated_physical_geometry = tuple(self.dilated_physical_geometry)
object.__setattr__(self, "collision_geometry", collision_geometry) object.__setattr__(self, "collision_geometry", collision_geometry)
@ -152,12 +154,19 @@ class ComponentResult:
object.__setattr__(self, "length", float(self.length)) object.__setattr__(self, "length", float(self.length))
bounds = tuple(poly.bounds for poly in collision_geometry) bounds = tuple(poly.bounds for poly in collision_geometry)
total_bounds = _combine_bounds(bounds)
object.__setattr__(self, "_bounds", bounds) object.__setattr__(self, "_bounds", bounds)
object.__setattr__(self, "_total_bounds", _combine_bounds(list(bounds))) object.__setattr__(self, "_total_bounds", total_bounds)
if physical_geometry is collision_geometry:
object.__setattr__(self, "_total_physical_bounds", total_bounds)
dilated_bounds = tuple(poly.bounds for poly in dilated_collision_geometry) if dilated_collision_geometry is collision_geometry:
dilated_bounds, total_dilated_bounds = bounds, total_bounds
else:
dilated_bounds = tuple(poly.bounds for poly in dilated_collision_geometry)
total_dilated_bounds = _combine_bounds(dilated_bounds)
object.__setattr__(self, "_dilated_bounds", dilated_bounds) object.__setattr__(self, "_dilated_bounds", dilated_bounds)
object.__setattr__(self, "_total_dilated_bounds", _combine_bounds(list(dilated_bounds))) object.__setattr__(self, "_total_dilated_bounds", total_dilated_bounds)
@property @property
def bounds(self) -> tuple[tuple[float, float, float, float], ...]: def bounds(self) -> tuple[tuple[float, float, float, float], ...]:
@ -230,15 +239,21 @@ class ComponentResult:
def translate_geometry(polygons: Sequence[Polygon]) -> tuple[Polygon, ...]: def translate_geometry(polygons: Sequence[Polygon]) -> tuple[Polygon, ...]:
return tuple(translated[id(polygon)] for polygon in polygons) return tuple(translated[id(polygon)] for polygon in polygons)
collision_geometry = translate_geometry(self.collision_geometry)
result = ComponentResult( result = ComponentResult(
start_port=self.start_port.translate(dx, dy), start_port=self.start_port.translate(dx, dy),
collision_geometry=translate_geometry(self.collision_geometry), collision_geometry=collision_geometry,
end_port=self.end_port.translate(dx, dy), end_port=self.end_port.translate(dx, dy),
length=self.length, length=self.length,
move_type=self.move_type, move_type=self.move_type,
move_spec=self.move_spec, move_spec=self.move_spec,
physical_geometry=translate_geometry(self.physical_geometry), physical_geometry=(
dilated_collision_geometry=translate_geometry(self.dilated_collision_geometry), collision_geometry if self.physical_geometry is self.collision_geometry else translate_geometry(self.physical_geometry)
),
dilated_collision_geometry=(
collision_geometry if self.dilated_collision_geometry is self.collision_geometry
else translate_geometry(self.dilated_collision_geometry)
),
dilated_physical_geometry=translate_geometry(self.dilated_physical_geometry), dilated_physical_geometry=translate_geometry(self.dilated_physical_geometry),
width=self.width, width=self.width,
standard_faces=self.standard_faces, standard_faces=self.standard_faces,
@ -247,7 +262,7 @@ class ComponentResult:
return result return result
def _combine_bounds(bounds_list: list[tuple[float, float, float, float]]) -> tuple[float, float, float, float]: def _combine_bounds(bounds_list: Sequence[tuple[float, float, float, float]]) -> tuple[float, float, float, float]:
if len(bounds_list) == 1: if len(bounds_list) == 1:
return bounds_list[0] return bounds_list[0]
arr = numpy.asarray(bounds_list, dtype=numpy.float64) arr = numpy.asarray(bounds_list, dtype=numpy.float64)
@ -285,10 +300,6 @@ def _arc_samples(
return physical, dilated return physical, dilated
def _sbend_angle(offset: float, radius: float) -> float:
return 2 * asin(sqrt(abs(offset) / radius) / 2)
def prepare_component_sampling( def prepare_component_sampling(
kind: MoveKind, parameters: tuple, width: float, *, kind: MoveKind, parameters: tuple, width: float, *,
collision_type: BendCollisionModel, physical_geometry_type: BendPhysicalGeometry, collision_type: BendCollisionModel, physical_geometry_type: BendPhysicalGeometry,
@ -302,7 +313,7 @@ def prepare_component_sampling(
return _arc_samples(radius, width, 90, sagitta, dilation, sample_dilation=sample_dilation) return _arc_samples(radius, width, 90, sagitta, dilation, sample_dilation=sample_dilation)
if kind == "sbend" and sbend_outline is None: if kind == "sbend" and sbend_outline is None:
offset, radius = parameters offset, radius = parameters
angle = float(numpy.degrees(_sbend_angle(offset, radius))) angle = float(numpy.degrees(sbend_angle(offset, radius)))
return _arc_samples(radius, width, angle, sagitta, dilation, sample_dilation=True) return _arc_samples(radius, width, angle, sagitta, dilation, sample_dilation=True)
return 0, None return 0, None
@ -612,7 +623,7 @@ class SBend:
seed = SBendSeed(offset, radius) seed = SBendSeed(offset, radius)
offset, radius = seed.offset, seed.radius offset, radius = seed.offset, seed.radius
sign = 1 if offset >= 0 else -1 sign = 1 if offset >= 0 else -1
theta = _sbend_angle(offset, radius) theta = sbend_angle(offset, radius)
end_port = component_endpoint(start_port, "sbend", (offset, radius)) end_port = component_endpoint(start_port, "sbend", (offset, radius))
dx = sbend_span(offset, radius) dx = sbend_span(offset, radius)
grid_span = sbend_grid_span(offset, radius) grid_span = sbend_grid_span(offset, radius)
@ -663,7 +674,7 @@ class SBend:
start_port=start_port, start_port=start_port,
collision_geometry=geometry, collision_geometry=geometry,
end_port=end_port, end_port=end_port,
length=2.0 * radius * theta + max(0.0, tail), length=sbend_centerline_length(radius, theta, tail),
move_type="sbend", move_type="sbend",
move_spec=seed, move_spec=seed,
physical_geometry=physical_geometry, physical_geometry=physical_geometry,

View file

@ -1,6 +1,6 @@
"""S-bend dimensions on the routing grid.""" """S-bend dimensions on the routing grid."""
from math import ceil, isfinite, isqrt, sqrt from math import asin, ceil, isfinite, isqrt, sqrt
from inire.constants import TOLERANCE_LINEAR from inire.constants import TOLERANCE_LINEAR
@ -23,6 +23,14 @@ def sbend_grid_span(offset: float, radius: float) -> int:
return ceil(sbend_span(offset, radius) - TOLERANCE_LINEAR) return ceil(sbend_span(offset, radius) - TOLERANCE_LINEAR)
def sbend_angle(offset: float, radius: float) -> float:
return 2 * asin(sqrt(abs(offset) / radius) / 2)
def sbend_centerline_length(radius: float, angle: float, tangent: float) -> float:
return 2.0 * radius * angle + max(0.0, tangent)
def exact_sbend_spans( def exact_sbend_spans(
radius: int, *, max_span: int, max_offset: int | None = None, offsets: tuple[float, ...] | None = None, radius: int, *, max_span: int, max_offset: int | None = None, offsets: tuple[float, ...] | None = None,
) -> dict[int, int]: ) -> dict[int, int]:

View file

@ -7,7 +7,7 @@ from shapely.geometry import Polygon
from inire.constants import TOLERANCE_LINEAR from inire.constants import TOLERANCE_LINEAR
from inire.geometry.component_overlap import components_overlap from inire.geometry.component_overlap import components_overlap
from inire.geometry.components import build_component, check_component_endpoint, path_within_board, uses_custom_outline from inire.geometry.components import build_component, check_component_endpoint, uses_custom_outline
from inire.geometry.primitives import Port from inire.geometry.primitives import Port
from inire.router.refiner import component_hits_ancestor_chain from inire.router.refiner import component_hits_ancestor_chain
@ -160,7 +160,7 @@ def add_node(
ce = context.cost_evaluator.collision_engine ce = context.cost_evaluator.collision_engine
danger = context.cost_evaluator.danger_map danger = context.cost_evaluator.danger_map
if ce.board is not None: if ce.board is not None:
outside_board = not path_within_board((result,), ce.board) outside_board = not ce.board_covers_component(result)
elif danger is not None and not result.standard_faces: elif danger is not None and not result.standard_faces:
outside_board = not danger.covers_bounds(result.total_physical_bounds) outside_board = not danger.covers_bounds(result.total_physical_bounds)
else: else:

View file

@ -11,6 +11,7 @@ from inire.geometry.collision import RoutingWorld
from inire.geometry.components import Bend90, ComponentResult from inire.geometry.components import Bend90, ComponentResult
from inire.geometry.components import Straight from inire.geometry.components import Straight
from inire.geometry.primitives import Port from inire.geometry.primitives import Port
from inire.model import RoutingProblem
from inire.router._astar_types import AStarMetrics from inire.router._astar_types import AStarMetrics
from inire.seeds import StraightSeed from inire.seeds import StraightSeed
@ -299,6 +300,19 @@ def test_verify_geometry_checks_the_board_using_the_polygon() -> None:
assert report.out_of_bounds assert report.out_of_bounds
def test_rectangular_board_includes_its_edges_and_custom_board_keeps_its_hole() -> None:
problem = RoutingProblem(bounds=(0, 0, 50, 50))
rectangle = RoutingWorld.from_problem(problem)
board_with_hole = Polygon(box(*problem.bounds).exterior.coords, holes=[box(20, 1, 30, 3).exterior.coords])
custom = RoutingWorld.from_problem(problem, board=board_with_hole)
touching = Straight.generate(Port(0, 2, 0), 50, width=4, dilation=problem.clearance / 2)
crossing = Straight.generate(Port(0, 1, 0), 50, width=4)
assert rectangle.verify_path_geometry((touching,)).is_valid
assert rectangle.verify_path_geometry((crossing,)).out_of_bounds
assert custom.verify_path_geometry((touching,)).out_of_bounds
def test_self_collision_check_only_visits_spatial_candidates(monkeypatch) -> None: def test_self_collision_check_only_visits_spatial_candidates(monkeypatch) -> None:
path = tuple(Straight.generate(Port(x, 0, 0), 5, 2) for x in range(0, 500, 5)) path = tuple(Straight.generate(Port(x, 0, 0), 5, 2) for x in range(0, 500, 5))
original_check = overlap_module.components_overlap original_check = overlap_module.components_overlap