gtsam/examples/Pose2SLAMExample_g2o.cpp

63 lines
1.9 KiB
C++
Raw Normal View History

2014-05-16 22:03:18 +08:00
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
2014-05-21 06:12:26 +08:00
* @file Pose2SLAMExample_g2o.cpp
* @brief A 2D Pose SLAM example that reads input from g2o, converts it to a factor graph and does the
* optimization. Output is written on a file, in g2o format
* Syntax for the script is ./Pose2SLAMExample_g2o input.g2o output.g2o
2014-05-16 22:03:18 +08:00
* @date May 15, 2014
* @author Luca Carlone
*/
#include <gtsam/slam/dataset.h>
2014-06-01 00:51:47 +08:00
#include <gtsam/slam/PriorFactor.h>
2014-05-16 22:03:18 +08:00
#include <gtsam/nonlinear/GaussNewtonOptimizer.h>
#include <fstream>
using namespace std;
using namespace gtsam;
2014-06-01 00:51:47 +08:00
int main(const int argc, const char *argv[]) {
2014-05-16 22:03:18 +08:00
2014-06-01 00:51:47 +08:00
// Read graph from file
string g2oFile;
2014-05-16 22:03:18 +08:00
if (argc < 2)
2014-06-01 00:51:47 +08:00
g2oFile = "../../examples/Data/noisyToyGraph.txt";
else
g2oFile = argv[1];
2014-05-16 22:03:18 +08:00
NonlinearFactorGraph graph;
Values initial;
readG2o(g2oFile, graph, initial);
2014-05-19 22:53:59 +08:00
// Add prior on the pose having index (key) = 0
NonlinearFactorGraph graphWithPrior = graph;
2014-06-01 00:51:47 +08:00
noiseModel::Diagonal::shared_ptr priorModel = //
noiseModel::Diagonal::Variances((Vector(3) << 1e-6, 1e-6, 1e-8));
graphWithPrior.add(PriorFactor<Pose2>(0, Pose2(), priorModel));
2014-05-16 22:03:18 +08:00
std::cout << "Optimizing the factor graph" << std::endl;
GaussNewtonOptimizer optimizer(graphWithPrior, initial); // , parameters);
2014-05-16 22:03:18 +08:00
Values result = optimizer.optimize();
std::cout << "Optimization complete" << std::endl;
2014-06-01 00:51:47 +08:00
if (argc < 3) {
result.print("result");
} else {
const string outputFile = argv[2];
std::cout << "Writing results to file: " << outputFile << std::endl;
writeG2o(outputFile, graph, result);
std::cout << "done! " << std::endl;
}
2014-05-16 22:03:18 +08:00
return 0;
}