23 #include "pybind11/pybind11.h"
24 #include "numpy/arrayobject.h"
25 #include "ndarray/pybind11.h"
31 #include "lsst/utils/python.h"
33 namespace py = pybind11;
34 using namespace pybind11::literals;
41 template <
typename Derived,
typename T,
int N>
42 using PyCoordinateBase = py::class_<CoordinateBase<Derived, T, N>>;
45 using PyCoordinateExpr = py::class_<CoordinateExpr<N>, CoordinateBase<CoordinateExpr<N>, bool, N>>;
47 template <
typename T,
int N>
48 using PyExtentBase = py::class_<ExtentBase<T, N>, CoordinateBase<Extent<T, N>, T, N>>;
50 template <
typename T,
int N>
51 using PyExtent = py::class_<Extent<T, N>, ExtentBase<T, N>>;
53 template <
typename T,
int N>
54 using PyPointBase = py::class_<PointBase<T, N>, CoordinateBase<Point<T, N>, T, N>>;
56 template <
typename T,
int N>
57 using PyPoint = py::class_<Point<T, N>, PointBase<T, N>>;
59 template <
typename Derived,
typename T,
int N>
60 void declareCoordinateBase(py::module &mod, std::string
const &suffix) {
61 static std::string
const name =
"CoordinateBase" + suffix;
62 PyCoordinateBase<Derived, T, N>
cls(mod, name.c_str());
65 cls.def(
"__getitem__", [](CoordinateBase<Derived, T, N> &
self,
int i) -> T {
66 return self[utils::python::cppIndex(N, i)];
68 cls.def(
"__setitem__", [](CoordinateBase<Derived, T, N> &
self,
int i, T value) {
69 self[utils::python::cppIndex(N, i)] = value;
71 cls.def(
"__len__", [](CoordinateBase<Derived, T, N> &c) ->
int {
return N; });
75 void declareCoordinateExpr(py::module &mod, std::string
const &suffix) {
76 static std::string
const name =
"CoordinateExpr" + suffix;
77 declareCoordinateBase<CoordinateExpr<N>, bool, N>(mod,
name);
79 PyCoordinateExpr<N> clsCoordinateExpr(mod, name.c_str());
82 clsCoordinateExpr.def(py::init<bool>(),
"val"_a =
false);
85 clsCoordinateExpr.def(
"and_", &CoordinateExpr<N>::and_);
86 clsCoordinateExpr.def(
"or_", &CoordinateExpr<N>::or_);
87 clsCoordinateExpr.def(
"not_", &CoordinateExpr<N>::not_);
89 mod.def(
"all", all<N>);
90 mod.def(
"any", any<N>);
93 template <
typename T,
int N>
94 void declareExtentBase(py::module &mod, std::string
const &suffix) {
95 declareCoordinateBase<Extent<T, N>, T, N>(mod,
"Extent" + suffix);
96 static std::string
const name =
"ExtentBase" + suffix;
98 PyExtentBase<T, N>
cls(mod, name.c_str());
103 cls.def(
"eq", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.eq(other); });
104 cls.def(
"ne", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.ne(other); });
105 cls.def(
"lt", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.lt(other); });
106 cls.def(
"le", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.le(other); });
107 cls.def(
"gt", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.gt(other); });
108 cls.def(
"ge", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.ge(other); });
109 cls.def(
"eq", [](ExtentBase<T, N>
const &
self, T other) {
return self.eq(other); });
110 cls.def(
"ne", [](ExtentBase<T, N>
const &
self, T other) {
return self.ne(other); });
111 cls.def(
"lt", [](ExtentBase<T, N>
const &
self, T other) {
return self.lt(other); });
112 cls.def(
"le", [](ExtentBase<T, N>
const &
self, T other) {
return self.le(other); });
113 cls.def(
"gt", [](ExtentBase<T, N>
const &
self, T other) {
return self.gt(other); });
114 cls.def(
"ge", [](ExtentBase<T, N>
const &
self, T other) {
return self.ge(other); });
117 cls.def(
"asPoint", &ExtentBase<T, N>::asPoint);
118 cls.def(
"computeNorm", &ExtentBase<T, N>::computeNorm);
119 cls.def(
"computeSquaredNorm", &ExtentBase<T, N>::computeSquaredNorm);
123 template <
typename T,
int N>
124 PyExtent<T, N> declareExtent(py::module &mod, std::string
const &suffix) {
125 declareExtentBase<T, N>(mod, suffix);
126 static std::string
const name =
"Extent" + suffix;
128 PyExtent<T, N>
cls(mod, name.c_str());
131 cls.def(py::init<T>(),
"value"_a = static_cast<T>(0));
132 cls.def(py::init<Point<int, N>
const &>());
133 cls.def(py::init<Point<T, N>
const &>());
134 cls.def(py::init<Extent<int, N>
const &>());
135 cls.def(py::init<Extent<T, N>
const &>());
136 cls.def(py::init<
typename Extent<T, N>::EigenVector>());
139 cls.def(
"__neg__", [](Extent<T, N>
const &
self) {
return -
self; });
140 cls.def(
"__pos__", [](Extent<T, N>
const &
self) {
return self; });
141 cls.def(
"__mul__", [](Extent<T, N>
const &
self,
int other) {
return self * other; }, py::is_operator());
142 cls.def(
"__mul__", [](Extent<T, N>
const &
self,
double other) {
return self * other; },
144 cls.def(
"__rmul__", [](Extent<T, N>
const &
self,
int other) {
return self * other; }, py::is_operator());
145 cls.def(
"__rmul__", [](Extent<T, N>
const &
self,
double other) {
return self * other; },
147 cls.def(
"__add__", [](Extent<T, N>
const &
self, Extent<int, N>
const &other) {
return self + other; },
149 cls.def(
"__add__", [](Extent<T, N>
const &
self, Extent<double, N>
const &other) {
return self + other; },
152 [](Extent<T, N>
const &
self, Point<int, N>
const &other) {
return self + Point<T, N>(other); },
154 cls.def(
"__add__", [](Extent<T, N>
const &
self, Point<double, N>
const &other) {
return self + other; },
157 [](Extent<T, N>
const &
self, Extent<int, N>
const &other) {
return self - Extent<T, N>(other); },
159 cls.def(
"__sub__", [](Extent<T, N>
const &
self, Extent<double, N>
const &other) {
return self - other; },
161 cls.def(
"__eq__", [](Extent<T, N>
const &
self, Extent<T, N>
const &other) {
return self == other; },
163 cls.def(
"__ne__", [](Extent<T, N>
const &
self, Extent<T, N>
const &other) {
return self != other; },
167 cls.def(
"clone", [](Extent<T, N>
const &
self) {
return Extent<T, N>{
self}; });
173 template <
typename T>
174 PyExtent<T, 2> declareExtent2(py::module &mod, std::string
const &suffix) {
175 auto cls = declareExtent<T, 2>(mod, std::string(
"2") + suffix);
178 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 2; });
181 cls.def(py::init<int, int>(),
"x"_a,
"y"_a);
182 cls.def(py::init<double, double>(),
"x"_a,
"y"_a);
185 cls.def(
"getX", [](Extent<T, 2>
const &
self) {
return self[0]; });
186 cls.def(
"getY", [](Extent<T, 2>
const &
self) {
return self[1]; });
187 cls.def(
"setX", [](Extent<T, 2> &
self, T other) {
self[0] = other; });
188 cls.def(
"setY", [](Extent<T, 2> &
self, T other) {
self[1] = other; });
194 template <
typename T>
195 PyExtent<T, 3> declareExtent3(py::module &mod,
const std::string &suffix) {
196 auto cls = declareExtent<T, 3>(mod, std::string(
"3") + suffix);
199 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 3; });
202 cls.def(py::init<int, int, int>(),
"x"_a,
"y"_a,
"z"_a);
203 cls.def(py::init<double, double, double>(),
"x"_a,
"y"_a,
"z"_a);
206 cls.def(
"getX", [](Extent<T, 3>
const &
self) {
return self[0]; });
207 cls.def(
"getY", [](Extent<T, 3>
const &
self) {
return self[1]; });
208 cls.def(
"getZ", [](Extent<T, 3>
const &
self) {
return self[2]; });
209 cls.def(
"setX", [](Extent<T, 3> &
self, T other) {
self[0] = other; });
210 cls.def(
"setY", [](Extent<T, 3> &
self, T other) {
self[1] = other; });
211 cls.def(
"setZ", [](Extent<T, 3> &
self, T other) {
self[2] = other; });
222 void declareExtentOperators(py::module &mod, PyExtent<int, N> &clsI, PyExtent<double, N> &clsD) {
226 clsI.def(
"__floordiv__",
227 [](Extent<int, N>
const &
self,
int other) -> Extent<int, N> {
228 return floor(
self / static_cast<double>(other));
232 clsI.def(
"__truediv__", [](Extent<int, N>
const &
self,
double other) {
return self / other; },
234 clsD.def(
"__truediv__", [](Extent<double, N>
const &
self,
double other) {
return self / other; },
237 clsI.def(
"__ifloordiv__", [](Extent<int, N> &
self,
int other) -> Extent<int, N> & {
238 self =
floor(
self / static_cast<double>(other));
242 clsI.def(
"__itruediv__", [](Extent<int, N> &
self,
double other) {
243 PyErr_SetString(PyExc_TypeError,
"In-place true division not supported for Extent<int,N>.");
244 throw py::error_already_set();
246 clsD.def(
"__itruediv__", [](Extent<double, N> &
self,
double other) -> Extent<double, N> & {
251 clsI.def(
"__iadd__", [](Extent<int, N> &
self, Extent<int, N>
const &other) -> Extent<int, N> & {
255 clsD.def(
"__iadd__", [](Extent<double, N> &
self, Extent<double, N>
const &other) -> Extent<double, N> & {
259 clsD.def(
"__iadd__", [](Extent<double, N> &
self, Extent<int, N>
const &other) -> Extent<double, N> & {
264 clsI.def(
"__isub__", [](Extent<int, N> &
self, Extent<int, N>
const &other) -> Extent<int, N> & {
268 clsD.def(
"__isub__", [](Extent<double, N> &
self, Extent<double, N>
const &other) -> Extent<double, N> & {
272 clsD.def(
"__isub__", [](Extent<double, N> &
self, Extent<int, N>
const &other) -> Extent<double, N> & {
277 clsI.def(
"__imul__", [](Extent<int, N> &
self,
int other) -> Extent<int, N> & {
281 clsD.def(
"__imul__", [](Extent<double, N> &
self,
int other) -> Extent<double, N> & {
285 clsD.def(
"__imul__", [](Extent<double, N> &
self,
double other) -> Extent<double, N> & {
291 mod.def(
"truncate", truncate<N>);
292 mod.def(
"floor", floor<N>);
293 mod.def(
"ceil", ceil<N>);
295 clsD.def(
"truncate", truncate<N>);
296 clsD.def(
"floor", floor<N>);
297 clsD.def(
"ceil", ceil<N>);
300 template <
typename T,
int N>
301 void declarePointBase(py::module &mod, std::string
const &suffix) {
302 declareCoordinateBase<Point<T, N>, T, N>(mod,
"Point" + suffix);
303 static std::string
const name =
"PointBase" + suffix;
304 PyPointBase<T, N>
cls(mod, name.c_str());
309 cls.def(
"eq", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.eq(other); });
310 cls.def(
"ne", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.ne(other); });
311 cls.def(
"lt", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.lt(other); });
312 cls.def(
"le", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.le(other); });
313 cls.def(
"gt", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.gt(other); });
314 cls.def(
"ge", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.ge(other); });
315 cls.def(
"eq", [](PointBase<T, N>
const &
self, T other) {
return self.eq(other); });
316 cls.def(
"ne", [](PointBase<T, N>
const &
self, T other) {
return self.ne(other); });
317 cls.def(
"lt", [](PointBase<T, N>
const &
self, T other) {
return self.lt(other); });
318 cls.def(
"le", [](PointBase<T, N>
const &
self, T other) {
return self.le(other); });
319 cls.def(
"gt", [](PointBase<T, N>
const &
self, T other) {
return self.gt(other); });
320 cls.def(
"ge", [](PointBase<T, N>
const &
self, T other) {
return self.ge(other); });
323 cls.def(
"asExtent", &PointBase<T, N>::asExtent);
324 cls.def(
"shift", &PointBase<T, N>::shift);
326 cls.def(
"distanceSquared", &PointBase<T, N>::distanceSquared);
327 cls.def(
"toString", &PointBase<T, N>::toString);
331 template <
typename T,
int N>
332 PyPoint<T, N> declarePoint(py::module &mod, std::string
const &suffix) {
333 declarePointBase<T, N>(mod, suffix);
334 static std::string
const name =
"Point" + suffix;
335 PyPoint<T, N>
cls(mod, name.c_str());
338 cls.def(py::init<T>(),
"value"_a = static_cast<T>(0));
340 cls.def(py::init<Point<double, N>
const &>());
341 cls.def(py::init<Point<int, N>
const &>());
342 cls.def(py::init<Extent<T, N>
const &>());
343 cls.def(py::init<
typename Point<T, N>::EigenVector>());
346 cls.def(
"__add__", [](Point<T, N>
const &
self, Extent<double, N> &other) {
return self + other; },
348 cls.def(
"__add__", [](Point<T, N>
const &
self, Extent<int, N> &other) {
return self + other; },
350 cls.def(
"__sub__", [](Point<T, N>
const &
self, Point<T, N> &other) {
return self - other; },
352 cls.def(
"__sub__", [](Point<T, N>
const &
self, Extent<T, N> &other) {
return self - other; },
354 cls.def(
"__sub__", [](Point<T, N>
const &
self, Point<double, N> &other) {
return self - other; },
356 cls.def(
"__sub__", [](Point<T, N>
const &
self, Point<int, N> &other) {
return self - other; },
358 cls.def(
"__sub__", [](Point<T, N>
const &
self, Extent<double, N> &other) {
return self - other; },
360 cls.def(
"__sub__", [](Point<T, N>
const &
self, Extent<int, N> &other) {
return self - other; },
362 cls.def(
"__eq__", [](Point<T, N>
const &
self, Point<T, N>
const &other) {
return self == other; },
364 cls.def(
"__ne__", [](Point<T, N>
const &
self, Point<T, N>
const &other) {
return self != other; },
368 cls.def(
"clone", [](Point<T, N>
const &
self) {
return Point<T, N>{
self}; });
374 template <
typename T>
375 PyPoint<T, 2> declarePoint2(py::module &mod, std::string
const &suffix) {
376 auto cls = declarePoint<T, 2>(mod, std::string(
"2") + suffix);
379 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 2; });
382 cls.def(py::init<int, int>(),
"x"_a,
"y"_a);
383 cls.def(py::init<double, double>(),
"x"_a,
"y"_a);
386 cls.def(
"getX", [](Point<T, 2>
const &
self) {
return self[0]; });
387 cls.def(
"getY", [](Point<T, 2>
const &
self) {
return self[1]; });
388 cls.def(
"setX", [](Point<T, 2> &
self, T other) {
self[0] = other; });
389 cls.def(
"setY", [](Point<T, 2> &
self, T other) {
self[1] = other; });
394 template <
typename T>
395 PyPoint<T, 3> declarePoint3(py::module &mod, std::string
const &suffix) {
396 auto cls = declarePoint<T, 3>(mod, std::string(
"3") + suffix);
399 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 3; });
402 cls.def(py::init<int, int, int>(),
"x"_a,
"y"_a,
"z"_a);
403 cls.def(py::init<double, double, double>(),
"x"_a,
"y"_a,
"z"_a);
406 cls.def(
"getX", [](Point<T, 3>
const &
self) {
return self[0]; });
407 cls.def(
"getY", [](Point<T, 3>
const &
self) {
return self[1]; });
408 cls.def(
"getZ", [](Point<T, 3>
const &
self) {
return self[2]; });
409 cls.def(
"setX", [](Point<T, 3> &
self, T other) {
self[0] = other; });
410 cls.def(
"setY", [](Point<T, 3> &
self, T other) {
self[1] = other; });
411 cls.def(
"setZ", [](Point<T, 3> &
self, T other) {
self[2] = other; });
422 void declarePointOperators(py::module &mod, PyPoint<int, N> &clsI, PyPoint<double, N> &clsD) {
423 clsI.def(
"__iadd__", [](Point<int, N> &
self, Extent<int, N>
const &other) {
427 clsD.def(
"__iadd__", [](Point<double, N> &
self, Extent<int, N>
const &other) {
431 clsD.def(
"__iadd__", [](Point<double, N> &
self, Extent<double, N>
const &other) {
435 clsI.def(
"__isub__", [](Point<int, N> &
self, Extent<int, N>
const &other) {
439 clsD.def(
"__isub__", [](Point<double, N> &
self, Extent<int, N>
const &other) {
443 clsD.def(
"__isub__", [](Point<double, N> &
self, Extent<double, N>
const &other) {
450 py::module mod(
"coordinates");
452 if (_import_array() < 0) {
453 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
460 declareCoordinateExpr<2>(mod,
"2");
461 declareCoordinateExpr<3>(mod,
"3");
463 auto clsExtent2I = declareExtent2<int>(mod,
"I");
464 auto clsExtent2D = declareExtent2<double>(mod,
"D");
465 declareExtentOperators(mod, clsExtent2I, clsExtent2D);
467 auto clsExtent3I = declareExtent3<int>(mod,
"I");
468 auto clsExtent3D = declareExtent3<double>(mod,
"D");
469 declareExtentOperators(mod, clsExtent3I, clsExtent3D);
471 auto clsPoint2I = declarePoint2<int>(mod,
"I");
472 auto clsPoint2D = declarePoint2<double>(mod,
"D");
473 declarePointOperators(mod, clsPoint2I, clsPoint2D);
475 auto clsPoint3I = declarePoint3<int>(mod,
"I");
476 auto clsPoint3D = declarePoint3<double>(mod,
"D");
477 declarePointOperators(mod, clsPoint3I, clsPoint3D);
PYBIND11_PLUGIN(_cameraPoint)
Extent< int, N > floor(Extent< double, N > const &input)
Return the component-wise floor (round towards more negative).
table::Key< std::string > name