2026-03-07 08:26:29 -08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-03-09 02:26:27 -07:00
|
|
|
from typing import TYPE_CHECKING, Literal
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
import rtree
|
2026-03-09 02:26:27 -07:00
|
|
|
from shapely.geometry import Point, Polygon
|
2026-03-07 08:26:29 -08:00
|
|
|
from shapely.prepared import prep
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from shapely.prepared import PreparedGeometry
|
|
|
|
|
from inire.geometry.primitives import Port
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CollisionEngine:
|
2026-03-09 02:26:27 -07:00
|
|
|
"""Manages spatial queries for collision detection with unified dilation logic."""
|
2026-03-07 08:26:29 -08:00
|
|
|
|
2026-03-08 20:18:53 -07:00
|
|
|
def __init__(self, clearance: float, max_net_width: float = 2.0, safety_zone_radius: float = 0.0021) -> None:
|
2026-03-07 08:26:29 -08:00
|
|
|
self.clearance = clearance
|
|
|
|
|
self.max_net_width = max_net_width
|
2026-03-08 20:18:53 -07:00
|
|
|
self.safety_zone_radius = safety_zone_radius
|
2026-03-09 02:26:27 -07:00
|
|
|
|
|
|
|
|
# Static obstacles: store raw geometries to avoid double-dilation
|
|
|
|
|
self.static_index = rtree.index.Index()
|
|
|
|
|
self.static_geometries: dict[int, Polygon] = {} # ID -> Polygon
|
|
|
|
|
self.static_prepared: dict[int, PreparedGeometry] = {} # ID -> PreparedGeometry
|
|
|
|
|
self._static_id_counter = 0
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
# Dynamic paths for multi-net congestion
|
2026-03-09 02:26:27 -07:00
|
|
|
self.dynamic_index = rtree.index.Index()
|
|
|
|
|
# obj_id -> (net_id, raw_geometry)
|
|
|
|
|
self.dynamic_geometries: dict[int, tuple[str, Polygon]] = {}
|
2026-03-07 08:26:29 -08:00
|
|
|
self._dynamic_id_counter = 0
|
|
|
|
|
|
2026-03-09 02:26:27 -07:00
|
|
|
def add_static_obstacle(self, polygon: Polygon) -> None:
|
|
|
|
|
"""Add a static obstacle (raw geometry) to the engine."""
|
|
|
|
|
obj_id = self._static_id_counter
|
|
|
|
|
self._static_id_counter += 1
|
2026-03-07 08:26:29 -08:00
|
|
|
|
2026-03-09 02:26:27 -07:00
|
|
|
self.static_geometries[obj_id] = polygon
|
|
|
|
|
self.static_prepared[obj_id] = prep(polygon)
|
|
|
|
|
self.static_index.insert(obj_id, polygon.bounds)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
def add_path(self, net_id: str, geometry: list[Polygon]) -> None:
|
2026-03-09 02:26:27 -07:00
|
|
|
"""Add a net's routed path (raw geometry) to the dynamic index."""
|
2026-03-07 08:26:29 -08:00
|
|
|
for poly in geometry:
|
|
|
|
|
obj_id = self._dynamic_id_counter
|
|
|
|
|
self._dynamic_id_counter += 1
|
2026-03-09 02:26:27 -07:00
|
|
|
self.dynamic_geometries[obj_id] = (net_id, poly)
|
|
|
|
|
self.dynamic_index.insert(obj_id, poly.bounds)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
def remove_path(self, net_id: str) -> None:
|
2026-03-09 02:26:27 -07:00
|
|
|
"""Remove a net's path from the dynamic index."""
|
|
|
|
|
to_remove = [obj_id for obj_id, (nid, _) in self.dynamic_geometries.items() if nid == net_id]
|
2026-03-07 08:26:29 -08:00
|
|
|
for obj_id in to_remove:
|
2026-03-09 02:26:27 -07:00
|
|
|
nid, poly = self.dynamic_geometries.pop(obj_id)
|
|
|
|
|
self.dynamic_index.delete(obj_id, poly.bounds)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
def lock_net(self, net_id: str) -> None:
|
|
|
|
|
"""Move a net's dynamic path to static obstacles permanently."""
|
2026-03-09 02:26:27 -07:00
|
|
|
to_move = [obj_id for obj_id, (nid, _) in self.dynamic_geometries.items() if nid == net_id]
|
2026-03-07 08:26:29 -08:00
|
|
|
for obj_id in to_move:
|
2026-03-09 02:26:27 -07:00
|
|
|
nid, poly = self.dynamic_geometries.pop(obj_id)
|
|
|
|
|
self.dynamic_index.delete(obj_id, poly.bounds)
|
|
|
|
|
self.add_static_obstacle(poly)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
def is_collision(
|
2026-03-09 02:26:27 -07:00
|
|
|
self,
|
|
|
|
|
geometry: Polygon,
|
|
|
|
|
net_width: float = 2.0,
|
|
|
|
|
start_port: Port | None = None,
|
|
|
|
|
end_port: Port | None = None
|
2026-03-07 08:26:29 -08:00
|
|
|
) -> bool:
|
2026-03-09 02:26:27 -07:00
|
|
|
"""Alias for check_collision(buffer_mode='static') for backward compatibility."""
|
|
|
|
|
_ = net_width
|
|
|
|
|
res = self.check_collision(geometry, "default", buffer_mode="static", start_port=start_port, end_port=end_port)
|
|
|
|
|
return bool(res)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
2026-03-09 02:26:27 -07:00
|
|
|
def count_congestion(self, geometry: Polygon, net_id: str) -> int:
|
|
|
|
|
"""Alias for check_collision(buffer_mode='congestion') for backward compatibility."""
|
|
|
|
|
res = self.check_collision(geometry, net_id, buffer_mode="congestion")
|
|
|
|
|
return int(res)
|
|
|
|
|
|
|
|
|
|
def check_collision(
|
|
|
|
|
self,
|
|
|
|
|
geometry: Polygon,
|
|
|
|
|
net_id: str,
|
|
|
|
|
buffer_mode: Literal["static", "congestion"] = "static",
|
2026-03-07 19:31:51 -08:00
|
|
|
start_port: Port | None = None,
|
2026-03-09 02:26:27 -07:00
|
|
|
end_port: Port | None = None
|
|
|
|
|
) -> bool | int:
|
|
|
|
|
"""
|
|
|
|
|
Check for collisions using unified dilation logic.
|
|
|
|
|
|
|
|
|
|
If buffer_mode == "static":
|
|
|
|
|
Returns True if geometry collides with static obstacles (buffered by full clearance).
|
|
|
|
|
If buffer_mode == "congestion":
|
|
|
|
|
Returns count of other nets colliding with geometry (both buffered by clearance/2).
|
|
|
|
|
"""
|
|
|
|
|
if buffer_mode == "static":
|
|
|
|
|
# Buffered move vs raw static obstacle
|
|
|
|
|
# Distance must be >= clearance
|
|
|
|
|
test_poly = geometry.buffer(self.clearance)
|
|
|
|
|
candidates = self.static_index.intersection(test_poly.bounds)
|
|
|
|
|
|
|
|
|
|
for obj_id in candidates:
|
|
|
|
|
if self.static_prepared[obj_id].intersects(test_poly):
|
|
|
|
|
# Safety zone check (using exact intersection area/bounds)
|
|
|
|
|
if start_port or end_port:
|
|
|
|
|
intersection = test_poly.intersection(self.static_geometries[obj_id])
|
|
|
|
|
if intersection.is_empty:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
ix_minx, ix_miny, ix_maxx, ix_maxy = intersection.bounds
|
|
|
|
|
|
|
|
|
|
is_safe = False
|
|
|
|
|
for p in [start_port, end_port]:
|
|
|
|
|
if p and (abs(ix_minx - p.x) < self.safety_zone_radius and
|
|
|
|
|
abs(ix_maxx - p.x) < self.safety_zone_radius and
|
|
|
|
|
abs(ix_miny - p.y) < self.safety_zone_radius and
|
|
|
|
|
abs(ix_maxy - p.y) < self.safety_zone_radius):
|
|
|
|
|
is_safe = True
|
|
|
|
|
break
|
|
|
|
|
if is_safe:
|
|
|
|
|
continue
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2026-03-07 08:26:29 -08:00
|
|
|
|
2026-03-09 02:26:27 -07:00
|
|
|
else: # buffer_mode == "congestion"
|
|
|
|
|
# Both paths buffered by clearance/2 => Total separation = clearance
|
|
|
|
|
dilation = self.clearance / 2.0
|
|
|
|
|
test_poly = geometry.buffer(dilation)
|
|
|
|
|
candidates = self.dynamic_index.intersection(test_poly.bounds)
|
|
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
for obj_id in candidates:
|
|
|
|
|
other_net_id, other_poly = self.dynamic_geometries[obj_id]
|
|
|
|
|
if other_net_id != net_id:
|
|
|
|
|
# Buffer the other path segment too
|
|
|
|
|
if test_poly.intersects(other_poly.buffer(dilation)):
|
|
|
|
|
count += 1
|
|
|
|
|
return count
|