Bound search memory and verify process geometry before layout export
This commit is contained in:
parent
facde64f09
commit
e6ae242cb2
1 changed files with 645 additions and 102 deletions
|
|
@ -1,23 +1,27 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
from itertools import chain, pairwise
|
||||
from math import ceil, floor, hypot
|
||||
from itertools import chain, islice, pairwise
|
||||
from math import ceil, floor, hypot, isfinite
|
||||
from time import perf_counter_ns
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import numpy
|
||||
import shapely
|
||||
from scipy.spatial import cKDTree
|
||||
from shapely.geometry import box
|
||||
from shapely.geometry import Point, box
|
||||
from shapely.strtree import STRtree
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable, Iterator
|
||||
|
||||
from shapely.geometry import LinearRing, Polygon
|
||||
from numpy.typing import NDArray
|
||||
from inire.router._astar_types import AStarMetrics
|
||||
|
||||
|
||||
_COST_CACHE_SIZE = 100000
|
||||
_MAX_SAMPLES = 20000
|
||||
|
||||
|
||||
def _clip_segment(
|
||||
|
|
@ -68,7 +72,10 @@ class DangerMap:
|
|||
A proximity cost evaluator using a KD-Tree of obstacle boundary points.
|
||||
Samples the part of the obstacle perimeter within range of the design.
|
||||
"""
|
||||
__slots__ = ('minx', 'miny', 'maxx', 'maxy', 'resolution', 'safety_threshold', 'k', 'tree', '_cost_cache', 'metrics')
|
||||
__slots__ = (
|
||||
"minx", "miny", "maxx", "maxy", "resolution", "safety_threshold", "k", "tree",
|
||||
"_boundary_tree", "_cost_distance_range", "_cost_cache", "metrics",
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
|
@ -76,6 +83,8 @@ class DangerMap:
|
|||
resolution: float = 5.0,
|
||||
safety_threshold: float = 10.0,
|
||||
k: float = 1.0,
|
||||
*,
|
||||
cost_distance_range: float = 10.0,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize the Danger Map.
|
||||
|
|
@ -91,6 +100,8 @@ class DangerMap:
|
|||
self.safety_threshold = safety_threshold
|
||||
self.k = k
|
||||
self.tree: cKDTree | None = None
|
||||
self._boundary_tree: STRtree | None = None
|
||||
self._cost_distance_range = cost_distance_range
|
||||
self._cost_cache: OrderedDict[tuple[int, int], float] = OrderedDict()
|
||||
self.metrics: AStarMetrics | None = None
|
||||
|
||||
|
|
@ -101,19 +112,37 @@ class DangerMap:
|
|||
reach = self.safety_threshold
|
||||
bounds = (self.minx - reach, self.miny - reach, self.maxx + reach, self.maxy + reach)
|
||||
window = box(*bounds)
|
||||
all_points = []
|
||||
for poly in obstacles:
|
||||
for ring in chain((poly.exterior,), poly.interiors):
|
||||
if window.intersects(ring):
|
||||
all_points.extend(_sample_ring(ring, bounds, self.resolution))
|
||||
|
||||
if all_points:
|
||||
self.tree = cKDTree(numpy.array(all_points))
|
||||
else:
|
||||
rings = [
|
||||
ring for poly in obstacles for ring in chain((poly.exterior,), poly.interiors)
|
||||
if window.intersects(ring)
|
||||
]
|
||||
if any(not isfinite(ring.length / self.resolution) for ring in rings):
|
||||
self.tree = None
|
||||
self._boundary_tree = STRtree(rings)
|
||||
self._cost_cache.clear()
|
||||
return
|
||||
points = list(islice(chain.from_iterable(_sample_ring(ring, bounds, self.resolution) for ring in rings), _MAX_SAMPLES + 1))
|
||||
self.tree = cKDTree(numpy.array(points)) if 0 < len(points) <= _MAX_SAMPLES else None
|
||||
self._boundary_tree = STRtree(rings) if len(points) > _MAX_SAMPLES else None
|
||||
|
||||
self._cost_cache.clear()
|
||||
|
||||
@property
|
||||
def has_data(self) -> bool:
|
||||
return self.tree is not None or self._boundary_tree is not None
|
||||
|
||||
def costs_at_points(self, coordinates: NDArray[numpy.float64]) -> NDArray[numpy.float64]:
|
||||
distances = numpy.full(len(coordinates), numpy.inf)
|
||||
if self.tree is not None:
|
||||
distances, _ = self.tree.query(coordinates, distance_upper_bound=self.safety_threshold)
|
||||
elif self._boundary_tree is not None:
|
||||
indices, measured = self._boundary_tree.query_nearest(
|
||||
shapely.points(coordinates), max_distance=self.safety_threshold, return_distance=True, all_matches=False,
|
||||
)
|
||||
distances[indices[0]] = measured
|
||||
safe = numpy.maximum(0.1, 10 * (distances / self._cost_distance_range))
|
||||
return numpy.where(distances < self.safety_threshold, self.k / safe ** 2, 0.0)
|
||||
|
||||
def is_within_bounds(self, x: float, y: float) -> bool:
|
||||
"""
|
||||
Check if a coordinate is within the design bounds.
|
||||
|
|
@ -154,12 +183,19 @@ class DangerMap:
|
|||
qy = qy_milli / 1000.0
|
||||
if not self.is_within_bounds(qx, qy):
|
||||
return 1e15
|
||||
if self.tree is None:
|
||||
if not self.has_data:
|
||||
return 0.0
|
||||
if self.metrics is not None:
|
||||
self.metrics.total_danger_map_query_calls += 1
|
||||
dist, _ = self.tree.query([qx, qy], distance_upper_bound=self.safety_threshold)
|
||||
if self.tree is not None:
|
||||
dist, _ = self.tree.query([qx, qy], distance_upper_bound=self.safety_threshold)
|
||||
else:
|
||||
assert self._boundary_tree is not None
|
||||
_, distances = self._boundary_tree.query_nearest(
|
||||
Point(qx, qy), max_distance=self.safety_threshold, return_distance=True, all_matches=False,
|
||||
)
|
||||
dist = distances[0] if len(distances) else numpy.inf
|
||||
if dist >= self.safety_threshold:
|
||||
return 0.0
|
||||
safe_dist = max(dist, 0.1)
|
||||
safe_dist = max(10 * (dist / self._cost_distance_range), 0.1)
|
||||
return float(self.k / (safe_dist ** 2))
|
||||
|
|
|
|||
Loading…
Reference in a new issue