22#include <gtsam/dllexport.h>
25#include <Eigen/Geometry>
27#if GTSAM_ENABLE_BOOST_SERIALIZATION
28#include <boost/serialization/nvp.hpp>
43 return (N < 0) ? Eigen::Dynamic : N * (N - 1) / 2;
47constexpr int NSquaredSO(
int N) {
return (N < 0) ? Eigen::Dynamic : N * N; }
58 using MatrixNN = Eigen::Matrix<double, N, N>;
59 using VectorN2 = Eigen::Matrix<double, internal::NSquaredSO(N), 1>;
60 using MatrixDD = Eigen::Matrix<double, dimension, dimension>;
71 using IsDynamic =
typename std::enable_if<N_ == Eigen::Dynamic, void>::type;
73 using IsFixed =
typename std::enable_if<N_ >= 2,
void>::type;
75 using IsSO3 =
typename std::enable_if<N_ == 3, void>::type;
82 template <
int N_ = N,
typename = IsFixed<N_>>
86 template <
int N_ = N,
typename = IsDynamic<N_>>
87 explicit SO(
size_t n = 0) {
90 matrix_ = Eigen::MatrixXd::Identity(n, n);
94 template <
typename Derived>
95 explicit SO(
const Eigen::MatrixBase<Derived>& R) :
matrix_(R.eval()) {}
98 template <
typename Derived>
104 template <
typename Derived,
int N_ = N,
typename = IsDynamic<N_>>
105 static SO Lift(
size_t n,
const Eigen::MatrixBase<Derived> &R) {
106 Matrix Q = Matrix::Identity(n, n);
107 const int p = R.rows();
108 assert(p >= 0 && p <=
static_cast<int>(n) && R.cols() == p);
109 Q.topLeftCorner(p, p) = R;
114 template <
int M,
int N_ = N,
typename = IsDynamic<N_>>
118 template <
int N_ = N,
typename = IsSO3<N_>>
119 explicit SO(
const Eigen::AngleAxisd& angleAxis) :
matrix_(angleAxis) {}
134 template <
int N_ = N,
typename = IsDynamic<N_>>
135 static SO Random(std::mt19937& rng,
size_t n = 0) {
136 if (n == 0)
throw std::runtime_error(
"SO: Dimensionality not known.");
138 static std::uniform_real_distribution<double> randomAngle(-M_PI, M_PI);
139 const size_t d = SO::Dimension(n);
141 for (
size_t j = 0; j < d; j++) {
142 xi(j) = randomAngle(rng);
148 template <
int N_ = N,
typename = IsFixed<N_>>
161 size_t rows()
const {
return matrix_.rows(); }
162 size_t cols()
const {
return matrix_.cols(); }
168 void print(
const std::string& s = std::string())
const;
170 bool equals(
const SO& other,
double tol)
const {
180 assert(
dim() == other.dim());
185 template <
int N_ = N,
typename = IsFixed<N_>>
191 template <
int N_ = N,
typename = IsDynamic<N_>>
203 using TangentVector = Eigen::Matrix<double, dimension, 1>;
208 static size_t Dimension(
size_t n) {
return n * (n - 1) / 2; }
211 static size_t AmbientDim(
size_t d) {
return (1 + std::sqrt(1 + 8 * d)) / 2; }
214 size_t dim()
const {
return Dimension(
static_cast<size_t>(
matrix_.rows())); }
231 static MatrixNN
Hat(
const TangentVector& xi);
234 static void Hat(
const Vector &xi, Eigen::Ref<MatrixNN> X);
237 static TangentVector
Vee(
const MatrixNN& X);
245 static SO Retract(
const TangentVector& xi);
249 static SO Retract(
const TangentVector& xi, ChartJacobian H) =
delete;
254 static TangentVector
Local(
const SO& R);
257 static TangentVector
Local(
const SO& R, ChartJacobian H) =
delete;
261 template <
int N_ = N,
typename = IsDynamic<N_>>
262 static MatrixDD IdentityJacobian(
size_t n) {
263 const size_t d = Dimension(n);
264 return MatrixDD::Identity(d, d);
282 static SO Expmap(
const TangentVector& omega, ChartJacobian H) =
delete;
293 static TangentVector
Logmap(
const SO& R, ChartJacobian H) =
delete;
312 template <
int N_ = N,
typename = IsFixed<N_>>
314 constexpr size_t N2 =
static_cast<size_t>(N * N);
315 Eigen::Matrix<double, N2, dimension> G;
316 for (
size_t j = 0; j < dimension; j++) {
317 const auto X =
Hat(Vector::Unit(dimension, j));
318 G.col(j) = Eigen::Map<const VectorN2>(X.data());
324 template <
int N_ = N,
typename = IsDynamic<N_>>
326 const size_t n2 = n * n, dim = Dimension(n);
328 for (
size_t j = 0; j < dim; j++) {
329 const auto X =
Hat(Vector::Unit(dim, j));
330 G.col(j) = Eigen::Map<const Matrix>(X.data(), n2, 1);
339#if GTSAM_ENABLE_BOOST_SERIALIZATION
340 template <
class Archive>
341 friend void save(Archive&,
SO&,
const unsigned int);
342 template <
class Archive>
343 friend void load(Archive&,
SO&,
const unsigned int);
344 template <
class Archive>
345 friend void serialize(Archive&,
SO&,
const unsigned int);
346 friend class boost::serialization::access;
353using SOn = SO<Eigen::Dynamic>;
374using DynamicJacobian = OptionalJacobian<Eigen::Dynamic, Eigen::Dynamic>;
378SOn LieGroup<SOn, Eigen::Dynamic>::compose(
const SOn& g, DynamicJacobian H1,
379 DynamicJacobian H2)
const;
383SOn LieGroup<SOn, Eigen::Dynamic>::between(
const SOn& g, DynamicJacobian H1,
384 DynamicJacobian H2)
const;
386#if GTSAM_ENABLE_BOOST_SERIALIZATION
388template<
class Archive>
391 const unsigned int file_version
393 Matrix& M = Q.matrix_;
394 ar& BOOST_SERIALIZATION_NVP(M);
Base class and basic functions for Matrix Lie groups.
constexpr int DimensionSO(int N)
Calculate dimensionality of SO<N> manifold, or return Dynamic if so.
Definition SOn.h:42
Template implementations for SO(n).
Global functions in a separate testing namespace.
Definition chartTesting.h:28
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition Matrix.cpp:154
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
equals with a tolerance
Definition Matrix.h:81
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A CRTP helper class that implements Lie group methods Prerequisites: methods operator*,...
Definition Lie.h:114
static SO< N > Retract(const TangentVector &v)
Definition Lie.h:193
A CRTP helper class that implements matrix Lie group methods.
Definition MatrixLieGroup.h:51
std::enable_if_t< M !=Eigen::Dynamic, int > dim() const
Both LieGroupTraits and Testable.
Definition MatrixLieGroup.h:350
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
Manifold of special orthogonal rotation matrices SO<N>.
Definition SOn.h:55
static SO FromMatrix(const Eigen::MatrixBase< Derived > &R)
Named constructor from Eigen Matrix.
Definition SOn.h:99
static Matrix VectorizedGenerators()
Calculate N^2 x dim matrix of vectorized Lie algebra generators for SO(N).
Definition SOn.h:313
SO inverse() const
inverse of a rotation = transpose
Definition SOn.h:197
static SO ChordalMean(const std::vector< SO > &rotations)
Named constructor that finds chordal mean , currently only defined for SO3.
SO operator*(const SO &other) const
Multiplication.
Definition SOn.h:179
static TangentVector Vee(const MatrixNN &X)
Inverse of Hat. See note about xi element order in Hat.
Definition SOn-inl.h:35
MatrixNN matrix_
Definition SOn.h:66
VectorN2 vec(OptionalJacobian< internal::NSquaredSO(N), dimension > H={}) const
Return vectorized rotation matrix in column order.
Definition SOn.h:306
static SO Expmap(const TangentVector &omega)
Exponential map at identity - create a rotation from canonical coordinates.
Definition SOn-inl.h:57
MatrixNN LieAlgebra
Definition SOn.h:63
static SO Expmap(const TangentVector &omega, ChartJacobian H)=delete
Exponential map with a Jacobian, specialized when supported.
static SO AxisAngle(const Vector3 &axis, double theta)
Constructor from axis and angle. Only defined for SO3.
static void Hat(const Vector &xi, Eigen::Ref< MatrixNN > X)
In-place version of Hat (see details there), implements recursion.
static SO Identity()
Definition SOn.h:186
static MatrixNN Hat(const TangentVector &xi)
Hat operator creates Lie algebra element corresponding to d-vector, where d is the dimensionality of ...
Definition SOn-inl.h:29
SO(const SO< M > &R)
Construct dynamic SO(n) from Fixed SO<M>.
Definition SOn.h:115
SO(size_t n=0)
Construct SO<N> identity for N == Eigen::Dynamic.
Definition SOn.h:87
SO()
Construct SO<N> identity for N >= 2.
Definition SOn.h:83
static MatrixDD LogmapDerivative(const TangentVector &omega)=delete
Derivative of Logmap, specialized when supported.
static MatrixDD ExpmapDerivative(const TangentVector &omega)=delete
Derivative of Expmap, specialized when supported.
static TangentVector Logmap(const SO &R)
Log map at identity - returns the canonical coordinates of this rotation.
Definition SOn-inl.h:62
static SO Lift(size_t n, const Eigen::MatrixBase< Derived > &R)
Named constructor from lower dimensional matrix.
Definition SOn.h:105
static SO Random(std::mt19937 &rng, size_t n=0)
Random SO(n) element (no big claims about uniformity). SO(3) is specialized in SO3....
Definition SOn.h:135
static SO Identity(size_t n=0)
SO<N> identity for N == Eigen::Dynamic.
Definition SOn.h:192
const MatrixNN & matrix() const
Definition SOn.h:159
static TangentVector Logmap(const SO &R, ChartJacobian H)=delete
Logarithm map with a Jacobian, specialized when supported.
static SO Random(std::mt19937 &rng)
Random SO(N) element (no big claims about uniformity).
Definition SOn.h:149
MatrixDD AdjointMap() const
Adjoint map.
Definition SOn.h:272
static SO ClosestTo(const MatrixNN &M)
Named constructor that finds SO(n) matrix closest to M in Frobenius norm, currently only defined for ...
SO(const Eigen::AngleAxisd &angleAxis)
Constructor from AngleAxisd.
Definition SOn.h:119
static Matrix VectorizedGenerators(size_t n=0)
Calculate n^2 x dim matrix of vectorized Lie algebra generators for SO(n).
Definition SOn.h:325
SO(const Eigen::MatrixBase< Derived > &R)
Constructor from Eigen Matrix, dynamic version.
Definition SOn.h:95
static TangentVector Local(const SO &R)
Inverse of Retract.
Definition SOn-inl.h:49
static SO Retract(const TangentVector &xi, ChartJacobian H)=delete
Retract with a Jacobian, specialized only for dimensions that support it.
static SO Retract(const TangentVector &xi)
Retract uses Cayley map.
Definition SOn-inl.h:40
static TangentVector Local(const SO &R, ChartJacobian H)=delete
Local coordinates with a Jacobian, specialized when supported.