Fix routing clearance, retries, and S-bend verification
This commit is contained in:
parent
760182f25c
commit
196cd00445
1 changed files with 257 additions and 90 deletions
|
|
@ -1,7 +1,8 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
from itertools import chain
|
||||
from itertools import chain, pairwise
|
||||
from math import ceil, floor, hypot
|
||||
from time import perf_counter_ns
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
|
|
@ -10,17 +11,62 @@ from scipy.spatial import cKDTree
|
|||
from shapely.geometry import box
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from shapely.geometry import Polygon
|
||||
from collections.abc import Iterator
|
||||
|
||||
from shapely.geometry import LinearRing, Polygon
|
||||
from inire.router._astar_types import AStarMetrics
|
||||
|
||||
|
||||
_COST_CACHE_SIZE = 100000
|
||||
|
||||
|
||||
def _clip_segment(
|
||||
start: tuple[float, float],
|
||||
end: tuple[float, float],
|
||||
bounds: tuple[float, float, float, float],
|
||||
) -> tuple[float, float] | None:
|
||||
low, high = 0.0, 1.0
|
||||
for axis in (0, 1):
|
||||
origin = start[axis]
|
||||
step = end[axis] - origin
|
||||
minimum, maximum = bounds[axis], bounds[axis + 2]
|
||||
if not step:
|
||||
if not minimum <= origin <= maximum:
|
||||
return None
|
||||
continue
|
||||
enter, leave = sorted(((minimum - origin) / step, (maximum - origin) / step))
|
||||
low, high = max(low, enter), min(high, leave)
|
||||
return (low, high) if low <= high else None
|
||||
|
||||
|
||||
def _sample_ring(
|
||||
ring: LinearRing,
|
||||
bounds: tuple[float, float, float, float],
|
||||
resolution: float,
|
||||
) -> Iterator[tuple[float, float]]:
|
||||
distance = 0.0
|
||||
next_sample = 0
|
||||
last_sample = ceil(ring.length / resolution) - 1
|
||||
for start, end in pairwise(ring.coords):
|
||||
dx, dy = end[0] - start[0], end[1] - start[1]
|
||||
length = hypot(dx, dy)
|
||||
clipped = _clip_segment(start, end, bounds)
|
||||
if length and clipped is not None:
|
||||
low, high = clipped
|
||||
first = max(next_sample, ceil((distance + low * length) / resolution))
|
||||
last = min(last_sample, floor((distance + high * length) / resolution))
|
||||
for index in range(first, last + 1):
|
||||
fraction = (index * resolution - distance) / length
|
||||
yield (start[0] + fraction * dx, start[1] + fraction * dy)
|
||||
if first <= last:
|
||||
next_sample = last + 1
|
||||
distance += length
|
||||
|
||||
|
||||
class DangerMap:
|
||||
"""
|
||||
A proximity cost evaluator using a KD-Tree of obstacle boundary points.
|
||||
Scales with obstacle perimeter rather than design area.
|
||||
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')
|
||||
|
||||
|
|
@ -53,18 +99,13 @@ class DangerMap:
|
|||
Pre-compute the proximity tree by sampling obstacle boundaries.
|
||||
"""
|
||||
reach = self.safety_threshold
|
||||
window = box(self.minx - reach, self.miny - reach, self.maxx + reach, self.maxy + reach)
|
||||
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 not window.intersects(ring):
|
||||
continue
|
||||
length = ring.length
|
||||
dist = 0.0
|
||||
while dist < length:
|
||||
pt = ring.interpolate(dist)
|
||||
all_points.append((pt.x, pt.y))
|
||||
dist += self.resolution
|
||||
if window.intersects(ring):
|
||||
all_points.extend(_sample_ring(ring, bounds, self.resolution))
|
||||
|
||||
if all_points:
|
||||
self.tree = cKDTree(numpy.array(all_points))
|
||||
|
|
|
|||
Loading…
Reference in a new issue