Coverage for tests/test_ConvexPolygon.py: 22%
75 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-09 05:49 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-07-09 05:49 -0700
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 pickle
24try:
25 import yaml
26except ImportError:
27 yaml = None
29import unittest
31import numpy as np
33from lsst.sphgeom import CONTAINS, ConvexPolygon, Circle, Region, UnitVector3d
36class ConvexPolygonTestCase(unittest.TestCase):
38 def testConstruction(self):
39 points = [UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]
40 p1 = ConvexPolygon(points)
41 self.assertEqual(points, p1.getVertices())
42 p2 = p1.clone()
43 self.assertEqual(p1, p2)
44 p3 = ConvexPolygon([-UnitVector3d.Z(),
45 UnitVector3d.X(),
46 UnitVector3d.Y()])
47 self.assertNotEqual(p1, p3)
48 p4 = ConvexPolygon.convexHull([UnitVector3d.Y(),
49 UnitVector3d.X(),
50 UnitVector3d(1, 1, 1),
51 UnitVector3d.Z()])
52 self.assertEqual(p1, p4)
54 def testCodec(self):
55 p = ConvexPolygon([UnitVector3d.Z(),
56 UnitVector3d.X(),
57 UnitVector3d.Y()])
58 s = p.encode()
59 self.assertEqual(ConvexPolygon.decode(s), p)
60 self.assertEqual(Region.decode(s), p)
62 def testRelationships(self):
63 p = ConvexPolygon([UnitVector3d.Z(),
64 UnitVector3d.X(),
65 UnitVector3d.Y()])
66 self.assertTrue(p.contains(p.getCentroid()))
67 boundingCircle = p.getBoundingCircle()
68 self.assertEqual(boundingCircle.relate(p), CONTAINS)
69 self.assertTrue(p.isWithin(boundingCircle))
70 self.assertTrue(p.intersects(boundingCircle))
71 self.assertFalse(p.isDisjointFrom(boundingCircle))
72 self.assertFalse(p.contains(boundingCircle))
73 tinyCircle = Circle(boundingCircle.getCenter())
74 self.assertFalse(p.isWithin(tinyCircle))
75 self.assertTrue(p.intersects(tinyCircle))
76 self.assertFalse(p.isDisjointFrom(tinyCircle))
77 self.assertTrue(p.contains(tinyCircle))
79 def test_vectorized_contains(self):
80 b = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
81 x = np.random.rand(5, 3)
82 y = np.random.rand(5, 3)
83 z = np.random.rand(5, 3)
84 c = b.contains(x, y, z)
85 lon = np.arctan2(y, x)
86 lat = np.arctan2(z, np.hypot(x, y))
87 c2 = b.contains(lon, lat)
88 for i in range(x.shape[0]):
89 for j in range(x.shape[1]):
90 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
91 self.assertEqual(c[i, j], b.contains(u))
92 self.assertEqual(c2[i, j], b.contains(u))
93 # test with non-contiguous memory
94 c3 = b.contains(x[::2], y[::2], z[::2])
95 c4 = b.contains(lon[::2], lat[::2])
96 for i in range(x.shape[0], 2):
97 for j in range(x.shape[1]):
98 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
99 self.assertEqual(c3[i//2, j], b.contains(u))
100 self.assertEqual(c4[i//2, j], b.contains(u))
102 def testString(self):
103 p = ConvexPolygon([UnitVector3d.Z(),
104 UnitVector3d.X(),
105 UnitVector3d.Y()])
106 self.assertEqual(str(p), repr(p))
107 self.assertEqual(repr(p),
108 'ConvexPolygon([UnitVector3d(0.0, 0.0, 1.0), '
109 'UnitVector3d(1.0, 0.0, 0.0), '
110 'UnitVector3d(0.0, 1.0, 0.0)])')
111 self.assertEqual(
112 p, eval(repr(p), dict(ConvexPolygon=ConvexPolygon,
113 UnitVector3d=UnitVector3d)))
115 def testPickle(self):
116 a = ConvexPolygon([UnitVector3d.Z(),
117 UnitVector3d.X(),
118 UnitVector3d.Y()])
119 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
120 self.assertEqual(a, b)
122 @unittest.skipIf(not yaml, "YAML module can not be imported")
123 def testYaml(self):
124 a = ConvexPolygon([UnitVector3d.Z(),
125 UnitVector3d.X(),
126 UnitVector3d.Y()])
127 b = yaml.safe_load(yaml.dump(a))
128 self.assertEqual(a, b)
131if __name__ == '__main__': 131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never true
132 unittest.main()