11#ifndef EIGEN_QUATERNION_H
12#define EIGEN_QUATERNION_H
22template<
typename Other,
23 int OtherRows=Other::RowsAtCompileTime,
24 int OtherCols=Other::ColsAtCompileTime>
25struct quaternionbase_assign_impl;
34template<
class Derived>
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(); }
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>
195 EIGEN_DEVICE_FUNC
inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type
cast()
const;
199 template<
typename NewScalarType>
200 EIGEN_DEVICE_FUNC
inline
201 typename internal::enable_if<internal::is_same<Scalar,NewScalarType>::value,
const Derived&>::type
cast()
const
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)
259template<
typename _Scalar,
int _Options>
260struct traits<Quaternion<_Scalar,_Options> >
262 typedef Quaternion<_Scalar,_Options> PlainObject;
263 typedef _Scalar Scalar;
264 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
266 Alignment = internal::traits<Coefficients>::Alignment,
272template<
typename _Scalar,
int _Options>
277 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment>0 };
279 typedef _Scalar Scalar;
281 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
282 using Base::operator*=;
284 typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
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> >
384 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
385 typedef traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> > TraitsBase;
387 Flags = TraitsBase::Flags & ~LvalueBit
403template<
typename _Scalar,
int _Options>
405 :
public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
410 typedef _Scalar Scalar;
411 typedef typename internal::traits<Map>::Coefficients Coefficients;
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;
440template<
typename _Scalar,
int _Options>
447 typedef _Scalar Scalar;
448 typedef typename internal::traits<Map>::Coefficients Coefficients;
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;
487template<
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()
502template <
class Derived>
503template <
class OtherDerived>
504EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
507 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
508 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
509 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
510 typename internal::traits<Derived>::Scalar>::run(*
this, other);
514template <
class Derived>
515template <
class OtherDerived>
518 derived() = derived() * other.derived();
529template <
class Derived>
538 Vector3 uv = this->vec().cross(v);
540 return v + this->w() * uv + this->vec().cross(uv);
543template<
class Derived>
546 coeffs() = other.
coeffs();
550template<
class Derived>
551template<
class OtherDerived>
552EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const QuaternionBase<OtherDerived>& other)
554 coeffs() = other.coeffs();
560template<
class Derived>
565 Scalar ha = Scalar(0.5)*aa.
angle();
567 this->vec() = sin(ha) * aa.
axis();
577template<
class Derived>
578template<
class MatrixDerived>
581 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
582 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
583 internal::quaternionbase_assign_impl<MatrixDerived>::run(*
this, xpr.
derived());
590template<
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);
636template<
class Derived>
637template<
typename Derived1,
typename Derived2>
640 EIGEN_USING_STD(sqrt)
643 Scalar c = v1.dot(v0);
655 c = numext::maxi(c,Scalar(-1));
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);
678template<
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));
703template<
typename Scalar,
int Options>
704template<
typename Derived1,
typename Derived2>
719template <
class Derived>
723 Scalar n2 = this->squaredNorm();
735template<
int Arch,
class Derived,
typename Scalar>
struct quat_conj
749template <
class Derived>
750EIGEN_DEVICE_FUNC
inline Quaternion<typename internal::traits<Derived>::Scalar>
753 return internal::quat_conj<Architecture::Target, Derived,
754 typename internal::traits<Derived>::Scalar>::run(*
this);
761template <
class Derived>
762template <
class OtherDerived>
763EIGEN_DEVICE_FUNC
inline typename internal::traits<Derived>::Scalar
766 EIGEN_USING_STD(atan2)
768 return Scalar(2) * atan2( d.
vec().norm(), numext::abs(d.
w()) );
779template <
class Derived>
780template <
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;
815template<
typename Other>
816struct quaternionbase_assign_impl<Other,3,3>
818 typedef typename Other::Scalar Scalar;
819 template<
class Derived> EIGEN_DEVICE_FUNC
static inline void run(
QuaternionBase<Derived>& q,
const Other& a_mat)
821 const typename internal::nested_eval<Other,2>::type mat(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;
856template<
typename Other>
857struct quaternionbase_assign_impl<Other,4,1>
859 typedef typename Other::Scalar Scalar;
860 template<
class Derived> 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:50
Scalar angle() const
Definition: AngleAxis.h:91
const Vector3 & axis() const
Definition: AngleAxis.h:96
Derived & derived()
Definition: EigenBase.h:46
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: JacobiSVD.h:490
Map(Scalar *coeffs)
Definition: Quaternion.h:458
Map(const Scalar *coeffs)
Definition: Quaternion.h:421
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:50
const PlainObject normalized() const
Definition: Dot.h:124
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:175
Base class for quaternion expressions.
Definition: Quaternion.h:36
Scalar squaredNorm() const
Definition: Quaternion.h:120
QuaternionBase & setIdentity()
Definition: Quaternion.h:115
Quaternion< Scalar > normalized() const
Definition: Quaternion.h:132
internal::traits< Derived >::Coefficients & coeffs()
Definition: Quaternion.h:93
internal::cast_return_type< Derived, Quaternion< NewScalarType > >::type cast() const
VectorBlock< Coefficients, 3 > vec()
Definition: Quaternion.h:87
const VectorBlock< const Coefficients, 3 > vec() const
Definition: Quaternion.h:84
static Quaternion< Scalar > Identity()
Definition: Quaternion.h:111
bool operator!=(const QuaternionBase< OtherDerived > &other) const
Definition: Quaternion.h:174
Quaternion< Scalar > conjugate() const
Definition: Quaternion.h:751
NonConstCoeffReturnType y()
Definition: Quaternion.h:77
bool isApprox(const QuaternionBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Quaternion.h:182
void normalize()
Definition: Quaternion.h:129
Derived & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)
Definition: Quaternion.h:638
CoeffReturnType z() const
Definition: Quaternion.h:70
NonConstCoeffReturnType x()
Definition: Quaternion.h:75
Matrix3 toRotationMatrix() const
Definition: Quaternion.h:592
Matrix< Scalar, 3, 1 > Vector3
Definition: Quaternion.h:57
NonConstCoeffReturnType z()
Definition: Quaternion.h:79
Scalar dot(const QuaternionBase< OtherDerived > &other) const
Definition: Quaternion.h:139
Derived & operator=(const AngleAxisType &aa)
Definition: Quaternion.h:561
Vector3 _transformVector(const Vector3 &v) const
Definition: Quaternion.h:531
CoeffReturnType y() const
Definition: Quaternion.h:68
Scalar norm() const
Definition: Quaternion.h:125
Quaternion< Scalar > inverse() const
Definition: Quaternion.h:720
CoeffReturnType w() const
Definition: Quaternion.h:72
Matrix< Scalar, 3, 3 > Matrix3
Definition: Quaternion.h:59
const internal::traits< Derived >::Coefficients & coeffs() const
Definition: Quaternion.h:90
bool operator==(const QuaternionBase< OtherDerived > &other) const
Definition: Quaternion.h:166
NonConstCoeffReturnType w()
Definition: Quaternion.h:81
AngleAxis< Scalar > AngleAxisType
Definition: Quaternion.h:61
Derived & operator*=(const QuaternionBase< OtherDerived > &q)
Definition: Quaternion.h:516
CoeffReturnType x() const
Definition: Quaternion.h:66
The quaternion class used to represent 3D orientations and rotations.
Definition: Quaternion.h:274
Quaternion(const QuaternionBase< Derived > &other)
Definition: Quaternion.h:303
static Quaternion UnitRandom()
Definition: Quaternion.h:679
Quaternion(const AngleAxisType &aa)
Definition: Quaternion.h:306
Quaternion(const Quaternion< OtherScalar, OtherOptions > &other)
Definition: Quaternion.h:317
Quaternion(const MatrixBase< Derived > &other)
Definition: Quaternion.h:313
Quaternion()
Definition: Quaternion.h:288
Quaternion(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition: Quaternion.h:297
Quaternion(const Scalar *data)
Definition: Quaternion.h:300
Common base class for compact rotation representations.
Definition: RotationBase.h:30
friend RotationMatrixType operator*(const EigenBase< OtherDerived > &l, const Derived &r)
Definition: RotationBase.h:76
const MatrixVType & matrixV() const
Definition: SVDBase.h:117
Expression of a fixed-size or dynamic-size sub-vector.
Definition: VectorBlock.h:60
@ Aligned
Definition: Constants.h:240
@ DontAlign
Definition: Constants.h:325
@ ComputeFullV
Definition: Constants.h:397
const unsigned int LvalueBit
Definition: Constants.h:144
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:233