Coverage for tests/test_ConvexPolygon.py: 20%

73 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-02 03:12 -0700

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/>. 

27 

28import pickle 

29 

30try: 

31 import yaml 

32except ImportError: 

33 yaml = None 

34 

35import unittest 

36 

37import numpy as np 

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

39 

40 

41class ConvexPolygonTestCase(unittest.TestCase): 

42 """Test ConvexPolygon.""" 

43 

44 def testConstruction(self): 

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

46 p1 = ConvexPolygon(points) 

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

48 p2 = p1.clone() 

49 self.assertEqual(p1, p2) 

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

51 self.assertNotEqual(p1, p3) 

52 p4 = ConvexPolygon.convexHull( 

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

54 ) 

55 self.assertEqual(p1, p4) 

56 

57 def testCodec(self): 

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

59 s = p.encode() 

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

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

62 

63 def testRelationships(self): 

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

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

66 boundingCircle = p.getBoundingCircle() 

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

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

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

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

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

72 tinyCircle = Circle(boundingCircle.getCenter()) 

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

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

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

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

77 

78 def test_vectorized_contains(self): 

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

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

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

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

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

84 lon = np.arctan2(y, x) 

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

86 c2 = b.contains(lon, lat) 

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

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

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

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

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

92 # test with non-contiguous memory 

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

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

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

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

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

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

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

100 

101 def testString(self): 

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

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

104 self.assertEqual( 

105 repr(p), 

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

107 "UnitVector3d(1.0, 0.0, 0.0), " 

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

109 ) 

110 self.assertEqual(p, eval(repr(p), {"ConvexPolygon": ConvexPolygon, "UnitVector3d": UnitVector3d})) 

111 

112 def testPickle(self): 

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

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

115 self.assertEqual(a, b) 

116 

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

118 def testYaml(self): 

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

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

121 self.assertEqual(a, b) 

122 

123 

124if __name__ == "__main__": 

125 unittest.main()