22 #include "pybind11/pybind11.h" 23 #include "pybind11/eigen.h" 24 #include "ndarray/pybind11.h" 39 template <
typename Derived,
typename T,
int N>
40 using PyCoordinateBase = py::class_<CoordinateBase<Derived, T, N>>;
43 using PyCoordinateExpr = py::class_<CoordinateExpr<N>, CoordinateBase<CoordinateExpr<N>, bool, N>>;
45 template <
typename T,
int N>
46 using PyExtentBase = py::class_<ExtentBase<T, N>, CoordinateBase<Extent<T, N>, T, N>>;
48 template <
typename T,
int N>
49 using PyExtent = py::class_<Extent<T, N>, ExtentBase<T, N>>;
51 template <
typename T,
int N>
52 using PyPointBase = py::class_<PointBase<T, N>, CoordinateBase<Point<T, N>, T, N>>;
54 template <
typename T,
int N>
55 using PyPoint = py::class_<Point<T, N>, PointBase<T, N>>;
57 template <
typename Derived,
typename T,
int N>
60 PyCoordinateBase<Derived, T, N>
cls(mod, name.
c_str());
63 cls.def(
"__getitem__", [](CoordinateBase<Derived, T, N> &
self,
int i) -> T {
66 cls.def(
"__setitem__", [](CoordinateBase<Derived, T, N> &
self,
int i, T value) {
69 cls.def(
"__len__", [](CoordinateBase<Derived, T, N> &c) ->
int {
return N; });
74 static std::string const name =
"CoordinateExpr" + suffix;
75 declareCoordinateBase<CoordinateExpr<N>, bool, N>(mod,
name);
77 PyCoordinateExpr<N> clsCoordinateExpr(mod, name.
c_str());
80 clsCoordinateExpr.def(py::init<bool>(),
"val"_a =
false);
83 clsCoordinateExpr.def(
"and_", &CoordinateExpr<N>::and_);
84 clsCoordinateExpr.def(
"or_", &CoordinateExpr<N>::or_);
85 clsCoordinateExpr.def(
"not_", &CoordinateExpr<N>::not_);
87 mod.def(
"all", all<N>);
88 mod.def(
"any", any<N>);
91 template <
typename T,
int N>
93 declareCoordinateBase<Extent<T, N>, T, N>(mod,
"Extent" + suffix);
94 static std::string const name =
"ExtentBase" + suffix;
96 PyExtentBase<T, N>
cls(mod, name.c_str());
101 cls.def(
"eq", [](ExtentBase<T, N>
const &
self, Extent<T, N>
other) {
return self.eq(other); });
102 cls.def(
"ne", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.ne(other); });
103 cls.def(
"lt", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.lt(other); });
104 cls.def(
"le", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.le(other); });
105 cls.def(
"gt", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.gt(other); });
106 cls.def(
"ge", [](ExtentBase<T, N>
const &
self, Extent<T, N> other) {
return self.ge(other); });
107 cls.def(
"eq", [](ExtentBase<T, N>
const &
self, T other) {
return self.eq(other); });
108 cls.def(
"ne", [](ExtentBase<T, N>
const &
self, T other) {
return self.ne(other); });
109 cls.def(
"lt", [](ExtentBase<T, N>
const &
self, T other) {
return self.lt(other); });
110 cls.def(
"le", [](ExtentBase<T, N>
const &
self, T other) {
return self.le(other); });
111 cls.def(
"gt", [](ExtentBase<T, N>
const &
self, T other) {
return self.gt(other); });
112 cls.def(
"ge", [](ExtentBase<T, N>
const &
self, T other) {
return self.ge(other); });
115 cls.def(
"asPoint", &ExtentBase<T, N>::asPoint);
116 cls.def(
"computeNorm", &ExtentBase<T, N>::computeNorm);
117 cls.def(
"computeSquaredNorm", &ExtentBase<T, N>::computeSquaredNorm);
121 template <
typename T,
int N>
123 declareExtentBase<T, N>(mod, suffix);
126 PyExtent<T, N>
cls(mod, name.c_str());
129 cls.def(py::init<T>(),
"value"_a = static_cast<T>(0));
134 cls.def(
py::init<
typename Extent<T, N>::EigenVector>());
137 cls.def(
"__neg__", [](Extent<T, N>
const &
self) {
return -
self; });
138 cls.def(
"__pos__", [](Extent<T, N>
const &
self) {
return self; });
139 cls.def(
"__mul__", [](Extent<T, N>
const &
self,
int other) {
return self *
other; }, py::is_operator());
140 cls.def(
"__mul__", [](Extent<T, N>
const &
self,
double other) {
return self *
other; },
142 cls.def(
"__rmul__", [](Extent<T, N>
const &
self,
int other) {
return self *
other; }, py::is_operator());
143 cls.def(
"__rmul__", [](Extent<T, N>
const &
self,
double other) {
return self *
other; },
145 cls.def(
"__add__", [](Extent<T, N>
const &
self, Extent<int, N>
const &other) {
return self +
other; },
147 cls.def(
"__add__", [](Extent<T, N>
const &
self, Extent<double, N>
const &other) {
return self +
other; },
150 [](Extent<T, N>
const &
self, Point<int, N>
const &other) {
return self + Point<T, N>(
other); },
152 cls.def(
"__add__", [](Extent<T, N>
const &
self, Point<double, N>
const &other) {
return self +
other; },
155 [](Extent<T, N>
const &
self, Extent<int, N>
const &other) {
return self - Extent<T, N>(
other); },
157 cls.def(
"__sub__", [](Extent<T, N>
const &
self, Extent<double, N>
const &other) {
return self -
other; },
159 cls.def(
"__eq__", [](Extent<T, N>
const &
self, Extent<T, N>
const &other) {
return self ==
other; },
161 cls.def(
"__ne__", [](Extent<T, N>
const &
self, Extent<T, N>
const &other) {
return self !=
other; },
165 cls.def(
"clone", [](Extent<T, N>
const &
self) {
return Extent<T, N>{
self}; });
171 template <
typename T>
176 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 2; });
179 cls.def(py::init<int, int>(),
"x"_a,
"y"_a);
180 cls.def(py::init<double, double>(),
"x"_a,
"y"_a);
183 cls.def(
"getX", [](Extent<T, 2>
const &
self) {
return self[0]; });
184 cls.def(
"getY", [](Extent<T, 2>
const &
self) {
return self[1]; });
185 cls.def(
"setX", [](Extent<T, 2> &
self, T other) {
self[0] =
other; });
186 cls.def(
"setY", [](Extent<T, 2> &
self, T other) {
self[1] =
other; });
192 template <
typename T>
197 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 3; });
200 cls.def(py::init<int, int, int>(),
"x"_a,
"y"_a,
"z"_a);
201 cls.def(py::init<double, double, double>(),
"x"_a,
"y"_a,
"z"_a);
204 cls.def(
"getX", [](Extent<T, 3>
const &
self) {
return self[0]; });
205 cls.def(
"getY", [](Extent<T, 3>
const &
self) {
return self[1]; });
206 cls.def(
"getZ", [](Extent<T, 3>
const &
self) {
return self[2]; });
207 cls.def(
"setX", [](Extent<T, 3> &
self, T other) {
self[0] =
other; });
208 cls.def(
"setY", [](Extent<T, 3> &
self, T other) {
self[1] =
other; });
209 cls.def(
"setZ", [](Extent<T, 3> &
self, T other) {
self[2] =
other; });
220 void declareExtentOperators(
py::module &mod, PyExtent<int, N> &clsI, PyExtent<double, N> &clsD) {
224 clsI.def(
"__floordiv__",
225 [](Extent<int, N>
const &
self,
int other) -> Extent<int, N> {
226 return floor(
self / static_cast<double>(other));
230 clsI.def(
"__truediv__", [](Extent<int, N>
const &
self,
double other) {
return self /
other; },
232 clsD.def(
"__truediv__", [](Extent<double, N>
const &
self,
double other) {
return self /
other; },
235 clsI.def(
"__ifloordiv__", [](Extent<int, N> &
self,
int other) -> Extent<int, N> & {
236 self =
floor(
self / static_cast<double>(other));
240 clsI.def(
"__itruediv__", [](Extent<int, N> &
self,
double other) {
241 PyErr_SetString(PyExc_TypeError,
"In-place true division not supported for Extent<int,N>.");
242 throw py::error_already_set();
244 clsD.def(
"__itruediv__", [](Extent<double, N> &
self,
double other) -> Extent<double, N> & {
249 clsI.def(
"__iadd__", [](Extent<int, N> &
self, Extent<int, N>
const &other) -> Extent<int, N> & {
253 clsD.def(
"__iadd__", [](Extent<double, N> &
self, Extent<double, N>
const &other) -> Extent<double, N> & {
257 clsD.def(
"__iadd__", [](Extent<double, N> &
self, Extent<int, N>
const &other) -> Extent<double, N> & {
262 clsI.def(
"__isub__", [](Extent<int, N> &
self, Extent<int, N>
const &other) -> Extent<int, N> & {
266 clsD.def(
"__isub__", [](Extent<double, N> &
self, Extent<double, N>
const &other) -> Extent<double, N> & {
270 clsD.def(
"__isub__", [](Extent<double, N> &
self, Extent<int, N>
const &other) -> Extent<double, N> & {
275 clsI.def(
"__imul__", [](Extent<int, N> &
self,
int other) -> Extent<int, N> & {
279 clsD.def(
"__imul__", [](Extent<double, N> &
self,
int other) -> Extent<double, N> & {
283 clsD.def(
"__imul__", [](Extent<double, N> &
self,
double other) -> Extent<double, N> & {
289 mod.def(
"truncate", truncate<N>);
290 mod.def(
"floor", floor<N>);
291 mod.def(
"ceil", ceil<N>);
293 clsD.def(
"truncate", truncate<N>);
294 clsD.def(
"floor", floor<N>);
295 clsD.def(
"ceil", ceil<N>);
298 template <
typename T,
int N>
300 declareCoordinateBase<Point<T, N>, T, N>(mod,
"Point" + suffix);
301 static std::string const name =
"PointBase" + suffix;
302 PyPointBase<T, N>
cls(mod, name.c_str());
307 cls.def(
"eq", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.eq(other); });
308 cls.def(
"ne", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.ne(other); });
309 cls.def(
"lt", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.lt(other); });
310 cls.def(
"le", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.le(other); });
311 cls.def(
"gt", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.gt(other); });
312 cls.def(
"ge", [](PointBase<T, N>
const &
self, Point<T, N>
const &other) {
return self.ge(other); });
313 cls.def(
"eq", [](PointBase<T, N>
const &
self, T other) {
return self.eq(other); });
314 cls.def(
"ne", [](PointBase<T, N>
const &
self, T other) {
return self.ne(other); });
315 cls.def(
"lt", [](PointBase<T, N>
const &
self, T other) {
return self.lt(other); });
316 cls.def(
"le", [](PointBase<T, N>
const &
self, T other) {
return self.le(other); });
317 cls.def(
"gt", [](PointBase<T, N>
const &
self, T other) {
return self.gt(other); });
318 cls.def(
"ge", [](PointBase<T, N>
const &
self, T other) {
return self.ge(other); });
321 cls.def(
"asExtent", &PointBase<T, N>::asExtent);
322 cls.def(
"shift", &PointBase<T, N>::shift);
324 cls.def(
"distanceSquared", &PointBase<T, N>::distanceSquared);
325 cls.def(
"toString", &PointBase<T, N>::toString);
329 template <
typename T,
int N>
331 declarePointBase<T, N>(mod, suffix);
333 PyPoint<T, N>
cls(mod, name.c_str());
336 cls.def(py::init<T>(),
"value"_a = static_cast<T>(0));
341 cls.def(
py::init<
typename Point<T, N>::EigenVector>());
344 cls.def(
"__add__", [](Point<T, N>
const &
self, Extent<double, N> &other) {
return self +
other; },
346 cls.def(
"__add__", [](Point<T, N>
const &
self, Extent<int, N> &other) {
return self +
other; },
348 cls.def(
"__sub__", [](Point<T, N>
const &
self, Point<T, N> &other) {
return self -
other; },
350 cls.def(
"__sub__", [](Point<T, N>
const &
self, Extent<T, N> &other) {
return self -
other; },
352 cls.def(
"__sub__", [](Point<T, N>
const &
self, Point<double, N> &other) {
return self -
other; },
354 cls.def(
"__sub__", [](Point<T, N>
const &
self, Point<int, N> &other) {
return self -
other; },
356 cls.def(
"__sub__", [](Point<T, N>
const &
self, Extent<double, N> &other) {
return self -
other; },
358 cls.def(
"__sub__", [](Point<T, N>
const &
self, Extent<int, N> &other) {
return self -
other; },
360 cls.def(
"__eq__", [](Point<T, N>
const &
self, Point<T, N>
const &other) {
return self ==
other; },
362 cls.def(
"__ne__", [](Point<T, N>
const &
self, Point<T, N>
const &other) {
return self !=
other; },
366 cls.def(
"clone", [](Point<T, N>
const &
self) {
return Point<T, N>{
self}; });
372 template <
typename T>
377 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 2; });
380 cls.def(py::init<int, int>(),
"x"_a,
"y"_a);
381 cls.def(py::init<double, double>(),
"x"_a,
"y"_a);
384 cls.def(
"getX", [](Point<T, 2>
const &
self) {
return self[0]; });
385 cls.def(
"getY", [](Point<T, 2>
const &
self) {
return self[1]; });
386 cls.def(
"setX", [](Point<T, 2> &
self, T other) {
self[0] =
other; });
387 cls.def(
"setY", [](Point<T, 2> &
self, T other) {
self[1] =
other; });
392 template <
typename T>
397 cls.def_property_readonly_static(
"dimensions", [](py::object ) {
return 3; });
400 cls.def(py::init<int, int, int>(),
"x"_a,
"y"_a,
"z"_a);
401 cls.def(py::init<double, double, double>(),
"x"_a,
"y"_a,
"z"_a);
404 cls.def(
"getX", [](Point<T, 3>
const &
self) {
return self[0]; });
405 cls.def(
"getY", [](Point<T, 3>
const &
self) {
return self[1]; });
406 cls.def(
"getZ", [](Point<T, 3>
const &
self) {
return self[2]; });
407 cls.def(
"setX", [](Point<T, 3> &
self, T other) {
self[0] =
other; });
408 cls.def(
"setY", [](Point<T, 3> &
self, T other) {
self[1] =
other; });
409 cls.def(
"setZ", [](Point<T, 3> &
self, T other) {
self[2] =
other; });
420 void declarePointOperators(
py::module &mod, PyPoint<int, N> &clsI, PyPoint<double, N> &clsD) {
421 clsI.def(
"__iadd__", [](Point<int, N> &
self, Extent<int, N>
const &other) {
425 clsD.def(
"__iadd__", [](Point<double, N> &
self, Extent<int, N>
const &other) {
429 clsD.def(
"__iadd__", [](Point<double, N> &
self, Extent<double, N>
const &other) {
433 clsI.def(
"__isub__", [](Point<int, N> &
self, Extent<int, N>
const &other) {
437 clsD.def(
"__isub__", [](Point<double, N> &
self, Extent<int, N>
const &other) {
441 clsD.def(
"__isub__", [](Point<double, N> &
self, Extent<double, N>
const &other) {
451 declareCoordinateExpr<2>(mod,
"2");
452 declareCoordinateExpr<3>(mod,
"3");
454 auto clsExtent2I = declareExtent2<int>(mod,
"I");
455 auto clsExtent2D = declareExtent2<double>(mod,
"D");
456 declareExtentOperators(mod, clsExtent2I, clsExtent2D);
458 auto clsExtent3I = declareExtent3<int>(mod,
"I");
459 auto clsExtent3D = declareExtent3<double>(mod,
"D");
460 declareExtentOperators(mod, clsExtent3I, clsExtent3D);
462 auto clsPoint2I = declarePoint2<int>(mod,
"I");
463 auto clsPoint2D = declarePoint2<double>(mod,
"D");
464 declarePointOperators(mod, clsPoint2I, clsPoint2D);
466 auto clsPoint3I = declarePoint3<int>(mod,
"I");
467 auto clsPoint3D = declarePoint3<double>(mod,
"D");
468 declarePointOperators(mod, clsPoint3I, clsPoint3D);
std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i)
Compute a C++ index from a Python index (negative values count from the end) and range-check.
def scale(algorithm, min, max=None, frame=None)
A base class for image defects.
PYBIND11_MODULE(cameraSys, mod)
Extent< int, N > floor(Extent< double, N > const &input) noexcept
Return the component-wise floor (round towards more negative).
ItemVariant const * other