| 1 |
#include "colmap/estimators/solvers/similarity_transform.h" |
| 2 |
|
| 3 |
#include "colmap/math/random.h" |
| 4 |
#include "colmap/optim/loransac.h" |
| 5 |
#include "colmap/util/logging.h" |
| 6 |
|
| 7 |
#include "pycolmap/pybind11_extension.h" |
| 8 |
#include "pycolmap/utils.h" |
| 9 |
|
| 10 |
#include <pybind11/eigen.h> |
| 11 |
#include <pybind11/pybind11.h> |
| 12 |
#include <pybind11/stl.h> |
| 13 |
|
| 14 |
using namespace colmap; |
| 15 |
using namespace pybind11::literals; |
| 16 |
namespace py = pybind11; |
| 17 |
|
| 18 |
void BindSimilarityTransformEstimator(py::module& m) { |
| 19 |
auto ransac_options = m.attr("RANSACOptions")().cast<RANSACOptions>(); |
| 20 |
|
| 21 |
m.def( |
| 22 |
"estimate_rigid3d", |
| 23 |
[](const std::vector<Eigen::Vector3d>& src, |
| 24 |
const std::vector<Eigen::Vector3d>& tgt) |
| 25 |
-> py::typing::Optional<Rigid3d> { |
| 26 |
py::gil_scoped_release release; |
| 27 |
Rigid3d tgt_from_src; |
| 28 |
const bool success = EstimateRigid3d(src, tgt, tgt_from_src); |
| 29 |
py::gil_scoped_acquire acquire; |
| 30 |
if (success) { |
| 31 |
return py::cast(tgt_from_src); |
| 32 |
} else { |
| 33 |
return py::none(); |
| 34 |
} |
| 35 |
}, |
| 36 |
"src"_a, |
| 37 |
"tgt"_a, |
| 38 |
"Estimate the 3D rigid transform tgt_from_src."); |
| 39 |
|
| 40 |
m.def( |
| 41 |
"estimate_rigid3d_robust", |
| 42 |
[](const std::vector<Eigen::Vector3d>& src, |
| 43 |
const std::vector<Eigen::Vector3d>& tgt, |
| 44 |
const RANSACOptions& options) -> py::typing::Optional<Rigid3d> { |
| 45 |
py::gil_scoped_release release; |
| 46 |
Rigid3d tgt_from_src; |
| 47 |
const auto report = |
| 48 |
EstimateRigid3dRobust(src, tgt, options, tgt_from_src); |
| 49 |
py::gil_scoped_acquire acquire; |
| 50 |
if (!report.success) { |
| 51 |
return py::none(); |
| 52 |
} |
| 53 |
return py::dict("tgt_from_src"_a = Rigid3d::FromMatrix(report.model), |
| 54 |
"num_inliers"_a = report.support.num_inliers, |
| 55 |
"inlier_mask"_a = ToPythonMask(report.inlier_mask)); |
| 56 |
}, |
| 57 |
"src"_a, |
| 58 |
"tgt"_a, |
| 59 |
py::arg_v("estimation_options", ransac_options, "RANSACOptions()"), |
| 60 |
"Robustly estimate the 3D rigid transform tgt_from_src using LO-RANSAC."); |
| 61 |
|
| 62 |
m.def( |
| 63 |
"estimate_sim3d", |
| 64 |
[](const std::vector<Eigen::Vector3d>& src, |
| 65 |
const std::vector<Eigen::Vector3d>& tgt) |
| 66 |
-> py::typing::Optional<Sim3d> { |
| 67 |
py::gil_scoped_release release; |
| 68 |
Sim3d tgt_from_src; |
| 69 |
const bool success = EstimateSim3d(src, tgt, tgt_from_src); |
| 70 |
py::gil_scoped_acquire acquire; |
| 71 |
if (success) { |
| 72 |
return py::cast(tgt_from_src); |
| 73 |
} else { |
| 74 |
return py::none(); |
| 75 |
} |
| 76 |
}, |
| 77 |
"src"_a, |
| 78 |
"tgt"_a, |
| 79 |
"Estimate the 3D similarity transform tgt_from_src."); |
| 80 |
|
| 81 |
m.def( |
| 82 |
"estimate_sim3d_robust", |
| 83 |
[](const std::vector<Eigen::Vector3d>& src, |
| 84 |
const std::vector<Eigen::Vector3d>& tgt, |
| 85 |
const RANSACOptions& options) -> py::typing::Optional<Sim3d> { |
| 86 |
py::gil_scoped_release release; |
| 87 |
Sim3d tgt_from_src; |
| 88 |
const auto report = |
| 89 |
EstimateSim3dRobust(src, tgt, options, tgt_from_src); |
| 90 |
py::gil_scoped_acquire acquire; |
| 91 |
if (!report.success) { |
| 92 |
return py::none(); |
| 93 |
} |
| 94 |
return py::dict("tgt_from_src"_a = Sim3d::FromMatrix(report.model), |
| 95 |
"num_inliers"_a = report.support.num_inliers, |
| 96 |
"inlier_mask"_a = ToPythonMask(report.inlier_mask)); |
| 97 |
}, |
| 98 |
"src"_a, |
| 99 |
"tgt"_a, |
| 100 |
py::arg_v("estimation_options", ransac_options, "RANSACOptions()"), |
| 101 |
"Robustly estimate the 3D similarity transform tgt_from_src using " |
| 102 |
"LO-RANSAC."); |
| 103 |
} |