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
+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))