25 #include "pybind11/pybind11.h"
26 #include "pybind11/stl.h"
27 #include "numpy/arrayobject.h"
28 #include "ndarray/pybind11.h"
35 namespace py = pybind11;
36 using namespace py::literals;
45 template <
class Class>
46 std::string formatStr(Class
const &
self, std::string
const &pyClassName) {
47 std::ostringstream os;
49 auto const frameSet =
self.getFrameSet();
50 os <<
"[" << frameSet->getNin() <<
"->" << frameSet->getNout() <<
"]";
54 template <
class ExtraEndpo
int,
class FromEndpo
int,
class ToEndpo
int,
class PyClass>
55 void declareMethodTemplates(PyClass &
cls) {
56 using FirstTransform = Transform<ExtraEndpoint, FromEndpoint>;
57 using SecondTransform = Transform<FromEndpoint, ToEndpoint>;
58 using FinalTransform = Transform<ExtraEndpoint, ToEndpoint>;
60 cls.def(
"_of", (FinalTransform (SecondTransform::*)(FirstTransform
const &)
const) &
61 SecondTransform::template of<ExtraEndpoint>,
68 template <
class FromEndpo
int,
class ToEndpo
int>
69 void declareTransform(py::module &mod, std::string
const &fromName, std::string
const &toName) {
70 using Class = Transform<FromEndpoint, ToEndpoint>;
71 using ToPoint =
typename ToEndpoint::Point;
72 using ToArray =
typename ToEndpoint::Array;
73 using FromPoint =
typename FromEndpoint::Point;
74 using FromArray =
typename FromEndpoint::Array;
76 std::string
const pyClassName =
"Transform" + fromName +
"To" + toName;
78 py::class_<Class, std::shared_ptr<Class>>
cls(mod, pyClassName.c_str());
80 cls.def(py::init<ast::FrameSet const &, bool>(),
"frameSet"_a,
"simplify"_a =
true);
81 cls.def(py::init<ast::Mapping const &, bool>(),
"mapping"_a,
"simplify"_a =
true);
83 cls.def(
"hasForward", &Class::hasForward);
84 cls.def(
"hasInverse", &Class::hasInverse);
86 cls.def(
"getFromEndpoint", &Class::getFromEndpoint);
89 cls.def(
"getFrameSet", [](Class
const &
self) {
return self.getFrameSet()->copy(); });
90 cls.def(
"getToEndpoint", &Class::getToEndpoint);
92 cls.def(
"tranForward", (ToArray (Class::*)(FromArray
const &)
const) & Class::tranForward,
"array"_a);
93 cls.def(
"tranForward", (ToPoint (Class::*)(FromPoint
const &)
const) & Class::tranForward,
"point"_a);
94 cls.def(
"tranInverse", (FromArray (Class::*)(ToArray
const &)
const) & Class::tranInverse,
"array"_a);
95 cls.def(
"tranInverse", (FromPoint (Class::*)(ToPoint
const &)
const) & Class::tranInverse,
"point"_a);
96 cls.def(
"getInverse", &Class::getInverse);
101 declareMethodTemplates<GenericEndpoint, FromEndpoint, ToEndpoint>(
cls);
102 declareMethodTemplates<Point2Endpoint, FromEndpoint, ToEndpoint>(
cls);
103 declareMethodTemplates<Point3Endpoint, FromEndpoint, ToEndpoint>(
cls);
104 declareMethodTemplates<SpherePointEndpoint, FromEndpoint, ToEndpoint>(
cls);
107 cls.def(
"__str__", [pyClassName](Class
const &
self) {
return formatStr(
self, pyClassName); });
110 [pyClassName](Class
const &
self) {
return "lsst.afw.geom." + formatStr(
self, pyClassName); });
114 py::module mod(
"transform");
116 py::module::import(
"lsst.afw.geom.endpoint");
119 if (_import_array() < 0) {
120 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
124 declareTransform<GenericEndpoint, GenericEndpoint>(mod,
"Generic",
"Generic");
125 declareTransform<GenericEndpoint, Point2Endpoint>(mod,
"Generic",
"Point2");
126 declareTransform<GenericEndpoint, Point3Endpoint>(mod,
"Generic",
"Point3");
127 declareTransform<GenericEndpoint, SpherePointEndpoint>(mod,
"Generic",
"SpherePoint");
128 declareTransform<Point2Endpoint, GenericEndpoint>(mod,
"Point2",
"Generic");
129 declareTransform<Point2Endpoint, Point2Endpoint>(mod,
"Point2",
"Point2");
130 declareTransform<Point2Endpoint, Point3Endpoint>(mod,
"Point2",
"Point3");
131 declareTransform<Point2Endpoint, SpherePointEndpoint>(mod,
"Point2",
"SpherePoint");
132 declareTransform<Point3Endpoint, GenericEndpoint>(mod,
"Point3",
"Generic");
133 declareTransform<Point3Endpoint, Point2Endpoint>(mod,
"Point3",
"Point2");
134 declareTransform<Point3Endpoint, Point3Endpoint>(mod,
"Point3",
"Point3");
135 declareTransform<Point3Endpoint, SpherePointEndpoint>(mod,
"Point3",
"SpherePoint");
136 declareTransform<SpherePointEndpoint, GenericEndpoint>(mod,
"SpherePoint",
"Generic");
137 declareTransform<SpherePointEndpoint, Point2Endpoint>(mod,
"SpherePoint",
"Point2");
138 declareTransform<SpherePointEndpoint, Point3Endpoint>(mod,
"SpherePoint",
"Point3");
139 declareTransform<SpherePointEndpoint, SpherePointEndpoint>(mod,
"SpherePoint",
"SpherePoint");
PYBIND11_PLUGIN(_cameraPoint)