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(); }
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
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)
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:45
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: JacobiSVD.h:489
Map(Scalar *coeffs)
Definition: Quaternion.h:420
Map(const Scalar *coeffs)
Definition: Quaternion.h:383
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
Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:183
Base class for quaternion expressions.
Definition: Quaternion.h:36
Scalar squaredNorm() const
Definition: Quaternion.h:120
const internal::traits< Derived >::Coefficients & coeffs() const
Definition: Quaternion.h:90
QuaternionBase & setIdentity()
Definition: Quaternion.h:115
Quaternion< Scalar > conjugate() const
Definition: Quaternion.h:713
NonConstCoeffReturnType y()
Definition: Quaternion.h:77
bool isApprox(const QuaternionBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Quaternion.h:166
static Quaternion< Scalar > Identity()
Definition: Quaternion.h:111
void normalize()
Definition: Quaternion.h:129
Derived & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)
Definition: Quaternion.h:600
CoeffReturnType z() const
Definition: Quaternion.h:70
NonConstCoeffReturnType x()
Definition: Quaternion.h:75
Matrix3 toRotationMatrix() const
Definition: Quaternion.h:554
VectorBlock< Coefficients, 3 > vec()
Definition: Quaternion.h:87
internal::cast_return_type< Derived, Quaternion< NewScalarType > >::type cast() const
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:523
Vector3 _transformVector(const Vector3 &v) const
Definition: Quaternion.h:493
CoeffReturnType y() const
Definition: Quaternion.h:68
Scalar norm() const
Definition: Quaternion.h:125
Quaternion< Scalar > inverse() const
Definition: Quaternion.h:682
CoeffReturnType w() const
Definition: Quaternion.h:72
Matrix< Scalar, 3, 3 > Matrix3
Definition: Quaternion.h:59
NonConstCoeffReturnType w()
Definition: Quaternion.h:81
const VectorBlock< const Coefficients, 3 > vec() const
Definition: Quaternion.h:84
Quaternion< Scalar > normalized() const
Definition: Quaternion.h:132
internal::traits< Derived >::Coefficients & coeffs()
Definition: Quaternion.h:93
AngleAxis< Scalar > AngleAxisType
Definition: Quaternion.h:61
Derived & operator*=(const QuaternionBase< OtherDerived > &q)
Definition: Quaternion.h:478
CoeffReturnType x() const
Definition: Quaternion.h:66
The quaternion class used to represent 3D orientations and rotations.
Definition: Quaternion.h:251
Quaternion(const QuaternionBase< Derived > &other)
Definition: Quaternion.h:280
static Quaternion UnitRandom()
Definition: Quaternion.h:641
Quaternion(const AngleAxisType &aa)
Definition: Quaternion.h:283
Quaternion(const Quaternion< OtherScalar, OtherOptions > &other)
Definition: Quaternion.h:294
Quaternion(const MatrixBase< Derived > &other)
Definition: Quaternion.h:290
Quaternion()
Definition: Quaternion.h:265
Quaternion(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition: Quaternion.h:274
Quaternion(const Scalar *data)
Definition: Quaternion.h:277
Common base class for compact rotation representations.
Definition: RotationBase.h:30
internal::traits< Derived >::Scalar Scalar
Definition: RotationBase.h:34
const MatrixVType & matrixV() const
Definition: SVDBase.h:99
Expression of a fixed-size or dynamic-size sub-vector.
Definition: VectorBlock.h:60
Map< Quaternion< double >, Aligned > QuaternionMapAlignedd
Definition: Quaternion.h:440
Quaternion< double > Quaterniond
Definition: Quaternion.h:328
Quaternion< float > Quaternionf
Definition: Quaternion.h:325
Map< Quaternion< float >, 0 > QuaternionMapf
Definition: Quaternion.h:431
Map< Quaternion< double >, 0 > QuaternionMapd
Definition: Quaternion.h:434
Map< Quaternion< float >, Aligned > QuaternionMapAlignedf
Definition: Quaternion.h:437
@ Aligned
Definition: Constants.h:235
@ DontAlign
Definition: Constants.h:326
@ AutoAlign
Definition: Constants.h:324
@ ComputeFullV
Definition: Constants.h:387
const unsigned int LvalueBit
Definition: Constants.h:139
Namespace containing all symbols from the Eigen library.
Definition: Core:309
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_acos_op< typename Derived::Scalar >, const Derived > acos(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cos_op< typename Derived::Scalar >, const Derived > cos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sin_op< typename Derived::Scalar >, const Derived > sin(const Eigen::ArrayBase< Derived > &x)
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: NumTraits.h:151