| 
									
										
										
										
											2013-08-03 07:35:39 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |  *  @file  TestSmartProjectionFactor.cpp | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  |  *  @brief Unit tests for ProjectionFactor Class | 
					
						
							|  |  |  |  *  @author Frank Dellaert | 
					
						
							|  |  |  |  *  @date Nov 2009 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/nonlinear/ConcurrentBatchFilter.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/PriorFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/BetweenFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/slam/ProjectionFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/slam/SmartProjectionFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/ISAM2.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-13 05:44:54 +08:00
										 |  |  | #include <gtsam/nonlinear/GaussNewtonOptimizer.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/LinearContainerFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Ordering.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Values.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Symbol.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Key.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianSequentialSolver.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/JunctionTree.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/geometry/triangulation.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Pose3.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Point3.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Point2.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Cal3DS2.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Cal3_S2.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/SimpleCamera.h>
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // make a realistic calibration matrix
 | 
					
						
							|  |  |  | static double fov = 60; // degrees
 | 
					
						
							|  |  |  | static size_t w=640,h=480; | 
					
						
							|  |  |  | static Cal3_S2::shared_ptr K(new Cal3_S2(fov,w,h)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Create a noise model for the pixel error
 | 
					
						
							|  |  |  | static SharedNoiseModel model(noiseModel::Unit::Create(2)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Convenience for named keys
 | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  | using symbol_shorthand::X; | 
					
						
							|  |  |  | using symbol_shorthand::L; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | typedef SmartProjectionFactor<Pose3, Point3> TestSmartProjectionFactor; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, Constructor) { | 
					
						
							|  |  |  |   Key poseKey(X(1)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += poseKey; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::vector<Point2> measurements; | 
					
						
							|  |  |  |   measurements.push_back(Point2(323.0, 240.0)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   TestSmartProjectionFactor factor(measurements, model, views, K); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, ConstructorWithTransform) { | 
					
						
							|  |  |  |   Key poseKey(X(1)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += poseKey; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::vector<Point2> measurements; | 
					
						
							|  |  |  |   measurements.push_back(Point2(323.0, 240.0)); | 
					
						
							|  |  |  |   Pose3 body_P_sensor(Rot3::RzRyRx(-M_PI_2, 0.0, -M_PI_2), Point3(0.25, -0.10, 1.0)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 05:44:54 +08:00
										 |  |  |   TestSmartProjectionFactor factor(measurements, model, views, K, boost::none, body_P_sensor); | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, Equals ) { | 
					
						
							|  |  |  |   // Create two identical factors and make sure they're equal
 | 
					
						
							|  |  |  |   std::vector<Point2> measurements; | 
					
						
							|  |  |  |   measurements.push_back(Point2(323.0, 240.0)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += X(1); | 
					
						
							|  |  |  |   TestSmartProjectionFactor factor1(measurements, model, views, K); | 
					
						
							|  |  |  |   TestSmartProjectionFactor factor2(measurements, model, views, K); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   CHECK(assert_equal(factor1, factor2)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, EqualsWithTransform ) { | 
					
						
							|  |  |  |   // Create two identical factors and make sure they're equal
 | 
					
						
							|  |  |  |   std::vector<Point2> measurements; | 
					
						
							|  |  |  |   measurements.push_back(Point2(323.0, 240.0)); | 
					
						
							|  |  |  |   Pose3 body_P_sensor(Rot3::RzRyRx(-M_PI_2, 0.0, -M_PI_2), Point3(0.25, -0.10, 1.0)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += X(1); | 
					
						
							| 
									
										
										
										
											2013-08-13 05:44:54 +08:00
										 |  |  |   TestSmartProjectionFactor factor1(measurements, model, views, K, boost::none, body_P_sensor); | 
					
						
							|  |  |  |   TestSmartProjectionFactor factor2(measurements, model, views, K, boost::none, body_P_sensor); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   CHECK(assert_equal(factor1, factor2)); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, noisy ){ | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  |   cout << " ************************ MultiProjectionFactor: noisy ****************************" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  |   Symbol x1('X',  1); | 
					
						
							|  |  |  |   Symbol x2('X',  2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noiseProjection = noiseModel::Isotropic::Sigma(2, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += x1, x2; //, x3;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Cal3_S2::shared_ptr K(new Cal3_S2(1500, 1200, 0, 640, 480)); | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  |   // create first camera. Looking along X-axis, 1 meter above ground plane (x-y)
 | 
					
						
							|  |  |  |   Pose3 level_pose = Pose3(Rot3::ypr(-M_PI/2, 0., -M_PI/2), gtsam::Point3(0,0,1)); | 
					
						
							|  |  |  |   SimpleCamera level_camera(level_pose, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create second camera 1 meter to the right of first camera
 | 
					
						
							|  |  |  |   Pose3 level_pose_right = level_pose * Pose3(Rot3(), Point3(1,0,0)); | 
					
						
							|  |  |  |   SimpleCamera level_camera_right(level_pose_right, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // landmark ~5 meters infront of camera
 | 
					
						
							|  |  |  |   Point3 landmark(5, 0.5, 1.2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 1. Project two landmarks into two cameras and triangulate
 | 
					
						
							|  |  |  |   Point2 pixelError(0.2,0.2); | 
					
						
							|  |  |  |   Point2 level_uv = level_camera.project(landmark) + pixelError; | 
					
						
							|  |  |  |   Point2 level_uv_right = level_camera_right.project(landmark); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Values values; | 
					
						
							|  |  |  |   values.insert(x1, level_pose); | 
					
						
							|  |  |  |   Pose3 noise_pose = Pose3(Rot3::ypr(-M_PI/10, 0., -M_PI/10), gtsam::Point3(0.5,0.1,0.3)); | 
					
						
							|  |  |  |   values.insert(x2, level_pose_right.compose(noise_pose)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   vector<Point2> measurements; | 
					
						
							|  |  |  |   measurements += level_uv, level_uv_right; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SmartProjectionFactor<Pose3, Point3, Cal3_S2>::shared_ptr | 
					
						
							|  |  |  |     smartFactor(new SmartProjectionFactor<Pose3, Point3, Cal3_S2>(measurements, noiseProjection, views, K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   double actualError = smartFactor->error(values); | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   std::cout << "Error: " << actualError << std::endl; | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // we do not expect to be able to predict the error, since the error on the pixel will change
 | 
					
						
							|  |  |  |   // the triangulation of the landmark which is internal to the factor.
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   // DOUBLES_EQUAL(expectedError, actualError, 1e-7);
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 05:44:54 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, 3poses_1iteration_projection_factor_comparison ){ | 
					
						
							|  |  |  |   cout << " ************************ SmartProjectionFactor: 3 cams + 3 landmarks, 1 iteration, comparison b/w Generic and Smart Projection Factors **********************" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Symbol x1('X',  1); | 
					
						
							|  |  |  |   Symbol x2('X',  2); | 
					
						
							|  |  |  |   Symbol x3('X',  3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noiseProjection = noiseModel::Isotropic::Sigma(2, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += x1, x2, x3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Cal3_S2::shared_ptr K(new Cal3_S2(1500, 1200, 0, 640, 480)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create first camera. Looking along X-axis, 1 meter above ground plane (x-y)
 | 
					
						
							|  |  |  |   Pose3 pose1 = Pose3(Rot3::ypr(-M_PI/2, 0., -M_PI/2), gtsam::Point3(0,0,1)); | 
					
						
							|  |  |  |   SimpleCamera cam1(pose1, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create second camera 1 meter to the right of first camera
 | 
					
						
							|  |  |  |   Pose3 pose2 = pose1 * Pose3(Rot3(), Point3(1,0,0)); | 
					
						
							|  |  |  |   SimpleCamera cam2(pose2, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create third camera 1 meter above the first camera
 | 
					
						
							|  |  |  |   Pose3 pose3 = pose1 * Pose3(Rot3(), Point3(0,-1,0)); | 
					
						
							|  |  |  |   SimpleCamera cam3(pose3, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // three landmarks ~5 meters infront of camera
 | 
					
						
							|  |  |  |   Point3 landmark1(5, 0.5, 1.2); | 
					
						
							|  |  |  |   Point3 landmark2(5, -0.5, 1.2); | 
					
						
							|  |  |  |   Point3 landmark3(3, 0, 3.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   vector<Point2> measurements_cam1, measurements_cam2, measurements_cam3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 1. Project three landmarks into three cameras and triangulate
 | 
					
						
							|  |  |  |   Point2 cam1_uv1 = cam1.project(landmark1); | 
					
						
							|  |  |  |   Point2 cam2_uv1 = cam2.project(landmark1); | 
					
						
							|  |  |  |   Point2 cam3_uv1 = cam3.project(landmark1); | 
					
						
							|  |  |  |   measurements_cam1 += cam1_uv1, cam2_uv1, cam3_uv1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   Point2 cam1_uv2 = cam1.project(landmark2); | 
					
						
							|  |  |  |   Point2 cam2_uv2 = cam2.project(landmark2); | 
					
						
							|  |  |  |   Point2 cam3_uv2 = cam3.project(landmark2); | 
					
						
							|  |  |  |   measurements_cam2 += cam1_uv2, cam2_uv2, cam3_uv2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Point2 cam1_uv3 = cam1.project(landmark3); | 
					
						
							|  |  |  |   Point2 cam2_uv3 = cam2.project(landmark3); | 
					
						
							|  |  |  |   Point2 cam3_uv3 = cam3.project(landmark3); | 
					
						
							|  |  |  |   measurements_cam3 += cam1_uv3, cam2_uv3, cam3_uv3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef SmartProjectionFactor<Pose3, Point3, Cal3_S2> SmartFactor; | 
					
						
							|  |  |  |   typedef GenericProjectionFactor<Pose3, Point3> ProjectionFactor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SmartFactor::shared_ptr smartFactor1(new SmartFactor(measurements_cam1, noiseProjection, views, K, boost::make_optional<Point3>(landmark1) )); | 
					
						
							|  |  |  |   SmartFactor::shared_ptr smartFactor2(new SmartFactor(measurements_cam2, noiseProjection, views, K, boost::make_optional<Point3>(landmark2) )); | 
					
						
							|  |  |  |   SmartFactor::shared_ptr smartFactor3(new SmartFactor(measurements_cam3, noiseProjection, views, K, boost::make_optional<Point3>(landmark3) )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   NonlinearFactorGraph graphWithOriginalFactor; | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam1.project(landmark1), noiseProjection, x1, L(1), K)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam2.project(landmark1), noiseProjection, x2, L(1), K)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam3.project(landmark1), noiseProjection, x3, L(1), K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam1.project(landmark2), noiseProjection, x1, L(2), K)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam2.project(landmark2), noiseProjection, x2, L(2), K)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam3.project(landmark2), noiseProjection, x3, L(2), K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam1.project(landmark3), noiseProjection, x1, L(3), K)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam2.project(landmark3), noiseProjection, x2, L(3), K)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(ProjectionFactor(cam3.project(landmark3), noiseProjection, x3, L(3), K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(PriorFactor<Pose3>(x1, pose1, noisePrior)); | 
					
						
							|  |  |  |   graphWithOriginalFactor.add(PriorFactor<Pose3>(x2, pose2, noisePrior)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Pose3 noise_pose = Pose3(Rot3::ypr(-M_PI/10, 0., -M_PI/10), gtsam::Point3(0.5,0.1,0.3)); | 
					
						
							|  |  |  |   Values valuesOriginalFactor; | 
					
						
							|  |  |  |   valuesOriginalFactor.insert(x1, pose1); | 
					
						
							|  |  |  |   valuesOriginalFactor.insert(x2, pose2); | 
					
						
							|  |  |  |   valuesOriginalFactor.insert(x3, pose3* noise_pose); | 
					
						
							|  |  |  |   valuesOriginalFactor.insert(L(1), landmark1); | 
					
						
							|  |  |  |   valuesOriginalFactor.insert(L(2), landmark2); | 
					
						
							|  |  |  |   valuesOriginalFactor.insert(L(3), landmark3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   NonlinearFactorGraph graphWithSmartFactor; | 
					
						
							|  |  |  |   graphWithSmartFactor.push_back(smartFactor1); | 
					
						
							|  |  |  |   graphWithSmartFactor.push_back(smartFactor2); | 
					
						
							|  |  |  |   graphWithSmartFactor.push_back(smartFactor3); | 
					
						
							|  |  |  |   graphWithSmartFactor.add(PriorFactor<Pose3>(x1, pose1, noisePrior)); | 
					
						
							|  |  |  |   graphWithSmartFactor.add(PriorFactor<Pose3>(x2, pose2, noisePrior)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Values valuesSmartFactor; | 
					
						
							|  |  |  |   valuesSmartFactor.insert(x1, pose1); | 
					
						
							|  |  |  |   valuesSmartFactor.insert(x2, pose2); | 
					
						
							|  |  |  |   // initialize third pose with some noise, we expect it to move back to original pose3
 | 
					
						
							|  |  |  |   valuesSmartFactor.insert(x3, pose3*noise_pose); | 
					
						
							|  |  |  |   valuesSmartFactor.at<Pose3>(x3).print("Pose3 before optimization: "); | 
					
						
							|  |  |  |   pose3.print("Pose3 ground truth: "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   LevenbergMarquardtParams params; | 
					
						
							|  |  |  |   params.maxIterations = 1; | 
					
						
							|  |  |  |   params.verbosityLM = LevenbergMarquardtParams::TRYLAMBDA; | 
					
						
							|  |  |  |   params.verbosity = NonlinearOptimizerParams::ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Values resultWithOriginalFactor; | 
					
						
							|  |  |  |   std::cout << "\n=========================================" << std::endl; | 
					
						
							|  |  |  |   std::cout << "Optimizing GenericProjectionFactor" << std::endl; | 
					
						
							|  |  |  |   LevenbergMarquardtOptimizer optimizerForOriginalFactor(graphWithOriginalFactor, valuesOriginalFactor, params); | 
					
						
							|  |  |  |   resultWithOriginalFactor = optimizerForOriginalFactor.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Values resultWithSmartFactor; | 
					
						
							|  |  |  |   std::cout << "\n=========================================" << std::endl; | 
					
						
							|  |  |  |   std::cout << "Optimizing SmartProjectionfactor" << std::endl; | 
					
						
							|  |  |  |   LevenbergMarquardtOptimizer optimizerForSmartFactor(graphWithSmartFactor, valuesSmartFactor, params); | 
					
						
							|  |  |  |   resultWithSmartFactor = optimizerForSmartFactor.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::cout << "\n=========================================" << std::endl; | 
					
						
							|  |  |  |   // result.print("results of 3 camera, 3 landmark optimization \n");
 | 
					
						
							|  |  |  |   resultWithOriginalFactor.at<Pose3>(x3).print("Original: Pose3 after optimization: "); | 
					
						
							|  |  |  |   resultWithSmartFactor.at<Pose3>(x3).print("\nSmart: Pose3 after optimization: "); | 
					
						
							|  |  |  |   EXPECT(assert_equal(resultWithOriginalFactor.at<Pose3>(x3),resultWithSmartFactor.at<Pose3>(x3))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GaussNewtonParams params2; | 
					
						
							|  |  |  |   params2.maxIterations = 1; | 
					
						
							|  |  |  |   Values resultWithOriginalFactor2; | 
					
						
							|  |  |  |   GaussNewtonOptimizer optimizerForOriginalFactor2(graphWithOriginalFactor, valuesOriginalFactor, params2); | 
					
						
							|  |  |  |   resultWithOriginalFactor2 = optimizerForOriginalFactor2.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Values resultWithSmartFactor2; | 
					
						
							|  |  |  |   GaussNewtonOptimizer optimizerForSmartFactor2(graphWithSmartFactor, valuesSmartFactor, params2); | 
					
						
							|  |  |  |   resultWithSmartFactor2 = optimizerForSmartFactor2.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   resultWithOriginalFactor2.at<Pose3>(x3).print("Original: Pose3 after optimization (GaussNewton): "); | 
					
						
							|  |  |  |   resultWithSmartFactor2.at<Pose3>(x3).print("\nSmart: Pose3 after optimization (GaussNewton): "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  | TEST( SmartProjectionFactor, 3poses_smart_projection_factor ){ | 
					
						
							|  |  |  |   cout << " ************************ SmartProjectionFactor: 3 cams + 3 landmarks **********************" << endl; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Symbol x1('X',  1); | 
					
						
							|  |  |  |   Symbol x2('X',  2); | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  |   Symbol x3('X',  3); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noiseProjection = noiseModel::Isotropic::Sigma(2, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<Key> views; | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   views += x1, x2, x3; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Cal3_S2::shared_ptr K(new Cal3_S2(1500, 1200, 0, 640, 480)); | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  |   // create first camera. Looking along X-axis, 1 meter above ground plane (x-y)
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   Pose3 pose1 = Pose3(Rot3::ypr(-M_PI/2, 0., -M_PI/2), gtsam::Point3(0,0,1)); | 
					
						
							|  |  |  |   SimpleCamera cam1(pose1, *K); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create second camera 1 meter to the right of first camera
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   Pose3 pose2 = pose1 * Pose3(Rot3(), Point3(1,0,0)); | 
					
						
							|  |  |  |   SimpleCamera cam2(pose2, *K); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   // create third camera 1 meter above the first camera
 | 
					
						
							|  |  |  |   Pose3 pose3 = pose1 * Pose3(Rot3(), Point3(0,-1,0)); | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  |   SimpleCamera cam3(pose3, *K); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   // three landmarks ~5 meters infront of camera
 | 
					
						
							|  |  |  |   Point3 landmark1(5, 0.5, 1.2); | 
					
						
							|  |  |  |   Point3 landmark2(5, -0.5, 1.2); | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  |   Point3 landmark3(3, 0, 3.0); | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   vector<Point2> measurements_cam1, measurements_cam2, measurements_cam3; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   // 1. Project three landmarks into three cameras and triangulate
 | 
					
						
							|  |  |  |   Point2 cam1_uv1 = cam1.project(landmark1); | 
					
						
							|  |  |  |   Point2 cam2_uv1 = cam2.project(landmark1); | 
					
						
							|  |  |  |   Point2 cam3_uv1 = cam3.project(landmark1); | 
					
						
							|  |  |  |   measurements_cam1 += cam1_uv1, cam2_uv1, cam3_uv1; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   //
 | 
					
						
							|  |  |  |   Point2 cam1_uv2 = cam1.project(landmark2); | 
					
						
							|  |  |  |   Point2 cam2_uv2 = cam2.project(landmark2); | 
					
						
							|  |  |  |   Point2 cam3_uv2 = cam3.project(landmark2); | 
					
						
							|  |  |  |   measurements_cam2 += cam1_uv2, cam2_uv2, cam3_uv2; | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Point2 cam1_uv3 = cam1.project(landmark3); | 
					
						
							|  |  |  |   Point2 cam2_uv3 = cam2.project(landmark3); | 
					
						
							|  |  |  |   Point2 cam3_uv3 = cam3.project(landmark3); | 
					
						
							|  |  |  |   measurements_cam3 += cam1_uv3, cam2_uv3, cam3_uv3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef SmartProjectionFactor<Pose3, Point3, Cal3_S2> SmartFactor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SmartFactor::shared_ptr smartFactor1(new SmartFactor(measurements_cam1, noiseProjection, views, K)); | 
					
						
							|  |  |  |   SmartFactor::shared_ptr smartFactor2(new SmartFactor(measurements_cam2, noiseProjection, views, K)); | 
					
						
							|  |  |  |   SmartFactor::shared_ptr smartFactor3(new SmartFactor(measurements_cam3, noiseProjection, views, K)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  |   const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   NonlinearFactorGraph graph; | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   graph.push_back(smartFactor1); | 
					
						
							|  |  |  |   graph.push_back(smartFactor2); | 
					
						
							|  |  |  |   graph.push_back(smartFactor3); | 
					
						
							|  |  |  |   graph.add(PriorFactor<Pose3>(x1, pose1, noisePrior)); | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  |   graph.add(PriorFactor<Pose3>(x2, pose2, noisePrior)); | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  | //  Pose3 noise_pose = Pose3(Rot3::ypr(-M_PI/10, 0., -M_PI/10), gtsam::Point3(0.5,0.1,0.3)); // noise from regular projection factor test below
 | 
					
						
							|  |  |  |   Pose3 noise_pose = Pose3(Rot3::ypr(-M_PI/100, 0., -M_PI/100), gtsam::Point3(0.1,0.1,0.1)); // smaller noise
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   Values values; | 
					
						
							|  |  |  |   values.insert(x1, pose1); | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  |   values.insert(x2, pose2); | 
					
						
							|  |  |  |   // initialize third pose with some noise, we expect it to move back to original pose3
 | 
					
						
							|  |  |  |   values.insert(x3, pose3*noise_pose); | 
					
						
							|  |  |  |   values.at<Pose3>(x3).print("Smart: Pose3 before optimization: "); | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   LevenbergMarquardtParams params; | 
					
						
							|  |  |  |   params.verbosityLM = LevenbergMarquardtParams::TRYLAMBDA; | 
					
						
							|  |  |  |   params.verbosity = NonlinearOptimizerParams::ERROR; | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Values result; | 
					
						
							|  |  |  |   gttic_(SmartProjectionFactor); | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  |   LevenbergMarquardtOptimizer optimizer(graph, values, params); | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   result = optimizer.optimize(); | 
					
						
							|  |  |  |   gttoc_(SmartProjectionFactor); | 
					
						
							|  |  |  |   tictoc_finishedIteration_(); | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  |   // result.print("results of 3 camera, 3 landmark optimization \n");
 | 
					
						
							|  |  |  |   result.at<Pose3>(x3).print("Smart: Pose3 after optimization: "); | 
					
						
							|  |  |  |   EXPECT(assert_equal(pose3,result.at<Pose3>(x3))); | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  |   tictoc_print_(); | 
					
						
							| 
									
										
										
										
											2013-08-05 23:50:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 23:09:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 07:32:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, 3poses_projection_factor ){ | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  | //  cout << " ************************ Normal ProjectionFactor: 3 cams + 3 landmarks **********************" << endl;
 | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Symbol x1('X',  1); | 
					
						
							|  |  |  |   Symbol x2('X',  2); | 
					
						
							|  |  |  |   Symbol x3('X',  3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noiseProjection = noiseModel::Isotropic::Sigma(2, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += x1, x2, x3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Cal3_S2::shared_ptr K(new Cal3_S2(1500, 1200, 0, 640, 480)); | 
					
						
							|  |  |  |   // create first camera. Looking along X-axis, 1 meter above ground plane (x-y)
 | 
					
						
							|  |  |  |   Pose3 pose1 = Pose3(Rot3::ypr(-M_PI/2, 0., -M_PI/2), gtsam::Point3(0,0,1)); | 
					
						
							|  |  |  |   SimpleCamera cam1(pose1, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create second camera 1 meter to the right of first camera
 | 
					
						
							|  |  |  |   Pose3 pose2 = pose1 * Pose3(Rot3(), Point3(1,0,0)); | 
					
						
							|  |  |  |   SimpleCamera cam2(pose2, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create third camera 1 meter above the first camera
 | 
					
						
							|  |  |  |   Pose3 pose3 = pose1 * Pose3(Rot3(), Point3(0,-1,0)); | 
					
						
							|  |  |  |   SimpleCamera cam3(pose3, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // three landmarks ~5 meters infront of camera
 | 
					
						
							|  |  |  |   Point3 landmark1(5, 0.5, 1.2); | 
					
						
							|  |  |  |   Point3 landmark2(5, -0.5, 1.2); | 
					
						
							|  |  |  |   Point3 landmark3(3, 0, 3.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef GenericProjectionFactor<Pose3, Point3> ProjectionFactor; | 
					
						
							|  |  |  |   NonlinearFactorGraph graph; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 1. Project three landmarks into three cameras and triangulate
 | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam1.project(landmark1), noiseProjection, x1, L(1), K)); | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam2.project(landmark1), noiseProjection, x2, L(1), K)); | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam3.project(landmark1), noiseProjection, x3, L(1), K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam1.project(landmark2), noiseProjection, x1, L(2), K)); | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam2.project(landmark2), noiseProjection, x2, L(2), K)); | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam3.project(landmark2), noiseProjection, x3, L(2), K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam1.project(landmark3), noiseProjection, x1, L(3), K)); | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam2.project(landmark3), noiseProjection, x2, L(3), K)); | 
					
						
							|  |  |  |   graph.add(ProjectionFactor(cam3.project(landmark3), noiseProjection, x3, L(3), K)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10); | 
					
						
							|  |  |  |   graph.add(PriorFactor<Pose3>(x1, pose1, noisePrior)); | 
					
						
							|  |  |  |   graph.add(PriorFactor<Pose3>(x2, pose2, noisePrior)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Pose3 noise_pose = Pose3(Rot3::ypr(-M_PI/10, 0., -M_PI/10), gtsam::Point3(0.5,0.1,0.3)); | 
					
						
							|  |  |  |   Values values; | 
					
						
							|  |  |  |   values.insert(x1, pose1); | 
					
						
							|  |  |  |   values.insert(x2, pose2); | 
					
						
							|  |  |  |   values.insert(x3, pose3* noise_pose); | 
					
						
							|  |  |  |   values.insert(L(1), landmark1); | 
					
						
							|  |  |  |   values.insert(L(2), landmark2); | 
					
						
							|  |  |  |   values.insert(L(3), landmark3); | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  | //  values.at<Pose3>(x3).print("Pose3 before optimization: ");
 | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   LevenbergMarquardtParams params; | 
					
						
							|  |  |  | //  params.verbosityLM = LevenbergMarquardtParams::TRYLAMBDA;
 | 
					
						
							|  |  |  | //  params.verbosity = NonlinearOptimizerParams::ERROR;
 | 
					
						
							|  |  |  |   LevenbergMarquardtOptimizer optimizer(graph, values, params); | 
					
						
							|  |  |  |   Values result = optimizer.optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  | //  result.at<Pose3>(x3).print("Pose3 after optimization: ");
 | 
					
						
							|  |  |  |   EXPECT(assert_equal(pose3,result.at<Pose3>(x3))); | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 22:00:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 05:44:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-08 22:00:26 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( SmartProjectionFactor, Hessian ){ | 
					
						
							| 
									
										
										
										
											2013-08-12 22:40:57 +08:00
										 |  |  |   cout << " ************************ SmartProjectionFactor: Hessian **********************" << endl; | 
					
						
							| 
									
										
										
										
											2013-08-08 22:00:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Symbol x1('X',  1); | 
					
						
							|  |  |  |   Symbol x2('X',  2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const SharedDiagonal noiseProjection = noiseModel::Isotropic::Sigma(2, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<Key> views; | 
					
						
							|  |  |  |   views += x1, x2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Cal3_S2::shared_ptr K(new Cal3_S2(1500, 1200, 0, 640, 480)); | 
					
						
							|  |  |  |   // create first camera. Looking along X-axis, 1 meter above ground plane (x-y)
 | 
					
						
							|  |  |  |   Pose3 pose1 = Pose3(Rot3::ypr(-M_PI/2, 0., -M_PI/2), gtsam::Point3(0,0,1)); | 
					
						
							|  |  |  |   SimpleCamera cam1(pose1, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create second camera 1 meter to the right of first camera
 | 
					
						
							|  |  |  |   Pose3 pose2 = pose1 * Pose3(Rot3(), Point3(1,0,0)); | 
					
						
							|  |  |  |   SimpleCamera cam2(pose2, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // three landmarks ~5 meters infront of camera
 | 
					
						
							|  |  |  |   Point3 landmark1(5, 0.5, 1.2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 1. Project three landmarks into three cameras and triangulate
 | 
					
						
							|  |  |  |   Point2 cam1_uv1 = cam1.project(landmark1); | 
					
						
							|  |  |  |   Point2 cam2_uv1 = cam2.project(landmark1); | 
					
						
							|  |  |  |   vector<Point2> measurements_cam1; | 
					
						
							|  |  |  |   measurements_cam1 += cam1_uv1, cam2_uv1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SmartProjectionFactor<Pose3, Point3, Cal3_S2> smartFactor(measurements_cam1, noiseProjection, views, K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Pose3 noise_pose = Pose3(Rot3::ypr(-M_PI/10, 0., -M_PI/10), gtsam::Point3(0.5,0.1,0.3)); | 
					
						
							|  |  |  |   Values values; | 
					
						
							|  |  |  |   values.insert(x1, pose1); | 
					
						
							|  |  |  |   values.insert(x2, pose2); | 
					
						
							|  |  |  |   //  values.insert(L(1), landmark1);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Ordering ordering; | 
					
						
							|  |  |  |   ordering.push_back(x1); | 
					
						
							|  |  |  |   ordering.push_back(x2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   boost::shared_ptr<GaussianFactor> hessianFactor = smartFactor.linearize(values, ordering); | 
					
						
							|  |  |  |   hessianFactor->print("Hessian factor \n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // compute triangulation from linearization point
 | 
					
						
							| 
									
										
										
										
											2013-08-08 22:30:26 +08:00
										 |  |  |   // compute reprojection errors (sum squared)
 | 
					
						
							| 
									
										
										
										
											2013-08-08 22:00:26 +08:00
										 |  |  |   // compare with hessianFactor.info(): the bottom right element is the squared sum of the reprojection errors (normalized by the covariance)
 | 
					
						
							| 
									
										
										
										
											2013-08-08 22:30:26 +08:00
										 |  |  |   // check that it is correctly scaled when using noiseProjection = [1/4  0; 0 1/4]
 | 
					
						
							| 
									
										
										
										
											2013-08-08 22:00:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 01:58:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-03 07:35:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr); } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 |