g2o
simple_optimize.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include <iostream>
28 
30 #include "g2o/core/block_solver.h"
34 
35 #include "g2o/core/factory.h"
36 //#include "g2o/types/slam3d/types_slam3d.h"
37 //#include "g2o/types/slam2d/types_slam2d.h"
38 
39 #include "g2o/stuff/command_args.h"
40 
41 using namespace std;
42 using namespace g2o;
43 
44 // we use the 2D and 3D SLAM types here
45 G2O_USE_TYPE_GROUP(slam2d);
46 G2O_USE_TYPE_GROUP(slam3d);
47 
48 int main(int argc, char** argv)
49 {
50  // Command line parsing
51  int maxIterations;
52  string outputFilename;
53  string inputFilename;
54  CommandArgs arg;
55  arg.param("i", maxIterations, 10, "perform n iterations, if negative consider the gain");
56  arg.param("o", outputFilename, "", "output final version of the graph");
57  arg.paramLeftOver("graph-input", inputFilename, "", "graph file which will be processed");
58  arg.parseArgs(argc, argv);
59 
60  // create the linear solver
62 
63  // create the block solver on top of the linear solver
64  BlockSolverX* blockSolver = new BlockSolverX(linearSolver);
65 
66  // create the algorithm to carry out the optimization
67  //OptimizationAlgorithmGaussNewton* optimizationAlgorithm = new OptimizationAlgorithmGaussNewton(blockSolver);
68  OptimizationAlgorithmLevenberg* optimizationAlgorithm = new OptimizationAlgorithmLevenberg(blockSolver);
69 
70  // NOTE: We skip to fix a variable here, either this is stored in the file
71  // itself or Levenberg will handle it.
72 
73  // create the optimizer to load the data and carry out the optimization
74  SparseOptimizer optimizer;
75  optimizer.setVerbose(true);
76  optimizer.setAlgorithm(optimizationAlgorithm);
77 
78  ifstream ifs(inputFilename.c_str());
79  if (! ifs) {
80  cerr << "unable to open " << inputFilename << endl;
81  return 1;
82  }
83  optimizer.load(ifs);
84  optimizer.initializeOptimization();
85  optimizer.optimize(maxIterations);
86 
87  if (outputFilename.size() > 0) {
88  if (outputFilename == "-") {
89  cerr << "saving to stdout";
90  optimizer.save(cout);
91  } else {
92  cerr << "saving " << outputFilename << " ... ";
93  optimizer.save(outputFilename.c_str());
94  }
95  cerr << "done." << endl;
96  }
97  return 0;
98 }
Command line parsing of argc and argv.
Definition: command_args.h:46
G2O_USE_TYPE_GROUP(slam2d)
Implementation of the Levenberg Algorithm.
int optimize(int iterations, bool online=false)
Traits::LinearSolverType LinearSolverType
Definition: block_solver.h:110
bool parseArgs(int argc, char **argv, bool exitOnError=true)
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
void setVerbose(bool verbose)
linear solver which uses CSparse
void paramLeftOver(const std::string &name, std::string &p, const std::string &defValue, const std::string &desc, bool optional=false)
void setAlgorithm(OptimizationAlgorithm *algorithm)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
virtual bool load(std::istream &is, bool createEdges=true)
load the graph from a stream. Uses the Factory singleton for creating the vertices and edges...
Implementation of a solver operating on the blocks of the Hessian.
Definition: block_solver.h:96
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
int main(int argc, char **argv)
BlockSolver< BlockSolverTraits< Eigen::Dynamic, Eigen::Dynamic > > BlockSolverX
Definition: block_solver.h:179