Pose3SLAMExample_g2o.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>
23 #include <fstream>
24 
25 using namespace std;
26 using namespace gtsam;
27 
28 int main(const int argc, const char* argv[]) {
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  std::tie(graph, initial) = readG2o(g2oFile, is3D);
40 
41  // Add prior on the first key
42  auto priorModel = noiseModel::Diagonal::Variances(
43  (Vector(6) << 1e-6, 1e-6, 1e-6, 1e-4, 1e-4, 1e-4).finished());
44  Key firstKey = 0;
45  for (const auto key : initial->keys()) {
46  std::cout << "Adding prior to g2o file " << std::endl;
47  firstKey = key;
48  graph->addPrior(firstKey, Pose3(), priorModel);
49  break;
50  }
51 
52  std::cout << "Optimizing the factor graph" << std::endl;
54  params.setVerbosity("TERMINATION"); // show info about stopping conditions
56  Values result = optimizer.optimize();
57  std::cout << "Optimization complete" << std::endl;
58 
59  std::cout << "initial error=" << graph->error(*initial) << std::endl;
60  std::cout << "final error=" << graph->error(result) << std::endl;
61 
62  if (argc < 3) {
63  result.print("result");
64  } else {
65  const string outputFile = argv[2];
66  std::cout << "Writing results to file: " << outputFile << std::endl;
68  Values::shared_ptr initial2;
69  std::tie(graphNoKernel, initial2) = readG2o(g2oFile);
70  writeG2o(*graphNoKernel, result, outputFile);
71  std::cout << "done! " << std::endl;
72  }
73  return 0;
74 }
gtsam::NonlinearOptimizer::optimize
virtual const Values & optimize()
Definition: NonlinearOptimizer.h:98
gtsam::GaussNewtonOptimizer
Definition: GaussNewtonOptimizer.h:38
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
initial
Values initial
Definition: OdometryOptimize.cpp:2
gtsam::readG2o
GraphAndValues readG2o(const std::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:621
vanilla::params
static const SmartProjectionParams params
Definition: smartFactorScenarios.h:69
result
Values result
Definition: OdometryOptimize.cpp:8
GaussNewtonOptimizer.h
gtsam::NonlinearFactorGraph::error
double error(const Values &values) const
Definition: NonlinearFactorGraph.cpp:170
dataset.h
utility functions for loading datasets
initial2
Definition: testScenarioRunner.cpp:209
BetweenFactor.h
gtsam::Pose3
Definition: Pose3.h:37
gtsam::GaussNewtonParams
Definition: GaussNewtonOptimizer.h:30
gtsam::NonlinearFactorGraph::addPrior
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Definition: NonlinearFactorGraph.h:199
main
int main(const int argc, const char *argv[])
Definition: Pose3SLAMExample_g2o.cpp:28
key
const gtsam::Symbol key('X', 0)
gtsam::Values::print
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:66
gtsam
traits
Definition: SFMdata.h:40
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
outputFile
string outputFile
Definition: SolverComparer.cpp:88
initial
Definition: testScenarioRunner.cpp:148
gtsam::findExampleDataFile
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:70
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
gtsam::NonlinearFactorGraph::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: NonlinearFactorGraph.h:61
gtsam::writeG2o
void writeG2o(const NonlinearFactorGraph &graph, const Values &estimate, const std::string &filename)
This function writes a g2o file from NonlinearFactorGraph and a Values structure.
Definition: dataset.cpp:636


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:34:24