Thea
RigidTransformN.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2011
12 //
13 //============================================================================
14 
15 #ifndef __Thea_RigidTransformN_hpp__
16 #define __Thea_RigidTransformN_hpp__
17 
18 #include "Common.hpp"
19 #include "AffineTransformN.hpp"
20 #include <sstream>
21 
22 namespace Thea {
23 
24 // Forward declarations
25 template <int N, typename T> class RigidTransformN;
26 
27 namespace Internal {
28 
37 template <int N, typename T>
38 class /* THEA_DLL_LOCAL */ RigidTransformNBase
39 {
40  public:
45 
46  THEA_DECL_SMART_POINTERS(RigidTransformT)
47 
48  public:
50  RigidTransformNBase() : aff(AffineTransformT::identity()) {}
51 
53  static RigidTransformT translation(VectorT const & translation_)
54  {
55  return _fromAffine(AffineTransformT::translation(translation_));
56  }
57 
62  static RigidTransformT _fromAffine(AffineTransformT const & aff_)
63  {
64  RigidTransformT rt;
65  rt.aff = aff_;
66  return rt;
67  }
68 
70  template <typename U> RigidTransformN<N, U> cast() const
71  {
72  return RigidTransformN<N, U>::_fromAffine(aff.template cast<U>());
73  }
74 
76  void setIdentity() { *this = identity(); }
77 
79  MatrixT const & getRotation() const { return aff.getLinear(); }
80 
82  void _setRotation(MatrixT const & rot) { aff.setLinear(rot); }
83 
85  VectorT const & getTranslation() const { return aff.getTranslation(); }
86 
88  void setTranslation(VectorT const & translation_) { aff.setTranslation(translation_); }
89 
91  Matrix<N, N + 1, T> toMatrix() const { return aff.toMatrix(); }
92 
94  Matrix<N + 1, N + 1, T> homogeneous() const { return aff.homogeneous(); }
95 
100  AffineTransformT const & toAffine() const { return aff; }
101 
106  operator AffineTransformT() const { return aff; }
107 
109  RigidTransformT inverse() const
110  {
111  MatrixT inv = aff.getLinear().transpose(); // transpose == inverse for orthogonal matrix
112  return _fromAffine(AffineTransformT(inv, inv * (-aff.getTranslation())));
113  }
114 
116  Real operator()(int i, int j) const { return aff(i, j); }
117 
119  RigidTransformT operator*(RigidTransformT const & rhs) const { return _fromAffine(aff * rhs.aff); }
120 
122  VectorT operator*(VectorT const & v) const { return aff * v; }
123 
125  std::string toString() const
126  {
127  std::ostringstream oss;
128  oss << "[R: " << Thea::toString(getRotation()) << ", T: " << Thea::toString(getTranslation()) << ']';
129  return oss.str();
130  }
131 
133  static RigidTransformT const & identity()
134  {
135  static RigidTransformT const idty = _fromAffine(AffineTransformT::identity());
136  return idty;
137  }
138 
139  private:
140  AffineTransformT aff;
141 
142 }; // class RigidTransformNBase
143 
144 } // namespace Internal
145 
152 template <int N, typename T = Real>
153 class /* THEA_API */ RigidTransformN : public Internal::RigidTransformNBase<N, T>
154 {
155  private:
157 
158  public:
159  typedef typename BaseT::AffineTransformT AffineTransformT;
160  typedef typename BaseT::VectorT VectorT;
161  typedef typename BaseT::MatrixT MatrixT;
162 
165 
166 }; // class RigidTransformN
167 
169 template <int N, typename T>
170 std::ostream &
171 operator<<(std::ostream & os, RigidTransformN<N, T> const & tr)
172 {
173  return os << tr.toString();
174 }
175 
176 } // namespace Thea
177 
178 #include "RigidTransform2.hpp"
179 #include "RigidTransform3.hpp"
180 
181 #endif
Eigen::Matrix< T, Size, 1, Options|((Options &Eigen::DontAlign)==0 &&Size==Eigen::Dynamic?Eigen::AutoAlign:Eigen::DontAlign), MaxRowsAtCompileTime, 1 > Vector
General 1D dense column vector template, alias for Eigen::Matrix<T, Size, 1,...>, with a custom align...
Definition: MatVec.hpp:146
RigidTransformN()
Default constructor.
std::string toString(char const *s)
Convert a C-string to a std::string object, returning an empty string if the input is a null pointer...
Real operator()(int i, int j) const
Get an element of the N x (N + 1) matrix representing this transform.
void setIdentity()
Set the transform to identity.
Root namespace for the Thea library.
RigidTransformT inverse() const
Get the inverse transform.
VectorT const & getTranslation() const
Get translation component.
AffineTransformN< N, T > AffineTransformT
N-dimensional affine transform type.
static RigidTransformT translation(VectorT const &translation_)
Construct a translation.
Vector< N, T > VectorT
N-dimensional vector.
Matrix< N, N+1, T > toMatrix() const
Convert to an N x (N + 1) transformation matrix.
static RigidTransformT _fromAffine(AffineTransformT const &aff_)
Construct from an affine transform, assuming it is rigid (use with caution since it can break the rig...
MatrixT const & getRotation() const
Get rotation component.
VectorT operator*(VectorT const &v) const
Apply this transform to a N-vector.
Matrix< N+1, N+1, T > homogeneous() const
Convert to an (N + 1) x (N + 1) transformation matrix in homogeneous coordinates (last row is identit...
A rigid transformation in N-space, consisting of a rotation followed by a translation.
RigidTransformT operator*(RigidTransformT const &rhs) const
Compose this transform with another.
AffineTransformT const & toAffine() const
Convert to a general affine transform.
Matrix< N, N, T > MatrixT
NxN matrix.
void _setRotation(MatrixT const &rot)
Set the rotation matrix (use with caution since it can break the rigidity guarantee).
RigidTransformN< N, T > RigidTransformT
N-dimensional rigid transform type.
[Internal] Base class for a rigid transformation in N-space, consisting of a rotation followed by a t...
void setTranslation(VectorT const &translation_)
Set translation component.
Eigen::Matrix< T, Rows, Cols, Options|((Options &Eigen::DontAlign)==0 &&(Rows==Eigen::Dynamic||Cols==Eigen::Dynamic)?Eigen::AutoAlign:Eigen::DontAlign), MaxRowsAtCompileTime, MaxColsAtCompileTime > Matrix
General 2D dense matrix template, alias for Eigen::Matrix with a custom default layout (row or column...
Definition: MatVec.hpp:114
static RigidTransformT const & identity()
Get the identity transform.
std::string toString() const
Get a string representing the transform.
RigidTransformN< N, U > cast() const
Cast the transform to a different scalar type.
An affine transform in N-dimensional space, where N is any positive (non-zero) integer and T is a fie...