File size: 5,556 Bytes
b2004ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """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()
|