From 63918ff7de29ff410782a0b0c76b87030ec99314 Mon Sep 17 00:00:00 2001 From: dellaert Date: Sat, 29 Nov 2014 15:06:26 +0100 Subject: [PATCH] Now dynamically sized matrices live in manifolds, too. --- gtsam/base/Manifold.h | 57 +++++++++++++++++++--------- gtsam/nonlinear/tests/testValues.cpp | 8 ++-- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/gtsam/base/Manifold.h b/gtsam/base/Manifold.h index a3a5b029b..1190822ed 100644 --- a/gtsam/base/Manifold.h +++ b/gtsam/base/Manifold.h @@ -73,7 +73,6 @@ template struct dimension: public Dynamic { }; - /** * zero::value is intended to be the origin of a canonical coordinate system * with canonical(t) == DefaultChart::local(zero::value, t) @@ -111,14 +110,16 @@ struct is_group > : publi }; template -struct is_manifold > : public boost::true_type{ +struct is_manifold > : public boost::true_type { }; template -struct dimension > : public boost::integral_constant { - //TODO after switch to c++11 : the above should should be extracted to a constexpr function - // for readability and to reduce code duplication +struct dimension > : public boost::integral_constant< + int, + M == Eigen::Dynamic ? Eigen::Dynamic : + (N == Eigen::Dynamic ? Eigen::Dynamic : M * N)> { + //TODO after switch to c++11 : the above should should be extracted to a constexpr function + // for readability and to reduce code duplication }; template @@ -131,10 +132,10 @@ struct zero > { }; template -struct identity > : public zero > { +struct identity > : public zero< + Eigen::Matrix > { }; - template struct is_chart: public boost::false_type { }; @@ -248,12 +249,16 @@ struct DefaultChart > { * This chart for the vector space of M x N matrices (represented by Eigen matrices) chooses as basis the one with respect to which the coordinates are exactly the matrix entries as laid out in memory (as determined by Options). * Computing coordinates for a matrix is then simply a reshape to the row vector of appropriate size. */ - typedef Eigen::Matrix type; + typedef Eigen::Matrix type; typedef type T; - typedef Eigen::Matrix::value, 1> vector;BOOST_STATIC_ASSERT_MSG((M!=Eigen::Dynamic && N!=Eigen::Dynamic), - "DefaultChart has not been implemented yet for dynamically sized matrices"); + typedef Eigen::Matrix::value, 1> vector; + + BOOST_STATIC_ASSERT_MSG((M!=Eigen::Dynamic && N!=Eigen::Dynamic), + "Internal error: DefaultChart for Dynamic should be handled by template below"); + static vector local(const T& origin, const T& other) { - return reshape(other) - reshape(origin); + return reshape(other) + - reshape(origin); } static T retract(const T& origin, const vector& d) { return origin + reshape(d); @@ -266,20 +271,36 @@ struct DefaultChart > { // Dynamically sized Vector template<> struct DefaultChart { - typedef Vector T; - typedef T type; - typedef T vector; - static vector local(const T& origin, const T& other) { + typedef Vector type; + typedef Vector vector; + static vector local(const Vector& origin, const Vector& other) { return other - origin; } - static T retract(const T& origin, const vector& d) { + static Vector retract(const Vector& origin, const vector& d) { return origin + d; } - static int getDimension(const T& origin) { + static int getDimension(const Vector& origin) { return origin.size(); } }; +// Dynamically sized Matrix +template<> +struct DefaultChart { + typedef Matrix type; + typedef Vector vector; + static vector local(const Matrix& origin, const Matrix& other) { + Matrix d = other - origin; + return Eigen::Map(d.data(),getDimension(d)); + } + static Matrix retract(const Matrix& origin, const vector& d) { + return origin + Eigen::Map(d.data(),origin.rows(),origin.cols()); + } + static int getDimension(const Matrix& m) { + return m.size(); + } +}; + /** * Old Concept check class for Manifold types * Requires a mapping between a linear tangent space and the underlying diff --git a/gtsam/nonlinear/tests/testValues.cpp b/gtsam/nonlinear/tests/testValues.cpp index 09fe0f253..941728d8c 100644 --- a/gtsam/nonlinear/tests/testValues.cpp +++ b/gtsam/nonlinear/tests/testValues.cpp @@ -12,6 +12,8 @@ /** * @file testValues.cpp * @author Richard Roberts + * @author Frank Dellaert + * @author Mike Bosse */ #include @@ -168,9 +170,9 @@ TEST(Values, basic_functions) Values values; const Values& values_c = values; values.insert(2, Vector3()); - values.insert(4, Vector3()); - values.insert(6, Vector3()); - values.insert(8, Vector3()); + values.insert(4, Vector(3)); + values.insert(6, Matrix23()); + values.insert(8, Matrix(2,3)); // find EXPECT_LONGS_EQUAL(4, values.find(4)->key);