gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LieMatrix.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <cstdarg>
21 
22 #include <gtsam/base/DerivedValue.h>
23 #include <gtsam/base/Lie.h>
24 #include <gtsam/base/Matrix.h>
25 #include <boost/serialization/nvp.hpp>
26 
27 namespace gtsam {
28 
32 struct LieMatrix : public Matrix, public DerivedValue<LieMatrix> {
33 
36 
38  LieMatrix() {}
39 
41  LieMatrix(const Matrix& v) : Matrix(v) {}
42 
43 // Currently TMP constructor causes ICE on MSVS 2013
44 #if (_MSC_VER < 1800)
45 
46  template<int M, int N>
47  LieMatrix(const Eigen::Matrix<double, M, N>& v) : Matrix(v) {}
48 #endif
49 
51  LieMatrix(size_t m, size_t n, const double* const data) :
52  Matrix(Eigen::Map<const Matrix>(data, m, n)) {}
53 
57 
59  GTSAM_EXPORT void print(const std::string& name="") const;
60 
62  inline bool equals(const LieMatrix& expected, double tol=1e-5) const {
63  return gtsam::equal_with_abs_tol(matrix(), expected.matrix(), tol);
64  }
65 
69 
71  inline Matrix matrix() const {
72  return static_cast<Matrix>(*this);
73  }
74 
78 
80  inline size_t dim() const { return this->size(); }
81 
85  inline LieMatrix retract(const Vector& v) const {
86  if(v.size() != this->size())
87  throw std::invalid_argument("LieMatrix::retract called with Vector of incorrect size");
88 
89  return LieMatrix(*this +
90  Eigen::Map<const Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> >(
91  &v(0), this->rows(), this->cols()));
92  }
93 
97  inline Vector localCoordinates(const LieMatrix& t2) const {
98  Vector result(this->size());
99  Eigen::Map<Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> >(
100  &result(0), this->rows(), this->cols()) = t2 - *this;
101  return result;
102  }
103 
107 
109  inline static LieMatrix identity() {
110  throw std::runtime_error("LieMatrix::identity(): Don't use this function");
111  return LieMatrix();
112  }
113 
114  // Note: Manually specifying the 'gtsam' namespace for the optional Matrix arguments
115  // This is a work-around for linux g++ 4.6.1 that incorrectly selects the Eigen::Matrix class
116  // instead of the gtsam::Matrix class. This is related to deriving this class from an Eigen Vector
117  // as the other geometry objects (Point3, Rot3, etc.) have this problem
119  inline LieMatrix compose(const LieMatrix& p,
120  boost::optional<gtsam::Matrix&> H1=boost::none,
121  boost::optional<gtsam::Matrix&> H2=boost::none) const {
122  if(H1) *H1 = eye(dim());
123  if(H2) *H2 = eye(p.dim());
124 
125  return LieMatrix(*this + p);
126  }
127 
129  inline LieMatrix between(const LieMatrix& l2,
130  boost::optional<gtsam::Matrix&> H1=boost::none,
131  boost::optional<gtsam::Matrix&> H2=boost::none) const {
132  if(H1) *H1 = -eye(dim());
133  if(H2) *H2 = eye(l2.dim());
134  return LieMatrix(l2 - *this);
135  }
136 
138  inline LieMatrix inverse(boost::optional<gtsam::Matrix&> H=boost::none) const {
139  if(H) *H = -eye(dim());
140 
141  return LieMatrix(-(*this));
142  }
143 
147 
149  static inline LieMatrix Expmap(const Vector& v) {
150  throw std::runtime_error("LieMatrix::Expmap(): Don't use this function");
151  return LieMatrix(v); }
152 
154  static inline Vector Logmap(const LieMatrix& p) {
155  Vector result(p.size());
156  Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(
157  result.data(), p.rows(), p.cols()) = p;
158  return result;
159  }
160 
162 
163 private:
164 
165  // Serialization function
166  friend class boost::serialization::access;
167  template<class Archive>
168  void serialize(Archive & ar, const unsigned int version) {
169  ar & boost::serialization::make_nvp("LieMatrix",
170  boost::serialization::base_object<Value>(*this));
171  ar & boost::serialization::make_nvp("Matrix",
172  boost::serialization::base_object<Matrix>(*this));
173 
174  }
175 
176 };
177 } // \namespace gtsam
Matrix matrix() const
get the underlying vector
Definition: LieMatrix.h:71
Vector localCoordinates(const LieMatrix &t2) const
Definition: LieMatrix.h:97
static Vector Logmap(const LieMatrix &p)
Logmap around identity.
Definition: LieMatrix.h:154
Base class and basic functions for Lie types.
Matrix eye(size_t m, size_t n)
Creates an identity matrix, with matlab-like syntax.
Definition: Matrix.cpp:50
size_t dim() const
Returns dimensionality of the tangent space.
Definition: LieMatrix.h:80
bool equals(const LieMatrix &expected, double tol=1e-5) const
equality up to tolerance
Definition: LieMatrix.h:62
GTSAM_EXPORT void print(const std::string &name="") const
print
Definition: LieMatrix.cpp:23
static LieMatrix Expmap(const Vector &v)
Expmap around identity.
Definition: LieMatrix.h:149
LieMatrix inverse(boost::optional< gtsam::Matrix & > H=boost::none) const
invert the object and yield a new one
Definition: LieMatrix.h:138
LieMatrix(size_t m, size_t n, const double *const data)
constructor with size and initial data, row order !
Definition: LieMatrix.h:51
LieVector is a wrapper around vector to allow it to be a Lie type.
Definition: LieMatrix.h:32
LieMatrix between(const LieMatrix &l2, boost::optional< gtsam::Matrix & > H1=boost::none, boost::optional< gtsam::Matrix & > H2=boost::none) const
between operation
Definition: LieMatrix.h:129
static LieMatrix identity()
identity - NOTE: no known size at compile time - so zero length
Definition: LieMatrix.h:109
LieMatrix(const Eigen::Matrix< double, M, N > &v)
initialize from a fixed size normal vector
Definition: LieMatrix.h:47
typedef and functions to augment Eigen's MatrixXd
LieMatrix(const Matrix &v)
initialize from a normal matrix
Definition: LieMatrix.h:41
LieMatrix compose(const LieMatrix &p, boost::optional< gtsam::Matrix & > H1=boost::none, boost::optional< gtsam::Matrix & > H2=boost::none) const
compose with another object
Definition: LieMatrix.h:119
Definition: DerivedValue.h:44
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
equals with an tolerance
Definition: Matrix.h:84
LieMatrix retract(const Vector &v) const
Update the LieMatrix with a tangent space update.
Definition: LieMatrix.h:85
LieMatrix()
default constructor - should be unnecessary
Definition: LieMatrix.h:38