64 lines
3.2 KiB
Python
64 lines
3.2 KiB
Python
"""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
|
|
|
|
|
|
def passage_centres(points, pose, target, geometry, obstacles):
|
|
position = np.asarray(pose[:2])
|
|
x, y, z, w = pose[3:7]
|
|
yaw = math.atan2(2*(w*z+x*y), 1-2*(y*y+z*z))
|
|
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))
|
|
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]
|
|
near = hazards[(hazards[:, 0] >= .4)
|
|
& (hazards[:, 0] <= along+length)]
|
|
for boundary in sorted(set(np.round(near[:, 1]/.1).astype(int))):
|
|
low, high = boundary*.1-.1, boundary*.1+.1
|
|
intervals = [piece for a,b in intervals for piece in (
|
|
[(a,b)] if high <= a or low >= b else
|
|
[(a,min(b,low)),(max(a,high),b)]) if piece[1]>piece[0]]
|
|
centres=[]
|
|
for low,high in intervals:
|
|
if high-low < width:
|
|
continue
|
|
middle=(low+high)/2
|
|
# A broad open area has no nearby pair of boundaries to centre
|
|
# between. The route can choose a direction within its interior.
|
|
if high-low >= 2*3.5 and route[0] > .1:
|
|
middle=float(np.clip(along*route[1]/route[0],low+width,high-width))
|
|
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
|