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, Point2Endpoint>(
cls);
111 addEquals<SelfClass, IcrsCoordEndpoint>(
cls);
118 template <
typename Po
int,
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;
123 py::class_<Class, std::shared_ptr<Class>>
cls(mod, pyClassName.c_str());
125 cls.def_property_readonly(
"nAxes", &Class::getNAxes);
126 addDataConverters(cls);
128 cls.def(
"normalizeFrame", &Class::normalizeFrame);
129 addAllEquals<Class>(
cls);
134 template <
typename Po
int>
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;
140 declareBaseEndpoint<Point, Array>(mod, suffix);
142 py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>>
cls(mod, pyClassName.c_str());
144 addDataConverters(cls);
148 void declareGenericEndpoint(py::module& mod) {
149 using Class = GenericEndpoint;
150 using Point =
typename Class::Point;
151 using Array =
typename Class::Array;
153 declareBaseEndpoint<Point, Array>(mod,
"Generic");
155 py::class_<Class, std::shared_ptr<Class>, BaseEndpoint<Point, Array>>
cls(mod,
"GenericEndpoint");
157 cls.def(py::init<int>(),
"nAxes"_a);
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";
169 declareBaseVectorEndpoint<Point>(mod, pointNumStr);
171 py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>>
cls(mod, pyClassName.c_str());
173 cls.def(py::init<>());
176 cls.def(
"normalizeFrame", &Class::normalizeFrame);
181 void declareIcrsCoordEndpoint(py::module& mod) {
182 using Class = IcrsCoordEndpoint;
183 using Point =
typename Class::Point;
185 declareBaseVectorEndpoint<Point>(mod,
"IcrsCoord");
187 py::class_<Class, std::shared_ptr<Class>, BaseVectorEndpoint<Point>>
cls(mod,
"IcrsCoordEndpoint");
189 cls.def(py::init<>());
193 cls.def(
"normalizeFrame", &Class::normalizeFrame);
198 py::module mod(
"endpoint");
200 py::module::import(
"lsst.afw.geom.coordinates");
206 if (_import_array() < 0) {
207 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
211 declareGenericEndpoint(mod);
212 declarePoint2Endpoint(mod);
213 declareIcrsCoordEndpoint(mod);
PYBIND11_PLUGIN(_cameraPoint)