• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 // no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
11 
12 namespace Eigen {
13 
14 // this file aims to contains the various representations of rotation/orientation
15 // in 2D and 3D space excepted Matrix and Quaternion.
16 
17 /** \class RotationBase
18   *
19   * \brief Common base class for compact rotation representations
20   *
21   * \param Derived is the derived type, i.e., a rotation type
22   * \param _Dim the dimension of the space
23   */
24 template<typename Derived, int _Dim>
25 class RotationBase
26 {
27   public:
28     enum { Dim = _Dim };
29     /** the scalar type of the coefficients */
30     typedef typename ei_traits<Derived>::Scalar Scalar;
31 
32     /** corresponding linear transformation matrix type */
33     typedef Matrix<Scalar,Dim,Dim> RotationMatrixType;
34 
derived()35     inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
derived()36     inline Derived& derived() { return *static_cast<Derived*>(this); }
37 
38     /** \returns an equivalent rotation matrix */
toRotationMatrix()39     inline RotationMatrixType toRotationMatrix() const { return derived().toRotationMatrix(); }
40 
41     /** \returns the inverse rotation */
inverse()42     inline Derived inverse() const { return derived().inverse(); }
43 
44     /** \returns the concatenation of the rotation \c *this with a translation \a t */
45     inline Transform<Scalar,Dim> operator*(const Translation<Scalar,Dim>& t) const
46     { return toRotationMatrix() * t; }
47 
48     /** \returns the concatenation of the rotation \c *this with a scaling \a s */
49     inline RotationMatrixType operator*(const Scaling<Scalar,Dim>& s) const
50     { return toRotationMatrix() * s; }
51 
52     /** \returns the concatenation of the rotation \c *this with an affine transformation \a t */
53     inline Transform<Scalar,Dim> operator*(const Transform<Scalar,Dim>& t) const
54     { return toRotationMatrix() * t; }
55 };
56 
57 /** \geometry_module
58   *
59   * Constructs a Dim x Dim rotation matrix from the rotation \a r
60   */
61 template<typename _Scalar, int _Rows, int _Cols, int _Storage, int _MaxRows, int _MaxCols>
62 template<typename OtherDerived>
63 Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>
Matrix(const RotationBase<OtherDerived,ColsAtCompileTime> & r)64 ::Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r)
65 {
66   EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix,int(OtherDerived::Dim),int(OtherDerived::Dim))
67   *this = r.toRotationMatrix();
68 }
69 
70 /** \geometry_module
71   *
72   * Set a Dim x Dim rotation matrix from the rotation \a r
73   */
74 template<typename _Scalar, int _Rows, int _Cols, int _Storage, int _MaxRows, int _MaxCols>
75 template<typename OtherDerived>
76 Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>&
77 Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>
78 ::operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r)
79 {
80   EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix,int(OtherDerived::Dim),int(OtherDerived::Dim))
81   return *this = r.toRotationMatrix();
82 }
83 
84 /** \internal
85   *
86   * Helper function to return an arbitrary rotation object to a rotation matrix.
87   *
88   * \param Scalar the numeric type of the matrix coefficients
89   * \param Dim the dimension of the current space
90   *
91   * It returns a Dim x Dim fixed size matrix.
92   *
93   * Default specializations are provided for:
94   *   - any scalar type (2D),
95   *   - any matrix expression,
96   *   - any type based on RotationBase (e.g., Quaternion, AngleAxis, Rotation2D)
97   *
98   * Currently ei_toRotationMatrix is only used by Transform.
99   *
100   * \sa class Transform, class Rotation2D, class Quaternion, class AngleAxis
101   */
102 template<typename Scalar, int Dim>
ei_toRotationMatrix(const Scalar & s)103 static inline Matrix<Scalar,2,2> ei_toRotationMatrix(const Scalar& s)
104 {
105   EIGEN_STATIC_ASSERT(Dim==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
106   return Rotation2D<Scalar>(s).toRotationMatrix();
107 }
108 
109 template<typename Scalar, int Dim, typename OtherDerived>
ei_toRotationMatrix(const RotationBase<OtherDerived,Dim> & r)110 static inline Matrix<Scalar,Dim,Dim> ei_toRotationMatrix(const RotationBase<OtherDerived,Dim>& r)
111 {
112   return r.toRotationMatrix();
113 }
114 
115 template<typename Scalar, int Dim, typename OtherDerived>
ei_toRotationMatrix(const MatrixBase<OtherDerived> & mat)116 static inline const MatrixBase<OtherDerived>& ei_toRotationMatrix(const MatrixBase<OtherDerived>& mat)
117 {
118   EIGEN_STATIC_ASSERT(OtherDerived::RowsAtCompileTime==Dim && OtherDerived::ColsAtCompileTime==Dim,
119     YOU_MADE_A_PROGRAMMING_MISTAKE)
120   return mat;
121 }
122 
123 } // end namespace Eigen
124