30#include "pybind11/pybind11.h"
32#include "pybind11/stl.h"
33#include "ndarray/pybind11.h"
40using namespace py::literals;
54template <
typename PyClass>
55void addStrAndRepr(PyClass &cls) {
56 using Class =
typename PyClass::type;
58 cls.def(
"__repr__", [](Class
const &self) {
59 std::ostringstream os;
60 os <<
"lsst.afw.geom." << self;
63 cls.def_static(
"getClassPrefix", &Class::getClassPrefix);
69template <
typename PyClass>
70void 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);
82template <
typename PyClass>
83void addMakeFrame(PyClass &cls) {
84 using Class =
typename PyClass::type;
86 cls.def(
"makeFrame", [](Class
const &self) {
87 auto frame = self.makeFrame();
93template <
typename SelfClass,
typename OtherClass,
typename PyClass>
94std::enable_if_t<std::is_base_of<SelfClass, OtherClass>::value> addEquals(PyClass &cls) {
95 cls.def(
"__eq__", &SelfClass::operator==);
96 cls.def(
"__ne__", &SelfClass::operator!=);
99template <
typename SelfClass,
typename OtherClass,
typename PyClass>
100std::enable_if_t<!std::is_base_of<SelfClass, OtherClass>::value> addEquals(PyClass &cls) {
101 cls.def(
"__eq__", [](SelfClass
const &self, OtherClass
const &other) {
return false; });
102 cls.def(
"__ne__", [](SelfClass
const &self, OtherClass
const &other) {
return true; });
105template <
typename SelfClass,
typename PyClass>
106void addAllEquals(PyClass &cls) {
107 addEquals<SelfClass, GenericEndpoint>(cls);
108 addEquals<SelfClass, Point2Endpoint>(cls);
109 addEquals<SelfClass, SpherePointEndpoint>(cls);
116template <
typename Po
int,
typename Array>
117void declareBaseEndpoint(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
119 std::string
const pyClassName =
"_BaseEndpoint" + suffix;
120 wrappers.
wrapType(py::class_<Class, std::shared_ptr<Class>>(wrappers.
module, pyClassName.
c_str()),
121 [](
auto &mod,
auto &cls) {
122 cls.def_property_readonly(
"nAxes", &Class::getNAxes);
123 addDataConverters(cls);
125 cls.def(
"normalizeFrame", &Class::normalizeFrame);
126 addAllEquals<Class>(cls);
132template <
typename Po
int>
133void declareBaseVectorEndpoint(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
135 using Array =
typename Class::Array;
137 std::string
const pyClassName =
"_BaseVectorEndpoint" + suffix;
139 declareBaseEndpoint<Point, Array>(wrappers, suffix);
142 [](
auto &mod,
auto &cls) { addDataConverters(cls); });
146void declareGenericEndpoint(lsst::cpputils::python::WrapperCollection &wrappers) {
148 using Point =
typename Class::Point;
149 using Array =
typename Class::Array;
151 declareBaseEndpoint<Point, Array>(wrappers,
"Generic");
154 wrappers.
module,
"GenericEndpoint"),
155 [](
auto &mod,
auto &cls) {
156 cls.def(py::init<int>(),
"nAxes"_a);
163void declarePoint2Endpoint(lsst::cpputils::python::WrapperCollection &wrappers) {
165 using Point =
typename Class::Point;
166 std::string
const pointNumStr =
"Point2";
167 std::string
const pyClassName = pointNumStr +
"Endpoint";
169 declareBaseVectorEndpoint<Point>(wrappers, pointNumStr);
173 [](
auto &mod,
auto &cls) {
174 cls.def(py::init<>());
177 cls.def(
"normalizeFrame", &Class::normalizeFrame);
183void declareSpherePointEndpoint(lsst::cpputils::python::WrapperCollection &wrappers) {
185 using Point =
typename Class::Point;
187 declareBaseVectorEndpoint<Point>(wrappers,
"SpherePoint");
190 wrappers.
module,
"SpherePointEndpoint"),
191 [](
auto &mod,
auto &cls) {
192 cls.def(py::init<>());
196 cls.def(
"normalizeFrame", &Class::normalizeFrame);
203 declareGenericEndpoint(wrappers);
204 declarePoint2Endpoint(wrappers);
205 declareSpherePointEndpoint(wrappers);
Virtual base class for endpoints, which are helper classes for Transform.
Base class for endpoints with Array = std::vector<Point> where Point has 2 dimensions.
A generic endpoint for data in the format used by ast::Mapping.
An endpoint for lsst::geom::Point2D.
An endpoint for lsst::geom::SpherePoint.
A helper class for subdividing pybind11 module across multiple translation units (i....
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
void wrapEndpoint(lsst::cpputils::python::WrapperCollection &wrappers)
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.