ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
bundle_adjustment.cc
Go to the documentation of this file.
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 
33 
34 #include <ceres/ceres.h>
35 
37 #include "util/misc.h"
38 
39 namespace colmap {
40 namespace {
41 
42 // Callback functor called after each bundle adjustment iteration.
43 class BundleAdjustmentIterationCallback : public ceres::IterationCallback {
44  public:
45  explicit BundleAdjustmentIterationCallback(Thread* thread)
46  : thread_(thread) {}
47 
48  virtual ceres::CallbackReturnType operator()(
49  const ceres::IterationSummary& summary) {
50  CHECK_NOTNULL(thread_);
51  thread_->BlockIfPaused();
52  if (thread_->IsStopped()) {
53  return ceres::SOLVER_TERMINATE_SUCCESSFULLY;
54  } else {
55  return ceres::SOLVER_CONTINUE;
56  }
57  }
58 
59  private:
60  Thread* thread_;
61 };
62 
63 } // namespace
64 
66  const OptionManager& options, Reconstruction* reconstruction)
67  : options_(options), reconstruction_(reconstruction) {}
68 
69 void BundleAdjustmentController::Run() {
70  CHECK_NOTNULL(reconstruction_);
71 
72  PrintHeading1("Global bundle adjustment");
73 
74  const std::vector<image_t>& reg_image_ids = reconstruction_->RegImageIds();
75 
76  if (reg_image_ids.size() < 2) {
77  std::cout << "WARNING: Need at least two views." << std::endl;
78  return;
79  }
80 
81  // Avoid degeneracies in bundle adjustment.
82  reconstruction_->FilterObservationsWithNegativeDepth();
83 
84  BundleAdjustmentOptions ba_options = *options_.bundle_adjustment;
85  ba_options.solver_options.minimizer_progress_to_stdout = true;
86 
87  BundleAdjustmentIterationCallback iteration_callback(this);
88  ba_options.solver_options.callbacks.push_back(&iteration_callback);
89 
90  // Configure bundle adjustment.
91  BundleAdjustmentConfig ba_config;
92  for (const image_t image_id : reg_image_ids) {
93  ba_config.AddImage(image_id);
94  }
95  ba_config.SetConstantPose(reg_image_ids[0]);
96  ba_config.SetConstantTvec(reg_image_ids[1], {0});
97 
98  // Run bundle adjustment.
99  BundleAdjuster bundle_adjuster(ba_options, ba_config);
100  bundle_adjuster.Solve(reconstruction_);
101 
103 }
104 
105 } // namespace colmap
BundleAdjustmentController(const OptionManager &options, Reconstruction *reconstruction)
std::shared_ptr< BundleAdjustmentOptions > bundle_adjustment
size_t FilterObservationsWithNegativeDepth()
const std::vector< image_t > & RegImageIds() const
const Timer & GetTimer() const
Definition: threading.cc:154
void PrintMinutes() const
Definition: timer.cc:93
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
uint32_t image_t
Definition: types.h:61
void PrintHeading1(const std::string &heading)
Definition: misc.cc:225