Check physical separation within and between routed nets

This commit is contained in:
Jan Petykiewicz 2026-11-28 20:16:29 -08:00
commit d02dbb872f

View file

@ -58,6 +58,10 @@ def _violates_clearance(left: BaseGeometry, right: BaseGeometry, clearance: floa
return left.distance(right) < clearance - tolerance
def _violates_net_clearance(left: BaseGeometry, right: BaseGeometry, clearance: float) -> bool:
return _violates_clearance(left, right, clearance) if clearance else left.intersects(right)
def _span_to_bounds(
gx_min: int,
gy_min: int,
@ -93,10 +97,13 @@ class RoutingWorld:
__slots__ = (
"clearance",
"self_clearance",
"safety_zone_radius",
"board",
"grid_cell_size",
"metrics",
"_reserved_obstacles",
"_reserved_geometry_ids",
"_dynamic_paths",
"_static_obstacles",
)
@ -107,13 +114,17 @@ class RoutingWorld:
safety_zone_radius: float = 0.0021,
*,
board: Polygon | None = None,
self_clearance: float = 0.0,
) -> None:
self.clearance = clearance
self.self_clearance = self_clearance
self.safety_zone_radius = safety_zone_radius
self.board = board
self.grid_cell_size = 50.0
self.metrics = None
self._reserved_obstacles: dict[int, int] = {}
self._reserved_geometry_ids: set[int] = set()
self._static_obstacles = StaticObstacleIndex(self)
self._dynamic_paths = DynamicPathIndex(self)
@ -141,10 +152,17 @@ class RoutingWorld:
for obj_id in self._dynamic_paths.index.intersection(query_bounds):
yield self._dynamic_paths.geometries[obj_id][1].bounds
def add_static_obstacle(self, polygon: Polygon, dilated_geometry: Polygon | None = None) -> int:
return self._static_obstacles.add_obstacle(polygon, dilated_geometry=dilated_geometry)
def add_static_obstacle(self, polygon: Polygon, dilated_geometry: Polygon | None = None, *, reserved: bool = False) -> int:
obstacle_id = self._static_obstacles.add_obstacle(polygon, dilated_geometry=dilated_geometry)
if reserved and not self.clearance:
self._reserved_obstacles[obstacle_id] = id(polygon)
self._reserved_geometry_ids.add(id(polygon))
return obstacle_id
def remove_static_obstacle(self, obj_id: int) -> None:
polygon_id = self._reserved_obstacles.pop(obj_id, None)
if polygon_id is not None and polygon_id not in self._reserved_obstacles.values():
self._reserved_geometry_ids.discard(polygon_id)
self._static_obstacles.remove_obstacle(obj_id)
def _ensure_static_tree(self) -> None:
@ -257,11 +275,19 @@ class RoutingWorld:
clearance = self.clearance
left, bottom, right, top = result.total_bounds
reserved = self._reserved_geometry_ids
if reserved:
x0, y0, x1, y1 = result.total_physical_bounds
left, bottom, right, top = min(left, x0), min(bottom, y0), max(right, x1), max(top, y1)
# Expand the whole proxy envelope, including the tangent faces of an arc.
hits = tree.query(box(left - clearance, bottom - clearance, right + clearance, top + clearance))
obstacles = tree.geometries
for hit_idx in hits:
obstacle = obstacles[hit_idx]
if reserved and id(obstacle) in reserved:
if any(polygon.intersects(obstacle) for polygon in result.physical_geometry):
return True
continue
for polygon in result.collision_geometry:
if _violates_clearance(polygon, obstacle, clearance) and not self._is_in_safety_zone(polygon, obstacle, start_port, end_port):
return True
@ -304,6 +330,30 @@ class RoutingWorld:
return DynamicCongestionDetail(soft_overlap_count=real_hits_count)
def check_physical_move_congestion(
self,
result: ComponentResult,
net_id: str,
*,
frozen_net_ids: frozenset[str] = frozenset(),
) -> DynamicCongestionDetail:
"""Use physical clearance when admitting a move with a hard obstacle."""
hits = set()
for polygon in result.physical_geometry:
for other_net_id, _index, other in self._physical_candidates(net_id, polygon):
if other_net_id in hits or not _violates_net_clearance(polygon, other, self.clearance):
continue
if other_net_id in frozen_net_ids:
return DynamicCongestionDetail(soft_overlap_count=len(hits), hits_frozen_net=True)
hits.add(other_net_id)
return DynamicCongestionDetail(soft_overlap_count=len(hits))
def _physical_candidates(self, net_id: str, polygon: Polygon) -> list[tuple[str, int, Polygon]]:
minx, miny, maxx, maxy = polygon.bounds
clearance = self.clearance
bounds = (minx - clearance, miny - clearance, maxx + clearance, maxy + clearance)
return self._dynamic_paths.physical_candidates(net_id, bounds)
def _collect_congestion_candidates(
self,
result: ComponentResult,
@ -598,6 +648,8 @@ class RoutingWorld:
dynamic_paths = self._dynamic_paths
if not dynamic_paths.geometries:
return DynamicCongestionDetail()
if not self.clearance or frozen_net_ids:
return self.check_physical_move_congestion(result, net_id, frozen_net_ids=frozen_net_ids)
candidates_by_net = self._collect_congestion_candidates(
result,
@ -639,6 +691,7 @@ class RoutingWorld:
self._ensure_static_raw_tree()
if static_obstacles.raw_tree is not None:
raw_geometries = static_obstacles.raw_tree.geometries
reserved = self._reserved_geometry_ids
for component in components:
for polygon in component.physical_geometry:
if self.metrics is not None:
@ -650,24 +703,21 @@ class RoutingWorld:
# 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 _violates_clearance(polygon, obstacle, self.clearance):
if (reserved and id(obstacle) in reserved) or _violates_clearance(polygon, obstacle, self.clearance):
static_collision_count += 1
report = RoutingReport(
static_collision_count=static_collision_count,
self_collision_count=count_path_self_collisions(components),
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),
discontinuous=not continuous,
)
if (include_invalid_dynamic or not report.intrinsic_collision_count) and dynamic_paths.has_physical_paths():
clearance = max(0.0, self.clearance)
for component_index, component in enumerate(components):
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)
candidates = self._physical_candidates(net_id, polygon)
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()
@ -676,7 +726,7 @@ class RoutingWorld:
continue
if self.metrics is not None:
self.metrics.total_verify_dynamic_exact_pair_checks += 1
if _violates_clearance(polygon, other, clearance):
if _violates_net_clearance(polygon, other, self.clearance):
polygon_hits.add(hit_net_id)
if capture_component_conflicts:
component_conflicts.add((component_index, hit_net_id, hit_component_index))