Coverage for tests/test_Box3d.py: 15%

107 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-01 00:58 -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# 

22 

23import pickle 

24import unittest 

25 

26import numpy as np 

27from lsst.sphgeom import CONTAINS, DISJOINT, Box3d, Interval1d, Vector3d 

28 

29 

30class Box3dTestCase(unittest.TestCase): 

31 def setUp(self): 

32 np.random.seed(1) 

33 

34 def test_construction(self): 

35 a = Box3d(Vector3d(0, 0, 0)) 

36 b = Box3d(a) 

37 self.assertEqual(a, b) 

38 self.assertNotEqual(id(a), id(b)) 

39 a = Box3d(Vector3d(1, 2, 3), Vector3d(3, 4, 5)) 

40 b = Box3d(Interval1d(1, 3), Interval1d(2, 4), Interval1d(3, 5)) 

41 c = Box3d(Vector3d(2, 3, 4), 1, 1, 1) 

42 self.assertEqual(a, b) 

43 self.assertEqual(b, c) 

44 i = Interval1d(1, 2) 

45 self.assertEqual(i, Interval1d(1, 2)) 

46 self.assertTrue(Interval1d.empty().isEmpty()) 

47 

48 def test_comparison_operators(self): 

49 self.assertEqual(Box3d(Vector3d(1, 1, 1)), Vector3d(1, 1, 1)) 

50 self.assertEqual(Box3d(Vector3d(1, 1, 1)), Box3d(Vector3d(1, 1, 1), Vector3d(1, 1, 1))) 

51 self.assertEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 1))) 

52 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Box3d(Vector3d(-1, -1, -1), Vector3d(1, 1, 2))) 

53 self.assertNotEqual(Box3d(Vector3d(0, 0, 0), 1, 1, 1), Vector3d(1, 1, 1)) 

54 

55 def test_center_and_dimensions(self): 

56 b = Box3d(Vector3d(1.5, 1.5, 1.5), 0.5, 1.0, 1.5) 

57 self.assertEqual(b[0], b[-3]) 

58 self.assertEqual(b[1], b[-2]) 

59 self.assertEqual(b[2], b[-1]) 

60 self.assertEqual(b[0], b.x()) 

61 self.assertEqual(b[1], b.y()) 

62 self.assertEqual(b[2], b.z()) 

63 with self.assertRaises(IndexError): 

64 b[-4] 

65 with self.assertRaises(IndexError): 

66 b[3] 

67 self.assertEqual(b.x(), Interval1d(1, 2)) 

68 self.assertEqual(b.y(), Interval1d(0.5, 2.5)) 

69 self.assertEqual(b.z(), Interval1d(0, 3)) 

70 self.assertEqual(b.getCenter(), Vector3d(1.5, 1.5, 1.5)) 

71 self.assertEqual(b.getWidth(), 1) 

72 self.assertEqual(b.getHeight(), 2) 

73 self.assertEqual(b.getDepth(), 3) 

74 self.assertEqual(b.isEmpty(), False) 

75 self.assertEqual(b.isFull(), False) 

76 

77 def test_relationships(self): 

78 b02 = Box3d(Interval1d(0, 2), Interval1d(0, 2), Interval1d(0, 2)) 

79 b13 = Box3d(Interval1d(1, 3), Interval1d(1, 3), Interval1d(1, 3)) 

80 b46 = Box3d(Interval1d(4, 6), Interval1d(4, 6), Interval1d(4, 6)) 

81 b06 = Box3d(Interval1d(0, 6), Interval1d(0, 6), Interval1d(0, 6)) 

82 self.assertTrue(b02.contains(Vector3d(1, 1, 1))) 

83 self.assertTrue(b02.contains(Box3d(Vector3d(1, 1, 1), 0.5, 0.5, 0.5))) 

84 self.assertTrue(b02.isDisjointFrom(Vector3d(3, 3, 3))) 

85 self.assertTrue(b02.isDisjointFrom(b46)) 

86 self.assertTrue(b02.intersects(Vector3d(1, 1, 1))) 

87 self.assertTrue(b02.intersects(b13)) 

88 self.assertTrue(Box3d(Vector3d(1, 1, 1), 0, 0, 0).isWithin(b02)) 

89 self.assertTrue(b02.isWithin(b06)) 

90 r = b02.relate(Vector3d(1, 1, 1)) 

91 self.assertEqual(r, CONTAINS) 

92 r = b46.relate(b02) 

93 self.assertEqual(r, DISJOINT) 

94 

95 def test_vectorized_contains(self): 

96 b = Box3d.aroundUnitSphere() 

97 x = np.random.rand(5, 3) 

98 y = np.random.rand(5, 3) 

99 z = np.random.rand(5, 3) 

100 c = b.contains(x, y, z) 

101 for i in range(x.shape[0]): 

102 for j in range(x.shape[1]): 

103 u = Vector3d(x[i, j], y[i, j], z[i, j]) 

104 self.assertEqual(c[i, j], b.contains(u)) 

105 # test with non-contiguous memory 

106 c2 = b.contains(x[::2], y[::2], z[::2]) 

107 for i in range(x.shape[0], 2): 

108 for j in range(x.shape[1]): 

109 u = Vector3d(x[i, j], y[i, j], z[i, j]) 

110 self.assertEqual(c2[i // 2, j], b.contains(u)) 

111 

112 def test_expanding_and_clipping(self): 

113 a = Box3d(Vector3d(1, 1, 1), Vector3d(2, 2, 2)) 

114 b = ( 

115 a.expandedTo(Vector3d(3, 3, 3)) 

116 .expandedTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1)) 

117 .clippedTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1)) 

118 .clippedTo(Vector3d(1, 1, 1)) 

119 ) 

120 a.expandTo(Vector3d(3, 3, 3)) 

121 a.expandTo(Box3d(Vector3d(3, 3, 3), 1, 1, 1)) 

122 a.clipTo(Box3d(Vector3d(1, 1, 1), 1, 1, 1)) 

123 a.clipTo(Vector3d(1, 1, 1)) 

124 self.assertEqual(a, b) 

125 self.assertEqual(a, Vector3d(1, 1, 1)) 

126 a.clipTo(Vector3d(0, 0, 0)) 

127 self.assertTrue(a.isEmpty()) 

128 

129 def test_dilation_and_erosion(self): 

130 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1) 

131 b = a.dilatedBy(1).erodedBy(2) 

132 a.dilateBy(1).erodeBy(2) 

133 self.assertEqual(a, b) 

134 self.assertEqual(a, Vector3d(0, 0, 0)) 

135 

136 def test_string(self): 

137 b = Box3d(Vector3d(0, 0, 0), 1, 1, 1) 

138 self.assertEqual(str(b), "[[-1.0, 1.0],\n" " [-1.0, 1.0],\n" " [-1.0, 1.0]]") 

139 self.assertEqual( 

140 repr(b), 

141 "Box3d(Interval1d(-1.0, 1.0),\n" " Interval1d(-1.0, 1.0),\n" " Interval1d(-1.0, 1.0))", 

142 ) 

143 self.assertEqual(b, eval(repr(b), dict(Box3d=Box3d, Interval1d=Interval1d))) 

144 

145 def test_pickle(self): 

146 a = Box3d(Vector3d(0, 0, 0), 1, 1, 1) 

147 b = pickle.loads(pickle.dumps(a)) 

148 self.assertEqual(a, b) 

149 

150 

151if __name__ == "__main__": 151 ↛ 152line 151 didn't jump to line 152, because the condition on line 151 was never true

152 unittest.main()