ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
triangulation.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <vector>
12 
13 #include "base/camera.h"
14 #include "optim/ransac.h"
15 #include "util/alignment.h"
16 #include "util/math.h"
17 #include "util/types.h"
18 
19 namespace colmap {
20 
21 // Triangulation estimator to estimate 3D point from multiple observations.
22 // The triangulation must satisfy the following constraints:
23 // - Sufficient triangulation angle between observation pairs.
24 // - All observations must satisfy cheirality constraint.
25 //
26 // An observation is composed of an image measurement and the corresponding
27 // camera pose and calibration.
29 public:
30  enum class ResidualType {
33  };
34 
35  struct PointData {
36  PointData() {}
37  PointData(const Eigen::Vector2d& point_,
38  const Eigen::Vector2d& point_N_)
39  : point(point_), point_normalized(point_N_) {}
40  // Image observation in pixels. Only needs to be set for
41  // REPROJECTION_ERROR.
42  Eigen::Vector2d point;
43  // Normalized image observation. Must always be set.
44  Eigen::Vector2d point_normalized;
45  };
46 
47  struct PoseData {
48  PoseData() : camera(nullptr) {}
49  PoseData(const Eigen::Matrix3x4d& proj_matrix_,
50  const Eigen::Vector3d& pose_,
51  const Camera* camera_)
52  : proj_matrix(proj_matrix_), proj_center(pose_), camera(camera_) {}
53  // The projection matrix for the image of the observation.
55  // The projection center for the image of the observation.
56  Eigen::Vector3d proj_center;
57  // The camera for the image of the observation.
58  const Camera* camera;
59  };
60 
61  typedef PointData X_t;
62  typedef PoseData Y_t;
63  typedef Eigen::Vector3d M_t;
64 
65  // Specify settings for triangulation estimator.
66  void SetMinTriAngle(const double min_tri_angle);
67  void SetResidualType(const ResidualType residual_type);
68 
69  // The minimum number of samples needed to estimate a model.
70  static const int kMinNumSamples = 2;
71 
72  // Estimate a 3D point from a two-view observation.
73  //
74  // @param point_data Image measurement.
75  // @param point_data Camera poses.
76  //
77  // @return Triangulated point if successful, otherwise
78  // none.
79  std::vector<M_t> Estimate(const std::vector<X_t>& point_data,
80  const std::vector<Y_t>& pose_data) const;
81 
82  // Calculate residuals in terms of squared reprojection or angular error.
83  //
84  // @param point_data Image measurements.
85  // @param point_data Camera poses.
86  // @param xyz 3D point.
87  //
88  // @return Residual for each observation.
89  void Residuals(const std::vector<X_t>& point_data,
90  const std::vector<Y_t>& pose_data,
91  const M_t& xyz,
92  std::vector<double>* residuals) const;
93 
94 private:
96  double min_tri_angle_ = 0.0;
97 };
98 
100  // Minimum triangulation angle in radians.
101  double min_tri_angle = 0.0;
102 
103  // The employed residual type.
106 
107  // RANSAC options for TriangulationEstimator.
109 
110  void Check() const {
111  CHECK_GE(min_tri_angle, 0.0);
113  }
114 };
115 
116 // Robustly estimate 3D point from observations in multiple views using RANSAC
117 // and a subsequent non-linear refinement using all inliers. Returns true
118 // if the estimated number of inliers has more than two views.
120  const EstimateTriangulationOptions& options,
121  const std::vector<TriangulationEstimator::PointData>& point_data,
122  const std::vector<TriangulationEstimator::PoseData>& pose_data,
123  std::vector<char>* inlier_mask,
124  Eigen::Vector3d* xyz);
125 
126 } // namespace colmap
127 
128 // EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION_CUSTOM(
129 // colmap::TriangulationEstimator::PointData)
130 // EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION_CUSTOM(
131 // colmap::TriangulationEstimator::PoseData)
void Residuals(const std::vector< X_t > &point_data, const std::vector< Y_t > &pose_data, const M_t &xyz, std::vector< double > *residuals) const
void SetResidualType(const ResidualType residual_type)
void SetMinTriAngle(const double min_tri_angle)
static const int kMinNumSamples
Definition: triangulation.h:70
std::vector< M_t > Estimate(const std::vector< X_t > &point_data, const std::vector< Y_t > &pose_data) const
Matrix< double, 3, 4 > Matrix3x4d
Definition: types.h:39
bool EstimateTriangulation(const EstimateTriangulationOptions &options, const std::vector< TriangulationEstimator::PointData > &point_data, const std::vector< TriangulationEstimator::PoseData > &pose_data, std::vector< char > *inlier_mask, Eigen::Vector3d *xyz)
TriangulationEstimator::ResidualType residual_type
void Check() const
Definition: ransac.h:43
PointData(const Eigen::Vector2d &point_, const Eigen::Vector2d &point_N_)
Definition: triangulation.h:37
PoseData(const Eigen::Matrix3x4d &proj_matrix_, const Eigen::Vector3d &pose_, const Camera *camera_)
Definition: triangulation.h:49