11#ifndef EIGEN_QUATERNION_H
12#define EIGEN_QUATERNION_H
14#include "./InternalHeaderCheck.h"
24template <
typename Other,
int OtherRows = Other::RowsAtCompileTime,
int OtherCols = Other::ColsAtCompileTime>
25struct quaternionbase_assign_impl;
34template <
class Derived>
39 using Base::operator*;
42 typedef typename internal::traits<Derived>::Scalar Scalar;
43 typedef typename NumTraits<Scalar>::Real RealScalar;
44 typedef typename internal::traits<Derived>::Coefficients Coefficients;
45 typedef typename Coefficients::CoeffReturnType CoeffReturnType;
46 typedef std::conditional_t<bool(internal::traits<Derived>::Flags&
LvalueBit), Scalar&, CoeffReturnType>
47 NonConstCoeffReturnType;
49 enum { Flags = Eigen::internal::traits<Derived>::Flags };
60 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline CoeffReturnType
x()
const {
return this->derived().coeffs().coeff(0); }
62 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline CoeffReturnType
y()
const {
return this->derived().coeffs().coeff(1); }
64 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline CoeffReturnType
z()
const {
return this->derived().coeffs().coeff(2); }
66 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline CoeffReturnType
w()
const {
return this->derived().coeffs().coeff(3); }
69 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline NonConstCoeffReturnType
x() {
return this->derived().coeffs().x(); }
71 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline NonConstCoeffReturnType
y() {
return this->derived().coeffs().y(); }
73 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline NonConstCoeffReturnType
z() {
return this->derived().coeffs().z(); }
75 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
inline NonConstCoeffReturnType
w() {
return this->derived().coeffs().w(); }
84 EIGEN_DEVICE_FUNC
inline const typename internal::traits<Derived>::Coefficients&
coeffs()
const {
85 return derived().coeffs();
89 EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Coefficients&
coeffs() {
return derived().coeffs(); }
92 template <
class OtherDerived>
103 template <
class OtherDerived>
116 coeffs() << Scalar(0), Scalar(0), Scalar(0), Scalar(1);
128 EIGEN_DEVICE_FUNC
inline Scalar
norm()
const {
return coeffs().norm(); }
142 template <
class OtherDerived>
147 template <
class OtherDerived>
154 template <
typename Derived1,
typename Derived2>
157 template <
class OtherDerived>
159 template <
class OtherDerived>
168 template <
class OtherDerived>
175 template <
class OtherDerived>
184 template <
class OtherDerived>
193 template <
class OtherDerived>
202#ifdef EIGEN_PARSED_BY_DOXYGEN
208 template <
typename NewScalarType>
209 EIGEN_DEVICE_FUNC
inline typename internal::cast_return_type<Derived, Quaternion<NewScalarType> >::type
cast()
const;
213 template <
typename NewScalarType>
214 EIGEN_DEVICE_FUNC
inline std::enable_if_t<internal::is_same<Scalar, NewScalarType>::value,
const Derived&>
cast()
219 template <
typename NewScalarType>
220 EIGEN_DEVICE_FUNC
inline std::enable_if_t<!internal::is_same<Scalar, NewScalarType>::value,
228 friend std::ostream& operator<<(std::ostream& s,
const QuaternionBase<Derived>& q) {
229 s << q.x() <<
"i + " << q.y() <<
"j + " << q.z() <<
"k"
235#ifdef EIGEN_QUATERNIONBASE_PLUGIN
236#include EIGEN_QUATERNIONBASE_PLUGIN
239 EIGEN_DEFAULT_COPY_CONSTRUCTOR(QuaternionBase)
240 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(QuaternionBase)
275template <
typename Scalar_,
int Options_>
276struct traits<Quaternion<Scalar_, Options_> > {
277 typedef Quaternion<Scalar_, Options_> PlainObject;
278 typedef Scalar_ Scalar;
279 typedef Matrix<Scalar_, 4, 1, Options_> Coefficients;
280 enum { Alignment = internal::traits<Coefficients>::Alignment, Flags =
LvalueBit };
284template <
typename Scalar_,
int Options_>
288 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment > 0 };
290 typedef Scalar_ Scalar;
292 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
293 using Base::operator*=;
295 typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
308 EIGEN_DEVICE_FUNC
inline Quaternion(
const Scalar&
w,
const Scalar&
x,
const Scalar&
y,
const Scalar&
z)
309 : m_coeffs(
x,
y,
z,
w) {}
314 template <
typename Derived>
317 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
321 EIGEN_DEVICE_FUNC
explicit inline Quaternion(
const Scalar* data) : m_coeffs(data) {}
324 template <
class Derived>
326 this->Base::operator=(other);
336 template <
typename Derived>
342 template <
typename OtherScalar,
int OtherOptions>
350 EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
351 : m_coeffs(std::move(other.coeffs())) {}
355 EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value) {
356 m_coeffs = std::move(other.
coeffs());
362 template <
typename Derived1,
typename Derived2>
365 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs; }
366 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
368 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(
bool(NeedsAlignment))
370#ifdef EIGEN_QUATERNION_PLUGIN
371#include EIGEN_QUATERNION_PLUGIN
375 Coefficients m_coeffs;
377#ifndef EIGEN_PARSED_BY_DOXYGEN
378 EIGEN_STATIC_ASSERT((Options_ &
DontAlign) == Options_, INVALID_MATRIX_TEMPLATE_PARAMETERS)
394template <
typename Scalar_,
int Options_>
396 : traits<Quaternion<Scalar_, (int(Options_) & Aligned) == Aligned ? AutoAlign : DontAlign> > {
402template <
typename Scalar_,
int Options_>
403struct traits<Map<const Quaternion<Scalar_>, Options_> >
404 : traits<Quaternion<Scalar_, (int(Options_) & Aligned) == Aligned ? AutoAlign : DontAlign> > {
405 typedef Map<const Matrix<Scalar_, 4, 1>, Options_> Coefficients;
406 typedef traits<Quaternion<Scalar_, (int(Options_) & Aligned) == Aligned ? AutoAlign : DontAlign> > TraitsBase;
407 enum { Flags = TraitsBase::Flags & ~LvalueBit };
422template <
typename Scalar_,
int Options_>
427 typedef Scalar_ Scalar;
428 typedef typename internal::traits<Map>::Coefficients Coefficients;
429 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
430 using Base::operator*=;
438 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(
const Scalar* coeffs) : m_coeffs(coeffs) {}
440 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
443 const Coefficients m_coeffs;
457template <
typename Scalar_,
int Options_>
462 typedef Scalar_ Scalar;
463 typedef typename internal::traits<Map>::Coefficients Coefficients;
464 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
465 using Base::operator*=;
473 EIGEN_DEVICE_FUNC
explicit EIGEN_STRONG_INLINE
Map(Scalar* coeffs) : m_coeffs(coeffs) {}
475 EIGEN_DEVICE_FUNC
inline Coefficients& coeffs() {
return m_coeffs; }
476 EIGEN_DEVICE_FUNC
inline const Coefficients& coeffs()
const {
return m_coeffs; }
479 Coefficients m_coeffs;
502template <
int Arch,
class Derived1,
class Derived2,
typename Scalar>
507 a.
w() * b.
x() + a.
x() * b.
w() + a.
y() * b.
z() - a.
z() * b.
y(),
508 a.
w() * b.
y() + a.
y() * b.
w() + a.
z() * b.
x() - a.
x() * b.
z(),
509 a.
w() * b.
z() + a.
z() * b.
w() + a.
x() * b.
y() - a.
y() * b.
x());
515template <
class Derived>
516template <
class OtherDerived>
517EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
520 (internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
521 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
522 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
523 typename internal::traits<Derived>::Scalar>::run(*
this, other);
527template <
class Derived>
528template <
class OtherDerived>
531 derived() = derived() * other.derived();
542template <
class Derived>
550 Vector3 uv = this->vec().cross(v);
552 return v + this->w() * uv + this->vec().cross(uv);
555template <
class Derived>
558 coeffs() = other.
coeffs();
562template <
class Derived>
563template <
class OtherDerived>
564EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
565 const QuaternionBase<OtherDerived>& other) {
566 coeffs() = other.coeffs();
572template <
class Derived>
576 Scalar ha = Scalar(0.5) * aa.
angle();
578 this->vec() = sin(ha) * aa.
axis();
588template <
class Derived>
589template <
class MatrixDerived>
592 (internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
593 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
594 internal::quaternionbase_assign_impl<MatrixDerived>::run(*
this, xpr.
derived());
601template <
class Derived>
610 const Scalar tx = Scalar(2) * this->x();
611 const Scalar ty = Scalar(2) * this->y();
612 const Scalar tz = Scalar(2) * this->z();
613 const Scalar twx = tx * this->w();
614 const Scalar twy = ty * this->w();
615 const Scalar twz = tz * this->w();
616 const Scalar txx = tx * this->x();
617 const Scalar txy = ty * this->x();
618 const Scalar txz = tz * this->x();
619 const Scalar tyy = ty * this->y();
620 const Scalar tyz = tz * this->y();
621 const Scalar tzz = tz * this->z();
623 res.
coeffRef(0, 0) = Scalar(1) - (tyy + tzz);
627 res.
coeffRef(1, 1) = Scalar(1) - (txx + tzz);
631 res.
coeffRef(2, 2) = Scalar(1) - (txx + tyy);
646template <
class Derived>
647template <
typename Derived1,
typename Derived2>
650 EIGEN_USING_STD(sqrt)
653 Scalar c = v1.dot(v0);
664 c = numext::maxi(c, Scalar(-1));
666 m << v0.transpose(), v1.transpose();
670 Scalar w2 = (Scalar(1) + c) * Scalar(0.5);
671 this->w() = sqrt(w2);
672 this->vec() = axis * sqrt(Scalar(1) - w2);
676 Scalar s = sqrt((Scalar(1) + c) * Scalar(2));
677 Scalar invs = Scalar(1) / s;
678 this->vec() = axis * invs;
679 this->w() = s * Scalar(0.5);
688template <
typename Scalar,
int Options>
690 EIGEN_USING_STD(sqrt)
693 const Scalar u1 = internal::random<Scalar>(0, 1), u2 = internal::random<Scalar>(0, 2 * EIGEN_PI),
694 u3 = internal::random<Scalar>(0, 2 * EIGEN_PI);
695 const Scalar a = sqrt(Scalar(1) - u1), b = sqrt(u1);
696 return Quaternion(a * sin(u2), a * cos(u2), b * sin(u3), b * cos(u3));
709template <
typename Scalar,
int Options>
710template <
typename Derived1,
typename Derived2>
724template <
class Derived>
728 Scalar n2 = this->squaredNorm();
739template <
int Arch,
class Derived,
typename Scalar>
753template <
class Derived>
756 return internal::quat_conj<Architecture::Target, Derived, typename internal::traits<Derived>::Scalar>::run(*
this);
762template <
class Derived>
763template <
class OtherDerived>
766 EIGEN_USING_STD(atan2)
768 return Scalar(2) * atan2(d.
vec().norm(), numext::abs(d.
w()));
777template <
class Derived>
778template <
class OtherDerived>
781 EIGEN_USING_STD(acos)
784 Scalar d = this->dot(other);
785 Scalar absD = numext::abs(d);
791 scale0 = Scalar(1) - t;
795 Scalar theta = acos(absD);
796 Scalar sinTheta = sin(theta);
798 scale0 = sin((Scalar(1) - t) * theta) / sinTheta;
799 scale1 = sin((t * theta)) / sinTheta;
801 if (d < Scalar(0)) scale1 = -scale1;
809template <
typename Other>
810struct quaternionbase_assign_impl<Other, 3, 3> {
811 typedef typename Other::Scalar Scalar;
812 template <
class Derived>
814 const typename internal::nested_eval<Other, 2>::type mat(a_mat);
815 EIGEN_USING_STD(
sqrt)
818 Scalar t = mat.trace();
820 t =
sqrt(t + Scalar(1.0));
821 q.
w() = Scalar(0.5) * t;
823 q.
x() = (mat.coeff(2, 1) - mat.coeff(1, 2)) * t;
824 q.
y() = (mat.coeff(0, 2) - mat.coeff(2, 0)) * t;
825 q.
z() = (mat.coeff(1, 0) - mat.coeff(0, 1)) * t;
828 if (mat.coeff(1, 1) > mat.coeff(0, 0)) i = 1;
829 if (mat.coeff(2, 2) > mat.coeff(i, i)) i = 2;
830 Index j = (i + 1) % 3;
831 Index k = (j + 1) % 3;
833 t =
sqrt(mat.coeff(i, i) - mat.coeff(j, j) - mat.coeff(k, k) + Scalar(1.0));
834 q.
coeffs().coeffRef(i) = Scalar(0.5) * t;
836 q.
w() = (mat.coeff(k, j) - mat.coeff(j, k)) * t;
837 q.
coeffs().coeffRef(j) = (mat.coeff(j, i) + mat.coeff(i, j)) * t;
838 q.
coeffs().coeffRef(k) = (mat.coeff(k, i) + mat.coeff(i, k)) * t;
844template <
typename Other>
845struct quaternionbase_assign_impl<Other, 4, 1> {
846 typedef typename Other::Scalar Scalar;
847 template <
class Derived>
848 EIGEN_DEVICE_FUNC
static inline void run(QuaternionBase<Derived>& q,
const Other& vec) {
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition AngleAxis.h:52
const Vector3 & axis() const
Definition AngleAxis.h:99
Scalar angle() const
Definition AngleAxis.h:94
Derived & derived()
Definition EigenBase.h:49
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition JacobiSVD.h:500
Map(Scalar *coeffs)
Definition Quaternion.h:473
Map(const Scalar *coeffs)
Definition Quaternion.h:438
A matrix or vector expression mapping an existing array of data.
Definition Map.h:96
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
const PlainObject normalized() const
Definition Dot.h:113
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:217
Base class for quaternion expressions.
Definition Quaternion.h:35
Matrix< Scalar, 3, 3 > Matrix3
Definition Quaternion.h:55
Scalar squaredNorm() const
Definition Quaternion.h:123
QuaternionBase & setIdentity()
Definition Quaternion.h:115
Quaternion< Scalar > normalized() const
Definition Quaternion.h:135
EIGEN_CONSTEXPR CoeffReturnType x() const
Definition Quaternion.h:60
internal::traits< Derived >::Coefficients & coeffs()
Definition Quaternion.h:89
Matrix< Scalar, 3, 1 > Vector3
Definition Quaternion.h:53
internal::cast_return_type< Derived, Quaternion< NewScalarType > >::type cast() const
VectorBlock< Coefficients, 3 > vec()
Definition Quaternion.h:81
const VectorBlock< const Coefficients, 3 > vec() const
Definition Quaternion.h:78
static Quaternion< Scalar > Identity()
Definition Quaternion.h:109
EIGEN_CONSTEXPR NonConstCoeffReturnType z()
Definition Quaternion.h:73
EIGEN_CONSTEXPR NonConstCoeffReturnType y()
Definition Quaternion.h:71
bool operator!=(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:185
Quaternion< Scalar > conjugate() const
Definition Quaternion.h:754
bool isApprox(const QuaternionBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Quaternion.h:194
EIGEN_CONSTEXPR NonConstCoeffReturnType w()
Definition Quaternion.h:75
EIGEN_CONSTEXPR NonConstCoeffReturnType x()
Definition Quaternion.h:69
void normalize()
Definition Quaternion.h:132
Derived & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)
Definition Quaternion.h:648
EIGEN_CONSTEXPR CoeffReturnType w() const
Definition Quaternion.h:66
Matrix3 toRotationMatrix() const
Definition Quaternion.h:602
EIGEN_CONSTEXPR CoeffReturnType y() const
Definition Quaternion.h:62
Scalar dot(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:143
Derived & operator=(const AngleAxisType &aa)
Definition Quaternion.h:573
Vector3 _transformVector(const Vector3 &v) const
Definition Quaternion.h:544
Scalar norm() const
Definition Quaternion.h:128
Quaternion< Scalar > inverse() const
Definition Quaternion.h:725
EIGEN_CONSTEXPR CoeffReturnType z() const
Definition Quaternion.h:64
const internal::traits< Derived >::Coefficients & coeffs() const
Definition Quaternion.h:84
bool operator==(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:176
AngleAxis< Scalar > AngleAxisType
Definition Quaternion.h:57
Derived & operator*=(const QuaternionBase< OtherDerived > &q)
Definition Quaternion.h:529
The quaternion class used to represent 3D orientations and rotations.
Definition Quaternion.h:285
Quaternion(const Quaternion< OtherScalar, OtherOptions > &other)
Definition Quaternion.h:343
Quaternion(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition Quaternion.h:308
Quaternion(const QuaternionBase< Derived > &other)
Definition Quaternion.h:325
Quaternion(const Scalar &w, const Eigen::MatrixBase< Derived > &vec)
Definition Quaternion.h:315
static Quaternion UnitRandom()
Definition Quaternion.h:689
Quaternion(const MatrixBase< Derived > &other)
Definition Quaternion.h:337
Quaternion(const AngleAxisType &aa)
Definition Quaternion.h:330
Quaternion()
Definition Quaternion.h:299
Quaternion & operator=(Quaternion &&other) EIGEN_NOEXCEPT_IF(std
Definition Quaternion.h:354
Quaternion(Quaternion &&other) EIGEN_NOEXCEPT_IF(std
Definition Quaternion.h:349
Quaternion(const Scalar *data)
Definition Quaternion.h:321
Common base class for compact rotation representations.
Definition RotationBase.h:32
friend RotationMatrixType operator*(const EigenBase< OtherDerived > &l, const Derived &r)
Definition RotationBase.h:83
const MatrixVType & matrixV() const
Definition SVDBase.h:189
Expression of a fixed-size or dynamic-size sub-vector.
Definition VectorBlock.h:58
@ Aligned
Definition Constants.h:242
@ DontAlign
Definition Constants.h:324
@ ComputeFullV
Definition Constants.h:393
const unsigned int LvalueBit
Definition Constants.h:148
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523