| 1 |
#include "colmap/util/logging.h" |
| 2 |
|
| 3 |
#include "pycolmap/helpers.h" |
| 4 |
#include "pycolmap/pybind11_extension.h" |
| 5 |
|
| 6 |
#include <Eigen/Geometry> |
| 7 |
#include <pybind11/eigen.h> |
| 8 |
#include <pybind11/pybind11.h> |
| 9 |
#include <pybind11/stl.h> |
| 10 |
|
| 11 |
using namespace colmap; |
| 12 |
using namespace pybind11::literals; |
| 13 |
namespace py = pybind11; |
| 14 |
|
| 15 |
void BindEigenGeometry(py::module& m) { |
| 16 |
using Rotation3dWrapper = pycolmap::Rotation3dWrapper; |
| 17 |
py::classh_ext<Rotation3dWrapper> PyRotation3d(m, "Rotation3d"); |
| 18 |
PyRotation3d.def(py::init<>()) |
| 19 |
.def(py::init<const Eigen::Vector4d&>(), |
| 20 |
"xyzw"_a, |
| 21 |
"Quaternion in [x,y,z,w] format.") |
| 22 |
.def(py::init<const Eigen::Matrix3d&>(), |
| 23 |
"matrix"_a, |
| 24 |
"3x3 rotation matrix.") |
| 25 |
.def(py::init<const Eigen::Vector3d&>(), |
| 26 |
"axis_angle"_a, |
| 27 |
"Axis-angle 3D vector.") |
| 28 |
.def_static( |
| 29 |
"from_buffer", |
| 30 |
[](py::array_t<double> arr) { |
| 31 |
THROW_CHECK_EQ(arr.size(), 4); |
| 32 |
return Rotation3dWrapper(std::move(arr)); |
| 33 |
}, |
| 34 |
"array"_a, |
| 35 |
"Create from numpy array view (zero-copy if contiguous).") |
| 36 |
.def_property( |
| 37 |
"quat", |
| 38 |
[](Rotation3dWrapper& self) { return self.data; }, |
| 39 |
[](Rotation3dWrapper& self, const Eigen::Vector4d& quat) { |
| 40 |
self.map().coeffs() = quat; |
| 41 |
}, |
| 42 |
"Quaternion in [x,y,z,w] format.") |
| 43 |
.def("__mul__", |
| 44 |
[](const Rotation3dWrapper& self, const Rotation3dWrapper& other) { |
| 45 |
return Rotation3dWrapper(self.map() * other.map()); |
| 46 |
}) |
| 47 |
.def("__mul__", |
| 48 |
[](const Rotation3dWrapper& self, const Eigen::Vector3d& v) { |
| 49 |
return Eigen::Vector3d(self.map() * v); |
| 50 |
}) |
| 51 |
.def("__mul__", |
| 52 |
[](const Rotation3dWrapper& self, |
| 53 |
const py::EigenDRef<const Eigen::MatrixX3d>& points) |
| 54 |
-> Eigen::MatrixX3d { |
| 55 |
return points * self.map().toRotationMatrix().transpose(); |
| 56 |
}) |
| 57 |
.def("normalize", [](Rotation3dWrapper& self) { self.map().normalize(); }) |
| 58 |
.def("matrix", |
| 59 |
[](const Rotation3dWrapper& self) { |
| 60 |
return self.map().toRotationMatrix(); |
| 61 |
}) |
| 62 |
.def("norm", |
| 63 |
[](const Rotation3dWrapper& self) { return self.map().norm(); }) |
| 64 |
.def("angle", |
| 65 |
[](const Rotation3dWrapper& self) { |
| 66 |
return Eigen::AngleAxis<double>(self.map()).angle(); |
| 67 |
}) |
| 68 |
.def( |
| 69 |
"angle_to", |
| 70 |
[](const Rotation3dWrapper& self, const Rotation3dWrapper& other) { |
| 71 |
return self.map().angularDistance(other.map()); |
| 72 |
}, |
| 73 |
"other"_a) |
| 74 |
.def("inverse", |
| 75 |
[](const Rotation3dWrapper& self) { |
| 76 |
return Rotation3dWrapper(self.map().inverse()); |
| 77 |
}) |
| 78 |
.def("__repr__", [](const Rotation3dWrapper& self) { |
| 79 |
std::ostringstream ss; |
| 80 |
ss << "Rotation3d(xyzw=[" << self.map().coeffs().format(vec_fmt) |
| 81 |
<< "])"; |
| 82 |
return ss.str(); |
| 83 |
}); |
| 84 |
py::implicitly_convertible<py::array, Rotation3dWrapper>(); |
| 85 |
// Define deepcopy before MakeDataclass to ensure proper deep copy of array |
| 86 |
// data |
| 87 |
PyRotation3d.def("__deepcopy__", |
| 88 |
[](const Rotation3dWrapper& self, const py::dict&) { |
| 89 |
Rotation3dWrapper copy; |
| 90 |
copy.map() = self.map(); |
| 91 |
return copy; |
| 92 |
}); |
| 93 |
MakeDataclass(PyRotation3d); |
| 94 |
|
| 95 |
py::classh_ext<Eigen::AlignedBox3d> PyAlignedBox3d(m, "AlignedBox3d"); |
| 96 |
PyAlignedBox3d.def(py::init<>()) |
| 97 |
.def(py::init<const Eigen::Vector3d&, const Eigen::Vector3d&>(), |
| 98 |
"min"_a, |
| 99 |
"max"_a) |
| 100 |
.def_property("min", |
| 101 |
py::overload_cast<>(&Eigen::AlignedBox3d::min), |
| 102 |
[](Eigen::AlignedBox3d& self, const Eigen::Vector3d& min) { |
| 103 |
self.min() = min; |
| 104 |
}) |
| 105 |
.def_property("max", |
| 106 |
py::overload_cast<>(&Eigen::AlignedBox3d::max), |
| 107 |
[](Eigen::AlignedBox3d& self, const Eigen::Vector3d& max) { |
| 108 |
self.max() = max; |
| 109 |
}) |
| 110 |
.def("diagonal", &Eigen::AlignedBox3d::diagonal) |
| 111 |
.def( |
| 112 |
"contains_point", |
| 113 |
[](const Eigen::AlignedBox3d& self, const Eigen::Vector3d& point) { |
| 114 |
return self.contains(point); |
| 115 |
}, |
| 116 |
"point"_a) |
| 117 |
.def( |
| 118 |
"contains_bbox", |
| 119 |
[](const Eigen::AlignedBox3d& self, |
| 120 |
const Eigen::AlignedBox3d& other) { return self.contains(other); }, |
| 121 |
"other"_a) |
| 122 |
.def("__repr__", [](const Eigen::AlignedBox3d& self) { |
| 123 |
std::ostringstream ss; |
| 124 |
ss << "AlignedBox3d(min=[" << self.min().format(vec_fmt) << "], max=[" |
| 125 |
<< self.max().format(vec_fmt) << "])"; |
| 126 |
return ss.str(); |
| 127 |
}); |
| 128 |
MakeDataclass(PyAlignedBox3d); |
| 129 |
} |