| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file Pose3SLAMExample_initializePose3.cpp | 
					
						
							|  |  |  |  * @brief A 3D Pose SLAM example that reads input from g2o, and initializes the Pose3 using InitializePose3 | 
					
						
							| 
									
										
										
										
											2014-09-12 08:54:46 +08:00
										 |  |  |  * Syntax for the script is ./Pose3SLAMExample_changeKeys input.g2o rewritted.g2o | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  |  * @date Aug 25, 2014 | 
					
						
							|  |  |  |  * @author Luca Carlone | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/slam/dataset.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/BetweenFactor.h>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(const int argc, const char *argv[]) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Read graph from file
 | 
					
						
							|  |  |  |   string g2oFile; | 
					
						
							|  |  |  |   if (argc < 2) | 
					
						
							|  |  |  |     g2oFile = findExampleDataFile("pose3example.txt"); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     g2oFile = argv[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   NonlinearFactorGraph::shared_ptr graph; | 
					
						
							|  |  |  |   Values::shared_ptr initial; | 
					
						
							|  |  |  |   bool is3D = true; | 
					
						
							|  |  |  |   boost::tie(graph, initial) = readG2o(g2oFile, is3D); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-12 08:54:46 +08:00
										 |  |  |   bool add = false; | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  |   Key firstKey = 8646911284551352320; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::cout << "Using reference key: " <<  firstKey  << std::endl; | 
					
						
							| 
									
										
										
										
											2014-09-12 08:54:46 +08:00
										 |  |  |   if(add) | 
					
						
							|  |  |  |     std::cout << "adding key "  << std::endl; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     std::cout << "subtracting key "  << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (argc < 3) { | 
					
						
							|  |  |  |     std::cout << "Please provide output file to write "   << std::endl; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     const string inputFileRewritten = argv[2]; | 
					
						
							|  |  |  |     std::cout << "Rewriting input to file: " << inputFileRewritten << std::endl; | 
					
						
							|  |  |  |     // Additional: rewrite input with simplified keys 0,1,...
 | 
					
						
							|  |  |  |     Values simpleInitial; | 
					
						
							| 
									
										
										
										
											2021-01-05 09:46:16 +08:00
										 |  |  |     for(const auto key_value: *initial) { | 
					
						
							| 
									
										
										
										
											2014-09-12 08:54:46 +08:00
										 |  |  |       Key key; | 
					
						
							|  |  |  |       if(add) | 
					
						
							|  |  |  |         key = key_value.key + firstKey; | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         key = key_value.key - firstKey; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  |       simpleInitial.insert(key, initial->at(key_value.key)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     NonlinearFactorGraph simpleGraph; | 
					
						
							| 
									
										
										
										
											2016-05-20 21:20:03 +08:00
										 |  |  |     for(const boost::shared_ptr<NonlinearFactor>& factor: *graph) { | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  |       boost::shared_ptr<BetweenFactor<Pose3> > pose3Between = | 
					
						
							|  |  |  |           boost::dynamic_pointer_cast<BetweenFactor<Pose3> >(factor); | 
					
						
							|  |  |  |       if (pose3Between){ | 
					
						
							| 
									
										
										
										
											2014-09-12 08:54:46 +08:00
										 |  |  |         Key key1, key2; | 
					
						
							|  |  |  |         if(add){ | 
					
						
							|  |  |  |           key1 = pose3Between->key1() + firstKey; | 
					
						
							|  |  |  |           key2 = pose3Between->key2() + firstKey; | 
					
						
							|  |  |  |         }else{ | 
					
						
							|  |  |  |           key1 = pose3Between->key1() - firstKey; | 
					
						
							|  |  |  |           key2 = pose3Between->key2() - firstKey; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  |         NonlinearFactor::shared_ptr simpleFactor( | 
					
						
							| 
									
										
										
										
											2019-05-17 02:40:55 +08:00
										 |  |  |             new BetweenFactor<Pose3>(key1, key2, pose3Between->measured(), pose3Between->noiseModel())); | 
					
						
							| 
									
										
										
										
											2014-09-10 21:35:49 +08:00
										 |  |  |         simpleGraph.add(simpleFactor); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     writeG2o(simpleGraph, simpleInitial, inputFileRewritten); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } |