lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
27 #include "pybind11/pybind11.h"
28 #include "pybind11/stl.h"
29 #include "numpy/arrayobject.h"
30 #include "ndarray/pybind11.h"
31 
32 #include "lsst/afw/coord/Coord.h"
33 #include "lsst/afw/geom/Point.h"
34 #include "lsst/afw/geom/Endpoint.h"
35 
36 namespace py = pybind11;
37 using namespace py::literals;
38 
39 namespace lsst {
40 namespace afw {
41 namespace geom {
42 namespace {
43 
44 /*
45 Add `__str__` and `__repr__` methods to an Endpoint pybind11 wrapper
46 
47 str(self) = "GenericEndpoint(_nAxes_)" for GenericEndpoint, e.g. "GenericEndpoint(4)";
48  "_typeName_()" for all other Endpoint classes, e.g. "IcrsCoordEndpoint()",
49 repr(self) = "lsst.afw.geom." + str(self), e.g. "lsst.afw.geom.GenericEndpoint(4)"
50 */
51 template <typename PyClass>
52 void addStrAndRepr(PyClass& cls) {
53  using Class = typename PyClass::type; // C++ class associated with pybind11 wrapper class
54  cls.def("__str__", [](Class const& self) {
55  std::ostringstream os;
56  os << self;
57  return os.str();
58  });
59  cls.def("__repr__", [](Class const& self) {
60  std::ostringstream os;
61  os << "lsst.afw.geom." << self;
62  return os.str();
63  });
64 }
65 
66 /*
67 Add getNPoints, dataFromPoint, dataFromArray, pointFromData and arrayFromData
68 */
69 template <typename PyClass>
70 void addDataConverters(PyClass& cls) {
71  using Class = typename PyClass::type; // C++ class associated with pybind11 wrapper class
72  cls.def("getNPoints", &Class::getNPoints);
73  cls.def("dataFromPoint", &Class::dataFromPoint);
74  cls.def("dataFromArray", &Class::dataFromArray);
75  cls.def("arrayFromData", &Class::arrayFromData);
76  cls.def("pointFromData", &Class::pointFromData);
77 }
78 
79 /*
80 Add makeFrame method
81 */
82 template <typename PyClass>
83 void addMakeFrame(PyClass& cls) {
84  using Class = typename PyClass::type; // C++ class associated with pybind11 wrapper class
85  // return a deep copy so Python cannot modify the internal state
86  cls.def("makeFrame", [](Class const& self) {
87  auto frame = self.makeFrame();
88  return frame->copy();
89  });
90 }
91 
92 // Comparison of different Endpoints useful in Python but counterproductive
93 // in C++: point2Endpoint == spherePoint should not compile instead of
94 // returning `false`. Therefore, implemented only on the Python side.
95 // Two Endpoints are defined to be equal if and only if they have the same
96 // implementation type and the same number of dimensions
97 template <typename SelfClass, typename OtherClass, typename PyClass>
98 void addEquals(PyClass& cls) {
99  auto pyEquals = [](SelfClass const& self, OtherClass const& other) {
100  return self.getNAxes() == other.getNAxes() && typeid(self) == typeid(other);
101  };
102  cls.def("__eq__", pyEquals);
103  cls.def("__ne__",
104  [pyEquals](SelfClass const& self, OtherClass const& other) { return !pyEquals(self, other); });
105 }
106 
107 template <typename SelfClass, typename PyClass>
108 void addAllEquals(PyClass& cls) {
109  addEquals<SelfClass, GenericEndpoint>(cls);
110  addEquals<SelfClass, Point2Endpoint>(cls);
111  addEquals<SelfClass, IcrsCoordEndpoint>(cls);
112 }
113 
114 /*
115  * Declare BaseVectorEndpoint<Point, Array>;
116  * this is meant to be called by other `declare...` functions;
117  */
118 template <typename Point, typename Array>
119 void declareBaseEndpoint(py::module& mod, std::string const& suffix) {
120  using Class = BaseEndpoint<Point, Array>;
121  std::string const pyClassName = "_BaseEndpoint" + suffix;
122 
123  py::class_<Class, std::shared_ptr<Class>> cls(mod, pyClassName.c_str());
124 
125  cls.def_property_readonly("nAxes", &Class::getNAxes);
126  addDataConverters(cls);
127  addMakeFrame(cls);
128  cls.def("normalizeFrame", &Class::normalizeFrame);
129  addAllEquals<Class>(cls);
130 }
131 
132 // Declare BaseVectorEndpoint and all subclasses (the corresponding BaseEndpoint)
133 // This is meant to be called by other `declare...` functions;
134 template <typename Point>
135 void declareBaseVectorEndpoint(py::module& mod, std::string const& suffix) {
136  using Class = BaseVectorEndpoint<Point>;
137  using Array = typename Class::Array;
138  std::string const pyClassName = "_BaseVectorEndpoint" + suffix;
139 
140  declareBaseEndpoint<Point, Array>(mod, suffix);
141 
142  py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>> cls(mod, pyClassName.c_str());
143 
144  addDataConverters(cls);
145 }
146 
147 // Declare GenericEndpoint and all subclasses
148 void declareGenericEndpoint(py::module& mod) {
149  using Class = GenericEndpoint;
150  using Point = typename Class::Point;
151  using Array = typename Class::Array;
152 
153  declareBaseEndpoint<Point, Array>(mod, "Generic");
154 
155  py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>> cls(mod, "GenericEndpoint");
156 
157  cls.def(py::init<int>(), "nAxes"_a);
158 
159  addStrAndRepr(cls);
160 }
161 
163 void declarePoint2Endpoint(py::module& mod) {
164  using Class = Point2Endpoint;
165  using Point = typename Class::Point;
166  std::string const pointNumStr = "Point2";
167  std::string const pyClassName = pointNumStr + "Endpoint";
168 
169  declareBaseVectorEndpoint<Point>(mod, pointNumStr);
170 
171  py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>> cls(mod, pyClassName.c_str());
172 
173  cls.def(py::init<>());
174  // do not wrap the constructor that takes nAxes; it is an implementation detail
175 
176  cls.def("normalizeFrame", &Class::normalizeFrame);
177  addStrAndRepr(cls);
178 }
179 
181 void declareIcrsCoordEndpoint(py::module& mod) {
182  using Class = IcrsCoordEndpoint;
183  using Point = typename Class::Point;
184 
185  declareBaseVectorEndpoint<Point>(mod, "IcrsCoord");
186 
187  py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>> cls(mod, "IcrsCoordEndpoint");
188 
189  cls.def(py::init<>());
190  // do not wrap the constructor that takes nAxes; it is an implementation detail
191 
192  addMakeFrame(cls);
193  cls.def("normalizeFrame", &Class::normalizeFrame);
194  addStrAndRepr(cls);
195 }
196 
197 PYBIND11_PLUGIN(endpoint) {
198  py::module mod("endpoint");
199 
200  py::module::import("lsst.afw.geom.coordinates");
201  // The following import causes a circular import. Uncomment or delete once DM-10999 is fixed.
202  // Until then a user must manually `import lsst.afw.coord` before using `IcrsEndpoint`
203  // py::module::import("lsst.afw.coord.coord");
204 
205  // Need to import numpy for ndarray and eigen conversions
206  if (_import_array() < 0) {
207  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
208  return nullptr;
209  }
210 
211  declareGenericEndpoint(mod);
212  declarePoint2Endpoint(mod);
213  declareIcrsCoordEndpoint(mod);
214 
215  return mod.ptr();
216 }
217 
218 } // namespace
219 } // namespace geom
220 } // namespace afw
221 } // namespace lsst
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
double const _a
Definition: RandomImage.cc:77