Main MRPT website > C++ reference
MRPT logo

RotationBase.h

Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ROTATIONBASE_H
00026 #define EIGEN_ROTATIONBASE_H
00027 
00028 // forward declaration
00029 namespace internal {
00030 template<typename RotationDerived, typename MatrixType, bool IsVector=MatrixType::IsVectorAtCompileTime>
00031 struct rotation_base_generic_product_selector;
00032 }
00033 
00034 /** \class RotationBase
00035   *
00036   * \brief Common base class for compact rotation representations
00037   *
00038   * \param Derived is the derived type, i.e., a rotation type
00039   * \param _Dim the dimension of the space
00040   */
00041 template<typename Derived, int _Dim>
00042 class RotationBase
00043 {
00044   public:
00045     enum { Dim = _Dim };
00046     /** the scalar type of the coefficients */
00047     typedef typename internal::traits<Derived>::Scalar Scalar;
00048 
00049     /** corresponding linear transformation matrix type */
00050     typedef Matrix<Scalar,Dim,Dim> RotationMatrixType;
00051     typedef Matrix<Scalar,Dim,1> VectorType;
00052 
00053   public:
00054     inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
00055     inline Derived& derived() { return *static_cast<Derived*>(this); }
00056 
00057     /** \returns an equivalent rotation matrix */
00058     inline RotationMatrixType toRotationMatrix() const { return derived().toRotationMatrix(); }
00059 
00060     /** \returns an equivalent rotation matrix 
00061       * This function is added to be conform with the Transform class' naming scheme.
00062       */
00063     inline RotationMatrixType matrix() const { return derived().toRotationMatrix(); }
00064 
00065     /** \returns the inverse rotation */
00066     inline Derived inverse() const { return derived().inverse(); }
00067 
00068     /** \returns the concatenation of the rotation \c *this with a translation \a t */
00069     inline Transform<Scalar,Dim,Isometry> operator*(const Translation<Scalar,Dim>& t) const
00070     { return Transform<Scalar,Dim,Isometry>(*this) * t; }
00071 
00072     /** \returns the concatenation of the rotation \c *this with a uniform scaling \a s */
00073     inline RotationMatrixType operator*(const UniformScaling<Scalar>& s) const
00074     { return toRotationMatrix() * s.factor(); }
00075 
00076     /** \returns the concatenation of the rotation \c *this with a generic expression \a e
00077       * \a e can be:
00078       *  - a DimxDim linear transformation matrix
00079       *  - a DimxDim diagonal matrix (axis aligned scaling)
00080       *  - a vector of size Dim
00081       */
00082     template<typename OtherDerived>
00083     EIGEN_STRONG_INLINE typename internal::rotation_base_generic_product_selector<Derived,OtherDerived,OtherDerived::IsVectorAtCompileTime>::ReturnType
00084     operator*(const EigenBase<OtherDerived>& e) const
00085     { return internal::rotation_base_generic_product_selector<Derived,OtherDerived>::run(derived(), e.derived()); }
00086 
00087     /** \returns the concatenation of a linear transformation \a l with the rotation \a r */
00088     template<typename OtherDerived> friend
00089     inline RotationMatrixType operator*(const EigenBase<OtherDerived>& l, const Derived& r)
00090     { return l.derived() * r.toRotationMatrix(); }
00091 
00092     /** \returns the concatenation of a scaling \a l with the rotation \a r */
00093     friend inline Transform<Scalar,Dim,Affine> operator*(const DiagonalMatrix<Scalar,Dim>& l, const Derived& r)
00094     { 
00095       Transform<Scalar,Dim,Affine> res(r);
00096       res.linear().applyOnTheLeft(l);
00097       return res;
00098     }
00099 
00100     /** \returns the concatenation of the rotation \c *this with a transformation \a t */
00101     template<int Mode>
00102     inline Transform<Scalar,Dim,Mode> operator*(const Transform<Scalar,Dim,Mode>& t) const
00103     { return toRotationMatrix() * t; }
00104 
00105     template<typename OtherVectorType>
00106     inline VectorType _transformVector(const OtherVectorType& v) const
00107     { return toRotationMatrix() * v; }
00108 };
00109 
00110 namespace internal {
00111 
00112 // implementation of the generic product rotation * matrix
00113 template<typename RotationDerived, typename MatrixType>
00114 struct rotation_base_generic_product_selector<RotationDerived,MatrixType,false>
00115 {
00116   enum { Dim = RotationDerived::Dim };
00117   typedef Matrix<typename RotationDerived::Scalar,Dim,Dim> ReturnType;
00118   inline static ReturnType run(const RotationDerived& r, const MatrixType& m)
00119   { return r.toRotationMatrix() * m; }
00120 };
00121 
00122 template<typename RotationDerived, typename Scalar, int Dim, int MaxDim>
00123 struct rotation_base_generic_product_selector< RotationDerived, DiagonalMatrix<Scalar,Dim,MaxDim>, false >
00124 {
00125   typedef Transform<Scalar,Dim,Affine> ReturnType;
00126   inline static ReturnType run(const RotationDerived& r, const DiagonalMatrix<Scalar,Dim,MaxDim>& m)
00127   {
00128     ReturnType res(r);
00129     res.linear() *= m;
00130     return res;
00131   }
00132 };
00133 
00134 template<typename RotationDerived,typename OtherVectorType>
00135 struct rotation_base_generic_product_selector<RotationDerived,OtherVectorType,true>
00136 {
00137   enum { Dim = RotationDerived::Dim };
00138   typedef Matrix<typename RotationDerived::Scalar,Dim,1> ReturnType;
00139   EIGEN_STRONG_INLINE static ReturnType run(const RotationDerived& r, const OtherVectorType& v)
00140   {
00141     return r._transformVector(v);
00142   }
00143 };
00144 
00145 } // end namespace internal
00146 
00147 /** \geometry_module
00148   *
00149   * \brief Constructs a Dim x Dim rotation matrix from the rotation \a r
00150   */
00151 template<typename _Scalar, int _Rows, int _Cols, int _Storage, int _MaxRows, int _MaxCols>
00152 template<typename OtherDerived>
00153 Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>
00154 ::Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r)
00155 {
00156   EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix,int(OtherDerived::Dim),int(OtherDerived::Dim))
00157   *this = r.toRotationMatrix();
00158 }
00159 
00160 /** \geometry_module
00161   *
00162   * \brief Set a Dim x Dim rotation matrix from the rotation \a r
00163   */
00164 template<typename _Scalar, int _Rows, int _Cols, int _Storage, int _MaxRows, int _MaxCols>
00165 template<typename OtherDerived>
00166 Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>&
00167 Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>
00168 ::operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r)
00169 {
00170   EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Matrix,int(OtherDerived::Dim),int(OtherDerived::Dim))
00171   return *this = r.toRotationMatrix();
00172 }
00173 
00174 namespace internal {
00175 
00176 /** \internal
00177   *
00178   * Helper function to return an arbitrary rotation object to a rotation matrix.
00179   *
00180   * \param Scalar the numeric type of the matrix coefficients
00181   * \param Dim the dimension of the current space
00182   *
00183   * It returns a Dim x Dim fixed size matrix.
00184   *
00185   * Default specializations are provided for:
00186   *   - any scalar type (2D),
00187   *   - any matrix expression,
00188   *   - any type based on RotationBase (e.g., Quaternion, AngleAxis, Rotation2D)
00189   *
00190   * Currently toRotationMatrix is only used by Transform.
00191   *
00192   * \sa class Transform, class Rotation2D, class Quaternion, class AngleAxis
00193   */
00194 template<typename Scalar, int Dim>
00195 inline static Matrix<Scalar,2,2> toRotationMatrix(const Scalar& s)
00196 {
00197   EIGEN_STATIC_ASSERT(Dim==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
00198   return Rotation2D<Scalar>(s).toRotationMatrix();
00199 }
00200 
00201 template<typename Scalar, int Dim, typename OtherDerived>
00202 inline static Matrix<Scalar,Dim,Dim> toRotationMatrix(const RotationBase<OtherDerived,Dim>& r)
00203 {
00204   return r.toRotationMatrix();
00205 }
00206 
00207 template<typename Scalar, int Dim, typename OtherDerived>
00208 inline static const MatrixBase<OtherDerived>& toRotationMatrix(const MatrixBase<OtherDerived>& mat)
00209 {
00210   EIGEN_STATIC_ASSERT(OtherDerived::RowsAtCompileTime==Dim && OtherDerived::ColsAtCompileTime==Dim,
00211     YOU_MADE_A_PROGRAMMING_MISTAKE)
00212   return mat;
00213 }
00214 
00215 } // end namespace internal
00216 
00217 #endif // EIGEN_ROTATIONBASE_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011