Optimize range clearing with equivalent early pixel rejection

This commit is contained in:
DCCONSTRUCTIONS
2026-10-08 21:14:11 +03:00
parent 9a2ea5fc2e
commit 7f87b5aa4d
2 changed files with 124 additions and 5 deletions
@@ -10,7 +10,6 @@ import itertools
import numpy as np
VIEW_FIELDS = ("view_models", "view_sizes", "view_axes", "view_parameters", "view_ranges")
@@ -60,7 +59,35 @@ class RangeImages:
self.models, self.sizes, self.origins, self.axes, self.parameters, strict=True):
image = self.ranges[offset:offset+w*h].reshape(h, w)
offset += w*h
local = (corners-origin) @ axes
# Clearing requires EVERY corner and EVERY pixel in its footprint.
# A single corner/pixel that fails is therefore a sufficient veto.
# Most retained returns are still occluded: reject those cheaply
# before projecting the other seven corners. Survivors still take
# the complete original test below; this grants no new free space.
first = (corners[:, :1, :]-origin) @ axes
first_distance = np.linalg.norm(first, axis=2)[:, 0]
if model == 0:
first_u = (np.degrees(np.arctan2(first[..., 1], first[..., 0]))-p[0])/p[1]
first_u = (first_u + (first_u-first_u+w/2) % w-w/2)[:, 0]
first_v = ((np.degrees(np.arctan2(first[..., 2],
np.linalg.norm(first[..., :2], axis=2)))-p[2])/p[3])[:, 0]
eligible = np.ones(len(points), dtype=bool)
else:
front = first[:, 0, 0]
eligible = front > 1e-6
denominator = np.maximum(front, 1e-6)
first_u = p[2]-p[0]*first[:, 0, 1]/denominator-0.5
first_v = p[3]-p[1]*first[:, 0, 2]/denominator-0.5
eligible &= (np.floor(first_u) >= 0) & (np.ceil(first_u) < w)
eligible &= ((first_distance > margin_m) & (np.floor(first_v) >= 0)
& (np.ceil(first_v) < h) & ~result)
candidates = np.flatnonzero(eligible)
measured = image[np.floor(first_v[candidates]).astype(int),
np.floor(first_u[candidates]).astype(int) % w]
candidates = candidates[measured > first_distance[candidates]+margin_m]
if not len(candidates):
continue
local = (corners[candidates]-origin) @ axes
distance = np.linalg.norm(local, axis=2)
if model == 0:
u = (np.degrees(np.arctan2(local[..., 1], local[..., 0]))-p[0])/p[1]
@@ -68,7 +95,7 @@ class RangeImages:
u = u[:, :1] + (u-u[:, :1]+w/2) % w-w/2
v = (np.degrees(np.arctan2(local[..., 2],
np.linalg.norm(local[..., :2], axis=2)))-p[2])/p[3]
visible = np.ones(len(points), dtype=bool)
visible = np.ones(len(candidates), dtype=bool)
else:
front = local[..., 0]
visible = (front > 1e-6).all(axis=1)
@@ -82,7 +109,7 @@ class RangeImages:
visible &= (low_u >= 0) & (high_u < w)
# A near-sensor voxel spanning a large portion of the image stays
# unknown. Bound work, without subsampling its clearing footprint.
visible &= (high_u-low_u <= 8) & (high_v-low_v <= 8) & ~result
visible &= (high_u-low_u <= 8) & (high_v-low_v <= 8)
indices = np.flatnonzero(visible)
if not len(indices):
continue
@@ -96,5 +123,5 @@ class RangeImages:
rows = np.flatnonzero(inside)
measured = image[lv[rows]+dv, (lu[rows]+du) % w]
free[rows] &= measured > required[rows]
result[indices[free]] = True
result[candidates[indices[free]]] = True
return result
+92
View File
@@ -0,0 +1,92 @@
"""Independent full-footprint oracle for conservative range-memory clearing."""
import importlib
import itertools
from pathlib import Path
import numpy as np
import pytest
@pytest.fixture
def range_images(monkeypatch):
monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/ai-polygon"))
return importlib.import_module("navigation.range_image").RangeImages
def full_footprint_oracle(images, points, voxel=0.06, margin=0.12):
"""Enumerate every corner and covered pixel, without a candidate prefilter."""
result = []
offsets = np.array(list(itertools.product((0, 1), repeat=3))) * voxel
for point in points:
cleared, offset = False, 0
for model, (w, h), origin, axes, p in zip(
images.models, images.sizes, images.origins, images.axes, images.parameters,
strict=True,
):
image = images.ranges[offset : offset + w * h].reshape(h, w)
offset += w * h
local = (np.floor(point / voxel) * voxel + offsets - origin) @ axes
distances = np.linalg.norm(local, axis=1)
if model == 0:
u = (np.degrees(np.arctan2(local[:, 1], local[:, 0])) - p[0]) / p[1]
u = u[0] + (u - u[0] + w / 2) % w - w / 2
elevation = np.degrees(np.arctan2(local[:, 2],
np.linalg.norm(local[:, :2], axis=1)))
v = (elevation - p[2]) / p[3]
else:
if np.any(local[:, 0] <= 1e-6):
continue
u = p[2] - p[0] * local[:, 1] / local[:, 0] - 0.5
v = p[3] - p[1] * local[:, 2] / local[:, 0] - 0.5
lu, lv = int(np.floor(u.min())), int(np.floor(v.min()))
hu, hv = int(np.ceil(u.max())), int(np.ceil(v.max()))
if (distances.min() <= margin or lv < 0 or hv >= h
or hu - lu > 8 or hv - lv > 8
or (model == 1 and (lu < 0 or hu >= w))):
continue
if all(image[y, x % w] > distances.max() + margin
for y in range(lv, hv + 1) for x in range(lu, hu + 1)):
cleared = True
result.append(cleared)
return np.array(result, dtype=bool)
@pytest.mark.parametrize("model", [0, 1])
@pytest.mark.parametrize("depth", ["open", "occluded", "discontinuities"])
def test_early_veto_matches_every_corner_and_pixel(range_images, model, depth):
rng = np.random.default_rng(20261008)
w, h = 80, 30
ranges = np.full((h, w), 8.0 if depth == "open" else 2.0)
if depth == "discontinuities":
ranges = rng.choice([0.0, 0.12, 1.5, 3.0, 8.0], (h, w))
angle = 0.47
rotation = np.array([[np.cos(angle), -np.sin(angle), 0],
[np.sin(angle), np.cos(angle), 0], [0, 0, 1]])
origin = np.array([12.11, -3.17, 0.91])
packet = dict(view_models=[model], view_sizes=[[w, h]], view_axes=[rotation],
view_parameters=[[-180, 360 / w, -40, 2.5] if model == 0
else [40, 40, w / 2, h / 2]],
view_ranges=ranges.ravel())
images = range_images([origin], packet)
local = np.vstack((rng.uniform([-5, -4, -3], [6, 4, 3], (400, 3)),
[[-2, 0, 0], [-2, -1e-10, 0], [-2, 1e-10, 0],
[0, 0, 0], [0.06, 0, 0], [2, 0, 0]]))
points = local @ rotation.T + origin
np.testing.assert_array_equal(images.cleared(points, 0.06, 0.12),
full_footprint_oracle(images, points))
def test_one_visible_corner_cannot_clear_a_voxel_with_hidden_corners(range_images):
packet = dict(view_models=[1], view_sizes=[[80, 30]], view_axes=[np.eye(3)],
view_parameters=[[40, 40, 40, 15]], view_ranges=np.full(2400, 8.0))
points = np.array([[2, 0, 0], [2, 0.06, 0.06], [2, -0.06, -0.06]])
images = range_images([[0, 0, 0]], packet)
assert images.cleared(points, 0.06, 0.12).all()
# Every individual unknown pixel must veto exactly the same full footprints.
for y, x in itertools.product(range(11, 18), range(36, 44)):
changed = {**packet, "view_ranges": packet["view_ranges"].copy()}
changed["view_ranges"][y * 80 + x] = 0
images = range_images([[0, 0, 0]], changed)
np.testing.assert_array_equal(images.cleared(points, 0.06, 0.12),
full_footprint_oracle(images, points))