Eigen  5.0.1
 
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_QUATERNION_H
13#define EIGEN_QUATERNION_H
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19/***************************************************************************
20 * Definition of QuaternionBase<Derived>
21 * The implementation is at the end of the file
22 ***************************************************************************/
23
24namespace internal {
25template <typename Other, int OtherRows = Other::RowsAtCompileTime, int OtherCols = Other::ColsAtCompileTime>
26struct quaternionbase_assign_impl;
27}
28
35template <class Derived>
36class QuaternionBase : public RotationBase<Derived, 3> {
37 public:
38 using Base = RotationBase<Derived, 3>;
39
40 using Base::operator*;
41 using Base::derived;
42
43 using Scalar = typename internal::traits<Derived>::Scalar;
44 using RealScalar = typename NumTraits<Scalar>::Real;
45 using Coefficients = typename internal::traits<Derived>::Coefficients;
46 using CoeffReturnType = typename Coefficients::CoeffReturnType;
47 using NonConstCoeffReturnType =
48 std::conditional_t<bool(internal::traits<Derived>::Flags& LvalueBit), Scalar&, CoeffReturnType>;
49
50 enum { Flags = Eigen::internal::traits<Derived>::Flags };
51
58
60 EIGEN_DEVICE_FUNC constexpr CoeffReturnType x() const { return this->derived().coeffs().coeff(0); }
62 EIGEN_DEVICE_FUNC constexpr CoeffReturnType y() const { return this->derived().coeffs().coeff(1); }
64 EIGEN_DEVICE_FUNC constexpr CoeffReturnType z() const { return this->derived().coeffs().coeff(2); }
66 EIGEN_DEVICE_FUNC constexpr CoeffReturnType w() const { return this->derived().coeffs().coeff(3); }
67
69 EIGEN_DEVICE_FUNC constexpr NonConstCoeffReturnType x() { return this->derived().coeffs().x(); }
71 EIGEN_DEVICE_FUNC constexpr NonConstCoeffReturnType y() { return this->derived().coeffs().y(); }
73 EIGEN_DEVICE_FUNC constexpr NonConstCoeffReturnType z() { return this->derived().coeffs().z(); }
75 EIGEN_DEVICE_FUNC constexpr NonConstCoeffReturnType w() { return this->derived().coeffs().w(); }
76
78 EIGEN_DEVICE_FUNC inline const VectorBlock<const Coefficients, 3> vec() const { return coeffs().template head<3>(); }
79
81 EIGEN_DEVICE_FUNC inline VectorBlock<Coefficients, 3> vec() { return coeffs().template head<3>(); }
82
84 EIGEN_DEVICE_FUNC inline const typename internal::traits<Derived>::Coefficients& coeffs() const {
85 return derived().coeffs();
86 }
87
96 EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Coefficients coeffsScalarFirst() const {
97 return derived().coeffsScalarFirst();
98 }
99
107 EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Coefficients coeffsScalarLast() const {
108 return derived().coeffsScalarLast();
109 }
110
112 EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Coefficients& coeffs() { return derived().coeffs(); }
113
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE QuaternionBase<Derived>& operator=(const QuaternionBase<Derived>& other);
115 template <class OtherDerived>
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator=(const QuaternionBase<OtherDerived>& other);
117
118 EIGEN_DEVICE_FUNC Derived& operator=(const AngleAxisType& aa);
119 template <class OtherDerived>
120 EIGEN_DEVICE_FUNC Derived& operator=(const MatrixBase<OtherDerived>& m);
121
125 EIGEN_DEVICE_FUNC static inline Quaternion<Scalar> Identity() {
126 return Quaternion<Scalar>(Scalar(1), Scalar(0), Scalar(0), Scalar(0));
127 }
128
131 EIGEN_DEVICE_FUNC inline QuaternionBase& setIdentity() {
132 coeffs() << Scalar(0), Scalar(0), Scalar(0), Scalar(1);
133 return *this;
134 }
135
139 EIGEN_DEVICE_FUNC inline Scalar squaredNorm() const { return coeffs().squaredNorm(); }
140
144 EIGEN_DEVICE_FUNC inline Scalar norm() const { return coeffs().norm(); }
145
148 EIGEN_DEVICE_FUNC inline void normalize() { coeffs().normalize(); }
151 EIGEN_DEVICE_FUNC inline Quaternion<Scalar> normalized() const { return Quaternion<Scalar>(coeffs().normalized()); }
152
158 template <class OtherDerived>
159 EIGEN_DEVICE_FUNC inline Scalar dot(const QuaternionBase<OtherDerived>& other) const {
160 return coeffs().dot(other.coeffs());
161 }
162
163 template <class OtherDerived>
164 EIGEN_DEVICE_FUNC Scalar angularDistance(const QuaternionBase<OtherDerived>& other) const;
165
167 EIGEN_DEVICE_FUNC inline Matrix3 toRotationMatrix() const;
168
170 template <typename Derived1, typename Derived2>
171 EIGEN_DEVICE_FUNC Derived& setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
172
173 template <typename OtherDerived>
174 EIGEN_DEVICE_FUNC Derived& setFromScaledAxis(const MatrixBase<OtherDerived>& scaled_axis);
175
176 EIGEN_DEVICE_FUNC inline Vector3 toScaledAxis() const;
177
178 template <class OtherDerived>
179 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<Scalar> operator*(const QuaternionBase<OtherDerived>& q) const;
180 template <class OtherDerived>
181 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator*=(const QuaternionBase<OtherDerived>& q);
182
184 EIGEN_DEVICE_FUNC Quaternion<Scalar> inverse() const;
185
187 EIGEN_DEVICE_FUNC Quaternion<Scalar> conjugate() const;
188
189 template <class OtherDerived>
190 EIGEN_DEVICE_FUNC Quaternion<Scalar> slerp(const Scalar& t, const QuaternionBase<OtherDerived>& other) const;
191
196 template <class OtherDerived>
197 EIGEN_DEVICE_FUNC inline bool operator==(const QuaternionBase<OtherDerived>& other) const {
198 return coeffs() == other.coeffs();
199 }
200
205 template <class OtherDerived>
206 EIGEN_DEVICE_FUNC inline bool operator!=(const QuaternionBase<OtherDerived>& other) const {
207 return coeffs() != other.coeffs();
208 }
209
214 template <class OtherDerived>
215 EIGEN_DEVICE_FUNC bool isApprox(const QuaternionBase<OtherDerived>& other,
216 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const {
217 return coeffs().isApprox(other.coeffs(), prec);
218 }
219
221 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Vector3 _transformVector(const Vector3& v) const;
222
223#ifdef EIGEN_PARSED_BY_DOXYGEN
229 template <typename NewScalarType>
230 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Derived, Quaternion<NewScalarType> >::type cast() const;
231
232#else
233
234 template <typename NewScalarType>
235 EIGEN_DEVICE_FUNC inline std::enable_if_t<std::is_same<Scalar, NewScalarType>::value, const Derived&> cast() const {
236 return derived();
237 }
238
239 template <typename NewScalarType>
240 EIGEN_DEVICE_FUNC inline std::enable_if_t<!std::is_same<Scalar, NewScalarType>::value, Quaternion<NewScalarType> >
241 cast() const {
242 return Quaternion<NewScalarType>(coeffs().template cast<NewScalarType>());
243 }
244#endif
245
246#ifndef EIGEN_NO_IO
247 friend std::ostream& operator<<(std::ostream& s, const QuaternionBase<Derived>& q) {
248 s << q.x() << "i + " << q.y() << "j + " << q.z() << "k"
249 << " + " << q.w();
250 return s;
251 }
252#endif
253
254#ifdef EIGEN_QUATERNIONBASE_PLUGIN
255#include EIGEN_QUATERNIONBASE_PLUGIN
256#endif
257 protected:
258 EIGEN_DEFAULT_COPY_CONSTRUCTOR(QuaternionBase)
259 EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(QuaternionBase)
260};
261
262/***************************************************************************
263 * Definition/implementation of Quaternion<Scalar>
264 ***************************************************************************/
265
292
293namespace internal {
294template <typename Scalar_, int Options_>
295struct traits<Quaternion<Scalar_, Options_> > {
296 using PlainObject = Quaternion<Scalar_, Options_>;
297 using Scalar = Scalar_;
298 using Coefficients = Matrix<Scalar_, 4, 1, Options_>;
299 enum { Alignment = internal::traits<Coefficients>::Alignment, Flags = LvalueBit };
300};
301} // namespace internal
302
303template <typename Scalar_, int Options_>
304class Quaternion : public QuaternionBase<Quaternion<Scalar_, Options_> > {
305 public:
307 enum { NeedsAlignment = internal::traits<Quaternion>::Alignment > 0 };
308
309 using Scalar = Scalar_;
310
311 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Quaternion)
312 using Base::operator*=;
313
314 using Coefficients = typename internal::traits<Quaternion>::Coefficients;
315 using AngleAxisType = typename Base::AngleAxisType;
316
318 EIGEN_DEVICE_FUNC inline Quaternion() {}
319
327 EIGEN_DEVICE_FUNC inline Quaternion(const Scalar& w, const Scalar& x, const Scalar& y, const Scalar& z)
328 : m_coeffs(x, y, z, w) {}
329
333 template <typename Derived>
334 EIGEN_DEVICE_FUNC inline Quaternion(const Scalar& w, const Eigen::MatrixBase<Derived>& vec)
335 : m_coeffs(vec.x(), vec.y(), vec.z(), w) {
336 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
337 }
338
340 EIGEN_DEVICE_FUNC explicit inline Quaternion(const Scalar* data) : m_coeffs(data) {}
341
343 template <class Derived>
344 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion(const QuaternionBase<Derived>& other) {
345 this->Base::operator=(other);
346 }
347
349 EIGEN_DEVICE_FUNC explicit inline Quaternion(const AngleAxisType& aa) { *this = aa; }
350
355 template <typename Derived>
356 EIGEN_DEVICE_FUNC explicit inline Quaternion(const MatrixBase<Derived>& other) {
357 *this = other;
358 }
359
361 template <typename OtherScalar, int OtherOptions>
362 EIGEN_DEVICE_FUNC explicit inline Quaternion(const Quaternion<OtherScalar, OtherOptions>& other) {
363 m_coeffs = other.coeffs().template cast<Scalar>();
364 }
365
366 // We define a copy constructor, which means we don't get an implicit move constructor or assignment operator.
368 EIGEN_DEVICE_FUNC inline Quaternion(Quaternion&& other) noexcept(std::is_nothrow_move_constructible<Scalar>::value)
369 : m_coeffs(std::move(other.coeffs())) {}
370
372 EIGEN_DEVICE_FUNC Quaternion& operator=(Quaternion&& other) noexcept(std::is_nothrow_move_assignable<Scalar>::value) {
373 m_coeffs = std::move(other.coeffs());
374 return *this;
375 }
376
377 EIGEN_DEVICE_FUNC static Quaternion UnitRandom();
378
379 EIGEN_DEVICE_FUNC static Quaternion FromCoeffsScalarLast(const Scalar& x, const Scalar& y, const Scalar& z,
380 const Scalar& w);
381
382 EIGEN_DEVICE_FUNC static Quaternion FromCoeffsScalarFirst(const Scalar& w, const Scalar& x, const Scalar& y,
383 const Scalar& z);
384
385 template <typename Derived1, typename Derived2>
386 EIGEN_DEVICE_FUNC static Quaternion FromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
387
388 template <typename Derived>
389 EIGEN_DEVICE_FUNC static Quaternion FromScaledAxis(const MatrixBase<Derived>& scaled_axis);
390
391 EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs; }
392 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs; }
393
394 EIGEN_DEVICE_FUNC inline Coefficients coeffsScalarLast() const { return m_coeffs; }
395
396 EIGEN_DEVICE_FUNC inline Coefficients coeffsScalarFirst() const {
397 return {m_coeffs.w(), m_coeffs.x(), m_coeffs.y(), m_coeffs.z()};
398 }
399 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(NeedsAlignment))
400
401#ifdef EIGEN_QUATERNION_PLUGIN
402#include EIGEN_QUATERNION_PLUGIN
403#endif
404
405 protected:
406 Coefficients m_coeffs;
407
408#ifndef EIGEN_PARSED_BY_DOXYGEN
409 EIGEN_STATIC_ASSERT((Options_ & DontAlign) == Options_, INVALID_MATRIX_TEMPLATE_PARAMETERS)
410#endif
411};
412
419
420/***************************************************************************
421 * Specialization of Map<Quaternion<Scalar>>
422 ***************************************************************************/
423
424namespace internal {
425template <typename Scalar_, int Options_>
426struct traits<Map<Quaternion<Scalar_>, Options_> >
427 : traits<Quaternion<Scalar_, (int(Options_) & Aligned) == Aligned ? AutoAlign : DontAlign> > {
428 using Coefficients = Map<Matrix<Scalar_, 4, 1>, Options_>;
429};
430} // namespace internal
431
432namespace internal {
433template <typename Scalar_, int Options_>
434struct traits<Map<const Quaternion<Scalar_>, Options_> >
435 : traits<Quaternion<Scalar_, (int(Options_) & Aligned) == Aligned ? AutoAlign : DontAlign> > {
436 using Coefficients = Map<const Matrix<Scalar_, 4, 1>, Options_>;
437 using TraitsBase = traits<Quaternion<Scalar_, (int(Options_) & Aligned) == Aligned ? AutoAlign : DontAlign>>;
438 enum { Flags = TraitsBase::Flags & ~LvalueBit };
439};
440} // namespace internal
441
453template <typename Scalar_, int Options_>
454class Map<const Quaternion<Scalar_>, Options_> : public QuaternionBase<Map<const Quaternion<Scalar_>, Options_> > {
455 public:
456 using Base = QuaternionBase<Map<const Quaternion<Scalar_>, Options_>>;
457
458 using Scalar = Scalar_;
459 using Coefficients = typename internal::traits<Map>::Coefficients;
460 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
461 using Base::operator*=;
462
469 EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Map(const Scalar* coeffs) : m_coeffs(coeffs) {}
470
471 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs; }
472
473 EIGEN_DEVICE_FUNC inline Coefficients coeffsScalarLast() const { return m_coeffs; }
474
475 EIGEN_DEVICE_FUNC inline Coefficients coeffsScalarFirst() const {
476 return {m_coeffs.w(), m_coeffs.x(), m_coeffs.y(), m_coeffs.z()};
477 }
478
479 protected:
480 const Coefficients m_coeffs;
481};
482
494template <typename Scalar_, int Options_>
495class Map<Quaternion<Scalar_>, Options_> : public QuaternionBase<Map<Quaternion<Scalar_>, Options_> > {
496 public:
497 using Base = QuaternionBase<Map<Quaternion<Scalar_>, Options_>>;
498
499 using Scalar = Scalar_;
500 using Coefficients = typename internal::traits<Map>::Coefficients;
501 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
502 using Base::operator*=;
503
510 EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Map(Scalar* coeffs) : m_coeffs(coeffs) {}
511
512 EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs; }
513 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs; }
514
515 EIGEN_DEVICE_FUNC inline Coefficients coeffsScalarLast() const { return m_coeffs; }
516
517 EIGEN_DEVICE_FUNC inline Coefficients coeffsScalarFirst() const {
518 return {m_coeffs.w(), m_coeffs.x(), m_coeffs.y(), m_coeffs.z()};
519 }
520
521 protected:
522 Coefficients m_coeffs;
523};
524
537
538/***************************************************************************
539 * Implementation of QuaternionBase methods
540 ***************************************************************************/
541
542// Generic Quaternion * Quaternion product
543// This product can be specialized for a given architecture via the Arch template argument.
544namespace internal {
545template <int Arch, class Derived1, class Derived2, typename Scalar>
546struct quat_product {
547 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Quaternion<Scalar> run(const QuaternionBase<Derived1>& input_a,
548 const QuaternionBase<Derived2>& input_b) {
549 const Derived1* a_ptr = &input_a.derived();
550 const Derived2* b_ptr = &input_b.derived();
551#if EIGEN_ARCH_ARM && EIGEN_COMP_CLANG
552 EIGEN_IF_CONSTEXPR (std::is_same<Scalar, double>::value) {
553 // Keep operand addresses opaque to LLVM's ARM load/store optimizer (llvm-project#223630).
554 // Register constraints avoid the spills introduced by the general optimization barrier.
555 asm("" : "+r"(a_ptr), "+r"(b_ptr));
556 }
557#endif
558 const Derived1& a = *a_ptr;
559 const Derived2& b = *b_ptr;
560 return Quaternion<Scalar>(a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
561 a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
562 a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
563 a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x());
564 }
565};
566} // namespace internal
567
569template <class Derived>
570template <class OtherDerived>
571EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
572QuaternionBase<Derived>::operator*(const QuaternionBase<OtherDerived>& other) const {
573 EIGEN_STATIC_ASSERT(
574 (std::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
575 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
576 return internal::quat_product<Architecture::Target, Derived, OtherDerived,
577 typename internal::traits<Derived>::Scalar>::run(*this, other);
578}
579
581template <class Derived>
582template <class OtherDerived>
583EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator*=(
584 const QuaternionBase<OtherDerived>& other) {
585 derived() = derived() * other.derived();
586 return derived();
587}
588
596template <class Derived>
597EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename QuaternionBase<Derived>::Vector3
599 // Note that this algorithm comes from the optimization by hand
600 // of the conversion to a Matrix followed by a Matrix/Vector product.
601 // It appears to be much faster than the common algorithm found
602 // in the literature (30 versus 39 flops). It also requires two
603 // Vector3 as temporaries.
604 Vector3 uv = this->vec().cross(v);
605 uv += uv;
606 return v + this->w() * uv + this->vec().cross(uv);
607}
608
609template <class Derived>
610EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE QuaternionBase<Derived>& QuaternionBase<Derived>::operator=(
611 const QuaternionBase<Derived>& other) {
612 coeffs() = other.coeffs();
613 return derived();
614}
615
616template <class Derived>
617template <class OtherDerived>
618EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(
619 const QuaternionBase<OtherDerived>& other) {
620 coeffs() = other.coeffs();
621 return derived();
622}
623
626template <class Derived>
627EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const AngleAxisType& aa) {
628 EIGEN_USING_STD(cos)
629 EIGEN_USING_STD(sin)
630 Scalar ha = Scalar(0.5) * aa.angle(); // Scalar(0.5) to suppress precision loss warnings
631 this->w() = cos(ha);
632 this->vec() = sin(ha) * aa.axis();
633 return derived();
634}
635
641
642template <class Derived>
643template <class MatrixDerived>
644EIGEN_DEVICE_FUNC inline Derived& QuaternionBase<Derived>::operator=(const MatrixBase<MatrixDerived>& xpr) {
645 EIGEN_STATIC_ASSERT(
646 (std::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
647 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
648 internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived());
649 return derived();
650}
651
655template <class Derived>
657 void) const {
658 // Keep this inline: forcing an out-of-line call is much more expensive than returning Matrix3 by value.
659 Matrix3 res;
660
661 const Scalar tx = Scalar(2) * this->x();
662 const Scalar ty = Scalar(2) * this->y();
663 const Scalar tz = Scalar(2) * this->z();
664 const Scalar twx = tx * this->w();
665 const Scalar twy = ty * this->w();
666 const Scalar twz = tz * this->w();
667 const Scalar txx = tx * this->x();
668 const Scalar txy = ty * this->x();
669 const Scalar txz = tz * this->x();
670 const Scalar tyy = ty * this->y();
671 const Scalar tyz = tz * this->y();
672 const Scalar tzz = tz * this->z();
673
674 res.coeffRef(0, 0) = Scalar(1) - (tyy + tzz);
675 res.coeffRef(0, 1) = txy - twz;
676 res.coeffRef(0, 2) = txz + twy;
677 res.coeffRef(1, 0) = txy + twz;
678 res.coeffRef(1, 1) = Scalar(1) - (txx + tzz);
679 res.coeffRef(1, 2) = tyz - twx;
680 res.coeffRef(2, 0) = txz - twy;
681 res.coeffRef(2, 1) = tyz + twx;
682 res.coeffRef(2, 2) = Scalar(1) - (txx + tyy);
683
684 return res;
685}
686
697template <class Derived>
698template <typename Derived1, typename Derived2>
699EIGEN_DEVICE_FUNC inline Derived& QuaternionBase<Derived>::setFromTwoVectors(const MatrixBase<Derived1>& a,
700 const MatrixBase<Derived2>& b) {
701 EIGEN_USING_STD(sqrt)
702 Vector3 v0 = a.normalized();
703 Vector3 v1 = b.normalized();
704 Scalar c = v1.dot(v0);
705
706 // if dot == -1, vectors are nearly opposites
707 // => any axis perpendicular to v0 will do for a ~180 degree rotation.
708 if (c < Scalar(-1) + NumTraits<Scalar>::dummy_precision()) {
709 c = numext::maxi(c, Scalar(-1));
710 Vector3 axis = v0.unitOrthogonal();
711
712 Scalar w2 = (Scalar(1) + c) * Scalar(0.5);
713 this->w() = sqrt(w2);
714 this->vec() = axis * sqrt(Scalar(1) - w2);
715 return derived();
716 }
717 Vector3 axis = v0.cross(v1);
718 Scalar s = sqrt((Scalar(1) + c) * Scalar(2));
719 Scalar invs = Scalar(1) / s;
720 this->vec() = axis * invs;
721 this->w() = s * Scalar(0.5);
722
723 return derived();
724}
725
737template <class Derived>
738template <typename OtherDerived>
739EIGEN_DEVICE_FUNC inline Derived& QuaternionBase<Derived>::setFromScaledAxis(
740 const MatrixBase<OtherDerived>& scaled_axis) {
741 EIGEN_USING_STD(sin)
742 EIGEN_USING_STD(cos)
743 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived, 3)
744
745 // Materialise the input once so the caller's expression (e.g. omega*dt) is not
746 // recomputed by the norm and the subsequent assignment to vec().
747 const Vector3 v = scaled_axis;
748
749 // Fast path with stableNorm() fallback when norm() returns a suspiciously small
750 // value (below epsilon, where squaredNorm() may have underflowed); same idiom
751 // as AngleAxis::operator=(QuaternionBase).
752 Scalar angle = v.norm();
753 if (angle < NumTraits<Scalar>::epsilon()) angle = v.stableNorm();
754 if (numext::is_exactly_zero(angle)) {
755 this->w() = Scalar(1);
756 this->vec().setZero();
757 return derived();
758 }
759
760 const Scalar half_angle = Scalar(0.5) * angle;
761 this->w() = cos(half_angle);
762 this->vec() = (sin(half_angle) / angle) * v;
763 return derived();
764}
765
779template <class Derived>
781 EIGEN_USING_STD(atan2)
782 eigen_assert(internal::isApprox(this->squaredNorm(), Scalar(1), Scalar(8) * NumTraits<Scalar>::dummy_precision()) &&
783 "QuaternionBase::toScaledAxis(): the quaternion must be approximately a unit quaternion");
784
785 // Fast path with stableNorm() fallback; same idiom as AngleAxis::operator=(QuaternionBase).
786 Scalar sin_half_angle = this->vec().norm();
787 if (sin_half_angle < NumTraits<Scalar>::epsilon()) sin_half_angle = this->vec().stableNorm();
788 if (numext::is_exactly_zero(sin_half_angle)) return Vector3::Zero();
789
790 // |w| in atan2 prevents -0.0 from flipping the sign; recover it via the comparison below
791 // (false for both +0.0 and -0.0).
792 // Cannot overflow: for unit q, atan2(s, |c|) is O(s) as s -> 0, so k stays in [2, pi].
793 Scalar k = Scalar(2) * atan2(sin_half_angle, numext::abs(this->w())) / sin_half_angle;
794 if (this->w() < Scalar(0)) k = -k;
795 return k * this->vec();
796}
797
802template <typename Scalar, int Options>
804 EIGEN_USING_STD(sqrt)
805 EIGEN_USING_STD(sin)
806 EIGEN_USING_STD(cos)
807 const Scalar u1 = internal::random<Scalar>(0, 1), u2 = internal::random<Scalar>(0, 2 * EIGEN_PI),
808 u3 = internal::random<Scalar>(0, 2 * EIGEN_PI);
809 const Scalar a = sqrt(Scalar(1) - u1), b = sqrt(u1);
810 return Quaternion(a * sin(u2), a * cos(u2), b * sin(u3), b * cos(u3));
811}
812
819template <typename Scalar, int Options>
821 const Scalar& y,
822 const Scalar& z,
823 const Scalar& w) {
824 return Quaternion(w, x, y, z);
825}
826
834template <typename Scalar, int Options>
836 const Scalar& x,
837 const Scalar& y,
838 const Scalar& z) {
839 return Quaternion(w, x, y, z);
840}
841
852template <typename Scalar, int Options>
853template <typename Derived1, typename Derived2>
854EIGEN_DEVICE_FUNC Quaternion<Scalar, Options> Quaternion<Scalar, Options>::FromTwoVectors(
855 const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b) {
856 Quaternion quat;
857 quat.setFromTwoVectors(a, b);
858 return quat;
859}
860
867template <typename Scalar, int Options>
868template <typename Derived>
869EIGEN_DEVICE_FUNC Quaternion<Scalar, Options> Quaternion<Scalar, Options>::FromScaledAxis(
870 const MatrixBase<Derived>& scaled_axis) {
871 Quaternion quat;
872 quat.setFromScaledAxis(scaled_axis);
873 return quat;
874}
875
882template <class Derived>
884 const {
885 // FIXME: consider renaming to multiplicativeInverse() and renaming conjugate() to inverse() or opposite().
886 Scalar n2 = this->squaredNorm();
887 if (n2 > Scalar(0))
888 return Quaternion<Scalar>(conjugate().coeffs() / n2);
889 else {
890 // return an invalid result to flag the error
891 return Quaternion<Scalar>(Coefficients::Zero());
892 }
893}
894
895// Generic conjugate of a Quaternion
896namespace internal {
897template <int Arch, class Derived, typename Scalar>
898struct quat_conj {
899 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Quaternion<Scalar> run(const QuaternionBase<Derived>& q) {
900 return Quaternion<Scalar>(q.w(), -q.x(), -q.y(), -q.z());
901 }
902};
903} // namespace internal
904
911template <class Derived>
913 const {
914 return internal::quat_conj<Architecture::Target, Derived, typename internal::traits<Derived>::Scalar>::run(*this);
915}
916
920template <class Derived>
921template <class OtherDerived>
922EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Scalar QuaternionBase<Derived>::angularDistance(
923 const QuaternionBase<OtherDerived>& other) const {
924 EIGEN_USING_STD(atan2)
925 Quaternion<Scalar> d = (*this) * other.conjugate();
926 return Scalar(2) * atan2(d.vec().norm(), numext::abs(d.w()));
927}
928
935template <class Derived>
936template <class OtherDerived>
937EIGEN_DEVICE_FUNC Quaternion<typename internal::traits<Derived>::Scalar> QuaternionBase<Derived>::slerp(
938 const Scalar& t, const QuaternionBase<OtherDerived>& other) const {
939 EIGEN_USING_STD(acos)
940 EIGEN_USING_STD(sin)
941 const Scalar one = Scalar(1) - NumTraits<Scalar>::epsilon();
942 Scalar d = this->dot(other);
943 Scalar absD = numext::abs(d);
944
945 Scalar scale0;
946 Scalar scale1;
947
948 if (absD >= one) {
949 // Near-parallel quaternions: use lerp to avoid division by ~zero sinTheta.
950 scale0 = Scalar(1) - t;
951 scale1 = t;
952 } else {
953 // theta is the angle between the 2 quaternions
954 Scalar theta = acos(absD);
955 Scalar sinTheta = numext::sqrt(Scalar(1) - absD * absD);
956
957 scale0 = sin((Scalar(1) - t) * theta) / sinTheta;
958 scale1 = sin(t * theta) / sinTheta;
959 }
960 if (d < Scalar(0)) scale1 = -scale1;
961
962 return Quaternion<Scalar>(scale0 * coeffs() + scale1 * other.coeffs());
963}
964
965namespace internal {
966
967// set from a rotation matrix
968template <typename Other>
969struct quaternionbase_assign_impl<Other, 3, 3> {
970 using Scalar = typename Other::Scalar;
971
972 template <bool Normalize>
973 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar compute_scale(Scalar& t) {
974 if (!Normalize) return Scalar(1);
975 t = numext::sqrt(t);
976 Scalar s = Scalar(0.5) / t;
977 t *= Scalar(0.5);
978 return s;
979 }
980
981 template <int AxisI, int AxisJ, int AxisK, bool Normalize, class Derived, class Mat>
982 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE void assign_branch(QuaternionBase<Derived>& q, const Mat& mat,
983 Scalar mii, Scalar mjj, Scalar mkk) {
984 // Guard against slightly negative argument from non-orthogonal matrices.
985 Scalar t = numext::maxi(mii - mjj - mkk + Scalar(1.0), Scalar(0));
986 const Scalar s = compute_scale<Normalize>(t);
987 q.coeffs().coeffRef(AxisI) = t;
988 q.w() = (mat.coeff(AxisK, AxisJ) - mat.coeff(AxisJ, AxisK)) * s;
989 q.coeffs().coeffRef(AxisJ) = (mat.coeff(AxisJ, AxisI) + mat.coeff(AxisI, AxisJ)) * s;
990 q.coeffs().coeffRef(AxisK) = (mat.coeff(AxisK, AxisI) + mat.coeff(AxisI, AxisK)) * s;
991 }
992
993 template <bool Normalize = true, class Derived>
994 EIGEN_DEVICE_FUNC static inline void run(QuaternionBase<Derived>& q, const Other& a_mat) {
995 const typename internal::nested_eval<Other, 2>::type mat(a_mat);
996 // This algorithm comes from "Quaternion Calculus and Fast Animation",
997 // Ken Shoemake, 1987 SIGGRAPH course notes
998 const Scalar m00 = mat.coeff(0, 0);
999 const Scalar m11 = mat.coeff(1, 1);
1000 const Scalar m22 = mat.coeff(2, 2);
1001 Scalar t = m00 + (m11 + m22);
1002 if (t > Scalar(0)) {
1003 t = numext::maxi(t + Scalar(1.0), Scalar(0));
1004 const Scalar s = compute_scale<Normalize>(t);
1005 q.w() = t;
1006 q.x() = (mat.coeff(2, 1) - mat.coeff(1, 2)) * s;
1007 q.y() = (mat.coeff(0, 2) - mat.coeff(2, 0)) * s;
1008 q.z() = (mat.coeff(1, 0) - mat.coeff(0, 1)) * s;
1009 } else if (m00 >= m11 && m00 >= m22) {
1010 assign_branch<0, 1, 2, Normalize>(q, mat, m00, m11, m22);
1011 } else if (m11 >= m22) {
1012 assign_branch<1, 2, 0, Normalize>(q, mat, m11, m22, m00);
1013 } else {
1014 assign_branch<2, 0, 1, Normalize>(q, mat, m22, m00, m11);
1015 }
1016 }
1017};
1018
1019// set from a vector of coefficients assumed to be a quaternion
1020template <typename Other>
1021struct quaternionbase_assign_impl<Other, 4, 1> {
1022 using Scalar = typename Other::Scalar;
1023 template <class Derived>
1024 EIGEN_DEVICE_FUNC static inline void run(QuaternionBase<Derived>& q, const Other& vec) {
1025 q.coeffs() = vec;
1026 }
1027};
1028
1029} // end namespace internal
1030
1031} // end namespace Eigen
1032
1033#endif // EIGEN_QUATERNION_H
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition AngleAxis.h:53
const Vector3 & axis() const
Definition AngleAxis.h:100
Scalar angle() const
Definition AngleAxis.h:95
const std::enable_if_t< std::is_same< typename LhsDerived::Scalar, typename RhsDerived::Scalar >::value, Eigen::CwiseBinaryOp< Eigen::internal::scalar_atan2_op< typename LhsDerived::Scalar, typename RhsDerived::Scalar >, const LhsDerived, const RhsDerived > > atan2(const Eigen::ArrayBase< LhsDerived > &x, const Eigen::ArrayBase< RhsDerived > &exponents)
Definition GlobalFunctions.h:222
Map(Scalar *coeffs)
Definition Quaternion.h:510
Map(const Scalar *coeffs)
Definition Quaternion.h:469
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
const PlainObject normalized() const
Definition Dot.h:339
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:205
Base class for quaternion expressions.
Definition Quaternion.h:36
internal::traits< Derived >::Coefficients coeffsScalarLast() const
Definition Quaternion.h:107
constexpr CoeffReturnType z() const
Definition Quaternion.h:64
Scalar squaredNorm() const
Definition Quaternion.h:139
QuaternionBase & setIdentity()
Definition Quaternion.h:131
Quaternion< Scalar > normalized() const
Definition Quaternion.h:151
internal::traits< Derived >::Coefficients & coeffs()
Definition Quaternion.h:112
constexpr CoeffReturnType x() const
Definition Quaternion.h:60
internal::cast_return_type< Derived, Quaternion< NewScalarType > >::type cast() const
constexpr NonConstCoeffReturnType z()
Definition Quaternion.h:73
VectorBlock< Coefficients, 3 > vec()
Definition Quaternion.h:81
const VectorBlock< const Coefficients, 3 > vec() const
Definition Quaternion.h:78
constexpr NonConstCoeffReturnType w()
Definition Quaternion.h:75
static Quaternion< Scalar > Identity()
Definition Quaternion.h:125
Matrix< Scalar, 3, 1 > Vector3
Definition Quaternion.h:53
Matrix< Scalar, 3, 3 > Matrix3
Definition Quaternion.h:55
bool operator!=(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:206
Quaternion< Scalar > conjugate() const
Definition Quaternion.h:912
bool isApprox(const QuaternionBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Quaternion.h:215
Vector3 toScaledAxis() const
Definition Quaternion.h:780
void normalize()
Definition Quaternion.h:148
Derived & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)
Definition Quaternion.h:699
Matrix3 toRotationMatrix() const
Definition Quaternion.h:656
constexpr CoeffReturnType y() const
Definition Quaternion.h:62
Scalar dot(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:159
Derived & operator=(const AngleAxisType &aa)
Definition Quaternion.h:627
Vector3 _transformVector(const Vector3 &v) const
Definition Quaternion.h:598
Scalar norm() const
Definition Quaternion.h:144
Quaternion< Scalar > inverse() const
Definition Quaternion.h:883
AngleAxis< Scalar > AngleAxisType
Definition Quaternion.h:57
const internal::traits< Derived >::Coefficients & coeffs() const
Definition Quaternion.h:84
bool operator==(const QuaternionBase< OtherDerived > &other) const
Definition Quaternion.h:197
internal::traits< Derived >::Coefficients coeffsScalarFirst() const
Definition Quaternion.h:96
constexpr CoeffReturnType w() const
Definition Quaternion.h:66
Derived & setFromScaledAxis(const MatrixBase< OtherDerived > &scaled_axis)
Definition Quaternion.h:739
constexpr NonConstCoeffReturnType x()
Definition Quaternion.h:69
constexpr NonConstCoeffReturnType y()
Definition Quaternion.h:71
Derived & operator*=(const QuaternionBase< OtherDerived > &q)
Definition Quaternion.h:583
The quaternion class used to represent 3D orientations and rotations.
Definition Quaternion.h:304
Quaternion(Quaternion &&other) noexcept(std::is_nothrow_move_constructible< Scalar >::value)
Definition Quaternion.h:368
Quaternion(const Quaternion< OtherScalar, OtherOptions > &other)
Definition Quaternion.h:362
static Quaternion FromCoeffsScalarLast(const Scalar &x, const Scalar &y, const Scalar &z, const Scalar &w)
Definition Quaternion.h:820
Quaternion(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition Quaternion.h:327
Quaternion(const QuaternionBase< Derived > &other)
Definition Quaternion.h:344
Quaternion(const Scalar &w, const Eigen::MatrixBase< Derived > &vec)
Definition Quaternion.h:334
static Quaternion UnitRandom()
Definition Quaternion.h:803
Quaternion(const MatrixBase< Derived > &other)
Definition Quaternion.h:356
Quaternion(const AngleAxisType &aa)
Definition Quaternion.h:349
Quaternion()
Definition Quaternion.h:318
static Quaternion FromCoeffsScalarFirst(const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z)
Definition Quaternion.h:835
Quaternion & operator=(Quaternion &&other) noexcept(std::is_nothrow_move_assignable< Scalar >::value)
Definition Quaternion.h:372
Quaternion(const Scalar *data)
Definition Quaternion.h:340
Common base class for compact rotation representations.
Definition RotationBase.h:33
Expression of a fixed-size or dynamic-size sub-vector.
Definition VectorBlock.h:59
Map< Quaternion< double >, 0 > QuaternionMapd
Definition Quaternion.h:530
Map< Quaternion< float >, Aligned > QuaternionMapAlignedf
Definition Quaternion.h:533
Map< Quaternion< float >, 0 > QuaternionMapf
Definition Quaternion.h:527
Quaternion< float > Quaternionf
Definition Quaternion.h:415
Quaternion< double > Quaterniond
Definition Quaternion.h:418
Map< Quaternion< double >, Aligned > QuaternionMapAlignedd
Definition Quaternion.h:536
@ Aligned
Definition Constants.h:243
@ DontAlign
Definition Constants.h:325
@ AutoAlign
Definition Constants.h:323
constexpr unsigned int LvalueBit
Definition Constants.h:149