Coverage for tests / test_Circle.py: 18%
122 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:41 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 08:41 +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/>.
28import pickle
30try:
31 import yaml
32except ImportError:
33 yaml = None
35import math
36import unittest
38import numpy as np
40from lsst.sphgeom import CONTAINS, DISJOINT, Angle, Circle, Region, UnitVector3d
43class CircleTestCase(unittest.TestCase):
44 """Test Circle."""
46 def setUp(self):
47 np.random.seed(1)
49 def test_construction(self):
50 self.assertTrue(Circle.empty().isEmpty())
51 self.assertTrue(Circle().isEmpty())
52 self.assertTrue(Circle.full().isFull())
53 c = Circle(UnitVector3d.X())
54 self.assertEqual(c.getOpeningAngle(), Angle(0))
55 self.assertEqual(c.getSquaredChordLength(), 0)
56 c = Circle(UnitVector3d.Z(), 2.0)
57 self.assertTrue(c.contains(UnitVector3d.Z()))
58 c = Circle(UnitVector3d.Z(), Angle(math.pi))
59 self.assertTrue(c.isFull())
60 d = c.clone()
61 self.assertEqual(c, d)
62 self.assertNotEqual(id(c), id(d))
63 e = Circle(d)
64 self.assertEqual(d, e)
66 def test_comparison_operators(self):
67 c = Circle(UnitVector3d.X(), 4.0)
68 d = Circle(UnitVector3d.Y(), 4.0)
69 self.assertEqual(c, d)
70 self.assertTrue(c.isFull())
71 self.assertNotEqual(c, Circle(UnitVector3d.Z()))
73 def test_center_and_dimensions(self):
74 c = Circle(UnitVector3d.X(), 1)
75 self.assertEqual(c.getCenter(), UnitVector3d.X())
76 self.assertEqual(c.getSquaredChordLength(), 1)
77 self.assertAlmostEqual(c.getOpeningAngle().asRadians(), math.pi / 3)
79 def test_relationships(self):
80 c = Circle(UnitVector3d.X(), Angle.fromDegrees(0.1))
81 d = Circle(UnitVector3d(1, 1, 1), Angle(math.pi / 2))
82 e = Circle(-UnitVector3d.X())
83 self.assertTrue(c.contains(UnitVector3d.X()))
84 self.assertTrue(UnitVector3d.X() in c)
85 self.assertTrue(d.contains(c))
86 self.assertTrue(c.isWithin(d))
87 self.assertTrue(c.intersects(d))
88 self.assertTrue(c.intersects(UnitVector3d.X()))
89 self.assertTrue(e.isDisjointFrom(d))
90 self.assertEqual(d.relate(c), CONTAINS)
91 self.assertEqual(d.overlaps(c), True)
92 self.assertEqual(e.relate(d), DISJOINT)
93 self.assertEqual(e.overlaps(d), False)
95 def test_vectorized_contains(self):
96 b = Circle(UnitVector3d(*np.random.randn(3)), Angle(0.4 * math.pi))
97 x = np.random.rand(5, 3)
98 y = np.random.rand(5, 3)
99 z = np.random.rand(5, 3)
100 c = b.contains(x, y, z)
101 lon = np.arctan2(y, x)
102 lat = np.arctan2(z, np.hypot(x, y))
103 c2 = b.contains(lon, lat)
104 for i in range(x.shape[0]):
105 for j in range(x.shape[1]):
106 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
107 self.assertEqual(c[i, j], b.contains(u))
108 self.assertEqual(c2[i, j], b.contains(u))
109 # test with non-contiguous memory
110 c3 = b.contains(x[::2], y[::2], z[::2])
111 c4 = b.contains(lon[::2], lat[::2])
112 for i in range(x.shape[0], 2):
113 for j in range(x.shape[1]):
114 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
115 self.assertEqual(c3[i // 2, j], b.contains(u))
116 self.assertEqual(c4[i // 2, j], b.contains(u))
118 def test_expanding_and_clipping(self):
119 a = Circle.empty()
120 b = (
121 a.expandedTo(UnitVector3d.X())
122 .expandedTo(Circle(UnitVector3d.Y(), 1))
123 .clippedTo(Circle(UnitVector3d(1, 1, 0), 1))
124 .clippedTo(UnitVector3d.Y())
125 )
126 a.expandTo(UnitVector3d.X())
127 a.expandTo(Circle(UnitVector3d.Y(), 1))
128 a.clipTo(Circle(UnitVector3d(1, 1, 0), 1))
129 a.clipTo(UnitVector3d.Y())
130 self.assertEqual(a, b)
131 self.assertEqual(a, Circle(UnitVector3d.Y()))
132 a.clipTo(UnitVector3d.Z())
133 self.assertTrue(a.isEmpty())
135 def test_dilation_and_erosion(self):
136 a = Angle(math.pi / 2)
137 c = Circle(UnitVector3d.X())
138 d = c.dilatedBy(a).erodedBy(a)
139 c.dilateBy(a).erodeBy(a)
140 self.assertEqual(c, d)
141 self.assertEqual(c, Circle(UnitVector3d.X()))
143 def test_complement(self):
144 c = Circle(UnitVector3d.X(), 2.0)
145 d = c.complemented()
146 c.complement()
147 self.assertEqual(c, d)
148 self.assertEqual(c.getCenter(), -UnitVector3d.X())
149 self.assertEqual(c.getSquaredChordLength(), 2.0)
151 def test_area(self):
152 c = Circle(UnitVector3d(1, 1, 1), 2.0)
153 self.assertAlmostEqual(c.getArea(), 2 * math.pi)
155 def test_codec(self):
156 c = Circle(UnitVector3d.Y(), 1.0)
157 s = c.encode()
158 self.assertEqual(Circle.decode(s), c)
159 self.assertEqual(Region.decode(s), c)
161 def test_string(self):
162 c = Circle(UnitVector3d.Z(), Angle(1.0))
163 self.assertEqual(str(c), "Circle([0.0, 0.0, 1.0], 1.0)")
164 self.assertEqual(repr(c), "Circle(UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))")
165 self.assertEqual(c, eval(repr(c), {"Angle": Angle, "Circle": Circle, "UnitVector3d": UnitVector3d}))
167 def test_pickle(self):
168 a = Circle(UnitVector3d(1, -1, 1), 1.0)
169 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
170 self.assertEqual(a, b)
172 @unittest.skipIf(not yaml, "YAML module can not be imported")
173 def test_yaml(self):
174 a = Circle(UnitVector3d(1, -1, 1), 1.0)
175 b = yaml.safe_load(yaml.dump(a))
176 self.assertEqual(a, b)
179if __name__ == "__main__":
180 unittest.main()