Coverage for tests/test_ConvexPolygon.py: 19%
73 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -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
25try:
26 import yaml
27except ImportError:
28 yaml = None
30import unittest
32import numpy as np
33from lsst.sphgeom import CONTAINS, Circle, ConvexPolygon, Region, UnitVector3d
36class ConvexPolygonTestCase(unittest.TestCase):
37 """Test ConvexPolygon."""
39 def testConstruction(self):
40 points = [UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]
41 p1 = ConvexPolygon(points)
42 self.assertEqual(points, p1.getVertices())
43 p2 = p1.clone()
44 self.assertEqual(p1, p2)
45 p3 = ConvexPolygon([-UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
46 self.assertNotEqual(p1, p3)
47 p4 = ConvexPolygon.convexHull(
48 [UnitVector3d.Y(), UnitVector3d.X(), UnitVector3d(1, 1, 1), UnitVector3d.Z()]
49 )
50 self.assertEqual(p1, p4)
52 def testCodec(self):
53 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
54 s = p.encode()
55 self.assertEqual(ConvexPolygon.decode(s), p)
56 self.assertEqual(Region.decode(s), p)
58 def testRelationships(self):
59 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
60 self.assertTrue(p.contains(p.getCentroid()))
61 boundingCircle = p.getBoundingCircle()
62 self.assertEqual(boundingCircle.relate(p), CONTAINS)
63 self.assertTrue(p.isWithin(boundingCircle))
64 self.assertTrue(p.intersects(boundingCircle))
65 self.assertFalse(p.isDisjointFrom(boundingCircle))
66 self.assertFalse(p.contains(boundingCircle))
67 tinyCircle = Circle(boundingCircle.getCenter())
68 self.assertFalse(p.isWithin(tinyCircle))
69 self.assertTrue(p.intersects(tinyCircle))
70 self.assertFalse(p.isDisjointFrom(tinyCircle))
71 self.assertTrue(p.contains(tinyCircle))
73 def test_vectorized_contains(self):
74 b = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
75 x = np.random.rand(5, 3)
76 y = np.random.rand(5, 3)
77 z = np.random.rand(5, 3)
78 c = b.contains(x, y, z)
79 lon = np.arctan2(y, x)
80 lat = np.arctan2(z, np.hypot(x, y))
81 c2 = b.contains(lon, lat)
82 for i in range(x.shape[0]):
83 for j in range(x.shape[1]):
84 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
85 self.assertEqual(c[i, j], b.contains(u))
86 self.assertEqual(c2[i, j], b.contains(u))
87 # test with non-contiguous memory
88 c3 = b.contains(x[::2], y[::2], z[::2])
89 c4 = b.contains(lon[::2], lat[::2])
90 for i in range(x.shape[0], 2):
91 for j in range(x.shape[1]):
92 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
93 self.assertEqual(c3[i // 2, j], b.contains(u))
94 self.assertEqual(c4[i // 2, j], b.contains(u))
96 def testString(self):
97 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
98 self.assertEqual(str(p), repr(p))
99 self.assertEqual(
100 repr(p),
101 "ConvexPolygon([UnitVector3d(0.0, 0.0, 1.0), "
102 "UnitVector3d(1.0, 0.0, 0.0), "
103 "UnitVector3d(0.0, 1.0, 0.0)])",
104 )
105 self.assertEqual(p, eval(repr(p), {"ConvexPolygon": ConvexPolygon, "UnitVector3d": UnitVector3d}))
107 def testPickle(self):
108 a = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
109 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
110 self.assertEqual(a, b)
112 @unittest.skipIf(not yaml, "YAML module can not be imported")
113 def testYaml(self):
114 a = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
115 b = yaml.safe_load(yaml.dump(a))
116 self.assertEqual(a, b)
119if __name__ == "__main__":
120 unittest.main()