11 #ifndef EIGEN_QUATERNION_H
12 #define EIGEN_QUATERNION_H
22 template<
typename Other,
23 int OtherRows=Other::RowsAtCompileTime,
24 int OtherCols=Other::ColsAtCompileTime>
25 struct quaternionbase_assign_impl;
34 template<
class Derived>
35 class QuaternionBase :
public RotationBase<Derived, 3>
38 typedef RotationBase<Derived, 3> Base;
40 using Base::operator*;
43 typedef typename internal::traits<Derived>::Scalar Scalar;
44 typedef typename NumTraits<Scalar>::Real RealScalar;
45 typedef typename internal::traits<Derived>::Coefficients Coefficients;
47 Flags = Eigen::internal::traits<Derived>::Flags
61 EIGEN_DEVICE_FUNC
inline Scalar
x()
const {
return this->derived().coeffs().coeff(0); }
63 EIGEN_DEVICE_FUNC
inline Scalar
y()
const {
return this->derived().coeffs().coeff(1); }
65 EIGEN_DEVICE_FUNC
inline Scalar
z()
const {
return this->derived().coeffs().coeff(2); }
67 EIGEN_DEVICE_FUNC
inline Scalar
w()
const {
return this->derived().coeffs().coeff(3); }
70 EIGEN_DEVICE_FUNC
inline Scalar&
x() {
return this->derived().coeffs().coeffRef(0); }
72 EIGEN_DEVICE_FUNC
inline Scalar&
y() {
return this->derived().coeffs().coeffRef(1); }
74 EIGEN_DEVICE_FUNC
inline Scalar&
z() {
return this->derived().coeffs().coeffRef(2); }
76 EIGEN_DEVICE_FUNC
inline Scalar&
w() {
return this->derived().coeffs().coeffRef(3); }
85 EIGEN_DEVICE_FUNC
inline const typename internal::traits<Derived>::Coefficients&
coeffs()
const {
return derived().coeffs(); }
88 EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Coefficients&
coeffs() {
return derived().coeffs(); }
100 EIGEN_DEVICE_FUNC Derived& operator=(
const AngleAxisType& aa);
120 EIGEN_DEVICE_FUNC
inline Scalar
norm()
const {
return coeffs().norm(); }
142 template<
typename Derived1,
typename Derived2>
160 template<
class OtherDerived>
162 {
return coeffs().isApprox(other.coeffs(), prec); }
172 template<
typename NewScalarType>
173 EIGEN_DEVICE_FUNC
inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type
cast()
const
175 return typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type(derived());
178 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
179 # include EIGEN_QUATERNIONBASE_PLUGIN
213 template<
typename _Scalar,
int _Options>
214 struct traits<Quaternion<_Scalar,_Options> >
216 typedef Quaternion<_Scalar,_Options> PlainObject;
217 typedef _Scalar Scalar;
218 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
220 Alignment = internal::traits<Coefficients>::Alignment,
226 template<
typename _Scalar,
int _Options>
227 class Quaternion :
public QuaternionBase<Quaternion<_Scalar,_Options> >
230 typedef QuaternionBase<Quaternion<_Scalar,_Options> > Base;
231 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment>0 };
233 typedef _Scalar Scalar;
235 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
236 using Base::operator*=;
238 typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
251 EIGEN_DEVICE_FUNC
inline Quaternion(
const Scalar& w,
const Scalar& x,
const Scalar& y,
const Scalar& z) : m_coeffs(x, y, z, w){}
254 EIGEN_DEVICE_FUNC
explicit inline Quaternion(
const Scalar* data) : m_coeffs(data) {}
266 template<
typename Derived>
270 template<
typename OtherScalar,
int OtherOptions>
272 { m_coeffs = other.coeffs().template cast<Scalar>(); }
276 template<
typename Derived1,
typename Derived2>
279 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs;}
280 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
282 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(
bool(NeedsAlignment))
284 #ifdef EIGEN_QUATERNION_PLUGIN
285 # include EIGEN_QUATERNION_PLUGIN
289 Coefficients m_coeffs;
291 #ifndef EIGEN_PARSED_BY_DOXYGEN
292 static EIGEN_STRONG_INLINE
void _check_template_params()
294 EIGEN_STATIC_ASSERT( (_Options &
DontAlign) == _Options,
295 INVALID_MATRIX_TEMPLATE_PARAMETERS)
312 template<
typename _Scalar,
int _Options>
313 struct traits<
Map<
Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
320 template<
typename _Scalar,
int _Options>
321 struct traits<Map<const Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
323 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
342 template<
typename _Scalar,
int _Options>
344 :
public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
349 typedef _Scalar Scalar;
350 typedef typename internal::traits<Map>::Coefficients Coefficients;
351 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
352 using Base::operator*=;
360 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(
const Scalar* coeffs) : m_coeffs(coeffs) {}
362 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
365 const Coefficients m_coeffs;
379 template<
typename _Scalar,
int _Options>
386 typedef _Scalar Scalar;
387 typedef typename internal::traits<Map>::Coefficients Coefficients;
388 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
389 using Base::operator*=;
397 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(Scalar* coeffs) : m_coeffs(coeffs) {}
399 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs; }
400 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
403 Coefficients m_coeffs;
426 template<
int Arch,
class Derived1,
class Derived2,
typename Scalar,
int _Options>
struct quat_product
431 a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
432 a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
433 a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
434 a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x()
441 template <
class Derived>
442 template <
class OtherDerived>
443 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
446 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
447 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
448 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
449 typename internal::traits<Derived>::Scalar,
450 EIGEN_PLAIN_ENUM_MIN(internal::traits<Derived>::Alignment, internal::traits<OtherDerived>::Alignment)>::run(*
this, other);
454 template <
class Derived>
455 template <
class OtherDerived>
458 derived() = derived() * other.derived();
469 template <
class Derived>
478 Vector3 uv = this->vec().cross(v);
480 return v + this->w() * uv + this->vec().cross(uv);
483 template<
class Derived>
486 coeffs() = other.coeffs();
490 template<
class Derived>
491 template<
class OtherDerived>
492 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const QuaternionBase<OtherDerived>& other)
494 coeffs() = other.coeffs();
500 template<
class Derived>
503 EIGEN_USING_STD_MATH(
cos)
504 EIGEN_USING_STD_MATH(
sin)
505 Scalar ha = Scalar(0.5)*aa.
angle();
507 this->vec() =
sin(ha) * aa.
axis();
517 template<
class Derived>
518 template<
class MatrixDerived>
521 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
522 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
523 internal::quaternionbase_assign_impl<MatrixDerived>::run(*
this, xpr.
derived());
530 template<
class Derived>
540 const Scalar tx = Scalar(2)*this->x();
541 const Scalar ty = Scalar(2)*this->y();
542 const Scalar tz = Scalar(2)*this->z();
543 const Scalar twx = tx*this->w();
544 const Scalar twy = ty*this->w();
545 const Scalar twz = tz*this->w();
546 const Scalar txx = tx*this->x();
547 const Scalar txy = ty*this->x();
548 const Scalar txz = tz*this->x();
549 const Scalar tyy = ty*this->y();
550 const Scalar tyz = tz*this->y();
551 const Scalar tzz = tz*this->z();
553 res.
coeffRef(0,0) = Scalar(1)-(tyy+tzz);
557 res.
coeffRef(1,1) = Scalar(1)-(txx+tzz);
561 res.
coeffRef(2,2) = Scalar(1)-(txx+tyy);
576 template<
class Derived>
577 template<
typename Derived1,
typename Derived2>
580 EIGEN_USING_STD_MATH(
sqrt)
583 Scalar c = v1.dot(v0);
595 c = numext::maxi(c,Scalar(-1));
598 Vector3 axis = svd.matrixV().col(2);
600 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
601 this->w() =
sqrt(w2);
602 this->vec() = axis *
sqrt(Scalar(1) - w2);
606 Scalar s =
sqrt((Scalar(1)+c)*Scalar(2));
607 Scalar invs = Scalar(1)/s;
608 this->vec() = axis * invs;
609 this->w() = s * Scalar(0.5);
618 template<
typename Scalar,
int Options>
621 EIGEN_USING_STD_MATH(
sqrt)
622 EIGEN_USING_STD_MATH(
sin)
623 EIGEN_USING_STD_MATH(
cos)
624 const Scalar u1 = internal::random<Scalar>(0, 1),
625 u2 = internal::random<Scalar>(0, 2*EIGEN_PI),
626 u3 = internal::random<Scalar>(0, 2*EIGEN_PI);
627 const Scalar a =
sqrt(1 - u1),
643 template<
typename Scalar,
int Options>
644 template<
typename Derived1,
typename Derived2>
648 quat.setFromTwoVectors(a, b);
659 template <
class Derived>
663 Scalar n2 = this->squaredNorm();
675 template<
int Arch,
class Derived,
typename Scalar,
int _Options>
struct quat_conj
689 template <
class Derived>
690 EIGEN_DEVICE_FUNC
inline Quaternion<typename internal::traits<Derived>::Scalar>
693 return internal::quat_conj<Architecture::Target, Derived,
694 typename internal::traits<Derived>::Scalar,
695 internal::traits<Derived>::Alignment>::run(*
this);
702 template <
class Derived>
703 template <
class OtherDerived>
704 EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Scalar
707 EIGEN_USING_STD_MATH(atan2)
709 return Scalar(2) * atan2( d.vec().norm(), numext::abs(d.w()) );
720 template <
class Derived>
721 template <
class OtherDerived>
725 EIGEN_USING_STD_MATH(
acos)
726 EIGEN_USING_STD_MATH(
sin)
728 Scalar d = this->dot(other);
729 Scalar absD = numext::abs(d);
736 scale0 = Scalar(1) - t;
742 Scalar theta =
acos(absD);
743 Scalar sinTheta =
sin(theta);
745 scale0 =
sin( ( Scalar(1) - t ) * theta) / sinTheta;
746 scale1 =
sin( ( t * theta) ) / sinTheta;
748 if(d<Scalar(0)) scale1 = -scale1;
756 template<
typename Other>
757 struct quaternionbase_assign_impl<Other,3,3>
759 typedef typename Other::Scalar Scalar;
760 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(
QuaternionBase<Derived>& q,
const Other& a_mat)
762 const typename internal::nested_eval<Other,2>::type mat(a_mat);
763 EIGEN_USING_STD_MATH(
sqrt)
766 Scalar t = mat.trace();
769 t =
sqrt(t + Scalar(1.0));
770 q.w() = Scalar(0.5)*t;
772 q.x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
773 q.y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
774 q.z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
779 if (mat.coeff(1,1) > mat.coeff(0,0))
781 if (mat.coeff(2,2) > mat.coeff(i,i))
786 t =
sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
787 q.coeffs().coeffRef(i) = Scalar(0.5) * t;
789 q.w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
790 q.coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
791 q.coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
797 template<
typename Other>
798 struct quaternionbase_assign_impl<Other,4,1>
800 typedef typename Other::Scalar Scalar;
801 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(QuaternionBase<Derived>& q,
const Other& vec)
811 #endif // EIGEN_QUATERNION_H