2026-03-07 08:26:29 -08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
from dataclasses import dataclass
|
2026-03-30 19:51:37 -07:00
|
|
|
from typing import TYPE_CHECKING
|
2026-03-29 20:35:58 -07:00
|
|
|
|
2026-03-15 21:14:42 -07:00
|
|
|
import numpy
|
2026-03-30 15:32:29 -07:00
|
|
|
from shapely.geometry import LineString, box
|
2026-03-29 20:35:58 -07:00
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
from inire.geometry.component_overlap import components_overlap
|
2026-11-27 15:21:37 -08:00
|
|
|
from inire.geometry.components import check_path_geometry, path_within_board
|
2026-03-29 20:35:58 -07:00
|
|
|
from inire.geometry.dynamic_path_index import DynamicPathIndex
|
2026-03-30 15:32:29 -07:00
|
|
|
from inire.geometry.index_helpers import grid_cell_span
|
2026-03-30 19:51:37 -07:00
|
|
|
from inire.results import RoutingReport
|
2026-03-29 20:35:58 -07:00
|
|
|
from inire.geometry.static_obstacle_index import StaticObstacleIndex
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2026-03-30 15:32:29 -07:00
|
|
|
from collections.abc import Iterable, Sequence
|
2026-03-29 20:35:58 -07:00
|
|
|
|
2026-03-09 03:19:01 -07:00
|
|
|
from shapely.geometry import Polygon
|
2026-03-30 15:32:29 -07:00
|
|
|
from shapely.geometry.base import BaseGeometry
|
2026-03-29 20:35:58 -07:00
|
|
|
from shapely.strtree import STRtree
|
|
|
|
|
|
2026-03-15 21:14:42 -07:00
|
|
|
from inire.geometry.components import ComponentResult
|
2026-03-29 20:35:58 -07:00
|
|
|
from inire.geometry.primitives import Port
|
2026-03-07 08:26:29 -08:00
|
|
|
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
_RAY_CAST_CACHE_LIMIT = 8192
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def _intersection_distance(origin: Port, geometry: BaseGeometry) -> float:
|
|
|
|
|
if hasattr(geometry, "geoms"):
|
|
|
|
|
return min(_intersection_distance(origin, sub_geometry) for sub_geometry in geometry.geoms)
|
|
|
|
|
return float(numpy.sqrt((geometry.coords[0][0] - origin.x) ** 2 + (geometry.coords[0][1] - origin.y) ** 2))
|
|
|
|
|
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def _bounds_overlap(
|
|
|
|
|
left: tuple[float, float, float, float],
|
|
|
|
|
right: tuple[float, float, float, float],
|
|
|
|
|
) -> bool:
|
|
|
|
|
return (
|
|
|
|
|
left[0] < right[2]
|
|
|
|
|
and left[2] > right[0]
|
|
|
|
|
and left[1] < right[3]
|
|
|
|
|
and left[3] > right[1]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _has_non_touching_overlap(left: BaseGeometry, right: BaseGeometry) -> bool:
|
|
|
|
|
return left.intersects(right) and not left.touches(right)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _span_to_bounds(
|
|
|
|
|
gx_min: int,
|
|
|
|
|
gy_min: int,
|
|
|
|
|
gx_max: int,
|
|
|
|
|
gy_max: int,
|
|
|
|
|
cell_size: float,
|
|
|
|
|
) -> tuple[float, float, float, float]:
|
|
|
|
|
return (
|
|
|
|
|
gx_min * cell_size,
|
|
|
|
|
gy_min * cell_size,
|
|
|
|
|
(gx_max + 1) * cell_size,
|
|
|
|
|
(gy_max + 1) * cell_size,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class PathVerificationDetail:
|
|
|
|
|
report: RoutingReport
|
|
|
|
|
conflicting_net_ids: tuple[str, ...] = ()
|
2026-04-02 14:39:39 -07:00
|
|
|
component_conflicts: tuple[tuple[int, str, int], ...] = ()
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class DynamicCongestionDetail:
|
|
|
|
|
soft_overlap_count: int = 0
|
|
|
|
|
hits_frozen_net: bool = False
|
|
|
|
|
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
class RoutingWorld:
|
2026-03-09 03:19:01 -07:00
|
|
|
"""
|
2026-03-30 15:32:29 -07:00
|
|
|
Internal spatial state for collision detection, congestion, and verification.
|
2026-03-09 03:19:01 -07:00
|
|
|
"""
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-03-09 03:19:01 -07:00
|
|
|
__slots__ = (
|
2026-03-30 15:32:29 -07:00
|
|
|
"clearance",
|
|
|
|
|
"safety_zone_radius",
|
2026-11-27 14:12:40 -08:00
|
|
|
"board",
|
2026-03-30 15:32:29 -07:00
|
|
|
"grid_cell_size",
|
2026-03-31 17:26:00 -07:00
|
|
|
"metrics",
|
2026-03-30 15:32:29 -07:00
|
|
|
"_dynamic_paths",
|
|
|
|
|
"_static_obstacles",
|
2026-03-09 03:19:01 -07:00
|
|
|
)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
2026-03-09 03:19:01 -07:00
|
|
|
def __init__(
|
2026-03-30 15:32:29 -07:00
|
|
|
self,
|
|
|
|
|
clearance: float,
|
|
|
|
|
safety_zone_radius: float = 0.0021,
|
2026-11-27 14:12:40 -08:00
|
|
|
*,
|
|
|
|
|
board: Polygon | None = None,
|
2026-03-30 15:32:29 -07:00
|
|
|
) -> None:
|
2026-03-07 08:26:29 -08:00
|
|
|
self.clearance = clearance
|
2026-03-08 20:18:53 -07:00
|
|
|
self.safety_zone_radius = safety_zone_radius
|
2026-11-27 14:12:40 -08:00
|
|
|
self.board = board
|
2026-03-09 03:19:01 -07:00
|
|
|
|
2026-03-19 15:03:29 -07:00
|
|
|
self.grid_cell_size = 50.0
|
2026-03-31 17:26:00 -07:00
|
|
|
self.metrics = None
|
2026-03-29 20:35:58 -07:00
|
|
|
self._static_obstacles = StaticObstacleIndex(self)
|
|
|
|
|
self._dynamic_paths = DynamicPathIndex(self)
|
|
|
|
|
|
|
|
|
|
def get_static_version(self) -> int:
|
|
|
|
|
return self._static_obstacles.version
|
|
|
|
|
|
2026-11-26 19:58:46 -08:00
|
|
|
def iter_static_geometries(self) -> Iterable[Polygon]:
|
|
|
|
|
"""Iterate over the current raw obstacles in insertion order."""
|
|
|
|
|
return self._static_obstacles.geometries.values()
|
|
|
|
|
|
2026-03-29 20:35:58 -07:00
|
|
|
def iter_static_dilated_geometries(self) -> Iterable[Polygon]:
|
|
|
|
|
return self._static_obstacles.dilated.values()
|
|
|
|
|
|
|
|
|
|
def iter_static_obstacle_bounds(
|
|
|
|
|
self,
|
|
|
|
|
query_bounds: tuple[float, float, float, float],
|
|
|
|
|
) -> Iterable[tuple[float, float, float, float]]:
|
|
|
|
|
for obj_id in self._static_obstacles.index.intersection(query_bounds):
|
|
|
|
|
yield self._static_obstacles.geometries[obj_id].bounds
|
|
|
|
|
|
|
|
|
|
def iter_dynamic_path_bounds(
|
|
|
|
|
self,
|
|
|
|
|
query_bounds: tuple[float, float, float, float],
|
|
|
|
|
) -> Iterable[tuple[float, float, float, float]]:
|
|
|
|
|
for obj_id in self._dynamic_paths.index.intersection(query_bounds):
|
|
|
|
|
yield self._dynamic_paths.geometries[obj_id][1].bounds
|
|
|
|
|
|
2026-03-29 01:26:22 -07:00
|
|
|
def add_static_obstacle(self, polygon: Polygon, dilated_geometry: Polygon | None = None) -> int:
|
2026-03-29 20:35:58 -07:00
|
|
|
return self._static_obstacles.add_obstacle(polygon, dilated_geometry=dilated_geometry)
|
2026-03-20 16:59:36 -07:00
|
|
|
|
|
|
|
|
def remove_static_obstacle(self, obj_id: int) -> None:
|
2026-03-29 20:35:58 -07:00
|
|
|
self._static_obstacles.remove_obstacle(obj_id)
|
2026-03-29 01:26:22 -07:00
|
|
|
|
2026-03-15 21:14:42 -07:00
|
|
|
def _ensure_static_tree(self) -> None:
|
2026-03-29 20:35:58 -07:00
|
|
|
self._static_obstacles.ensure_tree()
|
2026-03-15 21:14:42 -07:00
|
|
|
|
2026-03-29 01:26:22 -07:00
|
|
|
def _ensure_net_static_tree(self, net_width: float) -> STRtree:
|
2026-03-29 20:35:58 -07:00
|
|
|
return self._static_obstacles.ensure_net_tree(net_width)
|
2026-03-29 01:26:22 -07:00
|
|
|
|
2026-03-19 15:03:29 -07:00
|
|
|
def _ensure_static_raw_tree(self) -> None:
|
2026-03-29 20:35:58 -07:00
|
|
|
self._static_obstacles.ensure_raw_tree()
|
2026-03-15 21:14:42 -07:00
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def add_path(
|
|
|
|
|
self,
|
|
|
|
|
net_id: str,
|
|
|
|
|
geometry: Sequence[Polygon],
|
|
|
|
|
dilated_geometry: Sequence[Polygon],
|
|
|
|
|
component_indexes: Sequence[int] | None = None,
|
2026-11-27 14:12:40 -08:00
|
|
|
*,
|
|
|
|
|
physical_geometry: Sequence[tuple[int, Polygon]] | None = None,
|
2026-04-02 14:39:39 -07:00
|
|
|
) -> None:
|
|
|
|
|
self._dynamic_paths.add_path(
|
|
|
|
|
net_id,
|
|
|
|
|
geometry,
|
|
|
|
|
dilated_geometry=dilated_geometry,
|
|
|
|
|
component_indexes=component_indexes,
|
2026-11-27 14:12:40 -08:00
|
|
|
physical_geometry=physical_geometry,
|
2026-04-02 14:39:39 -07:00
|
|
|
)
|
2026-03-09 03:19:01 -07:00
|
|
|
|
2026-11-27 14:12:40 -08:00
|
|
|
def install_path(self, net_id: str, path: Sequence[ComponentResult]) -> None:
|
2026-11-27 15:21:37 -08:00
|
|
|
if any(not component.physical_checks[0] for component in path):
|
|
|
|
|
return
|
2026-11-27 14:12:40 -08:00
|
|
|
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)
|
|
|
|
|
|
2026-03-19 15:03:29 -07:00
|
|
|
def remove_path(self, net_id: str) -> None:
|
2026-03-29 20:35:58 -07:00
|
|
|
self._dynamic_paths.remove_path(net_id)
|
2026-03-07 08:26:29 -08:00
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def has_dynamic_paths(self) -> bool:
|
|
|
|
|
return bool(self._dynamic_paths.geometries)
|
|
|
|
|
|
2026-03-29 01:26:22 -07:00
|
|
|
def check_move_straight_static(self, start_port: Port, length: float, net_width: float) -> bool:
|
2026-03-31 17:41:15 -07:00
|
|
|
reach = self.ray_cast(
|
|
|
|
|
start_port,
|
|
|
|
|
start_port.r,
|
|
|
|
|
max_dist=length + 0.01,
|
|
|
|
|
net_width=net_width,
|
|
|
|
|
caller="straight_static",
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
return reach < length - 0.001
|
2026-03-15 21:14:42 -07:00
|
|
|
|
2026-03-26 20:22:17 -07:00
|
|
|
def _is_in_safety_zone_fast(self, idx: int, start_port: Port | None, end_port: Port | None) -> bool:
|
2026-03-30 23:54:30 -07:00
|
|
|
bounds_array = self._static_obstacles.bounds_array
|
|
|
|
|
if bounds_array is None:
|
|
|
|
|
return False
|
|
|
|
|
bounds = bounds_array[idx]
|
2026-03-30 15:32:29 -07:00
|
|
|
safety_zone = self.safety_zone_radius
|
2026-03-30 23:54:30 -07:00
|
|
|
if (
|
|
|
|
|
start_port
|
|
|
|
|
and bounds[0] - safety_zone <= start_port.x <= bounds[2] + safety_zone
|
|
|
|
|
and bounds[1] - safety_zone <= start_port.y <= bounds[3] + safety_zone
|
|
|
|
|
):
|
2026-03-30 15:32:29 -07:00
|
|
|
return True
|
|
|
|
|
return bool(
|
|
|
|
|
end_port
|
|
|
|
|
and bounds[0] - safety_zone <= end_port.x <= bounds[2] + safety_zone
|
|
|
|
|
and bounds[1] - safety_zone <= end_port.y <= bounds[3] + safety_zone
|
2026-03-29 20:35:58 -07:00
|
|
|
)
|
2026-03-15 21:14:42 -07:00
|
|
|
|
2026-03-29 20:35:58 -07:00
|
|
|
def _is_in_safety_zone(
|
|
|
|
|
self,
|
|
|
|
|
geometry: Polygon,
|
|
|
|
|
obj_id: int,
|
|
|
|
|
start_port: Port | None,
|
|
|
|
|
end_port: Port | None,
|
|
|
|
|
) -> bool:
|
2026-03-30 15:32:29 -07:00
|
|
|
raw_obstacle = self._static_obstacles.geometries[obj_id]
|
|
|
|
|
safety_zone = self.safety_zone_radius
|
|
|
|
|
|
|
|
|
|
obstacle_bounds = raw_obstacle.bounds
|
|
|
|
|
near_start = start_port and (
|
|
|
|
|
obstacle_bounds[0] - safety_zone <= start_port.x <= obstacle_bounds[2] + safety_zone
|
|
|
|
|
and obstacle_bounds[1] - safety_zone <= start_port.y <= obstacle_bounds[3] + safety_zone
|
|
|
|
|
)
|
|
|
|
|
near_end = end_port and (
|
|
|
|
|
obstacle_bounds[0] - safety_zone <= end_port.x <= obstacle_bounds[2] + safety_zone
|
|
|
|
|
and obstacle_bounds[1] - safety_zone <= end_port.y <= obstacle_bounds[3] + safety_zone
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not near_start and not near_end:
|
|
|
|
|
return False
|
|
|
|
|
if not geometry.intersects(raw_obstacle):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
intersection = geometry.intersection(raw_obstacle)
|
|
|
|
|
if intersection.is_empty:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
ix_bounds = intersection.bounds
|
|
|
|
|
if (
|
|
|
|
|
start_port
|
|
|
|
|
and near_start
|
|
|
|
|
and abs(ix_bounds[0] - start_port.x) < safety_zone
|
|
|
|
|
and abs(ix_bounds[1] - start_port.y) < safety_zone
|
|
|
|
|
and abs(ix_bounds[2] - start_port.x) < safety_zone
|
|
|
|
|
and abs(ix_bounds[3] - start_port.y) < safety_zone
|
|
|
|
|
):
|
|
|
|
|
return True
|
|
|
|
|
return bool(
|
|
|
|
|
end_port
|
|
|
|
|
and near_end
|
|
|
|
|
and abs(ix_bounds[0] - end_port.x) < safety_zone
|
|
|
|
|
and abs(ix_bounds[1] - end_port.y) < safety_zone
|
|
|
|
|
and abs(ix_bounds[2] - end_port.x) < safety_zone
|
|
|
|
|
and abs(ix_bounds[3] - end_port.y) < safety_zone
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def check_move_static(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
start_port: Port | None = None,
|
|
|
|
|
end_port: Port | None = None,
|
|
|
|
|
) -> bool:
|
2026-03-30 19:51:37 -07:00
|
|
|
# TODO: If static buffering becomes net-width-specific, add dedicated
|
|
|
|
|
# width-aware geometry/index handling instead of reviving dead args here.
|
2026-03-30 15:32:29 -07:00
|
|
|
static_obstacles = self._static_obstacles
|
|
|
|
|
if not static_obstacles.dilated:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
self._ensure_static_tree()
|
2026-03-30 23:54:30 -07:00
|
|
|
tree = static_obstacles.tree
|
|
|
|
|
bounds_array = static_obstacles.bounds_array
|
|
|
|
|
if tree is None or bounds_array is None:
|
|
|
|
|
return False
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-03-30 23:54:30 -07:00
|
|
|
hits = tree.query(box(*result.total_dilated_bounds))
|
2026-03-30 15:32:29 -07:00
|
|
|
if hits.size == 0:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
move_poly_bounds = result.dilated_bounds
|
|
|
|
|
for hit_idx in hits:
|
2026-03-30 23:54:30 -07:00
|
|
|
obstacle_bounds = bounds_array[hit_idx]
|
2026-03-30 15:32:29 -07:00
|
|
|
poly_hits_obstacle_aabb = False
|
|
|
|
|
for poly_bounds in move_poly_bounds:
|
|
|
|
|
if (
|
|
|
|
|
poly_bounds[0] < obstacle_bounds[2]
|
|
|
|
|
and poly_bounds[2] > obstacle_bounds[0]
|
|
|
|
|
and poly_bounds[1] < obstacle_bounds[3]
|
|
|
|
|
and poly_bounds[3] > obstacle_bounds[1]
|
|
|
|
|
):
|
|
|
|
|
poly_hits_obstacle_aabb = True
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if not poly_hits_obstacle_aabb:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
obj_id = static_obstacles.obj_ids[hit_idx]
|
|
|
|
|
if self._is_in_safety_zone_fast(hit_idx, start_port, end_port):
|
|
|
|
|
collision_found = False
|
|
|
|
|
for polygon in result.collision_geometry:
|
|
|
|
|
if not self._is_in_safety_zone(polygon, obj_id, start_port, end_port):
|
|
|
|
|
collision_found = True
|
|
|
|
|
break
|
|
|
|
|
if collision_found:
|
|
|
|
|
return True
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
static_obstacle = static_obstacles.dilated[obj_id]
|
|
|
|
|
for polygon in result.dilated_collision_geometry:
|
|
|
|
|
if polygon.intersects(static_obstacle) and not polygon.touches(static_obstacle):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def _check_real_congestion(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
candidates_by_net: dict[str, dict[int, tuple[int, ...]]],
|
|
|
|
|
frozen_net_ids: frozenset[str] = frozenset(),
|
|
|
|
|
) -> DynamicCongestionDetail:
|
|
|
|
|
if not candidates_by_net:
|
|
|
|
|
return DynamicCongestionDetail()
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
geometries_to_test = result.dilated_collision_geometry
|
|
|
|
|
|
|
|
|
|
real_hits_count = 0
|
2026-04-01 21:29:23 -07:00
|
|
|
for other_net_id, other_obj_ids in candidates_by_net.items():
|
2026-03-30 15:32:29 -07:00
|
|
|
found_real = False
|
2026-04-01 21:29:23 -07:00
|
|
|
for obj_id, test_geometry_indexes in other_obj_ids.items():
|
|
|
|
|
tree_geometry = dynamic_paths.dilated[obj_id]
|
|
|
|
|
for test_geometry_index in test_geometry_indexes:
|
|
|
|
|
test_geometry = geometries_to_test[test_geometry_index]
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_exact_pair_checks += 1
|
|
|
|
|
if _has_non_touching_overlap(test_geometry, tree_geometry):
|
|
|
|
|
found_real = True
|
|
|
|
|
break
|
|
|
|
|
if found_real:
|
2026-03-30 15:32:29 -07:00
|
|
|
break
|
|
|
|
|
if found_real:
|
2026-04-01 21:29:23 -07:00
|
|
|
if other_net_id in frozen_net_ids:
|
|
|
|
|
return DynamicCongestionDetail(
|
|
|
|
|
soft_overlap_count=real_hits_count,
|
|
|
|
|
hits_frozen_net=True,
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
real_hits_count += 1
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
return DynamicCongestionDetail(soft_overlap_count=real_hits_count)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def _collect_congestion_candidates(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
net_id: str,
|
|
|
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
broad_phase_cache: dict[tuple[str, int, int, int, int], dict[str, tuple[int, ...]]] | None = None,
|
|
|
|
|
) -> dict[str, dict[int, tuple[int, ...]]]:
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
if not dynamic_paths.dilated:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
candidates_by_net: dict[str, dict[int, set[int]]] = {}
|
|
|
|
|
for test_geometry_index, test_bounds in enumerate(result.dilated_bounds):
|
|
|
|
|
if not self._has_possible_congestion_in_grid(test_bounds, net_id):
|
|
|
|
|
continue
|
|
|
|
|
envelope_net_ids = self._get_net_envelope_candidates(
|
|
|
|
|
test_bounds,
|
|
|
|
|
net_id,
|
|
|
|
|
net_envelope_cache,
|
|
|
|
|
)
|
|
|
|
|
if not envelope_net_ids:
|
|
|
|
|
continue
|
|
|
|
|
grid_net_ids = self._get_grid_span_net_candidates(
|
|
|
|
|
test_bounds,
|
|
|
|
|
net_id,
|
|
|
|
|
grid_net_cache,
|
|
|
|
|
)
|
|
|
|
|
if not grid_net_ids:
|
|
|
|
|
continue
|
|
|
|
|
candidate_net_ids = tuple(sorted(set(envelope_net_ids) & set(grid_net_ids)))
|
|
|
|
|
if not candidate_net_ids:
|
|
|
|
|
continue
|
|
|
|
|
grid_candidates = self._get_grid_span_candidates(
|
|
|
|
|
test_bounds,
|
|
|
|
|
net_id,
|
|
|
|
|
broad_phase_cache,
|
|
|
|
|
)
|
|
|
|
|
for other_net_id in candidate_net_ids:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_candidate_nets += 1
|
|
|
|
|
obj_ids = grid_candidates.get(other_net_id)
|
|
|
|
|
if not obj_ids:
|
|
|
|
|
continue
|
|
|
|
|
for obj_id in obj_ids:
|
|
|
|
|
if not _bounds_overlap(test_bounds, dynamic_paths.dilated_bounds[obj_id]):
|
|
|
|
|
continue
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_candidate_ids += 1
|
|
|
|
|
candidate_indexes = candidates_by_net.setdefault(other_net_id, {}).setdefault(obj_id, set())
|
|
|
|
|
candidate_indexes.add(test_geometry_index)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
other_net_id: {
|
|
|
|
|
obj_id: tuple(sorted(test_geometry_indexes))
|
|
|
|
|
for obj_id, test_geometry_indexes in sorted(obj_ids.items())
|
|
|
|
|
}
|
|
|
|
|
for other_net_id, obj_ids in candidates_by_net.items()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def _has_possible_congestion_in_grid(
|
|
|
|
|
self,
|
|
|
|
|
bounds: tuple[float, float, float, float],
|
|
|
|
|
net_id: str,
|
|
|
|
|
) -> bool:
|
|
|
|
|
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
|
2026-11-26 21:55:34 -08:00
|
|
|
cells = self._dynamic_paths.grid_net_obj_ids
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
if gx_min == gx_max and gy_min == gy_max:
|
2026-11-26 21:55:34 -08:00
|
|
|
members = cells.get((gx_min, gy_min))
|
|
|
|
|
return bool(members and (len(members) > 1 or net_id not in members))
|
2026-04-01 21:29:23 -07:00
|
|
|
|
|
|
|
|
for gx in range(gx_min, gx_max + 1):
|
|
|
|
|
for gy in range(gy_min, gy_max + 1):
|
2026-11-26 21:55:34 -08:00
|
|
|
members = cells.get((gx, gy))
|
|
|
|
|
if members and (len(members) > 1 or net_id not in members):
|
2026-04-01 21:29:23 -07:00
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def has_possible_move_congestion(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
net_id: str,
|
|
|
|
|
presence_cache: dict[tuple[str, int, int, int, int], bool] | None = None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
if not dynamic_paths.dilated:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
for bounds in result.dilated_bounds:
|
|
|
|
|
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
|
|
|
|
|
cache_key = (net_id, gx_min, gy_min, gx_max, gy_max)
|
|
|
|
|
if presence_cache is not None and cache_key in presence_cache:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_presence_cache_hits += 1
|
|
|
|
|
has_possible = presence_cache[cache_key]
|
|
|
|
|
else:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_presence_cache_misses += 1
|
|
|
|
|
has_possible = self._has_possible_congestion_in_grid(bounds, net_id)
|
|
|
|
|
if presence_cache is not None:
|
|
|
|
|
presence_cache[cache_key] = has_possible
|
|
|
|
|
if has_possible:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def has_candidate_move_congestion(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
net_id: str,
|
|
|
|
|
candidate_precheck_cache: dict[tuple[str, int, int, int, int], bool] | None = None,
|
|
|
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
if not dynamic_paths.dilated:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
for bounds in result.dilated_bounds:
|
|
|
|
|
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
|
|
|
|
|
cache_key = (net_id, gx_min, gy_min, gx_max, gy_max)
|
|
|
|
|
if candidate_precheck_cache is not None and cache_key in candidate_precheck_cache:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_candidate_precheck_hits += 1
|
|
|
|
|
has_candidates = candidate_precheck_cache[cache_key]
|
|
|
|
|
else:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_candidate_precheck_misses += 1
|
|
|
|
|
span_bounds = _span_to_bounds(gx_min, gy_min, gx_max, gy_max, self.grid_cell_size)
|
|
|
|
|
envelope_net_ids = self._get_net_envelope_candidates(
|
|
|
|
|
span_bounds,
|
|
|
|
|
net_id,
|
|
|
|
|
net_envelope_cache,
|
|
|
|
|
)
|
|
|
|
|
if not envelope_net_ids:
|
|
|
|
|
has_candidates = False
|
|
|
|
|
else:
|
|
|
|
|
grid_net_ids = self._get_grid_span_net_candidates(
|
|
|
|
|
span_bounds,
|
|
|
|
|
net_id,
|
|
|
|
|
grid_net_cache,
|
|
|
|
|
)
|
|
|
|
|
if not grid_net_ids:
|
|
|
|
|
has_candidates = False
|
|
|
|
|
else:
|
|
|
|
|
has_candidates = bool(set(envelope_net_ids) & set(grid_net_ids))
|
|
|
|
|
if candidate_precheck_cache is not None:
|
|
|
|
|
candidate_precheck_cache[cache_key] = has_candidates
|
|
|
|
|
if has_candidates:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def _get_grid_span_candidates(
|
|
|
|
|
self,
|
|
|
|
|
bounds: tuple[float, float, float, float],
|
|
|
|
|
net_id: str,
|
|
|
|
|
broad_phase_cache: dict[tuple[str, int, int, int, int], dict[str, tuple[int, ...]]] | None,
|
|
|
|
|
) -> dict[str, tuple[int, ...]]:
|
|
|
|
|
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
|
|
|
|
|
cache_key = (net_id, gx_min, gy_min, gx_max, gy_max)
|
|
|
|
|
if broad_phase_cache is not None and cache_key in broad_phase_cache:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_grid_span_cache_hits += 1
|
|
|
|
|
return broad_phase_cache[cache_key]
|
|
|
|
|
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_grid_span_cache_misses += 1
|
|
|
|
|
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
candidates_by_net: dict[str, set[int]] = {}
|
|
|
|
|
for gx in range(gx_min, gx_max + 1):
|
|
|
|
|
for gy in range(gy_min, gy_max + 1):
|
|
|
|
|
for other_net_id, obj_ids in dynamic_paths.grid_net_obj_ids.get((gx, gy), {}).items():
|
|
|
|
|
if other_net_id == net_id:
|
|
|
|
|
continue
|
|
|
|
|
candidates_by_net.setdefault(other_net_id, set()).update(obj_ids)
|
|
|
|
|
|
|
|
|
|
frozen = {
|
|
|
|
|
other_net_id: tuple(sorted(obj_ids))
|
|
|
|
|
for other_net_id, obj_ids in sorted(candidates_by_net.items())
|
|
|
|
|
}
|
|
|
|
|
if broad_phase_cache is not None:
|
|
|
|
|
broad_phase_cache[cache_key] = frozen
|
|
|
|
|
return frozen
|
|
|
|
|
|
|
|
|
|
def _get_grid_span_net_candidates(
|
|
|
|
|
self,
|
|
|
|
|
bounds: tuple[float, float, float, float],
|
|
|
|
|
net_id: str,
|
|
|
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None,
|
|
|
|
|
) -> tuple[str, ...]:
|
|
|
|
|
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
|
|
|
|
|
cache_key = (net_id, gx_min, gy_min, gx_max, gy_max)
|
|
|
|
|
if grid_net_cache is not None and cache_key in grid_net_cache:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_grid_net_cache_hits += 1
|
|
|
|
|
return grid_net_cache[cache_key]
|
|
|
|
|
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_grid_net_cache_misses += 1
|
|
|
|
|
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
candidate_net_ids: set[str] = set()
|
|
|
|
|
for gx in range(gx_min, gx_max + 1):
|
|
|
|
|
for gy in range(gy_min, gy_max + 1):
|
|
|
|
|
for other_net_id in dynamic_paths.grid_net_obj_ids.get((gx, gy), {}):
|
|
|
|
|
if other_net_id != net_id:
|
|
|
|
|
candidate_net_ids.add(other_net_id)
|
|
|
|
|
|
|
|
|
|
frozen = tuple(sorted(candidate_net_ids))
|
|
|
|
|
if grid_net_cache is not None:
|
|
|
|
|
grid_net_cache[cache_key] = frozen
|
|
|
|
|
return frozen
|
|
|
|
|
|
|
|
|
|
def _get_net_envelope_candidates(
|
|
|
|
|
self,
|
|
|
|
|
bounds: tuple[float, float, float, float],
|
|
|
|
|
net_id: str,
|
|
|
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None,
|
|
|
|
|
) -> tuple[str, ...]:
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
gx_min, gy_min, gx_max, gy_max = grid_cell_span(bounds, self.grid_cell_size)
|
|
|
|
|
cache_key = (net_id, gx_min, gy_min, gx_max, gy_max)
|
|
|
|
|
if net_envelope_cache is not None and cache_key in net_envelope_cache:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_net_envelope_cache_hits += 1
|
|
|
|
|
cached_net_ids = net_envelope_cache[cache_key]
|
|
|
|
|
else:
|
|
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_net_envelope_cache_misses += 1
|
|
|
|
|
span_bounds = _span_to_bounds(gx_min, gy_min, gx_max, gy_max, self.grid_cell_size)
|
|
|
|
|
cached_net_ids = tuple(
|
|
|
|
|
sorted(
|
|
|
|
|
dynamic_paths.net_envelope_obj_to_net[obj_id]
|
|
|
|
|
for obj_id in dynamic_paths.net_envelope_index.intersection(span_bounds)
|
|
|
|
|
if dynamic_paths.net_envelope_obj_to_net[obj_id] != net_id
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if net_envelope_cache is not None:
|
|
|
|
|
net_envelope_cache[cache_key] = cached_net_ids
|
|
|
|
|
|
|
|
|
|
return tuple(
|
|
|
|
|
other_net_id
|
|
|
|
|
for other_net_id in cached_net_ids
|
|
|
|
|
if _bounds_overlap(bounds, dynamic_paths.net_envelopes[other_net_id])
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def check_move_congestion(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
net_id: str,
|
|
|
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
broad_phase_cache: dict[tuple[str, int, int, int, int], dict[str, tuple[int, ...]]] | None = None,
|
|
|
|
|
) -> int:
|
|
|
|
|
return self.check_move_congestion_detail(
|
|
|
|
|
result,
|
|
|
|
|
net_id,
|
|
|
|
|
net_envelope_cache=net_envelope_cache,
|
|
|
|
|
grid_net_cache=grid_net_cache,
|
|
|
|
|
broad_phase_cache=broad_phase_cache,
|
|
|
|
|
).soft_overlap_count
|
|
|
|
|
|
|
|
|
|
def check_move_congestion_detail(
|
|
|
|
|
self,
|
|
|
|
|
result: ComponentResult,
|
|
|
|
|
net_id: str,
|
|
|
|
|
*,
|
|
|
|
|
frozen_net_ids: frozenset[str] = frozenset(),
|
|
|
|
|
net_envelope_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
grid_net_cache: dict[tuple[str, int, int, int, int], tuple[str, ...]] | None = None,
|
|
|
|
|
broad_phase_cache: dict[tuple[str, int, int, int, int], dict[str, tuple[int, ...]]] | None = None,
|
|
|
|
|
) -> DynamicCongestionDetail:
|
2026-03-31 17:26:00 -07:00
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_congestion_check_calls += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
if not dynamic_paths.geometries:
|
2026-04-01 21:29:23 -07:00
|
|
|
return DynamicCongestionDetail()
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
candidates_by_net = self._collect_congestion_candidates(
|
|
|
|
|
result,
|
|
|
|
|
net_id,
|
|
|
|
|
net_envelope_cache,
|
|
|
|
|
grid_net_cache,
|
|
|
|
|
broad_phase_cache,
|
|
|
|
|
)
|
|
|
|
|
if not candidates_by_net:
|
|
|
|
|
return DynamicCongestionDetail()
|
|
|
|
|
return self._check_real_congestion(
|
|
|
|
|
result,
|
|
|
|
|
candidates_by_net,
|
|
|
|
|
frozen_net_ids=frozen_net_ids,
|
|
|
|
|
)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
2026-04-02 14:39:39 -07:00
|
|
|
def verify_path_details(
|
|
|
|
|
self,
|
|
|
|
|
net_id: str,
|
|
|
|
|
components: Sequence[ComponentResult],
|
|
|
|
|
*,
|
|
|
|
|
capture_component_conflicts: bool = False,
|
|
|
|
|
) -> PathVerificationDetail:
|
2026-03-31 17:26:00 -07:00
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_verify_path_report_calls += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
static_collision_count = 0
|
|
|
|
|
dynamic_collision_count = 0
|
|
|
|
|
self_collision_count = 0
|
|
|
|
|
total_length = sum(component.length for component in components)
|
2026-11-27 15:21:37 -08:00
|
|
|
valid_geometry, continuous = check_path_geometry(components)
|
|
|
|
|
if not valid_geometry:
|
|
|
|
|
return PathVerificationDetail(RoutingReport(total_length=total_length, invalid_geometry=True))
|
2026-04-01 21:29:23 -07:00
|
|
|
conflicting_net_ids: set[str] = set()
|
2026-04-02 14:39:39 -07:00
|
|
|
component_conflicts: set[tuple[int, str, int]] = set()
|
2026-03-29 20:35:58 -07:00
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
static_obstacles = self._static_obstacles
|
|
|
|
|
dynamic_paths = self._dynamic_paths
|
|
|
|
|
|
|
|
|
|
self._ensure_static_raw_tree()
|
|
|
|
|
if static_obstacles.raw_tree is not None:
|
|
|
|
|
raw_geometries = static_obstacles.raw_tree.geometries
|
|
|
|
|
for component in components:
|
|
|
|
|
for polygon in component.physical_geometry:
|
2026-03-31 17:26:00 -07:00
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_verify_static_buffer_ops += 1
|
2026-03-30 23:54:30 -07:00
|
|
|
buffered = polygon.buffer(self.clearance, join_style="mitre")
|
2026-03-30 15:32:29 -07:00
|
|
|
hits = static_obstacles.raw_tree.query(buffered, predicate="intersects")
|
|
|
|
|
for hit_idx in hits:
|
|
|
|
|
obstacle = raw_geometries[hit_idx]
|
|
|
|
|
if buffered.touches(obstacle):
|
|
|
|
|
continue
|
2026-11-26 11:57:35 -08:00
|
|
|
# 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
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
obj_id = static_obstacles.raw_obj_ids[hit_idx]
|
|
|
|
|
if not self._is_in_safety_zone(polygon, obj_id, None, None):
|
|
|
|
|
static_collision_count += 1
|
|
|
|
|
|
2026-11-27 14:12:40 -08:00
|
|
|
if dynamic_paths.has_physical_paths():
|
|
|
|
|
clearance = max(0.0, self.clearance)
|
2026-04-02 14:39:39 -07:00
|
|
|
for component_index, component in enumerate(components):
|
2026-11-27 14:12:40 -08:00
|
|
|
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)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
for index, component in enumerate(components):
|
2026-11-27 15:51:53 -08:00
|
|
|
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):
|
2026-03-30 15:32:29 -07:00
|
|
|
self_collision_count += 1
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
return PathVerificationDetail(
|
|
|
|
|
report=RoutingReport(
|
|
|
|
|
static_collision_count=static_collision_count,
|
|
|
|
|
dynamic_collision_count=dynamic_collision_count,
|
|
|
|
|
self_collision_count=self_collision_count,
|
|
|
|
|
total_length=total_length,
|
2026-11-27 14:12:40 -08:00
|
|
|
out_of_bounds=self.board is not None and not path_within_board(components, self.board),
|
2026-11-27 15:21:37 -08:00
|
|
|
discontinuous=not continuous,
|
2026-04-01 21:29:23 -07:00
|
|
|
),
|
|
|
|
|
conflicting_net_ids=tuple(sorted(conflicting_net_ids)),
|
2026-04-02 14:39:39 -07:00
|
|
|
component_conflicts=tuple(sorted(component_conflicts)),
|
2026-03-30 15:32:29 -07:00
|
|
|
)
|
|
|
|
|
|
2026-04-01 21:29:23 -07:00
|
|
|
def verify_path_report(self, net_id: str, components: Sequence[ComponentResult]) -> RoutingReport:
|
|
|
|
|
return self.verify_path_details(net_id, components).report
|
|
|
|
|
|
2026-03-30 15:32:29 -07:00
|
|
|
def ray_cast(
|
|
|
|
|
self,
|
|
|
|
|
origin: Port,
|
|
|
|
|
angle_deg: float,
|
|
|
|
|
max_dist: float = 2000.0,
|
|
|
|
|
net_width: float | None = None,
|
2026-03-31 17:41:15 -07:00
|
|
|
caller: str = "other",
|
2026-03-30 15:32:29 -07:00
|
|
|
) -> float:
|
2026-03-31 17:26:00 -07:00
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_ray_cast_calls += 1
|
2026-03-31 17:41:15 -07:00
|
|
|
if caller == "straight_static":
|
|
|
|
|
self.metrics.total_ray_cast_calls_straight_static += 1
|
|
|
|
|
elif caller == "expand_snap":
|
|
|
|
|
self.metrics.total_ray_cast_calls_expand_snap += 1
|
|
|
|
|
elif caller == "expand_forward":
|
|
|
|
|
self.metrics.total_ray_cast_calls_expand_forward += 1
|
|
|
|
|
elif caller == "visibility_build":
|
|
|
|
|
self.metrics.total_ray_cast_calls_visibility_build += 1
|
|
|
|
|
elif caller == "visibility_query":
|
|
|
|
|
self.metrics.total_ray_cast_calls_visibility_query += 1
|
|
|
|
|
elif caller == "visibility_tangent":
|
|
|
|
|
self.metrics.total_ray_cast_calls_visibility_tangent += 1
|
|
|
|
|
else:
|
|
|
|
|
self.metrics.total_ray_cast_calls_other += 1
|
2026-11-26 19:58:46 -08:00
|
|
|
|
|
|
|
|
# Static obstacle changes clear these results along with the trees.
|
|
|
|
|
cache = self._static_obstacles.ray_cast_cache
|
|
|
|
|
# Equal NumPy and Python scalars can produce different ray geometry.
|
|
|
|
|
key = (
|
|
|
|
|
origin.x, origin.y, angle_deg, max_dist, net_width, self.clearance,
|
|
|
|
|
type(angle_deg), type(max_dist), type(net_width), type(self.clearance),
|
|
|
|
|
)
|
|
|
|
|
reach = cache.get(key)
|
|
|
|
|
if reach is not None:
|
|
|
|
|
return reach
|
|
|
|
|
|
|
|
|
|
reach = self._cast_static_ray(origin, angle_deg, max_dist, net_width)
|
|
|
|
|
if len(cache) >= _RAY_CAST_CACHE_LIMIT:
|
|
|
|
|
cache.clear()
|
|
|
|
|
cache[key] = reach
|
|
|
|
|
return reach
|
|
|
|
|
|
|
|
|
|
def _cast_static_ray(
|
|
|
|
|
self,
|
|
|
|
|
origin: Port,
|
|
|
|
|
angle_deg: float,
|
|
|
|
|
max_dist: float,
|
|
|
|
|
net_width: float | None,
|
|
|
|
|
) -> float:
|
2026-03-30 15:32:29 -07:00
|
|
|
static_obstacles = self._static_obstacles
|
2026-03-30 23:54:30 -07:00
|
|
|
tree: STRtree | None
|
|
|
|
|
is_rect_array: numpy.ndarray | None
|
|
|
|
|
bounds_array: numpy.ndarray | None
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
radians = numpy.radians(angle_deg)
|
|
|
|
|
cos_v, sin_v = numpy.cos(radians), numpy.sin(radians)
|
|
|
|
|
dx, dy = max_dist * cos_v, max_dist * sin_v
|
|
|
|
|
min_x, max_x = sorted([origin.x, origin.x + dx])
|
|
|
|
|
min_y, max_y = sorted([origin.y, origin.y + dy])
|
|
|
|
|
|
|
|
|
|
if net_width is not None:
|
|
|
|
|
tree = self._ensure_net_static_tree(net_width)
|
|
|
|
|
key = (round(net_width, 4), round(self.clearance, 4))
|
|
|
|
|
is_rect_array = static_obstacles.net_specific_is_rect[key]
|
|
|
|
|
bounds_array = static_obstacles.net_specific_bounds[key]
|
|
|
|
|
else:
|
|
|
|
|
self._ensure_static_tree()
|
|
|
|
|
tree = static_obstacles.tree
|
|
|
|
|
is_rect_array = static_obstacles.is_rect_array
|
|
|
|
|
bounds_array = static_obstacles.bounds_array
|
|
|
|
|
|
2026-03-30 23:54:30 -07:00
|
|
|
if tree is None or is_rect_array is None or bounds_array is None:
|
2026-03-30 15:32:29 -07:00
|
|
|
return max_dist
|
|
|
|
|
|
|
|
|
|
candidates = tree.query(box(min_x, min_y, max_x, max_y))
|
|
|
|
|
if candidates.size == 0:
|
|
|
|
|
return max_dist
|
2026-03-31 17:26:00 -07:00
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_ray_cast_candidate_bounds += int(candidates.size)
|
2026-03-30 15:32:29 -07:00
|
|
|
|
|
|
|
|
min_dist = max_dist
|
|
|
|
|
inv_dx = 1.0 / dx if abs(dx) > 1e-12 else 1e30
|
|
|
|
|
inv_dy = 1.0 / dy if abs(dy) > 1e-12 else 1e30
|
|
|
|
|
tree_geometries = tree.geometries
|
|
|
|
|
ray_line = None
|
|
|
|
|
|
|
|
|
|
candidates_bounds = bounds_array[candidates]
|
|
|
|
|
dist_sq = (candidates_bounds[:, 0] - origin.x) ** 2 + (candidates_bounds[:, 1] - origin.y) ** 2
|
|
|
|
|
sorted_indices = numpy.argsort(dist_sq)
|
|
|
|
|
|
|
|
|
|
for idx in sorted_indices:
|
|
|
|
|
candidate_id = candidates[idx]
|
|
|
|
|
bounds = bounds_array[candidate_id]
|
|
|
|
|
|
|
|
|
|
if abs(dx) < 1e-12:
|
|
|
|
|
if origin.x < bounds[0] or origin.x > bounds[2]:
|
|
|
|
|
tx_min, tx_max = 1e30, -1e30
|
|
|
|
|
else:
|
|
|
|
|
tx_min, tx_max = -1e30, 1e30
|
|
|
|
|
else:
|
|
|
|
|
t1, t2 = (bounds[0] - origin.x) * inv_dx, (bounds[2] - origin.x) * inv_dx
|
|
|
|
|
tx_min, tx_max = min(t1, t2), max(t1, t2)
|
|
|
|
|
|
|
|
|
|
if abs(dy) < 1e-12:
|
|
|
|
|
if origin.y < bounds[1] or origin.y > bounds[3]:
|
|
|
|
|
ty_min, ty_max = 1e30, -1e30
|
|
|
|
|
else:
|
|
|
|
|
ty_min, ty_max = -1e30, 1e30
|
|
|
|
|
else:
|
|
|
|
|
t1, t2 = (bounds[1] - origin.y) * inv_dy, (bounds[3] - origin.y) * inv_dy
|
|
|
|
|
ty_min, ty_max = min(t1, t2), max(t1, t2)
|
|
|
|
|
|
|
|
|
|
t_min, t_max = max(tx_min, ty_min), min(tx_max, ty_max)
|
|
|
|
|
if t_max < 0 or t_min > t_max or t_min > 1.0:
|
|
|
|
|
continue
|
|
|
|
|
if t_min * max_dist >= min_dist:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if is_rect_array[candidate_id]:
|
|
|
|
|
min_dist = max(0.0, t_min * max_dist)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if ray_line is None:
|
|
|
|
|
ray_line = LineString([(origin.x, origin.y), (origin.x + dx, origin.y + dy)])
|
|
|
|
|
|
|
|
|
|
obstacle = tree_geometries[candidate_id]
|
2026-03-31 17:26:00 -07:00
|
|
|
if self.metrics is not None:
|
|
|
|
|
self.metrics.total_ray_cast_exact_geometry_checks += 1
|
2026-03-30 15:32:29 -07:00
|
|
|
if not obstacle.intersects(ray_line):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
intersection = ray_line.intersection(obstacle)
|
|
|
|
|
if intersection.is_empty:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
distance = _intersection_distance(origin, intersection)
|
|
|
|
|
min_dist = min(min_dist, distance)
|
|
|
|
|
|
|
|
|
|
return min_dist
|