diff --git a/inire/geometry/collision.py b/inire/geometry/collision.py index 5b0c8b7..5639e5b 100644 --- a/inire/geometry/collision.py +++ b/inire/geometry/collision.py @@ -103,7 +103,8 @@ class RoutingWorld: "clearance", "self_clearance", "safety_zone_radius", - "board", + "_board", + "_board_bounds", "grid_cell_size", "metrics", "_dynamic_paths", @@ -122,7 +123,8 @@ class RoutingWorld: self.clearance = clearance self.self_clearance = self_clearance 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.metrics = None @@ -131,13 +133,18 @@ class RoutingWorld: @classmethod 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( clearance=problem.clearance, safety_zone_radius=problem.safety_zone_radius, - board=box(*problem.bounds) if board is None else board, + board=outline, self_clearance=problem.self_clearance, 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: world.add_static_obstacle(obstacle) for fixed in problem.fixed_routes: @@ -145,6 +152,18 @@ class RoutingWorld: world.add_static_obstacle(polygon, owner=fixed.net_id) 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: return self._static_obstacles.version @@ -717,7 +736,7 @@ class RoutingWorld: static_collision_count=static_collision_count, self_collision_count=count_path_self_collisions(components, self.self_clearance), 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( diff --git a/inire/geometry/components.py b/inire/geometry/components.py index cf0c42b..662643b 100644 --- a/inire/geometry/components.py +++ b/inire/geometry/components.py @@ -12,7 +12,7 @@ from shapely.affinity import translate as shapely_translate from shapely.geometry import Polygon, box 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 .primitives import Port, rotate_cardinal, rotation_matrix2 @@ -141,8 +141,10 @@ class ComponentResult: def __post_init__(self) -> None: collision_geometry = tuple(self.collision_geometry) - physical_geometry = tuple(self.physical_geometry) - dilated_collision_geometry = tuple(self.dilated_collision_geometry) + physical_geometry = collision_geometry if self.physical_geometry is self.collision_geometry else tuple(self.physical_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) object.__setattr__(self, "collision_geometry", collision_geometry) @@ -152,12 +154,19 @@ class ComponentResult: object.__setattr__(self, "length", float(self.length)) bounds = tuple(poly.bounds for poly in collision_geometry) + total_bounds = _combine_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, "_total_dilated_bounds", _combine_bounds(list(dilated_bounds))) + object.__setattr__(self, "_total_dilated_bounds", total_dilated_bounds) @property def bounds(self) -> tuple[tuple[float, float, float, float], ...]: @@ -230,15 +239,21 @@ class ComponentResult: def translate_geometry(polygons: Sequence[Polygon]) -> tuple[Polygon, ...]: return tuple(translated[id(polygon)] for polygon in polygons) + collision_geometry = translate_geometry(self.collision_geometry) result = ComponentResult( 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), length=self.length, move_type=self.move_type, move_spec=self.move_spec, - physical_geometry=translate_geometry(self.physical_geometry), - dilated_collision_geometry=translate_geometry(self.dilated_collision_geometry), + physical_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), width=self.width, standard_faces=self.standard_faces, @@ -247,7 +262,7 @@ class ComponentResult: 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: return bounds_list[0] arr = numpy.asarray(bounds_list, dtype=numpy.float64) @@ -285,10 +300,6 @@ def _arc_samples( return physical, dilated -def _sbend_angle(offset: float, radius: float) -> float: - return 2 * asin(sqrt(abs(offset) / radius) / 2) - - def prepare_component_sampling( kind: MoveKind, parameters: tuple, width: float, *, 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) if kind == "sbend" and sbend_outline is None: 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 0, None @@ -612,7 +623,7 @@ class SBend: seed = SBendSeed(offset, radius) offset, radius = seed.offset, seed.radius 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)) dx = sbend_span(offset, radius) grid_span = sbend_grid_span(offset, radius) @@ -663,7 +674,7 @@ class SBend: start_port=start_port, collision_geometry=geometry, end_port=end_port, - length=2.0 * radius * theta + max(0.0, tail), + length=sbend_centerline_length(radius, theta, tail), move_type="sbend", move_spec=seed, physical_geometry=physical_geometry, diff --git a/inire/geometry/sbend.py b/inire/geometry/sbend.py index a00630d..a204fb1 100644 --- a/inire/geometry/sbend.py +++ b/inire/geometry/sbend.py @@ -1,6 +1,6 @@ """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 @@ -23,6 +23,14 @@ def sbend_grid_span(offset: float, radius: float) -> int: 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( radius: int, *, max_span: int, max_offset: int | None = None, offsets: tuple[float, ...] | None = None, ) -> dict[int, int]: diff --git a/inire/router/_astar_admission.py b/inire/router/_astar_admission.py index d893af9..463f6bd 100644 --- a/inire/router/_astar_admission.py +++ b/inire/router/_astar_admission.py @@ -7,7 +7,7 @@ from shapely.geometry import Polygon from inire.constants import TOLERANCE_LINEAR 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.router.refiner import component_hits_ancestor_chain @@ -160,7 +160,7 @@ def add_node( ce = context.cost_evaluator.collision_engine danger = context.cost_evaluator.danger_map 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: outside_board = not danger.covers_bounds(result.total_physical_bounds) else: diff --git a/inire/tests/test_collision.py b/inire/tests/test_collision.py index dda2c33..6da3177 100644 --- a/inire/tests/test_collision.py +++ b/inire/tests/test_collision.py @@ -11,6 +11,7 @@ from inire.geometry.collision import RoutingWorld from inire.geometry.components import Bend90, ComponentResult from inire.geometry.components import Straight from inire.geometry.primitives import Port +from inire.model import RoutingProblem from inire.router._astar_types import AStarMetrics 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 +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: path = tuple(Straight.generate(Port(x, 0, 0), 5, 2) for x in range(0, 500, 5)) original_check = overlap_module.components_overlap