| 
									
										
										
										
											2010-10-14 12:54:38 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SimpleRotation.cpp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is a super-simple example of optimizing a single rotation according to a single prior | 
					
						
							|  |  |  |  * yet it is quite painful (took 1.5 hours to code from scratch) and is overly complex | 
					
						
							|  |  |  |  * An example like this should be very easy to do, so let's work at it. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Created on: Jul 1, 2010 | 
					
						
							|  |  |  |  *  @Author: Frank Dellaert | 
					
						
							|  |  |  |  *  @Author: Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | #include <math.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/PriorFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Rot2.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/NoiseModel.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/nonlinear/Key.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:55 +08:00
										 |  |  | #include <gtsam/nonlinear/LieValues-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearFactorGraph-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-18 02:52:52 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearOptimization-inl.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |  * TODO: make factors independent of Values | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  |  * TODO: get rid of excessive shared pointer stuff: mostly gone | 
					
						
							|  |  |  |  * TODO: make toplevel documentation | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  |  * TODO: investigate whether we can just use ints as keys: will occur for linear, not nonlinear | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef TypedSymbol<Rot2, 'x'> Key; | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | typedef LieValues<Key> Values; | 
					
						
							|  |  |  | typedef NonlinearFactorGraph<Values> Graph; | 
					
						
							|  |  |  | typedef NonlinearOptimizer<Graph,Values> Optimizer; | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const double degree = M_PI / 180; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// optimize a unary factor on rotation 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create a factor
 | 
					
						
							|  |  |  | 	Rot2 prior1 = Rot2::fromAngle(30 * degree); | 
					
						
							| 
									
										
										
										
											2010-10-22 05:38:38 +08:00
										 |  |  | 	prior1.print("goal angle"); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 	SharedDiagonal model1 = noiseModel::Isotropic::Sigma(1, 1 * degree); | 
					
						
							|  |  |  | 	Key key1(1); | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	PriorFactor<Values, Key> factor1(key1, prior1, model1); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Create a factor graph
 | 
					
						
							|  |  |  | 	Graph graph; | 
					
						
							|  |  |  | 	graph.add(factor1); | 
					
						
							| 
									
										
										
										
											2010-10-22 05:38:38 +08:00
										 |  |  | 	graph.print("full graph") ; | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// and an initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-10-22 05:38:38 +08:00
										 |  |  | 	Values initial; | 
					
						
							|  |  |  | 	initial.insert(key1, Rot2::fromAngle(20 * degree)); | 
					
						
							|  |  |  | 	initial.print("initial estimate"); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-22 05:38:38 +08:00
										 |  |  | 	Values result = optimize<Graph, Values>(graph, initial); | 
					
						
							|  |  |  | 	result.print("final result"); | 
					
						
							| 
									
										
										
										
											2010-08-27 02:46:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } |