floor-visualizer-gpu / test_floor_uv_coverage.py
github-actions[bot]
Automated Deploy: 001e0f3089bb38a4e8b7c1c85891b38e75a77ec7
b2004ce
Raw History Blame Contribute Delete
5.56 kB
"""Regression checks for floor per-pixel UV coverage.
A floor whose depth is noisy in a small minority of pixels must still ship
per-pixel UV. Discarding the whole mapping over a couple of percent drops the
floor onto the regularized-plane homography, whose quad is a straight-edged
trapezoid: it under-covers the mask at the corners and cannot follow the real
floor boundary.
"""
import unittest
from unittest import mock
import numpy as np
from services.geometry import multi_plane
from services.geometry.uv_mapping import build_surface_uv_mapping
HEIGHT, WIDTH = 200, 260
FX = FY = 300.0
CX, CY = 130.0, 100.0
CAMERA_HEIGHT = 1.5
CY_START = 150 # far enough below the horizon to avoid grazing-angle UV strides
def _floor_scene(off_plane_pixels: int, off_plane_offset: float = 0.30):
"""A planar floor seen from a camera, with a minority of off-plane pixels.
Returns (surface_mask, surface_indices, geometry). The off-plane pixels are
finite and in front of the camera, so they reach plane assignment and are
rejected there for distance -- which is how real depth noise behaves.
"""
rows, cols = np.mgrid[0:HEIGHT, 0:WIDTH].astype(np.float32)
ray = np.stack(((cols - CX) / FX, (rows - CY) / FY, np.ones_like(cols)), axis=2)
# Only rows below the principal point look at the floor.
ray_y = np.clip(ray[:, :, 1], 1e-3, None)
distance = (CAMERA_HEIGHT / ray_y).astype(np.float32)
points = (ray * distance[:, :, None]).astype(np.float32)
mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
mask[CY_START:HEIGHT, 10:WIDTH - 10] = 1
normals = np.zeros((HEIGHT, WIDTH, 3), dtype=np.float32)
normals[:, :, 1] = -1.0 # floor faces up, toward the camera
# Displace a scattered minority along the plane normal, far enough to fail
# the assignment distance limit but too few to attract a plane of their own.
flat = np.flatnonzero(mask)
generator = np.random.default_rng(7)
noisy = generator.choice(flat, size=off_plane_pixels, replace=False)
noisy_rows, noisy_cols = np.unravel_index(noisy, (HEIGHT, WIDTH))
points[noisy_rows, noisy_cols, 1] -= off_plane_offset
geometry = {
"points": points,
"normals": normals,
"validMask": np.ones((HEIGHT, WIDTH), dtype=bool),
"intrinsics": np.array([[FX, 0.0, CX], [0.0, FY, CY], [0.0, 0.0, 1.0]], dtype=np.float32),
"provider": "moge-2",
}
return mask, np.flatnonzero(mask).astype(np.uint32), geometry
class FloorUvCoverageTests(unittest.TestCase):
def test_floor_keeps_per_pixel_uv_despite_a_noisy_minority(self):
"""The whole mapping must not be discarded over a few percent of pixels.
Without the ray-plane fill the off-plane pixels keep NaN UV, coverage
lands near 0.96, and the 0.995 floor gate rejects the mapping outright.
"""
mask, indices, geometry = _floor_scene(off_plane_pixels=800)
mapping = build_surface_uv_mapping(mask, indices, geometry)
self.assertIsNotNone(mapping, "expected a mapping for a clean planar floor")
metadata = mapping["metadata"]
self.assertGreater(
metadata["surfaceUvRejectedByPlaneDistance"], 0,
"test scene should actually exercise distance rejection",
)
self.assertEqual(
metadata["surfaceUvCoverage"], 1.0,
f"every surface pixel should carry a UV, got {metadata['surfaceUvCoverage']}",
)
self.assertTrue(
mapping["useSurfaceUv"],
f"floor fell back to {metadata['textureMappingMode']} "
f"({metadata['textureMappingReason']})",
)
def test_a_clean_floor_still_maps(self):
"""Guards against the fill masking a regression on the easy case."""
mask, indices, geometry = _floor_scene(off_plane_pixels=0)
mapping = build_surface_uv_mapping(mask, indices, geometry)
self.assertIsNotNone(mapping)
self.assertTrue(mapping["useSurfaceUv"])
self.assertEqual(mapping["metadata"]["surfaceUvCoverage"], 1.0)
def test_raising_max_planes_does_not_split_a_flat_floor(self):
"""max_planes went 1 -> 5 to let walls split; the floor must not notice.
The cap is shared: it bounds RANSAC for every surface, not just walls.
Raising it gives a floor room to be fitted as several planes, and a
flat floor broken into two would tile with a seam across it. A planar
floor must still come back as exactly one plane, and produce the same
mapping it did under the old cap of 1.
"""
mask, indices, geometry = _floor_scene(off_plane_pixels=0)
at_five = build_surface_uv_mapping(mask, indices, geometry)
self.assertEqual(
at_five["metadata"]["surfaceUvAssignedPlaneCount"], 1,
"a flat floor was split into multiple planes once the cap was raised",
)
with mock.patch.object(multi_plane, "SURFACE_MAX_PLANES", 1):
at_one = build_surface_uv_mapping(mask, indices, geometry)
self.assertEqual(at_one["useSurfaceUv"], at_five["useSurfaceUv"])
self.assertEqual(
at_one["metadata"]["surfaceUvCoverage"],
at_five["metadata"]["surfaceUvCoverage"],
"raising the cap changed floor UV coverage",
)
self.assertTrue(
np.allclose(at_one["uv"], at_five["uv"], equal_nan=True),
"raising the cap moved the floor's texture coordinates",
)
if __name__ == "__main__":
unittest.main()