LSST Applications  21.0.0+c4f5df5339,21.0.0+e70536a077,21.0.0-1-ga51b5d4+7c60f8a6ea,21.0.0-10-g560fb7b+411cd868f8,21.0.0-10-gcf60f90+8c49d86aa0,21.0.0-13-gc485e61d+38156233bf,21.0.0-16-g7a993c7b9+1041c3824f,21.0.0-2-g103fe59+d9ceee3e5a,21.0.0-2-g1367e85+0b2f7db15a,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g5242d73+0b2f7db15a,21.0.0-2-g7f82c8f+feb9862f5e,21.0.0-2-g8f08a60+9c9a9cfcc8,21.0.0-2-ga326454+feb9862f5e,21.0.0-2-gde069b7+bedfc5e1fb,21.0.0-2-gecfae73+417509110f,21.0.0-2-gfc62afb+0b2f7db15a,21.0.0-3-g21c7a62+a91f7c0b59,21.0.0-3-g357aad2+062581ff1a,21.0.0-3-g4be5c26+0b2f7db15a,21.0.0-3-g65f322c+85aa0ead76,21.0.0-3-g7d9da8d+c4f5df5339,21.0.0-3-gaa929c8+411cd868f8,21.0.0-3-gc44e71e+fd4029fd48,21.0.0-3-ge02ed75+5d9b90b8aa,21.0.0-38-g070523fc+44fda2b515,21.0.0-4-g591bb35+5d9b90b8aa,21.0.0-4-g88306b8+3cdc83ea97,21.0.0-4-gc004bbf+d52368b591,21.0.0-4-gccdca77+a5c54364a0,21.0.0-5-g7ebb681+81e2098694,21.0.0-5-gdf36809+87b8d260e6,21.0.0-6-g2d4f3f3+e70536a077,21.0.0-6-g4e60332+5d9b90b8aa,21.0.0-6-g5ef7dad+3f4e29eeae,21.0.0-7-gc8ca178+0f5e56d48f,21.0.0-9-g9eb8d17+cc2c7a81aa,master-gac4afde19b+5d9b90b8aa,w.2021.07
LSST Data Management Base Package
endpoint.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 <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 #include <ostream>
23 #include <memory>
24 #include <string>
25 #include <typeinfo>
26 #include <type_traits>
27 
28 #include "pybind11/pybind11.h"
29 #include "pybind11/stl.h"
30 #include "ndarray/pybind11.h"
31 
32 #include "lsst/utils/python.h"
33 
34 #include "lsst/geom/Point.h"
35 #include "lsst/geom/SpherePoint.h"
36 #include "lsst/afw/geom/Endpoint.h"
37 
38 namespace py = pybind11;
39 using namespace py::literals;
40 
41 namespace lsst {
42 namespace afw {
43 namespace geom {
44 namespace {
45 
46 /*
47 Add `__str__`, `__repr__` and `getClassPrefix` methods to an Endpoint pybind11 wrapper
48 
49 str(self) = "GenericEndpoint(_nAxes_)" for GenericEndpoint, e.g. "GenericEndpoint(4)";
50  "_typeName_()" for all other Endpoint classes, e.g. "SpherePointEndpoint()",
51 repr(self) = "lsst.afw.geom." + str(self), e.g. "lsst.afw.geom.GenericEndpoint(4)"
52 */
53 template <typename PyClass>
54 void addStrAndRepr(PyClass& cls) {
55  using Class = typename PyClass::type; // C++ class associated with pybind11 wrapper class
56  utils::python::addOutputOp(cls, "__str__");
57  cls.def("__repr__", [](Class const& self) {
59  os << "lsst.afw.geom." << self;
60  return os.str();
61  });
62  cls.def_static("getClassPrefix", &Class::getClassPrefix);
63 }
64 
65 /*
66 Add getNPoints, dataFromPoint, dataFromArray, pointFromData and arrayFromData
67 */
68 template <typename PyClass>
69 void addDataConverters(PyClass& cls) {
70  using Class = typename PyClass::type; // C++ class associated with pybind11 wrapper class
71  cls.def("getNPoints", &Class::getNPoints);
72  cls.def("dataFromPoint", &Class::dataFromPoint);
73  cls.def("dataFromArray", &Class::dataFromArray);
74  cls.def("arrayFromData", &Class::arrayFromData);
75  cls.def("pointFromData", &Class::pointFromData);
76 }
77 
78 /*
79 Add makeFrame method
80 */
81 template <typename PyClass>
82 void addMakeFrame(PyClass& cls) {
83  using Class = typename PyClass::type; // C++ class associated with pybind11 wrapper class
84  // return a deep copy so Python cannot modify the internal state
85  cls.def("makeFrame", [](Class const& self) {
86  auto frame = self.makeFrame();
87  return frame->copy();
88  });
89 }
90 
91 // Allow Python classes to be compared across different BaseEndpoints
92 template <typename SelfClass, typename OtherClass, typename PyClass>
93 std::enable_if_t<std::is_base_of<SelfClass, OtherClass>::value> addEquals(PyClass& cls) {
94  cls.def("__eq__", &SelfClass::operator==);
95  cls.def("__ne__", &SelfClass::operator!=);
96 }
97 
98 template <typename SelfClass, typename OtherClass, typename PyClass>
99 std::enable_if_t<!std::is_base_of<SelfClass, OtherClass>::value> addEquals(PyClass& cls) {
100  cls.def("__eq__", [](SelfClass const& self, OtherClass const& other) { return false; });
101  cls.def("__ne__", [](SelfClass const& self, OtherClass const& other) { return true; });
102 }
103 
104 template <typename SelfClass, typename PyClass>
105 void addAllEquals(PyClass& cls) {
106  addEquals<SelfClass, GenericEndpoint>(cls);
107  addEquals<SelfClass, Point2Endpoint>(cls);
108  addEquals<SelfClass, SpherePointEndpoint>(cls);
109 }
110 
111 /*
112  * Declare BaseVectorEndpoint<Point, Array>;
113  * this is meant to be called by other `declare...` functions;
114  */
115 template <typename Point, typename Array>
116 void declareBaseEndpoint(py::module& mod, std::string const& suffix) {
117  using Class = BaseEndpoint<Point, Array>;
118  std::string const pyClassName = "_BaseEndpoint" + suffix;
119 
120  py::class_<Class, std::shared_ptr<Class>> cls(mod, pyClassName.c_str());
121 
122  cls.def_property_readonly("nAxes", &Class::getNAxes);
123  addDataConverters(cls);
124  addMakeFrame(cls);
125  cls.def("normalizeFrame", &Class::normalizeFrame);
126  addAllEquals<Class>(cls);
127 }
128 
129 // Declare BaseVectorEndpoint and all subclasses (the corresponding BaseEndpoint)
130 // This is meant to be called by other `declare...` functions;
131 template <typename Point>
132 void declareBaseVectorEndpoint(py::module& mod, std::string const& suffix) {
133  using Class = BaseVectorEndpoint<Point>;
134  using Array = typename Class::Array;
135  std::string const pyClassName = "_BaseVectorEndpoint" + suffix;
136 
137  declareBaseEndpoint<Point, Array>(mod, suffix);
138 
139  py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>> cls(mod, pyClassName.c_str());
140 
141  addDataConverters(cls);
142 }
143 
144 // Declare GenericEndpoint and all subclasses
145 void declareGenericEndpoint(py::module& mod) {
146  using Class = GenericEndpoint;
147  using Point = typename Class::Point;
148  using Array = typename Class::Array;
149 
150  declareBaseEndpoint<Point, Array>(mod, "Generic");
151 
152  py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>> cls(mod, "GenericEndpoint");
153 
154  cls.def(py::init<int>(), "nAxes"_a);
155 
156  addStrAndRepr(cls);
157 }
158 
160 void declarePoint2Endpoint(py::module& mod) {
161  using Class = Point2Endpoint;
162  using Point = typename Class::Point;
163  std::string const pointNumStr = "Point2";
164  std::string const pyClassName = pointNumStr + "Endpoint";
165 
166  declareBaseVectorEndpoint<Point>(mod, pointNumStr);
167 
168  py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>> cls(mod, pyClassName.c_str());
169 
170  cls.def(py::init<>());
171  // do not wrap the constructor that takes nAxes; it is an implementation detail
172 
173  cls.def("normalizeFrame", &Class::normalizeFrame);
174  addStrAndRepr(cls);
175 }
176 
178 void declareSpherePointEndpoint(py::module& mod) {
179  using Class = SpherePointEndpoint;
180  using Point = typename Class::Point;
181 
182  declareBaseVectorEndpoint<Point>(mod, "SpherePoint");
183 
184  py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>> cls(mod, "SpherePointEndpoint");
185 
186  cls.def(py::init<>());
187  // do not wrap the constructor that takes nAxes; it is an implementation detail
188 
189  addMakeFrame(cls);
190  cls.def("normalizeFrame", &Class::normalizeFrame);
191  addStrAndRepr(cls);
192 }
193 
194 PYBIND11_MODULE(endpoint, mod) {
195  py::module::import("lsst.geom");
196 
197  declareGenericEndpoint(mod);
198  declarePoint2Endpoint(mod);
199  declareSpherePointEndpoint(mod);
200 }
201 
202 } // namespace
203 } // namespace geom
204 } // namespace afw
205 } // namespace lsst
ItemVariant const * other
Definition: Schema.cc:56
std::ostream * os
Definition: Schema.cc:746
T c_str(T... args)
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133
py::class_< PixelAreaBoundedField, std::shared_ptr< PixelAreaBoundedField >, BoundedField > PyClass
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.
Definition: python.h:87
A base class for image defects.
table::Key< int > type
Definition: Detector.cc:163