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;
69 template <
typename PyClass>
70 void addDataConverters(PyClass& cls) {
71 using Class =
typename PyClass::type;
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);
82 template <
typename PyClass>
83 void addMakeFrame(PyClass& cls) {
84 using Class =
typename PyClass::type;
86 cls.def(
"makeFrame", [](Class
const&
self) {
87 auto frame =
self.makeFrame();
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);
102 cls.def(
"__eq__", pyEquals);
104 [pyEquals](SelfClass
const&
self, OtherClass
const& other) {
return !pyEquals(
self, other); });
107 template <
typename SelfClass,
typename PyClass>
108 void addAllEquals(PyClass& cls) {
109 addEquals<SelfClass, GenericEndpoint>(
cls);
110 addEquals<SelfClass, PointEndpoint<2>>(
cls);
111 addEquals<SelfClass, PointEndpoint<3>>(
cls);
112 addEquals<SelfClass, SpherePointEndpoint>(
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(
"getNAxes", &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);
165 void declarePointEndpoint(py::module& mod) {
166 using Class = PointEndpoint<N>;
167 using Point =
typename Class::Point;
168 std::string
const pointNumStr =
"Point" + std::to_string(N);
169 std::string
const pyClassName = pointNumStr +
"Endpoint";
171 declareBaseVectorEndpoint<Point>(mod, pointNumStr);
173 py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>>
cls(mod, pyClassName.c_str());
175 cls.def(py::init<>());
178 cls.def(
"normalizeFrame", &Class::normalizeFrame);
183 void declareSpherePointEndpoint(py::module& mod) {
184 using Class = SpherePointEndpoint;
185 using Point =
typename Class::Point;
187 declareBaseVectorEndpoint<Point>(mod,
"SpherePoint");
189 py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>>
cls(mod,
"SpherePointEndpoint");
191 cls.def(py::init<>());
195 cls.def(
"normalizeFrame", &Class::normalizeFrame);
200 py::module mod(
"endpoint");
202 py::module::import(
"lsst.afw.geom.coordinates");
203 py::module::import(
"lsst.afw.geom.spherePoint");
206 if (_import_array() < 0) {
207 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
211 declareGenericEndpoint(mod);
212 declarePointEndpoint<2>(mod);
213 declarePointEndpoint<3>(mod);
214 declareSpherePointEndpoint(mod);
PYBIND11_PLUGIN(_cameraPoint)