00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef EIGEN_QUATERNION_H
00027 #define EIGEN_QUATERNION_H
00028
00029
00030
00031
00032
00033
00034 namespace internal {
00035 template<typename Other,
00036 int OtherRows=Other::RowsAtCompileTime,
00037 int OtherCols=Other::ColsAtCompileTime>
00038 struct quaternionbase_assign_impl;
00039 }
00040
00041 template<class Derived>
00042 class QuaternionBase : public RotationBase<Derived, 3>
00043 {
00044 typedef RotationBase<Derived, 3> Base;
00045 public:
00046 using Base::operator*;
00047 using Base::derived;
00048
00049 typedef typename internal::traits<Derived>::Scalar Scalar;
00050 typedef typename NumTraits<Scalar>::Real RealScalar;
00051 typedef typename internal::traits<Derived>::Coefficients Coefficients;
00052 enum {
00053 Flags = Eigen::internal::traits<Derived>::Flags
00054 };
00055
00056
00058 typedef Matrix<Scalar,3,1> Vector3;
00060 typedef Matrix<Scalar,3,3> Matrix3;
00062 typedef AngleAxis<Scalar> AngleAxisType;
00063
00064
00065
00067 inline Scalar x() const { return this->derived().coeffs().coeff(0); }
00069 inline Scalar y() const { return this->derived().coeffs().coeff(1); }
00071 inline Scalar z() const { return this->derived().coeffs().coeff(2); }
00073 inline Scalar w() const { return this->derived().coeffs().coeff(3); }
00074
00076 inline Scalar& x() { return this->derived().coeffs().coeffRef(0); }
00078 inline Scalar& y() { return this->derived().coeffs().coeffRef(1); }
00080 inline Scalar& z() { return this->derived().coeffs().coeffRef(2); }
00082 inline Scalar& w() { return this->derived().coeffs().coeffRef(3); }
00083
00085 inline const VectorBlock<const Coefficients,3> vec() const { return coeffs().template head<3>(); }
00086
00088 inline VectorBlock<Coefficients,3> vec() { return coeffs().template head<3>(); }
00089
00091 inline const typename internal::traits<Derived>::Coefficients& coeffs() const { return derived().coeffs(); }
00092
00094 inline typename internal::traits<Derived>::Coefficients& coeffs() { return derived().coeffs(); }
00095
00096 EIGEN_STRONG_INLINE QuaternionBase<Derived>& operator=(const QuaternionBase<Derived>& other);
00097 template<class OtherDerived> EIGEN_STRONG_INLINE Derived& operator=(const QuaternionBase<OtherDerived>& other);
00098
00099
00100
00101
00102
00103
00104
00105
00106 Derived& operator=(const AngleAxisType& aa);
00107 template<class OtherDerived> Derived& operator=(const MatrixBase<OtherDerived>& m);
00108
00112 inline static Quaternion<Scalar> Identity() { return Quaternion<Scalar>(1, 0, 0, 0); }
00113
00116 inline QuaternionBase& setIdentity() { coeffs() << 0, 0, 0, 1; return *this; }
00117
00121 inline Scalar squaredNorm() const { return coeffs().squaredNorm(); }
00122
00126 inline Scalar norm() const { return coeffs().norm(); }
00127
00130 inline void normalize() { coeffs().normalize(); }
00133 inline Quaternion<Scalar> normalized() const { return Quaternion<Scalar>(coeffs().normalized()); }
00134
00140 template<class OtherDerived> inline Scalar dot(const QuaternionBase<OtherDerived>& other) const { return coeffs().dot(other.coeffs()); }
00141
00142 template<class OtherDerived> Scalar angularDistance(const QuaternionBase<OtherDerived>& other) const;
00143
00145 Matrix3 toRotationMatrix() const;
00146
00148 template<typename Derived1, typename Derived2>
00149 Derived& setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
00150
00151 template<class OtherDerived> EIGEN_STRONG_INLINE Quaternion<Scalar> operator* (const QuaternionBase<OtherDerived>& q) const;
00152 template<class OtherDerived> EIGEN_STRONG_INLINE Derived& operator*= (const QuaternionBase<OtherDerived>& q);
00153
00155 Quaternion<Scalar> inverse() const;
00156
00158 Quaternion<Scalar> conjugate() const;
00159
00164 template<class OtherDerived> Quaternion<Scalar> slerp(Scalar t, const QuaternionBase<OtherDerived>& other) const;
00165
00170 template<class OtherDerived>
00171 bool isApprox(const QuaternionBase<OtherDerived>& other, RealScalar prec = NumTraits<Scalar>::dummy_precision()) const
00172 { return coeffs().isApprox(other.coeffs(), prec); }
00173
00175 EIGEN_STRONG_INLINE Vector3 _transformVector(Vector3 v) const;
00176
00182 template<typename NewScalarType>
00183 inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type cast() const
00184 {
00185 return typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type(
00186 coeffs().template cast<NewScalarType>());
00187 }
00188
00189 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
00190 # include EIGEN_QUATERNIONBASE_PLUGIN
00191 #endif
00192 };
00193
00194
00195
00196
00197
00220 namespace internal {
00221 template<typename _Scalar,int _Options>
00222 struct traits<Quaternion<_Scalar,_Options> >
00223 {
00224 typedef Quaternion<_Scalar,_Options> PlainObject;
00225 typedef _Scalar Scalar;
00226 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
00227 enum{
00228 IsAligned = bool(EIGEN_ALIGN) && ((int(_Options)&Aligned)==Aligned),
00229 Flags = IsAligned ? (AlignedBit | LvalueBit) : LvalueBit
00230 };
00231 };
00232 }
00233
00234 template<typename _Scalar, int _Options>
00235 class Quaternion : public QuaternionBase<Quaternion<_Scalar,_Options> >{
00236 typedef QuaternionBase<Quaternion<_Scalar,_Options> > Base;
00237 public:
00238 typedef _Scalar Scalar;
00239
00240 EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Quaternion)
00241 using Base::operator*=;
00242
00243 typedef typename internal::traits<Quaternion<Scalar,_Options> >::Coefficients Coefficients;
00244 typedef typename Base::AngleAxisType AngleAxisType;
00245
00247 inline Quaternion() {}
00248
00256 inline Quaternion(Scalar w, Scalar x, Scalar y, Scalar z) : m_coeffs(x, y, z, w){}
00257
00259 inline Quaternion(const Scalar* data) : m_coeffs(data) {}
00260
00262 template<class Derived> EIGEN_STRONG_INLINE Quaternion(const QuaternionBase<Derived>& other) { this->Base::operator=(other); }
00263
00265 explicit inline Quaternion(const AngleAxisType& aa) { *this = aa; }
00266
00271 template<typename Derived>
00272 explicit inline Quaternion(const MatrixBase<Derived>& other) { *this = other; }
00273
00274 inline Coefficients& coeffs() { return m_coeffs;}
00275 inline const Coefficients& coeffs() const { return m_coeffs;}
00276
00277 protected:
00278 Coefficients m_coeffs;
00279
00280 #ifndef EIGEN_PARSED_BY_DOXYGEN
00281 EIGEN_STRONG_INLINE static void _check_template_params()
00282 {
00283 EIGEN_STATIC_ASSERT( (_Options & DontAlign) == _Options,
00284 INVALID_MATRIX_TEMPLATE_PARAMETERS)
00285 }
00286 #endif
00287 };
00288
00291 typedef Quaternion<float> Quaternionf;
00294 typedef Quaternion<double> Quaterniond;
00295
00296
00297
00298
00299
00300 namespace internal {
00301 template<typename _Scalar, int _Options>
00302 struct traits<Map<Quaternion<_Scalar>, _Options> >:
00303 traits<Quaternion<_Scalar, _Options> >
00304 {
00305 typedef _Scalar Scalar;
00306 typedef Map<Matrix<_Scalar,4,1>, _Options> Coefficients;
00307
00308 typedef traits<Quaternion<_Scalar, _Options> > TraitsBase;
00309 enum {
00310 IsAligned = TraitsBase::IsAligned,
00311
00312 Flags = TraitsBase::Flags
00313 };
00314 };
00315 }
00316
00317 namespace internal {
00318 template<typename _Scalar, int _Options>
00319 struct traits<Map<const Quaternion<_Scalar>, _Options> >:
00320 traits<Quaternion<_Scalar> >
00321 {
00322 typedef _Scalar Scalar;
00323 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
00324
00325 typedef traits<Quaternion<_Scalar, _Options> > TraitsBase;
00326 enum {
00327 IsAligned = TraitsBase::IsAligned,
00328 Flags = TraitsBase::Flags & ~LvalueBit
00329 };
00330 };
00331 }
00332
00343 template<typename _Scalar, int _Options>
00344 class Map<const Quaternion<_Scalar>, _Options >
00345 : public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
00346 {
00347 typedef QuaternionBase<Map<const Quaternion<_Scalar>, _Options> > Base;
00348
00349 public:
00350 typedef _Scalar Scalar;
00351 typedef typename internal::traits<Map>::Coefficients Coefficients;
00352 EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Map)
00353 using Base::operator*=;
00354
00361 EIGEN_STRONG_INLINE Map(const Scalar* coeffs) : m_coeffs(coeffs) {}
00362
00363 inline const Coefficients& coeffs() const { return m_coeffs;}
00364
00365 protected:
00366 const Coefficients m_coeffs;
00367 };
00368
00379 template<typename _Scalar, int _Options>
00380 class Map<Quaternion<_Scalar>, _Options >
00381 : public QuaternionBase<Map<Quaternion<_Scalar>, _Options> >
00382 {
00383 typedef QuaternionBase<Map<Quaternion<_Scalar>, _Options> > Base;
00384
00385 public:
00386 typedef _Scalar Scalar;
00387 typedef typename internal::traits<Map>::Coefficients Coefficients;
00388 EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Map)
00389 using Base::operator*=;
00390
00397 EIGEN_STRONG_INLINE Map(Scalar* coeffs) : m_coeffs(coeffs) {}
00398
00399 inline Coefficients& coeffs() { return m_coeffs; }
00400 inline const Coefficients& coeffs() const { return m_coeffs; }
00401
00402 protected:
00403 Coefficients m_coeffs;
00404 };
00405
00408 typedef Map<Quaternion<float>, 0> QuaternionMapf;
00411 typedef Map<Quaternion<double>, 0> QuaternionMapd;
00414 typedef Map<Quaternion<float>, Aligned> QuaternionMapAlignedf;
00417 typedef Map<Quaternion<double>, Aligned> QuaternionMapAlignedd;
00418
00419
00420
00421
00422
00423
00424
00425 namespace internal {
00426 template<int Arch, class Derived1, class Derived2, typename Scalar, int _Options> struct quat_product
00427 {
00428 EIGEN_STRONG_INLINE static Quaternion<Scalar> run(const QuaternionBase<Derived1>& a, const QuaternionBase<Derived2>& b){
00429 return Quaternion<Scalar>
00430 (
00431 a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
00432 a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
00433 a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
00434 a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x()
00435 );
00436 }
00437 };
00438 }
00439
00441 template <class Derived>
00442 template <class OtherDerived>
00443 EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
00444 QuaternionBase<Derived>::operator* (const QuaternionBase<OtherDerived>& other) const
00445 {
00446 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
00447 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00448 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
00449 typename internal::traits<Derived>::Scalar,
00450 internal::traits<Derived>::IsAligned && internal::traits<OtherDerived>::IsAligned>::run(*this, other);
00451 }
00452
00454 template <class Derived>
00455 template <class OtherDerived>
00456 EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator*= (const QuaternionBase<OtherDerived>& other)
00457 {
00458 derived() = derived() * other.derived();
00459 return derived();
00460 }
00461
00469 template <class Derived>
00470 EIGEN_STRONG_INLINE typename QuaternionBase<Derived>::Vector3
00471 QuaternionBase<Derived>::_transformVector(Vector3 v) const
00472 {
00473
00474
00475
00476
00477
00478 Vector3 uv = this->vec().cross(v);
00479 uv += uv;
00480 return v + this->w() * uv + this->vec().cross(uv);
00481 }
00482
00483 template<class Derived>
00484 EIGEN_STRONG_INLINE QuaternionBase<Derived>& QuaternionBase<Derived>::operator=(const QuaternionBase<Derived>& other)
00485 {
00486 coeffs() = other.coeffs();
00487 return derived();
00488 }
00489
00490 template<class Derived>
00491 template<class OtherDerived>
00492 EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const QuaternionBase<OtherDerived>& other)
00493 {
00494 coeffs() = other.coeffs();
00495 return derived();
00496 }
00497
00500 template<class Derived>
00501 EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const AngleAxisType& aa)
00502 {
00503 Scalar ha = Scalar(0.5)*aa.angle();
00504 this->w() = internal::cos(ha);
00505 this->vec() = internal::sin(ha) * aa.axis();
00506 return derived();
00507 }
00508
00515 template<class Derived>
00516 template<class MatrixDerived>
00517 inline Derived& QuaternionBase<Derived>::operator=(const MatrixBase<MatrixDerived>& xpr)
00518 {
00519 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
00520 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00521 internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived());
00522 return derived();
00523 }
00524
00528 template<class Derived>
00529 inline typename QuaternionBase<Derived>::Matrix3
00530 QuaternionBase<Derived>::toRotationMatrix(void) const
00531 {
00532
00533
00534
00535
00536 Matrix3 res;
00537
00538 const Scalar tx = 2*this->x();
00539 const Scalar ty = 2*this->y();
00540 const Scalar tz = 2*this->z();
00541 const Scalar twx = tx*this->w();
00542 const Scalar twy = ty*this->w();
00543 const Scalar twz = tz*this->w();
00544 const Scalar txx = tx*this->x();
00545 const Scalar txy = ty*this->x();
00546 const Scalar txz = tz*this->x();
00547 const Scalar tyy = ty*this->y();
00548 const Scalar tyz = tz*this->y();
00549 const Scalar tzz = tz*this->z();
00550
00551 res.coeffRef(0,0) = 1-(tyy+tzz);
00552 res.coeffRef(0,1) = txy-twz;
00553 res.coeffRef(0,2) = txz+twy;
00554 res.coeffRef(1,0) = txy+twz;
00555 res.coeffRef(1,1) = 1-(txx+tzz);
00556 res.coeffRef(1,2) = tyz-twx;
00557 res.coeffRef(2,0) = txz-twy;
00558 res.coeffRef(2,1) = tyz+twx;
00559 res.coeffRef(2,2) = 1-(txx+tyy);
00560
00561 return res;
00562 }
00563
00574 template<class Derived>
00575 template<typename Derived1, typename Derived2>
00576 inline Derived& QuaternionBase<Derived>::setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b)
00577 {
00578 using std::max;
00579 Vector3 v0 = a.normalized();
00580 Vector3 v1 = b.normalized();
00581 Scalar c = v1.dot(v0);
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 if (c < Scalar(-1)+NumTraits<Scalar>::dummy_precision())
00592 {
00593 c = max<Scalar>(c,-1);
00594 Matrix<Scalar,2,3> m; m << v0.transpose(), v1.transpose();
00595 JacobiSVD<Matrix<Scalar,2,3> > svd(m, ComputeFullV);
00596 Vector3 axis = svd.matrixV().col(2);
00597
00598 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
00599 this->w() = internal::sqrt(w2);
00600 this->vec() = axis * internal::sqrt(Scalar(1) - w2);
00601 return derived();
00602 }
00603 Vector3 axis = v0.cross(v1);
00604 Scalar s = internal::sqrt((Scalar(1)+c)*Scalar(2));
00605 Scalar invs = Scalar(1)/s;
00606 this->vec() = axis * invs;
00607 this->w() = s * Scalar(0.5);
00608
00609 return derived();
00610 }
00611
00618 template <class Derived>
00619 inline Quaternion<typename internal::traits<Derived>::Scalar> QuaternionBase<Derived>::inverse() const
00620 {
00621
00622 Scalar n2 = this->squaredNorm();
00623 if (n2 > 0)
00624 return Quaternion<Scalar>(conjugate().coeffs() / n2);
00625 else
00626 {
00627
00628 return Quaternion<Scalar>(Coefficients::Zero());
00629 }
00630 }
00631
00638 template <class Derived>
00639 inline Quaternion<typename internal::traits<Derived>::Scalar>
00640 QuaternionBase<Derived>::conjugate() const
00641 {
00642 return Quaternion<Scalar>(this->w(),-this->x(),-this->y(),-this->z());
00643 }
00644
00648 template <class Derived>
00649 template <class OtherDerived>
00650 inline typename internal::traits<Derived>::Scalar
00651 QuaternionBase<Derived>::angularDistance(const QuaternionBase<OtherDerived>& other) const
00652 {
00653 using std::acos;
00654 double d = internal::abs(this->dot(other));
00655 if (d>=1.0)
00656 return Scalar(0);
00657 return static_cast<Scalar>(2 * acos(d));
00658 }
00659
00663 template <class Derived>
00664 template <class OtherDerived>
00665 Quaternion<typename internal::traits<Derived>::Scalar>
00666 QuaternionBase<Derived>::slerp(Scalar t, const QuaternionBase<OtherDerived>& other) const
00667 {
00668 using std::acos;
00669 static const Scalar one = Scalar(1) - NumTraits<Scalar>::epsilon();
00670 Scalar d = this->dot(other);
00671 Scalar absD = internal::abs(d);
00672
00673 Scalar scale0;
00674 Scalar scale1;
00675
00676 if (absD>=one)
00677 {
00678 scale0 = Scalar(1) - t;
00679 scale1 = t;
00680 }
00681 else
00682 {
00683
00684 Scalar theta = acos(absD);
00685 Scalar sinTheta = internal::sin(theta);
00686
00687 scale0 = internal::sin( ( Scalar(1) - t ) * theta) / sinTheta;
00688 scale1 = internal::sin( ( t * theta) ) / sinTheta;
00689 if (d<0)
00690 scale1 = -scale1;
00691 }
00692
00693 return Quaternion<Scalar>(scale0 * coeffs() + scale1 * other.coeffs());
00694 }
00695
00696 namespace internal {
00697
00698
00699 template<typename Other>
00700 struct quaternionbase_assign_impl<Other,3,3>
00701 {
00702 typedef typename Other::Scalar Scalar;
00703 typedef DenseIndex Index;
00704 template<class Derived> inline static void run(QuaternionBase<Derived>& q, const Other& mat)
00705 {
00706
00707
00708 Scalar t = mat.trace();
00709 if (t > Scalar(0))
00710 {
00711 t = sqrt(t + Scalar(1.0));
00712 q.w() = Scalar(0.5)*t;
00713 t = Scalar(0.5)/t;
00714 q.x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
00715 q.y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
00716 q.z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
00717 }
00718 else
00719 {
00720 DenseIndex i = 0;
00721 if (mat.coeff(1,1) > mat.coeff(0,0))
00722 i = 1;
00723 if (mat.coeff(2,2) > mat.coeff(i,i))
00724 i = 2;
00725 DenseIndex j = (i+1)%3;
00726 DenseIndex k = (j+1)%3;
00727
00728 t = sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
00729 q.coeffs().coeffRef(i) = Scalar(0.5) * t;
00730 t = Scalar(0.5)/t;
00731 q.w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
00732 q.coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
00733 q.coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
00734 }
00735 }
00736 };
00737
00738
00739 template<typename Other>
00740 struct quaternionbase_assign_impl<Other,4,1>
00741 {
00742 typedef typename Other::Scalar Scalar;
00743 template<class Derived> inline static void run(QuaternionBase<Derived>& q, const Other& vec)
00744 {
00745 q.coeffs() = vec;
00746 }
00747 };
00748
00749 }
00750
00751 #endif // EIGEN_QUATERNION_H