LSST Applications g180d380827+770a9040cc,g2079a07aa2+86d27d4dc4,g2305ad1205+09cfdadad9,g2bbee38e9b+c6a8a0fb72,g337abbeb29+c6a8a0fb72,g33d1c0ed96+c6a8a0fb72,g3a166c0a6a+c6a8a0fb72,g3ddfee87b4+1ea5e09c42,g48712c4677+7e2ea9cd42,g487adcacf7+301d09421d,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+96fcb956a6,g64a986408d+23540ee355,g858d7b2824+23540ee355,g864b0138d7+aa38e45daa,g95921f966b+d83dc58ecd,g991b906543+23540ee355,g99cad8db69+7f13b58a93,g9c22b2923f+e2510deafe,g9ddcbc5298+9a081db1e4,ga1e77700b3+03d07e1c1f,gb0e22166c9+60f28cb32d,gb23b769143+23540ee355,gba4ed39666+c2a2e4ac27,gbb8dafda3b+49e7449578,gbd998247f1+585e252eca,gc120e1dc64+1bbfa184e1,gc28159a63d+c6a8a0fb72,gc3e9b769f7+385ea95214,gcf0d15dbbd+1ea5e09c42,gdaeeff99f8+f9a426f77a,ge6526c86ff+1bccc98490,ge79ae78c31+c6a8a0fb72,gee10cc3b42+585e252eca,w.2024.18
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) &
89 Polygon::intersectionSingle);
90 cls.def("intersectionSingle",
91 (std::shared_ptr<Polygon>(Polygon::*)(Polygon::Box const &) const) &
92 Polygon::intersectionSingle);
93 cls.def("intersection",
94 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
95 Polygon::intersection);
96 cls.def("intersection",
97 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
98 Polygon::intersection);
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) &
102 Polygon::unionSingle);
103
104 // Wrap Polygon::union_ (C++) as Polygon.union (Python)
105 cls.def("union", (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
106 Polygon::union_);
107 cls.def("union",
108 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
109 Polygon::union_);
110 cls.def("symDifference",
111 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon const &) const) &
112 Polygon::symDifference);
113 cls.def("symDifference",
114 (std::vector<std::shared_ptr<Polygon>>(Polygon::*)(Polygon::Box const &) const) &
115 Polygon::symDifference);
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) &
120 Polygon::transform);
121 cls.def("transform",
122 (std::shared_ptr<Polygon>(Polygon::*)(lsst::geom::AffineTransform const &) const) &
123 Polygon::transform);
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) &
130 Polygon::createImage);
131 cls.def("createImage", (std::shared_ptr<afw::image::Image<float>>(Polygon::*)(
132 lsst::geom::Extent2I const &) const) &
133 Polygon::createImage);
134 });
135}
136} // namespace
137void wrapPolygon(lsst::utils::python::WrapperCollection &wrappers) {
138 wrappers.addInheritanceDependency("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
int y
Definition SpanSet.cc:48
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
size_t getNumEdges() const
Return number of edges.
Definition Polygon.cc:309
std::vector< Point > getVertices() const
Get vector of vertices.
Definition Polygon.cc:337
An exception that indicates the single-polygon assumption has been violated.
Definition Polygon.h:53
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
Extent< int, 2 > Extent2I
Definition Extent.h:397
Point< int, 2 > Point2I
Definition Point.h:321
STL namespace.