Coverage for tests / test_ConvexPolygon.py: 18%
75 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:29 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-14 23:29 +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 unittest
37import numpy as np
39from lsst.sphgeom import CONTAINS, Circle, ConvexPolygon, Region, UnitVector3d
42class ConvexPolygonTestCase(unittest.TestCase):
43 """Test ConvexPolygon."""
45 def testConstruction(self):
46 points = [UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()]
47 p1 = ConvexPolygon(points)
48 self.assertEqual(points, p1.getVertices())
49 p2 = p1.clone()
50 self.assertEqual(p1, p2)
51 p3 = ConvexPolygon([-UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
52 self.assertNotEqual(p1, p3)
53 p4 = ConvexPolygon.convexHull(
54 [UnitVector3d.Y(), UnitVector3d.X(), UnitVector3d(1, 1, 1), UnitVector3d.Z()]
55 )
56 self.assertEqual(p1, p4)
58 def testCodec(self):
59 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
60 s = p.encode()
61 self.assertEqual(ConvexPolygon.decode(s), p)
62 self.assertEqual(Region.decode(s), p)
64 def testRelationships(self):
65 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), 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.assertEqual(p.overlaps(boundingCircle), True)
73 self.assertFalse(p.contains(boundingCircle))
74 tinyCircle = Circle(boundingCircle.getCenter())
75 self.assertFalse(p.isWithin(tinyCircle))
76 self.assertTrue(p.intersects(tinyCircle))
77 self.assertFalse(p.isDisjointFrom(tinyCircle))
78 self.assertEqual(p.overlaps(tinyCircle), True)
79 self.assertTrue(p.contains(tinyCircle))
81 def test_vectorized_contains(self):
82 b = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
83 x = np.random.rand(5, 3)
84 y = np.random.rand(5, 3)
85 z = np.random.rand(5, 3)
86 c = b.contains(x, y, z)
87 lon = np.arctan2(y, x)
88 lat = np.arctan2(z, np.hypot(x, y))
89 c2 = b.contains(lon, lat)
90 for i in range(x.shape[0]):
91 for j in range(x.shape[1]):
92 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
93 self.assertEqual(c[i, j], b.contains(u))
94 self.assertEqual(c2[i, j], b.contains(u))
95 # test with non-contiguous memory
96 c3 = b.contains(x[::2], y[::2], z[::2])
97 c4 = b.contains(lon[::2], lat[::2])
98 for i in range(x.shape[0], 2):
99 for j in range(x.shape[1]):
100 u = UnitVector3d(x[i, j], y[i, j], z[i, j])
101 self.assertEqual(c3[i // 2, j], b.contains(u))
102 self.assertEqual(c4[i // 2, j], b.contains(u))
104 def testString(self):
105 p = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
106 self.assertEqual(str(p), repr(p))
107 self.assertEqual(
108 repr(p),
109 "ConvexPolygon([UnitVector3d(0.0, 0.0, 1.0), "
110 "UnitVector3d(1.0, 0.0, 0.0), "
111 "UnitVector3d(0.0, 1.0, 0.0)])",
112 )
113 self.assertEqual(p, eval(repr(p), {"ConvexPolygon": ConvexPolygon, "UnitVector3d": UnitVector3d}))
115 def testPickle(self):
116 a = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
117 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
118 self.assertEqual(a, b)
120 @unittest.skipIf(not yaml, "YAML module can not be imported")
121 def testYaml(self):
122 a = ConvexPolygon([UnitVector3d.Z(), UnitVector3d.X(), UnitVector3d.Y()])
123 b = yaml.safe_load(yaml.dump(a))
124 self.assertEqual(a, b)
127if __name__ == "__main__":
128 unittest.main()