| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * testInvDepthFactor.cpp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Created on: Apr 13, 2012 | 
					
						
							|  |  |  |  *      Author: cbeall3 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/numericalDerivative.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/SimpleCamera.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/geometry/InvDepthCamera3.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Cal3_S2::shared_ptr K(new Cal3_S2(1500, 1200, 0, 640, 480)); | 
					
						
							| 
									
										
										
										
											2016-01-27 15:09:58 +08:00
										 |  |  | Pose3 level_pose = Pose3(Rot3::Ypr(-M_PI/2, 0., -M_PI/2), gtsam::Point3(0,0,1)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | SimpleCamera level_camera(level_pose, *K); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( InvDepthFactor, Project1) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // landmark 5 meters infront of camera
 | 
					
						
							|  |  |  |   Point3 landmark(5, 0, 1); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Point2 expected_uv = level_camera.project(landmark); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose, K); | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 inv_landmark((Vector(5) << 1., 0., 1., 0., 0.).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./4); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Point2 actual_uv = inv_camera.project(inv_landmark, inv_depth); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:48:30 +08:00
										 |  |  |   EXPECT(assert_equal(expected_uv, actual_uv,1e-8)); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   EXPECT(assert_equal(Point2(640,480), actual_uv)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( InvDepthFactor, Project2) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // landmark 1m to the left and 1m up from camera
 | 
					
						
							|  |  |  |   // inv landmark xyz is same as camera xyz, so depth  actually doesn't matter
 | 
					
						
							|  |  |  |   Point3 landmark(1, 1, 2); | 
					
						
							|  |  |  |   Point2 expected = level_camera.project(landmark); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose, K); | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 diag_landmark((Vector(5) << 0., 0., 1., M_PI/4., atan(1.0/sqrt(2.0))).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1/sqrt(3.0)); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Point2 actual = inv_camera.project(diag_landmark, inv_depth); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, actual)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( InvDepthFactor, Project3) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // landmark 1m to the left and 1m up from camera
 | 
					
						
							|  |  |  |   // inv depth landmark xyz at origion
 | 
					
						
							|  |  |  |   Point3 landmark(1, 1, 2); | 
					
						
							|  |  |  |   Point2 expected = level_camera.project(landmark); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose, K); | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 diag_landmark((Vector(5) << 0., 0., 0., M_PI/4., atan(2./sqrt(2.0))).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth( 1./sqrt(1.0+1+4)); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Point2 actual = inv_camera.project(diag_landmark, inv_depth); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, actual)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( InvDepthFactor, Project4) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // landmark 4m to the left and 1m up from camera
 | 
					
						
							|  |  |  |   // inv depth landmark xyz at origion
 | 
					
						
							|  |  |  |   Point3 landmark(1, 4, 2); | 
					
						
							|  |  |  |   Point2 expected = level_camera.project(landmark); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose, K); | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 diag_landmark((Vector(5) << 0., 0., 0., atan(4.0/1), atan(2./sqrt(1.+16.))).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./sqrt(1.+16.+4.)); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Point2 actual = inv_camera.project(diag_landmark, inv_depth); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected, actual)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  | Point2 project_(const Pose3& pose, const Vector5& landmark, const double& inv_depth) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return InvDepthCamera3<Cal3_S2>(pose,K).project(landmark, inv_depth); } | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST( InvDepthFactor, Dproject_pose) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 landmark((Vector(5) << 0.1,0.2,0.3, 0.1,0.2).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./4); | 
					
						
							| 
									
										
										
										
											2014-10-22 00:50:52 +08:00
										 |  |  |   Matrix expected = numericalDerivative31(project_,level_pose, landmark, inv_depth); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose,K); | 
					
						
							|  |  |  |   Matrix actual; | 
					
						
							| 
									
										
										
										
											2014-11-04 22:48:30 +08:00
										 |  |  |   inv_camera.project(landmark, inv_depth, actual, boost::none, boost::none); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   EXPECT(assert_equal(expected,actual,1e-6)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( InvDepthFactor, Dproject_landmark) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 landmark((Vector(5) << 0.1,0.2,0.3, 0.1,0.2).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./4); | 
					
						
							| 
									
										
										
										
											2014-10-22 00:50:52 +08:00
										 |  |  |   Matrix expected = numericalDerivative32(project_,level_pose, landmark, inv_depth); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose,K); | 
					
						
							|  |  |  |   Matrix actual; | 
					
						
							| 
									
										
										
										
											2014-11-04 22:48:30 +08:00
										 |  |  |   inv_camera.project(landmark, inv_depth, boost::none, actual, boost::none); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   EXPECT(assert_equal(expected,actual,1e-7)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST( InvDepthFactor, Dproject_inv_depth) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector5 landmark((Vector(5) << 0.1,0.2,0.3, 0.1,0.2).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./4); | 
					
						
							| 
									
										
										
										
											2014-10-22 00:50:52 +08:00
										 |  |  |   Matrix expected = numericalDerivative33(project_,level_pose, landmark, inv_depth); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose,K); | 
					
						
							|  |  |  |   Matrix actual; | 
					
						
							| 
									
										
										
										
											2014-11-04 22:48:30 +08:00
										 |  |  |   inv_camera.project(landmark, inv_depth, boost::none, boost::none, actual); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   EXPECT(assert_equal(expected,actual,1e-7)); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(InvDepthFactor, backproject) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector expected((Vector(5) << 0.,0.,1., 0.1,0.2).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./4); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(level_pose,K); | 
					
						
							|  |  |  |   Point2 z = inv_camera.project(expected, inv_depth); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 07:32:59 +08:00
										 |  |  |   Vector5 actual_vec; | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double actual_inv; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   boost::tie(actual_vec, actual_inv) = inv_camera.backproject(z, 4); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected,actual_vec,1e-7)); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   EXPECT_DOUBLES_EQUAL(inv_depth,actual_inv,1e-7); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(InvDepthFactor, backproject2) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // backwards facing camera
 | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |   Vector expected((Vector(5) << -5.,-5.,2., 3., -0.1).finished()); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double inv_depth(1./10); | 
					
						
							| 
									
										
										
										
											2016-01-27 15:09:58 +08:00
										 |  |  |   InvDepthCamera3<Cal3_S2> inv_camera(Pose3(Rot3::Ypr(1.5,0.1, -1.5), Point3(-5, -5, 2)),K); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   Point2 z = inv_camera.project(expected, inv_depth); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 07:32:59 +08:00
										 |  |  |   Vector5 actual_vec; | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   double actual_inv; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   boost::tie(actual_vec, actual_inv) = inv_camera.backproject(z, 10); | 
					
						
							|  |  |  |   EXPECT(assert_equal(expected,actual_vec,1e-7)); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   EXPECT_DOUBLES_EQUAL(inv_depth,actual_inv,1e-7); | 
					
						
							| 
									
										
										
										
											2012-08-03 04:47:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr);} | 
					
						
							|  |  |  | /* ************************************************************************* */ |