Verify physical route clearance and board containment

This commit is contained in:
Jan Petykiewicz 2026-11-27 14:12:40 -08:00
commit c200d0f7c1

View file

@ -7,6 +7,7 @@ import numpy
from shapely.geometry import LineString, box
from inire.geometry.component_overlap import components_overlap
from inire.geometry.components import path_within_board
from inire.geometry.dynamic_path_index import DynamicPathIndex
from inire.geometry.index_helpers import grid_cell_span
from inire.results import RoutingReport
@ -84,6 +85,7 @@ class RoutingWorld:
__slots__ = (
"clearance",
"safety_zone_radius",
"board",
"grid_cell_size",
"metrics",
"_dynamic_paths",
@ -94,9 +96,12 @@ class RoutingWorld:
self,
clearance: float,
safety_zone_radius: float = 0.0021,
*,
board: Polygon | None = None,
) -> None:
self.clearance = clearance
self.safety_zone_radius = safety_zone_radius
self.board = board
self.grid_cell_size = 50.0
self.metrics = None
@ -148,14 +153,27 @@ class RoutingWorld:
geometry: Sequence[Polygon],
dilated_geometry: Sequence[Polygon],
component_indexes: Sequence[int] | None = None,
*,
physical_geometry: Sequence[tuple[int, Polygon]] | None = None,
) -> None:
self._dynamic_paths.add_path(
net_id,
geometry,
dilated_geometry=dilated_geometry,
component_indexes=component_indexes,
physical_geometry=physical_geometry,
)
def install_path(self, net_id: str, path: Sequence[ComponentResult]) -> None:
proxies: list[Polygon] = []
dilated_proxies: list[Polygon] = []
physical_geometry: list[tuple[int, Polygon]] = []
for component_index, component in enumerate(path):
proxies.extend(component.collision_geometry)
dilated_proxies.extend(component.dilated_collision_geometry)
physical_geometry.extend((component_index, polygon) for polygon in component.physical_geometry)
self.add_path(net_id, proxies, dilated_proxies, physical_geometry=physical_geometry)
def remove_path(self, net_id: str) -> None:
self._dynamic_paths.remove_path(net_id)
@ -578,37 +596,6 @@ class RoutingWorld:
if _bounds_overlap(bounds, dynamic_paths.net_envelopes[other_net_id])
)
def _get_verify_net_envelope_candidates(
self,
bounds: tuple[float, float, float, float],
net_id: str,
) -> tuple[str, ...]:
dynamic_paths = self._dynamic_paths
candidate_net_ids: list[str] = []
for obj_id in dynamic_paths.net_envelope_index.intersection(bounds):
other_net_id = dynamic_paths.net_envelope_obj_to_net[obj_id]
if other_net_id == net_id:
continue
if not _bounds_overlap(bounds, dynamic_paths.net_envelopes[other_net_id]):
continue
candidate_net_ids.append(other_net_id)
if self.metrics is not None:
self.metrics.total_verify_dynamic_candidate_nets += len(candidate_net_ids)
return tuple(candidate_net_ids)
def _get_verify_grid_span_obj_ids(
self,
bounds: tuple[float, float, float, float],
other_net_id: str,
) -> tuple[int, ...]:
dynamic_paths = self._dynamic_paths
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
obj_ids: set[int] = set()
for gx in range(gx_min, gx_max + 1):
for gy in range(gy_min, gy_max + 1):
obj_ids.update(dynamic_paths.grid_net_obj_ids.get((gx, gy), {}).get(other_net_id, ()))
return tuple(sorted(obj_ids))
def check_move_congestion(
self,
result: ComponentResult,
@ -698,34 +685,30 @@ class RoutingWorld:
if not self._is_in_safety_zone(polygon, obj_id, None, None):
static_collision_count += 1
if dynamic_paths.dilated:
if dynamic_paths.has_physical_paths():
clearance = max(0.0, self.clearance)
for component_index, component in enumerate(components):
test_geometries = component.dilated_physical_geometry
component_hits = []
for new_geometry in test_geometries:
for hit_net_id in self._get_verify_net_envelope_candidates(new_geometry.bounds, str(net_id)):
for obj_id in self._get_verify_grid_span_obj_ids(new_geometry.bounds, hit_net_id):
if not _bounds_overlap(new_geometry.bounds, dynamic_paths.dilated_bounds[obj_id]):
continue
if self.metrics is not None:
self.metrics.total_verify_dynamic_exact_pair_checks += 1
tree_geometry = dynamic_paths.dilated[obj_id]
if _has_non_touching_overlap(new_geometry, tree_geometry):
component_hits.append(hit_net_id)
if capture_component_conflicts:
component_conflicts.add(
(
component_index,
hit_net_id,
dynamic_paths.component_indexes[obj_id],
)
)
break
if component_hits:
unique_hits = tuple(sorted(set(component_hits)))
dynamic_collision_count += len(unique_hits)
conflicting_net_ids.update(unique_hits)
component_hits: set[str] = set()
for polygon in component.physical_geometry:
minx, miny, maxx, maxy = polygon.bounds
bounds = (minx - clearance, miny - clearance, maxx + clearance, maxy + clearance)
candidates = dynamic_paths.physical_candidates(str(net_id), bounds)
if self.metrics is not None:
self.metrics.total_verify_dynamic_candidate_nets += len({candidate[0] for candidate in candidates})
polygon_hits: set[str] = set()
for hit_net_id, hit_component_index, other in candidates:
if hit_net_id in polygon_hits:
continue
if self.metrics is not None:
self.metrics.total_verify_dynamic_exact_pair_checks += 1
overlaps = polygon.distance(other) < clearance if clearance > 0 else _has_non_touching_overlap(polygon, other)
if overlaps:
polygon_hits.add(hit_net_id)
if capture_component_conflicts:
component_conflicts.add((component_index, hit_net_id, hit_component_index))
component_hits.update(polygon_hits)
dynamic_collision_count += len(component_hits)
conflicting_net_ids.update(component_hits)
for index, component in enumerate(components):
for other_index in range(index + 2, len(components)):
@ -738,6 +721,7 @@ class RoutingWorld:
dynamic_collision_count=dynamic_collision_count,
self_collision_count=self_collision_count,
total_length=total_length,
out_of_bounds=self.board is not None and not path_within_board(components, self.board),
),
conflicting_net_ids=tuple(sorted(conflicting_net_ids)),
component_conflicts=tuple(sorted(component_conflicts)),