g2o
Loading...
Searching...
No Matches
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
29#include "g2o/core/factory.h"
33
34using namespace std;
35using namespace g2o;
36
37// we use the 2D and 3D SLAM types here
41
42int main(int argc, char** argv) {
43 // Command line parsing
44 int maxIterations;
45 string outputFilename;
46 string inputFilename;
47 CommandArgs arg;
48 arg.param("i", maxIterations, 10,
49 "perform n iterations, if negative consider the gain");
50 arg.param("o", outputFilename, "", "output final version of the graph");
51 arg.paramLeftOver("graph-input", inputFilename, "",
52 "graph file which will be processed");
53 arg.parseArgs(argc, argv);
54
55 // NOTE: We skip to fix a vertex here, either this is stored in the file
56 // itself or Levenberg will handle it.
57
58 // create the optimizer to load the data and carry out the optimization
59 SparseOptimizer optimizer;
60 optimizer.setVerbose(true);
61
62 // allocate the solver
64 optimizer.setAlgorithm(
66 solverProperty));
67
68 ifstream ifs(inputFilename.c_str());
69 if (!ifs) {
70 cerr << "unable to open " << inputFilename << endl;
71 return 1;
72 }
73 optimizer.load(ifs);
74 optimizer.initializeOptimization();
75 optimizer.optimize(maxIterations);
76
77 if (outputFilename.size() > 0) {
78 if (outputFilename == "-") {
79 cerr << "saving to stdout";
80 optimizer.save(cout);
81 } else {
82 cerr << "saving " << outputFilename << " ... ";
83 optimizer.save(outputFilename.c_str());
84 }
85 cerr << "done." << endl;
86 }
87 return 0;
88}
Command line parsing of argc and argv.
bool parseArgs(int argc, char **argv, bool exitOnError=true)
void paramLeftOver(const std::string &name, std::string &p, const std::string &defValue, const std::string &desc, bool optional=false)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
static OptimizationAlgorithmFactory * instance()
return the instance
int optimize(int iterations, bool online=false)
void setVerbose(bool verbose)
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
void setAlgorithm(OptimizationAlgorithm *algorithm)
#define G2O_USE_TYPE_GROUP(typeGroupName)
Definition factory.h:159
int main()
Definition gicp_demo.cpp:44
Definition jet.h:876
#define G2O_USE_OPTIMIZATION_LIBRARY(libraryname)
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
virtual bool load(std::istream &is)