Batch exact ground support queries in the control loop

This commit is contained in:
DCCONSTRUCTIONS
2026-10-10 04:57:32 +03:00
parent d8c79a1134
commit 5a68240511
2 changed files with 85 additions and 18 deletions
@@ -38,8 +38,17 @@ class SupportSurface:
if not ground.any():
return points[ground]
# Match the observed range voxel resolution; no scene-dependent scale.
_, inverse = np.unique(np.floor(points[:, :2] / .06).astype(int),
axis=0, return_inverse=True)
columns = np.floor(points[:, :2] / .06).astype(np.int64)
# A bounded sensor cloud can group XY columns with a scalar integer
# key. This preserves exact column membership without NumPy's costly
# structured-row sort on every control tick. Fall back for wide inputs.
extent = [int(columns[:, i].max())-int(columns[:, i].min())+1 for i in (0, 1)]
if extent[0] * extent[1] <= np.iinfo(np.int64).max:
relative = columns - columns.min(axis=0)
keys = relative[:, 0] * extent[1] + relative[:, 1]
_, inverse = np.unique(keys, return_inverse=True)
else:
_, inverse = np.unique(columns, axis=0, return_inverse=True)
lowest = np.full(int(inverse.max()) + 1, np.inf)
np.minimum.at(lowest, inverse[ground], points[ground, 2])
obstacle_top = np.full(len(lowest), -np.inf)
@@ -94,27 +103,37 @@ class SupportSurface:
height discontinuity retains the raw veto. Unknown ground is unchanged.
"""
offsets = np.asarray(offsets, dtype=float).reshape(-1, 2)
states, reasons = self.classify(offsets)
states, reasons = states.astype(object), Counter(reasons)
states, raw_reasons = self._classify_details(offsets)
states, reasons = states.astype(object), Counter(raw_reasons)
spacing, radius = .05, .15
delta = np.array([[x*spacing, y*spacing] for x in range(-3,4)
for y in range(-3,4) if x*x+y*y<=9])
critical = math.floor(2*radius*(contact_width_m/3)/spacing**2)
for i in np.flatnonzero(states == 'observed-unsafe'):
point = offsets[i]
_, raw = self.classify([point])
reason = next(iter(raw))
if reason not in {'slope', 'roughness'}:
continue
key = (tuple(np.round(point,12)), float(contact_width_m))
candidates = np.flatnonzero((states == 'observed-unsafe')
& np.isin(raw_reasons, ['slope', 'roughness']))
keys = {i: (tuple(np.round(offsets[i], 12)), float(contact_width_m))
for i in candidates}
pending = {}
for i, key in keys.items():
if key not in self.contact_results:
_, surrounding = self.classify(point+delta)
forbidden = set(surrounding)-{'observed-patch','slope','roughness'}
count = surrounding.get('slope',0)+surrounding.get('roughness',0)
self.contact_results[key] = not forbidden and count<=critical
pending.setdefault(key, offsets[i])
items = list(pending.items())
# Classify each identical 29-point disk in bounded batches. Re-entering
# the native wrapper per cell scanned the full return cloud hundreds
# of times; batching changes neither samples nor the relief rule.
for start in range(0, len(items), 64):
batch = items[start:start+64]
queries = np.array([point for _, point in batch])[:, None, :] + delta
_, surrounding = self._classify_details(queries.reshape(-1, 2))
surrounding = surrounding.reshape(len(batch), len(delta))
permitted = np.isin(surrounding, ['observed-patch', 'slope', 'roughness'])
counts = np.isin(surrounding, ['slope', 'roughness']).sum(axis=1)
for (key, _), allowed in zip(batch, permitted.all(axis=1) & (counts <= critical), strict=True):
self.contact_results[key] = bool(allowed)
for i, key in keys.items():
if self.contact_results[key]:
states[i] = 'observed-supported'
reasons[reason] -= 1
reasons[raw_reasons[i]] -= 1
reasons['observed-local-relief'] += 1
return states, {key:value for key,value in reasons.items() if value}
@@ -158,6 +177,10 @@ class SupportSurface:
def classify(self, offsets):
"""Return observed-supported, observed-unsafe or unknown per XY sample."""
states, reasons = self._classify_details(offsets)
return states, dict(Counter(reasons))
def _classify_details(self, offsets):
offsets = np.asarray(offsets, dtype=float).reshape(-1, 2)
if not np.isfinite(offsets).all():
raise ValueError("Invalid support samples")
@@ -165,7 +188,7 @@ class SupportSurface:
states, reasons, raised = self.native.classify(offsets)
for point in raised + self.pose[:3]:
self.raised_obstacles[tuple(np.round(point, 6))] = point.tolist()
return states, dict(Counter(reasons))
return states, reasons
keys = [tuple(point) for point in np.round(offsets, 12)]
pending = {}
for point, sample_key in zip(offsets, keys):
@@ -200,7 +223,7 @@ class SupportSurface:
for item, state, reason in zip(batch, states, reasons))
values = [self.results[key] for key in keys]
return (np.array([v[0] for v in values], dtype=str),
dict(Counter(v[1] for v in values)))
np.array([v[1] for v in values], dtype=str))
def hazard_samples(self, unsafe_offsets):
"""Bind rejected surface samples to real returns for path-search costs.