diff --git a/gtsam.h b/gtsam.h index 1aee996dc..33e95b558 100644 --- a/gtsam.h +++ b/gtsam.h @@ -778,7 +778,7 @@ class CalibratedCamera { // Action on Point3 gtsam::Point2 project(const gtsam::Point3& point) const; - static gtsam::Point2 project_to_camera(const gtsam::Point3& cameraPoint); + static gtsam::Point2 Project(const gtsam::Point3& cameraPoint); // Standard Interface gtsam::Pose3 pose() const; @@ -815,7 +815,7 @@ class SimpleCamera { static size_t Dim(); // Transformations and measurement functions - static gtsam::Point2 project_to_camera(const gtsam::Point3& cameraPoint); + static gtsam::Point2 Project(const gtsam::Point3& cameraPoint); pair projectSafe(const gtsam::Point3& pw) const; gtsam::Point2 project(const gtsam::Point3& point); gtsam::Point3 backproject(const gtsam::Point2& p, double depth) const; @@ -854,7 +854,7 @@ class PinholeCamera { static size_t Dim(); // Transformations and measurement functions - static gtsam::Point2 project_to_camera(const gtsam::Point3& cameraPoint); + static gtsam::Point2 Project(const gtsam::Point3& cameraPoint); pair projectSafe(const gtsam::Point3& pw) const; gtsam::Point2 project(const gtsam::Point3& point); gtsam::Point3 backproject(const gtsam::Point2& p, double depth) const; diff --git a/gtsam/geometry/CalibratedCamera.cpp b/gtsam/geometry/CalibratedCamera.cpp index fb518f2e3..6fbef721c 100644 --- a/gtsam/geometry/CalibratedCamera.cpp +++ b/gtsam/geometry/CalibratedCamera.cpp @@ -85,8 +85,7 @@ const Pose3& PinholeBase::getPose(OptionalJacobian<6, 6> H) const { } /* ************************************************************************* */ -Point2 PinholeBase::project_to_camera(const Point3& pc, - OptionalJacobian<2, 3> Dpoint) { +Point2 PinholeBase::Project(const Point3& pc, OptionalJacobian<2, 3> Dpoint) { double d = 1.0 / pc.z(); const double u = pc.x() * d, v = pc.y() * d; if (Dpoint) @@ -95,22 +94,27 @@ Point2 PinholeBase::project_to_camera(const Point3& pc, } /* ************************************************************************* */ -Point2 PinholeBase::project_to_camera(const Unit3& pc, - OptionalJacobian<2, 2> Dpoint) { +Point2 PinholeBase::project_to_camera_old(const Point3& pc, + OptionalJacobian<2, 3> Dpoint) { + return Project(pc); +} + +/* ************************************************************************* */ +Point2 PinholeBase::Project(const Unit3& pc, OptionalJacobian<2, 2> Dpoint) { if (Dpoint) { Matrix32 Dpoint3_pc; Matrix23 Duv_point3; - Point2 uv = project_to_camera(pc.point3(Dpoint3_pc), Duv_point3); + Point2 uv = Project(pc.point3(Dpoint3_pc), Duv_point3); *Dpoint = Duv_point3 * Dpoint3_pc; return uv; } else - return project_to_camera(pc.point3()); + return Project(pc.point3()); } /* ************************************************************************* */ pair PinholeBase::projectSafe(const Point3& pw) const { const Point3 pc = pose().transform_to(pw); - const Point2 pn = project_to_camera(pc); + const Point2 pn = Project(pc); return make_pair(pn, pc.z() > 0); } @@ -124,7 +128,7 @@ Point2 PinholeBase::project2(const Point3& point, OptionalJacobian<2, 6> Dpose, if (q.z() <= 0) throw CheiralityException(); #endif - const Point2 pn = project_to_camera(q); + const Point2 pn = Project(q); if (Dpose || Dpoint) { const double d = 1.0 / q.z(); @@ -148,8 +152,7 @@ Point2 PinholeBase::project2(const Unit3& pw, OptionalJacobian<2, 6> Dpose, // camera to normalized image coordinate Matrix2 Dpn_pc; - const Point2 pn = PinholeBase::project_to_camera(pc, - Dpose || Dpoint ? &Dpn_pc : 0); + const Point2 pn = Project(pc, Dpose || Dpoint ? &Dpn_pc : 0); // chain the Jacobian matrices if (Dpose) { diff --git a/gtsam/geometry/CalibratedCamera.h b/gtsam/geometry/CalibratedCamera.h index e43cafcc2..b4513a2a3 100644 --- a/gtsam/geometry/CalibratedCamera.h +++ b/gtsam/geometry/CalibratedCamera.h @@ -164,7 +164,11 @@ public: * Does *not* throw a CheiralityException, even if pc behind image plane * @param pc point in camera coordinates */ - static Point2 project_to_camera(const Point3& pc, // + static Point2 Project(const Point3& pc, // + OptionalJacobian<2, 3> Dpoint = boost::none); + + /// @deprecated not correct naming for static function, use Project above + static Point2 project_to_camera_old(const Point3& pc, // OptionalJacobian<2, 3> Dpoint = boost::none); /** @@ -172,7 +176,7 @@ public: * Does *not* throw a CheiralityException, even if pc behind image plane * @param pc point in camera coordinates */ - static Point2 project_to_camera(const Unit3& pc, // + static Point2 Project(const Unit3& pc, // OptionalJacobian<2, 2> Dpoint = boost::none); /// Project a point into the image and check depth @@ -193,8 +197,9 @@ public: * @param point 3D point in world coordinates * @return the intrinsic coordinates of the projected point */ - Point2 project2(const Unit3& point, OptionalJacobian<2, 6> Dpose = - boost::none, OptionalJacobian<2, 2> Dpoint = boost::none) const; + Point2 project2(const Unit3& point, + OptionalJacobian<2, 6> Dpose = boost::none, + OptionalJacobian<2, 2> Dpoint = boost::none) const; /// backproject a 2-dimensional point to a 3-dimensional point at given depth static Point3 backproject_from_camera(const Point2& p, const double depth); @@ -214,7 +219,6 @@ public: /// @} - private: /** Serialization function */ diff --git a/gtsam/geometry/PinholePose.h b/gtsam/geometry/PinholePose.h index a2449871a..d98f36b6e 100644 --- a/gtsam/geometry/PinholePose.h +++ b/gtsam/geometry/PinholePose.h @@ -100,7 +100,7 @@ public: */ Point2 project(const Unit3& pw) const { const Unit3 pc = pose().rotation().unrotate(pw); // convert to camera frame - const Point2 pn = PinholeBase::project_to_camera(pc); + const Point2 pn = PinholeBase::Project(pc); return calibration().uncalibrate(pn); } diff --git a/gtsam/geometry/tests/testCalibratedCamera.cpp b/gtsam/geometry/tests/testCalibratedCamera.cpp index af4c65127..199ae30ce 100644 --- a/gtsam/geometry/tests/testCalibratedCamera.cpp +++ b/gtsam/geometry/tests/testCalibratedCamera.cpp @@ -88,28 +88,28 @@ TEST( CalibratedCamera, project) } /* ************************************************************************* */ -static Point2 project_to_camera1(const Point3& point) { - return PinholeBase::project_to_camera(point); +static Point2 Project1(const Point3& point) { + return PinholeBase::Project(point); } -TEST( CalibratedCamera, Dproject_to_camera1) { +TEST( CalibratedCamera, DProject1) { Point3 pp(155, 233, 131); Matrix test1; - CalibratedCamera::project_to_camera(pp, test1); - Matrix test2 = numericalDerivative11(project_to_camera1, pp); + CalibratedCamera::Project(pp, test1); + Matrix test2 = numericalDerivative11(Project1, pp); CHECK(assert_equal(test1, test2)); } /* ************************************************************************* */ -static Point2 project_to_camera2(const Unit3& point) { - return PinholeBase::project_to_camera(point); +static Point2 Project2(const Unit3& point) { + return PinholeBase::Project(point); } Unit3 pointAtInfinity(0, 0, 1000); -TEST( CalibratedCamera, Dproject_to_cameraInfinity) { +TEST( CalibratedCamera, DProjectInfinity) { Matrix test1; - CalibratedCamera::project_to_camera(pointAtInfinity, test1); - Matrix test2 = numericalDerivative11(project_to_camera2, + CalibratedCamera::Project(pointAtInfinity, test1); + Matrix test2 = numericalDerivative11(Project2, pointAtInfinity); CHECK(assert_equal(test1, test2)); } diff --git a/gtsam/nonlinear/tests/testExpression.cpp b/gtsam/nonlinear/tests/testExpression.cpp index 80100af5e..1c97394e1 100644 --- a/gtsam/nonlinear/tests/testExpression.cpp +++ b/gtsam/nonlinear/tests/testExpression.cpp @@ -166,7 +166,8 @@ using namespace binary; Expression K(3); // Create expression tree -Expression projection(PinholeCamera::project_to_camera, p_cam); +Point2 (*f)(const Point3&, OptionalJacobian<2, 3>) = &PinholeBase::Project; +Expression projection(f, p_cam); Expression uv_hat(uncalibrate, K, projection); } /* ************************************************************************* */ diff --git a/gtsam/nonlinear/tests/testExpressionFactor.cpp b/gtsam/nonlinear/tests/testExpressionFactor.cpp deleted file mode 100644 index 99b8120e3..000000000 --- a/gtsam/nonlinear/tests/testExpressionFactor.cpp +++ /dev/null @@ -1,502 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * 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 - - * -------------------------------------------------------------------------- */ - -/** - * @file testExpressionFactor.cpp - * @date September 18, 2014 - * @author Frank Dellaert - * @author Paul Furgale - * @brief unit tests for Block Automatic Differentiation - */ - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -using boost::assign::list_of; - -using namespace std; -using namespace gtsam; - -Point2 measured(-17, 30); -SharedNoiseModel model = noiseModel::Unit::Create(2); - -namespace leaf { -// Create some values -struct MyValues: public Values { - MyValues() { - insert(2, Point2(3, 5)); - } -} values; - -// Create leaf -Point2_ p(2); -} - -/* ************************************************************************* */ -// Leaf -TEST(ExpressionFactor, Leaf) { - using namespace leaf; - - // Create old-style factor to create expected value and derivatives - PriorFactor old(2, Point2(0, 0), model); - - // Concise version - ExpressionFactor f(model, Point2(0, 0), p); - EXPECT_DOUBLES_EQUAL(old.error(values), f.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f.dim()); - boost::shared_ptr gf2 = f.linearize(values); - EXPECT( assert_equal(*old.linearize(values), *gf2, 1e-9)); -} - -/* ************************************************************************* */ -// non-zero noise model -TEST(ExpressionFactor, Model) { - using namespace leaf; - - SharedNoiseModel model = noiseModel::Diagonal::Sigmas(Vector2(0.1, 0.01)); - - // Create old-style factor to create expected value and derivatives - PriorFactor old(2, Point2(0, 0), model); - - // Concise version - ExpressionFactor f(model, Point2(0, 0), p); - - // Check values and derivatives - EXPECT_DOUBLES_EQUAL(old.error(values), f.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f.dim()); - boost::shared_ptr gf2 = f.linearize(values); - EXPECT( assert_equal(*old.linearize(values), *gf2, 1e-9)); - EXPECT_CORRECT_FACTOR_JACOBIANS(f, values, 1e-5, 1e-5); // another way -} - -/* ************************************************************************* */ -// Constrained noise model -TEST(ExpressionFactor, Constrained) { - using namespace leaf; - - SharedDiagonal model = noiseModel::Constrained::MixedSigmas(Vector2(0.2, 0)); - - // Create old-style factor to create expected value and derivatives - PriorFactor old(2, Point2(0, 0), model); - - // Concise version - ExpressionFactor f(model, Point2(0, 0), p); - EXPECT_DOUBLES_EQUAL(old.error(values), f.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f.dim()); - boost::shared_ptr gf2 = f.linearize(values); - EXPECT( assert_equal(*old.linearize(values), *gf2, 1e-9)); -} - -/* ************************************************************************* */ -// Unary(Leaf)) -TEST(ExpressionFactor, Unary) { - - // Create some values - Values values; - values.insert(2, Point3(0, 0, 1)); - - JacobianFactor expected( // - 2, (Matrix(2, 3) << 1, 0, 0, 0, 1, 0).finished(), // - Vector2(-17, 30)); - - // Create leaves - Point3_ p(2); - - // Concise version - ExpressionFactor f(model, measured, project(p)); - EXPECT_LONGS_EQUAL(2, f.dim()); - boost::shared_ptr gf = f.linearize(values); - boost::shared_ptr jf = // - boost::dynamic_pointer_cast(gf); - EXPECT( assert_equal(expected, *jf, 1e-9)); -} - -/* ************************************************************************* */ -// Unary(Leaf)) and Unary(Unary(Leaf))) -// wide version (not handled in fixed-size pipeline) -typedef Eigen::Matrix Matrix93; -Vector9 wide(const Point3& p, OptionalJacobian<9,3> H) { - Vector9 v; - v << p.vector(), p.vector(), p.vector(); - if (H) *H << eye(3), eye(3), eye(3); - return v; -} -typedef Eigen::Matrix Matrix9; -Vector9 id9(const Vector9& v, OptionalJacobian<9,9> H) { - if (H) *H = Matrix9::Identity(); - return v; -} -TEST(ExpressionFactor, Wide) { - // Create some values - Values values; - values.insert(2, Point3(0, 0, 1)); - Point3_ point(2); - Vector9 measured; - measured.setZero(); - Expression expression(wide,point); - SharedNoiseModel model = noiseModel::Unit::Create(9); - - ExpressionFactor f1(model, measured, expression); - EXPECT_CORRECT_FACTOR_JACOBIANS(f1, values, 1e-5, 1e-9); - - Expression expression2(id9,expression); - ExpressionFactor f2(model, measured, expression2); - EXPECT_CORRECT_FACTOR_JACOBIANS(f2, values, 1e-5, 1e-9); -} - -/* ************************************************************************* */ -static Point2 myUncal(const Cal3_S2& K, const Point2& p, - OptionalJacobian<2,5> Dcal, OptionalJacobian<2,2> Dp) { - return K.uncalibrate(p, Dcal, Dp); -} - -// Binary(Leaf,Leaf) -TEST(ExpressionFactor, Binary) { - - typedef BinaryExpression Binary; - - Cal3_S2_ K_(1); - Point2_ p_(2); - Binary binary(myUncal, K_, p_); - - // Create some values - Values values; - values.insert(1, Cal3_S2()); - values.insert(2, Point2(0, 0)); - - // traceRaw will fill raw with [Trace | Binary::Record] - EXPECT_LONGS_EQUAL(8, sizeof(double)); - EXPECT_LONGS_EQUAL(16, sizeof(Point2)); - EXPECT_LONGS_EQUAL(40, sizeof(Cal3_S2)); - EXPECT_LONGS_EQUAL(16, sizeof(ExecutionTrace)); - EXPECT_LONGS_EQUAL(16, sizeof(ExecutionTrace)); - EXPECT_LONGS_EQUAL(2*5*8, sizeof(Jacobian::type)); - EXPECT_LONGS_EQUAL(2*2*8, sizeof(Jacobian::type)); - size_t expectedRecordSize = 16 + 16 + 40 + 2 * 16 + 80 + 32; - EXPECT_LONGS_EQUAL(expectedRecordSize + 8, sizeof(Binary::Record)); - - // Check size - size_t size = binary.traceSize(); - CHECK(size); - EXPECT_LONGS_EQUAL(expectedRecordSize + 8, size); - // Use Variable Length Array, allocated on stack by gcc - // Note unclear for Clang: http://clang.llvm.org/compatibility.html#vla - ExecutionTraceStorage traceStorage[size]; - ExecutionTrace trace; - Point2 value = binary.traceExecution(values, trace, traceStorage); - EXPECT(assert_equal(Point2(),value, 1e-9)); - // trace.print(); - - // Expected Jacobians - Matrix25 expected25; - expected25 << 0, 0, 0, 1, 0, 0, 0, 0, 0, 1; - Matrix2 expected22; - expected22 << 1, 0, 0, 1; - - // Check matrices - boost::optional r = trace.record(); - CHECK(r); - EXPECT( - assert_equal(expected25, (Matrix) (*r)-> jacobian(), 1e-9)); - EXPECT( assert_equal(expected22, (Matrix) (*r)->jacobian(), 1e-9)); -} -/* ************************************************************************* */ -// Unary(Binary(Leaf,Leaf)) -TEST(ExpressionFactor, Shallow) { - - // Create some values - Values values; - values.insert(1, Pose3()); - values.insert(2, Point3(0, 0, 1)); - - // Create old-style factor to create expected value and derivatives - GenericProjectionFactor old(measured, model, 1, 2, - boost::make_shared()); - double expected_error = old.error(values); - GaussianFactor::shared_ptr expected = old.linearize(values); - - // Create leaves - Pose3_ x_(1); - Point3_ p_(2); - - // Construct expression, concise evrsion - Point2_ expression = project(transform_to(x_, p_)); - - // Get and check keys and dims - FastVector keys; - FastVector dims; - boost::tie(keys, dims) = expression.keysAndDims(); - LONGS_EQUAL(2,keys.size()); - LONGS_EQUAL(2,dims.size()); - LONGS_EQUAL(1,keys[0]); - LONGS_EQUAL(2,keys[1]); - LONGS_EQUAL(6,dims[0]); - LONGS_EQUAL(3,dims[1]); - - // traceExecution of shallow tree - typedef UnaryExpression Unary; - typedef BinaryExpression Binary; - size_t expectedTraceSize = sizeof(Unary::Record) + sizeof(Binary::Record); - EXPECT_LONGS_EQUAL(112, sizeof(Unary::Record)); -#ifdef GTSAM_USE_QUATERNIONS - EXPECT_LONGS_EQUAL(352, sizeof(Binary::Record)); - LONGS_EQUAL(112+352, expectedTraceSize); -#else - EXPECT_LONGS_EQUAL(400, sizeof(Binary::Record)); - LONGS_EQUAL(112+400, expectedTraceSize); -#endif - size_t size = expression.traceSize(); - CHECK(size); - EXPECT_LONGS_EQUAL(expectedTraceSize, size); - ExecutionTraceStorage traceStorage[size]; - ExecutionTrace trace; - Point2 value = expression.traceExecution(values, trace, traceStorage); - EXPECT(assert_equal(Point2(),value, 1e-9)); - // trace.print(); - - // Expected Jacobians - Matrix23 expected23; - expected23 << 1, 0, 0, 0, 1, 0; - - // Check matrices - boost::optional r = trace.record(); - CHECK(r); - EXPECT(assert_equal(expected23, (Matrix)(*r)->jacobian(), 1e-9)); - - // Linearization - ExpressionFactor f2(model, measured, expression); - EXPECT_DOUBLES_EQUAL(expected_error, f2.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f2.dim()); - boost::shared_ptr gf2 = f2.linearize(values); - EXPECT( assert_equal(*expected, *gf2, 1e-9)); -} - -/* ************************************************************************* */ -// Binary(Leaf,Unary(Binary(Leaf,Leaf))) -TEST(ExpressionFactor, tree) { - - // Create some values - Values values; - values.insert(1, Pose3()); - values.insert(2, Point3(0, 0, 1)); - values.insert(3, Cal3_S2()); - - // Create old-style factor to create expected value and derivatives - GeneralSFMFactor2 old(measured, model, 1, 2, 3); - double expected_error = old.error(values); - GaussianFactor::shared_ptr expected = old.linearize(values); - - // Create leaves - Pose3_ x(1); - Point3_ p(2); - Cal3_S2_ K(3); - - // Create expression tree - Point3_ p_cam(x, &Pose3::transform_to, p); - Point2_ xy_hat(PinholeCamera::project_to_camera, p_cam); - Point2_ uv_hat(K, &Cal3_S2::uncalibrate, xy_hat); - - // Create factor and check value, dimension, linearization - ExpressionFactor f(model, measured, uv_hat); - EXPECT_DOUBLES_EQUAL(expected_error, f.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f.dim()); - boost::shared_ptr gf = f.linearize(values); - EXPECT( assert_equal(*expected, *gf, 1e-9)); - - // Concise version - ExpressionFactor f2(model, measured, - uncalibrate(K, project(transform_to(x, p)))); - EXPECT_DOUBLES_EQUAL(expected_error, f2.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f2.dim()); - boost::shared_ptr gf2 = f2.linearize(values); - EXPECT( assert_equal(*expected, *gf2, 1e-9)); - - TernaryExpression::Function fff = project6; - - // Try ternary version - ExpressionFactor f3(model, measured, project3(x, p, K)); - EXPECT_DOUBLES_EQUAL(expected_error, f3.error(values), 1e-9); - EXPECT_LONGS_EQUAL(2, f3.dim()); - boost::shared_ptr gf3 = f3.linearize(values); - EXPECT( assert_equal(*expected, *gf3, 1e-9)); -} - -/* ************************************************************************* */ - -TEST(ExpressionFactor, Compose1) { - - // Create expression - Rot3_ R1(1), R2(2); - Rot3_ R3 = R1 * R2; - - // Create factor - ExpressionFactor f(noiseModel::Unit::Create(3), Rot3(), R3); - - // Create some values - Values values; - values.insert(1, Rot3()); - values.insert(2, Rot3()); - - // Check unwhitenedError - std::vector H(2); - Vector actual = f.unwhitenedError(values, H); - EXPECT( assert_equal(eye(3), H[0],1e-9)); - EXPECT( assert_equal(eye(3), H[1],1e-9)); - - // Check linearization - JacobianFactor expected(1, eye(3), 2, eye(3), zero(3)); - boost::shared_ptr gf = f.linearize(values); - boost::shared_ptr jf = // - boost::dynamic_pointer_cast(gf); - EXPECT( assert_equal(expected, *jf,1e-9)); -} - -/* ************************************************************************* */ -// Test compose with arguments referring to the same rotation -TEST(ExpressionFactor, compose2) { - - // Create expression - Rot3_ R1(1), R2(1); - Rot3_ R3 = R1 * R2; - - // Create factor - ExpressionFactor f(noiseModel::Unit::Create(3), Rot3(), R3); - - // Create some values - Values values; - values.insert(1, Rot3()); - - // Check unwhitenedError - std::vector H(1); - Vector actual = f.unwhitenedError(values, H); - EXPECT_LONGS_EQUAL(1, H.size()); - EXPECT( assert_equal(2*eye(3), H[0],1e-9)); - - // Check linearization - JacobianFactor expected(1, 2 * eye(3), zero(3)); - boost::shared_ptr gf = f.linearize(values); - boost::shared_ptr jf = // - boost::dynamic_pointer_cast(gf); - EXPECT( assert_equal(expected, *jf,1e-9)); -} - -/* ************************************************************************* */ -// Test compose with one arguments referring to a constant same rotation -TEST(ExpressionFactor, compose3) { - - // Create expression - Rot3_ R1(Rot3::identity()), R2(3); - Rot3_ R3 = R1 * R2; - - // Create factor - ExpressionFactor f(noiseModel::Unit::Create(3), Rot3(), R3); - - // Create some values - Values values; - values.insert(3, Rot3()); - - // Check unwhitenedError - std::vector H(1); - Vector actual = f.unwhitenedError(values, H); - EXPECT_LONGS_EQUAL(1, H.size()); - EXPECT( assert_equal(eye(3), H[0],1e-9)); - - // Check linearization - JacobianFactor expected(3, eye(3), zero(3)); - boost::shared_ptr gf = f.linearize(values); - boost::shared_ptr jf = // - boost::dynamic_pointer_cast(gf); - EXPECT( assert_equal(expected, *jf,1e-9)); -} - -/* ************************************************************************* */ -// Test compose with three arguments -Rot3 composeThree(const Rot3& R1, const Rot3& R2, const Rot3& R3, - OptionalJacobian<3, 3> H1, OptionalJacobian<3, 3> H2, OptionalJacobian<3, 3> H3) { - // return dummy derivatives (not correct, but that's ok for testing here) - if (H1) - *H1 = eye(3); - if (H2) - *H2 = eye(3); - if (H3) - *H3 = eye(3); - return R1 * (R2 * R3); -} - -TEST(ExpressionFactor, composeTernary) { - - // Create expression - Rot3_ A(1), B(2), C(3); - Rot3_ ABC(composeThree, A, B, C); - - // Create factor - ExpressionFactor f(noiseModel::Unit::Create(3), Rot3(), ABC); - - // Create some values - Values values; - values.insert(1, Rot3()); - values.insert(2, Rot3()); - values.insert(3, Rot3()); - - // Check unwhitenedError - std::vector H(3); - Vector actual = f.unwhitenedError(values, H); - EXPECT_LONGS_EQUAL(3, H.size()); - EXPECT( assert_equal(eye(3), H[0],1e-9)); - EXPECT( assert_equal(eye(3), H[1],1e-9)); - EXPECT( assert_equal(eye(3), H[2],1e-9)); - - // Check linearization - JacobianFactor expected(1, eye(3), 2, eye(3), 3, eye(3), zero(3)); - boost::shared_ptr gf = f.linearize(values); - boost::shared_ptr jf = // - boost::dynamic_pointer_cast(gf); - EXPECT( assert_equal(expected, *jf,1e-9)); -} - -TEST(ExpressionFactor, tree_finite_differences) { - - // Create some values - Values values; - values.insert(1, Pose3()); - values.insert(2, Point3(0, 0, 1)); - values.insert(3, Cal3_S2()); - - // Create leaves - Pose3_ x(1); - Point3_ p(2); - Cal3_S2_ K(3); - - // Create expression tree - Point3_ p_cam(x, &Pose3::transform_to, p); - Point2_ xy_hat(PinholeCamera::project_to_camera, p_cam); - Point2_ uv_hat(K, &Cal3_S2::uncalibrate, xy_hat); - - const double fd_step = 1e-5; - const double tolerance = 1e-5; - EXPECT_CORRECT_EXPRESSION_JACOBIANS(uv_hat, values, fd_step, tolerance); -} - -/* ************************************************************************* */ -int main() { - TestResult tr; - return TestRegistry::runAllTests(tr); -} -/* ************************************************************************* */ - diff --git a/gtsam/slam/EssentialMatrixFactor.h b/gtsam/slam/EssentialMatrixFactor.h index 9d4a8e6e5..da22225e5 100644 --- a/gtsam/slam/EssentialMatrixFactor.h +++ b/gtsam/slam/EssentialMatrixFactor.h @@ -173,7 +173,7 @@ public: Point3 _1T2 = E.direction().point3(); Point3 d1T2 = d * _1T2; Point3 dP2 = E.rotation().unrotate(dP1_ - d1T2); // 2R1*((x,y,1)-d*1T2) - pn = SimpleCamera::project_to_camera(dP2); + pn = PinholeBase::Project(dP2); } else { @@ -186,7 +186,7 @@ public: Point3 dP2 = E.rotation().unrotate(dP1_ - d1T2, DdP2_rot, DP2_point); Matrix23 Dpn_dP2; - pn = SimpleCamera::project_to_camera(dP2, Dpn_dP2); + pn = PinholeBase::Project(dP2, Dpn_dP2); if (DE) { Matrix DdP2_E(3, 5); diff --git a/gtsam/slam/expressions.h b/gtsam/slam/expressions.h index b819993ef..fac2cf03d 100644 --- a/gtsam/slam/expressions.h +++ b/gtsam/slam/expressions.h @@ -28,6 +28,7 @@ inline Point2_ transform_to(const Pose2_& x, const Point2_& p) { // 3D Geometry typedef Expression Point3_; +typedef Expression Unit3_; typedef Expression Rot3_; typedef Expression Pose3_; @@ -40,33 +41,52 @@ inline Point3_ transform_to(const Pose3_& x, const Point3_& p) { typedef Expression Cal3_S2_; typedef Expression Cal3Bundler_; +/// Expression version of PinholeBase::Project inline Point2_ project(const Point3_& p_cam) { - return Point2_(PinholeCamera::project_to_camera, p_cam); + Point2 (*f)(const Point3&, OptionalJacobian<2, 3>) = &PinholeBase::Project; + return Point2_(f, p_cam); } -template -Point2 project4(const CAMERA& camera, const Point3& p, - OptionalJacobian<2, CAMERA::dimension> Dcam, OptionalJacobian<2, 3> Dpoint) { +inline Point2_ project(const Unit3_& p_cam) { + Point2 (*f)(const Unit3&, OptionalJacobian<2, 2>) = &PinholeBase::Project; + return Point2_(f, p_cam); +} + +namespace internal { +// Helper template for project2 expression below +template +Point2 project4(const CAMERA& camera, const POINT& p, + OptionalJacobian<2, CAMERA::dimension> Dcam, + OptionalJacobian<2, FixedDimension::value> Dpoint) { return camera.project2(p, Dcam, Dpoint); } - -template -Point2_ project2(const Expression& camera_, const Point3_& p_) { - return Point2_(project4, camera_, p_); } +template +Point2_ project2(const Expression& camera_, + const Expression& p_) { + return Point2_(internal::project4, camera_, p_); +} + +namespace internal { +// Helper template for project3 expression below +template inline Point2 project6(const Pose3& x, const Point3& p, const Cal3_S2& K, - OptionalJacobian<2, 6> Dpose, OptionalJacobian<2, 3> Dpoint, OptionalJacobian<2, 5> Dcal) { + OptionalJacobian<2, 6> Dpose, OptionalJacobian<2, 3> Dpoint, + OptionalJacobian<2, 5> Dcal) { return PinholeCamera(x, K).project(p, Dpose, Dpoint, Dcal); } - -inline Point2_ project3(const Pose3_& x, const Point3_& p, const Cal3_S2_& K) { - return Point2_(project6, x, p, K); } -template -Point2_ uncalibrate(const Expression& K, const Point2_& xy_hat) { - return Point2_(K, &CAL::uncalibrate, xy_hat); +template +inline Point2_ project3(const Pose3_& x, const Expression& p, + const Expression& K) { + return Point2_(internal::project6, x, p, K); +} + +template +Point2_ uncalibrate(const Expression& K, const Point2_& xy_hat) { + return Point2_(K, &CALIBRATION::uncalibrate, xy_hat); } } // \namespace gtsam