Coverage for tests/test_Circle.py : 21%

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#
23import yaml
24import pickle
26import math
27import unittest
29from lsst.sphgeom import (Angle, CONTAINS, Circle, DISJOINT, Region,
30 UnitVector3d)
33class CircleTestCase(unittest.TestCase):
35 def test_construction(self):
36 self.assertTrue(Circle.empty().isEmpty())
37 self.assertTrue(Circle().isEmpty())
38 self.assertTrue(Circle.full().isFull())
39 c = Circle(UnitVector3d.X())
40 self.assertEqual(c.getOpeningAngle(), Angle(0))
41 self.assertEqual(c.getSquaredChordLength(), 0)
42 c = Circle(UnitVector3d.Z(), 2.0)
43 self.assertTrue(c.contains(UnitVector3d.Z()))
44 c = Circle(UnitVector3d.Z(), Angle(math.pi))
45 self.assertTrue(c.isFull())
46 d = c.clone()
47 self.assertEqual(c, d)
48 self.assertNotEqual(id(c), id(d))
49 e = Circle(d)
50 self.assertEqual(d, e)
52 def test_comparison_operators(self):
53 c = Circle(UnitVector3d.X(), 4.0)
54 d = Circle(UnitVector3d.Y(), 4.0)
55 self.assertEqual(c, d)
56 self.assertTrue(c.isFull())
57 self.assertNotEqual(c, Circle(UnitVector3d.Z()))
59 def test_center_and_dimensions(self):
60 c = Circle(UnitVector3d.X(), 1)
61 self.assertEqual(c.getCenter(), UnitVector3d.X())
62 self.assertEqual(c.getSquaredChordLength(), 1)
63 self.assertAlmostEqual(c.getOpeningAngle().asRadians(), math.pi / 3)
65 def test_relationships(self):
66 c = Circle(UnitVector3d.X(), Angle.fromDegrees(0.1))
67 d = Circle(UnitVector3d(1, 1, 1), Angle(math.pi / 2))
68 e = Circle(-UnitVector3d.X())
69 self.assertTrue(c.contains(UnitVector3d.X()))
70 self.assertTrue(UnitVector3d.X() in c)
71 self.assertTrue(d.contains(c))
72 self.assertTrue(c.isWithin(d))
73 self.assertTrue(c.intersects(d))
74 self.assertTrue(c.intersects(UnitVector3d.X()))
75 self.assertTrue(e.isDisjointFrom(d))
76 self.assertEqual(d.relate(c), CONTAINS)
77 self.assertEqual(e.relate(d), DISJOINT)
79 def test_expanding_and_clipping(self):
80 a = Circle.empty()
81 b = (a.expandedTo(UnitVector3d.X())
82 .expandedTo(Circle(UnitVector3d.Y(), 1))
83 .clippedTo(Circle(UnitVector3d(1, 1, 0), 1))
84 .clippedTo(UnitVector3d.Y()))
85 a.expandTo(UnitVector3d.X())
86 a.expandTo(Circle(UnitVector3d.Y(), 1))
87 a.clipTo(Circle(UnitVector3d(1, 1, 0), 1))
88 a.clipTo(UnitVector3d.Y())
89 self.assertEqual(a, b)
90 self.assertEqual(a, Circle(UnitVector3d.Y()))
91 a.clipTo(UnitVector3d.Z())
92 self.assertTrue(a.isEmpty())
94 def test_dilation_and_erosion(self):
95 a = Angle(math.pi / 2)
96 c = Circle(UnitVector3d.X())
97 d = c.dilatedBy(a).erodedBy(a)
98 c.dilateBy(a).erodeBy(a)
99 self.assertEqual(c, d)
100 self.assertEqual(c, Circle(UnitVector3d.X()))
102 def test_complement(self):
103 c = Circle(UnitVector3d.X(), 2.0)
104 d = c.complemented()
105 c.complement()
106 self.assertEqual(c, d)
107 self.assertEqual(c.getCenter(), -UnitVector3d.X())
108 self.assertEqual(c.getSquaredChordLength(), 2.0)
110 def test_area(self):
111 c = Circle(UnitVector3d(1, 1, 1), 2.0)
112 self.assertAlmostEqual(c.getArea(), 2 * math.pi)
114 def test_codec(self):
115 c = Circle(UnitVector3d.Y(), 1.0)
116 s = c.encode()
117 self.assertEqual(Circle.decode(s), c)
118 self.assertEqual(Region.decode(s), c)
120 def test_string(self):
121 c = Circle(UnitVector3d.Z(), Angle(1.0))
122 self.assertEqual(str(c), 'Circle([0.0, 0.0, 1.0], 1.0)')
123 self.assertEqual(repr(c),
124 'Circle(UnitVector3d(0.0, 0.0, 1.0), Angle(1.0))')
125 self.assertEqual(c, eval(repr(c), dict(
126 Angle=Angle, Circle=Circle, UnitVector3d=UnitVector3d)))
128 def test_pickle(self):
129 a = Circle(UnitVector3d(1, -1, 1), 1.0)
130 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
131 self.assertEqual(a, b)
133 def test_yaml(self):
134 a = Circle(UnitVector3d(1, -1, 1), 1.0)
135 b = yaml.safe_load(yaml.dump(a))
136 self.assertEqual(a, b)
139if __name__ == '__main__': 139 ↛ 140line 139 didn't jump to line 140, because the condition on line 139 was never true
140 unittest.main()