Eigen  3.3.0
 
Loading...
Searching...
No Matches
Quaternion.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2009 Mathieu Gautier <mathieu.gautier@cea.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_QUATERNION_H
12#define EIGEN_QUATERNION_H
13namespace Eigen {
14
15
16/***************************************************************************
17* Definition of QuaternionBase<Derived>
18* The implementation is at the end of the file
19***************************************************************************/
20
21namespace internal {
22template<typename Other,
23 int OtherRows=Other::RowsAtCompileTime,
24 int OtherCols=Other::ColsAtCompileTime>
25struct quaternionbase_assign_impl;
26}
27
34template<class Derived>
35class QuaternionBase : public RotationBase<Derived, 3>
36{
37 public:
39
40 using Base::operator*;
41 using Base::derived;
42
43 typedef typename internal::traits<Derived>::Scalar Scalar;
44 typedef typename NumTraits<Scalar>::Real RealScalar;
45 typedef typename internal::traits<Derived>::Coefficients Coefficients;
46 enum {
47 Flags = Eigen::internal::traits<Derived>::Flags
48 };
49
50 // typedef typename Matrix<Scalar,4,1> Coefficients;
57
58
59
61 EIGEN_DEVICE_FUNC inline Scalar x() const { return this->derived().coeffs().coeff(0); }
63 EIGEN_DEVICE_FUNC inline Scalar y() const { return this->derived().coeffs().coeff(1); }
65 EIGEN_DEVICE_FUNC inline Scalar z() const { return this->derived().coeffs().coeff(2); }
67 EIGEN_DEVICE_FUNC inline Scalar w() const { return this->derived().coeffs().coeff(3); }
68
70 EIGEN_DEVICE_FUNC inline Scalar& x() { return this->derived().coeffs().coeffRef(0); }
72 EIGEN_DEVICE_FUNC inline Scalar& y() { return this->derived().coeffs().coeffRef(1); }
74 EIGEN_DEVICE_FUNC inline Scalar& z() { return this->derived().coeffs().coeffRef(2); }
76 EIGEN_DEVICE_FUNC inline Scalar& w() { return this->derived().coeffs().coeffRef(3); }
77
79 EIGEN_DEVICE_FUNC inline const VectorBlock<const Coefficients,3> vec() const { return coeffs().template head<3>(); }
80
82 EIGEN_DEVICE_FUNC inline VectorBlock<Coefficients,3> vec() { return coeffs().template head<3>(); }
83
85 EIGEN_DEVICE_FUNC inline const typename internal::traits<Derived>::Coefficients& coeffs() const { return derived().coeffs(); }
86
88 EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Coefficients& coeffs() { return derived().coeffs(); }
89
90 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE QuaternionBase<Derived>& operator=(const QuaternionBase<Derived>& other);
91 template<class OtherDerived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator=(const QuaternionBase<OtherDerived>& other);
92
93// disabled this copy operator as it is giving very strange compilation errors when compiling
94// test_stdvector with GCC 4.4.2. This looks like a GCC bug though, so feel free to re-enable it if it's
95// useful; however notice that we already have the templated operator= above and e.g. in MatrixBase
96// we didn't have to add, in addition to templated operator=, such a non-templated copy operator.
97// Derived& operator=(const QuaternionBase& other)
98// { return operator=<Derived>(other); }
99
100 EIGEN_DEVICE_FUNC Derived& operator=(const AngleAxisType& aa);
101 template<class OtherDerived> EIGEN_DEVICE_FUNC Derived& operator=(const MatrixBase<OtherDerived>& m);
102
106 EIGEN_DEVICE_FUNC static inline Quaternion<Scalar> Identity() { return Quaternion<Scalar>(Scalar(1), Scalar(0), Scalar(0), Scalar(0)); }
107
110 EIGEN_DEVICE_FUNC inline QuaternionBase& setIdentity() { coeffs() << Scalar(0), Scalar(0), Scalar(0), Scalar(1); return *this; }
111
115 EIGEN_DEVICE_FUNC inline Scalar squaredNorm() const { return coeffs().squaredNorm(); }
116
120 EIGEN_DEVICE_FUNC inline Scalar norm() const { return coeffs().norm(); }
121
124 EIGEN_DEVICE_FUNC inline void normalize() { coeffs().normalize(); }
127 EIGEN_DEVICE_FUNC inline Quaternion<Scalar> normalized() const { return Quaternion<Scalar>(coeffs().normalized()); }
128
134 template<class OtherDerived> EIGEN_DEVICE_FUNC inline Scalar dot(const QuaternionBase<OtherDerived>& other) const { return coeffs().dot(other.coeffs()); }
135
136 template<class OtherDerived> EIGEN_DEVICE_FUNC Scalar angularDistance(const QuaternionBase<OtherDerived>& other) const;
137
139 EIGEN_DEVICE_FUNC Matrix3 toRotationMatrix() const;
140
142 template<typename Derived1, typename Derived2>
143 EIGEN_DEVICE_FUNC Derived& setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
144
145 template<class OtherDerived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<Scalar> operator* (const QuaternionBase<OtherDerived>& q) const;
146 template<class OtherDerived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator*= (const QuaternionBase<OtherDerived>& q);
147
149 EIGEN_DEVICE_FUNC Quaternion<Scalar> inverse() const;
150
152 EIGEN_DEVICE_FUNC Quaternion<Scalar> conjugate() const;
153
154 template<class OtherDerived> EIGEN_DEVICE_FUNC Quaternion<Scalar> slerp(const Scalar& t, const QuaternionBase<OtherDerived>& other) const;
155
160 template<class OtherDerived>
161 EIGEN_DEVICE_FUNC bool isApprox(const QuaternionBase<OtherDerived>& other, const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
162 { return coeffs().isApprox(other.coeffs(), prec); }
163
165 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Vector3 _transformVector(const Vector3& v) const;
166
172 template<typename NewScalarType>
173 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type cast() const
174 {
175 return typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type(derived());
176 }
177
178#ifdef EIGEN_QUATERNIONBASE_PLUGIN
179# include EIGEN_QUATERNIONBASE_PLUGIN
180#endif
181};
182
183/***************************************************************************
184* Definition/implementation of Quaternion<Scalar>
185***************************************************************************/
186
212namespace internal {
213template<typename _Scalar,int _Options>
214struct traits<Quaternion<_Scalar,_Options> >
215{
216 typedef Quaternion<_Scalar,_Options> PlainObject;
217 typedef _Scalar Scalar;
218 typedef Matrix<_Scalar,4,1,_Options> Coefficients;
219 enum{
220 Alignment = internal::traits<Coefficients>::Alignment,
221 Flags = LvalueBit
222 };
223};
224}
225
226template<typename _Scalar, int _Options>
227class Quaternion : public QuaternionBase<Quaternion<_Scalar,_Options> >
228{
229public:
231 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment>0 };
232
233 typedef _Scalar Scalar;
234
235 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Quaternion)
236 using Base::operator*=;
237
238 typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
239 typedef typename Base::AngleAxisType AngleAxisType;
240
242 EIGEN_DEVICE_FUNC inline Quaternion() {}
243
251 EIGEN_DEVICE_FUNC inline Quaternion(const Scalar& w, const Scalar& x, const Scalar& y, const Scalar& z) : m_coeffs(x, y, z, w){}
252
254 EIGEN_DEVICE_FUNC explicit inline Quaternion(const Scalar* data) : m_coeffs(data) {}
255
257 template<class Derived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion(const QuaternionBase<Derived>& other) { this->Base::operator=(other); }
258
260 EIGEN_DEVICE_FUNC explicit inline Quaternion(const AngleAxisType& aa) { *this = aa; }
261
266 template<typename Derived>
267 EIGEN_DEVICE_FUNC explicit inline Quaternion(const MatrixBase<Derived>& other) { *this = other; }
268
270 template<typename OtherScalar, int OtherOptions>
271 EIGEN_DEVICE_FUNC explicit inline Quaternion(const Quaternion<OtherScalar, OtherOptions>& other)
272 { m_coeffs = other.coeffs().template cast<Scalar>(); }
273
274 EIGEN_DEVICE_FUNC static Quaternion UnitRandom();
275
276 template<typename Derived1, typename Derived2>
277 EIGEN_DEVICE_FUNC static Quaternion FromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
278
279 EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs;}
280 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs;}
281
282 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(NeedsAlignment))
283
284#ifdef EIGEN_QUATERNION_PLUGIN
285# include EIGEN_QUATERNION_PLUGIN
286#endif
287
288protected:
289 Coefficients m_coeffs;
290
291#ifndef EIGEN_PARSED_BY_DOXYGEN
292 static EIGEN_STRONG_INLINE void _check_template_params()
293 {
294 EIGEN_STATIC_ASSERT( (_Options & DontAlign) == _Options,
295 INVALID_MATRIX_TEMPLATE_PARAMETERS)
296 }
297#endif
298};
299
306
307/***************************************************************************
308* Specialization of Map<Quaternion<Scalar>>
309***************************************************************************/
310
311namespace internal {
312 template<typename _Scalar, int _Options>
313 struct traits<Map<Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
314 {
315 typedef Map<Matrix<_Scalar,4,1>, _Options> Coefficients;
316 };
317}
318
319namespace internal {
320 template<typename _Scalar, int _Options>
321 struct traits<Map<const Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
322 {
323 typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
324 typedef traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> > TraitsBase;
325 enum {
326 Flags = TraitsBase::Flags & ~LvalueBit
327 };
328 };
329}
330
342template<typename _Scalar, int _Options>
343class Map<const Quaternion<_Scalar>, _Options >
344 : public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
345{
346 public:
348
349 typedef _Scalar Scalar;
350 typedef typename internal::traits<Map>::Coefficients Coefficients;
351 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
352 using Base::operator*=;
353
360 EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Map(const Scalar* coeffs) : m_coeffs(coeffs) {}
361
362 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs;}
363
364 protected:
365 const Coefficients m_coeffs;
366};
367
379template<typename _Scalar, int _Options>
380class Map<Quaternion<_Scalar>, _Options >
381 : public QuaternionBase<Map<Quaternion<_Scalar>, _Options> >
382{
383 public:
384 typedef QuaternionBase<Map<Quaternion<_Scalar>, _Options> > Base;
385
386 typedef _Scalar Scalar;
387 typedef typename internal::traits<Map>::Coefficients Coefficients;
388 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
389 using Base::operator*=;
390
397 EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Map(Scalar* coeffs) : m_coeffs(coeffs) {}
398
399 EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs; }
400 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs; }
401
402 protected:
403 Coefficients m_coeffs;
404};
405
418
419/***************************************************************************
420* Implementation of QuaternionBase methods
421***************************************************************************/
422
423// Generic Quaternion * Quaternion product
424// This product can be specialized for a given architecture via the Arch template argument.
425namespace internal {
426template<int Arch, class Derived1, class Derived2, typename Scalar, int _Options> struct quat_product
427{
428 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Quaternion<Scalar> run(const QuaternionBase<Derived1>& a, const QuaternionBase<Derived2>& b){
429 return Quaternion<Scalar>
430 (
431 a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
432 a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
433 a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
434 a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x()
435 );
436 }
437};
438}
439
441template <class Derived>
442template <class OtherDerived>
443EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
445{
446 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
447 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
448 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
449 typename internal::traits<Derived>::Scalar,
450 EIGEN_PLAIN_ENUM_MIN(internal::traits<Derived>::Alignment, internal::traits<OtherDerived>::Alignment)>::run(*this, other);
451}
452
454template <class Derived>
455template <class OtherDerived>
456EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator*= (const QuaternionBase<OtherDerived>& other)
457{
458 derived() = derived() * other.derived();
459 return derived();
460}
461
469template <class Derived>
470EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename QuaternionBase<Derived>::Vector3
472{
473 // Note that this algorithm comes from the optimization by hand
474 // of the conversion to a Matrix followed by a Matrix/Vector product.
475 // It appears to be much faster than the common algorithm found
476 // in the literature (30 versus 39 flops). It also requires two
477 // Vector3 as temporaries.
478 Vector3 uv = this->vec().cross(v);
479 uv += uv;
480 return v + this->w() * uv + this->vec().cross(uv);
481}
482
483template<class Derived>
484EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE QuaternionBase<Derived>& QuaternionBase<Derived>::operator=(const QuaternionBase<Derived>& other)
485{
486 coeffs() = other.coeffs();
487 return derived();
488}
489
490template<class Derived>
491template<class OtherDerived>
492EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const QuaternionBase<OtherDerived>& other)
493{
494 coeffs() = other.coeffs();
495 return derived();
496}
497
500template<class Derived>
501EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const AngleAxisType& aa)
502{
503 EIGEN_USING_STD_MATH(cos)
504 EIGEN_USING_STD_MATH(sin)
505 Scalar ha = Scalar(0.5)*aa.angle(); // Scalar(0.5) to suppress precision loss warnings
506 this->w() = cos(ha);
507 this->vec() = sin(ha) * aa.axis();
508 return derived();
509}
510
517template<class Derived>
518template<class MatrixDerived>
519EIGEN_DEVICE_FUNC inline Derived& QuaternionBase<Derived>::operator=(const MatrixBase<MatrixDerived>& xpr)
520{
521 EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
522 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
523 internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived());
524 return derived();
525}
526
530template<class Derived>
531EIGEN_DEVICE_FUNC inline typename QuaternionBase<Derived>::Matrix3
533{
534 // NOTE if inlined, then gcc 4.2 and 4.4 get rid of the temporary (not gcc 4.3 !!)
535 // if not inlined then the cost of the return by value is huge ~ +35%,
536 // however, not inlining this function is an order of magnitude slower, so
537 // it has to be inlined, and so the return by value is not an issue
538 Matrix3 res;
539
540 const Scalar tx = Scalar(2)*this->x();
541 const Scalar ty = Scalar(2)*this->y();
542 const Scalar tz = Scalar(2)*this->z();
543 const Scalar twx = tx*this->w();
544 const Scalar twy = ty*this->w();
545 const Scalar twz = tz*this->w();
546 const Scalar txx = tx*this->x();
547 const Scalar txy = ty*this->x();
548 const Scalar txz = tz*this->x();
549 const Scalar tyy = ty*this->y();
550 const Scalar tyz = tz*this->y();
551 const Scalar tzz = tz*this->z();
552
553 res.coeffRef(0,0) = Scalar(1)-(tyy+tzz);
554 res.coeffRef(0,1) = txy-twz;
555 res.coeffRef(0,2) = txz+twy;
556 res.coeffRef(1,0) = txy+twz;
557 res.coeffRef(1,1) = Scalar(1)-(txx+tzz);
558 res.coeffRef(1,2) = tyz-twx;
559 res.coeffRef(2,0) = txz-twy;
560 res.coeffRef(2,1) = tyz+twx;
561 res.coeffRef(2,2) = Scalar(1)-(txx+tyy);
562
563 return res;
564}
565
576template<class Derived>
577template<typename Derived1, typename Derived2>
579{
580 EIGEN_USING_STD_MATH(sqrt)
581 Vector3 v0 = a.normalized();
582 Vector3 v1 = b.normalized();
583 Scalar c = v1.dot(v0);
584
585 // if dot == -1, vectors are nearly opposites
586 // => accurately compute the rotation axis by computing the
587 // intersection of the two planes. This is done by solving:
588 // x^T v0 = 0
589 // x^T v1 = 0
590 // under the constraint:
591 // ||x|| = 1
592 // which yields a singular value problem
593 if (c < Scalar(-1)+NumTraits<Scalar>::dummy_precision())
594 {
595 c = numext::maxi(c,Scalar(-1));
596 Matrix<Scalar,2,3> m; m << v0.transpose(), v1.transpose();
598 Vector3 axis = svd.matrixV().col(2);
599
600 Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
601 this->w() = sqrt(w2);
602 this->vec() = axis * sqrt(Scalar(1) - w2);
603 return derived();
604 }
605 Vector3 axis = v0.cross(v1);
606 Scalar s = sqrt((Scalar(1)+c)*Scalar(2));
607 Scalar invs = Scalar(1)/s;
608 this->vec() = axis * invs;
609 this->w() = s * Scalar(0.5);
610
611 return derived();
612}
613
618template<typename Scalar, int Options>
620{
621 EIGEN_USING_STD_MATH(sqrt)
622 EIGEN_USING_STD_MATH(sin)
623 EIGEN_USING_STD_MATH(cos)
624 const Scalar u1 = internal::random<Scalar>(0, 1),
625 u2 = internal::random<Scalar>(0, 2*EIGEN_PI),
626 u3 = internal::random<Scalar>(0, 2*EIGEN_PI);
627 const Scalar a = sqrt(1 - u1),
628 b = sqrt(u1);
629 return Quaternion (a * sin(u2), a * cos(u2), b * sin(u3), b * cos(u3));
630}
631
632
643template<typename Scalar, int Options>
644template<typename Derived1, typename Derived2>
646{
647 Quaternion quat;
648 quat.setFromTwoVectors(a, b);
649 return quat;
650}
651
652
659template <class Derived>
661{
662 // FIXME should this function be called multiplicativeInverse and conjugate() be called inverse() or opposite() ??
663 Scalar n2 = this->squaredNorm();
664 if (n2 > Scalar(0))
665 return Quaternion<Scalar>(conjugate().coeffs() / n2);
666 else
667 {
668 // return an invalid result to flag the error
669 return Quaternion<Scalar>(Coefficients::Zero());
670 }
671}
672
673// Generic conjugate of a Quaternion
674namespace internal {
675template<int Arch, class Derived, typename Scalar, int _Options> struct quat_conj
676{
677 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Quaternion<Scalar> run(const QuaternionBase<Derived>& q){
678 return Quaternion<Scalar>(q.w(),-q.x(),-q.y(),-q.z());
679 }
680};
681}
682
689template <class Derived>
690EIGEN_DEVICE_FUNC inline Quaternion<typename internal::traits<Derived>::Scalar>
692{
693 return internal::quat_conj<Architecture::Target, Derived,
694 typename internal::traits<Derived>::Scalar,
695 internal::traits<Derived>::Alignment>::run(*this);
696
697}
698
702template <class Derived>
703template <class OtherDerived>
704EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Scalar
706{
707 EIGEN_USING_STD_MATH(atan2)
708 Quaternion<Scalar> d = (*this) * other.conjugate();
709 return Scalar(2) * atan2( d.vec().norm(), numext::abs(d.w()) );
710}
711
712
713
720template <class Derived>
721template <class OtherDerived>
724{
725 EIGEN_USING_STD_MATH(acos)
726 EIGEN_USING_STD_MATH(sin)
727 const Scalar one = Scalar(1) - NumTraits<Scalar>::epsilon();
728 Scalar d = this->dot(other);
729 Scalar absD = numext::abs(d);
730
731 Scalar scale0;
732 Scalar scale1;
733
734 if(absD>=one)
735 {
736 scale0 = Scalar(1) - t;
737 scale1 = t;
738 }
739 else
740 {
741 // theta is the angle between the 2 quaternions
742 Scalar theta = acos(absD);
743 Scalar sinTheta = sin(theta);
744
745 scale0 = sin( ( Scalar(1) - t ) * theta) / sinTheta;
746 scale1 = sin( ( t * theta) ) / sinTheta;
747 }
748 if(d<Scalar(0)) scale1 = -scale1;
749
750 return Quaternion<Scalar>(scale0 * coeffs() + scale1 * other.coeffs());
751}
752
753namespace internal {
754
755// set from a rotation matrix
756template<typename Other>
757struct quaternionbase_assign_impl<Other,3,3>
758{
759 typedef typename Other::Scalar Scalar;
760 template<class Derived> EIGEN_DEVICE_FUNC static inline void run(QuaternionBase<Derived>& q, const Other& a_mat)
761 {
762 const typename internal::nested_eval<Other,2>::type mat(a_mat);
763 EIGEN_USING_STD_MATH(sqrt)
764 // This algorithm comes from "Quaternion Calculus and Fast Animation",
765 // Ken Shoemake, 1987 SIGGRAPH course notes
766 Scalar t = mat.trace();
767 if (t > Scalar(0))
768 {
769 t = sqrt(t + Scalar(1.0));
770 q.w() = Scalar(0.5)*t;
771 t = Scalar(0.5)/t;
772 q.x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
773 q.y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
774 q.z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
775 }
776 else
777 {
778 Index i = 0;
779 if (mat.coeff(1,1) > mat.coeff(0,0))
780 i = 1;
781 if (mat.coeff(2,2) > mat.coeff(i,i))
782 i = 2;
783 Index j = (i+1)%3;
784 Index k = (j+1)%3;
785
786 t = sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
787 q.coeffs().coeffRef(i) = Scalar(0.5) * t;
788 t = Scalar(0.5)/t;
789 q.w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
790 q.coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
791 q.coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
792 }
793 }
794};
795
796// set from a vector of coefficients assumed to be a quaternion
797template<typename Other>
798struct quaternionbase_assign_impl<Other,4,1>
799{
800 typedef typename Other::Scalar Scalar;
801 template<class Derived> EIGEN_DEVICE_FUNC static inline void run(QuaternionBase<Derived>& q, const Other& vec)
802 {
803 q.coeffs() = vec;
804 }
805};
806
807} // end namespace internal
808
809} // end namespace Eigen
810
811#endif // EIGEN_QUATERNION_H
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:44
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: JacobiSVD.h:485
Map(Scalar *coeffs)
Definition: Quaternion.h:397
Map(const Scalar *coeffs)
Definition: Quaternion.h:360
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:90
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
const PlainObject normalized() const
Definition: Dot.h:118
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:177
Base class for quaternion expressions.
Definition: Quaternion.h:36
Scalar y() const
Definition: Quaternion.h:63
Scalar squaredNorm() const
Definition: Quaternion.h:115
Scalar & y()
Definition: Quaternion.h:72
QuaternionBase & setIdentity()
Definition: Quaternion.h:110
Quaternion< Scalar > normalized() const
Definition: Quaternion.h:127
internal::traits< Derived >::Coefficients & coeffs()
Definition: Quaternion.h:88
internal::cast_return_type< Derived, Quaternion< NewScalarType > >::type cast() const
Definition: Quaternion.h:173
VectorBlock< Coefficients, 3 > vec()
Definition: Quaternion.h:82
const VectorBlock< const Coefficients, 3 > vec() const
Definition: Quaternion.h:79
static Quaternion< Scalar > Identity()
Definition: Quaternion.h:106
Scalar w() const
Definition: Quaternion.h:67
Quaternion< Scalar > conjugate() const
Definition: Quaternion.h:691
bool isApprox(const QuaternionBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Quaternion.h:161
Scalar & z()
Definition: Quaternion.h:74
void normalize()
Definition: Quaternion.h:124
Derived & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)
Definition: Quaternion.h:578
Scalar & x()
Definition: Quaternion.h:70
Matrix3 toRotationMatrix() const
Definition: Quaternion.h:532
Matrix< Scalar, 3, 1 > Vector3
Definition: Quaternion.h:52
Scalar dot(const QuaternionBase< OtherDerived > &other) const
Definition: Quaternion.h:134
Derived & operator=(const AngleAxisType &aa)
Definition: Quaternion.h:501
Vector3 _transformVector(const Vector3 &v) const
Definition: Quaternion.h:471
Scalar norm() const
Definition: Quaternion.h:120
Quaternion< Scalar > inverse() const
Definition: Quaternion.h:660
Scalar z() const
Definition: Quaternion.h:65
Matrix< Scalar, 3, 3 > Matrix3
Definition: Quaternion.h:54
const internal::traits< Derived >::Coefficients & coeffs() const
Definition: Quaternion.h:85
AngleAxis< Scalar > AngleAxisType
Definition: Quaternion.h:56
Scalar x() const
Definition: Quaternion.h:61
Scalar & w()
Definition: Quaternion.h:76
Derived & operator*=(const QuaternionBase< OtherDerived > &q)
Definition: Quaternion.h:456
The quaternion class used to represent 3D orientations and rotations.
Definition: Quaternion.h:228
Quaternion(const QuaternionBase< Derived > &other)
Definition: Quaternion.h:257
static Quaternion UnitRandom()
Definition: Quaternion.h:619
Quaternion(const AngleAxisType &aa)
Definition: Quaternion.h:260
Quaternion(const Quaternion< OtherScalar, OtherOptions > &other)
Definition: Quaternion.h:271
Quaternion(const MatrixBase< Derived > &other)
Definition: Quaternion.h:267
Quaternion()
Definition: Quaternion.h:242
Quaternion(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition: Quaternion.h:251
Quaternion(const Scalar *data)
Definition: Quaternion.h:254
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:99
Expression of a fixed-size or dynamic-size sub-vector.
Definition: VectorBlock.h:60
@ 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:287
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_acos_op< typename Derived::Scalar >, const Derived > acos(const Eigen::ArrayBase< Derived > &x)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:151