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 unittest 

30 

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

32 

33 

34class ConvexPolygonTestCase(unittest.TestCase): 

35 

36 def testConstruction(self): 

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

38 p1 = ConvexPolygon(points) 

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

40 p2 = p1.clone() 

41 self.assertEqual(p1, p2) 

42 p3 = ConvexPolygon([-UnitVector3d.Z(), 

43 UnitVector3d.X(), 

44 UnitVector3d.Y()]) 

45 self.assertNotEqual(p1, p3) 

46 p4 = ConvexPolygon.convexHull([UnitVector3d.Y(), 

47 UnitVector3d.X(), 

48 UnitVector3d(1, 1, 1), 

49 UnitVector3d.Z()]) 

50 self.assertEqual(p1, p4) 

51 

52 def testCodec(self): 

53 p = ConvexPolygon([UnitVector3d.Z(), 

54 UnitVector3d.X(), 

55 UnitVector3d.Y()]) 

56 s = p.encode() 

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

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

59 

60 def testRelationships(self): 

61 p = ConvexPolygon([UnitVector3d.Z(), 

62 UnitVector3d.X(), 

63 UnitVector3d.Y()]) 

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

65 boundingCircle = p.getBoundingCircle() 

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

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

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

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

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

71 tinyCircle = Circle(boundingCircle.getCenter()) 

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

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

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

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

76 

77 def testString(self): 

78 p = ConvexPolygon([UnitVector3d.Z(), 

79 UnitVector3d.X(), 

80 UnitVector3d.Y()]) 

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

82 self.assertEqual(repr(p), 

83 'ConvexPolygon([UnitVector3d(0.0, 0.0, 1.0), ' 

84 'UnitVector3d(1.0, 0.0, 0.0), ' 

85 'UnitVector3d(0.0, 1.0, 0.0)])') 

86 self.assertEqual( 

87 p, eval(repr(p), dict(ConvexPolygon=ConvexPolygon, 

88 UnitVector3d=UnitVector3d))) 

89 

90 def testPickle(self): 

91 a = ConvexPolygon([UnitVector3d.Z(), 

92 UnitVector3d.X(), 

93 UnitVector3d.Y()]) 

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

95 self.assertEqual(a, b) 

96 

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

98 def testYaml(self): 

99 a = ConvexPolygon([UnitVector3d.Z(), 

100 UnitVector3d.X(), 

101 UnitVector3d.Y()]) 

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

103 self.assertEqual(a, b) 

104 

105 

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

107 unittest.main()