File size: 4,095 Bytes
94b277a
 
 
 
 
2ada41e
 
 
 
94b277a
 
 
 
 
 
 
2ada41e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94b277a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest

import numpy as np

from services.floor.geometry_filter import filter_surface_by_floor_geometry
from services.geometry.multi_plane import (
    _prune_weak_secondary_planes,
    geometry_point_maps,
)
from services.geometry.ransac import (
    _best_surface_hypothesis,
    _candidate_hypotheses,
)


class GeometryOptimizationTests(unittest.TestCase):
    def test_weak_secondary_planes_are_removed_and_ids_are_reindexed(self):
        planes = [
            {"id": 0, "inlierCount": 325_002},
            {"id": 1, "inlierCount": 5_302},
            {"id": 2, "inlierCount": 26_000},
        ]

        retained = _prune_weak_secondary_planes(planes)

        self.assertEqual(
            [plane["inlierCount"] for plane in retained],
            [325_002, 26_000],
        )
        self.assertEqual([plane["id"] for plane in retained], [0, 1])

    def test_batched_ransac_scoring_matches_sequential_scoring(self):
        rng = np.random.default_rng(7)
        xz = rng.uniform(-2, 2, size=(4_000, 2)).astype(np.float32)
        points = np.column_stack((
            xz[:, 0],
            rng.normal(0, 0.008, len(xz)),
            xz[:, 1],
        )).astype(np.float32)
        normals = np.tile(np.array([0, 1, 0], dtype=np.float32), (len(points), 1))
        normals += rng.normal(0, 0.02, normals.shape).astype(np.float32)
        normals /= np.linalg.norm(normals, axis=1, keepdims=True)

        origins, hypotheses = _candidate_hypotheses(
            points,
            iterations=90,
            rng=np.random.default_rng(41),
        )
        distance_threshold = 0.03
        normal_cos = 0.75
        expected_index = -1
        expected_count = 0
        for index, (origin, normal) in enumerate(zip(origins, hypotheses, strict=True)):
            distances = np.abs((points - origin) @ normal)
            alignments = np.abs(normals @ normal)
            count = int(((distances <= distance_threshold) & (alignments >= normal_cos)).sum())
            if count > expected_count:
                expected_count = count
                expected_index = index

        result = _best_surface_hypothesis(
            points,
            origins,
            hypotheses,
            distance_threshold,
            candidate_normals=normals,
            normal_cos=normal_cos,
        )

        self.assertIsNotNone(result)
        origin, normal = result
        np.testing.assert_array_equal(origin, origins[expected_index])
        np.testing.assert_array_equal(normal, hypotheses[expected_index])

    def test_prepared_geometry_maps_preserve_filter_output(self):
        height, width = 120, 160
        y_coordinates = np.arange(height, dtype=np.float32)[:, None]
        x_coordinates = np.arange(width, dtype=np.float32)[None, :]
        points = np.empty((height, width, 3), dtype=np.float32)
        points[:, :, 0] = (x_coordinates - width / 2) / 100
        points[:, :, 1] = 0
        points[:, :, 2] = 1 + y_coordinates / 100
        normals = np.zeros_like(points)
        normals[:, :, 1] = 1
        surface = np.zeros((height, width), dtype=np.uint8)
        surface[height // 3:] = 1
        geometry = {
            "provider": "moge-2",
            "points": points,
            "normals": normals,
            "validMask": np.ones((height, width), dtype=bool),
            "intrinsics": np.eye(3, dtype=np.float32),
        }
        plane = {
            "planeNormal": [0, 1, 0],
            "planeOrigin": [0, 0, 0],
        }

        expected, expected_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
        prepared_maps = geometry_point_maps(surface, geometry)
        actual, actual_metadata = filter_surface_by_floor_geometry(
            surface,
            geometry,
            plane,
            geometry_maps=prepared_maps,
        )

        np.testing.assert_array_equal(actual, expected)
        self.assertEqual(
            actual_metadata["geometrySurfaceFilterReason"],
            expected_metadata["geometrySurfaceFilterReason"],
        )


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