LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
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
lsst::utils::python::addOutputOp
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.
Definition: python.h:87
std::string
STL class.
python.h
lsst::afw
Definition: imageAlgorithm.dox:1
Endpoint.h
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
Point.h
std::string::c_str
T c_str(T... args)
other
ItemVariant const * other
Definition: Schema.cc:56
PyClass
py::class_< ProductBoundedField, std::shared_ptr< ProductBoundedField >, BoundedField > PyClass
Definition: productBoundedField.cc:32
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
std::ostringstream
STL class.
lsst::geom
Definition: AffineTransform.h:36
os
std::ostream * os
Definition: Schema.cc:746
type
table::Key< int > type
Definition: Detector.cc:163
pybind11
Definition: _GenericMap.cc:40
SpherePoint.h
lsst::meas::modelfit.psf.psfContinued.module
module
Definition: psfContinued.py:42
lsst::afw::cameraGeom::PYBIND11_MODULE
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133