ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
incremental_triangulator.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 "base/database_cache.h"
11 #include "base/reconstruction.h"
12 #include "util/alignment.h"
13 
14 namespace colmap {
15 
16 // Class that triangulates points during the incremental reconstruction.
17 // It holds the state and provides all functionality for triangulation.
19 public:
20  struct Options {
21  // Maximum transitivity to search for correspondences.
23 
24  // Maximum angular error to create new triangulations.
25  double create_max_angle_error = 2.0;
26 
27  // Maximum angular error to continue existing triangulations.
29 
30  // Maximum reprojection error in pixels to merge triangulations.
31  double merge_max_reproj_error = 4.0;
32 
33  // Maximum reprojection error to complete an existing triangulation.
35 
36  // Maximum transitivity for track completion.
38 
39  // Maximum angular error to re-triangulate under-reconstructed image
40  // pairs.
41  double re_max_angle_error = 5.0;
42 
43  // Minimum ratio of common triangulations between an image pair over the
44  // number of correspondences between that image pair to be considered
45  // as under-reconstructed.
46  double re_min_ratio = 0.2;
47 
48  // Maximum number of trials to re-triangulate an image pair.
49  int re_max_trials = 1;
50 
51  // Minimum pairwise triangulation angle for a stable triangulation.
52  double min_angle = 1.5;
53 
54  // Whether to ignore two-view tracks.
56 
57  // Thresholds for bogus camera parameters. Images with bogus camera
58  // parameters are ignored in triangulation.
59  double min_focal_length_ratio = 0.1;
60  double max_focal_length_ratio = 10.0;
61  double max_extra_param = 1.0;
62 
63  bool Check() const;
64  };
65 
66  // Create new incremental triangulator. Note that both the correspondence
67  // graph and the reconstruction objects must live as long as the
68  // triangulator.
69  IncrementalTriangulator(const CorrespondenceGraph* correspondence_graph,
70  Reconstruction* reconstruction);
71 
72  // Triangulate observations of image.
73  //
74  // Triangulation includes creation of new points, continuation of existing
75  // points, and merging of separate points if given image bridges tracks.
76  //
77  // Note that the given image must be registered and its pose must be set
78  // in the associated reconstruction.
79  size_t TriangulateImage(const Options& options, const image_t image_id);
80 
81  // Complete triangulations for image. Tries to create new tracks for not
82  // yet triangulated observations and tries to complete existing tracks.
83  // Returns the number of completed observations.
84  size_t CompleteImage(const Options& options, const image_t image_id);
85 
86  // Complete tracks for specific 3D points.
87  //
88  // Completion tries to recursively add observations to a track that might
89  // have failed to triangulate before due to inaccurate poses, etc.
90  // Returns the number of completed observations.
91  size_t CompleteTracks(const Options& options,
92  const std::unordered_set<point3D_t>& point3D_ids);
93 
94  // Complete tracks of all 3D points.
95  // Returns the number of completed observations.
96  size_t CompleteAllTracks(const Options& options);
97 
98  // Merge tracks of for specific 3D points.
99  // Returns the number of merged observations.
100  size_t MergeTracks(const Options& options,
101  const std::unordered_set<point3D_t>& point3D_ids);
102 
103  // Merge tracks of all 3D points.
104  // Returns the number of merged observations.
105  size_t MergeAllTracks(const Options& options);
106 
107  // Perform retriangulation for under-reconstructed image pairs. Under-
108  // reconstruction usually occurs in the case of a drifting reconstruction.
109  //
110  // Image pairs are under-reconstructed if less than
111  // `Options::tri_re_min_ratio > tri_ratio`, where `tri_ratio` is the number
112  // of triangulated matches over inlier matches between the image pair.
113  size_t Retriangulate(const Options& options);
114 
115  // Indicate that a 3D point has been modified.
116  void AddModifiedPoint3D(const point3D_t point3D_id);
117 
118  // Get changed 3D points, since the last call to `ClearModifiedPoints3D`.
119  const std::unordered_set<point3D_t>& GetModifiedPoints3D();
120 
121  // Clear the collection of changed 3D points.
122  void ClearModifiedPoints3D();
123 
124  // Data for a correspondence / element of a track, used to store all
125  // relevant data for triangulation, in order to avoid duplicate lookup
126  // in the underlying unordered_map's in the Reconstruction
127  struct CorrData {
130  const Image* image;
131  const Camera* camera;
132  const Point2D* point2D;
133  };
134 
135 private:
136  // Clear cache of bogus camera parameters and merge trials.
137  void ClearCaches();
138 
139  // Find (transitive) correspondences to other images.
140  size_t Find(const Options& options,
141  const image_t image_id,
142  const point2D_t point2D_idx,
143  const size_t transitivity,
144  std::vector<CorrData>* corrs_data);
145 
146  // Try to create a new 3D point from the given correspondences.
147  size_t Create(const Options& options,
148  const std::vector<CorrData>& corrs_data);
149 
150  // Try to continue the 3D point with the given correspondences.
151  size_t Continue(const Options& options,
152  const CorrData& ref_corr_data,
153  const std::vector<CorrData>& corrs_data);
154 
155  // Try to merge 3D point with any of its corresponding 3D points.
156  size_t Merge(const Options& options, const point3D_t point3D_id);
157 
158  // Try to transitively complete the track of a 3D point.
159  size_t Complete(const Options& options, const point3D_t point3D_id);
160 
161  // Check if camera has bogus parameters and cache the result.
162  bool HasCameraBogusParams(const Options& options, const Camera& camera);
163 
164  // Database cache for the reconstruction. Used to retrieve correspondence
165  // information for triangulation.
166  const CorrespondenceGraph* correspondence_graph_;
167 
168  // Reconstruction of the model. Modified when triangulating new points.
169  Reconstruction* reconstruction_;
170 
171  // Cache for cameras with bogus parameters.
172  std::unordered_map<camera_t, bool> camera_has_bogus_params_;
173 
174  // Cache for tried track merges to avoid duplicate merge trials.
175  std::unordered_map<point3D_t, std::unordered_set<point3D_t>> merge_trials_;
176 
177  // Number of trials to retriangulate image pair.
178  std::unordered_map<image_pair_t, int> re_num_trials_;
179 
180  // Changed 3D points, i.e. if a 3D point is modified (created, continued,
181  // deleted, merged, etc.). Cleared once `ModifiedPoints3D` is called.
182  std::unordered_set<point3D_t> modified_point3D_ids_;
183 };
184 
185 } // namespace colmap
186 
187 // EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION_CUSTOM(
188 // colmap::IncrementalTriangulator::CorrData)
size_t TriangulateImage(const Options &options, const image_t image_id)
void AddModifiedPoint3D(const point3D_t point3D_id)
const std::unordered_set< point3D_t > & GetModifiedPoints3D()
size_t CompleteTracks(const Options &options, const std::unordered_set< point3D_t > &point3D_ids)
size_t MergeAllTracks(const Options &options)
size_t MergeTracks(const Options &options, const std::unordered_set< point3D_t > &point3D_ids)
size_t CompleteImage(const Options &options, const image_t image_id)
size_t CompleteAllTracks(const Options &options)
size_t Retriangulate(const Options &options)
IncrementalTriangulator(const CorrespondenceGraph *correspondence_graph, Reconstruction *reconstruction)
uint64_t point3D_t
Definition: types.h:72
uint32_t point2D_t
Definition: types.h:67
uint32_t image_t
Definition: types.h:61