File size: 3,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
"""Checks that wall lighting can actually be switched off.

The floor's lighting runs behind a three-way cascade of flags. Walls bypassed
it entirely: `builder._compute_wall_lighting` submitted
`build_luminance_lighting_maps` for every wall component unconditionally, and
`build_wall_segment` computed the same maps inline whenever none were handed
to it. So with every lighting flag off, walls were still lit -- 0.63s of a
5.67s conversion on the GPU box, and no way to stop it.

Note the second half: gating only the pre-compute would have moved the work
into the packaging phase rather than removing it, and serially at that. The
switch has to reach `build_wall_segment` itself, which is what these pin.
"""

import unittest

import numpy as np

from services.pipeline.bundle_assembly import build_wall_segment

HEIGHT, WIDTH = 200, 260
MODELS = {"segmentation": {"name": "test-segmenter"}}


def _lit_room() -> np.ndarray:
    """A textured image, so luminance lighting has something to find.

    A flat image can yield a degenerate shade map, which would make the
    "lighting on" guard below pass for the wrong reason.
    """
    rows, cols = np.mgrid[0:HEIGHT, 0:WIDTH].astype(np.float32)
    gradient = 40 + 170 * (cols / WIDTH) * (0.4 + 0.6 * (rows / HEIGHT))
    image = np.stack([gradient, gradient * 0.9, gradient * 0.8], axis=2)
    return np.clip(image, 0, 255).astype(np.uint8)


def _wall_mask() -> np.ndarray:
    mask = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
    mask[20:180, 20:WIDTH - 20] = 1
    return mask


def _geometry() -> dict:
    """Enough for the plane fit to be attempted and decline harmlessly."""
    return {"points": None, "validMask": None, "normals": None, "intrinsics": None}


def _build(**overrides):
    kwargs = {
        "wall_index": 0,
        "component_mask": _wall_mask(),
        "wall_protected_mask": np.zeros((HEIGHT, WIDTH), dtype=np.uint8),
        "geometry": _geometry(),
        "img_np": _lit_room(),
        "wall_components": [None],
        "models": MODELS,
        "precomputed_lighting": None,
    }
    kwargs.update(overrides)
    return build_wall_segment(**kwargs)


class WallLightingGateTest(unittest.TestCase):
    def test_lighting_is_computed_inline_when_enabled(self):
        """Guard: proves the assertion below can tell the two apart."""
        segment = _build(lighting_enabled=True)

        self.assertIsNotNone(segment)
        self.assertIsNotNone(segment["shadeMap"])
        self.assertTrue(segment["metadata"]["shadingEnabled"])

    def test_no_lighting_maps_are_produced_when_disabled(self):
        segment = _build(lighting_enabled=False)

        self.assertIsNotNone(segment)
        self.assertIsNone(segment["shadeMap"])
        self.assertIsNone(segment["showroomShadeMap"])
        self.assertIsNone(segment["reflectionMap"])
        self.assertFalse(segment["metadata"]["shadingEnabled"])

    def test_disabling_beats_precomputed_maps(self):
        """The flag must win, so a stale pre-compute cannot leak lighting back."""
        segment = _build(
            lighting_enabled=False,
            precomputed_lighting={"shadeMap": np.ones((HEIGHT, WIDTH), dtype=np.uint8)},
        )

        self.assertIsNotNone(segment)
        self.assertIsNone(segment["shadeMap"])

    def test_lighting_defaults_to_on(self):
        """Omitting the flag must not silently darken production."""
        segment = _build()

        self.assertIsNotNone(segment)
        self.assertIsNotNone(segment["shadeMap"])


if __name__ == "__main__":
    unittest.main()