lsst.afw  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
polygon.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include <memory>
24 
25 #include <pybind11/pybind11.h>
26 #include <pybind11/stl.h>
27 
28 #include "lsst/pex/exceptions/Runtime.h"
29 #include "lsst/pex/exceptions/python/Exception.h"
30 #include "lsst/afw/geom/Box.h"
31 #include "lsst/afw/geom/Point.h"
36 #include "lsst/afw/table/io/python.h" // for declarePersistableFacade
37 
38 namespace py = pybind11;
39 using namespace py::literals;
40 
41 namespace lsst {
42 namespace afw {
43 namespace geom {
44 namespace polygon {
45 
46 PYBIND11_PLUGIN(_polygon) {
47  py::module mod("_polygon", "Python wrapper for afw _polygon library");
48 
49  // TODO: Commented-out code is waiting until needed and is untested.
50  // Add tests for it and enable it or remove it before the final pybind11 merge.
51 
52  /* Module level */
53  table::io::python::declarePersistableFacade<Polygon>(mod, "Polygon");
54 
55  py::class_<Polygon, std::shared_ptr<Polygon>, table::io::PersistableFacade<Polygon>,
57  clsPolygon(mod, "Polygon");
58 
59  pex::exceptions::python::declareException<SinglePolygonException, pex::exceptions::RuntimeError>(
60  mod, "SinglePolygonException", "RuntimeError");
61 
62  /* Member types and enums */
63 
64  /* Constructors */
65  clsPolygon.def(py::init<Polygon::Box const &>());
66  clsPolygon.def(py::init<Polygon::Box const &, std::shared_ptr<XYTransform const> const &>());
67  clsPolygon.def(py::init<Polygon::Box const &, AffineTransform const &>());
68  clsPolygon.def(py::init<std::vector<Polygon::Point> const &>());
69 
70  /* Operators */
71  clsPolygon.def("__eq__", [](Polygon const &self, Polygon const &other) { return self == other; },
72  py::is_operator());
73  clsPolygon.def("__ne__", [](Polygon const &self, Polygon const &other) { return self != other; },
74  py::is_operator());
75 
76  /* Members */
77  clsPolygon.def("getNumEdges", &Polygon::getNumEdges);
78  clsPolygon.def("getBBox", &Polygon::getBBox);
79  clsPolygon.def("calculateCenter", &Polygon::calculateCenter);
80  clsPolygon.def("calculateArea", &Polygon::calculateArea);
81  clsPolygon.def("calculatePerimeter", &Polygon::calculatePerimeter);
82  clsPolygon.def("getVertices", &Polygon::getVertices);
83  clsPolygon.def("getEdges", &Polygon::getEdges);
84  clsPolygon.def("contains", &Polygon::contains);
85  clsPolygon.def("overlaps", (bool (Polygon::*)(Polygon const &) const) & Polygon::overlaps);
86  clsPolygon.def("overlaps", (bool (Polygon::*)(Polygon::Box const &) const) & Polygon::overlaps);
87  clsPolygon.def("intersectionSingle", (std::shared_ptr<Polygon> (Polygon::*)(Polygon const &) const) &
88  Polygon::intersectionSingle);
89  clsPolygon.def("intersectionSingle", (std::shared_ptr<Polygon> (Polygon::*)(Polygon::Box const &) const) &
90  Polygon::intersectionSingle);
91  clsPolygon.def("intersection",
92  (std::vector<std::shared_ptr<Polygon>> (Polygon::*)(Polygon const &) const) &
93  Polygon::intersection);
94  clsPolygon.def("intersection",
95  (std::vector<std::shared_ptr<Polygon>> (Polygon::*)(Polygon::Box const &) const) &
96  Polygon::intersection);
97  clsPolygon.def("unionSingle",
98  (std::shared_ptr<Polygon> (Polygon::*)(Polygon const &) const) & Polygon::unionSingle);
99  clsPolygon.def("unionSingle", (std::shared_ptr<Polygon> (Polygon::*)(Polygon::Box const &) const) &
100  Polygon::unionSingle);
101 
102  // Wrap Polygon::union_ (C++) as Polygon.union (Python)
103  clsPolygon.def("union", (std::vector<std::shared_ptr<Polygon>> (Polygon::*)(Polygon const &) const) &
104  Polygon::union_);
105  clsPolygon.def("union", (std::vector<std::shared_ptr<Polygon>> (Polygon::*)(Polygon::Box const &) const) &
106  Polygon::union_);
107  clsPolygon.def("symDifference",
108  (std::vector<std::shared_ptr<Polygon>> (Polygon::*)(Polygon const &) const) &
109  Polygon::symDifference);
110  clsPolygon.def("symDifference",
111  (std::vector<std::shared_ptr<Polygon>> (Polygon::*)(Polygon::Box const &) const) &
112  Polygon::symDifference);
113  // clsPolygon.def("simplify", &Polygon::simplify);
114  clsPolygon.def("convexHull", &Polygon::convexHull);
115  clsPolygon.def("transform",
116  (std::shared_ptr<Polygon> (Polygon::*)(std::shared_ptr<XYTransform const> const &) const) &
117  Polygon::transform);
118  clsPolygon.def("transform", (std::shared_ptr<Polygon> (Polygon::*)(AffineTransform const &) const) &
119  Polygon::transform);
120  clsPolygon.def("subSample", (std::shared_ptr<Polygon> (Polygon::*)(size_t) const) & Polygon::subSample);
121  clsPolygon.def("subSample", (std::shared_ptr<Polygon> (Polygon::*)(double) const) & Polygon::subSample);
122  clsPolygon.def("createImage",
123  (std::shared_ptr<afw::image::Image<float>> (Polygon::*)(Box2I const &) const) &
124  Polygon::createImage);
125  clsPolygon.def("createImage",
126  (std::shared_ptr<afw::image::Image<float>> (Polygon::*)(Extent2I const &) const) &
127  Polygon::createImage);
128  // clsPolygon.def("isPersistable", &Polygon::isPersistable);
129 
130  return mod.ptr();
131 }
132 }
133 }
134 }
135 } // namespace lsst::afw::geom::polygon
An integer coordinate rectangle.
Definition: Box.h:55
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:73
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
An affine coordinate transformation consisting of a linear transformation and an offset.
Cartesian polygons.
Definition: Polygon.h:58
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:486