LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
_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 <pybind11/numpy.h>
28#include <lsst/utils/python.h>
29
30#include <pybind11/stl.h>
31
34#include "lsst/geom/Box.h"
35#include "lsst/geom/Point.h"
40#include "lsst/afw/table/io/python.h" // for addPersistableMethods
41
42namespace py = pybind11;
43using namespace py::literals;
44
45namespace lsst {
46namespace afw {
47namespace geom {
48namespace polygon {
49namespace {
50void declarePolygon(lsst::utils::python::WrapperCollection &wrappers) {
51 wrappers.wrapType(
52 py::class_<Polygon, std::shared_ptr<Polygon>, typehandling::Storable>(wrappers.module, "Polygon"),
53 [](auto &mod, auto &cls) {
54 /* Constructors */
55 cls.def(py::init<Polygon::Box const &>());
56 cls.def(py::init<Polygon::Box const &, TransformPoint2ToPoint2 const &>());
57 cls.def(py::init<Polygon::Box const &, lsst::geom::AffineTransform const &>());
58 cls.def(py::init<std::vector<Polygon::Point> const &>());
59
60 table::io::python::addPersistableMethods<Polygon>(cls);
61
62 /* Operators */
63 cls.def(
64 "__eq__", [](Polygon const &self, Polygon const &other) { return self == other; },
65 py::is_operator());
66 cls.def(
67 "__ne__", [](Polygon const &self, Polygon const &other) { return self != other; },
68 py::is_operator());
69
70 /* Members */
71 cls.def("getNumEdges", &Polygon::getNumEdges);
72 cls.def("getBBox", &Polygon::getBBox);
73 cls.def("calculateCenter", &Polygon::calculateCenter);
74 cls.def("calculateArea", &Polygon::calculateArea);
75 cls.def("calculatePerimeter", &Polygon::calculatePerimeter);
76 cls.def("getVertices", &Polygon::getVertices);
77 cls.def("getEdges", &Polygon::getEdges);
78
79 cls.def("contains", (bool (Polygon::*)(Polygon::Point const&) const) &Polygon::contains);
80 cls.def("contains", (std::vector<bool> (Polygon::*)(std::vector<Polygon::Point> const&) const) &Polygon::contains);
81 cls.def("contains", (std::vector<bool> (Polygon::*)(std::vector<lsst::geom::Point2I> const&) const) &Polygon::contains);
82 cls.def("contains", py::vectorize((bool (Polygon::*)(double x, double y) const) &Polygon::contains<double, double>));
83 cls.def("contains", py::vectorize((bool (Polygon::*)(float x, float y) const) &Polygon::contains<float, float>));
84 cls.def("contains", py::vectorize((bool (Polygon::*)(int x, int y) const) &Polygon::contains<int, int>));
85
86 cls.def("overlaps", (bool (Polygon::*)(Polygon const &) const) & Polygon::overlaps);
87 cls.def("overlaps", (bool (Polygon::*)(Polygon::Box const &) const) & Polygon::overlaps);
88 cls.def("intersectionSingle", (std::shared_ptr<Polygon>(Polygon::*)(Polygon const &) const) &
90 cls.def("intersectionSingle",
91 (std::shared_ptr<Polygon>(Polygon::*)(Polygon::Box const &) const) &
93 cls.def("intersection",
94 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
96 cls.def("intersection",
97 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
99 cls.def("unionSingle",
100 (std::shared_ptr<Polygon>(Polygon::*)(Polygon const &) const) & Polygon::unionSingle);
101 cls.def("unionSingle", (std::shared_ptr<Polygon>(Polygon::*)(Polygon::Box const &) const) &
103
104 // Wrap Polygon::union_ (C++) as Polygon.union (Python)
105 cls.def("union", (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
107 cls.def("union",
108 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
110 cls.def("symDifference",
111 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
113 cls.def("symDifference",
114 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
116 // cls.def("simplify", &Polygon::simplify);
117 cls.def("convexHull", &Polygon::convexHull);
118 cls.def("transform",
119 (std::shared_ptr<Polygon>(Polygon::*)(TransformPoint2ToPoint2 const &) const) &
121 cls.def("transform",
122 (std::shared_ptr<Polygon>(Polygon::*)(lsst::geom::AffineTransform const &) const) &
124 cls.def("subSample",
125 (std::shared_ptr<Polygon>(Polygon::*)(size_t) const) & Polygon::subSample);
126 cls.def("subSample",
127 (std::shared_ptr<Polygon>(Polygon::*)(double) const) & Polygon::subSample);
128 cls.def("createImage", (std::shared_ptr<afw::image::Image<float>>(Polygon::*)(
129 lsst::geom::Box2I const &) const) &
131 cls.def("createImage", (std::shared_ptr<afw::image::Image<float>>(Polygon::*)(
132 lsst::geom::Extent2I const &) const) &
134 });
135}
136} // namespace
137void wrapPolygon(lsst::utils::python::WrapperCollection &wrappers) {
138 wrappers.addSignatureDependency("lsst.pex.exceptions");
139 wrappers.addInheritanceDependency("lsst.afw.typehandling");
140 wrappers.addSignatureDependency("lsst.afw.table.io");
141 wrappers.wrapException<SinglePolygonException, pex::exceptions::RuntimeError>("SinglePolygonException",
142 "RuntimeError");
143 declarePolygon(wrappers);
144}
145} // namespace polygon
146} // namespace geom
147} // namespace afw
148} // namespace lsst
double x
int y
Definition: SpanSet.cc:48
lsst::geom::Point2D Point
Definition: Polygon.h:62
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:394
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
Definition: Polygon.cc:374
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:420
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:378
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
Definition: Polygon.cc:462
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
Definition: Polygon.cc:440
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:386
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:426
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:400
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Definition: Polygon.cc:406
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:137
Transform< Point2Endpoint, Point2Endpoint > TransformPoint2ToPoint2
Definition: Transform.h:300