Main MRPT website > C++ reference
MRPT logo

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



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