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