LSSTApplications  20.0.0
LSSTDataManagementBasePackage
box.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 
24 #include <memory>
25 
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/LonLat.h"
34 #include "lsst/sphgeom/Region.h"
36 
39 
40 namespace py = pybind11;
41 using namespace pybind11::literals;
42 
43 namespace lsst {
44 namespace sphgeom {
45 namespace {
46 
48  uint8_t const *buffer = reinterpret_cast<uint8_t const *>(
49  PYBIND11_BYTES_AS_STRING(bytes.ptr()));
50  size_t n = static_cast<size_t>(PYBIND11_BYTES_SIZE(bytes.ptr()));
51  return Box::decode(buffer, n);
52 }
53 
54 PYBIND11_MODULE(box, mod) {
55  py::module::import("lsst.sphgeom.region");
56 
57  py::class_<Box, std::unique_ptr<Box>, Region> cls(mod, "Box");
58 
59  cls.attr("TYPE_CODE") = py::int_(Box::TYPE_CODE);
60 
61  cls.def_static("fromDegrees", &Box::fromDegrees, "lon1"_a, "lat1"_a,
62  "lon2"_a, "lat2"_a);
63  cls.def_static("fromRadians", &Box::fromRadians, "lon1"_a, "lat1"_a,
64  "lon2"_a, "lat2"_a);
65  cls.def_static("empty", &Box::empty);
66  cls.def_static("full", &Box::full);
67  cls.def_static("halfWidthForCircle", &Box::halfWidthForCircle, "radius"_a,
68  "lat"_a);
69  cls.def_static("allLongitudes", &Box::allLongitudes);
70  cls.def_static("allLatitudes", &Box::allLatitudes);
71 
72  cls.def(py::init<>());
73  cls.def(py::init<LonLat const &>(), "point"_a);
74  cls.def(py::init<LonLat const &, LonLat const &>(), "point1"_a, "point2"_a);
75  cls.def(py::init<LonLat const &, Angle, Angle>(), "center"_a, "width"_a,
76  "height"_a);
77  cls.def(py::init<NormalizedAngleInterval const &, AngleInterval const &>(),
78  "lon"_a, "lat"_a);
79  cls.def(py::init<Box const &>(), "box"_a);
80 
81  cls.def("__eq__", (bool (Box::*)(Box const &) const) & Box::operator==,
82  py::is_operator());
83  cls.def("__eq__", (bool (Box::*)(LonLat const &) const) & Box::operator==,
84  py::is_operator());
85  cls.def("__ne__", (bool (Box::*)(Box const &) const) & Box::operator!=,
86  py::is_operator());
87  cls.def("__ne__", (bool (Box::*)(LonLat const &) const) & Box::operator!=,
88  py::is_operator());
89  cls.def("__contains__",
90  (bool (Box::*)(LonLat const &) const) & Box::contains,
91  py::is_operator());
92  cls.def("__contains__", (bool (Box::*)(Box const &) const) & Box::contains,
93  py::is_operator());
94  // Rewrap this base class method since there are overloads in this subclass
95  cls.def("__contains__",
96  (bool (Box::*)(UnitVector3d const &) const) & Box::contains,
97  py::is_operator());
98 
99  cls.def("getLon", &Box::getLon);
100  cls.def("getLat", &Box::getLat);
101  cls.def("isEmpty", &Box::isEmpty);
102  cls.def("isFull", &Box::isFull);
103  cls.def("getCenter", &Box::getCenter);
104  cls.def("getWidth", &Box::getWidth);
105  cls.def("getHeight", &Box::getHeight);
106  cls.def("contains", (bool (Box::*)(LonLat const &) const) & Box::contains);
107  cls.def("contains", (bool (Box::*)(Box const &) const) & Box::contains);
108  // Rewrap this base class method since there are overloads in this subclass
109  cls.def("contains",
110  (bool (Box::*)(UnitVector3d const &) const) & Box::contains);
111  cls.def("isDisjointFrom",
112  (bool (Box::*)(LonLat const &) const) & Box::isDisjointFrom);
113  cls.def("isDisjointFrom",
114  (bool (Box::*)(Box const &) const) & Box::isDisjointFrom);
115  cls.def("intersects",
116  (bool (Box::*)(LonLat const &) const) & Box::intersects);
117  cls.def("intersects", (bool (Box::*)(Box const &) const) & Box::intersects);
118  cls.def("isWithin", (bool (Box::*)(LonLat const &) const) & Box::isWithin);
119  cls.def("isWithin", (bool (Box::*)(Box const &) const) & Box::isWithin);
120  cls.def("clipTo", (Box & (Box::*)(LonLat const &)) & Box::clipTo);
121  cls.def("clipTo", (Box & (Box::*)(Box const &)) & Box::clipTo);
122  cls.def("clippedTo", (Box(Box::*)(LonLat const &) const) & Box::clippedTo);
123  cls.def("clippedTo", (Box(Box::*)(Box const &) const) & Box::clippedTo);
124  cls.def("expandTo", (Box & (Box::*)(LonLat const &)) & Box::expandTo);
125  cls.def("expandTo", (Box & (Box::*)(Box const &)) & Box::expandTo);
126  cls.def("expandedTo",
127  (Box(Box::*)(LonLat const &) const) & Box::expandedTo);
128  cls.def("expandedTo", (Box(Box::*)(Box const &) const) & Box::expandedTo);
129  cls.def("dilateBy", (Box & (Box::*)(Angle)) & Box::dilateBy, "angle"_a);
130  cls.def("dilateBy", (Box & (Box::*)(Angle, Angle)) & Box::dilateBy,
131  "width"_a, "height"_a);
132  cls.def("dilatedBy", (Box(Box::*)(Angle) const) & Box::dilatedBy,
133  "angle"_a);
134  cls.def("dilatedBy", (Box(Box::*)(Angle, Angle) const) & Box::dilatedBy,
135  "width"_a, "height"_a);
136  cls.def("erodeBy", (Box & (Box::*)(Angle)) & Box::erodeBy, "angle"_a);
137  cls.def("erodeBy", (Box & (Box::*)(Angle, Angle)) & Box::erodeBy, "width"_a,
138  "height"_a);
139  cls.def("erodedBy", (Box(Box::*)(Angle) const) & Box::erodedBy, "angle"_a);
140  cls.def("erodedBy", (Box(Box::*)(Angle, Angle) const) & Box::erodedBy,
141  "width"_a, "height"_a);
142  cls.def("getArea", &Box::getArea);
143  cls.def("relate",
144  (Relationship(Box::*)(LonLat const &) const) & Box::relate,
145  "point"_a);
146  // Rewrap this base class method since there are overloads in this subclass
147  cls.def("relate",
148  (Relationship(Box::*)(Region const &) const) & Box::relate,
149  "region"_a);
150 
151  // Note that the Region interface has already been wrapped.
152 
153  // The lambda is necessary for now; returning the unique pointer
154  // directly leads to incorrect results and crashes.
155  cls.def_static("decode",
156  [](py::bytes bytes) { return decode(bytes).release(); },
157  "bytes"_a);
158 
159  cls.def("__str__", [](Box const &self) {
160  return py::str("Box({!s}, {!s})").format(self.getLon(), self.getLat());
161  });
162  cls.def("__repr__", [](Box const &self) {
163  return py::str("Box({!r}, {!r})").format(self.getLon(), self.getLat());
164  });
165  cls.def(py::pickle(
166  [](const Box &self) { return python::encode(self); },
167  [](py::bytes bytes) { return decode(bytes).release(); }));
168 }
169 
170 } // <anonymous>
171 } // sphgeom
172 } // 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 ℝ³.
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
lsst::afw::table::Angle
lsst::geom::Angle Angle
Definition: misc.h:33
astshim.fitsChanContinued.contains
def contains(self, name)
Definition: fitsChanContinued.py:127
lsst::sphgeom::detail::relate
Relationship relate(VertexIterator const begin, VertexIterator const end, Box const &b)
Definition: ConvexPolygonImpl.h:258
lsst::sphgeom::Relationship
std::bitset< 3 > Relationship
Relationship describes how two sets are related.
Definition: Relationship.h:35
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.
NormalizedAngleInterval.h
This file declares a class representing closed intervals of normalized angles, i.e....
bytes
table::Key< table::Array< std::uint8_t > > bytes
Definition: Transform.cc:199
AngleInterval.h
This file defines a class for representing angle intervals.
LonLat.h
This file contains a class representing spherical coordinates.
relationship.h
Box.h
This file declares a class for representing longitude/latitude angle boxes on the unit sphere.
pybind11
Definition: _GenericMap.cc:40
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...