"""Measured passage centres for local intent, independent of waypoint attraction. The route ranks distinct passages. It cannot move a passage's centre toward its endpoint. This is a proposal to the native planner, never a free-space or swept body certificate. In particular small gaps in a sampled cross-section are not converted to positive semantic cells. """ import math import numpy as np from scipy.spatial import cKDTree def passage_centres(points, pose, target, geometry, obstacles, *, travel_direction=None): position = np.asarray(pose[:2]) # An explicitly selected route continuation stations the observed corridor. # It is a direction, never the vector toward a remote endpoint. Legacy # unrouted tasks retain their body-forward observation convention. if travel_direction is None: x, y, z, w = pose[3:7] yaw = math.atan2(2*(w*z+x*y), 1-2*(y*y+z*z)) else: direction = np.asarray(travel_direction, dtype=float) if direction.shape != (2,) or not np.isfinite(direction).all() or np.linalg.norm(direction) <= 1e-9: raise ValueError("Invalid passage travel direction") yaw = math.atan2(direction[1], direction[0]) axes = np.array([[math.cos(yaw), -math.sin(yaw)], [math.sin(yaw), math.cos(yaw)]]) local = (points[:, :2]-position) @ axes hazards = (obstacles-position) @ axes if len(obstacles) else np.empty((0, 2)) tree = cKDTree(hazards) if len(hazards) else None width = geometry['width_m'] length = geometry.get('length_m', width) route = (np.asarray(target)-position) @ axes # Common forward sections avoid rewarding a lens side with longer rays. # The 3.5 m authority horizon is unchanged. The complete measured section, # including returns laterally outside that circle, supplies the boundaries. horizon = min(3., float(local[:, 0].max())) for along in np.arange(horizon, .59, -.2): lateral = np.sort(local[np.abs(local[:, 0]-along) <= length/2, 1]) if len(lateral) < 8: continue # 20 cm semantic cells: two adjacent measured cells are one interval. # Larger unsupported intervals remain distinct passages. groups = np.split(lateral, np.flatnonzero(np.diff(lateral) > .3)+1) intervals = [(float(g[0]), float(g[-1])) for g in groups if len(g) >= 3] centres=[] for low,high in intervals: if high-low < width: continue # Rank lateral positions by their worst clearance to the observed # passage limits and to solids along the whole continuing bearing. # Keep each obstacle at its actual longitudinal coordinate: a wall # beside the rover must not become a fictitious barrier ahead. lateral_samples = np.linspace(low+width/2, high-width/2, max(2, math.ceil((high-low-width)/.05)+1)) centre_clearance = np.minimum(lateral_samples-low, high-lateral_samples) if tree is not None: delta = np.column_stack((np.full(len(lateral_samples),along), lateral_samples)) direction = delta / np.linalg.norm(delta,axis=1)[:,None] radial = np.linspace(length/2, 3.5, 65) queries = direction[:,None,:]*radial[None,:,None] distances = tree.query(queries.reshape(-1,2))[0].reshape(len(delta),-1) centre_clearance = np.minimum(centre_clearance, distances.min(axis=1)) # Beyond the observation horizon, extra empty width has no local # preference. Route direction can break ties only among equally # clear interior samples, never overwrite an obstacle score. centre_clearance = np.minimum(centre_clearance, 3.5) peak = centre_clearance.max() maxima = np.flatnonzero(centre_clearance >= peak-.025) lobes = np.split(maxima, np.flatnonzero(np.diff(maxima)>1)+1) # Two equally clear sides of an obstacle are separate choices. # Averaging them would put the goal back through the obstacle. lobe = min(lobes, key=lambda ids: abs(lateral_samples[ids].mean()-(low+high)/2)) middle = float(lateral_samples[lobe].mean()) if peak >= 3.5 and route[0] > .1: preferred = along*route[1]/route[0] middle = float(lateral_samples[maxima[np.argmin( np.abs(lateral_samples[maxima]-preferred))]]) xy=position+axes @ np.array([along,middle]) if np.linalg.norm(xy-position) > 3.5: continue centres.append(dict(xy=xy, width=high-low, along=float(along), bounds=[low,high], yaw=yaw)) if centres: # Topological choice: the endpoint ranks passages, never samples # within one passage. Native planning still validates connectivity. direction=np.asarray(target)-position centres.sort(key=lambda c: abs(math.atan2( direction[0]*(c['xy'][1]-position[1])-direction[1]*(c['xy'][0]-position[0]), np.dot(direction,c['xy']-position)))) yield centres