Pose2SLAMExample_lago.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file Pose2SLAMExample_lago.cpp
  10. * @brief A 2D Pose SLAM example that reads input from g2o, and solve the Pose2 problem
  11. * using LAGO (Linear Approximation for Graph Optimization). See class lago.h
  12. * Output is written on a file, in g2o format
  13. * Syntax for the script is ./Pose2SLAMExample_lago input.g2o output.g2o
  14. * @date May 15, 2014
  15. * @author Luca Carlone
  16. */
  17. #include <gtsam/slam/lago.h>
  18. #include <gtsam/slam/dataset.h>
  19. #include <gtsam/geometry/Pose2.h>
  20. #include <fstream>
  21. using namespace std;
  22. using namespace gtsam;
  23. int main(const int argc, const char *argv[]) {
  24. // Read graph from file
  25. string g2oFile;
  26. if (argc < 2)
  27. g2oFile = findExampleDataFile("noisyToyGraph.txt");
  28. else
  29. g2oFile = argv[1];
  30. NonlinearFactorGraph::shared_ptr graph;
  31. Values::shared_ptr initial;
  32. boost::tie(graph, initial) = readG2o(g2oFile);
  33. // Add prior on the pose having index (key) = 0
  34. auto priorModel = noiseModel::Diagonal::Variances(Vector3(1e-6, 1e-6, 1e-8));
  35. graph->addPrior(0, Pose2(), priorModel);
  36. graph->print();
  37. std::cout << "Computing LAGO estimate" << std::endl;
  38. Values estimateLago = lago::initialize(*graph);
  39. std::cout << "done!" << std::endl;
  40. if (argc < 3) {
  41. estimateLago.print("estimateLago");
  42. } else {
  43. const string outputFile = argv[2];
  44. std::cout << "Writing results to file: " << outputFile << std::endl;
  45. NonlinearFactorGraph::shared_ptr graphNoKernel;
  46. Values::shared_ptr initial2;
  47. boost::tie(graphNoKernel, initial2) = readG2o(g2oFile);
  48. writeG2o(*graphNoKernel, estimateLago, outputFile);
  49. std::cout << "done! " << std::endl;
  50. }
  51. return 0;
  52. }