Use the same physical clearance rule for fixed-route admission and verification. Preserve imported vertices while clearing GEOS precision metadata. Count touching terminal faces as conflicts at zero clearance.
672 lines
27 KiB
Python
672 lines
27 KiB
Python
from dataclasses import replace
|
|
|
|
import numpy
|
|
import pytest
|
|
from shapely import get_precision, set_precision
|
|
from shapely.geometry import Polygon, box
|
|
|
|
import inire.geometry.collision as collision_module
|
|
import inire.geometry.component_overlap as overlap_module
|
|
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.router._astar_types import AStarMetrics
|
|
from inire.seeds import StraightSeed
|
|
|
|
|
|
def _install_static_straight(
|
|
engine: RoutingWorld,
|
|
start: Port,
|
|
length: float,
|
|
*,
|
|
width: float,
|
|
dilation: float = 0.0,
|
|
) -> None:
|
|
obstacle = Straight.generate(start, length, width=width, dilation=dilation)
|
|
for polygon in obstacle.physical_geometry:
|
|
engine.add_static_obstacle(polygon)
|
|
|
|
|
|
def test_verify_path_rejects_invalid_polygon_before_clearance_checks() -> None:
|
|
world = RoutingWorld(clearance=2)
|
|
world.add_static_obstacle(box(2, -2, 8, 2))
|
|
straight = Straight.generate(Port(0, 0, 0), 10, width=2)
|
|
self_intersecting = Polygon([(0, -1), (10, 1), (0, 1), (10, -1)])
|
|
component = replace(straight, physical_geometry=(self_intersecting,))
|
|
|
|
world.install_path("invalid", (component,))
|
|
report = world.verify_path_report("invalid", (component,))
|
|
|
|
assert not world.has_dynamic_paths()
|
|
assert report.invalid_geometry
|
|
assert not report.is_valid
|
|
|
|
|
|
def test_collision_detection() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
_install_static_straight(engine, Port(10, 15, 0), 10.0, width=10.0, dilation=1.0)
|
|
|
|
direct_hit = Straight.generate(Port(12, 12.5, 0), 1.0, width=1.0, dilation=1.0)
|
|
assert engine.check_move_static(direct_hit, start_port=direct_hit.start_port)
|
|
|
|
far_away = Straight.generate(Port(0, 2.5, 0), 5.0, width=5.0, dilation=1.0)
|
|
assert not engine.check_move_static(far_away, start_port=far_away.start_port)
|
|
|
|
near_hit = Straight.generate(Port(8, 12.5, 0), 1.0, width=5.0, dilation=1.0)
|
|
assert engine.check_move_static(near_hit, start_port=near_hit.start_port)
|
|
|
|
|
|
def test_safety_zone() -> None:
|
|
engine = RoutingWorld(clearance=0.0)
|
|
_install_static_straight(engine, Port(10, 15, 0), 10.0, width=10.0)
|
|
|
|
start_port = Port(10, 12, 0)
|
|
test_move = Straight.generate(start_port, 0.002, width=0.001)
|
|
assert not engine.check_move_static(test_move, start_port=start_port)
|
|
|
|
|
|
def test_ray_cast_width_clearance() -> None:
|
|
# Clearance = 2.0um, Width = 2.0um.
|
|
# Centerline to obstacle edge must be >= W/2 + C = 1.0 + 2.0 = 3.0um.
|
|
engine = RoutingWorld(clearance=2.0)
|
|
|
|
# Obstacle at x=10 to 20
|
|
_install_static_straight(engine, Port(10, 50, 0), 10.0, width=100.0)
|
|
|
|
# 1. Parallel move at x=6. Gap = 10 - 6 = 4.0. Clearly OK.
|
|
start_ok = Port(6, 50, 90)
|
|
reach_ok = engine.ray_cast(start_ok, 90, max_dist=10.0, net_width=2.0)
|
|
assert reach_ok >= 10.0
|
|
|
|
# 2. Parallel move at x=8. Gap = 10 - 8 = 2.0. COLLISION.
|
|
start_fail = Port(8, 50, 90)
|
|
reach_fail = engine.ray_cast(start_fail, 90, max_dist=10.0, net_width=2.0)
|
|
assert reach_fail < 10.0
|
|
|
|
|
|
def test_ray_cast_reuses_exact_queries_and_separates_parameters() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
engine.add_static_obstacle(box(10, -1, 20, 1))
|
|
origin = Port(0, 0, 0)
|
|
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(7.0)
|
|
assert engine.metrics.total_ray_cast_candidate_bounds == 1
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0, caller="expand_forward") == pytest.approx(7.0)
|
|
assert engine.metrics.total_ray_cast_calls == 2
|
|
assert engine.metrics.total_ray_cast_calls_expand_forward == 1
|
|
assert engine.metrics.total_ray_cast_candidate_bounds == 1
|
|
|
|
assert engine.ray_cast(origin, 0, 30, net_width=6.0) == pytest.approx(5.0)
|
|
assert engine.ray_cast(origin, 0, 30) == pytest.approx(9.0)
|
|
assert engine.ray_cast(origin, 0, 4, net_width=2.0) == 4.0
|
|
assert engine.ray_cast(origin, 90, 30, net_width=2.0) == 30.0
|
|
engine.clearance = 4.0
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(5.0)
|
|
|
|
|
|
def test_ray_cast_discards_results_after_obstacle_changes() -> None:
|
|
engine = RoutingWorld(clearance=0.0)
|
|
origin = Port(0, 0, 0)
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == 30.0
|
|
|
|
first = engine.add_static_obstacle(box(10, -1, 20, 1))
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(9.0)
|
|
second = engine.add_static_obstacle(box(4, -1, 6, 1))
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(3.0)
|
|
engine.remove_static_obstacle(second)
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == pytest.approx(9.0)
|
|
engine.remove_static_obstacle(first)
|
|
assert engine.ray_cast(origin, 0, 30, net_width=2.0) == 30.0
|
|
|
|
|
|
def test_ray_cast_preserves_scalar_precision_at_an_obstacle_boundary() -> None:
|
|
engine = RoutingWorld(clearance=0.0)
|
|
engine.add_static_obstacle(box(0, 10, 10, 20))
|
|
origin = Port(0, 0, 0)
|
|
|
|
assert engine.ray_cast(origin, 90.0, 30.0) == pytest.approx(10.0)
|
|
assert engine.ray_cast(origin, numpy.float32(90), 30.0) == 30.0
|
|
|
|
|
|
def test_ray_cast_cache_has_a_fixed_capacity(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(collision_module, "_RAY_CAST_CACHE_LIMIT", 2)
|
|
engine = RoutingWorld(clearance=0.0)
|
|
engine.add_static_obstacle(box(10, -1, 20, 1))
|
|
origin = Port(0, 0, 0)
|
|
for limit in (30, 40, 50):
|
|
assert engine.ray_cast(origin, 0, limit) == pytest.approx(10.0)
|
|
assert len(engine._static_obstacles.ray_cast_cache) <= 2
|
|
|
|
|
|
def test_static_index_ignores_precision_metadata_on_direct_import() -> None:
|
|
obstacle = set_precision(box(10, 10, 20, 20), 1, mode="pointwise")
|
|
world = RoutingWorld(clearance=.25)
|
|
world.add_static_obstacle(obstacle)
|
|
indexed, = world.iter_static_geometries()
|
|
assert indexed.equals_exact(obstacle, 0)
|
|
assert get_precision(indexed) == 0
|
|
|
|
move = Straight.generate(Port(5, 9, 0), 20, 2)
|
|
assert world.verify_path_report("track", (move,)).static_collision_count == 1
|
|
|
|
|
|
def test_check_move_static_clearance() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
_install_static_straight(engine, Port(10, 50, 0), 10.0, width=100.0, dilation=1.0)
|
|
|
|
# Straight move of length 10 at x=8 (Width 2.0)
|
|
# Gap = 10 - 8 = 2.0 < 3.0. COLLISION.
|
|
start = Port(8, 0, 90)
|
|
res = Straight.generate(start, 10.0, width=2.0, dilation=1.0) # dilation = C/2
|
|
|
|
assert engine.check_move_static(res, start_port=start)
|
|
|
|
# Move at x=7. Gap = 3.0 == minimum. OK.
|
|
start_ok = Port(7, 0, 90)
|
|
res_ok = Straight.generate(start_ok, 10.0, width=2.0, dilation=1.0)
|
|
assert not engine.check_move_static(res_ok, start_port=start_ok)
|
|
|
|
# 3. Same exact-boundary case.
|
|
start_exact = Port(7, 0, 90)
|
|
res_exact = Straight.generate(start_exact, 10.0, width=2.0, dilation=1.0)
|
|
assert not engine.check_move_static(res_exact, start_port=start_exact)
|
|
|
|
|
|
def test_static_bend_query_checks_clearance_beyond_its_tangent_face() -> None:
|
|
engine = RoutingWorld(clearance=2)
|
|
bend = Bend90.generate(Port(20, 20, 0), 10, 2, "CCW", dilation=1)
|
|
obstacle = box(16, 18.6, 18.25, 18.9)
|
|
assert bend.physical_geometry[0].distance(obstacle) < 2
|
|
engine.add_static_obstacle(obstacle)
|
|
|
|
assert engine.check_move_static(bend, start_port=bend.start_port, end_port=bend.end_port)
|
|
|
|
|
|
def test_verify_path_report_uses_physical_distance_at_a_mitred_corner() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
path = (Straight.generate(Port(0, 0, 0), 20.0, width=2.0),)
|
|
outside_radius = box(21.6, 2.6, 22.0, 3.0)
|
|
engine.add_static_obstacle(outside_radius)
|
|
|
|
assert engine.verify_path_report("net", path).is_valid
|
|
|
|
engine.add_static_obstacle(box(21.2, 2.2, 21.8, 2.8))
|
|
|
|
assert engine.verify_path_report("net", path).static_collision_count == 1
|
|
|
|
|
|
def test_verify_path_report_preserves_long_net_id() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
net_id = "net_abcdefghijklmnopqrstuvwxyz_0123456789"
|
|
path = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
geoms = [poly for component in path for poly in component.collision_geometry]
|
|
dilated = [poly for component in path for poly in component.dilated_collision_geometry]
|
|
|
|
engine.add_path(net_id, geoms, dilated_geometry=dilated)
|
|
report = engine.verify_path_report(net_id, path)
|
|
|
|
assert report.dynamic_collision_count == 0
|
|
assert engine.metrics.total_verify_dynamic_candidate_nets == 0
|
|
|
|
|
|
def test_verify_path_report_distinguishes_long_net_ids_with_shared_prefix() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
shared_prefix = "net_shared_prefix_abcdefghijklmnopqrstuvwxyz_"
|
|
net_a = f"{shared_prefix}A"
|
|
net_b = f"{shared_prefix}B"
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
net_a,
|
|
[poly for component in path_a for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_a for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
net_b,
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
report = engine.verify_path_report(net_a, path_a)
|
|
|
|
assert report.dynamic_collision_count == 1
|
|
assert engine.metrics.total_verify_dynamic_candidate_nets == 1
|
|
|
|
|
|
def test_verify_path_report_uses_physical_bounds_before_exact_checks() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_far = [Straight.generate(Port(100, 100, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
"netFar",
|
|
[poly for component in path_far for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_far for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
report = engine.verify_path_report("netA", path_a)
|
|
|
|
assert report.dynamic_collision_count == 1
|
|
assert engine.metrics.total_verify_dynamic_candidate_nets == 1
|
|
assert engine.metrics.total_verify_dynamic_exact_pair_checks == 1
|
|
|
|
|
|
@pytest.mark.parametrize(("clearance", "valid"), [(1.4, True), (1.5, False)])
|
|
def test_verify_path_report_uses_physical_distance_between_routes(clearance, valid) -> None:
|
|
engine = RoutingWorld(clearance=clearance)
|
|
existing = Straight.generate(Port(0, 0, 0), 10, width=2, dilation=clearance / 2)
|
|
query = Straight.generate(Port(12, 2, 90), 10, width=2, dilation=clearance / 2)
|
|
engine.install_path("existing", (existing,))
|
|
|
|
# The physical corners are one unit apart on each axis.
|
|
assert engine.verify_path_report("query", (query,)).is_valid is valid
|
|
engine.remove_path("existing")
|
|
assert engine.verify_path_report("query", (query,)).is_valid
|
|
|
|
|
|
@pytest.mark.parametrize("y", [1, 2, 50])
|
|
@pytest.mark.parametrize(("gap", "valid"), [(0.1, True), (0.099, False), (0, False)])
|
|
def test_verify_path_clearance_threshold_survives_translation(y, gap, valid) -> None:
|
|
engine = RoutingWorld(clearance=0.1)
|
|
width = 1 - gap
|
|
tested = Straight.generate(Port(0, y, 0), 20, width)
|
|
peer = Straight.generate(Port(0, y + 1, 0), 20, width)
|
|
|
|
engine.install_path("peer", (peer,))
|
|
assert engine.verify_path_report("query", (tested,)).is_valid is valid
|
|
engine.remove_path("peer")
|
|
engine.add_static_obstacle(peer.physical_geometry[0])
|
|
assert engine.verify_path_report("query", (tested,)).is_valid is valid
|
|
|
|
|
|
def test_verify_geometry_rejects_a_straight_past_its_arrival_port() -> None:
|
|
component = Straight.generate(Port(10, 25, 0), 40.49, 2)
|
|
report = RoutingWorld(0).verify_path_report("straight", (component,))
|
|
assert component.end_port == Port(50, 25, 0)
|
|
assert report.discontinuous
|
|
|
|
|
|
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
|
|
checks = 0
|
|
|
|
def count_check(*args, **kwargs):
|
|
nonlocal checks
|
|
checks += 1
|
|
return original_check(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(overlap_module, "components_overlap", count_check)
|
|
report = RoutingWorld(0).verify_path_report("straight", path)
|
|
|
|
assert report.is_valid
|
|
assert checks == len(path) - 1
|
|
|
|
|
|
def test_verify_path_details_returns_conflicting_net_ids() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
detail = engine.verify_path_details("netA", path_a)
|
|
|
|
assert detail.report.dynamic_collision_count == 1
|
|
assert detail.conflicting_net_ids == ("netB",)
|
|
|
|
|
|
def test_verify_path_details_reports_component_conflicts() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [
|
|
Straight.generate(Port(100, 0, 0), 10.0, width=2.0, dilation=1.0),
|
|
Straight.generate(Port(0, 0, 0), 10.0, width=2.0, dilation=1.0),
|
|
Straight.generate(Port(10, 0, 0), 10.0, width=2.0, dilation=1.0),
|
|
]
|
|
|
|
engine.install_path("netB", path_b)
|
|
|
|
detail = engine.verify_path_details("netA", path_a, capture_component_conflicts=True)
|
|
|
|
assert detail.conflicting_net_ids == ("netB",)
|
|
assert detail.component_conflicts == ((0, "netB", 1), (0, "netB", 2))
|
|
assert detail.report.dynamic_collision_count == 1
|
|
assert engine.verify_path_report("netA", path_a) == detail.report
|
|
|
|
|
|
def test_verify_path_details_deduplicates_component_conflicts() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
query_component = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
end_port=Port(22, 0, 0),
|
|
length=22.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(22.0),
|
|
physical_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
dilated_collision_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
dilated_physical_geometry=[box(0, 0, 10, 10), box(12, 0, 22, 10)],
|
|
)
|
|
blocker_component = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(5, 0, 17, 10)],
|
|
end_port=Port(17, 0, 0),
|
|
length=12.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(12.0),
|
|
physical_geometry=[box(5, 0, 17, 10)],
|
|
dilated_collision_geometry=[box(5, 0, 17, 10)],
|
|
dilated_physical_geometry=[box(5, 0, 17, 10)],
|
|
)
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
blocker_component.collision_geometry,
|
|
dilated_geometry=blocker_component.dilated_collision_geometry,
|
|
component_indexes=[0],
|
|
)
|
|
|
|
detail = engine.verify_path_details("netA", [query_component], capture_component_conflicts=True)
|
|
|
|
assert detail.conflicting_net_ids == ("netB",)
|
|
assert detail.component_conflicts == ((0, "netB", 0),)
|
|
|
|
|
|
def test_remove_path_clears_dynamic_path() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
path = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
geoms = [poly for component in path for poly in component.collision_geometry]
|
|
dilated = [poly for component in path for poly in component.dilated_collision_geometry]
|
|
|
|
engine.add_path("netA", geoms, dilated_geometry=dilated)
|
|
assert {net_id for net_id, _ in engine._dynamic_paths.geometries.values()} == {"netA"}
|
|
|
|
engine.remove_path("netA")
|
|
assert list(engine._dynamic_paths.geometries.values()) == []
|
|
assert len(engine._static_obstacles.geometries) == 0
|
|
|
|
|
|
def test_dynamic_grid_updates_incrementally_on_add_and_remove() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 4, 0), 20.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netA",
|
|
[poly for component in path_a for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_a for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
dynamic_paths = engine._dynamic_paths
|
|
assert dynamic_paths.net_to_obj_ids["netA"]
|
|
assert dynamic_paths.net_to_obj_ids["netB"]
|
|
assert dynamic_paths.grid_net_obj_ids
|
|
assert engine.metrics.total_dynamic_grid_rebuilds == 0
|
|
|
|
engine.remove_path("netA")
|
|
|
|
assert "netA" not in dynamic_paths.net_to_obj_ids
|
|
assert "netB" in dynamic_paths.net_to_obj_ids
|
|
assert engine.metrics.total_dynamic_grid_rebuilds == 0
|
|
assert "netA" not in dynamic_paths.grid_net_obj_ids.get((0, -1), {})
|
|
assert "netB" in dynamic_paths.grid_net_obj_ids.get((0, 0), {})
|
|
|
|
|
|
@pytest.mark.parametrize("large_install", [True, False])
|
|
def test_large_dynamic_spans_use_tree_for_congestion(large_install) -> None:
|
|
engine = RoutingWorld(clearance=2)
|
|
installed = Straight.generate(Port(0, 0, 0), 100_000 if large_install else 20, width=2, dilation=1)
|
|
query = Straight.generate(Port(10, -10, 90), 20 if large_install else 100_000, width=2, dilation=1)
|
|
engine.install_path("peer", (installed,))
|
|
index = engine._dynamic_paths
|
|
|
|
assert bool(index.grid_net_obj_ids) is (not large_install)
|
|
assert engine.has_possible_move_congestion(query, "query")
|
|
assert engine.has_candidate_move_congestion(query, "query")
|
|
assert engine.check_move_congestion(query, "query") == 1
|
|
assert engine.verify_path_report("query", (query,)).dynamic_collision_count == 1
|
|
assert engine.check_move_congestion(query, "peer") == 0
|
|
|
|
engine.remove_path("peer")
|
|
|
|
assert not engine.has_possible_move_congestion(query, "query")
|
|
assert engine.check_move_congestion(query, "query") == 0
|
|
assert not index.grid_net_obj_ids
|
|
|
|
|
|
def test_dynamic_net_envelopes_update_incrementally_on_add_and_remove() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
path_a = [Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)]
|
|
path_b = [Straight.generate(Port(0, 40, 0), 10.0, width=2.0, dilation=1.0)]
|
|
|
|
engine.add_path(
|
|
"netA",
|
|
[poly for component in path_a for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_a for poly in component.dilated_collision_geometry],
|
|
)
|
|
engine.add_path(
|
|
"netB",
|
|
[poly for component in path_b for poly in component.collision_geometry],
|
|
dilated_geometry=[poly for component in path_b for poly in component.dilated_collision_geometry],
|
|
)
|
|
|
|
dynamic_paths = engine._dynamic_paths
|
|
assert set(dynamic_paths.net_envelopes) == {"netA", "netB"}
|
|
assert dynamic_paths.net_envelopes["netA"] == (-1.0, -2.0, 21.0, 2.0)
|
|
assert dynamic_paths.net_envelopes["netB"] == (-1.0, 38.0, 11.0, 42.0)
|
|
assert engine.metrics.total_dynamic_tree_rebuilds == 0
|
|
|
|
net_b_envelope_obj_id = dynamic_paths.net_envelope_obj_ids["netB"]
|
|
assert list(dynamic_paths.net_envelope_index.intersection((-5.0, 35.0, 15.0, 45.0))) == [net_b_envelope_obj_id]
|
|
|
|
engine.remove_path("netA")
|
|
|
|
assert "netA" not in dynamic_paths.net_envelopes
|
|
assert "netA" not in dynamic_paths.net_envelope_obj_ids
|
|
assert "netB" in dynamic_paths.net_envelopes
|
|
assert engine.metrics.total_dynamic_tree_rebuilds == 0
|
|
|
|
|
|
|
|
def test_congestion_query_uses_per_polygon_bounds() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
|
|
blocker = Straight.generate(Port(40, 4, 90), 2.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
|
|
move = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
end_port=Port(100, 0, 0),
|
|
length=100.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(100.0),
|
|
physical_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
dilated_collision_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
dilated_physical_geometry=[box(0, 0, 10, 10), box(90, 0, 100, 10)],
|
|
)
|
|
|
|
assert engine.check_move_congestion(move, "netA") == 0
|
|
assert engine.metrics.total_congestion_candidate_nets == 0
|
|
assert engine.metrics.total_congestion_candidate_ids == 0
|
|
assert engine.metrics.total_congestion_exact_pair_checks == 0
|
|
|
|
|
|
def test_congestion_touching_geometries_do_not_count_as_overlap() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
|
|
existing = Straight.generate(Port(0, 0, 0), 10.0, width=2.0, dilation=1.0)
|
|
touching = Straight.generate(Port(12, 0, 0), 10.0, width=2.0, dilation=1.0)
|
|
|
|
engine.add_path(
|
|
"netB",
|
|
list(existing.collision_geometry),
|
|
dilated_geometry=list(existing.dilated_collision_geometry),
|
|
)
|
|
|
|
assert engine.check_move_congestion(touching, "netA") == 0
|
|
|
|
|
|
def test_congestion_exact_checks_only_touch_relevant_move_polygons() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
|
|
blocker = Straight.generate(Port(0, 0, 0), 10.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
|
|
move = ComponentResult(
|
|
start_port=Port(0, 0, 0),
|
|
collision_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
end_port=Port(100, 0, 0),
|
|
length=100.0,
|
|
move_type="straight",
|
|
move_spec=StraightSeed(100.0),
|
|
physical_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
dilated_collision_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
dilated_physical_geometry=[box(0, -2, 10, 2), box(90, -2, 100, 2)],
|
|
)
|
|
|
|
assert engine.check_move_congestion(move, "netA") == 1
|
|
assert engine.metrics.total_congestion_candidate_nets == 1
|
|
assert engine.metrics.total_congestion_candidate_ids == 1
|
|
assert engine.metrics.total_congestion_exact_pair_checks == 1
|
|
|
|
def test_congestion_grid_span_cache_reuses_broad_phase_candidates() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
cache: dict[tuple[str, int, int, int, int], dict[str, tuple[int, ...]]] = {}
|
|
|
|
blocker = Straight.generate(Port(15, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
|
|
move_a = Straight.generate(Port(5, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
move_b = Straight.generate(Port(7, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert engine.check_move_congestion(
|
|
move_a,
|
|
"netA",
|
|
net_envelope_cache=net_envelope_cache,
|
|
grid_net_cache=grid_net_cache,
|
|
broad_phase_cache=cache,
|
|
) == 1
|
|
assert engine.check_move_congestion(
|
|
move_b,
|
|
"netA",
|
|
net_envelope_cache=net_envelope_cache,
|
|
grid_net_cache=grid_net_cache,
|
|
broad_phase_cache=cache,
|
|
) == 1
|
|
assert engine.metrics.total_congestion_candidate_nets == 2
|
|
assert engine.metrics.total_congestion_net_envelope_cache_misses == 1
|
|
assert engine.metrics.total_congestion_net_envelope_cache_hits == 1
|
|
assert engine.metrics.total_congestion_grid_net_cache_misses == 1
|
|
assert engine.metrics.total_congestion_grid_net_cache_hits == 1
|
|
assert engine.metrics.total_congestion_grid_span_cache_misses == 1
|
|
assert engine.metrics.total_congestion_grid_span_cache_hits == 1
|
|
|
|
|
|
def test_has_possible_move_congestion_uses_presence_cache_and_skips_empty_spans() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
presence_cache: dict[tuple[str, int, int, int, int], bool] = {}
|
|
|
|
blocker = Straight.generate(Port(10, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
empty_move = Straight.generate(Port(200, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert not engine.has_possible_move_congestion(empty_move, "netA", presence_cache)
|
|
assert not engine.has_possible_move_congestion(empty_move, "netA", presence_cache)
|
|
assert engine.metrics.total_congestion_presence_cache_misses == 1
|
|
assert engine.metrics.total_congestion_presence_cache_hits == 1
|
|
|
|
occupied_move = Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert engine.has_possible_move_congestion(occupied_move, "netA")
|
|
|
|
|
|
def test_has_candidate_move_congestion_uses_candidate_precheck_cache() -> None:
|
|
engine = RoutingWorld(clearance=2.0)
|
|
engine.metrics = AStarMetrics()
|
|
candidate_precheck_cache: dict[tuple[str, int, int, int, int], bool] = {}
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] = {}
|
|
|
|
blocker = Straight.generate(Port(10, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
engine.add_path(
|
|
"netB",
|
|
list(blocker.collision_geometry),
|
|
dilated_geometry=list(blocker.dilated_collision_geometry),
|
|
)
|
|
empty_move = Straight.generate(Port(200, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
occupied_move = Straight.generate(Port(0, 0, 0), 20.0, width=2.0, dilation=1.0)
|
|
|
|
assert not engine.has_candidate_move_congestion(
|
|
empty_move,
|
|
"netA",
|
|
candidate_precheck_cache,
|
|
net_envelope_cache,
|
|
grid_net_cache,
|
|
)
|
|
assert not engine.has_candidate_move_congestion(
|
|
empty_move,
|
|
"netA",
|
|
candidate_precheck_cache,
|
|
net_envelope_cache,
|
|
grid_net_cache,
|
|
)
|
|
assert engine.has_candidate_move_congestion(
|
|
occupied_move,
|
|
"netA",
|
|
candidate_precheck_cache,
|
|
net_envelope_cache,
|
|
grid_net_cache,
|
|
)
|
|
assert engine.metrics.total_congestion_candidate_precheck_misses >= 2
|
|
assert engine.metrics.total_congestion_candidate_precheck_hits >= 1
|