Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Rotation2D.h Source File

Rotation2D.h

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 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_ROTATION2D_H
00011 #define EIGEN_ROTATION2D_H
00012 
00013 namespace Eigen { 
00014 
00015 /** \geometry_module \ingroup Geometry_Module
00016   *
00017   * \class Rotation2D
00018   *
00019   * \brief Represents a rotation/orientation in a 2 dimensional space.
00020   *
00021   * \param _Scalar the scalar type, i.e., the type of the coefficients
00022   *
00023   * This class is equivalent to a single scalar representing a counter clock wise rotation
00024   * as a single angle in radian. It provides some additional features such as the automatic
00025   * conversion from/to a 2x2 rotation matrix. Moreover this class aims to provide a similar
00026   * interface to Quaternion in order to facilitate the writing of generic algorithms
00027   * dealing with rotations.
00028   *
00029   * \sa class Quaternion, class Transform
00030   */
00031 
00032 namespace internal {
00033 
00034 template<typename _Scalar> struct traits<Rotation2D<_Scalar> >
00035 {
00036   typedef _Scalar Scalar;
00037 };
00038 } // end namespace internal
00039 
00040 template<typename _Scalar>
00041 class Rotation2D  : public RotationBase<Rotation2D<_Scalar>,2>
00042 {
00043   typedef RotationBase<Rotation2D<_Scalar>,2> Base ;
00044 
00045 public:
00046 
00047   using Base::operator*;
00048 
00049   enum { Dim = 2 };
00050   /** the scalar type of the coefficients */
00051   typedef _Scalar Scalar;
00052   typedef Matrix<Scalar,2,1> Vector2;
00053   typedef Matrix<Scalar,2,2> Matrix2;
00054 
00055 protected:
00056 
00057   Scalar m_angle;
00058 
00059 public:
00060 
00061   /** Construct a 2D counter clock wise rotation from the angle \a a in radian. */
00062   inline Rotation2D(const Scalar& a) : m_angle(a) {}
00063   
00064   /** Default constructor wihtout initialization. The represented rotation is undefined. */
00065   Rotation2D() {}
00066 
00067   /** \returns the rotation angle */
00068   inline Scalar angle () const { return m_angle; }
00069 
00070   /** \returns a read-write reference to the rotation angle */
00071   inline Scalar& angle () { return m_angle; }
00072 
00073   /** \returns the inverse rotation */
00074   inline Rotation2D  inverse () const { return -m_angle; }
00075 
00076   /** Concatenates two rotations */
00077   inline Rotation2D  operator*(const Rotation2D & other) const
00078   { return m_angle + other.m_angle; }
00079 
00080   /** Concatenates two rotations */
00081   inline Rotation2D & operator*=(const Rotation2D & other)
00082   { m_angle += other.m_angle; return *this; }
00083 
00084   /** Applies the rotation to a 2D vector */
00085   Vector2 operator* (const Vector2& vec) const
00086   { return toRotationMatrix() * vec; }
00087   
00088   template<typename Derived>
00089   Rotation2D & fromRotationMatrix(const MatrixBase<Derived>& m);
00090   Matrix2 toRotationMatrix() const;
00091 
00092   /** \returns the spherical interpolation between \c *this and \a other using
00093     * parameter \a t. It is in fact equivalent to a linear interpolation.
00094     */
00095   inline Rotation2D  slerp (const Scalar& t, const Rotation2D & other) const
00096   { return m_angle * (1-t) + other.angle () * t; }
00097 
00098   /** \returns \c *this with scalar type casted to \a NewScalarType
00099     *
00100     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
00101     * then this function smartly returns a const reference to \c *this.
00102     */
00103   template<typename NewScalarType>
00104   inline typename internal::cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type cast () const
00105   { return typename internal::cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type(*this); }
00106 
00107   /** Copy constructor with scalar type conversion */
00108   template<typename OtherScalarType>
00109   inline explicit Rotation2D(const Rotation2D<OtherScalarType> & other)
00110   {
00111     m_angle = Scalar(other.angle ());
00112   }
00113 
00114   static inline Rotation2D  Identity() { return Rotation2D(0); }
00115 
00116   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
00117     * determined by \a prec.
00118     *
00119     * \sa MatrixBase::isApprox() */
00120   bool isApprox (const Rotation2D & other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
00121   { return internal::isApprox (m_angle,other.m_angle, prec); }
00122 };
00123 
00124 /** \ingroup Geometry_Module
00125   * single precision 2D rotation type */
00126 typedef Rotation2D<float>  Rotation2Df;
00127 /** \ingroup Geometry_Module
00128   * double precision 2D rotation type */
00129 typedef Rotation2D<double>  Rotation2Dd;
00130 
00131 /** Set \c *this from a 2x2 rotation matrix \a mat.
00132   * In other words, this function extract the rotation angle
00133   * from the rotation matrix.
00134   */
00135 template<typename Scalar>
00136 template<typename Derived>
00137 Rotation2D<Scalar> & Rotation2D<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
00138 {
00139   using std::atan2;
00140   EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime==2 && Derived::ColsAtCompileTime==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
00141   m_angle = atan2(mat.coeff(1,0), mat.coeff(0,0));
00142   return *this;
00143 }
00144 
00145 /** Constructs and \returns an equivalent 2x2 rotation matrix.
00146   */
00147 template<typename Scalar>
00148 typename Rotation2D<Scalar>::Matrix2
00149 Rotation2D<Scalar>::toRotationMatrix(void) const
00150 {
00151   using std::sin;
00152   using std::cos;
00153   Scalar sinA = sin(m_angle);
00154   Scalar cosA = cos(m_angle);
00155   return (Matrix2() << cosA, -sinA, sinA, cosA).finished();
00156 }
00157 
00158 } // end namespace Eigen
00159 
00160 #endif // EIGEN_ROTATION2D_H