| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  *  @file  testPose2Prior.cpp | 
					
						
							|  |  |  |  *  @brief Unit tests for Pose2Prior Class | 
					
						
							|  |  |  |  *  @authors Frank Dellaert, Viorela Ila | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define GTSAM_MAGIC_KEY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | #include "numericalDerivative.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-17 02:01:16 +08:00
										 |  |  | #include "pose2SLAM.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Common measurement covariance
 | 
					
						
							|  |  |  | static double sx=0.5, sy=0.5,st=0.1; | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  | static SharedGaussian sigmas = sharedSigmas(Vector_(3,sx,sy,st)); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Very simple test establishing Ax-b \approx z-h(x)
 | 
					
						
							|  |  |  | TEST( Pose2Prior, error ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Choose a linearization point
 | 
					
						
							|  |  |  | 	Pose2 p1(1, 0, 0); // robot at (1,0)
 | 
					
						
							|  |  |  | 	Pose2Config x0; | 
					
						
							|  |  |  | 	x0.insert(1, p1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create factor
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | 	Pose2Prior factor(1, p1, sigmas); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Actual linearization
 | 
					
						
							|  |  |  | 	boost::shared_ptr<GaussianFactor> linear = factor.linearize(x0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check error at x0, i.e. delta = zero !
 | 
					
						
							|  |  |  | 	VectorConfig delta; | 
					
						
							|  |  |  | 	delta.insert("x1", zero(3)); | 
					
						
							|  |  |  | 	Vector error_at_zero = Vector_(3,0.0,0.0,0.0); | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | 	CHECK(assert_equal(error_at_zero,factor.whitenedError(x0))); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 	CHECK(assert_equal(-error_at_zero,linear->error_vector(delta))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check error after increasing p2
 | 
					
						
							|  |  |  | 	VectorConfig plus = delta + VectorConfig("x1", Vector_(3, 0.1, 0.0, 0.0)); | 
					
						
							|  |  |  | 	Pose2Config x1 = expmap(x0, plus); | 
					
						
							|  |  |  | 	Vector error_at_plus = Vector_(3,0.1/sx,0.0,0.0); // h(x)-z = 0.1 !
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | 	CHECK(assert_equal(error_at_plus,factor.whitenedError(x1))); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 	CHECK(assert_equal(error_at_plus,linear->error_vector(plus))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // common Pose2Prior for tests below
 | 
					
						
							|  |  |  | static Pose2 prior(2,2,M_PI_2); | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | static Pose2Prior factor(1,prior, sigmas); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-17 00:46:57 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | // The error |A*dx-b| approximates (h(x0+dx)-z) = -error_vector
 | 
					
						
							|  |  |  | // Hence i.e., b = approximates z-h(x0) = error_vector(x0)
 | 
					
						
							|  |  |  | Vector h(const Pose2& p1) { | 
					
						
							| 
									
										
										
										
											2010-01-18 13:38:53 +08:00
										 |  |  | 	return sigmas->whiten(factor.evaluateError(p1)); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-17 00:46:57 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | TEST( Pose2Prior, linearize ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Choose a linearization point at ground truth
 | 
					
						
							|  |  |  | 	Pose2Config x0; | 
					
						
							|  |  |  | 	x0.insert(1,prior); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Actual linearization
 | 
					
						
							|  |  |  | 	boost::shared_ptr<GaussianFactor> actual = factor.linearize(x0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-17 00:46:57 +08:00
										 |  |  | 	// Test with numerical derivative
 | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | 	Matrix numericalH = numericalDerivative11(h, prior, 1e-5); | 
					
						
							| 
									
										
										
										
											2010-01-17 00:46:57 +08:00
										 |  |  | 	CHECK(assert_equal(numericalH,actual->get_A("x1"))); | 
					
						
							| 
									
										
										
										
											2010-01-16 23:39:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |