Pose3SLAMExample_changeKeys.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #include <gtsam/slam/dataset.h>
22 #include <fstream>
23 
24 using namespace std;
25 using namespace gtsam;
26 
27 int main(const int argc, const char *argv[]) {
28 
29  // Read graph from file
30  string g2oFile;
31  if (argc < 2)
32  g2oFile = findExampleDataFile("pose3example.txt");
33  else
34  g2oFile = argv[1];
35 
37  Values::shared_ptr initial;
38  bool is3D = true;
39  boost::tie(graph, initial) = readG2o(g2oFile, is3D);
40 
41  bool add = false;
42  Key firstKey = 8646911284551352320;
43 
44  std::cout << "Using reference key: " << firstKey << std::endl;
45  if(add)
46  std::cout << "adding key " << std::endl;
47  else
48  std::cout << "subtracting key " << std::endl;
49 
50 
51  if (argc < 3) {
52  std::cout << "Please provide output file to write " << std::endl;
53  } else {
54  const string inputFileRewritten = argv[2];
55  std::cout << "Rewriting input to file: " << inputFileRewritten << std::endl;
56  // Additional: rewrite input with simplified keys 0,1,...
57  Values simpleInitial;
58  for(const auto key_value: *initial) {
59  Key key;
60  if(add)
61  key = key_value.key + firstKey;
62  else
63  key = key_value.key - firstKey;
64 
65  simpleInitial.insert(key, initial->at(key_value.key));
66  }
67  NonlinearFactorGraph simpleGraph;
68  for(const boost::shared_ptr<NonlinearFactor>& factor: *graph) {
69  boost::shared_ptr<BetweenFactor<Pose3> > pose3Between =
70  boost::dynamic_pointer_cast<BetweenFactor<Pose3> >(factor);
71  if (pose3Between){
72  Key key1, key2;
73  if(add){
74  key1 = pose3Between->key1() + firstKey;
75  key2 = pose3Between->key2() + firstKey;
76  }else{
77  key1 = pose3Between->key1() - firstKey;
78  key2 = pose3Between->key2() - firstKey;
79  }
80  NonlinearFactor::shared_ptr simpleFactor(
81  new BetweenFactor<Pose3>(key1, key2, pose3Between->measured(), pose3Between->noiseModel()));
82  simpleGraph.add(simpleFactor);
83  }
84  }
85  writeG2o(simpleGraph, simpleInitial, inputFileRewritten);
86  }
87  return 0;
88 }
void writeG2o(const NonlinearFactorGraph &graph, const Values &estimate, const string &filename)
This function writes a g2o file from NonlinearFactorGraph and a Values structure. ...
Definition: dataset.cpp:630
void insert(Key j, const Value &val)
Definition: Values.cpp:140
Values initial
Definition: Half.h:150
IsDerived< DERIVEDFACTOR > add(boost::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:190
NonlinearFactorGraph graph
graph add(boost::make_shared< UnaryFactor >(1, 0.0, 0.0, unaryNoise))
string findExampleDataFile(const string &name)
Definition: dataset.cpp:65
const Symbol key1('v', 1)
int main(const int argc, const char *argv[])
boost::shared_ptr< This > shared_ptr
boost::shared_ptr< This > shared_ptr
traits
Definition: chartTesting.h:28
const Symbol key2('v', 2)
GraphAndValues readG2o(const string &g2oFile, const bool is3D, KernelFunctionType kernelFunctionType)
This function parses a g2o file and stores the measurements into a NonlinearFactorGraph and the initi...
Definition: dataset.cpp:615
utility functions for loading datasets
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:27