"""Bounded spatial refinement of a declared arrival region. Only proposal coordinates and a cursor survive between observations. Each body assessment still uses the current terrain, permission, attitude and timestamp. """ import math import numpy as np class ArrivalPoseSearch: batch_size = 32 def __init__(self, *, stopping_margin_m=0.0): if not math.isfinite(stopping_margin_m) or not 0 <= stopping_margin_m < .39: raise ValueError("Invalid arrival stopping margin") radius = .39 - stopping_margin_m axis = np.linspace(-radius, radius, math.ceil(2 * radius / .025) + 1) offsets = [(float(x), float(y)) for x in axis for y in axis if math.hypot(x, y) <= radius] offsets += [(sample_radius * math.cos(angle), sample_radius * math.sin(angle)) for sample_radius in (.1, .2, .3, radius) if sample_radius <= radius for angle in (0, math.pi/4, -math.pi/4, math.pi/2, -math.pi/2, math.pi)] self.offsets = list(dict.fromkeys(offsets)) self.ordered = self.offsets self.target = self.pose = None self.cursor = 0 def batch(self, pose, target, minimum_translation_m): key = (*map(float, target), float(minimum_translation_m)) if (self.target != key or self.pose is None or np.linalg.norm(np.asarray(pose[:3]) - self.pose[:3]) > .025 or abs(float(np.dot(pose[3:], self.pose[3:]))) < math.cos(.025 / 2)): self.target, self.pose, self.cursor = key, np.asarray(pose).copy(), 0 qx, qy, qz, qw = pose[3:] yaw = math.atan2(2*(qw*qz+qx*qy), 1-2*(qy*qy+qz*qz)) axes = np.array([[math.cos(yaw), -math.sin(yaw)], [math.sin(yaw), math.cos(yaw)]]) local_target = (np.asarray(target) - np.asarray(pose[:2])) @ axes def rank(offset): delta = local_target + offset return abs(math.atan2(delta[1], delta[0])), float(np.linalg.norm(delta)) # Arrival is a region, not its centre. Prefer a supported placement # requiring less heading change before considering travel distance. # Centre-first proposals can cause a needless near-edge pivot. # This ordering grants no path, support or motion authority. self.ordered = sorted(self.offsets, key=rank) if self.cursor >= len(self.offsets): self.cursor = 0 # Revisit on new observations; no cached admission. result = self.ordered[self.cursor:self.cursor + self.batch_size] self.cursor += len(result) return result