| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SmartProjectionFactorsCreator.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Created on: Oct 8, 2013 | 
					
						
							|  |  |  |  *      Author: aspn | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef SMARTPROJECTIONFACTORSCREATOR_H_
 | 
					
						
							|  |  |  | #define SMARTPROJECTIONFACTORSCREATOR_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 05:08:27 +08:00
										 |  |  | #include <gtsam/geometry/PinholeCamera.h>
 | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  | #include <gtsam/linear/NoiseModel.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/NonlinearFactorGraph.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Use a map to store landmark/smart factor pairs
 | 
					
						
							|  |  |  | #include <gtsam/base/FastMap.h>
 | 
					
						
							| 
									
										
										
										
											2013-10-16 06:43:10 +08:00
										 |  |  | // #include <gtsam_unstable/slam/SmartProjectionFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/slam/SmartProjectionHessianFactor.h>
 | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 00:21:15 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/assign/std/vector.hpp>
 | 
					
						
							| 
									
										
										
										
											2013-10-13 01:15:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 00:21:15 +08:00
										 |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  | #include <utility>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template<class POSE, class LANDMARK, class CALIBRATION = Cal3_S2> | 
					
						
							|  |  |  |   class SmartProjectionFactorsCreator { | 
					
						
							| 
									
										
										
										
											2013-10-16 06:43:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   typedef SmartProjectionHessianFactor<Pose3, Point3, CALIBRATION> SmartFactor; | 
					
						
							|  |  |  |   typedef FastMap<Key, boost::shared_ptr<SmartProjectionHessianFactorState> > SmartFactorToStateMap; | 
					
						
							|  |  |  |   typedef FastMap<Key, boost::shared_ptr<SmartFactor> > SmartFactorMap; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  |   public: | 
					
						
							|  |  |  |     SmartProjectionFactorsCreator(const SharedNoiseModel& model, | 
					
						
							|  |  |  |         const boost::shared_ptr<CALIBRATION>& K, | 
					
						
							|  |  |  |         const double rankTol, | 
					
						
							|  |  |  |         const double linThreshold, | 
					
						
							|  |  |  |         boost::optional<POSE> body_P_sensor = boost::none) : | 
					
						
							|  |  |  |           noise_(model), K_(K), rankTolerance_(rankTol), | 
					
						
							|  |  |  |           linearizationThreshold_(linThreshold), body_P_sensor_(body_P_sensor), | 
					
						
							|  |  |  |           totalNumMeasurements(0), numLandmarks(0) {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void add(Key landmarkKey, | 
					
						
							|  |  |  |         Key poseKey, Point2 measurement, NonlinearFactorGraph &graph) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       std::vector<Key> views; | 
					
						
							|  |  |  |       std::vector<Point2> measurements; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       bool debug = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Check if landmark exists in mapping
 | 
					
						
							|  |  |  |       SmartFactorToStateMap::iterator fsit = smartFactorStates.find(landmarkKey); | 
					
						
							| 
									
										
										
										
											2013-10-16 06:43:10 +08:00
										 |  |  |       typename SmartFactorMap::iterator fit = smartFactors.find(landmarkKey); | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  |       if (fsit != smartFactorStates.end() && fit != smartFactors.end()) { | 
					
						
							|  |  |  |         if (debug) fprintf(stderr,"Adding measurement to existing landmark\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Add measurement to smart factor
 | 
					
						
							| 
									
										
										
										
											2013-10-16 06:43:10 +08:00
										 |  |  |         (*fit).second->add(measurement, poseKey, noise_, K_); | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  |         totalNumMeasurements++; | 
					
						
							|  |  |  |         if (debug) (*fit).second->print(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (debug) fprintf(stderr,"New landmark (%d,%d)\n", fsit != smartFactorStates.end(), fit != smartFactors.end()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 03:29:07 +08:00
										 |  |  |         views.push_back(poseKey); | 
					
						
							|  |  |  |         measurements.push_back(measurement); | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // This is a new landmark, create a new factor and add to mapping
 | 
					
						
							| 
									
										
										
										
											2013-10-16 06:43:10 +08:00
										 |  |  |         boost::shared_ptr<SmartProjectionHessianFactorState> smartFactorState(new SmartProjectionHessianFactorState()); | 
					
						
							|  |  |  |         boost::shared_ptr<SmartFactor> smartFactor(new SmartFactor(rankTolerance_, linearizationThreshold_)); | 
					
						
							|  |  |  |         smartFactor->add(measurement, poseKey, noise_, K_); | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  |         smartFactorStates.insert( std::make_pair(landmarkKey, smartFactorState) ); | 
					
						
							|  |  |  |         smartFactors.insert( std::make_pair(landmarkKey, smartFactor) ); | 
					
						
							|  |  |  |         graph.push_back(smartFactor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         numLandmarks++; | 
					
						
							|  |  |  |         //landmarkKeys.push_back( L(l) );
 | 
					
						
							|  |  |  |         totalNumMeasurements++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         views.clear(); | 
					
						
							|  |  |  |         measurements.clear(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 06:43:10 +08:00
										 |  |  |     void add(Key landmarkKey, Key poseKey, | 
					
						
							|  |  |  |         Point2 measurement, | 
					
						
							|  |  |  |         const SharedNoiseModel& model, | 
					
						
							|  |  |  |         const boost::shared_ptr<CALIBRATION>& K, | 
					
						
							|  |  |  |         NonlinearFactorGraph &graph) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       std::vector<Key> views; | 
					
						
							|  |  |  |       std::vector<Point2> measurements; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       bool debug = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Check if landmark exists in mapping
 | 
					
						
							|  |  |  |       SmartFactorToStateMap::iterator fsit = smartFactorStates.find(landmarkKey); | 
					
						
							|  |  |  |       typename SmartFactorMap::iterator fit = smartFactors.find(landmarkKey); | 
					
						
							|  |  |  |       if (fsit != smartFactorStates.end() && fit != smartFactors.end()) { | 
					
						
							|  |  |  |         if (debug) fprintf(stderr,"Adding measurement to existing landmark\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Add measurement to smart factor
 | 
					
						
							|  |  |  |         (*fit).second->add(measurement, poseKey, model, K); | 
					
						
							|  |  |  |         totalNumMeasurements++; | 
					
						
							|  |  |  |         if (debug) (*fit).second->print(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (debug) fprintf(stderr,"New landmark (%d,%d)\n", fsit != smartFactorStates.end(), fit != smartFactors.end()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // This is a new landmark, create a new factor and add to mapping
 | 
					
						
							|  |  |  |         boost::shared_ptr<SmartProjectionHessianFactorState> smartFactorState(new SmartProjectionHessianFactorState()); | 
					
						
							|  |  |  |         boost::shared_ptr<SmartFactor> smartFactor(new SmartFactor(rankTolerance_, linearizationThreshold_)); | 
					
						
							|  |  |  |         smartFactor->add(measurement, poseKey, model, K); | 
					
						
							|  |  |  |         smartFactorStates.insert( std::make_pair(landmarkKey, smartFactorState) ); | 
					
						
							|  |  |  |         smartFactors.insert( std::make_pair(landmarkKey, smartFactor) ); | 
					
						
							|  |  |  |         graph.push_back(smartFactor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         numLandmarks++; | 
					
						
							|  |  |  |         totalNumMeasurements++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-11 04:21:40 +08:00
										 |  |  |     unsigned int getTotalNumMeasurements() { return totalNumMeasurements; } | 
					
						
							|  |  |  |     unsigned int getNumLandmarks() { return numLandmarks; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  |   protected: | 
					
						
							|  |  |  |     const SharedNoiseModel noise_;   ///< noise model used
 | 
					
						
							|  |  |  |     ///< (important that the order is the same as the keys that we use to create the factor)
 | 
					
						
							|  |  |  |     boost::shared_ptr<CALIBRATION> K_;  ///< shared pointer to calibration object
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     double rankTolerance_; ///< threshold to decide whether triangulation is degenerate
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     double linearizationThreshold_; ///< threshold to decide whether to re-linearize
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     boost::optional<POSE> body_P_sensor_; ///< The pose of the sensor in the body frame
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SmartFactorToStateMap smartFactorStates; | 
					
						
							|  |  |  |     SmartFactorMap smartFactors; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-11 04:21:40 +08:00
										 |  |  |     unsigned int totalNumMeasurements; | 
					
						
							|  |  |  |     unsigned int numLandmarks; | 
					
						
							| 
									
										
										
										
											2013-10-10 01:13:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* SMARTPROJECTIONFACTORSCREATOR_H_ */
 |