Refactor routing internals and archive retry benchmarks
This commit is contained in:
parent
a8720ac8c9
commit
ea5cfe7f31
7 changed files with 3481 additions and 2583 deletions
|
|
@ -23,6 +23,9 @@ if TYPE_CHECKING:
|
|||
from inire.geometry.primitives import Port
|
||||
|
||||
|
||||
_RAY_CAST_CACHE_LIMIT = 8192
|
||||
|
||||
|
||||
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)
|
||||
|
|
@ -103,6 +106,10 @@ class RoutingWorld:
|
|||
def get_static_version(self) -> int:
|
||||
return self._static_obstacles.version
|
||||
|
||||
def iter_static_geometries(self) -> Iterable[Polygon]:
|
||||
"""Iterate over the current raw obstacles in insertion order."""
|
||||
return self._static_obstacles.geometries.values()
|
||||
|
||||
def iter_static_dilated_geometries(self) -> Iterable[Polygon]:
|
||||
return self._static_obstacles.dilated.values()
|
||||
|
||||
|
|
@ -777,6 +784,31 @@ class RoutingWorld:
|
|||
self.metrics.total_ray_cast_calls_visibility_tangent += 1
|
||||
else:
|
||||
self.metrics.total_ray_cast_calls_other += 1
|
||||
|
||||
# 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:
|
||||
static_obstacles = self._static_obstacles
|
||||
tree: STRtree | None
|
||||
is_rect_array: numpy.ndarray | None
|
||||
|
|
|
|||
|
|
@ -91,16 +91,30 @@ class ComponentResult:
|
|||
return self._total_dilated_bounds
|
||||
|
||||
def translate(self, dx: int | float, dy: int | float) -> ComponentResult:
|
||||
# The collision and physical views often reference the same polygons.
|
||||
translated: dict[int, Polygon] = {}
|
||||
|
||||
def translate_geometry(polygons: Sequence[Polygon]) -> tuple[Polygon, ...]:
|
||||
geometry = []
|
||||
for polygon in polygons:
|
||||
key = id(polygon)
|
||||
shifted = translated.get(key)
|
||||
if shifted is None:
|
||||
shifted = shapely_translate(polygon, dx, dy)
|
||||
translated[key] = shifted
|
||||
geometry.append(shifted)
|
||||
return tuple(geometry)
|
||||
|
||||
return ComponentResult(
|
||||
start_port=self.start_port.translate(dx, dy),
|
||||
collision_geometry=[shapely_translate(poly, dx, dy) for poly in self.collision_geometry],
|
||||
collision_geometry=translate_geometry(self.collision_geometry),
|
||||
end_port=self.end_port.translate(dx, dy),
|
||||
length=self.length,
|
||||
move_type=self.move_type,
|
||||
move_spec=self.move_spec,
|
||||
physical_geometry=[shapely_translate(poly, dx, dy) for poly in self.physical_geometry],
|
||||
dilated_collision_geometry=[shapely_translate(poly, dx, dy) for poly in self.dilated_collision_geometry],
|
||||
dilated_physical_geometry=[shapely_translate(poly, dx, dy) for poly in self.dilated_physical_geometry],
|
||||
physical_geometry=translate_geometry(self.physical_geometry),
|
||||
dilated_collision_geometry=translate_geometry(self.dilated_collision_geometry),
|
||||
dilated_physical_geometry=translate_geometry(self.dilated_physical_geometry),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -486,3 +500,27 @@ class SBend:
|
|||
dilated_collision_geometry=dilated_collision_geometry,
|
||||
dilated_physical_geometry=dilated_physical_geometry,
|
||||
)
|
||||
|
||||
|
||||
def build_component(
|
||||
start: Port,
|
||||
width: float,
|
||||
kind: MoveKind,
|
||||
parameters: tuple,
|
||||
*,
|
||||
collision_type: BendCollisionModel,
|
||||
physical_geometry_type: BendPhysicalGeometry,
|
||||
clip_margin: float | None,
|
||||
dilation: float,
|
||||
) -> ComponentResult:
|
||||
if kind == "straight":
|
||||
return Straight.generate(start, parameters[0], width, dilation=dilation)
|
||||
if kind == "bend90":
|
||||
return Bend90.generate(
|
||||
start, parameters[0], width, parameters[1],
|
||||
collision_type=collision_type, physical_geometry_type=physical_geometry_type, clip_margin=clip_margin, dilation=dilation,
|
||||
)
|
||||
return SBend.generate(
|
||||
start, parameters[0], parameters[1], width,
|
||||
collision_type=collision_type, physical_geometry_type=physical_geometry_type, clip_margin=clip_margin, dilation=dilation,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import numpy
|
||||
|
|
@ -153,10 +154,8 @@ class DynamicPathIndex:
|
|||
for cell in cells:
|
||||
obj_ids = self.grid.get(cell)
|
||||
if obj_ids is not None:
|
||||
try:
|
||||
with suppress(ValueError):
|
||||
obj_ids.remove(obj_id)
|
||||
except ValueError:
|
||||
pass
|
||||
if not obj_ids:
|
||||
self.grid.pop(cell, None)
|
||||
net_obj_ids = self.grid_net_obj_ids.get(cell)
|
||||
|
|
|
|||
17
inire/geometry/intervals.py
Normal file
17
inire/geometry/intervals.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
"""Intervals left open by solid spans."""
|
||||
|
||||
Gap = tuple[float, float]
|
||||
|
||||
|
||||
def open_intervals(solid: list[Gap], ymin: float, ymax: float) -> tuple[Gap, ...]:
|
||||
openings = []
|
||||
cursor = ymin
|
||||
for bottom, top in sorted(solid):
|
||||
if top <= ymin or bottom >= ymax:
|
||||
continue
|
||||
if cursor < bottom:
|
||||
openings.append((cursor, bottom))
|
||||
cursor = max(cursor, top)
|
||||
if cursor < ymax:
|
||||
openings.append((cursor, ymax))
|
||||
return tuple(openings)
|
||||
|
|
@ -63,3 +63,20 @@ ROT2_270 = numpy.array(((0, 1), (-1, 0)), dtype=numpy.int32)
|
|||
def rotation_matrix2(rotation_deg: int | float) -> NDArray[numpy.int32]:
|
||||
quadrant = (_normalize_angle(rotation_deg) // 90) % 4
|
||||
return (ROT2_0, ROT2_90, ROT2_180, ROT2_270)[quadrant]
|
||||
|
||||
|
||||
def rotate_cardinal(x: float, y: float, heading: int) -> tuple[float, float]:
|
||||
"""Rotate by a multiple of 90 degrees without snapping the point."""
|
||||
angle = heading % 360
|
||||
if angle == 0:
|
||||
return x, y
|
||||
if angle == 90:
|
||||
return -y, x
|
||||
if angle == 180:
|
||||
return -x, -y
|
||||
return y, -x
|
||||
|
||||
|
||||
def port_local_coordinates(origin: Port, x: float, y: float) -> tuple[float, float]:
|
||||
"""Project a point into the forward and leftward axes of a port."""
|
||||
return rotate_cardinal(x - origin.x, y - origin.y, -int(origin.r))
|
||||
|
|
|
|||
14
inire/geometry/sbend.py
Normal file
14
inire/geometry/sbend.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
"""Exact S-bend dimensions for constructive planners."""
|
||||
|
||||
from math import isqrt
|
||||
|
||||
|
||||
def exact_sbend_spans(radius: int) -> dict[int, int]:
|
||||
"""Map lateral offsets to their positive integer spans for a fixed radius."""
|
||||
spans = {}
|
||||
for offset in range(1, 2 * radius):
|
||||
squared = offset * (4 * radius - offset)
|
||||
span = isqrt(squared)
|
||||
if span * span == squared:
|
||||
spans[offset] = span
|
||||
return spans
|
||||
|
|
@ -30,6 +30,7 @@ class StaticObstacleIndex:
|
|||
"net_specific_trees",
|
||||
"net_specific_is_rect",
|
||||
"net_specific_bounds",
|
||||
"ray_cast_cache",
|
||||
"id_counter",
|
||||
"version",
|
||||
)
|
||||
|
|
@ -49,6 +50,7 @@ class StaticObstacleIndex:
|
|||
self.net_specific_trees: dict[tuple[float, float], STRtree] = {}
|
||||
self.net_specific_is_rect: dict[tuple[float, float], numpy.ndarray] = {}
|
||||
self.net_specific_bounds: dict[tuple[float, float], numpy.ndarray] = {}
|
||||
self.ray_cast_cache: dict[tuple[float, float, float, float, float | None, float, type, type, type, type], float] = {}
|
||||
self.id_counter = 0
|
||||
self.version = 0
|
||||
|
||||
|
|
@ -89,6 +91,7 @@ class StaticObstacleIndex:
|
|||
self.net_specific_trees.clear()
|
||||
self.net_specific_is_rect.clear()
|
||||
self.net_specific_bounds.clear()
|
||||
self.ray_cast_cache.clear()
|
||||
self.version += 1
|
||||
|
||||
def ensure_tree(self) -> None:
|
||||
|
|
|
|||
Loading…
Reference in a new issue