| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**********************************************************
 | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  |  Written by Frank Dellaert, Nov 2009 | 
					
						
							|  |  |  |  **********************************************************/ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-11 04:19:00 +08:00
										 |  |  | #include "VSLAMConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "VSLAMFactor.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  | #include "VSLAMGraph.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | #include "Point3.h"
 | 
					
						
							|  |  |  | #include "Pose3.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | // make cube
 | 
					
						
							|  |  |  | Point3 x000(-1, -1, -1), x001(-1, -1, +1), x010(-1, +1, -1), x011(-1, +1, +1), | 
					
						
							|  |  |  | 		x100(-1, -1, -1), x101(-1, -1, +1), x110(-1, +1, -1), x111(-1, +1, +1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // make a realistic calibration matrix
 | 
					
						
							|  |  |  | double fov = 60; // degrees
 | 
					
						
							|  |  |  | size_t w=640,h=480; | 
					
						
							|  |  |  | Cal3_S2 K(fov,w,h); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // make cameras
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( VSLAMFactor, error ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Create the factor with a measurement that is 3 pixels off in x
 | 
					
						
							| 
									
										
										
										
											2009-11-13 14:19:21 +08:00
										 |  |  | 	Point2 z(323.,240.); | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | 	double sigma=1.0; | 
					
						
							|  |  |  | 	int cameraFrameNumber=1, landmarkNumber=1; | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  | 	boost::shared_ptr<VSLAMFactor> | 
					
						
							| 
									
										
										
										
											2009-12-17 18:02:43 +08:00
										 |  |  | 		factor(new VSLAMFactor(z, sigma, cameraFrameNumber, landmarkNumber, shared_ptrK(new Cal3_S2(K)))); | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // For the following configuration, the factor predicts 320,240
 | 
					
						
							| 
									
										
										
										
											2009-11-11 04:19:00 +08:00
										 |  |  |   VSLAMConfig config; | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  |   Rot3 R;Point3 t1(0,0,-6); Pose3 x1(R,t1); config.insert(1, x1); | 
					
						
							|  |  |  |   Point3 l1;  config.insert(1, l1); | 
					
						
							| 
									
										
											  
											
												Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just  says something like
	if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
											
										 
											2010-01-14 06:25:03 +08:00
										 |  |  |   CHECK(assert_equal(Point2(320.,240.),factor->predict(x1,l1))); | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Which yields an error of 3^2/2 = 4.5
 | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  |   DOUBLES_EQUAL(4.5,factor->error(config),1e-9); | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check linearize
 | 
					
						
							|  |  |  |   Matrix Al1 = Matrix_(2, 3, 61.584, 0., 0., 0., 61.584, 0.); | 
					
						
							|  |  |  |   Matrix Ax1 = Matrix_(2, 6, 0., -369.504, 0., -61.584, 0., 0., 369.504, 0., 0., 0., -61.584, 0.); | 
					
						
							|  |  |  |   Vector b = Vector_(2,3.,0.); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   GaussianFactor expected("l1", Al1, "x1", Ax1, b, 1); | 
					
						
							|  |  |  |   GaussianFactor::shared_ptr actual = factor->linearize(config); | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  |   CHECK(assert_equal(expected,*actual,1e-3)); | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // linearize graph
 | 
					
						
							|  |  |  |   VSLAMGraph graph; | 
					
						
							|  |  |  | 	graph.push_back(factor); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph expected_lfg; | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  | 	expected_lfg.push_back(actual); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph actual_lfg = graph.linearize(config); | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  | 	CHECK(assert_equal(expected_lfg,actual_lfg)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 	// expmap on a config
 | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  | 	VectorConfig delta; | 
					
						
							|  |  |  | 	delta.insert("x1",Vector_(6, 0.,0.,0., 1.,1.,1.)); | 
					
						
							|  |  |  | 	delta.insert("l1",Vector_(3, 1.,2.,3.)); | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 	VSLAMConfig actual_config = expmap(config, delta); | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  |   VSLAMConfig expected_config; | 
					
						
							| 
									
										
										
										
											2010-01-14 10:58:29 +08:00
										 |  |  |   Point3 t2(1,1,-5); Pose3 x2(R,t2); expected_config.insert(1, x2); | 
					
						
							|  |  |  |   Point3 l2(1,2,3); expected_config.insert(1, l2); | 
					
						
							| 
									
										
										
										
											2009-11-11 13:14:03 +08:00
										 |  |  |   CHECK(assert_equal(expected_config,actual_config,1e-9)); | 
					
						
							| 
									
										
										
										
											2009-11-09 12:46:34 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 05:09:43 +08:00
										 |  |  | TEST( VSLAMFactor, equals ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Create two identical factors and make sure they're equal
 | 
					
						
							|  |  |  | 	Vector z = Vector_(2,323.,240.); | 
					
						
							|  |  |  | 	double sigma=1.0; | 
					
						
							|  |  |  | 	int cameraFrameNumber=1, landmarkNumber=1; | 
					
						
							|  |  |  | 	boost::shared_ptr<VSLAMFactor> | 
					
						
							| 
									
										
										
										
											2009-12-17 18:02:43 +08:00
										 |  |  | 	  factor1(new VSLAMFactor(z, sigma, cameraFrameNumber, landmarkNumber, shared_ptrK(new Cal3_S2(K)))); | 
					
						
							| 
									
										
										
										
											2009-11-12 05:09:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	boost::shared_ptr<VSLAMFactor> | 
					
						
							| 
									
										
										
										
											2009-12-17 18:02:43 +08:00
										 |  |  | 		factor2(new VSLAMFactor(z, sigma, cameraFrameNumber, landmarkNumber, shared_ptrK(new Cal3_S2(K)))); | 
					
						
							| 
									
										
										
										
											2009-11-12 05:09:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	CHECK(assert_equal(*factor1, *factor2)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 	TestResult tr; | 
					
						
							|  |  |  | 	TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 |