| 1 |
#include "colmap/geometry/rigid3.h" |
| 2 |
|
| 3 |
#include "colmap/geometry/pose.h" |
| 4 |
#include "colmap/util/logging.h" |
| 5 |
|
| 6 |
#include "pycolmap/helpers.h" |
| 7 |
#include "pycolmap/pybind11_extension.h" |
| 8 |
|
| 9 |
#include <pybind11/eigen.h> |
| 10 |
#include <pybind11/pybind11.h> |
| 11 |
#include <pybind11/stl.h> |
| 12 |
|
| 13 |
using namespace colmap; |
| 14 |
using namespace pybind11::literals; |
| 15 |
namespace py = pybind11; |
| 16 |
|
| 17 |
void BindRigid3(py::module& m) { |
| 18 |
using Rotation3dWrapper = pycolmap::Rotation3dWrapper; |
| 19 |
py::classh_ext<Rigid3d> PyRigid3d(m, "Rigid3d"); |
| 20 |
PyRigid3d.def(py::init<>()) |
| 21 |
.def(py::init<const Eigen::Quaterniond&, const Eigen::Vector3d&>(), |
| 22 |
"rotation"_a, |
| 23 |
"translation"_a) |
| 24 |
.def(py::init(&Rigid3d::FromMatrix), |
| 25 |
"matrix"_a, |
| 26 |
"3x4 transformation matrix.") |
| 27 |
.def_property( |
| 28 |
"params", |
| 29 |
[](Rigid3d& self) -> Eigen::Vector7d& { return self.params; }, |
| 30 |
[](Rigid3d& self, const Eigen::Vector7d& params) { |
| 31 |
self.params = params; |
| 32 |
}) |
| 33 |
.def_property( |
| 34 |
"rotation", |
| 35 |
[](py::object self) { |
| 36 |
Rigid3d& rigid = self.cast<Rigid3d&>(); |
| 37 |
py::array_t<double> arr( |
| 38 |
{4}, {sizeof(double)}, rigid.params.data(), self); |
| 39 |
return Rotation3dWrapper(std::move(arr)); |
| 40 |
}, |
| 41 |
[](Rigid3d& self, const Eigen::Quaterniond& q) { |
| 42 |
self.rotation() = q; |
| 43 |
}) |
| 44 |
.def_property( |
| 45 |
"translation", |
| 46 |
[](py::object self) { |
| 47 |
Rigid3d& rigid = self.cast<Rigid3d&>(); |
| 48 |
return py::array_t<double>( |
| 49 |
{3}, {sizeof(double)}, rigid.params.data() + 4, self); |
| 50 |
}, |
| 51 |
[](Rigid3d& self, const Eigen::Vector3d& t) { |
| 52 |
self.translation() = t; |
| 53 |
}) |
| 54 |
.def("matrix", &Rigid3d::ToMatrix) |
| 55 |
.def("tgt_origin_in_src", &Rigid3d::TgtOriginInSrc) |
| 56 |
.def("adjoint", &Rigid3d::Adjoint) |
| 57 |
.def("adjoint_inverse", &Rigid3d::AdjointInverse) |
| 58 |
.def(py::self * Rigid3d()) |
| 59 |
.def(py::self * Eigen::Vector3d()) |
| 60 |
.def("__mul__", |
| 61 |
[](const Rigid3d& t, |
| 62 |
const py::EigenDRef<const Eigen::MatrixX3d>& points) |
| 63 |
-> Eigen::MatrixX3d { |
| 64 |
return (points * t.rotation().toRotationMatrix().transpose()) |
| 65 |
.rowwise() + |
| 66 |
t.translation().transpose(); |
| 67 |
}) |
| 68 |
.def("inverse", static_cast<Rigid3d (*)(const Rigid3d&)>(&Inverse)) |
| 69 |
.def_static("interpolate", |
| 70 |
&InterpolateCameraPoses, |
| 71 |
"cam_from_world1"_a, |
| 72 |
"cam_from_world2"_a, |
| 73 |
"t"_a); |
| 74 |
py::implicitly_convertible<py::array, Rigid3d>(); |
| 75 |
MakeDataclass(PyRigid3d); |
| 76 |
|
| 77 |
m.def("get_covariance_for_inverse", |
| 78 |
&GetCovarianceForRigid3dInverse, |
| 79 |
"rigid3d"_a, |
| 80 |
"covar"_a); |
| 81 |
m.def("get_covariance_for_composed_rigid3d", |
| 82 |
&GetCovarianceForComposedRigid3d, |
| 83 |
"left_rigid3d"_a, |
| 84 |
"joint_covar"_a); |
| 85 |
m.def("get_covariance_for_relative_rigid3d", |
| 86 |
&GetCovarianceForRelativeRigid3d, |
| 87 |
"base_rigid3d"_a, |
| 88 |
"target_rigid3d"_a, |
| 89 |
"joint_covar"_a); |
| 90 |
|
| 91 |
m.def("average_quaternions", &AverageQuaternions, "quats"_a, "weights"_a); |
| 92 |
m.def("interpolate_camera_poses", |
| 93 |
&InterpolateCameraPoses, |
| 94 |
"cam1_from_world"_a, |
| 95 |
"cam2_from_world"_a, |
| 96 |
"t"_a); |
| 97 |
} |