| 
									
										
										
										
											2014-07-15 22:14:23 +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 SmartProjectionFactorExample.cpp | 
					
						
							|  |  |  | * @brief A stereo visual odometry example | 
					
						
							|  |  |  | * @date May 30, 2014 | 
					
						
							|  |  |  | * @author Stephen Camp | 
					
						
							|  |  |  | * @author Chris Beall | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * A smart projection factor example based on stereo data, throwing away the | 
					
						
							|  |  |  |  * measurement from the right camera | 
					
						
							|  |  |  |  *  -robot starts at origin | 
					
						
							|  |  |  |  *  -moves forward, taking periodic stereo measurements | 
					
						
							|  |  |  |  *  -makes monocular observations of many landmarks | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Pose3.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Cal3_S2Stereo.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Values.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  | #include <gtsam/nonlinear/utilities.h>
 | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearEquality.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/NonlinearFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/Symbol.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/dataset.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 01:51:12 +08:00
										 |  |  | #include <gtsam_unstable/slam/SmartStereoProjectionPoseFactor.h>
 | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  | using symbol_shorthand::X; | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char** argv){ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-29 03:14:39 +08:00
										 |  |  |   typedef SmartStereoProjectionPoseFactor SmartFactor; | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:08:38 +08:00
										 |  |  |   bool output_poses = true; | 
					
						
							|  |  |  |   string poseOutput("../../../examples/data/optimized_poses.txt"); | 
					
						
							| 
									
										
										
										
											2014-07-23 00:49:09 +08:00
										 |  |  |   string init_poseOutput("../../../examples/data/initial_poses.txt"); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   Values initial_estimate; | 
					
						
							|  |  |  |   NonlinearFactorGraph graph; | 
					
						
							| 
									
										
										
										
											2014-07-16 01:09:12 +08:00
										 |  |  |   const noiseModel::Isotropic::shared_ptr model = noiseModel::Isotropic::Sigma(3,1); | 
					
						
							| 
									
										
										
										
											2014-07-23 00:49:09 +08:00
										 |  |  |   ofstream pose3Out, init_pose3Out; | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-17 02:45:56 +08:00
										 |  |  |   bool add_initial_noise = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   string calibration_loc = findExampleDataFile("VO_calibration.txt"); | 
					
						
							|  |  |  |   string pose_loc = findExampleDataFile("VO_camera_poses_large.txt"); | 
					
						
							|  |  |  |   string factor_loc = findExampleDataFile("VO_stereo_factors_large.txt"); | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   //read camera calibration info from file
 | 
					
						
							|  |  |  |   // focal lengths fx, fy, skew s, principal point u0, v0, baseline b
 | 
					
						
							|  |  |  |   cout << "Reading calibration info" << endl; | 
					
						
							|  |  |  |   ifstream calibration_file(calibration_loc.c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   double fx, fy, s, u0, v0, b; | 
					
						
							|  |  |  |   calibration_file >> fx >> fy >> s >> u0 >> v0 >> b; | 
					
						
							|  |  |  |   const Cal3_S2Stereo::shared_ptr K(new Cal3_S2Stereo(fx, fy, s, u0, v0,b)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cout << "Reading camera poses" << endl; | 
					
						
							|  |  |  |   ifstream pose_file(pose_loc.c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int pose_id; | 
					
						
							|  |  |  |   MatrixRowMajor m(4,4); | 
					
						
							|  |  |  |   //read camera pose parameters and use to make initial estimates of camera poses
 | 
					
						
							|  |  |  |   while (pose_file >> pose_id) { | 
					
						
							|  |  |  |     for (int i = 0; i < 16; i++) { | 
					
						
							|  |  |  |       pose_file >> m.data()[i]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-17 02:45:56 +08:00
										 |  |  |     if(add_initial_noise){ | 
					
						
							| 
									
										
										
										
											2014-07-23 10:00:33 +08:00
										 |  |  |       m(1,3) += (pose_id % 10)/10.0; | 
					
						
							| 
									
										
										
										
											2014-07-17 02:45:56 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |     initial_estimate.insert(X(pose_id), Pose3(m)); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-07-23 00:49:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |   const auto initialPoses = initial_estimate.extract<Pose3>(); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:05:34 +08:00
										 |  |  |   if (output_poses) { | 
					
						
							|  |  |  |     init_pose3Out.open(init_poseOutput.c_str(), ios::out); | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |     for (size_t i = 1; i <= initialPoses.size(); i++) { | 
					
						
							| 
									
										
										
										
											2015-05-13 06:05:34 +08:00
										 |  |  |       init_pose3Out | 
					
						
							|  |  |  |           << i << " " | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |           << initialPoses.at(X(i)).matrix().format( | 
					
						
							| 
									
										
										
										
											2015-05-13 06:05:34 +08:00
										 |  |  |               Eigen::IOFormat(Eigen::StreamPrecision, 0, " ", " ")) << endl; | 
					
						
							| 
									
										
										
										
											2014-07-23 00:49:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   // camera and landmark keys
 | 
					
						
							|  |  |  |   size_t x, l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // pixel coordinates uL, uR, v (same for left/right images due to rectification)
 | 
					
						
							|  |  |  |   // landmark coordinates X, Y, Z in camera frame, resulting from triangulation
 | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |   double uL, uR, v, _X, Y, Z; | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   ifstream factor_file(factor_loc.c_str()); | 
					
						
							|  |  |  |   cout << "Reading stereo factors" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //read stereo measurements and construct smart factors
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-27 01:02:39 +08:00
										 |  |  |   SmartFactor::shared_ptr factor(new SmartFactor(model)); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   size_t current_l = 3;   // hardcoded landmark ID from first measurement
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |   while (factor_file >> x >> l >> uL >> uR >> v >> _X >> Y >> Z) { | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if(current_l != l) { | 
					
						
							|  |  |  |       graph.push_back(factor); | 
					
						
							| 
									
										
										
										
											2015-08-27 01:02:39 +08:00
										 |  |  |       factor = SmartFactor::shared_ptr(new SmartFactor(model)); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |       current_l = l; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |     factor->add(StereoPoint2(uL,uR,v), X(x), K); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |   Pose3 first_pose = initial_estimate.at<Pose3>(X(1)); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   //constrain the first pose such that it cannot change from its original value during optimization
 | 
					
						
							|  |  |  |   // NOTE: NonlinearEquality forces the optimizer to use QR rather than Cholesky
 | 
					
						
							|  |  |  |   // QR is much slower than Cholesky, but numerically more stable
 | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |   graph.emplace_shared<NonlinearEquality<Pose3> >(X(1),first_pose); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   LevenbergMarquardtParams params; | 
					
						
							|  |  |  |   params.verbosityLM = LevenbergMarquardtParams::TRYLAMBDA; | 
					
						
							|  |  |  |   params.verbosity = NonlinearOptimizerParams::ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cout << "Optimizing" << endl; | 
					
						
							|  |  |  |   //create Levenberg-Marquardt optimizer to optimize the factor graph
 | 
					
						
							| 
									
										
										
										
											2016-06-20 03:29:37 +08:00
										 |  |  |   LevenbergMarquardtOptimizer optimizer(graph, initial_estimate, params); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   Values result = optimizer.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cout << "Final result sample:" << endl; | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |   Values pose_values = utilities::allPose3s(result); | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   pose_values.print("Final camera poses:\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 21:08:38 +08:00
										 |  |  |   if(output_poses){ | 
					
						
							|  |  |  |     pose3Out.open(poseOutput.c_str(),ios::out); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:05:34 +08:00
										 |  |  |     for(size_t i = 1; i<=pose_values.size(); i++){ | 
					
						
							| 
									
										
										
										
											2023-01-22 03:39:42 +08:00
										 |  |  |       pose3Out << i << " " << pose_values.at<Pose3>(X(i)).matrix().format(Eigen::IOFormat(Eigen::StreamPrecision, 0, | 
					
						
							| 
									
										
										
										
											2014-07-22 21:08:38 +08:00
										 |  |  |         " ", " ")) << endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     cout << "Writing output" << endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 22:14:23 +08:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } |