RotationBase.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_ROTATIONBASE_H
26 #define EIGEN_ROTATIONBASE_H
27 
28 namespace Eigen {
29 
30 // forward declaration
31 namespace internal {
32 template<typename RotationDerived, typename MatrixType, bool IsVector=MatrixType::IsVectorAtCompileTime>
33 struct rotation_base_generic_product_selector;
34 }
35 
43 template<typename Derived, int _Dim>
45 {
46  public:
47  enum { Dim = _Dim };
49  typedef typename internal::traits<Derived>::Scalar Scalar;
50 
54 
55  public:
56  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
57  inline Derived& derived() { return *static_cast<Derived*>(this); }
58 
60  inline RotationMatrixType toRotationMatrix() const { return derived().toRotationMatrix(); }
61 
65  inline RotationMatrixType matrix() const { return derived().toRotationMatrix(); }
66 
68  inline Derived inverse() const { return derived().inverse(); }
69 
72  { return Transform<Scalar,Dim,Isometry>(*this) * t; }
73 
76  { return toRotationMatrix() * s.factor(); }
77 
84  template<typename OtherDerived>
85  EIGEN_STRONG_INLINE typename internal::rotation_base_generic_product_selector<Derived,OtherDerived,OtherDerived::IsVectorAtCompileTime>::ReturnType
87  { return internal::rotation_base_generic_product_selector<Derived,OtherDerived>::run(derived(), e.derived()); }
88 
90  template<typename OtherDerived> friend
91  inline RotationMatrixType operator*(const EigenBase<OtherDerived>& l, const Derived& r)
92  { return l.derived() * r.toRotationMatrix(); }
93 
95  friend inline Transform<Scalar,Dim,Affine> operator*(const DiagonalMatrix<Scalar,Dim>& l, const Derived& r)
96  {
98  res.linear().applyOnTheLeft(l);
99  return res;
100  }
101 
103  template<int Mode, int Options>
105  { return toRotationMatrix() * t; }
106 
107  template<typename OtherVectorType>
108  inline VectorType _transformVector(const OtherVectorType& v) const
109  { return toRotationMatrix() * v; }
110 };
111 
112 namespace internal {
113 
114 // implementation of the generic product rotation * matrix
115 template<typename RotationDerived, typename MatrixType>
116 struct rotation_base_generic_product_selector<RotationDerived,MatrixType,false>
117 {
118  enum { Dim = RotationDerived::Dim };
119  typedef Matrix<typename RotationDerived::Scalar,Dim,Dim> ReturnType;
120  static inline ReturnType run(const RotationDerived& r, const MatrixType& m)
121  { return r.toRotationMatrix() * m; }
122 };
123 
124 template<typename RotationDerived, typename Scalar, int Dim, int MaxDim>
125 struct rotation_base_generic_product_selector< RotationDerived, DiagonalMatrix<Scalar,Dim,MaxDim>, false >
126 {
127  typedef Transform<Scalar,Dim,Affine> ReturnType;
128  static inline ReturnType run(const RotationDerived& r, const DiagonalMatrix<Scalar,Dim,MaxDim>& m)
129  {
130  ReturnType res(r);
131  res.linear() *= m;
132  return res;
133  }
134 };
135 
136 template<typename RotationDerived,typename OtherVectorType>
137 struct rotation_base_generic_product_selector<RotationDerived,OtherVectorType,true>
138 {
139  enum { Dim = RotationDerived::Dim };
140  typedef Matrix<typename RotationDerived::Scalar,Dim,1> ReturnType;
141  static EIGEN_STRONG_INLINE ReturnType run(const RotationDerived& r, const OtherVectorType& v)
142  {
143  return r._transformVector(v);
144  }
145 };
146 
147 } // end namespace internal
148 
153 template<typename _Scalar, int _Rows, int _Cols, int _Storage, int _MaxRows, int _MaxCols>
154 template<typename OtherDerived>
157 {
158  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix,int(OtherDerived::Dim),int(OtherDerived::Dim))
159  *this = r.toRotationMatrix();
160 }
161 
166 template<typename _Scalar, int _Rows, int _Cols, int _Storage, int _MaxRows, int _MaxCols>
167 template<typename OtherDerived>
171 {
172  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix,int(OtherDerived::Dim),int(OtherDerived::Dim))
173  return *this = r.toRotationMatrix();
174 }
175 
176 namespace internal {
177 
196 template<typename Scalar, int Dim>
197 static inline Matrix<Scalar,2,2> toRotationMatrix(const Scalar& s)
198 {
199  EIGEN_STATIC_ASSERT(Dim==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
200  return Rotation2D<Scalar>(s).toRotationMatrix();
201 }
202 
203 template<typename Scalar, int Dim, typename OtherDerived>
204 static inline Matrix<Scalar,Dim,Dim> toRotationMatrix(const RotationBase<OtherDerived,Dim>& r)
205 {
206  return r.toRotationMatrix();
207 }
208 
209 template<typename Scalar, int Dim, typename OtherDerived>
210 static inline const MatrixBase<OtherDerived>& toRotationMatrix(const MatrixBase<OtherDerived>& mat)
211 {
212  EIGEN_STATIC_ASSERT(OtherDerived::RowsAtCompileTime==Dim && OtherDerived::ColsAtCompileTime==Dim,
213  YOU_MADE_A_PROGRAMMING_MISTAKE)
214  return mat;
215 }
216 
217 } // end namespace internal
218 
219 } // end namespace Eigen
220 
221 #endif // EIGEN_ROTATIONBASE_H