Hide keyboard shortcuts

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# 

22 

23import pickle 

24try: 

25 import yaml 

26except ImportError: 

27 yaml = None 

28 

29import math 

30import unittest 

31 

32from lsst.sphgeom import (Angle, CONTAINS, Circle, DISJOINT, Region, 

33 UnitVector3d) 

34 

35 

36class CircleTestCase(unittest.TestCase): 

37 

38 def test_construction(self): 

39 self.assertTrue(Circle.empty().isEmpty()) 

40 self.assertTrue(Circle().isEmpty()) 

41 self.assertTrue(Circle.full().isFull()) 

42 c = Circle(UnitVector3d.X()) 

43 self.assertEqual(c.getOpeningAngle(), Angle(0)) 

44 self.assertEqual(c.getSquaredChordLength(), 0) 

45 c = Circle(UnitVector3d.Z(), 2.0) 

46 self.assertTrue(c.contains(UnitVector3d.Z())) 

47 c = Circle(UnitVector3d.Z(), Angle(math.pi)) 

48 self.assertTrue(c.isFull()) 

49 d = c.clone() 

50 self.assertEqual(c, d) 

51 self.assertNotEqual(id(c), id(d)) 

52 e = Circle(d) 

53 self.assertEqual(d, e) 

54 

55 def test_comparison_operators(self): 

56 c = Circle(UnitVector3d.X(), 4.0) 

57 d = Circle(UnitVector3d.Y(), 4.0) 

58 self.assertEqual(c, d) 

59 self.assertTrue(c.isFull()) 

60 self.assertNotEqual(c, Circle(UnitVector3d.Z())) 

61 

62 def test_center_and_dimensions(self): 

63 c = Circle(UnitVector3d.X(), 1) 

64 self.assertEqual(c.getCenter(), UnitVector3d.X()) 

65 self.assertEqual(c.getSquaredChordLength(), 1) 

66 self.assertAlmostEqual(c.getOpeningAngle().asRadians(), math.pi / 3) 

67 

68 def test_relationships(self): 

69 c = Circle(UnitVector3d.X(), Angle.fromDegrees(0.1)) 

70 d = Circle(UnitVector3d(1, 1, 1), Angle(math.pi / 2)) 

71 e = Circle(-UnitVector3d.X()) 

72 self.assertTrue(c.contains(UnitVector3d.X())) 

73 self.assertTrue(UnitVector3d.X() in c) 

74 self.assertTrue(d.contains(c)) 

75 self.assertTrue(c.isWithin(d)) 

76 self.assertTrue(c.intersects(d)) 

77 self.assertTrue(c.intersects(UnitVector3d.X())) 

78 self.assertTrue(e.isDisjointFrom(d)) 

79 self.assertEqual(d.relate(c), CONTAINS) 

80 self.assertEqual(e.relate(d), DISJOINT) 

81 

82 def test_expanding_and_clipping(self): 

83 a = Circle.empty() 

84 b = (a.expandedTo(UnitVector3d.X()) 

85 .expandedTo(Circle(UnitVector3d.Y(), 1)) 

86 .clippedTo(Circle(UnitVector3d(1, 1, 0), 1)) 

87 .clippedTo(UnitVector3d.Y())) 

88 a.expandTo(UnitVector3d.X()) 

89 a.expandTo(Circle(UnitVector3d.Y(), 1)) 

90 a.clipTo(Circle(UnitVector3d(1, 1, 0), 1)) 

91 a.clipTo(UnitVector3d.Y()) 

92 self.assertEqual(a, b) 

93 self.assertEqual(a, Circle(UnitVector3d.Y())) 

94 a.clipTo(UnitVector3d.Z()) 

95 self.assertTrue(a.isEmpty()) 

96 

97 def test_dilation_and_erosion(self): 

98 a = Angle(math.pi / 2) 

99 c = Circle(UnitVector3d.X()) 

100 d = c.dilatedBy(a).erodedBy(a) 

101 c.dilateBy(a).erodeBy(a) 

102 self.assertEqual(c, d) 

103 self.assertEqual(c, Circle(UnitVector3d.X())) 

104 

105 def test_complement(self): 

106 c = Circle(UnitVector3d.X(), 2.0) 

107 d = c.complemented() 

108 c.complement() 

109 self.assertEqual(c, d) 

110 self.assertEqual(c.getCenter(), -UnitVector3d.X()) 

111 self.assertEqual(c.getSquaredChordLength(), 2.0) 

112 

113 def test_area(self): 

114 c = Circle(UnitVector3d(1, 1, 1), 2.0) 

115 self.assertAlmostEqual(c.getArea(), 2 * math.pi) 

116 

117 def test_codec(self): 

118 c = Circle(UnitVector3d.Y(), 1.0) 

119 s = c.encode() 

120 self.assertEqual(Circle.decode(s), c) 

121 self.assertEqual(Region.decode(s), c) 

122 

123 def test_string(self): 

124 c = Circle(UnitVector3d.Z(), Angle(1.0)) 

125 self.assertEqual(str(c), 'Circle([0.0, 0.0, 1.0], 1.0)') 

126 self.assertEqual(repr(c), 

127 'Circle(UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))') 

128 self.assertEqual(c, eval(repr(c), dict( 

129 Angle=Angle, Circle=Circle, UnitVector3d=UnitVector3d))) 

130 

131 def test_pickle(self): 

132 a = Circle(UnitVector3d(1, -1, 1), 1.0) 

133 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL)) 

134 self.assertEqual(a, b) 

135 

136 @unittest.skipIf(not yaml, "YAML module can not be imported") 

137 def test_yaml(self): 

138 a = Circle(UnitVector3d(1, -1, 1), 1.0) 

139 b = yaml.safe_load(yaml.dump(a)) 

140 self.assertEqual(a, b) 

141 

142 

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

144 unittest.main()