11#include " bundle_adjustment.h"
22
3+ #include < optional>
4+
35#include < colmap/estimators/cost_functions.h>
46#include < colmap/estimators/manifold.h>
57#include < colmap/sensor/models.h>
@@ -33,7 +35,9 @@ bool BundleAdjuster::Solve(const ViewGraph& view_graph,
3335 AddCamerasAndPointsToParameterGroups (cameras, images, tracks);
3436
3537 // Parameterize the variables
36- ParameterizeVariables (cameras, images, tracks);
38+ if (!ParameterizeVariables (cameras, images, tracks)) {
39+ return false ;
40+ }
3741
3842 // Set the solver options.
3943 ceres::Solver::Summary summary;
@@ -180,25 +184,25 @@ void BundleAdjuster::AddCamerasAndPointsToParameterGroups(
180184 }
181185}
182186
183- void BundleAdjuster::ParameterizeVariables (
187+ bool BundleAdjuster::ParameterizeVariables (
184188 std::unordered_map<camera_t , Camera>& cameras,
185189 std::unordered_map<image_t , Image>& images,
186190 std::unordered_map<track_t , Track>& tracks) {
187- image_t center;
188-
189191 // Parameterize rotations, and set rotations and translations to be constant
190192 // if desired FUTURE: Consider fix the scale of the reconstruction
191- int counter = 0 ;
193+ std::optional< image_t > center ;
192194 for (auto & [image_id, image] : images) {
193195 if (problem_->HasParameterBlock (
194196 image.cam_from_world .rotation .coeffs ().data ())) {
195197 colmap::SetQuaternionManifold (
196198 problem_.get (), image.cam_from_world .rotation .coeffs ().data ());
197199
198- if (counter == 0 ) {
200+ if (!center) {
201+ // Center is taken as the first image whose rotation is present in the
202+ // problem variables.
199203 center = image_id;
200- counter++;
201204 }
205+
202206 if (!options_.optimize_rotations )
203207 problem_->SetParameterBlockConstant (
204208 image.cam_from_world .rotation .coeffs ().data ());
@@ -208,11 +212,16 @@ void BundleAdjuster::ParameterizeVariables(
208212 }
209213 }
210214
215+ if (!center.has_value ()) {
216+ LOG (ERROR) << " No valid image found in the problem parameters." ;
217+ return false ;
218+ }
219+
211220 // Set the first camera to be fixed to remove the gauge ambiguity.
212221 problem_->SetParameterBlockConstant (
213- images[center].cam_from_world .rotation .coeffs ().data ());
222+ images[* center].cam_from_world .rotation .coeffs ().data ());
214223 problem_->SetParameterBlockConstant (
215- images[center].cam_from_world .translation .data ());
224+ images[* center].cam_from_world .translation .data ());
216225
217226 // Parameterize the camera parameters, or set them to be constant if desired
218227 if (options_.optimize_intrinsics && !options_.optimize_principal_point ) {
@@ -244,6 +253,7 @@ void BundleAdjuster::ParameterizeVariables(
244253 }
245254 }
246255 }
256+ return true ;
247257}
248258
249259} // namespace glomap
0 commit comments