rework structure of everything
This commit is contained in:
parent
dcc4d6436c
commit
941d3e01df
1 changed files with 3819 additions and 3559 deletions
|
|
@ -3,67 +3,69 @@ from __future__ import annotations
|
|||
from typing import TYPE_CHECKING, Literal
|
||||
|
||||
import numpy
|
||||
from shapely.geometry import LineString, box
|
||||
|
||||
from inire.geometry.collision_query_checker import CollisionQueryChecker
|
||||
from inire.geometry.dynamic_congestion_checker import DynamicCongestionChecker
|
||||
from inire.geometry.component_overlap import components_overlap
|
||||
from inire.geometry.dynamic_path_index import DynamicPathIndex
|
||||
from inire.geometry.path_verifier import PathVerificationReport, PathVerifier
|
||||
from inire.geometry.ray_caster import RayCaster
|
||||
from inire.geometry.index_helpers import grid_cell_span
|
||||
from inire.geometry.static_obstacle_index import StaticObstacleIndex
|
||||
from inire.geometry.static_move_checker import StaticMoveChecker
|
||||
from inire.router.results import RoutingReport
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterable, Sequence
|
||||
|
||||
from shapely.geometry import Polygon
|
||||
from shapely.geometry.base import BaseGeometry
|
||||
from shapely.strtree import STRtree
|
||||
|
||||
from inire.geometry.components import ComponentResult
|
||||
from inire.geometry.primitives import Port
|
||||
|
||||
|
||||
class CollisionEngine:
|
||||
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))
|
||||
|
||||
|
||||
class RoutingWorld:
|
||||
"""
|
||||
Manages spatial queries for collision detection with unified dilation logic.
|
||||
Internal spatial state for collision detection, congestion, and verification.
|
||||
"""
|
||||
|
||||
__slots__ = (
|
||||
'clearance', 'max_net_width', 'safety_zone_radius',
|
||||
'metrics', 'grid_cell_size', '_inv_grid_cell_size', '_dynamic_bounds_array',
|
||||
'_path_verifier', '_dynamic_paths', '_static_obstacles', '_ray_caster', '_static_move_checker',
|
||||
'_dynamic_congestion_checker', '_collision_query_checker',
|
||||
"clearance",
|
||||
"max_net_width",
|
||||
"safety_zone_radius",
|
||||
"metrics",
|
||||
"grid_cell_size",
|
||||
"_dynamic_paths",
|
||||
"_static_obstacles",
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
clearance: float,
|
||||
max_net_width: float = 2.0,
|
||||
safety_zone_radius: float = 0.0021,
|
||||
) -> None:
|
||||
self,
|
||||
clearance: float,
|
||||
max_net_width: float = 2.0,
|
||||
safety_zone_radius: float = 0.0021,
|
||||
) -> None:
|
||||
self.clearance = clearance
|
||||
self.max_net_width = max_net_width
|
||||
self.safety_zone_radius = safety_zone_radius
|
||||
|
||||
self.grid_cell_size = 50.0
|
||||
self._inv_grid_cell_size = 1.0 / self.grid_cell_size
|
||||
self._static_obstacles = StaticObstacleIndex(self)
|
||||
|
||||
self._dynamic_bounds_array = numpy.array([], dtype=numpy.float64).reshape(0, 4)
|
||||
self._dynamic_paths = DynamicPathIndex(self)
|
||||
|
||||
self.metrics = {
|
||||
'static_cache_hits': 0,
|
||||
'static_grid_skips': 0,
|
||||
'static_tree_queries': 0,
|
||||
'static_straight_fast': 0,
|
||||
'congestion_grid_skips': 0,
|
||||
'congestion_tree_queries': 0,
|
||||
'safety_zone_checks': 0
|
||||
"static_cache_hits": 0,
|
||||
"static_grid_skips": 0,
|
||||
"static_tree_queries": 0,
|
||||
"static_straight_fast": 0,
|
||||
"congestion_grid_skips": 0,
|
||||
"congestion_tree_queries": 0,
|
||||
"safety_zone_checks": 0,
|
||||
}
|
||||
self._path_verifier = PathVerifier(self)
|
||||
self._ray_caster = RayCaster(self)
|
||||
self._static_move_checker = StaticMoveChecker(self)
|
||||
self._dynamic_congestion_checker = DynamicCongestionChecker(self)
|
||||
self._collision_query_checker = CollisionQueryChecker(self)
|
||||
|
||||
def get_static_version(self) -> int:
|
||||
return self._static_obstacles.version
|
||||
|
|
@ -89,23 +91,22 @@ class CollisionEngine:
|
|||
return self._dynamic_paths.geometries.values()
|
||||
|
||||
def reset_metrics(self) -> None:
|
||||
for k in self.metrics:
|
||||
self.metrics[k] = 0
|
||||
for key in self.metrics:
|
||||
self.metrics[key] = 0
|
||||
|
||||
def get_metrics_summary(self) -> str:
|
||||
m = self.metrics
|
||||
return (f"Collision Performance: \n"
|
||||
f" Static: {m['static_tree_queries']} checks\n"
|
||||
f" Congestion: {m['congestion_tree_queries']} checks\n"
|
||||
f" Safety Zone: {m['safety_zone_checks']} full intersections performed")
|
||||
metrics = self.metrics
|
||||
return (
|
||||
"Collision Performance: \n"
|
||||
f" Static: {metrics['static_tree_queries']} checks\n"
|
||||
f" Congestion: {metrics['congestion_tree_queries']} checks\n"
|
||||
f" Safety Zone: {metrics['safety_zone_checks']} full intersections performed"
|
||||
)
|
||||
|
||||
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 remove_static_obstacle(self, obj_id: int) -> None:
|
||||
"""
|
||||
Remove a static obstacle by ID.
|
||||
"""
|
||||
self._static_obstacles.remove_obstacle(obj_id)
|
||||
|
||||
def _invalidate_static_caches(self) -> None:
|
||||
|
|
@ -115,9 +116,6 @@ class CollisionEngine:
|
|||
self._static_obstacles.ensure_tree()
|
||||
|
||||
def _ensure_net_static_tree(self, net_width: float) -> STRtree:
|
||||
"""
|
||||
Lazily generate a tree where obstacles are dilated by (net_width/2 + clearance).
|
||||
"""
|
||||
return self._static_obstacles.ensure_net_tree(net_width)
|
||||
|
||||
def _ensure_static_raw_tree(self) -> None:
|
||||
|
|
@ -125,7 +123,6 @@ class CollisionEngine:
|
|||
|
||||
def _ensure_dynamic_tree(self) -> None:
|
||||
self._dynamic_paths.ensure_tree()
|
||||
self._dynamic_bounds_array = self._dynamic_paths.bounds_array
|
||||
|
||||
def _ensure_dynamic_grid(self) -> None:
|
||||
self._dynamic_paths.ensure_grid()
|
||||
|
|
@ -134,45 +131,28 @@ class CollisionEngine:
|
|||
self._dynamic_paths.tree = None
|
||||
self._ensure_dynamic_tree()
|
||||
|
||||
def add_path(self, net_id: str, geometry: list[Polygon], dilated_geometry: list[Polygon] | None = None) -> None:
|
||||
def add_path(self, net_id: str, geometry: Sequence[Polygon], dilated_geometry: Sequence[Polygon]) -> None:
|
||||
self._dynamic_paths.add_path(net_id, geometry, dilated_geometry=dilated_geometry)
|
||||
|
||||
def remove_path(self, net_id: str) -> None:
|
||||
self._dynamic_paths.remove_path(net_id)
|
||||
|
||||
def lock_net(self, net_id: str) -> None:
|
||||
""" Convert a routed net into static obstacles. """
|
||||
self._dynamic_paths.lock_net(net_id)
|
||||
|
||||
def unlock_net(self, net_id: str) -> None:
|
||||
self._dynamic_paths.unlock_net(net_id)
|
||||
|
||||
def check_move_straight_static(self, start_port: Port, length: float, net_width: float) -> bool:
|
||||
return self._static_move_checker.check_move_straight_static(start_port, length, net_width)
|
||||
self.metrics["static_straight_fast"] += 1
|
||||
reach = self.ray_cast(start_port, start_port.r, max_dist=length + 0.01, net_width=net_width)
|
||||
return reach < length - 0.001
|
||||
|
||||
def _is_in_safety_zone_fast(self, idx: int, start_port: Port | None, end_port: Port | None) -> bool:
|
||||
return self._static_move_checker.is_in_safety_zone_fast(idx, start_port, end_port)
|
||||
|
||||
def check_move_static(
|
||||
self,
|
||||
result: ComponentResult,
|
||||
start_port: Port | None = None,
|
||||
end_port: Port | None = None,
|
||||
net_width: float | None = None,
|
||||
) -> bool:
|
||||
return self._static_move_checker.check_move_static(
|
||||
result,
|
||||
start_port=start_port,
|
||||
end_port=end_port,
|
||||
net_width=net_width,
|
||||
bounds = self._static_obstacles.bounds_array[idx]
|
||||
safety_zone = self.safety_zone_radius
|
||||
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:
|
||||
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
|
||||
)
|
||||
|
||||
def check_move_congestion(self, result: ComponentResult, net_id: str) -> int:
|
||||
return self._dynamic_congestion_checker.check_move_congestion(result, net_id)
|
||||
|
||||
def _check_real_congestion(self, result: ComponentResult, net_id: str) -> int:
|
||||
return self._dynamic_congestion_checker.check_real_congestion(result, net_id)
|
||||
|
||||
def _is_in_safety_zone(
|
||||
self,
|
||||
geometry: Polygon,
|
||||
|
|
@ -180,7 +160,247 @@ class CollisionEngine:
|
|||
start_port: Port | None,
|
||||
end_port: Port | None,
|
||||
) -> bool:
|
||||
return self._static_move_checker.is_in_safety_zone(geometry, obj_id, start_port, end_port)
|
||||
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
|
||||
|
||||
self.metrics["safety_zone_checks"] += 1
|
||||
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,
|
||||
net_width: float | None = None,
|
||||
) -> bool:
|
||||
del net_width
|
||||
|
||||
static_obstacles = self._static_obstacles
|
||||
if not static_obstacles.dilated:
|
||||
return False
|
||||
|
||||
self.metrics["static_tree_queries"] += 1
|
||||
self._ensure_static_tree()
|
||||
|
||||
hits = static_obstacles.tree.query(box(*result.total_dilated_bounds))
|
||||
if hits.size == 0:
|
||||
return False
|
||||
|
||||
static_bounds = static_obstacles.bounds_array
|
||||
move_poly_bounds = result.dilated_bounds
|
||||
for hit_idx in hits:
|
||||
obstacle_bounds = static_bounds[hit_idx]
|
||||
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
|
||||
|
||||
def _check_real_congestion(self, result: ComponentResult, net_id: str) -> int:
|
||||
dynamic_paths = self._dynamic_paths
|
||||
self.metrics["congestion_tree_queries"] += 1
|
||||
self._ensure_dynamic_tree()
|
||||
if dynamic_paths.tree is None:
|
||||
return 0
|
||||
|
||||
total_bounds = result.total_dilated_bounds
|
||||
dynamic_bounds = dynamic_paths.bounds_array
|
||||
possible_total = (
|
||||
(total_bounds[0] < dynamic_bounds[:, 2])
|
||||
& (total_bounds[2] > dynamic_bounds[:, 0])
|
||||
& (total_bounds[1] < dynamic_bounds[:, 3])
|
||||
& (total_bounds[3] > dynamic_bounds[:, 1])
|
||||
)
|
||||
|
||||
valid_hits_mask = dynamic_paths.net_ids_array != net_id
|
||||
if not numpy.any(possible_total & valid_hits_mask):
|
||||
return 0
|
||||
|
||||
geometries_to_test = result.dilated_collision_geometry
|
||||
res_indices, tree_indices = dynamic_paths.tree.query(geometries_to_test, predicate="intersects")
|
||||
if tree_indices.size == 0:
|
||||
return 0
|
||||
|
||||
hit_net_ids = numpy.take(dynamic_paths.net_ids_array, tree_indices)
|
||||
unique_other_nets = numpy.unique(hit_net_ids[hit_net_ids != net_id])
|
||||
if unique_other_nets.size == 0:
|
||||
return 0
|
||||
|
||||
tree_geometries = dynamic_paths.tree.geometries
|
||||
real_hits_count = 0
|
||||
for other_net_id in unique_other_nets:
|
||||
other_mask = hit_net_ids == other_net_id
|
||||
sub_tree_indices = tree_indices[other_mask]
|
||||
sub_res_indices = res_indices[other_mask]
|
||||
|
||||
found_real = False
|
||||
for index in range(len(sub_tree_indices)):
|
||||
test_geometry = geometries_to_test[sub_res_indices[index]]
|
||||
tree_geometry = tree_geometries[sub_tree_indices[index]]
|
||||
if not test_geometry.touches(tree_geometry) and test_geometry.intersection(tree_geometry).area > 1e-7:
|
||||
found_real = True
|
||||
break
|
||||
|
||||
if found_real:
|
||||
real_hits_count += 1
|
||||
|
||||
return real_hits_count
|
||||
|
||||
def check_move_congestion(self, result: ComponentResult, net_id: str) -> int:
|
||||
dynamic_paths = self._dynamic_paths
|
||||
if not dynamic_paths.geometries:
|
||||
return 0
|
||||
|
||||
total_bounds = result.total_dilated_bounds
|
||||
self._ensure_dynamic_grid()
|
||||
dynamic_grid = dynamic_paths.grid
|
||||
if not dynamic_grid:
|
||||
return 0
|
||||
|
||||
gx_min, gy_min, gx_max, gy_max = grid_cell_span(total_bounds, self.grid_cell_size)
|
||||
|
||||
if gx_min == gx_max and gy_min == gy_max:
|
||||
cell = (gx_min, gy_min)
|
||||
if cell in dynamic_grid:
|
||||
for obj_id in dynamic_grid[cell]:
|
||||
if dynamic_paths.geometries[obj_id][0] != net_id:
|
||||
return self._check_real_congestion(result, net_id)
|
||||
return 0
|
||||
|
||||
any_possible = False
|
||||
for gx in range(gx_min, gx_max + 1):
|
||||
for gy in range(gy_min, gy_max + 1):
|
||||
cell = (gx, gy)
|
||||
if cell in dynamic_grid:
|
||||
for obj_id in dynamic_grid[cell]:
|
||||
if dynamic_paths.geometries[obj_id][0] != net_id:
|
||||
any_possible = True
|
||||
break
|
||||
if any_possible:
|
||||
break
|
||||
if any_possible:
|
||||
break
|
||||
|
||||
if not any_possible:
|
||||
return 0
|
||||
return self._check_real_congestion(result, net_id)
|
||||
|
||||
def _check_static_collision(
|
||||
self,
|
||||
geometry: Polygon,
|
||||
start_port: Port | None = None,
|
||||
end_port: Port | None = None,
|
||||
dilated_geometry: Polygon | None = None,
|
||||
) -> bool:
|
||||
static_obstacles = self._static_obstacles
|
||||
self._ensure_static_tree()
|
||||
if static_obstacles.tree is None:
|
||||
return False
|
||||
|
||||
if dilated_geometry is not None:
|
||||
test_geometry = dilated_geometry
|
||||
else:
|
||||
distance = self.clearance / 2.0
|
||||
test_geometry = geometry.buffer(distance + 1e-7, join_style=2) if distance > 0 else geometry
|
||||
|
||||
hits = static_obstacles.tree.query(test_geometry, predicate="intersects")
|
||||
tree_geometries = static_obstacles.tree.geometries
|
||||
for hit_idx in hits:
|
||||
if test_geometry.touches(tree_geometries[hit_idx]):
|
||||
continue
|
||||
obj_id = static_obstacles.obj_ids[hit_idx]
|
||||
if self._is_in_safety_zone(geometry, obj_id, start_port, end_port):
|
||||
continue
|
||||
return True
|
||||
return False
|
||||
|
||||
def _check_dynamic_collision(
|
||||
self,
|
||||
geometry: Polygon,
|
||||
net_id: str,
|
||||
dilated_geometry: Polygon | None = None,
|
||||
) -> int:
|
||||
dynamic_paths = self._dynamic_paths
|
||||
self._ensure_dynamic_tree()
|
||||
if dynamic_paths.tree is None:
|
||||
return 0
|
||||
|
||||
test_geometry = dilated_geometry if dilated_geometry else geometry.buffer(self.clearance / 2.0)
|
||||
hits = dynamic_paths.tree.query(test_geometry, predicate="intersects")
|
||||
tree_geometries = dynamic_paths.tree.geometries
|
||||
hit_net_ids: list[str] = []
|
||||
for hit_idx in hits:
|
||||
if test_geometry.touches(tree_geometries[hit_idx]):
|
||||
continue
|
||||
obj_id = dynamic_paths.obj_ids[hit_idx]
|
||||
other_net_id = dynamic_paths.geometries[obj_id][0]
|
||||
if other_net_id != net_id:
|
||||
hit_net_ids.append(other_net_id)
|
||||
if not hit_net_ids:
|
||||
return 0
|
||||
return len(numpy.unique(hit_net_ids))
|
||||
|
||||
def check_collision(
|
||||
self,
|
||||
|
|
@ -193,16 +413,16 @@ class CollisionEngine:
|
|||
bounds: tuple[float, float, float, float] | None = None,
|
||||
net_width: float | None = None,
|
||||
) -> bool | int:
|
||||
return self._collision_query_checker.check_collision(
|
||||
geometry,
|
||||
net_id,
|
||||
buffer_mode=buffer_mode,
|
||||
start_port=start_port,
|
||||
end_port=end_port,
|
||||
dilated_geometry=dilated_geometry,
|
||||
bounds=bounds,
|
||||
net_width=net_width,
|
||||
)
|
||||
del bounds, net_width
|
||||
|
||||
if buffer_mode == "static":
|
||||
return self._check_static_collision(
|
||||
geometry,
|
||||
start_port=start_port,
|
||||
end_port=end_port,
|
||||
dilated_geometry=dilated_geometry,
|
||||
)
|
||||
return self._check_dynamic_collision(geometry, net_id, dilated_geometry=dilated_geometry)
|
||||
|
||||
def is_collision(
|
||||
self,
|
||||
|
|
@ -212,7 +432,6 @@ class CollisionEngine:
|
|||
start_port: Port | None = None,
|
||||
end_port: Port | None = None,
|
||||
) -> bool:
|
||||
""" Unified entry point for static collision checks. """
|
||||
result = self.check_collision(
|
||||
geometry,
|
||||
net_id,
|
||||
|
|
@ -223,12 +442,157 @@ class CollisionEngine:
|
|||
)
|
||||
return bool(result)
|
||||
|
||||
def verify_path_report(self, net_id: str, components: list[ComponentResult]) -> PathVerificationReport:
|
||||
return self._path_verifier.verify_path_report(net_id, components)
|
||||
def verify_path_report(self, net_id: str, components: Sequence[ComponentResult]) -> RoutingReport:
|
||||
static_collision_count = 0
|
||||
dynamic_collision_count = 0
|
||||
self_collision_count = 0
|
||||
total_length = sum(component.length for component in components)
|
||||
|
||||
def verify_path(self, net_id: str, components: list[ComponentResult]) -> tuple[bool, int]:
|
||||
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:
|
||||
buffered = polygon.buffer(self.clearance, join_style=2)
|
||||
hits = static_obstacles.raw_tree.query(buffered, predicate="intersects")
|
||||
for hit_idx in hits:
|
||||
obstacle = raw_geometries[hit_idx]
|
||||
if buffered.touches(obstacle):
|
||||
continue
|
||||
|
||||
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
|
||||
|
||||
self._ensure_dynamic_tree()
|
||||
if dynamic_paths.tree is not None:
|
||||
tree_geometries = dynamic_paths.tree.geometries
|
||||
for component in components:
|
||||
test_geometries = component.dilated_physical_geometry
|
||||
res_indices, tree_indices = dynamic_paths.tree.query(test_geometries, predicate="intersects")
|
||||
if tree_indices.size == 0:
|
||||
continue
|
||||
|
||||
hit_net_ids = numpy.take(dynamic_paths.net_ids_array, tree_indices)
|
||||
component_hits = []
|
||||
for index in range(len(tree_indices)):
|
||||
if hit_net_ids[index] == str(net_id):
|
||||
continue
|
||||
|
||||
new_geometry = test_geometries[res_indices[index]]
|
||||
tree_geometry = tree_geometries[tree_indices[index]]
|
||||
if not new_geometry.touches(tree_geometry) and new_geometry.intersection(tree_geometry).area > 1e-7:
|
||||
component_hits.append(hit_net_ids[index])
|
||||
|
||||
if component_hits:
|
||||
dynamic_collision_count += len(numpy.unique(component_hits))
|
||||
|
||||
for index, component in enumerate(components):
|
||||
for other_index in range(index + 2, len(components)):
|
||||
if components_overlap(component, components[other_index], prefer_actual=True):
|
||||
self_collision_count += 1
|
||||
|
||||
return RoutingReport(
|
||||
static_collision_count=static_collision_count,
|
||||
dynamic_collision_count=dynamic_collision_count,
|
||||
self_collision_count=self_collision_count,
|
||||
total_length=total_length,
|
||||
)
|
||||
|
||||
def verify_path(self, net_id: str, components: Sequence[ComponentResult]) -> tuple[bool, int]:
|
||||
report = self.verify_path_report(net_id, components)
|
||||
return report.is_valid, report.collision_count
|
||||
|
||||
def ray_cast(self, origin: Port, angle_deg: float, max_dist: float = 2000.0, net_width: float | None = None) -> float:
|
||||
return self._ray_caster.ray_cast(origin, angle_deg, max_dist=max_dist, net_width=net_width)
|
||||
def ray_cast(
|
||||
self,
|
||||
origin: Port,
|
||||
angle_deg: float,
|
||||
max_dist: float = 2000.0,
|
||||
net_width: float | None = None,
|
||||
) -> float:
|
||||
static_obstacles = self._static_obstacles
|
||||
|
||||
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
|
||||
|
||||
if tree is None:
|
||||
return max_dist
|
||||
|
||||
candidates = tree.query(box(min_x, min_y, max_x, max_y))
|
||||
if candidates.size == 0:
|
||||
return max_dist
|
||||
|
||||
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]
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue