Coverage for tests/test_Box3d.py: 13%
105 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
1# This file is part of sphgeom.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import pickle
29import unittest
31import numpy as np
32from lsst.sphgeom import CONTAINS, DISJOINT, Box3d, Interval1d, Vector3d
35class Box3dTestCase(unittest.TestCase):
36 """Test Box3d."""
38 def setUp(self):
39 np.random.seed(1)
41 def test_construction(self):
42 a = Box3d(Vector3d(0, 0, 0))
43 b = Box3d(a)
44 self.assertEqual(a, b)
45 self.assertNotEqual(id(a), id(b))
46 a = Box3d(Vector3d(1, 2, 3), Vector3d(3, 4, 5))
47 b = Box3d(Interval1d(1, 3), Interval1d(2, 4), Interval1d(3, 5))
48 c = Box3d(Vector3d(2, 3, 4), 1, 1, 1)
49 self.assertEqual(a, b)
50 self.assertEqual(b, c)
51 i = Interval1d(1, 2)
52 self.assertEqual(i, Interval1d(1, 2))
53 self.assertTrue(Interval1d.empty().isEmpty())
55 def test_comparison_operators(self):
56 self.assertEqual(Box3d(Vector3d(1, 1, 1)), Vector3d(1, 1, 1))
57 self.assertEqual(Box3d(Vector3d(1, 1, 1)), Box3d(Vector3d(1, 1, 1), Vector3d(1, 1, 1)))
58 self.assertEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 1)))
59 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 2)))
60 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), 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 [-1.0, 1.0],\n [-1.0, 1.0]]")
146 self.assertEqual(
147 repr(b),
148 "Box3d(Interval1d(-1.0, 1.0),\n Interval1d(-1.0, 1.0),\n Interval1d(-1.0, 1.0))",
149 )
150 self.assertEqual(b, eval(repr(b), {"Box3d": Box3d, "Interval1d": Interval1d}))
152 def test_pickle(self):
153 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
154 b = pickle.loads(pickle.dumps(a))
155 self.assertEqual(a, b)
158if __name__ == "__main__":
159 unittest.main()