Coverage for tests/test_Region.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 09:17 +0000

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 unittest 

29 

30from lsst.sphgeom import ( 

31 CONTAINS, 

32 Angle, 

33 Box, 

34 Circle, 

35 ConvexPolygon, 

36 Ellipse, 

37 IntersectionRegion, 

38 LonLat, 

39 UnionRegion, 

40 UnitVector3d, 

41) 

42 

43 

44class RegionTestCase(unittest.TestCase): 

45 """Test the methods defined on the Region base class.""" 

46 

47 def setUp(self): 

48 self.regions = [ 

49 Box(LonLat.fromRadians(0.0, 1.0), Angle(0.2), Angle(0.2)), 

50 Circle(UnitVector3d.Z(), Angle(0.3)), 

51 ConvexPolygon( 

52 [ 

53 UnitVector3d(1.0, 0.0, 0.1), 

54 UnitVector3d(0.0, 1.0, 0.1), 

55 UnitVector3d(-1.0, -1.0, 0.1), 

56 ] 

57 ), 

58 Ellipse(UnitVector3d.Z(), Angle(0.3)), 

59 UnionRegion(Circle(UnitVector3d.X(), Angle(0.2)), Circle(UnitVector3d.Y(), Angle(0.2))), 

60 IntersectionRegion(Circle(UnitVector3d.X(), Angle(0.4)), Circle(UnitVector3d.X(), Angle(0.2))), 

61 ] 

62 

63 def test_contains_region(self): 

64 outer = Circle(UnitVector3d.Z(), Angle(0.5)) 

65 inner = Circle(UnitVector3d.Z(), Angle(0.1)) 

66 self.assertTrue(outer.contains(inner)) 

67 self.assertFalse(inner.contains(outer)) 

68 

69 def test_contains_region_all_types(self): 

70 """Every region type accepts a Region argument. 

71 

72 Subclasses that define their own ``contains`` overloads hide the ones 

73 bound on Region, so each of them has to rewrap it explicitly. 

74 """ 

75 for region in self.regions: 

76 for other in self.regions: 

77 with self.subTest(region=type(region).__name__, other=type(other).__name__): 

78 self.assertIsInstance(region.contains(other), bool) 

79 

80 def test_contains_region_agrees_with_relate(self): 

81 """``contains`` is defined in terms of ``relate``, and so is equally 

82 conservative: a false result only means containment could not be 

83 established. 

84 """ 

85 for region in self.regions: 

86 for other in self.regions: 

87 with self.subTest(region=type(region).__name__, other=type(other).__name__): 

88 self.assertEqual(region.contains(other), (region.relate(other) & CONTAINS) != 0) 

89 

90 

91if __name__ == "__main__": 

92 unittest.main()