1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 //       notice, this list of conditions and the following disclaimer.
9 //
10 //     * Redistributions in binary form must reproduce the above copyright
11 //       notice, this list of conditions and the following disclaimer in the
12 //       documentation and/or other materials provided with the distribution.
13 //
14 //     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 //       its contributors may be used to endorse or promote products derived
16 //       from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #ifndef COLMAP_SRC_CONTROLLERS_INCREMENTAL_MAPPER_H_
33 #define COLMAP_SRC_CONTROLLERS_INCREMENTAL_MAPPER_H_
34 
35 #include "base/reconstruction_manager.h"
36 #include "sfm/incremental_mapper.h"
37 #include "util/threading.h"
38 
39 namespace colmap {
40 
41 struct IncrementalMapperOptions {
42  public:
43   // The minimum number of matches for inlier matches to be considered.
44   int min_num_matches = 15;
45 
46   // Whether to ignore the inlier matches of watermark image pairs.
47   bool ignore_watermarks = false;
48 
49   // Whether to reconstruct multiple sub-models.
50   bool multiple_models = true;
51 
52   // The number of sub-models to reconstruct.
53   int max_num_models = 50;
54 
55   // The maximum number of overlapping images between sub-models. If the
56   // current sub-models shares more than this number of images with another
57   // model, then the reconstruction is stopped.
58   int max_model_overlap = 20;
59 
60   // The minimum number of registered images of a sub-model, otherwise the
61   // sub-model is discarded.
62   int min_model_size = 10;
63 
64   // The image identifiers used to initialize the reconstruction. Note that
65   // only one or both image identifiers can be specified. In the former case,
66   // the second image is automatically determined.
67   int init_image_id1 = -1;
68   int init_image_id2 = -1;
69 
70   // The number of trials to initialize the reconstruction.
71   int init_num_trials = 200;
72 
73   // Whether to extract colors for reconstructed points.
74   bool extract_colors = true;
75 
76   // The number of threads to use during reconstruction.
77   int num_threads = -1;
78 
79   // Thresholds for filtering images with degenerate intrinsics.
80   double min_focal_length_ratio = 0.1;
81   double max_focal_length_ratio = 10.0;
82   double max_extra_param = 1.0;
83 
84   // Which intrinsic parameters to optimize during the reconstruction.
85   bool ba_refine_focal_length = true;
86   bool ba_refine_principal_point = false;
87   bool ba_refine_extra_params = true;
88 
89   // The minimum number of residuals per bundle adjustment problem to
90   // enable multi-threading solving of the problems.
91   int ba_min_num_residuals_for_multi_threading = 50000;
92 
93   // The number of images to optimize in local bundle adjustment.
94   int ba_local_num_images = 6;
95 
96   // The maximum number of local bundle adjustment iterations.
97   int ba_local_max_num_iterations = 25;
98 
99   // Whether to use PBA in global bundle adjustment.
100   bool ba_global_use_pba = false;
101 
102   // The GPU index for PBA bundle adjustment.
103   int ba_global_pba_gpu_index = -1;
104 
105   // The growth rates after which to perform global bundle adjustment.
106   double ba_global_images_ratio = 1.1;
107   double ba_global_points_ratio = 1.1;
108   int ba_global_images_freq = 500;
109   int ba_global_points_freq = 250000;
110 
111   // The maximum number of global bundle adjustment iterations.
112   int ba_global_max_num_iterations = 50;
113 
114   // The thresholds for iterative bundle adjustment refinements.
115   int ba_local_max_refinements = 2;
116   double ba_local_max_refinement_change = 0.001;
117   int ba_global_max_refinements = 5;
118   double ba_global_max_refinement_change = 0.0005;
119 
120   // Path to a folder with reconstruction snapshots during incremental
121   // reconstruction. Snapshots will be saved according to the specified
122   // frequency of registered images.
123   std::string snapshot_path = "";
124   int snapshot_images_freq = 0;
125 
126   // Which images to reconstruct. If no images are specified, all images will
127   // be reconstructed by default.
128   std::unordered_set<std::string> image_names;
129 
130   // If reconstruction is provided as input, fix the existing image poses.
131   bool fix_existing_images = false;
132 
133   IncrementalMapper::Options Mapper() const;
134   IncrementalTriangulator::Options Triangulation() const;
135   BundleAdjustmentOptions LocalBundleAdjustment() const;
136   BundleAdjustmentOptions GlobalBundleAdjustment() const;
137   ParallelBundleAdjuster::Options ParallelGlobalBundleAdjustment() const;
138 
139   bool Check() const;
140 
141  private:
142   friend class OptionManager;
143   friend class MapperGeneralOptionsWidget;
144   friend class MapperTriangulationOptionsWidget;
145   friend class MapperRegistrationOptionsWidget;
146   friend class MapperInitializationOptionsWidget;
147   friend class MapperBundleAdjustmentOptionsWidget;
148   friend class MapperFilteringOptionsWidget;
149   friend class ReconstructionOptionsWidget;
150   IncrementalMapper::Options mapper;
151   IncrementalTriangulator::Options triangulation;
152 };
153 
154 // Class that controls the incremental mapping procedure by iteratively
155 // initializing reconstructions from the same scene graph.
156 class IncrementalMapperController : public Thread {
157  public:
158   enum {
159     INITIAL_IMAGE_PAIR_REG_CALLBACK,
160     NEXT_IMAGE_REG_CALLBACK,
161     LAST_IMAGE_REG_CALLBACK,
162   };
163 
164   IncrementalMapperController(const IncrementalMapperOptions* options,
165                               const std::string& image_path,
166                               const std::string& database_path,
167                               ReconstructionManager* reconstruction_manager);
168 
169  private:
170   void Run();
171   bool LoadDatabase();
172   void Reconstruct(const IncrementalMapper::Options& init_mapper_options);
173 
174   const IncrementalMapperOptions* options_;
175   const std::string image_path_;
176   const std::string database_path_;
177   ReconstructionManager* reconstruction_manager_;
178   DatabaseCache database_cache_;
179 };
180 
181 // Globally filter points and images in mapper.
182 size_t FilterPoints(const IncrementalMapperOptions& options,
183                     IncrementalMapper* mapper);
184 size_t FilterImages(const IncrementalMapperOptions& options,
185                     IncrementalMapper* mapper);
186 
187 // Globally complete and merge tracks in mapper.
188 size_t CompleteAndMergeTracks(const IncrementalMapperOptions& options,
189                               IncrementalMapper* mapper);
190 
191 }  // namespace colmap
192 
193 #endif  // COLMAP_SRC_CONTROLLERS_INCREMENTAL_MAPPER_H_
194