SFMExample_bal_COLAMD_METIS.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 // For an explanation of headers, see SFMExample.cpp
21 #include <gtsam/sfm/SfmData.h> // for loading BAL datasets !
22 #include <gtsam/slam/dataset.h>
25 #include <gtsam/inference/Symbol.h>
27 #include <gtsam/base/timing.h>
28 
29 #include <vector>
30 
31 using namespace std;
32 using namespace gtsam;
35 
36 // We will be using a projection factor that ties a SFM_Camera to a 3D point.
37 // An SFM_Camera is defined in datase.h as a camera with unknown Cal3Bundler
38 // calibration and has a total of 9 free parameters
40 
41 
42 int main(int argc, char* argv[]) {
43  // Find default file, but if an argument is given, try loading a file
44  string filename = findExampleDataFile("dubrovnik-3-7-pre");
45  if (argc > 1) filename = string(argv[1]);
46 
47  // Load the SfM data from file
48  SfmData mydata = SfmData::FromBalFile(filename);
49  cout << "read " << mydata.numberTracks() << " tracks on " << mydata.numberCameras() << " cameras" << endl;
50 
51  // Create a factor graph
53 
54  // We share *one* noiseModel between all projection factors
55  auto noise = noiseModel::Isotropic::Sigma(2, 1.0); // one pixel in u and v
56 
57  // Add measurements to the factor graph
58  size_t j = 0;
59  for (const SfmTrack& track : mydata.tracks) {
60  for (const auto& [i, uv] : track.measurements) {
62  uv, noise, C(i), P(j)); // note use of shorthand symbols C and P
63  }
64  j += 1;
65  }
66 
67  // Add a prior on pose x1. This indirectly specifies where the origin is.
68  // and a prior on the position of the first landmark to fix the scale
69  graph.addPrior(C(0), mydata.cameras[0], noiseModel::Isotropic::Sigma(9, 0.1));
70  graph.addPrior(P(0), mydata.tracks[0].p,
71  noiseModel::Isotropic::Sigma(3, 0.1));
72 
73  // Create initial estimate
75  size_t i = 0;
76  j = 0;
77  for (const SfmCamera& camera : mydata.cameras) initial.insert(C(i++), camera);
78  for (const SfmTrack& track : mydata.tracks) initial.insert(P(j++), track.p);
79 
83  LevenbergMarquardtParams params_using_COLAMD, params_using_METIS;
84  try {
85  params_using_METIS.setVerbosity("ERROR");
86  gttic_(METIS_ORDERING);
87  params_using_METIS.ordering = Ordering::Create(Ordering::METIS, graph);
88  gttoc_(METIS_ORDERING);
89 
90  params_using_COLAMD.setVerbosity("ERROR");
91  gttic_(COLAMD_ORDERING);
92  params_using_COLAMD.ordering = Ordering::Create(Ordering::COLAMD, graph);
93  gttoc_(COLAMD_ORDERING);
94  } catch (exception& e) {
95  cout << e.what();
96  }
97 
98  // expect they have different ordering results
99  if (params_using_COLAMD.ordering == params_using_METIS.ordering) {
100  cout << "COLAMD and METIS produce the same ordering. "
101  << "Problem here!!!" << endl;
102  }
103 
104  /* Optimize the graph with METIS and COLAMD and time the results */
105 
106  Values result_METIS, result_COLAMD;
107  try {
108  gttic_(OPTIMIZE_WITH_METIS);
109  LevenbergMarquardtOptimizer lm_METIS(graph, initial, params_using_METIS);
110  result_METIS = lm_METIS.optimize();
111  gttoc_(OPTIMIZE_WITH_METIS);
112 
113  gttic_(OPTIMIZE_WITH_COLAMD);
114  LevenbergMarquardtOptimizer lm_COLAMD(graph, initial, params_using_COLAMD);
115  result_COLAMD = lm_COLAMD.optimize();
116  gttoc_(OPTIMIZE_WITH_COLAMD);
117  } catch (exception& e) {
118  cout << e.what();
119  }
120 
121  { // printing the result
122 
123  cout << "COLAMD final error: " << graph.error(result_COLAMD) << endl;
124  cout << "METIS final error: " << graph.error(result_METIS) << endl;
125 
126  cout << endl << endl;
127 
128  cout << "Time comparison by solving " << filename << " results:" << endl;
129 
130  cout << mydata.numberTracks() << " point tracks and " << mydata.numberCameras()
131  << " cameras" << endl;
132 
133  tictoc_print_();
134  }
135 
136  return 0;
137 }
138 
timing.h
Timing utilities.
gtsam::NonlinearOptimizer::optimize
virtual const Values & optimize()
Definition: NonlinearOptimizer.h:98
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::NonlinearOptimizerParams::setVerbosity
void setVerbosity(const std::string &src)
Definition: NonlinearOptimizerParams.h:59
LevenbergMarquardtOptimizer.h
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
initial
Values initial
Definition: OdometryOptimize.cpp:2
gtsam::SfmTrack2d::measurements
std::vector< SfmMeasurement > measurements
The 2D image projections (id,(u,v))
Definition: SfmTrack.h:44
GeneralSFMFactor.h
a general SFM factor with an unknown calibration
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::SfmData
SfmData stores a bunch of SfmTracks.
Definition: SfmData.h:39
gtsam::SfmTrack
Definition: SfmTrack.h:125
gtsam::NonlinearFactorGraph::error
double error(const Values &values) const
Definition: NonlinearFactorGraph.cpp:170
gtsam::SfmData::tracks
std::vector< SfmTrack > tracks
Sparse set of points.
Definition: SfmData.h:42
dataset.h
utility functions for loading datasets
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gttoc_
#define gttoc_(label)
Definition: timing.h:250
gttic_
#define gttic_(label)
Definition: timing.h:245
relicense.filename
filename
Definition: relicense.py:57
gtsam::PinholeCamera< Cal3Bundler >
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::NonlinearFactorGraph::addPrior
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Definition: NonlinearFactorGraph.h:199
gtsam::tictoc_print_
void tictoc_print_()
Definition: timing.h:268
gtsam::LevenbergMarquardtOptimizer
Definition: LevenbergMarquardtOptimizer.h:35
Symbol.h
gtsam::SfmData::numberCameras
size_t numberCameras() const
The number of cameras.
Definition: SfmData.h:77
main
int main(int argc, char *argv[])
Definition: SFMExample_bal_COLAMD_METIS.cpp:42
SfmData.h
Data structure for dealing with Structure from Motion data.
C
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
gtsam
traits
Definition: SFMdata.h:40
NonlinearFactorGraph.h
Factor Graph consisting of non-linear factors.
gtsam::SfmData::cameras
std::vector< SfmCamera > cameras
Set of cameras.
Definition: SfmData.h:40
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
gtsam::GeneralSFMFactor
Definition: GeneralSFMFactor.h:59
P
static double P[]
Definition: ellpe.c:68
gtsam::LevenbergMarquardtParams
Definition: LevenbergMarquardtParams.h:35
initial
Definition: testScenarioRunner.cpp:148
gtsam::findExampleDataFile
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:70
camera
static const CalibratedCamera camera(kDefaultPose)
gtsam::SfmTrack::p
Point3 p
3D position of the point
Definition: SfmTrack.h:126
gtsam::NonlinearOptimizerParams::ordering
std::optional< Ordering > ordering
The optional variable elimination ordering, or empty to use COLAMD (default: empty)
Definition: NonlinearOptimizerParams.h:108
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::SfmData::numberTracks
size_t numberTracks() const
The number of reconstructed 3D points.
Definition: SfmData.h:74
MyFactor
GeneralSFMFactor< SfmCamera, Point3 > MyFactor
Definition: SFMExample_bal_COLAMD_METIS.cpp:39
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::FactorGraph::emplace_shared
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:153


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:11