Thea
AffineTransformN.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_AffineTransformN_hpp__
16 #define __Thea_AffineTransformN_hpp__
17 
18 #include "Common.hpp"
19 #include "MatVec.hpp"
20 #include <sstream>
21 
22 namespace Thea {
23 
24 // Forward declarations
25 template <int N, typename T> class AffineTransformN;
26 
27 namespace Internal {
28 
35 template <int N, typename T>
36 class /* THEA_DLL_LOCAL */ AffineTransformNBase
37 {
38  public:
42 
43  THEA_DECL_SMART_POINTERS(AffineTransformT)
44 
45 
47 
49  AffineTransformNBase(MatrixT const & linear_, VectorT const & translation_) : linear(linear_), trans(translation_) {}
50 
52  template <typename U> AffineTransformN<N, U> cast() const
53  {
54  return AffineTransformN<N, U>(linear.template cast<U>(), trans.template cast<U>());
55  }
56 
58  static AffineTransformT scaling(VectorT const & s)
59  {
60  return AffineTransformT(Math::scaling(s), VectorT::Zero());
61  }
62 
64  static AffineTransformT scaling(T const & s)
65  {
66  return AffineTransformT(Math::scaling<N>(s), VectorT::Zero());;
67  }
68 
70  static AffineTransformT translation(VectorT const & v)
71  {
72  return AffineTransformT(MatrixT::Identity(), v);
73  }
74 
76  void setIdentity() { *this = identity(); }
77 
79  MatrixT const & getLinear() const { return linear; }
80 
82  MatrixT & getLinear() { return linear; }
83 
85  void setLinear(MatrixT const & linear_) { linear = linear_; }
86 
88  VectorT const & getTranslation() const { return trans; }
89 
91  VectorT & getTranslation() { return trans; }
92 
94  void setTranslation(VectorT const & translation_) { trans = translation_; }
95 
98  {
100  for (intx i = 0; i < N; ++i)
101  {
102  for (intx j = 0; j < N; ++j)
103  m(i, j) = linear(i, j);
104 
105  m(i, N) = trans[i];
106  }
107 
108  return m;
109  }
110 
113  {
115  for (intx i = 0; i < N; ++i)
116  {
117  for (intx j = 0; j < N; ++j)
118  m(i, j) = linear(i, j);
119 
120  m(i, N) = trans[i];
121  }
122 
123  return m;
124  }
125 
127  AffineTransformT inverse() const
128  {
129  MatrixT inv = linear.inverse();
130  return AffineTransformT(inv, inv * (-trans));
131  }
132 
134  T operator()(intx i, intx j) const
135  {
136  debugAssertM(i >= 0 && i < N && j >= 0 && j <= N, "AffineTransformT: Index out of bounds");
137  return j == N ? trans[i] : linear(i, j);
138  }
139 
141  T & operator()(intx i, intx j)
142  {
143  debugAssertM(i >= 0 && i < N && j >= 0 && j <= N, "AffineTransformT: Index out of bounds");
144  return j == N ? trans[i] : linear(i, j);
145  }
146 
148  AffineTransformT operator*(AffineTransformT const & rhs) const
149  {
150  return AffineTransformT(linear * rhs.linear, linear * rhs.trans + trans);
151  }
152 
154  VectorT operator*(VectorT const & v) const { return linear * v + trans; }
155 
157  std::string toString() const
158  {
159  std::ostringstream oss;
160  oss << "[L: " << Thea::toString(linear) << ", T: " << Thea::toString(trans) << ']';
161  return oss.str();
162  }
163 
165  static AffineTransformT const & identity()
166  {
167  static AffineTransformT const idty(MatrixT::Identity(), VectorT::Zero());
168  return idty;
169  }
170 
171  private:
172  MatrixT linear;
173  VectorT trans;
174 
175 }; // class AffineTransformNBase
176 
177 } // namespace Internal
178 
180 template <int N, typename T = Real>
181 class /* THEA_API */ AffineTransformN : public Internal::AffineTransformNBase<N, T>
182 {
183  private:
185 
186  public:
187  typedef typename BaseT::VectorT VectorT;
188  typedef typename BaseT::MatrixT MatrixT;
189 
192 
194  AffineTransformN(MatrixT const & linear_, VectorT const & translation_ = VectorT::Zero()) : BaseT(linear_, translation_) {}
195 
197  AffineTransformN(AffineTransformN const & src) : BaseT(src) {}
198 
199 }; // class AffineTransformN
200 
202 template <int N, typename T>
203 std::ostream &
204 operator<<(std::ostream & os, AffineTransformN<N, T> const & tr)
205 {
206  return os << tr.toString();
207 }
208 
209 } // namespace Thea
210 
211 #include "AffineTransform2.hpp"
212 #include "AffineTransform3.hpp"
213 
214 #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
std::ptrdiff_t intx
A signed integer suitable for indexing a structure held in memory.
Definition: Platform.hpp:161
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...
VectorT & getTranslation()
Get translation component.
T & operator()(intx i, intx j)
Get an element of the N x (N + 1) matrix representing this transform.
static AffineTransformT translation(VectorT const &v)
Construct a translation.
void setTranslation(VectorT const &translation_)
Set translation component.
Root namespace for the Thea library.
static AffineTransformT const & identity()
Get the identity transform.
VectorT const & getTranslation() const
Get translation component.
AffineTransformN(MatrixT const &linear_, VectorT const &translation_=VectorT::Zero())
Construct from a linear transform, followed by a translation.
static AffineTransformT scaling(T const &s)
Construct a uniform scaling transform.
AffineTransformT inverse() const
Get the inverse transform.
Matrix< N, N, T > scaling(Vector< N, T > const &s)
Matrix to scale a point by the corresponding scaling parameter along each dimension.
Definition: MatrixUtil.hpp:268
AffineTransformN()
Default constructor.
static AffineTransformT scaling(VectorT const &s)
Construct a scaling transform.
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...
AffineTransformN< N, T > AffineTransformT
N-dimensional affine transform type.
Matrix< N, N+1, T > toMatrix() const
Convert to an N x (N + 1) transformation matrix.
void setLinear(MatrixT const &linear_)
Set linear transform component.
MatrixT const & getLinear() const
Get linear transform component.
VectorT operator*(VectorT const &v) const
Apply this transform to a vector.
Matrix< N, N, T > MatrixT
NxN matrix.
[Internal] Base class for affine transform in N-dimensional space, where N is any positive (non-zero)...
std::string toString() const
Get a string representing the transform.
AffineTransformN< N, U > cast() const
Cast the transform to a different scalar type.
T operator()(intx i, intx j) const
Get an element of the N x (N + 1) matrix representing this transform.
void setIdentity()
Set the transform to identity.
AffineTransformNBase(MatrixT const &linear_, VectorT const &translation_)
Construct from a linear transform, followed by a translation.
Vector< N, T > VectorT
N-dimensional vector.
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
AffineTransformT operator*(AffineTransformT const &rhs) const
Compose this transform with another.
AffineTransformN(AffineTransformN const &src)
Copy constructor.
An affine transform in N-dimensional space, where N is any positive (non-zero) integer and T is a fie...
MatrixT & getLinear()
Get linear transform component.
void debugAssertM(CondT const &test, MessageT const &msg)
Check if a test condition is true, and immediately abort the program with an error code if not...
Definition: Common.hpp:52