Coverage for tests/test_Box3d.py: 13%
105 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -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
27from lsst.sphgeom import CONTAINS, DISJOINT, Box3d, Interval1d, Vector3d
30class Box3dTestCase(unittest.TestCase):
31 """Test Box3d."""
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)), Vector3d(1, 1, 1))
52 self.assertEqual(Box3d(Vector3d(1, 1, 1)), Box3d(Vector3d(1, 1, 1), Vector3d(1, 1, 1)))
53 self.assertEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 1)))
54 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 2)))
55 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Vector3d(1, 1, 1))
57 def test_center_and_dimensions(self):
58 b = Box3d(Vector3d(1.5, 1.5, 1.5), 0.5, 1.0, 1.5)
59 self.assertEqual(b[0], b[-3])
60 self.assertEqual(b[1], b[-2])
61 self.assertEqual(b[2], b[-1])
62 self.assertEqual(b[0], b.x())
63 self.assertEqual(b[1], b.y())
64 self.assertEqual(b[2], b.z())
65 with self.assertRaises(IndexError):
66 b[-4]
67 with self.assertRaises(IndexError):
68 b[3]
69 self.assertEqual(b.x(), Interval1d(1, 2))
70 self.assertEqual(b.y(), Interval1d(0.5, 2.5))
71 self.assertEqual(b.z(), Interval1d(0, 3))
72 self.assertEqual(b.getCenter(), Vector3d(1.5, 1.5, 1.5))
73 self.assertEqual(b.getWidth(), 1)
74 self.assertEqual(b.getHeight(), 2)
75 self.assertEqual(b.getDepth(), 3)
76 self.assertEqual(b.isEmpty(), False)
77 self.assertEqual(b.isFull(), False)
79 def test_relationships(self):
80 b02 = Box3d(Interval1d(0, 2), Interval1d(0, 2), Interval1d(0, 2))
81 b13 = Box3d(Interval1d(1, 3), Interval1d(1, 3), Interval1d(1, 3))
82 b46 = Box3d(Interval1d(4, 6), Interval1d(4, 6), Interval1d(4, 6))
83 b06 = Box3d(Interval1d(0, 6), Interval1d(0, 6), Interval1d(0, 6))
84 self.assertTrue(b02.contains(Vector3d(1, 1, 1)))
85 self.assertTrue(b02.contains(Box3d(Vector3d(1, 1, 1), 0.5, 0.5, 0.5)))
86 self.assertTrue(b02.isDisjointFrom(Vector3d(3, 3, 3)))
87 self.assertTrue(b02.isDisjointFrom(b46))
88 self.assertTrue(b02.intersects(Vector3d(1, 1, 1)))
89 self.assertTrue(b02.intersects(b13))
90 self.assertTrue(Box3d(Vector3d(1, 1, 1), 0, 0, 0).isWithin(b02))
91 self.assertTrue(b02.isWithin(b06))
92 r = b02.relate(Vector3d(1, 1, 1))
93 self.assertEqual(r, CONTAINS)
94 r = b46.relate(b02)
95 self.assertEqual(r, DISJOINT)
97 def test_vectorized_contains(self):
98 b = Box3d.aroundUnitSphere()
99 x = np.random.rand(5, 3)
100 y = np.random.rand(5, 3)
101 z = np.random.rand(5, 3)
102 c = b.contains(x, y, z)
103 for i in range(x.shape[0]):
104 for j in range(x.shape[1]):
105 u = Vector3d(x[i, j], y[i, j], z[i, j])
106 self.assertEqual(c[i, j], b.contains(u))
107 # test with non-contiguous memory
108 c2 = b.contains(x[::2], y[::2], z[::2])
109 for i in range(x.shape[0], 2):
110 for j in range(x.shape[1]):
111 u = Vector3d(x[i, j], y[i, j], z[i, j])
112 self.assertEqual(c2[i // 2, j], b.contains(u))
114 def test_expanding_and_clipping(self):
115 a = Box3d(Vector3d(1, 1, 1), Vector3d(2, 2, 2))
116 b = (
117 a.expandedTo(Vector3d(3, 3, 3))
118 .expandedTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1))
119 .clippedTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1))
120 .clippedTo(Vector3d(1, 1, 1))
121 )
122 a.expandTo(Vector3d(3, 3, 3))
123 a.expandTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1))
124 a.clipTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1))
125 a.clipTo(Vector3d(1, 1, 1))
126 self.assertEqual(a, b)
127 self.assertEqual(a, Vector3d(1, 1, 1))
128 a.clipTo(Vector3d(0, 0, 0))
129 self.assertTrue(a.isEmpty())
131 def test_dilation_and_erosion(self):
132 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
133 b = a.dilatedBy(1).erodedBy(2)
134 a.dilateBy(1).erodeBy(2)
135 self.assertEqual(a, b)
136 self.assertEqual(a, Vector3d(0, 0, 0))
138 def test_string(self):
139 b = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
140 self.assertEqual(str(b), "[[-1.0, 1.0],\n [-1.0, 1.0],\n [-1.0, 1.0]]")
141 self.assertEqual(
142 repr(b),
143 "Box3d(Interval1d(-1.0, 1.0),\n Interval1d(-1.0, 1.0),\n Interval1d(-1.0, 1.0))",
144 )
145 self.assertEqual(b, eval(repr(b), {"Box3d": Box3d, "Interval1d": Interval1d}))
147 def test_pickle(self):
148 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
149 b = pickle.loads(pickle.dumps(a))
150 self.assertEqual(a, b)
153if __name__ == "__main__":
154 unittest.main()