Choose local passage centres before route guidance

This commit is contained in:
DCCONSTRUCTIONS
2026-10-09 23:40:19 +03:00
parent e61aa6da5f
commit 8bd2a9967a
6 changed files with 187 additions and 53 deletions
@@ -12,6 +12,7 @@ import numpy as np
from scipy.spatial import cKDTree
from .semantic_surface import SemanticSurface
from .observed_passages import passage_centres
class ObservedGoals:
@@ -316,8 +317,6 @@ class ObservedGoals:
remaining = float(np.linalg.norm(direction))
if remaining <= 0.4:
return None # Exact arrival remains the native whole-body receipt.
forward = direction / remaining
lateral = np.array([-forward[1], forward[0]])
delta = points[:, :2] - pose[:2]
distance = np.linalg.norm(delta, axis=1)
progress = np.linalg.norm(points[:, :2] - target, axis=1)
@@ -330,30 +329,39 @@ class ObservedGoals:
good &= candidate_mask(points)
for failed in excluded:
good &= np.linalg.norm(points[:, :2] - np.asarray(failed[:2]), axis=1) > 0.6
candidates = points[good]
if len(candidates) < 8:
if not np.any(good):
return None
allowed = {
key: stamp for key, (permission, stamp) in self.surface.cells.items()
if permission == 1 and self.surface.retained(key, stamp, now)
}
candidates = candidates[[tuple(key) in allowed
for key in self.surface.cell_keys(candidates)]]
if not len(candidates):
permitted = points[[tuple(key) in allowed for key in self.surface.cell_keys(points)]]
local = np.linalg.norm(permitted[:, :2]-pose[:2], axis=1)
permitted = permitted[(local <= 6.) & (np.abs(permitted[:, 2] -
(pose[2]-geometry['body_contact_height_m'])) <= geometry.get('max_step_m', .1)
+ local*math.tan(math.radians(geometry['max_slope_degrees'])))]
if candidate_mask is not None:
permitted = permitted[candidate_mask(permitted)]
candidates = points[good]
candidates = candidates[[tuple(key) in allowed for key in self.surface.cell_keys(candidates)]]
if not len(candidates) or len(permitted) < 8:
return None
offsets = np.linspace(-geometry["width_m"] / 2, geometry["width_m"] / 2,
math.ceil(geometry["width_m"] / 0.1) + 1)
def supported(point):
bearing = point[:2]-pose[:2]
bearing /= max(np.linalg.norm(bearing), 1e-9)
lateral = np.array([-bearing[1], bearing[0]])
across = point[:2] + offsets[:, None] * lateral
# Bind each lateral query to measured, permitted ground at its own
# height. Do not copy the centre elevation across a cross-slope or
# project a sidewalk label through an overhang onto another floor.
distances = np.linalg.norm(across[:,None,:]-candidates[None,:,:2],axis=2)
distances = np.linalg.norm(across[:,None,:]-permitted[None,:,:2],axis=2)
nearest = np.argmin(distances,axis=1)
if np.any(distances[np.arange(len(across)),nearest] > .15):
return False
keys = [tuple(cell) for cell in self.surface.cell_keys(candidates[nearest])]
keys = [tuple(cell) for cell in self.surface.cell_keys(permitted[nearest])]
self.evidence = {
"source": "current-range-and-observed-semantic-memory",
"goal": point.tolist(),
@@ -384,43 +392,23 @@ class ObservedGoals:
self.evidence['approach_clearance_m'] = (
float(retained_clearance) if math.isfinite(retained_clearance) else None)
return retain.tolist()
# Score the task direction at the existing 3.5 m observation horizon.
# Distance to a remote endpoint mostly rewards longitudinal advance:
# slightly farther side returns can beat a clear straight destination
# and make successive measured goals oscillate across the corridor.
# This reference only ranks actual permitted returns. It is never a
# generated drive goal, a straight-line route or a clearance receipt.
reference = pose[:2] + forward * horizon
# A smooth local attraction, normalized to metres like boundary_cost.
# Its gradient diminishes near the reference so centimetres of extra
# advance cannot outweigh clearance from an observed corridor edge.
distance_to_target = np.sum((candidates[:, :2] - reference)**2, axis=1) / horizon
# The declared task, not the selected lens, owns the preferred direction.
x,y,z,w = pose[3:7]
heading = math.atan2(2*(w*z+x*y),1-2*(y*y+z*z))
bearing = np.arctan2(candidates[:,1]-pose[1],candidates[:,0]-pose[0])-heading
score = (distance_to_target + .2*np.abs(np.arctan2(np.sin(bearing),np.cos(bearing)))
+ self.boundary_cost(candidates, geometry, now))
clearance_cost, clearances = self.clearance_cost(
candidates, pose, geometry, now, lookahead_m=horizon)
score += clearance_cost
for index in np.argsort(score, kind="stable"):
if supported(candidates[index]):
selected = candidates[index]
if retain is not None and not (
retained_clearance < geometry['width_m']
and clearances[index] > retained_clearance + .1
):
selected = retain
supported(retain)
if self.evidence is not None:
self.evidence['approach_clearance_m'] = (
float(self.approach_clearance(selected[None], pose, geometry, now)[0])
if self.clearance_observation is not None
and 0 <= now-self.clearance_observation[0] <= .8
and len(self.clearance_observation[1]) else None)
if self.evidence is not None and not math.isfinite(
self.evidence.get('approach_clearance_m') or 0.):
self.evidence['approach_clearance_m'] = None
return selected.tolist()
observed = self.clearance_observation
obstacles = (observed[1] if observed is not None and 0 <= now-observed[0] <= .8
else np.empty((0, 2)))
for passages in passage_centres(permitted, pose, target, geometry, obstacles):
for passage in passages:
distance = np.linalg.norm(candidates[:, :2]-passage['xy'], axis=1)
for index in np.argsort(distance, kind="stable"):
if distance[index] > .25:
break
selected = candidates[index]
if not supported(selected):
continue
clearance = self.approach_clearance(selected[None], pose, geometry, now)[0]
self.evidence.update(source="observed-passage-centre",
passage_width_m=float(passage['width']),
passage_centre_xy=passage['xy'].tolist(),
route_role="rank-distinct-passages-only",
approach_clearance_m=float(clearance) if math.isfinite(clearance) else None)
return selected.tolist()
return None