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>
40 using Base::operator*;
43 typedef typename internal::traits<Derived>::Scalar Scalar;
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>
172 #ifdef EIGEN_PARSED_BY_DOXYGEN
178 template<
typename NewScalarType>
179 EIGEN_DEVICE_FUNC
inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type
cast()
const;
183 template<
typename NewScalarType>
184 EIGEN_DEVICE_FUNC
inline
185 typename internal::enable_if<internal::is_same<Scalar,NewScalarType>::value,
const Derived&>::type
cast()
const
190 template<
typename NewScalarType>
191 EIGEN_DEVICE_FUNC
inline
192 typename internal::enable_if<!internal::is_same<Scalar,NewScalarType>::value,Quaternion<NewScalarType> >::type
cast()
const
194 return Quaternion<NewScalarType>(
coeffs().
template cast<NewScalarType>());
198 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
199 # include EIGEN_QUATERNIONBASE_PLUGIN
202 EIGEN_DEFAULT_COPY_CONSTRUCTOR(QuaternionBase)
203 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(QuaternionBase)
236 template<
typename _Scalar,
int _Options>
237 struct traits<Quaternion<_Scalar,_Options> >
239 typedef Quaternion<_Scalar,_Options> PlainObject;
240 typedef _Scalar Scalar;
241 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
243 Alignment = internal::traits<Coefficients>::Alignment,
249 template<
typename _Scalar,
int _Options>
254 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment>0 };
256 typedef _Scalar Scalar;
258 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
259 using Base::operator*=;
261 typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
274 EIGEN_DEVICE_FUNC
inline Quaternion(
const Scalar&
w,
const Scalar&
x,
const Scalar&
y,
const Scalar&
z) : m_coeffs(
x,
y,
z,
w){}
277 EIGEN_DEVICE_FUNC
explicit inline Quaternion(
const Scalar* data) : m_coeffs(data) {}
289 template<
typename Derived>
293 template<
typename OtherScalar,
int OtherOptions>
295 { m_coeffs = other.coeffs().template cast<Scalar>(); }
299 template<
typename Derived1,
typename Derived2>
302 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs;}
303 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
305 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(
bool(NeedsAlignment))
307 #ifdef EIGEN_QUATERNION_PLUGIN
308 # include EIGEN_QUATERNION_PLUGIN
312 Coefficients m_coeffs;
314 #ifndef EIGEN_PARSED_BY_DOXYGEN
315 static EIGEN_STRONG_INLINE
void _check_template_params()
317 EIGEN_STATIC_ASSERT( (_Options &
DontAlign) == _Options,
318 INVALID_MATRIX_TEMPLATE_PARAMETERS)
335 template<
typename _Scalar,
int _Options>
336 struct traits<
Map<
Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
343 template<
typename _Scalar,
int _Options>
344 struct traits<Map<const Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
346 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
365 template<
typename _Scalar,
int _Options>
367 :
public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
372 typedef _Scalar Scalar;
373 typedef typename internal::traits<Map>::Coefficients Coefficients;
374 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
375 using Base::operator*=;
383 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(
const Scalar* coeffs) : m_coeffs(coeffs) {}
385 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
388 const Coefficients m_coeffs;
402 template<
typename _Scalar,
int _Options>
409 typedef _Scalar Scalar;
410 typedef typename internal::traits<Map>::Coefficients Coefficients;
411 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
412 using Base::operator*=;
420 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(Scalar* coeffs) : m_coeffs(coeffs) {}
422 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs; }
423 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
426 Coefficients m_coeffs;
449 template<
int Arch,
class Derived1,
class Derived2,
typename Scalar>
struct quat_product
454 a.
w() * b.
w() - a.
x() * b.
x() - a.
y() * b.
y() - a.
z() * b.
z(),
455 a.
w() * b.
x() + a.
x() * b.
w() + a.
y() * b.
z() - a.
z() * b.
y(),
456 a.
w() * b.
y() + a.
y() * b.
w() + a.
z() * b.
x() - a.
x() * b.
z(),
457 a.
w() * b.
z() + a.
z() * b.
w() + a.
x() * b.
y() - a.
y() * b.
x()
464 template <
class Derived>
465 template <
class OtherDerived>
466 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
469 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
470 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
471 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
472 typename internal::traits<Derived>::Scalar>::run(*
this, other);
476 template <
class Derived>
477 template <
class OtherDerived>
480 derived() = derived() * other.derived();
491 template <
class Derived>
500 Vector3 uv = this->vec().cross(v);
502 return v + this->w() * uv + this->vec().cross(uv);
505 template<
class Derived>
508 coeffs() = other.
coeffs();
512 template<
class Derived>
513 template<
class OtherDerived>
514 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const QuaternionBase<OtherDerived>& other)
516 coeffs() = other.coeffs();
522 template<
class Derived>
525 EIGEN_USING_STD_MATH(
cos)
526 EIGEN_USING_STD_MATH(
sin)
527 Scalar ha = Scalar(0.5)*aa.
angle();
529 this->vec() =
sin(ha) * aa.
axis();
539 template<
class Derived>
540 template<
class MatrixDerived>
543 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
544 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
545 internal::quaternionbase_assign_impl<MatrixDerived>::run(*
this, xpr.
derived());
552 template<
class Derived>
562 const Scalar tx = Scalar(2)*this->x();
563 const Scalar ty = Scalar(2)*this->y();
564 const Scalar tz = Scalar(2)*this->z();
565 const Scalar twx = tx*this->w();
566 const Scalar twy = ty*this->w();
567 const Scalar twz = tz*this->w();
568 const Scalar txx = tx*this->x();
569 const Scalar txy = ty*this->x();
570 const Scalar txz = tz*this->x();
571 const Scalar tyy = ty*this->y();
572 const Scalar tyz = tz*this->y();
573 const Scalar tzz = tz*this->z();
575 res.
coeffRef(0,0) = Scalar(1)-(tyy+tzz);
579 res.
coeffRef(1,1) = Scalar(1)-(txx+tzz);
583 res.
coeffRef(2,2) = Scalar(1)-(txx+tyy);
598 template<
class Derived>
599 template<
typename Derived1,
typename Derived2>
602 EIGEN_USING_STD_MATH(
sqrt)
605 Scalar c = v1.dot(v0);
617 c = numext::maxi(c,Scalar(-1));
622 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
623 this->w() =
sqrt(w2);
624 this->vec() = axis *
sqrt(Scalar(1) - w2);
628 Scalar s =
sqrt((Scalar(1)+c)*Scalar(2));
629 Scalar invs = Scalar(1)/s;
630 this->vec() = axis * invs;
631 this->w() = s * Scalar(0.5);
640 template<
typename Scalar,
int Options>
643 EIGEN_USING_STD_MATH(
sqrt)
644 EIGEN_USING_STD_MATH(
sin)
645 EIGEN_USING_STD_MATH(
cos)
646 const Scalar u1 = internal::random<Scalar>(0, 1),
647 u2 = internal::random<Scalar>(0, 2*EIGEN_PI),
648 u3 = internal::random<Scalar>(0, 2*EIGEN_PI);
649 const Scalar a =
sqrt(1 - u1),
665 template<
typename Scalar,
int Options>
666 template<
typename Derived1,
typename Derived2>
681 template <
class Derived>
685 Scalar n2 = this->squaredNorm();
697 template<
int Arch,
class Derived,
typename Scalar>
struct quat_conj
711 template <
class Derived>
712 EIGEN_DEVICE_FUNC
inline Quaternion<typename internal::traits<Derived>::Scalar>
715 return internal::quat_conj<Architecture::Target, Derived,
716 typename internal::traits<Derived>::Scalar>::run(*
this);
723 template <
class Derived>
724 template <
class OtherDerived>
725 EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Scalar
728 EIGEN_USING_STD_MATH(atan2)
730 return Scalar(2) * atan2( d.
vec().norm(), numext::abs(d.
w()) );
741 template <
class Derived>
742 template <
class OtherDerived>
746 EIGEN_USING_STD_MATH(
acos)
747 EIGEN_USING_STD_MATH(
sin)
749 Scalar d = this->dot(other);
750 Scalar absD = numext::abs(d);
757 scale0 = Scalar(1) - t;
763 Scalar theta =
acos(absD);
764 Scalar sinTheta =
sin(theta);
766 scale0 =
sin( ( Scalar(1) - t ) * theta) / sinTheta;
767 scale1 =
sin( ( t * theta) ) / sinTheta;
769 if(d<Scalar(0)) scale1 = -scale1;
777 template<
typename Other>
778 struct quaternionbase_assign_impl<Other,3,3>
780 typedef typename Other::Scalar Scalar;
781 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(
QuaternionBase<Derived>& q,
const Other& a_mat)
783 const typename internal::nested_eval<Other,2>::type mat(a_mat);
784 EIGEN_USING_STD_MATH(
sqrt)
787 Scalar t = mat.trace();
790 t =
sqrt(t + Scalar(1.0));
791 q.
w() = Scalar(0.5)*t;
793 q.
x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
794 q.
y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
795 q.
z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
800 if (mat.coeff(1,1) > mat.coeff(0,0))
802 if (mat.coeff(2,2) > mat.coeff(i,i))
807 t =
sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
808 q.
coeffs().coeffRef(i) = Scalar(0.5) * t;
810 q.
w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
811 q.
coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
812 q.
coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
818 template<
typename Other>
819 struct quaternionbase_assign_impl<Other,4,1>
821 typedef typename Other::Scalar Scalar;
822 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(QuaternionBase<Derived>& q,
const Other& vec)
832 #endif // EIGEN_QUATERNION_H