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 namespace Eigen { 
00029 
00047 namespace internal {
00048 
00049 template<typename _Scalar> struct traits<Rotation2D<_Scalar> >
00050 {
00051   typedef _Scalar Scalar;
00052 };
00053 } // end namespace internal
00054 
00055 template<typename _Scalar>
00056 class Rotation2D : public RotationBase<Rotation2D<_Scalar>,2>
00057 {
00058   typedef RotationBase<Rotation2D<_Scalar>,2> Base;
00059 
00060 public:
00061 
00062   using Base::operator*;
00063 
00064   enum { Dim = 2 };
00066   typedef _Scalar Scalar;
00067   typedef Matrix<Scalar,2,1> Vector2;
00068   typedef Matrix<Scalar,2,2> Matrix2;
00069 
00070 protected:
00071 
00072   Scalar m_angle;
00073 
00074 public:
00075 
00077   inline Rotation2D(Scalar a) : m_angle(a) {}
00078 
00080   inline Scalar angle() const { return m_angle; }
00081 
00083   inline Scalar& angle() { return m_angle; }
00084 
00086   inline Rotation2D inverse() const { return -m_angle; }
00087 
00089   inline Rotation2D operator*(const Rotation2D& other) const
00090   { return m_angle + other.m_angle; }
00091 
00093   inline Rotation2D& operator*=(const Rotation2D& other)
00094   { m_angle += other.m_angle; return *this; }
00095 
00097   Vector2 operator* (const Vector2& vec) const
00098   { return toRotationMatrix() * vec; }
00099 
00100   template<typename Derived>
00101   Rotation2D& fromRotationMatrix(const MatrixBase<Derived>& m);
00102   Matrix2 toRotationMatrix(void) const;
00103 
00107   inline Rotation2D slerp(Scalar t, const Rotation2D& other) const
00108   { return m_angle * (1-t) + other.angle() * t; }
00109 
00115   template<typename NewScalarType>
00116   inline typename internal::cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type cast() const
00117   { return typename internal::cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type(*this); }
00118 
00120   template<typename OtherScalarType>
00121   inline explicit Rotation2D(const Rotation2D<OtherScalarType>& other)
00122   {
00123     m_angle = Scalar(other.angle());
00124   }
00125 
00126   static inline Rotation2D Identity() { return Rotation2D(0); }
00127 
00132   bool isApprox(const Rotation2D& other, typename NumTraits<Scalar>::Real prec = NumTraits<Scalar>::dummy_precision()) const
00133   { return internal::isApprox(m_angle,other.m_angle, prec); }
00134 };
00135 
00138 typedef Rotation2D<float> Rotation2Df;
00141 typedef Rotation2D<double> Rotation2Dd;
00142 
00147 template<typename Scalar>
00148 template<typename Derived>
00149 Rotation2D<Scalar>& Rotation2D<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
00150 {
00151   EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime==2 && Derived::ColsAtCompileTime==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
00152   m_angle = internal::atan2(mat.coeff(1,0), mat.coeff(0,0));
00153   return *this;
00154 }
00155 
00158 template<typename Scalar>
00159 typename Rotation2D<Scalar>::Matrix2
00160 Rotation2D<Scalar>::toRotationMatrix(void) const
00161 {
00162   Scalar sinA = internal::sin(m_angle);
00163   Scalar cosA = internal::cos(m_angle);
00164   return (Matrix2() << cosA, -sinA, sinA, cosA).finished();
00165 }
00166 
00167 } // end namespace Eigen
00168 
00169 #endif // EIGEN_ROTATION2D_H