| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  |  * 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  *  @file  testTSAMFactors.cpp | 
					
						
							|  |  |  |  *  @brief Unit tests for TSAM 1 Factors | 
					
						
							|  |  |  |  *  @author Frank Dellaert | 
					
						
							|  |  |  |  *  @date May 2014 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/slam/TSAMFactors.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/numericalDerivative.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-16 01:07:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  | #include "gtsam/geometry/Point2.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 09:03:15 +08:00
										 |  |  | using namespace std::placeholders; | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Key i(1), j(2); // Key for pose and point
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //*************************************************************************
 | 
					
						
							|  |  |  | TEST( DeltaFactor, all ) { | 
					
						
							|  |  |  |   // Create a factor
 | 
					
						
							|  |  |  |   Point2 measurement(1, 1); | 
					
						
							|  |  |  |   static SharedNoiseModel model(noiseModel::Unit::Create(2)); | 
					
						
							|  |  |  |   DeltaFactor factor(i, j, measurement, model); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Set the linearization point
 | 
					
						
							|  |  |  |   Pose2 pose(1, 2, 0); | 
					
						
							|  |  |  |   Point2 point(4, 11); | 
					
						
							|  |  |  |   Vector2 expected(4 - 1 - 1, 11 - 2 - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Use the factor to calculate the Jacobians
 | 
					
						
							|  |  |  |   Matrix H1Actual, H2Actual; | 
					
						
							|  |  |  |   Vector actual = factor.evaluateError(pose, point, H1Actual, H2Actual); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, actual, 1e-9)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Use numerical derivatives to calculate the Jacobians
 | 
					
						
							|  |  |  |   Matrix H1Expected, H2Expected; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 06:23:40 +08:00
										 |  |  |   H1Expected = numericalDerivative11<Vector2, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &point](const Pose2& pose) { return factor.evaluateError(pose, point); }, pose); | 
					
						
							| 
									
										
										
										
											2014-12-16 06:23:40 +08:00
										 |  |  |   H2Expected = numericalDerivative11<Vector2, Point2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose](const Point2& point) { return factor.evaluateError(pose, point); }, point); | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Verify the Jacobians are correct
 | 
					
						
							|  |  |  |   EXPECT(assert_equal(H1Expected, H1Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H2Expected, H2Actual, 1e-9)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //*************************************************************************
 | 
					
						
							|  |  |  | TEST( DeltaFactorBase, all ) { | 
					
						
							|  |  |  |   // Create a factor
 | 
					
						
							|  |  |  |   Key b1(10), b2(20); | 
					
						
							|  |  |  |   Point2 measurement(1, 1); | 
					
						
							|  |  |  |   static SharedNoiseModel model(noiseModel::Unit::Create(2)); | 
					
						
							|  |  |  |   DeltaFactorBase factor(b1, i, b2, j, measurement, model); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Set the linearization point
 | 
					
						
							|  |  |  |   Pose2 base1, base2(1, 0, 0); | 
					
						
							|  |  |  |   Pose2 pose(1, 2, 0); | 
					
						
							|  |  |  |   Point2 point(4, 11); | 
					
						
							|  |  |  |   Vector2 expected(4 + 1 - 1 - 1, 11 - 2 - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Use the factor to calculate the Jacobians
 | 
					
						
							|  |  |  |   Matrix H1Actual, H2Actual, H3Actual, H4Actual; | 
					
						
							|  |  |  |   Vector actual = factor.evaluateError(base1, pose, base2, point, H1Actual, | 
					
						
							|  |  |  |       H2Actual, H3Actual, H4Actual); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, actual, 1e-9)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Use numerical derivatives to calculate the Jacobians
 | 
					
						
							|  |  |  |   Matrix H1Expected, H2Expected, H3Expected, H4Expected; | 
					
						
							| 
									
										
										
										
											2014-12-16 06:23:40 +08:00
										 |  |  |   H1Expected = numericalDerivative11<Vector2, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose, &base2, &point](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(pose_arg, pose, base2, point); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       base1); | 
					
						
							| 
									
										
										
										
											2014-12-16 06:23:40 +08:00
										 |  |  |   H2Expected = numericalDerivative11<Vector2, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &point, &base1, &base2](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(base1, pose_arg, base2, point); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       pose); | 
					
						
							| 
									
										
										
										
											2014-12-16 06:23:40 +08:00
										 |  |  |   H3Expected = numericalDerivative11<Vector2, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose, &base1, &point](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(base1, pose, pose_arg, point); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       base2); | 
					
						
							| 
									
										
										
										
											2014-12-16 06:23:40 +08:00
										 |  |  |   H4Expected = numericalDerivative11<Vector2, Point2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose, &base1, &base2](const Point2& point_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(base1, pose, base2, point_arg); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       point); | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Verify the Jacobians are correct
 | 
					
						
							|  |  |  |   EXPECT(assert_equal(H1Expected, H1Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H2Expected, H2Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H3Expected, H3Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H4Expected, H4Actual, 1e-9)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //*************************************************************************
 | 
					
						
							|  |  |  | TEST( OdometryFactorBase, all ) { | 
					
						
							|  |  |  |   // Create a factor
 | 
					
						
							|  |  |  |   Key b1(10), b2(20); | 
					
						
							|  |  |  |   Pose2 measurement(1, 1, 0); | 
					
						
							|  |  |  |   static SharedNoiseModel model(noiseModel::Unit::Create(2)); | 
					
						
							|  |  |  |   OdometryFactorBase factor(b1, i, b2, j, measurement, model); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Set the linearization pose2
 | 
					
						
							|  |  |  |   Pose2 base1, base2(1, 0, 0); | 
					
						
							|  |  |  |   Pose2 pose1(1, 2, 0), pose2(4, 11, 0); | 
					
						
							|  |  |  |   Vector3 expected(4 + 1 - 1 - 1, 11 - 2 - 1, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Use the factor to calculate the Jacobians
 | 
					
						
							|  |  |  |   Matrix H1Actual, H2Actual, H3Actual, H4Actual; | 
					
						
							|  |  |  |   Vector actual = factor.evaluateError(base1, pose1, base2, pose2, H1Actual, | 
					
						
							|  |  |  |       H2Actual, H3Actual, H4Actual); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, actual, 1e-9)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Use numerical derivatives to calculate the Jacobians
 | 
					
						
							|  |  |  |   Matrix H1Expected, H2Expected, H3Expected, H4Expected; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |   // using lambdas to replace bind
 | 
					
						
							| 
									
										
										
										
											2014-12-22 16:39:51 +08:00
										 |  |  |   H1Expected = numericalDerivative11<Vector3, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose1, &pose2, &base2](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(pose_arg, pose1, base2, pose2); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       base1); | 
					
						
							| 
									
										
										
										
											2014-12-22 16:39:51 +08:00
										 |  |  |   H2Expected = numericalDerivative11<Vector3, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose2, &base1, &base2](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(base1, pose_arg, base2, pose2); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       pose1); | 
					
						
							| 
									
										
										
										
											2014-12-22 16:39:51 +08:00
										 |  |  |   H3Expected = numericalDerivative11<Vector3, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose1, &base1, &pose2](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(base1, pose1, pose_arg, pose2); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       base2); | 
					
						
							| 
									
										
										
										
											2014-12-22 16:39:51 +08:00
										 |  |  |   H4Expected = numericalDerivative11<Vector3, Pose2>( | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       [&factor, &pose1, &base1, &base2](const Pose2& pose_arg) { | 
					
						
							|  |  |  |         return factor.evaluateError(base1, pose1, base2, pose_arg); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2014-05-05 22:14:36 +08:00
										 |  |  |       pose2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Verify the Jacobians are correct
 | 
					
						
							|  |  |  |   EXPECT(assert_equal(H1Expected, H1Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H2Expected, H2Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H3Expected, H3Actual, 1e-9)); | 
					
						
							|  |  |  |   EXPECT(assert_equal(H4Expected, H4Actual, 1e-9)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //*************************************************************************
 | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  |   TestResult tr; | 
					
						
							|  |  |  |   return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | //*************************************************************************
 | 
					
						
							|  |  |  | 
 |