![]() |
Eigen
3.2.8
|
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_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) {} 00081 template<typename QuatDerived> inline explicit AngleAxis(const QuaternionBase<QuatDerived>& q) { *this = q; } 00083 template<typename Derived> 00084 inline explicit AngleAxis(const MatrixBase<Derived>& m) { *this = m; } 00085 00087 Scalar angle() const { return m_angle; } 00089 Scalar& angle() { return m_angle; } 00090 00092 const Vector3& axis() const { return m_axis; } 00097 Vector3& axis() { return m_axis; } 00098 00100 inline QuaternionType operator* (const AngleAxis& other) const 00101 { return QuaternionType(*this) * QuaternionType(other); } 00102 00104 inline QuaternionType operator* (const QuaternionType& other) const 00105 { return QuaternionType(*this) * other; } 00106 00108 friend inline QuaternionType operator* (const QuaternionType& a, const AngleAxis& b) 00109 { return a * QuaternionType(b); } 00110 00112 AngleAxis inverse() const 00113 { return AngleAxis(-m_angle, m_axis); } 00114 00115 template<class QuatDerived> 00116 AngleAxis& operator=(const QuaternionBase<QuatDerived>& q); 00117 template<typename Derived> 00118 AngleAxis& operator=(const MatrixBase<Derived>& m); 00119 00120 template<typename Derived> 00121 AngleAxis& fromRotationMatrix(const MatrixBase<Derived>& m); 00122 Matrix3 toRotationMatrix(void) const; 00123 00129 template<typename NewScalarType> 00130 inline typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type cast() const 00131 { return typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type(*this); } 00132 00134 template<typename OtherScalarType> 00135 inline explicit AngleAxis(const AngleAxis<OtherScalarType>& other) 00136 { 00137 m_axis = other.axis().template cast<Scalar>(); 00138 m_angle = Scalar(other.angle()); 00139 } 00140 00141 static inline const AngleAxis Identity() { return AngleAxis(Scalar(0), Vector3::UnitX()); } 00142 00147 bool isApprox(const AngleAxis& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const 00148 { return m_axis.isApprox(other.m_axis, prec) && internal::isApprox(m_angle,other.m_angle, prec); } 00149 }; 00150 00153 typedef AngleAxis<float> AngleAxisf; 00156 typedef AngleAxis<double> AngleAxisd; 00157 00164 template<typename Scalar> 00165 template<typename QuatDerived> 00166 AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const QuaternionBase<QuatDerived>& q) 00167 { 00168 using std::acos; 00169 using std::min; 00170 using std::max; 00171 using std::sqrt; 00172 Scalar n2 = q.vec().squaredNorm(); 00173 if (n2 < NumTraits<Scalar>::dummy_precision()*NumTraits<Scalar>::dummy_precision()) 00174 { 00175 m_angle = Scalar(0); 00176 m_axis << Scalar(1), Scalar(0), Scalar(0); 00177 } 00178 else 00179 { 00180 m_angle = Scalar(2)*acos((min)((max)(Scalar(-1),q.w()),Scalar(1))); 00181 m_axis = q.vec() / sqrt(n2); 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