| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  *  @file  testPose2Constraint.cpp | 
					
						
							| 
									
										
										
										
											2009-12-10 07:43:01 +08:00
										 |  |  |  *  @brief Unit tests for Pose2Factor Class | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  |  *  @authors Frank Dellaert, Viorela Ila | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*STL/C++*/ | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp>
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | #include "NonlinearOptimizer-inl.h"
 | 
					
						
							|  |  |  | #include "NonlinearEquality.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-10 07:43:01 +08:00
										 |  |  | #include "Pose2Factor.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-11 07:45:38 +08:00
										 |  |  | #include "Pose2Graph.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( Pose2Factor, linearize ) | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// create a factor between unknown poses p1 and p2
 | 
					
						
							|  |  |  | 	Pose2 measured(2,2,M_PI_2); | 
					
						
							|  |  |  | 	Matrix measurement_covariance = Matrix_(3,3, | 
					
						
							|  |  |  | 			0.25, 0.0, 0.0, | 
					
						
							|  |  |  | 			0.0, 0.25, 0.0, | 
					
						
							|  |  |  | 			0.0, 0.0, 0.01 | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2009-12-10 07:43:01 +08:00
										 |  |  | 	Pose2Factor constraint("p1","p2",measured, measurement_covariance); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Choose a linearization point
 | 
					
						
							|  |  |  | 	Pose2 p1(1.1,2,M_PI_2); // robot at (1.1,2) looking towards y (ground truth is at 1,2, see testPose2)
 | 
					
						
							| 
									
										
										
										
											2009-12-18 08:10:20 +08:00
										 |  |  | 	Pose2 p2(-1,4.1,M_PI);  // robot at (-1,4.1) looking at negative (ground truth is at 4.1,2)
 | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 	Pose2Config config; | 
					
						
							| 
									
										
										
										
											2009-12-11 07:45:38 +08:00
										 |  |  | 	config.insert("p1",p1); | 
					
						
							|  |  |  | 	config.insert("p2",p2); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | 	// expected linearization
 | 
					
						
							|  |  |  | 	// we need the minus signs below as "inverse_square_root"
 | 
					
						
							|  |  |  | 	// uses SVD and the sign is simply arbitrary (but still a square root!)
 | 
					
						
							|  |  |  | 	Matrix square_root_inverse_covariance = Matrix_(3,3, | 
					
						
							|  |  |  | 	    -2.0, 0.0, 0.0, | 
					
						
							|  |  |  | 	    0.0, -2.0, 0.0, | 
					
						
							|  |  |  | 	    0.0, 0.0, -10.0 | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 	Matrix expectedH1 = Matrix_(3,3, | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | 	    0.0,-1.0,-2.1, | 
					
						
							|  |  |  | 	    1.0, 0.0,-2.1, | 
					
						
							|  |  |  | 	    0.0, 0.0,-1.0 | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 	Matrix expectedH2 = Matrix_(3,3, | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | 	    1.0, 0.0, 0.0, | 
					
						
							|  |  |  | 	    0.0, 1.0, 0.0, | 
					
						
							|  |  |  | 	    0.0, 0.0, 1.0 | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 	GaussianFactor expected( | 
					
						
							|  |  |  | 			"p1", square_root_inverse_covariance*expectedH1, | 
					
						
							|  |  |  | 			"p2", square_root_inverse_covariance*expectedH2, | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | 			Vector_(3, 0.1, -0.1, 0.0), 1.0); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | 	// Actual linearization
 | 
					
						
							|  |  |  | 	boost::shared_ptr<GaussianFactor> actual = constraint.linearize(config); | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | 	CHECK(assert_equal(expected,*actual)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-10 07:43:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | bool poseCompare(const std::string& key, | 
					
						
							|  |  |  |     const gtsam::Pose2Config& feasible, | 
					
						
							|  |  |  |     const gtsam::Pose2Config& input) { | 
					
						
							|  |  |  |   return feasible.get(key).equals(input.get(key)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | TEST(Pose2Factor, optimize) { | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create a Pose graph with one equality constraint and one measurement
 | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  |   Pose2Graph fg; | 
					
						
							|  |  |  |   Pose2Config feasible; | 
					
						
							|  |  |  |   feasible.insert("p0", Pose2(0,0,0)); | 
					
						
							|  |  |  |   fg.push_back(Pose2Graph::sharedFactor( | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  |       new NonlinearEquality<Pose2Config>("p0", feasible, dim(Pose2()), poseCompare))); | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  |   fg.push_back(Pose2Graph::sharedFactor( | 
					
						
							|  |  |  |       new Pose2Factor("p0", "p1", Pose2(1,2,M_PI_2), Matrix_(3,3, | 
					
						
							|  |  |  |           0.5, 0.0, 0.0, | 
					
						
							|  |  |  |           0.0, 0.5, 0.0, | 
					
						
							|  |  |  |           0.0, 0.0, 0.5)))); | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create initial config
 | 
					
						
							|  |  |  |   boost::shared_ptr<Pose2Config> initial = | 
					
						
							|  |  |  |   		boost::shared_ptr<Pose2Config>(new Pose2Config()); | 
					
						
							|  |  |  |   initial->insert("p0", Pose2(0,0,0)); | 
					
						
							|  |  |  |   initial->insert("p1", Pose2(0,0,0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Choose an ordering and optimize
 | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  |   Ordering ordering; | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  |   ordering += "p0","p1"; | 
					
						
							|  |  |  |   NonlinearOptimizer<Pose2Graph, Pose2Config> optimizer(fg, ordering, initial); | 
					
						
							| 
									
										
										
										
											2009-12-31 02:41:33 +08:00
										 |  |  |   optimizer = optimizer.levenbergMarquardt(1e-15, 1e-15); | 
					
						
							| 
									
										
										
										
											2009-12-22 00:43:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-02 18:33:33 +08:00
										 |  |  |   // Check with expected config
 | 
					
						
							|  |  |  |   Pose2Config expected; | 
					
						
							|  |  |  |   expected.insert("p0", Pose2(0,0,0)); | 
					
						
							|  |  |  |   expected.insert("p1", Pose2(1,2,M_PI_2)); | 
					
						
							|  |  |  |   CHECK(assert_equal(expected, *optimizer.config())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-18 08:10:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-10 01:29:43 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |