Query exact corner visibility on demand
Cache local queries at indexed corners and preserve exact distance limits. Keep the legacy counters available and report corner-index builds in the performance summary.
This commit is contained in:
parent
725f385ba3
commit
a6b0b9216a
1 changed files with 58 additions and 109 deletions
|
|
@ -9,19 +9,16 @@ if TYPE_CHECKING:
|
|||
from inire.geometry.collision import RoutingWorld
|
||||
from inire.geometry.primitives import Port
|
||||
|
||||
|
||||
class VisibilityManager:
|
||||
"""
|
||||
Manages corners of static obstacles for sparse A* / Visibility Graph jumps.
|
||||
"""
|
||||
"""Index static obstacle corners and query them for sparse A* moves."""
|
||||
__slots__ = (
|
||||
"collision_engine",
|
||||
"corners",
|
||||
"_exact_corners",
|
||||
"corner_index",
|
||||
"_corner_graph",
|
||||
"_point_visibility_cache",
|
||||
"_corner_index_version",
|
||||
"_corner_graph_version",
|
||||
)
|
||||
|
||||
def __init__(self, collision_engine: RoutingWorld) -> None:
|
||||
|
|
@ -29,10 +26,8 @@ class VisibilityManager:
|
|||
self.corners: list[tuple[float, float]] = []
|
||||
self._exact_corners: list[tuple[float, float]] = []
|
||||
self.corner_index = rtree.index.Index()
|
||||
self._corner_graph: dict[int, list[tuple[float, float, float]]] = {}
|
||||
self._point_visibility_cache: dict[tuple[int, int, int], list[tuple[float, float, float]]] = {}
|
||||
self._point_visibility_cache: dict[tuple[float, float, float], list[tuple[float, float, float]]] = {}
|
||||
self._corner_index_version = -1
|
||||
self._corner_graph_version = -1
|
||||
|
||||
def clear_cache(self) -> None:
|
||||
"""
|
||||
|
|
@ -41,30 +36,20 @@ class VisibilityManager:
|
|||
self.corners = []
|
||||
self._exact_corners = []
|
||||
self.corner_index = rtree.index.Index()
|
||||
self._corner_graph = {}
|
||||
self._point_visibility_cache = {}
|
||||
self._corner_index_version = -1
|
||||
self._corner_graph_version = -1
|
||||
|
||||
def ensure_corner_index_current(self) -> None:
|
||||
if self._corner_index_version != self.collision_engine.get_static_version():
|
||||
self._build_corner_index()
|
||||
|
||||
def ensure_corner_graph_current(self) -> None:
|
||||
self.ensure_corner_index_current()
|
||||
static_version = self.collision_engine.get_static_version()
|
||||
if self._corner_graph_version != static_version:
|
||||
self._build_corner_graph()
|
||||
|
||||
def _build_corner_index(self) -> None:
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_index_builds += 1
|
||||
self.corners = []
|
||||
self._exact_corners = []
|
||||
self.corner_index = rtree.index.Index()
|
||||
self._corner_graph = {}
|
||||
self._point_visibility_cache = {}
|
||||
self._corner_graph_version = -1
|
||||
self._corner_index_version = self.collision_engine.get_static_version()
|
||||
raw_corners = []
|
||||
for poly in self.collision_engine.iter_static_dilated_geometries():
|
||||
|
|
@ -92,37 +77,6 @@ class VisibilityManager:
|
|||
for i, (x, y) in enumerate(self.corners):
|
||||
self.corner_index.insert(i, (x, y, x, y))
|
||||
|
||||
def _build_corner_graph(self) -> None:
|
||||
"""
|
||||
Pre-compute corner-to-corner visibility from the current corner index.
|
||||
"""
|
||||
self.ensure_corner_index_current()
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_builds += 1
|
||||
self._corner_graph = {}
|
||||
self._corner_graph_version = self.collision_engine.get_static_version()
|
||||
|
||||
if not self.corners:
|
||||
return
|
||||
|
||||
# Pre-compute visibility graph between corners
|
||||
num_corners = len(self.corners)
|
||||
if num_corners > 200:
|
||||
# Limit pre-computation if too many corners
|
||||
return
|
||||
|
||||
for i in range(num_corners):
|
||||
self._corner_graph[i] = []
|
||||
for j in range(num_corners):
|
||||
if i == j:
|
||||
continue
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_pairs_checked += 1
|
||||
start, end = self._exact_corners[i], self._exact_corners[j]
|
||||
if self.collision_engine.corner_segment_visible(start, end):
|
||||
cx, cy = self.corners[j]
|
||||
self._corner_graph[i].append((cx, cy, hypot(end[0] - start[0], end[1] - start[1])))
|
||||
|
||||
def _corner_idx_at(self, origin: Port) -> int | None:
|
||||
self.ensure_corner_index_current()
|
||||
ox, oy = round(origin.x, 3), round(origin.y, 3)
|
||||
|
|
@ -176,42 +130,42 @@ class VisibilityManager:
|
|||
|
||||
return sorted(candidate_ids)
|
||||
|
||||
def get_point_visibility(self, origin: Port, max_dist: float = 1000.0) -> list[tuple[float, float, float]]:
|
||||
"""
|
||||
Find visible corners from an arbitrary point.
|
||||
This may test local corner segments against keepouts.
|
||||
"""
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_point_queries += 1
|
||||
self.ensure_corner_index_current()
|
||||
def get_corner_visibility(self, origin: Port, max_dist: float = 1000.0) -> list[tuple[float, float, float]]:
|
||||
"""Return visible neighbors when the origin is at an indexed corner."""
|
||||
metrics = self.collision_engine.metrics
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_corner_queries_exact += 1
|
||||
if max_dist < 0:
|
||||
return []
|
||||
|
||||
corner_idx = self._corner_idx_at(origin)
|
||||
if corner_idx is not None:
|
||||
self.ensure_corner_graph_current()
|
||||
if corner_idx is not None and corner_idx in self._corner_graph:
|
||||
return [corner for corner in self._corner_graph[corner_idx] if corner[2] <= max_dist]
|
||||
if corner_idx is None:
|
||||
return []
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_corner_hits_exact += 1
|
||||
metrics.total_visibility_point_queries += 1
|
||||
|
||||
ox, oy = round(origin.x, 3), round(origin.y, 3)
|
||||
cache_key = (int(ox * 1000), int(oy * 1000), int(round(max_dist * 1000)))
|
||||
start = self._exact_corners[corner_idx]
|
||||
cache_key = (*start, max_dist)
|
||||
if cache_key in self._point_visibility_cache:
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_point_cache_hits += 1
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_point_cache_hits += 1
|
||||
return self._point_visibility_cache[cache_key]
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_point_cache_misses += 1
|
||||
if metrics is not None:
|
||||
metrics.total_visibility_point_cache_misses += 1
|
||||
|
||||
bounds = (origin.x - max_dist, origin.y - max_dist, origin.x + max_dist, origin.y + max_dist)
|
||||
# The index stores rounded corner coordinates. Filter by exact distance below.
|
||||
reach = max_dist + 0.001
|
||||
x, y = start
|
||||
bounds = (x - reach, y - reach, x + reach, y + reach)
|
||||
candidates = sorted(self.corner_index.intersection(bounds))
|
||||
|
||||
visible = []
|
||||
start = (origin.x, origin.y) if corner_idx is None else self._exact_corners[corner_idx]
|
||||
for i in candidates:
|
||||
end = self._exact_corners[i]
|
||||
dist = hypot(end[0] - start[0], end[1] - start[1])
|
||||
dist = hypot(end[0] - x, end[1] - y)
|
||||
|
||||
if dist > max_dist or dist < 1e-3:
|
||||
if dist == 0 or dist > max_dist:
|
||||
continue
|
||||
|
||||
if self.collision_engine.corner_segment_visible(start, end):
|
||||
|
|
@ -222,19 +176,3 @@ class VisibilityManager:
|
|||
self._point_visibility_cache.clear()
|
||||
self._point_visibility_cache[cache_key] = visible
|
||||
return visible
|
||||
|
||||
def get_corner_visibility(self, origin: Port, max_dist: float = 1000.0) -> list[tuple[float, float, float]]:
|
||||
"""
|
||||
Return visible neighbors when the origin is already at a known corner.
|
||||
"""
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_queries_exact += 1
|
||||
if max_dist < 0:
|
||||
return []
|
||||
|
||||
corner_idx = self._corner_idx_at(origin)
|
||||
if corner_idx is None:
|
||||
return []
|
||||
if self.collision_engine.metrics is not None:
|
||||
self.collision_engine.metrics.total_visibility_corner_hits_exact += 1
|
||||
return self.get_point_visibility(origin, max_dist)
|
||||
|
|
|
|||
Loading…
Reference in a new issue