27 #include "pybind11/pybind11.h"
28 #include "pybind11/stl.h"
29 #include "numpy/arrayobject.h"
30 #include "ndarray/pybind11.h"
36 namespace py = pybind11;
37 using namespace py::literals;
51 template <
typename PyClass>
52 void addStrAndRepr(PyClass&
cls) {
53 using Class =
typename PyClass::type;
54 cls.def(
"__str__", [](Class
const&
self) {
55 std::ostringstream os;
59 cls.def(
"__repr__", [](Class
const&
self) {
60 std::ostringstream os;
61 os <<
"lsst.afw.geom." <<
self;
64 cls.def_static(
"getClassPrefix", &Class::getClassPrefix);
70 template <
typename PyClass>
71 void addDataConverters(PyClass& cls) {
72 using Class =
typename PyClass::type;
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);
83 template <
typename PyClass>
84 void addMakeFrame(PyClass& cls) {
85 using Class =
typename PyClass::type;
87 cls.def(
"makeFrame", [](Class
const&
self) {
88 auto frame =
self.makeFrame();
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);
103 cls.def(
"__eq__", pyEquals);
105 [pyEquals](SelfClass
const&
self, OtherClass
const& other) {
return !pyEquals(
self, other); });
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);
119 template <
typename Po
int,
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;
124 py::class_<Class, std::shared_ptr<Class>>
cls(mod, pyClassName.c_str());
126 cls.def_property_readonly(
"nAxes", &Class::getNAxes);
127 addDataConverters(cls);
129 cls.def(
"normalizeFrame", &Class::normalizeFrame);
130 addAllEquals<Class>(
cls);
135 template <
typename Po
int>
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;
141 declareBaseEndpoint<Point, Array>(mod, suffix);
143 py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>>
cls(mod, pyClassName.c_str());
145 addDataConverters(cls);
149 void declareGenericEndpoint(py::module& mod) {
150 using Class = GenericEndpoint;
151 using Point =
typename Class::Point;
152 using Array =
typename Class::Array;
154 declareBaseEndpoint<Point, Array>(mod,
"Generic");
156 py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>>
cls(mod,
"GenericEndpoint");
158 cls.def(py::init<int>(),
"nAxes"_a);
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";
170 declareBaseVectorEndpoint<Point>(mod, pointNumStr);
172 py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>>
cls(mod, pyClassName.c_str());
174 cls.def(py::init<>());
177 cls.def(
"normalizeFrame", &Class::normalizeFrame);
182 void declareIcrsCoordEndpoint(py::module& mod) {
183 using Class = IcrsCoordEndpoint;
184 using Point =
typename Class::Point;
186 declareBaseVectorEndpoint<Point>(mod,
"IcrsCoord");
188 py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>>
cls(mod,
"IcrsCoordEndpoint");
190 cls.def(py::init<>());
194 cls.def(
"normalizeFrame", &Class::normalizeFrame);
199 py::module mod(
"endpoint");
201 py::module::import(
"lsst.afw.geom.coordinates");
207 if (_import_array() < 0) {
208 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
212 declareGenericEndpoint(mod);
213 declarePoint2Endpoint(mod);
214 declareIcrsCoordEndpoint(mod);
PYBIND11_PLUGIN(_cameraPoint)