Pose3SLAMExample_g2o.py
Go to the documentation of this file.
1 """
2  * @file Pose3SLAMExample_initializePose3.cpp
3  * @brief A 3D Pose SLAM example that reads input from g2o, and initializes the
4  * Pose3 using InitializePose3
5  * @date Jan 17, 2019
6  * @author Vikrant Shah based on CPP example by Luca Carlone
7 """
8 # pylint: disable=invalid-name, E1101
9 
10 from __future__ import print_function
11 import argparse
12 import numpy as np
13 import matplotlib.pyplot as plt
14 from mpl_toolkits.mplot3d import Axes3D
15 
16 import gtsam
17 from gtsam.utils import plot
18 
19 
20 def vector6(x, y, z, a, b, c):
21  """Create 6d double numpy array."""
22  return np.array([x, y, z, a, b, c], dtype=float)
23 
24 
25 parser = argparse.ArgumentParser(
26  description="A 3D Pose SLAM example that reads input from g2o, and "
27  "initializes Pose3")
28 parser.add_argument('-i', '--input', help='input file g2o format')
29 parser.add_argument('-o', '--output',
30  help="the path to the output file with optimized graph")
31 parser.add_argument("-p", "--plot", action="store_true",
32  help="Flag to plot results")
33 args = parser.parse_args()
34 
35 g2oFile = gtsam.findExampleDataFile("pose3example.txt") if args.input is None \
36  else args.input
37 
38 is3D = True
39 graph, initial = gtsam.readG2o(g2oFile, is3D)
40 
41 # Add Prior on the first key
42 priorModel = gtsam.noiseModel.Diagonal.Variances(vector6(1e-6, 1e-6, 1e-6,
43  1e-4, 1e-4, 1e-4))
44 
45 print("Adding prior to g2o file ")
46 firstKey = initial.keys()[0]
47 graph.add(gtsam.PriorFactorPose3(firstKey, gtsam.Pose3(), priorModel))
48 
50 params.setVerbosity("Termination") # this will show info about stopping conds
51 optimizer = gtsam.GaussNewtonOptimizer(graph, initial, params)
52 result = optimizer.optimize()
53 print("Optimization complete")
54 
55 print("initial error = ", graph.error(initial))
56 print("final error = ", graph.error(result))
57 
58 if args.output is None:
59  print("Final Result:\n{}".format(result))
60 else:
61  outputFile = args.output
62  print("Writing results to file: ", outputFile)
63  graphNoKernel, _ = gtsam.readG2o(g2oFile, is3D)
64  gtsam.writeG2o(graphNoKernel, result, outputFile)
65  print ("Done!")
66 
67 if args.plot:
68  resultPoses = gtsam.utilities.allPose3s(result)
69  for i in range(resultPoses.size()):
70  plot.plot_pose3(1, resultPoses.atPose3(i))
71  plt.show()
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Values allPose3s(const Values &values)
Extract all Pose3 values.
GTSAM_EXPORT GraphAndValues readG2o(const std::string &g2oFile, const bool is3D=false, KernelFunctionType kernelFunctionType=KernelFunctionTypeNONE)
This function parses a g2o file and stores the measurements into a NonlinearFactorGraph and the initi...
Definition: dataset.cpp:615
GTSAM_EXPORT 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:630
static shared_ptr Variances(const Vector &variances, bool smart=true)
Definition: NoiseModel.cpp:258
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:65


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