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*;
46 typedef typename Coefficients::CoeffReturnType CoeffReturnType;
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); }
125 EIGEN_DEVICE_FUNC
inline Scalar
norm()
const {
return coeffs().norm(); }
147 template<
typename Derived1,
typename Derived2>
165 template<
class OtherDerived>
173 template<
class OtherDerived>
181 template<
class OtherDerived>
188 #ifdef EIGEN_PARSED_BY_DOXYGEN
194 template<
typename NewScalarType>
199 template<
typename NewScalarType>
200 EIGEN_DEVICE_FUNC
inline
206 template<
typename NewScalarType>
207 EIGEN_DEVICE_FUNC
inline
215 friend std::ostream& operator<<(std::ostream& s,
const QuaternionBase<Derived>& q) {
216 s << q.x() <<
"i + " << q.y() <<
"j + " << q.z() <<
"k" <<
" + " << q.w();
221 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
222 # include EIGEN_QUATERNIONBASE_PLUGIN
225 EIGEN_DEFAULT_COPY_CONSTRUCTOR(QuaternionBase)
226 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(QuaternionBase)
259 template<
typename _Scalar,
int _Options>
263 typedef _Scalar Scalar;
272 template<
typename _Scalar,
int _Options>
279 typedef _Scalar Scalar;
281 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
282 using Base::operator*=;
297 EIGEN_DEVICE_FUNC
inline Quaternion(
const Scalar& w,
const Scalar& x,
const Scalar& y,
const Scalar& z) : m_coeffs(x, y, z, w){}
300 EIGEN_DEVICE_FUNC
explicit inline Quaternion(
const Scalar* data) : m_coeffs(data) {}
312 template<
typename Derived>
316 template<
typename OtherScalar,
int OtherOptions>
318 { m_coeffs = other.coeffs().template cast<Scalar>(); }
320 #if EIGEN_HAS_RVALUE_REFERENCES
323 EIGEN_DEVICE_FUNC
inline Quaternion(
Quaternion&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
324 : m_coeffs(std::move(other.coeffs()))
328 EIGEN_DEVICE_FUNC
Quaternion& operator=(
Quaternion&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value)
330 m_coeffs = std::move(other.
coeffs());
337 template<
typename Derived1,
typename Derived2>
338 EIGEN_DEVICE_FUNC
static Quaternion FromTwoVectors(
const MatrixBase<Derived1>& a,
const MatrixBase<Derived2>& b);
340 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs;}
341 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
343 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(
bool(NeedsAlignment))
345 #ifdef EIGEN_QUATERNION_PLUGIN
346 # include EIGEN_QUATERNION_PLUGIN
350 Coefficients m_coeffs;
352 #ifndef EIGEN_PARSED_BY_DOXYGEN
353 static EIGEN_STRONG_INLINE
void _check_template_params()
355 EIGEN_STATIC_ASSERT( (_Options &
DontAlign) == _Options,
356 INVALID_MATRIX_TEMPLATE_PARAMETERS)
373 template<
typename _Scalar,
int _Options>
374 struct traits<
Map<
Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
381 template<
typename _Scalar,
int _Options>
382 struct traits<
Map<const
Quaternion<_Scalar>, _Options> > :
traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
403 template<
typename _Scalar,
int _Options>
405 :
public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
410 typedef _Scalar Scalar;
412 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
413 using Base::operator*=;
421 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(
const Scalar* coeffs) : m_coeffs(coeffs) {}
423 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs;}
426 const Coefficients m_coeffs;
440 template<
typename _Scalar,
int _Options>
447 typedef _Scalar Scalar;
449 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
450 using Base::operator*=;
458 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(Scalar* coeffs) : m_coeffs(coeffs) {}
460 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs; }
461 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
464 Coefficients m_coeffs;
487 template<
int Arch,
class Derived1,
class Derived2,
typename Scalar>
struct quat_product
492 a.
w() * b.
w() - a.
x() * b.
x() - a.
y() * b.
y() - a.
z() * b.
z(),
493 a.
w() * b.
x() + a.
x() * b.
w() + a.
y() * b.
z() - a.
z() * b.
y(),
494 a.
w() * b.
y() + a.
y() * b.
w() + a.
z() * b.
x() - a.
x() * b.
z(),
495 a.
w() * b.
z() + a.
z() * b.
w() + a.
x() * b.
y() - a.
y() * b.
x()
502 template <
class Derived>
503 template <
class OtherDerived>
504 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
508 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
514 template <
class Derived>
515 template <
class OtherDerived>
518 derived() = derived() * other.derived();
529 template <
class Derived>
538 Vector3 uv = this->vec().cross(v);
540 return v + this->w() * uv + this->vec().cross(uv);
543 template<
class Derived>
546 coeffs() = other.
coeffs();
550 template<
class Derived>
551 template<
class OtherDerived>
552 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const QuaternionBase<OtherDerived>& other)
554 coeffs() = other.coeffs();
560 template<
class Derived>
565 Scalar ha = Scalar(0.5)*aa.
angle();
567 this->vec() = sin(ha) * aa.
axis();
577 template<
class Derived>
578 template<
class MatrixDerived>
582 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
590 template<
class Derived>
600 const Scalar tx = Scalar(2)*this->x();
601 const Scalar ty = Scalar(2)*this->y();
602 const Scalar tz = Scalar(2)*this->z();
603 const Scalar twx = tx*this->w();
604 const Scalar twy = ty*this->w();
605 const Scalar twz = tz*this->w();
606 const Scalar txx = tx*this->x();
607 const Scalar txy = ty*this->x();
608 const Scalar txz = tz*this->x();
609 const Scalar tyy = ty*this->y();
610 const Scalar tyz = tz*this->y();
611 const Scalar tzz = tz*this->z();
613 res.
coeffRef(0,0) = Scalar(1)-(tyy+tzz);
617 res.
coeffRef(1,1) = Scalar(1)-(txx+tzz);
621 res.
coeffRef(2,2) = Scalar(1)-(txx+tyy);
636 template<
class Derived>
637 template<
typename Derived1,
typename Derived2>
640 EIGEN_USING_STD(sqrt)
643 Scalar c = v1.dot(v0);
655 c = numext::maxi(c,Scalar(-1));
658 Vector3 axis = svd.matrixV().col(2);
660 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
661 this->w() = sqrt(w2);
662 this->vec() = axis * sqrt(Scalar(1) - w2);
666 Scalar s = sqrt((Scalar(1)+c)*Scalar(2));
667 Scalar invs = Scalar(1)/s;
668 this->vec() = axis * invs;
669 this->w() = s * Scalar(0.5);
678 template<
typename Scalar,
int Options>
681 EIGEN_USING_STD(sqrt)
684 const Scalar u1 = internal::random<Scalar>(0, 1),
685 u2 = internal::random<Scalar>(0, 2*EIGEN_PI),
686 u3 = internal::random<Scalar>(0, 2*EIGEN_PI);
687 const Scalar a = sqrt(Scalar(1) - u1),
689 return Quaternion (a * sin(u2), a * cos(u2), b * sin(u3), b * cos(u3));
703 template<
typename Scalar,
int Options>
704 template<
typename Derived1,
typename Derived2>
708 quat.setFromTwoVectors(a, b);
719 template <
class Derived>
723 Scalar n2 = this->squaredNorm();
735 template<
int Arch,
class Derived,
typename Scalar>
struct quat_conj
749 template <
class Derived>
761 template <
class Derived>
762 template <
class OtherDerived>
766 EIGEN_USING_STD(atan2)
768 return Scalar(2) * atan2( d.vec().norm(), numext::abs(d.w()) );
779 template <
class Derived>
780 template <
class OtherDerived>
784 EIGEN_USING_STD(acos)
787 Scalar d = this->dot(other);
788 Scalar absD = numext::abs(d);
795 scale0 = Scalar(1) - t;
801 Scalar theta = acos(absD);
802 Scalar sinTheta = sin(theta);
804 scale0 = sin( ( Scalar(1) - t ) * theta) / sinTheta;
805 scale1 = sin( ( t * theta) ) / sinTheta;
807 if(d<Scalar(0)) scale1 = -scale1;
815 template<
typename Other>
818 typedef typename Other::Scalar Scalar;
819 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(
QuaternionBase<Derived>& q,
const Other& a_mat)
822 EIGEN_USING_STD(sqrt)
825 Scalar t = mat.trace();
828 t = sqrt(t + Scalar(1.0));
829 q.
w() = Scalar(0.5)*t;
831 q.
x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
832 q.
y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
833 q.
z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
838 if (mat.coeff(1,1) > mat.coeff(0,0))
840 if (mat.coeff(2,2) > mat.coeff(i,i))
845 t = sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
846 q.
coeffs().coeffRef(i) = Scalar(0.5) * t;
848 q.
w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
849 q.
coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
850 q.
coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
856 template<
typename Other>
859 typedef typename Other::Scalar Scalar;
870 #endif // EIGEN_QUATERNION_H