LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
_polygon.cc
Go to the documentation of this file.
1/*
2 * This file is part of afw.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (https://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <memory>
25
26#include <pybind11/pybind11.h>
27#include <lsst/utils/python.h>
28
29#include <pybind11/stl.h>
30
33#include "lsst/geom/Box.h"
34#include "lsst/geom/Point.h"
39#include "lsst/afw/table/io/python.h" // for addPersistableMethods
40
41namespace py = pybind11;
42using namespace py::literals;
43
44namespace lsst {
45namespace afw {
46namespace geom {
47namespace polygon {
48namespace {
49void declarePolygon(lsst::utils::python::WrapperCollection &wrappers) {
50 wrappers.wrapType(
51 py::class_<Polygon, std::shared_ptr<Polygon>, typehandling::Storable>(wrappers.module, "Polygon"),
52 [](auto &mod, auto &cls) {
53 /* Constructors */
54 cls.def(py::init<Polygon::Box const &>());
55 cls.def(py::init<Polygon::Box const &, TransformPoint2ToPoint2 const &>());
56 cls.def(py::init<Polygon::Box const &, lsst::geom::AffineTransform const &>());
57 cls.def(py::init<std::vector<Polygon::Point> const &>());
58
59 table::io::python::addPersistableMethods<Polygon>(cls);
60
61 /* Operators */
62 cls.def(
63 "__eq__", [](Polygon const &self, Polygon const &other) { return self == other; },
64 py::is_operator());
65 cls.def(
66 "__ne__", [](Polygon const &self, Polygon const &other) { return self != other; },
67 py::is_operator());
68
69 /* Members */
70 cls.def("getNumEdges", &Polygon::getNumEdges);
71 cls.def("getBBox", &Polygon::getBBox);
72 cls.def("calculateCenter", &Polygon::calculateCenter);
73 cls.def("calculateArea", &Polygon::calculateArea);
74 cls.def("calculatePerimeter", &Polygon::calculatePerimeter);
75 cls.def("getVertices", &Polygon::getVertices);
76 cls.def("getEdges", &Polygon::getEdges);
77 cls.def("contains", &Polygon::contains);
78 cls.def("overlaps", (bool (Polygon::*)(Polygon const &) const) & Polygon::overlaps);
79 cls.def("overlaps", (bool (Polygon::*)(Polygon::Box const &) const) & Polygon::overlaps);
80 cls.def("intersectionSingle", (std::shared_ptr<Polygon>(Polygon::*)(Polygon const &) const) &
82 cls.def("intersectionSingle",
83 (std::shared_ptr<Polygon>(Polygon::*)(Polygon::Box const &) const) &
85 cls.def("intersection",
86 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
88 cls.def("intersection",
89 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
91 cls.def("unionSingle",
92 (std::shared_ptr<Polygon>(Polygon::*)(Polygon const &) const) & Polygon::unionSingle);
93 cls.def("unionSingle", (std::shared_ptr<Polygon>(Polygon::*)(Polygon::Box const &) const) &
95
96 // Wrap Polygon::union_ (C++) as Polygon.union (Python)
97 cls.def("union", (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
99 cls.def("union",
100 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
102 cls.def("symDifference",
103 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
105 cls.def("symDifference",
106 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
108 // cls.def("simplify", &Polygon::simplify);
109 cls.def("convexHull", &Polygon::convexHull);
110 cls.def("transform",
111 (std::shared_ptr<Polygon>(Polygon::*)(TransformPoint2ToPoint2 const &) const) &
113 cls.def("transform",
114 (std::shared_ptr<Polygon>(Polygon::*)(lsst::geom::AffineTransform const &) const) &
116 cls.def("subSample",
117 (std::shared_ptr<Polygon>(Polygon::*)(size_t) const) & Polygon::subSample);
118 cls.def("subSample",
119 (std::shared_ptr<Polygon>(Polygon::*)(double) const) & Polygon::subSample);
120 cls.def("createImage", (std::shared_ptr<afw::image::Image<float>>(Polygon::*)(
121 lsst::geom::Box2I const &) const) &
123 cls.def("createImage", (std::shared_ptr<afw::image::Image<float>>(Polygon::*)(
124 lsst::geom::Extent2I const &) const) &
126 });
127}
128} // namespace
129void wrapPolygon(lsst::utils::python::WrapperCollection &wrappers) {
130 wrappers.addSignatureDependency("lsst.pex.exceptions");
131 wrappers.addInheritanceDependency("lsst.afw.typehandling");
132 wrappers.addSignatureDependency("lsst.afw.table.io");
133 wrappers.wrapException<SinglePolygonException, pex::exceptions::RuntimeError>("SinglePolygonException",
134 "RuntimeError");
135 declarePolygon(wrappers);
136}
137} // namespace polygon
138} // namespace geom
139} // namespace afw
140} // namespace lsst
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:378
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
Definition: Polygon.cc:358
Box getBBox() const
Return bounding box.
Definition: Polygon.cc:314
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
Definition: Polygon.cc:404
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
Definition: Polygon.cc:326
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:362
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
Definition: Polygon.cc:446
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
Definition: Polygon.cc:424
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:370
size_t getNumEdges() const
Return number of edges.
Definition: Polygon.cc:309
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
Definition: Polygon.cc:410
std::vector< Point > getVertices() const
Get vector of vertices.
Definition: Polygon.cc:337
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:384
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Definition: Polygon.cc:390
bool contains(Point const &point) const
Returns whether the polygon contains the point.
Definition: Polygon.cc:356
double calculatePerimeter() const
Definition: Polygon.cc:324
An exception that indicates the single-polygon assumption has been violated.
Definition: Polygon.h:53
An affine coordinate transformation consisting of a linear transformation and an offset.
An integer coordinate rectangle.
Definition: Box.h:55
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
void wrapPolygon(lsst::utils::python::WrapperCollection &)
Definition: _polygon.cc:129
Transform< Point2Endpoint, Point2Endpoint > TransformPoint2ToPoint2
Definition: Transform.h:300
A base class for image defects.