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;
46 typedef typename Coefficients::CoeffReturnType CoeffReturnType;
47 typedef typename internal::conditional<bool(internal::traits<Derived>::Flags&
LvalueBit),
48 Scalar&, CoeffReturnType>::type NonConstCoeffReturnType;
52 Flags = Eigen::internal::traits<Derived>::Flags
66 EIGEN_DEVICE_FUNC
inline CoeffReturnType
x()
const {
return this->derived().coeffs().coeff(0); }
68 EIGEN_DEVICE_FUNC
inline CoeffReturnType
y()
const {
return this->derived().coeffs().coeff(1); }
70 EIGEN_DEVICE_FUNC
inline CoeffReturnType
z()
const {
return this->derived().coeffs().coeff(2); }
72 EIGEN_DEVICE_FUNC
inline CoeffReturnType
w()
const {
return this->derived().coeffs().coeff(3); }
75 EIGEN_DEVICE_FUNC
inline NonConstCoeffReturnType
x() {
return this->derived().coeffs().x(); }
77 EIGEN_DEVICE_FUNC
inline NonConstCoeffReturnType
y() {
return this->derived().coeffs().y(); }
79 EIGEN_DEVICE_FUNC
inline NonConstCoeffReturnType
z() {
return this->derived().coeffs().z(); }
81 EIGEN_DEVICE_FUNC
inline NonConstCoeffReturnType
w() {
return this->derived().coeffs().w(); }
90 EIGEN_DEVICE_FUNC
inline const typename internal::traits<Derived>::Coefficients&
coeffs()
const {
return derived().coeffs(); }
93 EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Coefficients&
coeffs() {
return derived().coeffs(); }
105 EIGEN_DEVICE_FUNC Derived& operator=(
const AngleAxisType& aa);
125 EIGEN_DEVICE_FUNC
inline Scalar
norm()
const {
return coeffs().norm(); }
147 template<
typename Derived1,
typename Derived2>
165 template<
class OtherDerived>
167 {
return coeffs().isApprox(other.coeffs(), prec); }
177 template<
typename NewScalarType>
178 EIGEN_DEVICE_FUNC
inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type
cast()
const
180 return typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type(derived());
183 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
184 # include EIGEN_QUATERNIONBASE_PLUGIN
221 template<
typename _Scalar,
int _Options>
222 struct traits<Quaternion<_Scalar,_Options> >
224 typedef Quaternion<_Scalar,_Options> PlainObject;
225 typedef _Scalar Scalar;
226 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
228 Alignment = internal::traits<Coefficients>::Alignment,
234 template<
typename _Scalar,
int _Options>
235 class Quaternion :
public QuaternionBase<Quaternion<_Scalar,_Options> >
238 typedef QuaternionBase<Quaternion<_Scalar,_Options> > Base;
239 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment>0 };
241 typedef _Scalar Scalar;
243 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
244 using Base::operator*=;
246 typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
259 EIGEN_DEVICE_FUNC
inline Quaternion(
const Scalar& w,
const Scalar& x,
const Scalar& y,
const Scalar& z) : m_coeffs(x, y, z, w){}
262 EIGEN_DEVICE_FUNC
explicit inline Quaternion(
const Scalar* data) : m_coeffs(data) {}
274 template<
typename Derived>
278 template<
typename OtherScalar,
int OtherOptions>
280 { m_coeffs = other.coeffs().template cast<Scalar>(); }
284 template<
typename Derived1,
typename Derived2>
287 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs;}
288 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
290 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(
bool(NeedsAlignment))
292 #ifdef EIGEN_QUATERNION_PLUGIN
293 # include EIGEN_QUATERNION_PLUGIN
297 Coefficients m_coeffs;
299 #ifndef EIGEN_PARSED_BY_DOXYGEN
300 static EIGEN_STRONG_INLINE
void _check_template_params()
302 EIGEN_STATIC_ASSERT( (_Options &
DontAlign) == _Options,
303 INVALID_MATRIX_TEMPLATE_PARAMETERS)
320 template<
typename _Scalar,
int _Options>
321 struct traits<
Map<
Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
328 template<
typename _Scalar,
int _Options>
329 struct traits<Map<const Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
331 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
350 template<
typename _Scalar,
int _Options>
352 :
public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
357 typedef _Scalar Scalar;
358 typedef typename internal::traits<Map>::Coefficients Coefficients;
359 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
360 using Base::operator*=;
368 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(
const Scalar* coeffs) : m_coeffs(coeffs) {}
370 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
373 const Coefficients m_coeffs;
387 template<
typename _Scalar,
int _Options>
394 typedef _Scalar Scalar;
395 typedef typename internal::traits<Map>::Coefficients Coefficients;
396 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
397 using Base::operator*=;
405 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(Scalar* coeffs) : m_coeffs(coeffs) {}
407 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs; }
408 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
411 Coefficients m_coeffs;
434 template<
int Arch,
class Derived1,
class Derived2,
typename Scalar>
struct quat_product
439 a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
440 a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
441 a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
442 a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x()
449 template <
class Derived>
450 template <
class OtherDerived>
451 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
454 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
455 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
456 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
457 typename internal::traits<Derived>::Scalar>::run(*
this, other);
461 template <
class Derived>
462 template <
class OtherDerived>
465 derived() = derived() * other.derived();
476 template <
class Derived>
485 Vector3 uv = this->vec().cross(v);
487 return v + this->w() * uv + this->vec().cross(uv);
490 template<
class Derived>
493 coeffs() = other.coeffs();
497 template<
class Derived>
498 template<
class OtherDerived>
499 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const QuaternionBase<OtherDerived>& other)
501 coeffs() = other.coeffs();
507 template<
class Derived>
510 EIGEN_USING_STD_MATH(
cos)
511 EIGEN_USING_STD_MATH(
sin)
512 Scalar ha = Scalar(0.5)*aa.
angle();
514 this->vec() =
sin(ha) * aa.
axis();
524 template<
class Derived>
525 template<
class MatrixDerived>
528 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
529 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
530 internal::quaternionbase_assign_impl<MatrixDerived>::run(*
this, xpr.
derived());
537 template<
class Derived>
547 const Scalar tx = Scalar(2)*this->x();
548 const Scalar ty = Scalar(2)*this->y();
549 const Scalar tz = Scalar(2)*this->z();
550 const Scalar twx = tx*this->w();
551 const Scalar twy = ty*this->w();
552 const Scalar twz = tz*this->w();
553 const Scalar txx = tx*this->x();
554 const Scalar txy = ty*this->x();
555 const Scalar txz = tz*this->x();
556 const Scalar tyy = ty*this->y();
557 const Scalar tyz = tz*this->y();
558 const Scalar tzz = tz*this->z();
560 res.
coeffRef(0,0) = Scalar(1)-(tyy+tzz);
564 res.
coeffRef(1,1) = Scalar(1)-(txx+tzz);
568 res.
coeffRef(2,2) = Scalar(1)-(txx+tyy);
583 template<
class Derived>
584 template<
typename Derived1,
typename Derived2>
587 EIGEN_USING_STD_MATH(
sqrt)
590 Scalar c = v1.dot(v0);
602 c = numext::maxi(c,Scalar(-1));
605 Vector3 axis = svd.matrixV().col(2);
607 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
608 this->w() =
sqrt(w2);
609 this->vec() = axis *
sqrt(Scalar(1) - w2);
613 Scalar s =
sqrt((Scalar(1)+c)*Scalar(2));
614 Scalar invs = Scalar(1)/s;
615 this->vec() = axis * invs;
616 this->w() = s * Scalar(0.5);
625 template<
typename Scalar,
int Options>
628 EIGEN_USING_STD_MATH(
sqrt)
629 EIGEN_USING_STD_MATH(
sin)
630 EIGEN_USING_STD_MATH(
cos)
631 const Scalar u1 = internal::random<Scalar>(0, 1),
632 u2 = internal::random<Scalar>(0, 2*EIGEN_PI),
633 u3 = internal::random<Scalar>(0, 2*EIGEN_PI);
634 const Scalar a =
sqrt(1 - u1),
650 template<
typename Scalar,
int Options>
651 template<
typename Derived1,
typename Derived2>
655 quat.setFromTwoVectors(a, b);
666 template <
class Derived>
670 Scalar n2 = this->squaredNorm();
682 template<
int Arch,
class Derived,
typename Scalar>
struct quat_conj
696 template <
class Derived>
697 EIGEN_DEVICE_FUNC
inline Quaternion<typename internal::traits<Derived>::Scalar>
700 return internal::quat_conj<Architecture::Target, Derived,
701 typename internal::traits<Derived>::Scalar>::run(*
this);
708 template <
class Derived>
709 template <
class OtherDerived>
710 EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Scalar
713 EIGEN_USING_STD_MATH(atan2)
715 return Scalar(2) * atan2( d.vec().norm(), numext::abs(d.w()) );
726 template <
class Derived>
727 template <
class OtherDerived>
731 EIGEN_USING_STD_MATH(
acos)
732 EIGEN_USING_STD_MATH(
sin)
734 Scalar d = this->dot(other);
735 Scalar absD = numext::abs(d);
742 scale0 = Scalar(1) - t;
748 Scalar theta =
acos(absD);
749 Scalar sinTheta =
sin(theta);
751 scale0 =
sin( ( Scalar(1) - t ) * theta) / sinTheta;
752 scale1 =
sin( ( t * theta) ) / sinTheta;
754 if(d<Scalar(0)) scale1 = -scale1;
762 template<
typename Other>
763 struct quaternionbase_assign_impl<Other,3,3>
765 typedef typename Other::Scalar Scalar;
766 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(
QuaternionBase<Derived>& q,
const Other& a_mat)
768 const typename internal::nested_eval<Other,2>::type mat(a_mat);
769 EIGEN_USING_STD_MATH(
sqrt)
772 Scalar t = mat.trace();
775 t =
sqrt(t + Scalar(1.0));
776 q.w() = Scalar(0.5)*t;
778 q.x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
779 q.y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
780 q.z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
785 if (mat.coeff(1,1) > mat.coeff(0,0))
787 if (mat.coeff(2,2) > mat.coeff(i,i))
792 t =
sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
793 q.coeffs().coeffRef(i) = Scalar(0.5) * t;
795 q.w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
796 q.coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
797 q.coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
803 template<
typename Other>
804 struct quaternionbase_assign_impl<Other,4,1>
806 typedef typename Other::Scalar Scalar;
807 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(QuaternionBase<Derived>& q,
const Other& vec)
817 #endif // EIGEN_QUATERNION_H