LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
polygon.py
Go to the documentation of this file.
1# This file is part of afw.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22__all__ = ["Polygon"] # import this module only for its side effects
23
24from lsst.utils import continueClass
25from ._geom import Polygon
26
27
28@continueClass
29class Polygon: # noqa: F811
30 def __repr__(self):
31 return f"{self.__class__.__name__}({[p for p in self.getVertices()]})"
32
33 def __reduce__(self):
34 return self.__class__, (self.getVertices(),)
35
36 def __iter__(self):
37 """Iterator over vertices"""
38 vertices = self.getVertices()
39 return iter(vertices)
40
41 def __getitem__(self, i):
42 return [pt for pt in self][i]
43
44 def __len__(self):
45 return self.getNumEdges()
46
47 def __contains__(self, point):
48 """point in polygon?"""
49 return self.containscontainscontainscontains(point)
50
51 def display(self, xy0=None, frame=1, ctype=None):
52 """Display polygon on existing frame"""
53 import lsst.geom
54 import lsst.afw.display as afwDisplay
55 xy0 = lsst.geom.Extent2D(0, 0) if xy0 is None else lsst.geom.Extent2D(xy0)
56 disp = afwDisplay.Display(frame=frame)
57 with disp.Buffering():
58 for p1, p2 in self.getEdges():
59 disp.line((p1 - xy0, p2 - xy0), ctype=ctype)
60
61 def plot(self, axes=None, **kwargs):
62 """Plot polygon with matplotlib
63
64 Parameters
65 ----------
66 axes : `matplotlib.axes.Axes`
67 Matplotlib axes to use, or None
68 kwargs : any
69 Additional arguments to `matplotlib.axes.Axes.plot`
70 or `matplotlib.axes.Axes.scatter`.
71
72 Returns
73 -------
74 axes : `matplotlib.axes.Axes`
75 The axes used to make the plot (same as ``axes``, if provided).
76 """
77 import numpy
78 if axes is None:
79 import matplotlib.pyplot as plt
80 plt.figure()
81 axes = plt.axes()
82 for p1, p2 in self.getEdges():
83 x = (p1.getX(), p2.getX())
84 y = (p1.getY(), p2.getY())
85 axes.plot(x, y, **kwargs)
86 vertices = self.getVertices()
87 x = numpy.array([p[0] for p in vertices])
88 y = numpy.array([p[1] for p in vertices])
89 axes.scatter(x, y, **kwargs)
90 return axes
std::vector< bool > contains(std::vector< lsst::geom::Point2I > const &points) const
Definition Polygon.cc:366
bool contains(Xtype x, Ytype y) const
Returns whether the polygon contains the x, y pair.
Definition Polygon.h:154
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
Definition Polygon.cc:326
plot(self, axes=None, **kwargs)
Definition polygon.py:61
size_t getNumEdges() const
Return number of edges.
Definition Polygon.cc:309
std::vector< bool > contains(std::vector< Point > const &points) const
Returns whether the polygon contains the vector of points.
Definition Polygon.cc:358
std::vector< Point > getVertices() const
Get vector of vertices.
Definition Polygon.cc:337
bool contains(Point const &point) const
Returns whether the polygon contains the point.
Definition Polygon.cc:356