LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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>
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::cpputils::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
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
table::Key< int > transform
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
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition python.h:242
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
Definition python.h:357
void addInheritanceDependency(std::string const &name)
Indicate an external module that provides a base class for a subsequent addType call.
Definition python.h:343
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
Definition python.h:391
auto wrapException(std::string const &pyName, std::string const &pyBase, bool setModuleName=true)
Wrap a C++ exception as a Python exception.
Definition python.h:421
Reports errors that are due to events beyond the control of the program.
Definition Runtime.h:104
void wrapPolygon(lsst::cpputils::python::WrapperCollection &)
Definition _polygon.cc:137
Extent< int, 2 > Extent2I
Definition Extent.h:397
Point< int, 2 > Point2I
Definition Point.h:321
STL namespace.
decltype(sizeof(void *)) size_t
Definition doctest.h:524