LSSTApplications  20.0.0
LSSTDataManagementBasePackage
convexPolygon.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * See COPYRIGHT file at the top of the source tree.
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 #include "pybind11/pybind11.h"
23 #include "pybind11/stl.h"
24 
25 #include <memory>
26 
27 #include "lsst/sphgeom/Box.h"
28 #include "lsst/sphgeom/Box3d.h"
29 #include "lsst/sphgeom/Circle.h"
31 #include "lsst/sphgeom/Ellipse.h"
32 #include "lsst/sphgeom/Region.h"
34 
37 
38 namespace py = pybind11;
39 using namespace pybind11::literals;
40 
41 namespace lsst {
42 namespace sphgeom {
43 namespace {
44 
46  uint8_t const *buffer = reinterpret_cast<uint8_t const *>(
47  PYBIND11_BYTES_AS_STRING(bytes.ptr()));
48  size_t n = static_cast<size_t>(PYBIND11_BYTES_SIZE(bytes.ptr()));
49  return ConvexPolygon::decode(buffer, n);
50 }
51 
52 PYBIND11_MODULE(convexPolygon, mod) {
53  py::module::import("lsst.sphgeom.region");
54 
55  py::class_<ConvexPolygon, std::unique_ptr<ConvexPolygon>, Region> cls(
56  mod, "ConvexPolygon");
57 
58  cls.attr("TYPE_CODE") = py::int_(ConvexPolygon::TYPE_CODE);
59 
60  cls.def_static("convexHull", &ConvexPolygon::convexHull, "points"_a);
61 
62  cls.def(py::init<std::vector<UnitVector3d> const &>(), "points"_a);
63  // Do not wrap the two unsafe (3 and 4 vertex) constructors
64  cls.def(py::init<ConvexPolygon const &>(), "convexPolygon"_a);
65 
66  cls.def("__eq__", &ConvexPolygon::operator==, py::is_operator());
67  cls.def("__ne__", &ConvexPolygon::operator!=, py::is_operator());
68 
69  cls.def("getVertices", &ConvexPolygon::getVertices);
70  cls.def("getCentroid", &ConvexPolygon::getCentroid);
71 
72  // Note that much of the Region interface has already been wrapped. Here are bits that have not:
73  cls.def("contains", py::overload_cast<UnitVector3d const &>(&ConvexPolygon::contains, py::const_));
74  cls.def("contains", py::overload_cast<Region const &>(&ConvexPolygon::contains, py::const_));
75  cls.def("isDisjointFrom", &ConvexPolygon::isDisjointFrom);
76  cls.def("intersects", &ConvexPolygon::intersects);
77  cls.def("isWithin", &ConvexPolygon::isWithin);
78 
79  // The lambda is necessary for now; returning the unique pointer
80  // directly leads to incorrect results and crashes.
81  cls.def_static("decode",
82  [](py::bytes bytes) { return decode(bytes).release(); },
83  "bytes"_a);
84 
85  cls.def("__repr__", [](ConvexPolygon const &self) {
86  return py::str("ConvexPolygon({!r})").format(self.getVertices());
87  });
88  cls.def(py::pickle(
89  [](const ConvexPolygon &self) { return python::encode(self); },
90  [](py::bytes bytes) { return decode(bytes).release(); }));
91 }
92 
93 } // <anonymous>
94 } // sphgeom
95 } // lsst
Region.h
This file defines an interface for spherical regions.
Box3d.h
This file declares a class for representing axis-aligned bounding boxes in ℝ³.
utils.h
UnitVector3d.h
This file declares a class for representing unit vectors in ℝ³.
std::vector
STL class.
Circle.h
This file declares a class for representing circular regions on the unit sphere.
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
astshim.fitsChanContinued.contains
def contains(self, name)
Definition: fitsChanContinued.py:127
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
Ellipse.h
This file declares a class for representing elliptical regions on the unit sphere.
bytes
table::Key< table::Array< std::uint8_t > > bytes
Definition: Transform.cc:199
relationship.h
Box.h
This file declares a class for representing longitude/latitude angle boxes on the unit sphere.
pybind11
Definition: _GenericMap.cc:40
lsst::utils.tests.init
def init()
Definition: tests.py:58
std::unique_ptr
STL class.
lsst::afw::cameraGeom::PYBIND11_MODULE
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133
ConvexPolygon.h
This file declares a class for representing convex polygons with great circle edges on the unit spher...