Coverage for tests/test_ConvexPolygon.py: 20%

75 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-23 18:04 +0000

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 

24 

25try: 

26 import yaml 

27except ImportError: 

28 yaml = None 

29 

30import unittest 

31 

32import numpy as np 

33from lsst.sphgeom import CONTAINS, Circle, ConvexPolygon, Region, UnitVector3d 

34 

35 

36class ConvexPolygonTestCase(unittest.TestCase): 

37 def testConstruction(self): 

38 points = [UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()] 

39 p1 = ConvexPolygon(points) 

40 self.assertEqual(points, p1.getVertices()) 

41 p2 = p1.clone() 

42 self.assertEqual(p1, p2) 

43 p3 = ConvexPolygon([-UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

44 self.assertNotEqual(p1, p3) 

45 p4 = ConvexPolygon.convexHull( 

46 [UnitVector3d.Y(), UnitVector3d.X(), UnitVector3d(1, 1, 1), UnitVector3d.Z()] 

47 ) 

48 self.assertEqual(p1, p4) 

49 

50 def testCodec(self): 

51 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

52 s = p.encode() 

53 self.assertEqual(ConvexPolygon.decode(s), p) 

54 self.assertEqual(Region.decode(s), p) 

55 

56 def testRelationships(self): 

57 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

58 self.assertTrue(p.contains(p.getCentroid())) 

59 boundingCircle = p.getBoundingCircle() 

60 self.assertEqual(boundingCircle.relate(p), CONTAINS) 

61 self.assertTrue(p.isWithin(boundingCircle)) 

62 self.assertTrue(p.intersects(boundingCircle)) 

63 self.assertFalse(p.isDisjointFrom(boundingCircle)) 

64 self.assertFalse(p.contains(boundingCircle)) 

65 tinyCircle = Circle(boundingCircle.getCenter()) 

66 self.assertFalse(p.isWithin(tinyCircle)) 

67 self.assertTrue(p.intersects(tinyCircle)) 

68 self.assertFalse(p.isDisjointFrom(tinyCircle)) 

69 self.assertTrue(p.contains(tinyCircle)) 

70 

71 def test_vectorized_contains(self): 

72 b = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

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

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

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

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

77 lon = np.arctan2(y, x) 

78 lat = np.arctan2(z, np.hypot(x, y)) 

79 c2 = b.contains(lon, lat) 

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

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

82 u = UnitVector3d(x[i, j], y[i, j], z[i, j]) 

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

84 self.assertEqual(c2[i, j], b.contains(u)) 

85 # test with non-contiguous memory 

86 c3 = b.contains(x[::2], y[::2], z[::2]) 

87 c4 = b.contains(lon[::2], lat[::2]) 

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

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

90 u = UnitVector3d(x[i, j], y[i, j], z[i, j]) 

91 self.assertEqual(c3[i // 2, j], b.contains(u)) 

92 self.assertEqual(c4[i // 2, j], b.contains(u)) 

93 

94 def testString(self): 

95 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

96 self.assertEqual(str(p), repr(p)) 

97 self.assertEqual( 

98 repr(p), 

99 "ConvexPolygon([UnitVector3d(0.0, 0.0, 1.0), " 

100 "UnitVector3d(1.0, 0.0, 0.0), " 

101 "UnitVector3d(0.0, 1.0, 0.0)])", 

102 ) 

103 self.assertEqual(p, eval(repr(p), dict(ConvexPolygon=ConvexPolygon, UnitVector3d=UnitVector3d))) 

104 

105 def testPickle(self): 

106 a = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

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

108 self.assertEqual(a, b) 

109 

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

111 def testYaml(self): 

112 a = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]) 

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

114 self.assertEqual(a, b) 

115 

116 

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

118 unittest.main()