Download test_wall_plane_basis.py from modventures/floor-visualizer-gpu: direct link, hf CLI and curl.
- Browser
- Download file 7.37 kB
-
https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/test_wall_plane_basis.py
- Command line
-
hf download hf://spaces/modventures/floor-visualizer-gpu/test_wall_plane_basis.py
-
curl -L -o test_wall_plane_basis.py https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/test_wall_plane_basis.py
7.37 kB
| """Regression checks for the wall plane's tile basis. | |
| A wall's (u, v) basis decides which way its tile grid runs. `v` is meant to be | |
| world-up projected onto the wall, so tiles run *up* the wall. | |
| Geometry providers return points in *camera* coordinates, so camera +Y is | |
| camera-up, not gravity-up. Assuming +Y is up therefore rotates every wall's | |
| tile grid by the camera's roll -- the tilted grid users see on photos that were | |
| not shot perfectly level. `plane_basis` already takes a `vertical_ref` for this | |
| and the per-pixel UV path already passes it; the plane fit did not, so the | |
| plane basis stayed camera-aligned. | |
| The floor is unaffected by design: a floor's `u` comes from a horizontal | |
| reference, so `vertical_ref` must not perturb it. | |
| """ | |
| import unittest | |
| import numpy as np | |
| from services.geometry.plane_fitting import fit_metric_floor_transform | |
| from services.pipeline.bundle_assembly import build_wall_segment | |
| from services.wall.plane import estimate_wall_plane | |
| HEIGHT, WIDTH = 200, 260 | |
| FX = FY = 300.0 | |
| CX, CY = 130.0, 100.0 | |
| WALL_DISTANCE = 3.0 | |
| CAMERA_ROLL_DEGREES = 20.0 | |
| # Gravity-up in camera coordinates for a camera rolled about its optical axis. | |
| # It stays in the z=0 plane, so for a fronto-parallel wall it needs no | |
| # projection and `v` should come back equal to it. | |
| ROLL_RADIANS = np.deg2rad(CAMERA_ROLL_DEGREES) | |
| TRUE_UP = np.array([np.sin(ROLL_RADIANS), np.cos(ROLL_RADIANS), 0.0], dtype=np.float32) | |
| CAMERA_UP = np.array([0.0, 1.0, 0.0], dtype=np.float32) | |
| def _wall_scene(): | |
| """A planar, fronto-parallel wall filling most of the frame. | |
| Returns (mask, geometry). Every masked pixel lies exactly on z=WALL_DISTANCE | |
| so the fit is unambiguous and the test isolates the basis, not the RANSAC. | |
| """ | |
| rows, cols = np.mgrid[0:HEIGHT, 0:WIDTH].astype(np.float32) | |
| points = np.stack( | |
| ( | |
| (cols - CX) / FX * WALL_DISTANCE, | |
| (rows - CY) / FY * WALL_DISTANCE, | |
| np.full_like(cols, WALL_DISTANCE), | |
| ), | |
| axis=2, | |
| ).astype(np.float32) | |
| mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8) | |
| mask[20:180, 20:WIDTH - 20] = 1 | |
| normals = np.zeros((HEIGHT, WIDTH, 3), dtype=np.float32) | |
| normals[:, :, 2] = -1.0 # the wall faces the camera | |
| geometry = { | |
| "points": points, | |
| "validMask": np.ones((HEIGHT, WIDTH), dtype=bool), | |
| "normals": normals, | |
| "intrinsics": None, | |
| } | |
| return mask, geometry | |
| def _floor_scene(): | |
| """A planar floor seen from a camera 1.5m above it.""" | |
| 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) | |
| ray_y = np.clip(ray[:, :, 1], 1e-3, None) | |
| points = (ray * (1.5 / ray_y)[:, :, None]).astype(np.float32) | |
| mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8) | |
| mask[150:HEIGHT, 10:WIDTH - 10] = 1 | |
| normals = np.zeros((HEIGHT, WIDTH, 3), dtype=np.float32) | |
| normals[:, :, 1] = -1.0 | |
| geometry = { | |
| "points": points, | |
| "validMask": np.ones((HEIGHT, WIDTH), dtype=bool), | |
| "normals": normals, | |
| "intrinsics": None, | |
| } | |
| return mask, geometry | |
| def _axis(vector) -> np.ndarray: | |
| """Normalize an axis and fix its sign so antiparallel axes compare equal.""" | |
| axis = np.asarray(vector, dtype=np.float32) | |
| axis = axis / max(float(np.linalg.norm(axis)), 1e-6) | |
| return axis if float(axis @ CAMERA_UP) >= 0 else -axis | |
| class WallPlaneBasisTest(unittest.TestCase): | |
| def test_wall_v_axis_follows_the_supplied_vertical(self): | |
| """The tile grid runs along gravity-up, not the camera's tilted up.""" | |
| mask, geometry = _wall_scene() | |
| result = fit_metric_floor_transform( | |
| mask, | |
| geometry["points"], | |
| geometry["validMask"], | |
| geometry["normals"], | |
| None, | |
| surface_kind="wall", | |
| vertical_ref=TRUE_UP, | |
| ) | |
| self.assertIsNotNone(result, "the wall fit rejected a perfectly planar wall") | |
| v_axis = _axis(result["surfaceVAxis"]) | |
| np.testing.assert_allclose(v_axis, TRUE_UP, atol=1e-3) | |
| def test_wall_v_axis_defaults_to_camera_up(self): | |
| """Without a reference there is nothing better than camera-up.""" | |
| mask, geometry = _wall_scene() | |
| result = fit_metric_floor_transform( | |
| mask, | |
| geometry["points"], | |
| geometry["validMask"], | |
| geometry["normals"], | |
| None, | |
| surface_kind="wall", | |
| ) | |
| self.assertIsNotNone(result) | |
| np.testing.assert_allclose(_axis(result["surfaceVAxis"]), CAMERA_UP, atol=1e-3) | |
| def test_estimate_wall_plane_forwards_the_vertical_reference(self): | |
| """The pipeline's entry point must thread the reference to the basis.""" | |
| mask, geometry = _wall_scene() | |
| _, plane = estimate_wall_plane(mask, geometry, vertical_ref=TRUE_UP) | |
| self.assertIsNotNone(plane) | |
| self.assertIn( | |
| "surfaceVAxis", | |
| plane, | |
| "the metric wall fit was rejected, so the basis was never reached", | |
| ) | |
| np.testing.assert_allclose(_axis(plane["surfaceVAxis"]), TRUE_UP, atol=1e-3) | |
| def test_build_wall_segment_passes_its_up_vector_to_the_plane(self): | |
| """The pipeline already knows true vertical; the plane must receive it. | |
| `build_wall_segment` forwarded `up_vector` to the per-pixel UV mapping | |
| but not to the plane fit, so the plane's basis -- which is what the | |
| renderer falls back to, and where the reported surface extents come | |
| from -- stayed camera-aligned. | |
| """ | |
| mask, geometry = _wall_scene() | |
| segment = build_wall_segment( | |
| 0, | |
| mask, | |
| np.zeros((HEIGHT, WIDTH), dtype=np.uint8), | |
| geometry, | |
| np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8), | |
| [None], | |
| {"segmentation": {"name": "test-segmenter"}}, | |
| precomputed_lighting={}, | |
| up_vector=TRUE_UP, | |
| ) | |
| self.assertIsNotNone(segment, "the wall segment builder rejected a clean wall") | |
| plane = segment["plane"] | |
| self.assertIn( | |
| "surfaceVAxis", | |
| plane, | |
| "the metric wall fit was rejected, so the basis was never reached", | |
| ) | |
| np.testing.assert_allclose(_axis(plane["surfaceVAxis"]), TRUE_UP, atol=1e-3) | |
| def test_floor_basis_ignores_the_vertical_reference(self): | |
| """A floor's basis is built from a horizontal reference, not from up.""" | |
| mask, geometry = _floor_scene() | |
| common = (mask, geometry["points"], geometry["validMask"], geometry["normals"], None) | |
| without = fit_metric_floor_transform(*common, surface_kind="floor") | |
| with_ref = fit_metric_floor_transform(*common, surface_kind="floor", vertical_ref=TRUE_UP) | |
| self.assertIsNotNone(without) | |
| self.assertIsNotNone(with_ref) | |
| np.testing.assert_allclose( | |
| np.asarray(with_ref["surfaceUAxis"], dtype=np.float32), | |
| np.asarray(without["surfaceUAxis"], dtype=np.float32), | |
| atol=1e-6, | |
| ) | |
| np.testing.assert_allclose( | |
| np.asarray(with_ref["surfaceVAxis"], dtype=np.float32), | |
| np.asarray(without["surfaceVAxis"], dtype=np.float32), | |
| atol=1e-6, | |
| ) | |
| if __name__ == "__main__": | |
| unittest.main() | |