File: test_aabb.py

package info (click to toggle)
pyrr 0.10.3-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 956 kB
  • sloc: python: 5,865; makefile: 132; sh: 23
file content (155 lines) | stat: -rw-r--r-- 4,450 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
try:
    import unittest2 as unittest
except:
    import unittest
import numpy as np
from pyrr import aabb


class test_aabb(unittest.TestCase):
    def test_import(self):
        import pyrr
        pyrr.aabb
        from pyrr import aabb

    def test_create_zeros(self):
        result = aabb.create_zeros()
        self.assertTrue(np.array_equal(result, [[0,0,0],[0,0,0]]))

    def test_create_from_bounds(self):
        result = aabb.create_from_bounds([-1,-1,-1],[1,1,1])
        self.assertTrue(np.array_equal(result, [[-1,-1,-1],[1,1,1]]))

    def test_create_from_points(self):
        points = np.array([[-1.0,-1.0,-1.0]])
        result = aabb.create_from_points(points)
        expected = np.array([
                [-1.0,-1.0,-1.0],
                [-1.0,-1.0,-1.0]
        ])
        self.assertTrue(np.array_equal(result, expected))

        points = np.array([
            [-1.0,-1.0,-1.0],
            [-1.0, 2.0,-2.0],
        ])
        result = aabb.create_from_points(points)
        expected = np.array([
            [-1.0,-1.0,-2.0],
            [-1.0, 2.0,-1.0],
        ])
        self.assertTrue(np.array_equal(result, expected))

    def test_create_from_aabbs(self):
        # -1
        a1 = np.array([
            [-1.0, 0.0, 0.0 ],
            [-1.0,-1.0,-1.0 ]
        ])
        # +1
        a2 = np.array([
            [ 1.0,-1.0,-1.0 ],
            [ 1.0, 1.0, 1.0 ]
        ])

        # -1 to +1
        result = aabb.create_from_aabbs(np.array([a1, a2]))
        expected = np.array([
            [-1.0,-1.0,-1.0 ],
            [ 1.0, 1.0, 1.0 ]
        ])

        self.assertTrue(np.array_equal(result, expected))

    def test_add_point(self):
        obj = np.array([
            [-1.0,-1.0,-1.0],
            [-1.0,-1.0,-1.0]
        ])
        points = np.array([1.0, 1.0, 1.0])
        result = aabb.add_points(obj, points)
        expected = np.array([
            [-1.0,-1.0,-1.0 ],
            [ 1.0, 1.0, 1.0 ]
        ])
        self.assertTrue(np.array_equal(result, expected))

    def test_add_aabbs(self):
        a = aabb.create_zeros()

        a1 = np.array([
            [-1.0,-1.0,-1.0],
            [ 1.0, 1.0, 1.0]
        ])
        result = aabb.add_aabbs(a, a1)
        expected = np.array([
            [-1.0,-1.0,-1.0],
            [ 1.0, 1.0, 1.0]
        ])
        self.assertTrue(np.array_equal(result, expected))


        a = np.array([
            [-1.0,-1.0,-1.0],
            [ 1.0, 1.0, 1.0]
        ])
        a2 = np.array([
            [-1.0, 0.0,-1.0],
            [ 2.0, 1.0, 1.0]
        ])
        result = aabb.add_aabbs(a, a2)
        expected = np.array([
            [-1.0,-1.0,-1.0],
            [ 2.0, 1.0, 1.0]
        ])
        self.assertTrue(np.array_equal(result, expected))

    def test_centre_point_single_point(self):
        points = np.array([[-1.0,-1.0,-1.0]])
        obj = aabb.create_from_points(points)
        result = aabb.centre_point(obj)
        expected = np.array([-1.0,-1.0,-1.0])
        self.assertTrue(np.array_equal(result, expected))

    def test_centre_point_multiple_points(self):
        points = np.array([
            [ 1.0, 1.0, 1.0],
            [-1.0,-1.0,-1.0]
        ])
        obj = aabb.create_from_points(points)
        result = aabb.centre_point(obj)
        expected = np.zeros(3)
        self.assertTrue(np.array_equal(result, expected))

    def test_minimum(self):
        a = aabb.create_from_bounds([-1,-1,-1],[1,1,1])
        result = aabb.minimum(a)
        self.assertTrue(np.array_equal(result, [-1,-1,-1]))

    def test_maximum(self):
        a = aabb.create_from_bounds([-1,-1,-1],[1,1,1])
        result = aabb.maximum(a)
        self.assertTrue(np.array_equal(result, [1,1,1]))

    def test_clamp_points_single(self):
        a = aabb.create_from_bounds([-1,-1,-1],[1,1,1])
        points = np.array([2,1,1])
        result = aabb.clamp_points(a, points)
        expected = np.array([1,1,1])
        self.assertTrue(np.array_equal(result, expected))

    def test_clamp_points_list(self):
        a = aabb.create_from_bounds([-1,-1,-1],[1,1,1])
        points = np.array([
            [1,1,1],
            [2,1,1],
            [-1,-1,-1],
            [-2,-2,-2],
        ])
        result = aabb.clamp_points(a, points)
        expected = np.array([[1,1,1],[1,1,1],[-1,-1,-1],[-1,-1,-1]])
        self.assertTrue(np.array_equal(result, expected))

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