Pose3SLAMExample_changeKeys.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Pose3SLAMExample_initializePose3.cpp
  10. * @brief A 3D Pose SLAM example that reads input from g2o, and initializes the Pose3 using InitializePose3
  11. * Syntax for the script is ./Pose3SLAMExample_changeKeys input.g2o rewritted.g2o
  12. * @date Aug 25, 2014
  13. * @author Luca Carlone
  14. */
  15. #include <gtsam/slam/dataset.h>
  16. #include <gtsam/slam/BetweenFactor.h>
  17. #include <fstream>
  18. using namespace std;
  19. using namespace gtsam;
  20. int main(const int argc, const char *argv[]) {
  21. // Read graph from file
  22. string g2oFile;
  23. if (argc < 2)
  24. g2oFile = findExampleDataFile("pose3example.txt");
  25. else
  26. g2oFile = argv[1];
  27. NonlinearFactorGraph::shared_ptr graph;
  28. Values::shared_ptr initial;
  29. bool is3D = true;
  30. boost::tie(graph, initial) = readG2o(g2oFile, is3D);
  31. bool add = false;
  32. Key firstKey = 8646911284551352320;
  33. std::cout << "Using reference key: " << firstKey << std::endl;
  34. if(add)
  35. std::cout << "adding key " << std::endl;
  36. else
  37. std::cout << "subtracting key " << std::endl;
  38. if (argc < 3) {
  39. std::cout << "Please provide output file to write " << std::endl;
  40. } else {
  41. const string inputFileRewritten = argv[2];
  42. std::cout << "Rewriting input to file: " << inputFileRewritten << std::endl;
  43. // Additional: rewrite input with simplified keys 0,1,...
  44. Values simpleInitial;
  45. for(const auto key_value: *initial) {
  46. Key key;
  47. if(add)
  48. key = key_value.key + firstKey;
  49. else
  50. key = key_value.key - firstKey;
  51. simpleInitial.insert(key, initial->at(key_value.key));
  52. }
  53. NonlinearFactorGraph simpleGraph;
  54. for(const boost::shared_ptr<NonlinearFactor>& factor: *graph) {
  55. boost::shared_ptr<BetweenFactor<Pose3> > pose3Between =
  56. boost::dynamic_pointer_cast<BetweenFactor<Pose3> >(factor);
  57. if (pose3Between){
  58. Key key1, key2;
  59. if(add){
  60. key1 = pose3Between->key1() + firstKey;
  61. key2 = pose3Between->key2() + firstKey;
  62. }else{
  63. key1 = pose3Between->key1() - firstKey;
  64. key2 = pose3Between->key2() - firstKey;
  65. }
  66. NonlinearFactor::shared_ptr simpleFactor(
  67. new BetweenFactor<Pose3>(key1, key2, pose3Between->measured(), pose3Between->noiseModel()));
  68. simpleGraph.add(simpleFactor);
  69. }
  70. }
  71. writeG2o(simpleGraph, simpleInitial, inputFileRewritten);
  72. }
  73. return 0;
  74. }