Fix clearance checks and document the Terminal-Bench routing flow

This commit is contained in:
Jan Petykiewicz 2026-11-27 19:12:09 -08:00
commit 365c14c59a

View file

@ -6,7 +6,8 @@ from typing import TYPE_CHECKING
import numpy
from shapely.geometry import LineString, box
from inire.geometry.component_overlap import components_overlap
from inire.constants import TOLERANCE_LINEAR
from inire.geometry.component_overlap import count_path_self_collisions
from inire.geometry.components import check_path_geometry, path_within_board
from inire.geometry.dynamic_path_index import DynamicPathIndex
from inire.geometry.index_helpers import grid_cell_span
@ -49,6 +50,13 @@ def _has_non_touching_overlap(left: BaseGeometry, right: BaseGeometry) -> bool:
return left.intersects(right) and not left.touches(right)
def _violates_clearance(left: BaseGeometry, right: BaseGeometry, clearance: float) -> bool:
if clearance <= 0:
return _has_non_touching_overlap(left, right)
tolerance = TOLERANCE_LINEAR * min(1.0, clearance)
return left.distance(right) < clearance - tolerance
def _span_to_bounds(
gx_min: int,
gy_min: int,
@ -656,7 +664,6 @@ class RoutingWorld:
self.metrics.total_verify_path_report_calls += 1
static_collision_count = 0
dynamic_collision_count = 0
self_collision_count = 0
total_length = sum(component.length for component in components)
valid_geometry, continuous = check_path_geometry(components)
if not valid_geometry:
@ -678,16 +685,10 @@ class RoutingWorld:
hits = static_obstacles.raw_tree.query(buffered, predicate="intersects")
for hit_idx in hits:
obstacle = raw_geometries[hit_idx]
if buffered.touches(obstacle):
continue
# A mitre may extend past the physical clearance
# radius at a component corner. Check the distance
# before counting an overlap with that broad phase.
if self.clearance > 0 and polygon.distance(obstacle) >= self.clearance:
continue
obj_id = static_obstacles.raw_obj_ids[hit_idx]
if not self._is_in_safety_zone(polygon, obj_id, None, None):
if _violates_clearance(polygon, obstacle, self.clearance):
static_collision_count += 1
if dynamic_paths.has_physical_paths():
@ -702,12 +703,11 @@ class RoutingWorld:
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:
if hit_net_id in polygon_hits and not capture_component_conflicts:
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:
if _violates_clearance(polygon, other, clearance):
polygon_hits.add(hit_net_id)
if capture_component_conflicts:
component_conflicts.add((component_index, hit_net_id, hit_component_index))
@ -715,16 +715,11 @@ class RoutingWorld:
dynamic_collision_count += len(component_hits)
conflicting_net_ids.update(component_hits)
for index, component in enumerate(components):
for other_index in range(index + 1, len(components)):
if components_overlap(component, components[other_index], prefer_actual=True, at_join=other_index == index + 1):
self_collision_count += 1
return PathVerificationDetail(
report=RoutingReport(
static_collision_count=static_collision_count,
dynamic_collision_count=dynamic_collision_count,
self_collision_count=self_collision_count,
self_collision_count=count_path_self_collisions(components),
total_length=total_length,
out_of_bounds=self.board is not None and not path_within_board(components, self.board),
discontinuous=not continuous,