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