Debian Codegraph @

src/pycolmap/estimators/covariance.cc

raw
1 #include "colmap/estimators/covariance.h"
2
3 #include "pycolmap/helpers.h"
4 #include "pycolmap/pybind11_extension.h"
5
6 #include <pybind11/eigen.h>
7 #include <pybind11/pybind11.h>
8 #include <pybind11/stl.h>
9 #include <pybind11/stl_bind.h>
10
11 using namespace colmap;
12 using namespace pybind11::literals;
13 namespace py = pybind11;
14
15 void BindCovarianceEstimator(py::module& m) {
16 auto PyBACovarianceOptionsParams =
17 py::enum_<BACovarianceOptions::Params>(m, "BACovarianceOptionsParams")
18 .value("POSES", BACovarianceOptions::Params::POSES)
19 .value("POINTS", BACovarianceOptions::Params::POINTS)
20 .value("POSES_AND_POINTS",
21 BACovarianceOptions::Params::POSES_AND_POINTS)
22 .value("ALL", BACovarianceOptions::Params::ALL);
23 AddStringToEnumConstructor(PyBACovarianceOptionsParams);
24
25 py::classh<internal::PoseParam> PyExperimentalPoseParam(
26 m, "ExperimentalPoseParam");
27 PyExperimentalPoseParam.def(py::init<>())
28 .def_readwrite("image_id", &internal::PoseParam::image_id)
29 .def_property(
30 "cam_from_world",
31 [](internal::PoseParam& self)
32 -> py::typing::Optional<py::array_t<double>> {
33 if (!self.cam_from_world)
34 return py::none();
35 else
36 return py::array_t<double>(4, self.cam_from_world);
37 },
38 [](internal::PoseParam& self, py::array_t<double> pyarray) {
39 THROW_CHECK_EQ(pyarray.ndim(), 1);
40 THROW_CHECK_EQ(pyarray.size(), 4);
41 py::buffer_info info = pyarray.request();
42 self.cam_from_world = (double*)info.ptr;
43 });
44 MakeDataclass(PyExperimentalPoseParam);
45
46 py::classh<BACovarianceOptions> PyBACovarianceOptions(m,
47 "BACovarianceOptions");
48 PyBACovarianceOptions.def(py::init<>())
49 .def_readwrite("params",
50 &BACovarianceOptions::params,
51 "For which parameters to compute the covariance.")
52 .def_readwrite(
53 "damping",
54 &BACovarianceOptions::damping,
55 "Damping factor for the Hessian in the Schur complement solver. "
56 "Enables to robustly deal with poorly conditioned parameters.")
57 .def_readwrite(
58 "experimental_custom_poses",
59 &BACovarianceOptions::experimental_custom_poses,
60 "WARNING: This option will be removed in a future release, use at "
61 "your own risk. For custom bundle adjustment problems, this enables "
62 "to specify a custom set of pose parameter blocks to consider. Note "
63 "that these pose blocks must not necessarily be part of the "
64 "reconstruction but they must follow the standard requirement for "
65 "applying the Schur complement trick.");
66 MakeDataclass(PyBACovarianceOptions);
67
68 py::classh<BACovariance>(m, "BACovariance")
69 .def("get_point_cov",
70 &BACovariance::GetPointCov,
71 "point3D_id"_a,
72 "Covariance for 3D points, conditioned on all other variables set "
73 "constant. If some dimensions are kept constant, the respective "
74 "rows/columns are omitted. Returns null if 3D point not a variable "
75 "in the problem.")
76 .def("get_cam_cov_from_world",
77 &BACovariance::GetCamCovFromWorld,
78 "image_id"_a,
79 "Tangent space covariance in the order [rotation, translation]. If "
80 "some dimensions are kept constant, the respective rows/columns are "
81 "omitted. Returns null if image is not a variable in the problem.")
82 .def("get_cam_cross_cov_from_world",
83 &BACovariance::GetCamCrossCovFromWorld,
84 "image_id1"_a,
85 "image_id2"_a,
86 "Tangent space covariance in the order [rotation, translation]. If "
87 "some dimensions are kept constant, the respective rows/columns are "
88 "omitted. Returns null if image is not a variable in the problem.")
89 .def("get_cam2_cov_from_cam1",
90 &BACovariance::GetCam2CovFromCam1,
91 "image_id1"_a,
92 "cam1_from_world"_a,
93 "image_id2"_a,
94 "cam2_from_world"_a,
95 "Get relative pose covariance in the order [rotation, translation]. "
96 "This function returns null if some dimensions are kept constant "
97 "for either of the two poses. This does not mean that one cannot "
98 "get relative pose covariance for such case, but requires custom "
99 "logic to fill in zero block in the covariance matrix.")
100 .def(
101 "get_other_params_cov",
102 [](BACovariance& self, py::array_t<double>& pyarray) {
103 THROW_CHECK_EQ(pyarray.ndim(), 1);
104 py::buffer_info info = pyarray.request();
105 return self.GetOtherParamsCov((double*)info.ptr);
106 },
107 "param"_a,
108 "Tangent space covariance for any variable parameter block in the "
109 "problem. If some dimensions are kept constant, the respective "
110 "rows/columns are omitted. Returns null if parameter block not a "
111 "variable in the problem.");
112
113 m.def(
114 "estimate_ba_covariance",
115 &EstimateBACovariance,
116 "options"_a,
117 "reconstruction"_a,
118 "bundle_adjuster"_a,
119 "Computes covariances for the parameters in a bundle adjustment "
120 "problem. It is important that the problem has a structure suitable for "
121 "solving using the Schur complement trick. This is the case for the "
122 "standard configuration of bundle adjustment problems, but be careful "
123 "if you modify the underlying problem with custom residuals. Returns "
124 "null if the estimation was not successful.");
125
126 m.def(
127 "estimate_ba_covariance_from_problem",
128 &EstimateBACovarianceFromProblem,
129 "options"_a,
130 "reconstruction"_a,
131 "problem"_a,
132 "Computes covariances for the parameters in a bundle adjustment "
133 "problem. It is important that the problem has a structure suitable for "
134 "solving using the Schur complement trick. This is the case for the "
135 "standard configuration of bundle adjustment problems, but be careful "
136 "if you modify the underlying problem with custom residuals. Returns "
137 "null if the estimation was not successful.");
138 }