ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ccGLMatrix.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <pybind11/numpy.h>
9 #include <pybind11/pybind11.h>
10 #include <pybind11/stl.h>
11 #include <pybind11/stl_bind.h>
12 
13 #include <ecvGLMatrix.h>
14 
15 #include "wrappers.h"
16 
17 namespace py = pybind11;
18 using namespace pybind11::literals;
19 
20 template <typename ccGLMatrixType, typename Inner>
21 static void define_ccGLMatrixClass(py::module &m, const char *name)
22 {
23  py::class_<ccGLMatrixType>(m, name)
24  .def(py::init<>())
25  .def(py::init<const Vector3Tpl<Inner> &,
26  const Vector3Tpl<Inner> &,
27  const Vector3Tpl<Inner> &,
28  const Vector3Tpl<Inner> &>(),
29  "X"_a,
30  "Y"_a,
31  "Z"_a,
32  "Tr"_a)
33  .def_static("Interpolate", &ccGLMatrixType::Interpolate, "coef"_a, "glMat1"_a, "glMat2"_a)
34  // For some unknown reason doing
35  // .def_static("FromToRotation", &ccGLMatrixType::FromViewDirAndUpDir, "forward"_a, "up"_a)
36  // creates a function that will create a runtime error when used:
37  // ```
38  // TypeError: Unable to convert function return value to a Python type! The signature was
39  // (forward: cccorelib.CCVector3d, up: cccorelib.CCVector3d) -> ccGLMatrixTpl<double>
40  // ```
41  // We have to use a fully typed lambda for it to work.
42  // We also provide this function as a __init__ alias
43  .def_static(
44  "FromViewDirAndUpDir",
45  [](const Vector3Tpl<Inner> &a, const Vector3Tpl<Inner> &b) -> ccGLMatrixType
46  { return ccGLMatrixType::FromViewDirAndUpDir(a, b); },
47  "forward"_a,
48  "up"_a)
49  .def(py::init(&ccGLMatrixType::FromViewDirAndUpDir), py::kw_only(), "forward"_a, "up"_a)
50  // Same problem and solution as above
51  .def_static(
52  "FromToRotation",
53  [](const Vector3Tpl<Inner> &a, const Vector3Tpl<Inner> &b) -> ccGLMatrixType
54  { return ccGLMatrixType::FromToRotation(a, b); },
55  "from"_a,
56  "to"_a)
57  .def(py::init(&ccGLMatrixType::FromToRotation), py::kw_only(), "from"_a, "to"_a)
58  .def("xRotation", &ccGLMatrixType::xRotation)
59  .def("yRotation", &ccGLMatrixType::yRotation)
60  .def("zRotation", &ccGLMatrixType::zRotation)
61  .def("toZero", &ccGLMatrixType::toZero)
62  .def("toIdentity", &ccGLMatrixType::toIdentity)
63  .def("clearTranslation", &ccGLMatrixType::clearTranslation)
64  .def("getTranslationAsVec3D", &ccGLMatrixType::getTranslationAsVec3D)
65  .def("initFromParameters",
66  static_cast<void (ccGLMatrixType::*)(
67  Inner, const Vector3Tpl<Inner> &, const Vector3Tpl<Inner> &)>(
68  &ccGLMatrixType::initFromParameters),
69  "alpha_rad"_a,
70  "axis3D"_a,
71  "t3D"_a)
72  .def("initFromParameters",
73  static_cast<void (ccGLMatrixType::*)(Inner, Inner, Inner, const Vector3Tpl<Inner> &)>(
74  &ccGLMatrixType::initFromParameters),
75  "phi_rad"_a,
76  "theta_rad"_a,
77  "psi_rad"_a,
78  "t3D"_a)
79  // getParameters, but there are 2 overloads which return their values
80  .def("setTranslation",
81  static_cast<void (ccGLMatrixType::*)(const Vector3Tpl<Inner> &)>(
82  &ccGLMatrixType::setTranslation),
83  "Tr"_a)
84  .def("getColumnAsVec3D", &ccGLMatrixType::getColumnAsVec3D, "index"_a)
85  .def("setColumn",
86  static_cast<void (ccGLMatrixType::*)(unsigned index, const Vector3Tpl<Inner> &)>(
87  &ccGLMatrixType::setColumn),
88  "index"_a,
89  "v"_a)
90  // .def("setColumn",
91  // static_cast<void (ccGLMatrixType::*)(unsigned index, const Tuple4Tpl<Inner>
92  // &)>(
93  // &ccGLMatrixType::setColumn),
94  // "index"_a,
95  // "v"_a)
96  // .def(py::self * py::self)
97  .def(py::self * Vector3Tpl<Inner>())
98  .def(py::self += py::self)
99  .def(py::self -= py::self)
100  .def(py::self *= py::self)
101  .def(py::self += Vector3Tpl<Inner>())
102  .def(py::self -= Vector3Tpl<Inner>())
103  .def("__mul__",
104  [](ccGLMatrixType &self, ccGLMatrixType &other) -> ccGLMatrixType
105  { return self * other; })
106  .def("__getitem__",
107  [](const ccGLMatrixType &self, const std::pair<unsigned, unsigned> &item)
108  { return self(item.first, item.second); })
109  // apply and so on
110  .def("shiftRotationCenter", &ccGLMatrixType::shiftRotationCenter, "vec"_a)
111  .def("transpose", &ccGLMatrixType::transpose)
112  .def("invert", &ccGLMatrixType::invert)
113  .def("inverse", &ccGLMatrixType::inverse)
114  .def("scaleRotation", &ccGLMatrixType::scaleRotation, "coef"_a)
115  .def("scaleRow", &ccGLMatrixType::scaleRow, "rowIndex"_a, "coef"_a)
116  .def("scaleColumn", &ccGLMatrixType::scaleColumn, "rowIndex"_a, "coef"_a)
117  .def("asArray",
118  [](ccGLMatrixType &self) { return PyCC::SpanAsNumpyArray(self.data(), {4, 4}); });
119 }
120 
121 void define_ccGLMatrix(py::module &m)
122 {
123  define_ccGLMatrixClass<ccGLMatrix, float>(m, "ccGLMatrix");
124  define_ccGLMatrixClass<ccGLMatrixd, double>(m, "ccGLMatrixd");
125 }
std::string name
static void define_ccGLMatrixClass(py::module &m, const char *name)
Definition: ccGLMatrix.cpp:21
void define_ccGLMatrix(py::module &m)
Definition: ccGLMatrix.cpp:121
3D Vector (templated version)
Definition: CVGeom.h:223
py::array_t< T > SpanAsNumpyArray(T *data, py::array::ShapeContainer shape)
Definition: wrappers.h:74