"""Observed detector hazards enter search and command admission separately. Inspired by the obstacle-layer / collision-monitor separation in Nav2. Boxes alone never create metric obstacles. Source indices bind each hazard to this range transaction. Search costs mark existing measured voxels; the independent swept-body guard tests the exact associated returns at the current pose. This is a simulation candidate, not a certified human-separation controller. """ import copy import math import numpy as np class DetectionObstacles: deadline_seconds = 2.0 # Additional reserve for detector-associated objects. Separate from the # ordinary 5 cm rigid-body margin, terrain capability and road permissions. clearance_m = 0.35 voxel_m = 0.12 def __init__(self, monitor): self.monitor = copy.copy(monitor) self.monitor.clearance_m = max(monitor.clearance_m, self.clearance_m) self.cost = monitor.max_step + 1.0 @staticmethod def observed(value): points = np.asarray(value["points"], dtype=float).reshape(-1, 3) indices = value.get("detection_point_indices", []) if (not isinstance(indices, list) or len(indices) > len(points) or len(indices) != len(set(indices)) or any(type(i) is not int or not 0 <= i < len(points) for i in indices) or not np.isfinite(points).all()): raise ValueError("Invalid detector range association") age = float(value.get("detection_age_seconds", 0)) if not math.isfinite(age) or age < 0: raise ValueError("Invalid detector capture age") return points[indices].copy() def overlay(self, terrain, detected): if not len(detected): return terrain cells = set(map(tuple, np.floor(detected / self.voxel_m).astype(np.int64))) keys = np.floor(terrain[:, :3] / self.voxel_m).astype(np.int64) marked = np.fromiter((tuple(k) in cells for k in keys), bool, len(keys)) result = terrain.copy() result[marked, 3] = np.maximum(result[marked, 3], self.cost) return result @staticmethod def without_ground(detected, permitted_terrain): """Remove only positively permitted PCL ground in this exact scan. No voxel-neighbour inference: segmentation preserves original return indices; unknown and unmatched candidates keep their object reserve. Raw range and CMU geometric costs remain untouched. """ detected = np.asarray(detected, dtype=float).reshape(-1, 3) ground = np.asarray(permitted_terrain, dtype=np.float32).reshape(-1, 3) if not np.isfinite(detected).all() or not np.isfinite(ground).all(): raise ValueError("Non-finite detection ground observation") known = set(map(tuple, ground)) keep = np.fromiter((tuple(p) not in known for p in detected.astype(np.float32)), bool, len(detected)) return detected[keep].copy() def regulate(self, speed, angular, detected, pose): # These are the original XYZ returns, not filled voxels or box centres. terrain = np.column_stack((detected, np.full(len(detected), self.cost))) return self.monitor.regulate_command(speed, angular, terrain, pose)