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>
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;
46 Flags = Eigen::internal::traits<Derived>::Flags
60 inline Scalar
x()
const {
return this->derived().coeffs().coeff(0); }
62 inline Scalar
y()
const {
return this->derived().coeffs().coeff(1); }
64 inline Scalar
z()
const {
return this->derived().coeffs().coeff(2); }
66 inline Scalar
w()
const {
return this->derived().coeffs().coeff(3); }
69 inline Scalar&
x() {
return this->derived().coeffs().coeffRef(0); }
71 inline Scalar&
y() {
return this->derived().coeffs().coeffRef(1); }
73 inline Scalar&
z() {
return this->derived().coeffs().coeffRef(2); }
75 inline Scalar&
w() {
return this->derived().coeffs().coeffRef(3); }
84 inline const typename internal::traits<Derived>::Coefficients&
coeffs()
const {
return derived().coeffs(); }
87 inline typename internal::traits<Derived>::Coefficients&
coeffs() {
return derived().coeffs(); }
141 template<
typename Derived1,
typename Derived2>
159 template<
class OtherDerived>
171 template<
typename NewScalarType>
172 inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type
cast()
const
174 return typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type(derived());
177#ifdef EIGEN_QUATERNIONBASE_PLUGIN
178# include EIGEN_QUATERNIONBASE_PLUGIN
212template<
typename _Scalar,
int _Options>
213struct traits<Quaternion<_Scalar,_Options> >
215 typedef Quaternion<_Scalar,_Options> PlainObject;
216 typedef _Scalar Scalar;
217 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
219 IsAligned = internal::traits<Coefficients>::Flags &
AlignedBit,
225template<
typename _Scalar,
int _Options>
229 enum { IsAligned = internal::traits<Quaternion>::IsAligned };
232 typedef _Scalar Scalar;
234 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Quaternion)
235 using Base::operator*=;
238 typedef typename Base::AngleAxisType AngleAxisType;
250 inline Quaternion(
const Scalar&
w,
const Scalar&
x,
const Scalar&
y,
const Scalar&
z) : m_coeffs(
x,
y,
z,
w){}
259 explicit inline Quaternion(
const AngleAxisType& aa) { *
this = aa; }
265 template<
typename Derived>
269 template<
typename OtherScalar,
int OtherOptions>
273 template<
typename Derived1,
typename Derived2>
276 inline Coefficients& coeffs() {
return m_coeffs;}
277 inline const Coefficients& coeffs()
const {
return m_coeffs;}
279 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(
bool(IsAligned))
282 Coefficients m_coeffs;
284#ifndef EIGEN_PARSED_BY_DOXYGEN
285 static EIGEN_STRONG_INLINE
void _check_template_params()
287 EIGEN_STATIC_ASSERT( (_Options & DontAlign) == _Options,
288 INVALID_MATRIX_TEMPLATE_PARAMETERS)
305 template<
typename _Scalar,
int _Options>
306 struct traits<
Map<
Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
313 template<
typename _Scalar,
int _Options>
314 struct traits<Map<const Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
316 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
317 typedef traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> > TraitsBase;
319 Flags = TraitsBase::Flags & ~LvalueBit
335template<
typename _Scalar,
int _Options>
337 :
public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
342 typedef _Scalar Scalar;
343 typedef typename internal::traits<Map>::Coefficients Coefficients;
344 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
345 using Base::operator*=;
353 EIGEN_STRONG_INLINE
Map(
const Scalar* coeffs) : m_coeffs(coeffs) {}
355 inline const Coefficients& coeffs()
const {
return m_coeffs;}
358 const Coefficients m_coeffs;
372template<
typename _Scalar,
int _Options>
379 typedef _Scalar Scalar;
380 typedef typename internal::traits<Map>::Coefficients Coefficients;
381 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(
Map)
382 using Base::operator*=;
390 EIGEN_STRONG_INLINE
Map(Scalar* coeffs) : m_coeffs(coeffs) {}
392 inline Coefficients& coeffs() {
return m_coeffs; }
393 inline const Coefficients& coeffs()
const {
return m_coeffs; }
396 Coefficients m_coeffs;
419template<
int Arch,
class Derived1,
class Derived2,
typename Scalar,
int _Options>
struct quat_product
424 a.
w() * b.
w() - a.
x() * b.
x() - a.
y() * b.
y() - a.
z() * b.
z(),
425 a.
w() * b.
x() + a.
x() * b.
w() + a.
y() * b.
z() - a.
z() * b.
y(),
426 a.
w() * b.
y() + a.
y() * b.
w() + a.
z() * b.
x() - a.
x() * b.
z(),
427 a.
w() * b.
z() + a.
z() * b.
w() + a.
x() * b.
y() - a.
y() * b.
x()
434template <
class Derived>
435template <
class OtherDerived>
436EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
439 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
440 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
441 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
442 typename internal::traits<Derived>::Scalar,
443 internal::traits<Derived>::IsAligned && internal::traits<OtherDerived>::IsAligned>::run(*
this, other);
447template <
class Derived>
448template <
class OtherDerived>
451 derived() = derived() * other.derived();
462template <
class Derived>
473 return v + this->
w() * uv + this->
vec().cross(uv);
476template<
class Derived>
479 coeffs() = other.
coeffs();
483template<
class Derived>
484template<
class OtherDerived>
485EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const QuaternionBase<OtherDerived>& other)
487 coeffs() = other.coeffs();
493template<
class Derived>
494EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
const AngleAxisType& aa)
498 Scalar ha = Scalar(0.5)*aa.
angle();
500 this->
vec() = sin(ha) * aa.
axis();
510template<
class Derived>
511template<
class MatrixDerived>
514 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
515 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
516 internal::quaternionbase_assign_impl<MatrixDerived>::run(*
this, xpr.derived());
523template<
class Derived>
533 const Scalar tx = Scalar(2)*this->
x();
534 const Scalar ty = Scalar(2)*this->
y();
535 const Scalar tz = Scalar(2)*this->
z();
536 const Scalar twx = tx*this->
w();
537 const Scalar twy = ty*this->w();
538 const Scalar twz = tz*this->w();
539 const Scalar txx = tx*this->
x();
540 const Scalar txy = ty*this->x();
541 const Scalar txz = tz*this->x();
542 const Scalar tyy = ty*this->
y();
543 const Scalar tyz = tz*this->y();
544 const Scalar tzz = tz*this->
z();
546 res.coeffRef(0,0) = Scalar(1)-(tyy+tzz);
547 res.coeffRef(0,1) = txy-twz;
548 res.coeffRef(0,2) = txz+twy;
549 res.coeffRef(1,0) = txy+twz;
550 res.coeffRef(1,1) = Scalar(1)-(txx+tzz);
551 res.coeffRef(1,2) = tyz-twx;
552 res.coeffRef(2,0) = txz-twy;
553 res.coeffRef(2,1) = tyz+twx;
554 res.coeffRef(2,2) = Scalar(1)-(txx+tyy);
569template<
class Derived>
570template<
typename Derived1,
typename Derived2>
577 Scalar c = v1.
dot(v0);
587 if (c < Scalar(-1)+NumTraits<Scalar>::dummy_precision())
589 c = (max)(c,Scalar(-1));
594 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
595 this->
w() = sqrt(w2);
596 this->
vec() = axis * sqrt(Scalar(1) - w2);
600 Scalar s = sqrt((Scalar(1)+c)*Scalar(2));
601 Scalar invs = Scalar(1)/s;
602 this->
vec() = axis * invs;
603 this->
w() = s * Scalar(0.5);
619template<
typename Scalar,
int Options>
620template<
typename Derived1,
typename Derived2>
635template <
class Derived>
655template <
class Derived>
665template <
class Derived>
666template <
class OtherDerived>
667inline typename internal::traits<Derived>::Scalar
673 return Scalar(2) * atan2( d.
vec().norm(), abs(d.
w()) );
684template <
class Derived>
685template <
class OtherDerived>
692 static const Scalar one = Scalar(1) - NumTraits<Scalar>::epsilon();
693 Scalar d = this->
dot(other);
694 Scalar absD = abs(d);
701 scale0 = Scalar(1) - t;
707 Scalar theta = acos(absD);
708 Scalar sinTheta = sin(theta);
710 scale0 = sin( ( Scalar(1) - t ) * theta) / sinTheta;
711 scale1 = sin( ( t * theta) ) / sinTheta;
713 if(d<Scalar(0)) scale1 = -scale1;
721template<
typename Other>
722struct quaternionbase_assign_impl<Other,3,3>
724 typedef typename Other::Scalar Scalar;
725 typedef DenseIndex Index;
731 Scalar t = mat.trace();
734 t = sqrt(t + Scalar(1.0));
735 q.
w() = Scalar(0.5)*t;
737 q.
x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
738 q.
y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
739 q.
z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
744 if (mat.coeff(1,1) > mat.coeff(0,0))
746 if (mat.coeff(2,2) > mat.coeff(i,i))
748 DenseIndex j = (i+1)%3;
749 DenseIndex k = (j+1)%3;
751 t = sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
752 q.
coeffs().coeffRef(i) = Scalar(0.5) * t;
754 q.
w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
755 q.
coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
756 q.
coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
762template<
typename Other>
763struct quaternionbase_assign_impl<Other,4,1>
765 typedef typename Other::Scalar Scalar;
766 template<
class Derived>
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:87
const Vector3 & axis() const
Definition AngleAxis.h:92
Eigen::Transpose< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > > transpose()
Definition Transpose.h:199
ColXpr col(Index i)
Definition DenseBase.h:733
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition JacobiSVD.h:521
const MatrixVType & matrixV() const
Definition JacobiSVD.h:649
Map(Scalar *coeffs)
Definition Quaternion.h:390
Map(const Scalar *coeffs)
Definition Quaternion.h:353
A matrix or vector expression mapping an existing array of data.
Definition Map.h:106
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
const PlainObject normalized() const
Definition Dot.h:139
MatrixBase< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::template cross_product_return_type< OtherDerived >::type cross(const MatrixBase< OtherDerived > &other) const
Definition OrthoMethods.h:26
internal::scalar_product_traits< typenameinternal::traits< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Scalar, typenameinternal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
Definition Dot.h:63
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:129
Base class for quaternion expressions.
Definition Quaternion.h:36
Scalar dot(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:133
QuaternionBase & setIdentity()
Definition Quaternion.h:109
VectorBlock< Coefficients, 3 > vec()
Definition Quaternion.h:81
Quaternion< Scalar > conjugate() const
Definition Quaternion.h:657
Scalar & z()
Definition Quaternion.h:73
Scalar w() const
Definition Quaternion.h:66
Scalar z() const
Definition Quaternion.h:64
internal::traits< Derived >::Coefficients & coeffs()
Definition Quaternion.h:87
internal::cast_return_type< Derived, Quaternion< NewScalarType > >::type cast() const
Definition Quaternion.h:172
bool isApprox(const QuaternionBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Quaternion.h:160
Quaternion< Scalar > normalized() const
Definition Quaternion.h:126
Vector3 _transformVector(const Vector3 &v) const
Definition Quaternion.h:464
Derived & operator=(const AngleAxisType &aa)
Definition Quaternion.h:494
const VectorBlock< const Coefficients, 3 > vec() const
Definition Quaternion.h:78
Scalar & y()
Definition Quaternion.h:71
Derived & operator*=(const QuaternionBase< OtherDerived > &q)
Definition Quaternion.h:449
Matrix< Scalar, 3, 1 > Vector3
Definition Quaternion.h:51
const internal::traits< Derived >::Coefficients & coeffs() const
Definition Quaternion.h:84
Matrix< Scalar, 3, 3 > Matrix3
Definition Quaternion.h:53
Derived & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)
Definition Quaternion.h:571
AngleAxis< Scalar > AngleAxisType
Definition Quaternion.h:55
void normalize()
Definition Quaternion.h:123
Quaternion< Scalar > inverse() const
Definition Quaternion.h:636
Scalar norm() const
Definition Quaternion.h:119
Scalar & x()
Definition Quaternion.h:69
Matrix3 toRotationMatrix() const
Definition Quaternion.h:525
Scalar x() const
Definition Quaternion.h:60
Scalar y() const
Definition Quaternion.h:62
Scalar squaredNorm() const
Definition Quaternion.h:114
Scalar & w()
Definition Quaternion.h:75
static Quaternion< Scalar > Identity()
Definition Quaternion.h:105
The quaternion class used to represent 3D orientations and rotations.
Definition Quaternion.h:227
Quaternion(const AngleAxisType &aa)
Definition Quaternion.h:259
Quaternion(const QuaternionBase< Derived > &other)
Definition Quaternion.h:256
Quaternion()
Definition Quaternion.h:241
Quaternion(const Quaternion< OtherScalar, OtherOptions > &other)
Definition Quaternion.h:270
Quaternion(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition Quaternion.h:250
Quaternion(const MatrixBase< Derived > &other)
Definition Quaternion.h:266
Quaternion(const Scalar *data)
Definition Quaternion.h:253
Common base class for compact rotation representations.
Definition RotationBase.h:30
Expression of a fixed-size or dynamic-size sub-vector.
Definition VectorBlock.h:61
Quaternion< double > Quaterniond
Definition Quaternion.h:298
Map< Quaternion< float >, 0 > QuaternionMapf
Definition Quaternion.h:401
Map< Quaternion< float >, Aligned > QuaternionMapAlignedf
Definition Quaternion.h:407
Map< Quaternion< double >, Aligned > QuaternionMapAlignedd
Definition Quaternion.h:410
Map< Quaternion< double >, 0 > QuaternionMapd
Definition Quaternion.h:404
Quaternion< float > Quaternionf
Definition Quaternion.h:295
@ ComputeFullV
Definition Constants.h:331
@ Aligned
Definition Constants.h:194
const unsigned int LvalueBit
Definition Constants.h:131
const unsigned int AlignedBit
Definition Constants.h:147