| 1 |
#include "colmap/estimators/triangulation.h" |
| 2 |
|
| 3 |
#include "colmap/scene/camera.h" |
| 4 |
#include "colmap/scene/image.h" |
| 5 |
#include "colmap/util/logging.h" |
| 6 |
|
| 7 |
#include "pycolmap/helpers.h" |
| 8 |
#include "pycolmap/pybind11_extension.h" |
| 9 |
#include "pycolmap/utils.h" |
| 10 |
|
| 11 |
#include <pybind11/eigen.h> |
| 12 |
#include <pybind11/pybind11.h> |
| 13 |
#include <pybind11/pytypes.h> |
| 14 |
#include <pybind11/stl.h> |
| 15 |
|
| 16 |
using namespace colmap; |
| 17 |
using namespace pybind11::literals; |
| 18 |
namespace py = pybind11; |
| 19 |
|
| 20 |
py::typing::Optional<py::dict> PyEstimateTriangulation( |
| 21 |
const std::vector<Eigen::Vector2d>& points, |
| 22 |
const std::vector<Rigid3d>& cams_from_world, |
| 23 |
const std::vector<Camera const*>& cameras, |
| 24 |
const EstimateTriangulationOptions& options) { |
| 25 |
py::gil_scoped_release release; |
| 26 |
Eigen::Vector3d xyz; |
| 27 |
std::vector<char> inlier_mask; |
| 28 |
const bool success = EstimateTriangulation( |
| 29 |
options, points, cams_from_world, cameras, &inlier_mask, &xyz); |
| 30 |
|
| 31 |
py::gil_scoped_acquire acquire; |
| 32 |
if (success) { |
| 33 |
return py::dict("xyz"_a = xyz, "inliers"_a = ToPythonMask(inlier_mask)); |
| 34 |
} else { |
| 35 |
return py::none(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
void BindTriangulationEstimator(py::module& m) { |
| 40 |
auto PyRANSACOptions = m.attr("RANSACOptions"); |
| 41 |
|
| 42 |
using ResType = TriangulationEstimator::ResidualType; |
| 43 |
auto PyResType = |
| 44 |
py::enum_<ResType>(m, "TriangulationResidualType") |
| 45 |
.value("ANGULAR_ERROR", ResType::ANGULAR_ERROR) |
| 46 |
.value("REPROJECTION_ERROR", ResType::REPROJECTION_ERROR); |
| 47 |
AddStringToEnumConstructor(PyResType); |
| 48 |
|
| 49 |
using Options = EstimateTriangulationOptions; |
| 50 |
py::classh<Options> PyTriangulationOptions(m, "EstimateTriangulationOptions"); |
| 51 |
PyTriangulationOptions.def(py::init<>()) |
| 52 |
.def_readwrite("min_tri_angle", |
| 53 |
&Options::min_tri_angle, |
| 54 |
"Minimum triangulation angle in radians.") |
| 55 |
.def_readwrite( |
| 56 |
"residual_type", &Options::residual_type, "Employed residual type.") |
| 57 |
.def_readwrite("ransac", &Options::ransac_options, "RANSAC options."); |
| 58 |
MakeDataclass(PyTriangulationOptions); |
| 59 |
|
| 60 |
m.def("estimate_triangulation", |
| 61 |
&PyEstimateTriangulation, |
| 62 |
"points"_a, |
| 63 |
"cams_from_world"_a, |
| 64 |
"cameras"_a, |
| 65 |
py::arg_v("options", |
| 66 |
EstimateTriangulationOptions(), |
| 67 |
"EstimateTriangulationOptions()"), |
| 68 |
"Robustly estimate 3D point from observations in multiple views using " |
| 69 |
"LO-RANSAC"); |
| 70 |
} |