floor-visualizer-gpu / test_wall_plane_extents.py
github-actions[bot]
Automated Deploy: b0a2a3cb7961b9cc25cc3123b04a509844bc9a4b
d7b5c2a
Raw History Blame Contribute Delete
3.84 kB
"""Regression checks for the reported physical size of a wall.
Tile density is computed from a wall's reported extents, so an extent that is
guessed rather than measured mis-scales the grid directly.
Without depth there is no absolute scale, so the wall *height* stays a prior
(WALL_HEIGHT_PRIOR_METERS). What must not be guessed is the wall's **aspect**:
it used to come from the axis-aligned bounding box of the source quad. A
slanted wall falls through to `cv2.minAreaRect`, whose corners may lie outside
the image entirely -- one wall in the sample photo reports a corner at x=-133 --
and the bounding box of that rotated rect is much wider than the wall is. The
quad's own edge lengths are the measurement.
"""
import unittest
import cv2
import numpy as np
from services.wall.plane import WALL_HEIGHT_PRIOR_METERS, estimate_wall_plane
HEIGHT, WIDTH = 600, 700
LONG_SIDE, SHORT_SIDE = 400.0, 250.0
TILT_DEGREES = 20.0
def _rotated_wall_mask(tilt_degrees: float):
"""A rectangular wall of known side lengths, tilted in the image."""
radians = np.deg2rad(tilt_degrees)
rotation = np.array(
[
[np.cos(radians), -np.sin(radians)],
[np.sin(radians), np.cos(radians)],
],
dtype=np.float32,
)
corners = np.array(
[
[-LONG_SIDE / 2, -SHORT_SIDE / 2],
[LONG_SIDE / 2, -SHORT_SIDE / 2],
[LONG_SIDE / 2, SHORT_SIDE / 2],
[-LONG_SIDE / 2, SHORT_SIDE / 2],
],
dtype=np.float32,
)
placed = corners @ rotation.T + np.array([WIDTH / 2, HEIGHT / 2], dtype=np.float32)
mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
cv2.fillConvexPoly(mask, placed.round().astype(np.int32), 1)
return mask
class WallPlaneExtentsTest(unittest.TestCase):
def test_a_tilted_wall_is_measured_along_its_own_edges(self):
"""Aspect comes from the quad's edges, not its bounding box."""
mask = _rotated_wall_mask(TILT_DEGREES)
_, plane = estimate_wall_plane(mask)
self.assertIsNotNone(plane)
expected = WALL_HEIGHT_PRIOR_METERS * (LONG_SIDE / SHORT_SIDE)
self.assertAlmostEqual(plane["surfaceWidthMeters"], expected, delta=0.05)
def test_the_bounding_box_of_a_tilted_wall_overstates_its_width(self):
"""Guards the test above: the old estimate really was different."""
radians = np.deg2rad(TILT_DEGREES)
box_width = LONG_SIDE * np.cos(radians) + SHORT_SIDE * np.sin(radians)
box_height = LONG_SIDE * np.sin(radians) + SHORT_SIDE * np.cos(radians)
from_box = WALL_HEIGHT_PRIOR_METERS * (box_width / box_height)
from_edges = WALL_HEIGHT_PRIOR_METERS * (LONG_SIDE / SHORT_SIDE)
self.assertGreater(abs(from_edges - from_box), 0.5)
def test_an_axis_aligned_wall_keeps_its_previous_extents(self):
"""The change must only move walls whose quad is actually tilted."""
mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
mask[100:100 + int(SHORT_SIDE), 150:150 + int(LONG_SIDE)] = 1
_, plane = estimate_wall_plane(mask)
self.assertIsNotNone(plane)
expected = WALL_HEIGHT_PRIOR_METERS * (LONG_SIDE / SHORT_SIDE)
self.assertAlmostEqual(plane["surfaceWidthMeters"], expected, delta=0.05)
self.assertAlmostEqual(plane["surfaceHeightMeters"], WALL_HEIGHT_PRIOR_METERS, places=6)
def test_a_wall_never_reports_a_degenerate_width(self):
"""A grazing sliver must still land in the renderer's usable range."""
mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
mask[100:560, 300:340] = 1 # a tall, very narrow strip
_, plane = estimate_wall_plane(mask)
self.assertIsNotNone(plane)
self.assertGreaterEqual(plane["surfaceWidthMeters"], 0.5)
if __name__ == "__main__":
unittest.main()