Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform many points #250

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions pycolmap/geometry/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,27 @@ void BindGeometry(py::module& m) {

py::class_<Eigen::Quaterniond> PyRotation3d(m, "Rotation3d");
PyRotation3d.def(py::init([]() { return Eigen::Quaterniond::Identity(); }))
.def(py::init<const Eigen::Vector4d&>(), "xyzw"_a)
.def(py::init<const Eigen::Matrix3d&>(), "rotmat"_a)
.def(py::init<const Eigen::Vector4d&>(),
"xyzw"_a,
"Quaternion in [x,y,z,w] format.")
.def(py::init<const Eigen::Matrix3d&>(),
"rotmat"_a,
"3x3 rotation matrix.")
.def_property(
"quat",
py::overload_cast<>(&Eigen::Quaterniond::coeffs),
[](Eigen::Quaterniond& self, const Eigen::Vector4d& quat) {
self.coeffs() = quat;
},
"Quaternion in [x,y,z,w] format.")
.def(py::self * Eigen::Quaterniond())
.def(py::self * Eigen::Vector3d())
.def_property("quat",
py::overload_cast<>(&Eigen::Quaterniond::coeffs),
[](Eigen::Quaterniond& self, const Eigen::Vector4d& quat) {
self.coeffs() = quat;
})
.def("__mul__",
[](const Eigen::Quaterniond& self,
const py::EigenDRef<const Eigen::MatrixX3d>& points)
-> Eigen::MatrixX3d {
return points * self.toRotationMatrix().transpose();
})
.def("normalize", &Eigen::Quaterniond::normalize)
.def("matrix", &Eigen::Quaterniond::toRotationMatrix)
.def("norm", &Eigen::Quaterniond::norm)
Expand All @@ -53,8 +65,16 @@ void BindGeometry(py::module& m) {
.def_readwrite("translation", &Rigid3d::translation)
.def("matrix", &Rigid3d::ToMatrix)
.def("essential_matrix", &EssentialMatrixFromPose)
.def(py::self * Eigen::Vector3d())
.def(py::self * Rigid3d())
.def(py::self * Eigen::Vector3d())
.def("__mul__",
[](const Rigid3d& t,
const py::EigenDRef<const Eigen::MatrixX3d>& points)
-> Eigen::MatrixX3d {
return (points * t.rotation.toRotationMatrix().transpose())
.rowwise() +
t.translation.transpose();
})
.def("inverse", static_cast<Rigid3d (*)(const Rigid3d&)>(&Inverse))
.def_static("interpolate", &InterpolateCameraPoses)
.def("__repr__", [](const Rigid3d& self) {
Expand All @@ -76,8 +96,17 @@ void BindGeometry(py::module& m) {
.def_readwrite("rotation", &Sim3d::rotation)
.def_readwrite("translation", &Sim3d::translation)
.def("matrix", &Sim3d::ToMatrix)
.def(py::self * Eigen::Vector3d())
.def(py::self * Sim3d())
.def(py::self * Eigen::Vector3d())
.def("__mul__",
[](const Sim3d& t,
const py::EigenDRef<const Eigen::MatrixX3d>& points)
-> Eigen::MatrixX3d {
return (t.scale *
(points * t.rotation.toRotationMatrix().transpose()))
.rowwise() +
t.translation.transpose();
})
.def("transform_camera_world", &TransformCameraWorld)
.def("inverse", static_cast<Sim3d (*)(const Sim3d&)>(&Inverse))
.def("__repr__", [](const Sim3d& self) {
Expand Down
48 changes: 0 additions & 48 deletions pycolmap/scene/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,54 +241,6 @@ void BindImage(py::module& m) {

return valid_points2D;
})
.def(
"project",
[](const Image& self,
const std::vector<Eigen::Vector3d>& world_coords) {
std::vector<Eigen::Vector2d> image_points(world_coords.size());
for (int idx = 0; idx < world_coords.size(); ++idx) {
image_points[idx] =
(self.CamFromWorld() * world_coords[idx]).hnormalized();
}
return image_points;
},
"Project list of world points to image (xy).")
.def(
"project",
[](const Image& self, const std::vector<Point3D>& point3Ds) {
std::vector<Eigen::Vector2d> world_points(point3Ds.size());
for (int idx = 0; idx < point3Ds.size(); ++idx) {
world_points[idx] =
(self.CamFromWorld() * point3Ds[idx].xyz).hnormalized();
}
return world_points;
},
"Project list of point3Ds to image (xy).")
.def(
"transform_to_image",
[](const Image& self,
const std::vector<Eigen::Vector3d>& world_coords) {
std::vector<Eigen::Vector3d> image_points(world_coords.size());
for (int idx = 0; idx < world_coords.size(); ++idx) {
image_points[idx] = (self.CamFromWorld() * world_coords[idx]);
}
return image_points;
},
"Project list of points in world coordinate frame to image "
"coordinates (xyz).")
.def(
"transform_to_world",
[](const Image& self,
const std::vector<Eigen::Vector3d>& image_coords) {
std::vector<Eigen::Vector3d> world_points(image_coords.size());
for (int idx = 0; idx < image_coords.size(); ++idx) {
world_points[idx] =
(Inverse(self.CamFromWorld()) * image_coords[idx]);
}
return world_points;
},
"Project list of image points (with depth) to world coordinate "
"frame.")
.def("__repr__", &PrintImage);
MakeDataclass(PyImage);
}
Loading