Coverage for tests/test_Box3d.py : 15%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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
26from lsst.sphgeom import Box3d, CONTAINS, DISJOINT, Interval1d, Vector3d
29class Box3dTestCase(unittest.TestCase):
31 def test_construction(self):
32 a = Box3d(Vector3d(0, 0, 0))
33 b = Box3d(a)
34 self.assertEqual(a, b)
35 self.assertNotEqual(id(a), id(b))
36 a = Box3d(Vector3d(1, 2, 3), Vector3d(3, 4, 5))
37 b = Box3d(Interval1d(1, 3), Interval1d(2, 4), Interval1d(3, 5))
38 c = Box3d(Vector3d(2, 3, 4), 1, 1, 1)
39 self.assertEqual(a, b)
40 self.assertEqual(b, c)
41 i = Interval1d(1, 2)
42 self.assertEqual(i, Interval1d(1, 2))
43 self.assertTrue(Interval1d.empty().isEmpty())
45 def test_comparison_operators(self):
46 self.assertEqual(Box3d(Vector3d(1, 1, 1)),
47 Vector3d(1, 1, 1))
48 self.assertEqual(Box3d(Vector3d(1, 1, 1)),
49 Box3d(Vector3d(1, 1, 1), Vector3d(1, 1, 1)))
50 self.assertEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1),
51 Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 1)))
52 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1),
53 Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 2)))
54 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1),
55 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_expanding_and_clipping(self):
98 a = Box3d(Vector3d(1, 1, 1), Vector3d(2, 2, 2))
99 b = (
100 a.expandedTo(Vector3d(3, 3, 3))
101 .expandedTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1))
102 .clippedTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1))
103 .clippedTo(Vector3d(1, 1, 1))
104 )
105 a.expandTo(Vector3d(3, 3, 3))
106 a.expandTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1))
107 a.clipTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1))
108 a.clipTo(Vector3d(1, 1, 1))
109 self.assertEqual(a, b)
110 self.assertEqual(a, Vector3d(1, 1, 1))
111 a.clipTo(Vector3d(0, 0, 0))
112 self.assertTrue(a.isEmpty())
114 def test_dilation_and_erosion(self):
115 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
116 b = a.dilatedBy(1).erodedBy(2)
117 a.dilateBy(1).erodeBy(2)
118 self.assertEqual(a, b)
119 self.assertEqual(a, Vector3d(0, 0, 0))
121 def test_string(self):
122 b = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
123 self.assertEqual(str(b), '[[-1.0, 1.0],\n'
124 ' [-1.0, 1.0],\n'
125 ' [-1.0, 1.0]]')
126 self.assertEqual(repr(b), 'Box3d(Interval1d(-1.0, 1.0),\n'
127 ' Interval1d(-1.0, 1.0),\n'
128 ' Interval1d(-1.0, 1.0))')
129 self.assertEqual(
130 b, eval(repr(b), dict(Box3d=Box3d, Interval1d=Interval1d)))
132 def test_pickle(self):
133 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1)
134 b = pickle.loads(pickle.dumps(a))
135 self.assertEqual(a, b)
138if __name__ == '__main__': 138 ↛ 139line 138 didn't jump to line 139, because the condition on line 138 was never true
139 unittest.main()