MOAB  4.9.3pre
AngleAxis.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 <[email protected]>
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_ANGLEAXIS_H
00011 #define EIGEN_ANGLEAXIS_H
00012 
00013 namespace Eigen { 
00014 
00041 namespace internal {
00042 template<typename _Scalar> struct traits<AngleAxis<_Scalar> >
00043 {
00044   typedef _Scalar Scalar;
00045 };
00046 }
00047 
00048 template<typename _Scalar>
00049 class AngleAxis : public RotationBase<AngleAxis<_Scalar>,3>
00050 {
00051   typedef RotationBase<AngleAxis<_Scalar>,3> Base;
00052 
00053 public:
00054 
00055   using Base::operator*;
00056 
00057   enum { Dim = 3 };
00059   typedef _Scalar Scalar;
00060   typedef Matrix<Scalar,3,3> Matrix3;
00061   typedef Matrix<Scalar,3,1> Vector3;
00062   typedef Quaternion<Scalar> QuaternionType;
00063 
00064 protected:
00065 
00066   Vector3 m_axis;
00067   Scalar m_angle;
00068 
00069 public:
00070 
00072   AngleAxis() {}
00078   template<typename Derived>
00079   inline AngleAxis(const Scalar& angle, const MatrixBase<Derived>& axis) : m_axis(axis), m_angle(angle) {}
00083   template<typename QuatDerived> inline explicit AngleAxis(const QuaternionBase<QuatDerived>& q) { *this = q; }
00085   template<typename Derived>
00086   inline explicit AngleAxis(const MatrixBase<Derived>& m) { *this = m; }
00087 
00089   Scalar angle() const { return m_angle; }
00091   Scalar& angle() { return m_angle; }
00092 
00094   const Vector3& axis() const { return m_axis; }
00099   Vector3& axis() { return m_axis; }
00100 
00102   inline QuaternionType operator* (const AngleAxis& other) const
00103   { return QuaternionType(*this) * QuaternionType(other); }
00104 
00106   inline QuaternionType operator* (const QuaternionType& other) const
00107   { return QuaternionType(*this) * other; }
00108 
00110   friend inline QuaternionType operator* (const QuaternionType& a, const AngleAxis& b)
00111   { return a * QuaternionType(b); }
00112 
00114   AngleAxis inverse() const
00115   { return AngleAxis(-m_angle, m_axis); }
00116 
00117   template<class QuatDerived>
00118   AngleAxis& operator=(const QuaternionBase<QuatDerived>& q);
00119   template<typename Derived>
00120   AngleAxis& operator=(const MatrixBase<Derived>& m);
00121 
00122   template<typename Derived>
00123   AngleAxis& fromRotationMatrix(const MatrixBase<Derived>& m);
00124   Matrix3 toRotationMatrix(void) const;
00125 
00131   template<typename NewScalarType>
00132   inline typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type cast() const
00133   { return typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type(*this); }
00134 
00136   template<typename OtherScalarType>
00137   inline explicit AngleAxis(const AngleAxis<OtherScalarType>& other)
00138   {
00139     m_axis = other.axis().template cast<Scalar>();
00140     m_angle = Scalar(other.angle());
00141   }
00142 
00143   static inline const AngleAxis Identity() { return AngleAxis(Scalar(0), Vector3::UnitX()); }
00144 
00149   bool isApprox(const AngleAxis& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
00150   { return m_axis.isApprox(other.m_axis, prec) && internal::isApprox(m_angle,other.m_angle, prec); }
00151 };
00152 
00155 typedef AngleAxis<float> AngleAxisf;
00158 typedef AngleAxis<double> AngleAxisd;
00159 
00165 template<typename Scalar>
00166 template<typename QuatDerived>
00167 AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const QuaternionBase<QuatDerived>& q)
00168 {
00169   using std::atan2;
00170   Scalar n = q.vec().norm();
00171   if(n<NumTraits<Scalar>::epsilon())
00172     n = q.vec().stableNorm();
00173   if (n > Scalar(0))
00174   {
00175     m_angle = Scalar(2)*atan2(n, q.w());
00176     m_axis  = q.vec() / n;
00177   }
00178   else
00179   {
00180     m_angle = Scalar(0);
00181     m_axis << Scalar(1), Scalar(0), Scalar(0);
00182   }
00183   return *this;
00184 }
00185 
00188 template<typename Scalar>
00189 template<typename Derived>
00190 AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const MatrixBase<Derived>& mat)
00191 {
00192   // Since a direct conversion would not be really faster,
00193   // let's use the robust Quaternion implementation:
00194   return *this = QuaternionType(mat);
00195 }
00196 
00200 template<typename Scalar>
00201 template<typename Derived>
00202 AngleAxis<Scalar>& AngleAxis<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
00203 {
00204   return *this = QuaternionType(mat);
00205 }
00206 
00209 template<typename Scalar>
00210 typename AngleAxis<Scalar>::Matrix3
00211 AngleAxis<Scalar>::toRotationMatrix(void) const
00212 {
00213   using std::sin;
00214   using std::cos;
00215   Matrix3 res;
00216   Vector3 sin_axis  = sin(m_angle) * m_axis;
00217   Scalar c = cos(m_angle);
00218   Vector3 cos1_axis = (Scalar(1)-c) * m_axis;
00219 
00220   Scalar tmp;
00221   tmp = cos1_axis.x() * m_axis.y();
00222   res.coeffRef(0,1) = tmp - sin_axis.z();
00223   res.coeffRef(1,0) = tmp + sin_axis.z();
00224 
00225   tmp = cos1_axis.x() * m_axis.z();
00226   res.coeffRef(0,2) = tmp + sin_axis.y();
00227   res.coeffRef(2,0) = tmp - sin_axis.y();
00228 
00229   tmp = cos1_axis.y() * m_axis.z();
00230   res.coeffRef(1,2) = tmp - sin_axis.x();
00231   res.coeffRef(2,1) = tmp + sin_axis.x();
00232 
00233   res.diagonal() = (cos1_axis.cwiseProduct(m_axis)).array() + c;
00234 
00235   return res;
00236 }
00237 
00238 } // end namespace Eigen
00239 
00240 #endif // EIGEN_ANGLEAXIS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines