Coverage for tests/test_Box3d.py: 14%
107 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -0700
1#
2# LSST Data Management System
3# See COPYRIGHT file at the top of the source tree.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
23import pickle
24import unittest
26import numpy as np
28from lsst.sphgeom import Box3d, CONTAINS, DISJOINT, Interval1d, Vector3d
31class Box3dTestCase(unittest.TestCase):
33 def setUp(self):
34 np.random.seed(1)
36 def test_construction(self):
37 a = Box3d(Vector3d(0, 0, 0))
38 b = Box3d(a)
39 self.assertEqual(a, b)
40 self.assertNotEqual(id(a), id(b))
41 a = Box3d(Vector3d(1, 2, 3), Vector3d(3, 4, 5))
42 b = Box3d(Interval1d(1, 3), Interval1d(2, 4), Interval1d(3, 5))
43 c = Box3d(Vector3d(2, 3, 4), 1, 1, 1)
44 self.assertEqual(a, b)
45 self.assertEqual(b, c)
46 i = Interval1d(1, 2)
47 self.assertEqual(i, Interval1d(1, 2))
48 self.assertTrue(Interval1d.empty().isEmpty())
50 def test_comparison_operators(self):
51 self.assertEqual(Box3d(Vector3d(1, 1, 1)),
52 Vector3d(1, 1, 1))
53 self.assertEqual(Box3d(Vector3d(1, 1, 1)),
54 Box3d(Vector3d(1, 1, 1), Vector3d(1, 1, 1)))
55 self.assertEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1),
56 Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 1)))
57 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1),
58 Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 2)))
59 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1),
60 Vector3d(1, 1, 1))
62 def test_center_and_dimensions(self):
63 b = Box3d(Vector3d(1.5, 1.5, 1.5), 0.5, 1.0, 1.5)
64 self.assertEqual(b[0], b[-3])
65 self.assertEqual(b[1], b[-2])
66 self.assertEqual(b[2], b[-1])
67 self.assertEqual(b[0], b.x())
68 self.assertEqual(b[1], b.y())
69 self.assertEqual(b[2], b.z())
70 with self.assertRaises(IndexError):
71 b[-4]
72 with self.assertRaises(IndexError):
73 b[3]
74 self.assertEqual(b.x(), Interval1d(1, 2))
75 self.assertEqual(b.y(), Interval1d(0.5, 2.5))
76 self.assertEqual(b.z(), Interval1d(0, 3))
77 self.assertEqual(b.getCenter(), Vector3d(1.5, 1.5, 1.5))
78 self.assertEqual(b.getWidth(), 1)
79 self.assertEqual(b.getHeight(), 2)
80 self.assertEqual(b.getDepth(), 3)
81 self.assertEqual(b.isEmpty(), False)
82 self.assertEqual(b.isFull(), False)
84 def test_relationships(self):
85 b02 = Box3d(Interval1d(0, 2), Interval1d(0, 2), Interval1d(0, 2))
86 b13 = Box3d(Interval1d(1, 3), Interval1d(1, 3), Interval1d(1, 3))
87 b46 = Box3d(Interval1d(4, 6), Interval1d(4, 6), Interval1d(4, 6))
88 b06 = Box3d(Interval1d(0, 6), Interval1d(0, 6), Interval1d(0, 6))
89 self.assertTrue(b02.contains(Vector3d(1, 1, 1)))
90 self.assertTrue(b02.contains(Box3d(Vector3d(1, 1, 1), 0.5, 0.5, 0.5)))
91 self.assertTrue(b02.isDisjointFrom(Vector3d(3, 3, 3)))
92 self.assertTrue(b02.isDisjointFrom(b46))
93 self.assertTrue(b02.intersects(Vector3d(1, 1, 1)))
94 self.assertTrue(b02.intersects(b13))
95 self.assertTrue(Box3d(Vector3d(1, 1, 1), 0, 0, 0).isWithin(b02))
96 self.assertTrue(b02.isWithin(b06))
97 r = b02.relate(Vector3d(1, 1, 1))
98 self.assertEqual(r, CONTAINS)
99 r = b46.relate(b02)
100 self.assertEqual(r, DISJOINT)
102 def test_vectorized_contains(self):
103 b = Box3d.aroundUnitSphere()
104 x = np.random.rand(5, 3)
105 y = np.random.rand(5, 3)
106 z = np.random.rand(5, 3)
107 c = b.contains(x, y, z)
108 for i in range(x.shape[0]):
109 for j in range(x.shape[1]):
110 u = Vector3d(x[i, j], y[i, j], z[i, j])
111 self.assertEqual(c[i, j], b.contains(u))
112 # test with non-contiguous memory
113 c2 = b.contains(x[::2], y[::2], z[::2])
114 for i in range(x.shape[0], 2):
115 for j in range(x.shape[1]):
116 u = Vector3d(x[i, j], y[i, j], z[i, j])
117 self.assertEqual(c2[i//2, j], b.contains(u))
119 def test_expanding_and_clipping(self):
120 a = Box3d(Vector3d(1, 1, 1), Vector3d(2, 2, 2))
121 b = (
122 a.expandedTo(Vector3d(3, 3, 3))
123 .expandedTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1))
124 .clippedTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1))
125 .clippedTo(Vector3d(1, 1, 1))
126 )
127 a.expandTo(Vector3d(3, 3, 3))
128 a.expandTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1))
129 a.clipTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1))
130 a.clipTo(Vector3d(1, 1, 1))
131 self.assertEqual(a, b)
132 self.assertEqual(a, Vector3d(1, 1, 1))
133 a.clipTo(Vector3d(0, 0, 0))
134 self.assertTrue(a.isEmpty())
136 def test_dilation_and_erosion(self):
137 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
138 b = a.dilatedBy(1).erodedBy(2)
139 a.dilateBy(1).erodeBy(2)
140 self.assertEqual(a, b)
141 self.assertEqual(a, Vector3d(0, 0, 0))
143 def test_string(self):
144 b = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
145 self.assertEqual(str(b), '[[-1.0, 1.0],\n'
146 ' [-1.0, 1.0],\n'
147 ' [-1.0, 1.0]]')
148 self.assertEqual(repr(b), 'Box3d(Interval1d(-1.0, 1.0),\n'
149 ' Interval1d(-1.0, 1.0),\n'
150 ' Interval1d(-1.0, 1.0))')
151 self.assertEqual(
152 b, eval(repr(b), dict(Box3d=Box3d, Interval1d=Interval1d)))
154 def test_pickle(self):
155 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
156 b = pickle.loads(pickle.dumps(a))
157 self.assertEqual(a, b)
160if __name__ == '__main__': 160 ↛ 161line 160 didn't jump to line 161, because the condition on line 160 was never true
161 unittest.main()