SA_Maths
Sapphire Suite's C++ Maths Library
Loading...
Searching...
No Matches
Quaternion.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire development team. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_MATHS_QUATERNION_GUARD
6#define SAPPHIRE_MATHS_QUATERNION_GUARD
7
8#include <cstdint>
9
10#include <SA/Maths/Debug.hpp>
11#include <SA/Maths/Config.hpp>
12
20
21#if SA_MATHS_QUATERNION_SIMD
22
23 #include <SA/Support/Intrinsics.hpp>
24
25#endif
26
37namespace SA
38{
39 template <typename T>
40 struct Vec3;
41
51 template <typename T>
52 struct alignas(sizeof(T) * 4) Quat
53 {
55 using Type = T;
56
57
59 T w = T(1);
60
62 T x = T();
63
65 T y = T();
66
68 T z = T();
69
70//{ Constants
71
73 static const Quat Zero;
74
76 static const Quat Identity;
77
78//}
79
80//{ Constructors
81
83 Quat() = default;
84
93 constexpr Quat(T _w, T _x, T _y, T _z) noexcept;
94
95
97 Quat(Quat&&) = default;
98
100 Quat(const Quat&) = default;
101
109 template <typename TIn>
110 constexpr Quat(const Quat<TIn>& _other) noexcept;
111
112
119 Quat(Deg<T> _angle, const Vec3<T>& _axis) noexcept;
120
121//}
122
123//{ Equals
124
130 constexpr bool IsZero() const noexcept;
131
137 constexpr bool IsIdentity() const noexcept;
138
147 constexpr bool Equals(const Quat& _other, T _threshold = std::numeric_limits<T>::epsilon()) const noexcept;
148
149
157 constexpr bool operator==(const Quat<T>& _rhs) const noexcept;
158
166 constexpr bool operator!=(const Quat<T>& _rhs) const noexcept;
167
168//}
169
170//{ Accessors
171
177 T* Data() noexcept;
178
184 const T* Data() const noexcept;
185
186
194 T& operator[](uint32_t _index);
195
203 T operator[](uint32_t _index) const;
204
205//}
206
207//{ Length
208
214 T Length() const;
215
221 T SqrLength() const noexcept;
222
223
230
237
243 bool IsNormalized() const noexcept;
244
245//}
246
247//{ Inverse
248
255 Quat& Inverse() noexcept;
256
263 constexpr Quat GetInversed() const noexcept;
264
265//}
266
267//{ Angle/Axis
268
276 Vec3<T>& ImgAxis() noexcept;
277
285 const Vec3<T>& ImgAxis() const noexcept;
286
292 constexpr Deg<T> GetAngle() const noexcept;
293
299 constexpr Vec3<T> GetAxis() const noexcept;
300
301//}
302
303//{ Rotate
304
315 Quat<T> Rotate(const Quat<T>& _other) const noexcept;
316
327 constexpr Vec3<T> Rotate(const Vec3<T>& _vec) const noexcept;
328
339 Quat<T> UnRotate(const Quat<T>& _other) const noexcept;
340
351 constexpr Vec3<T> UnRotate(const Vec3<T>& _vec) const noexcept;
352
358 constexpr Vec3<T> RightVector() const noexcept;
359
365 constexpr Vec3<T> UpVector() const noexcept;
366
372 constexpr Vec3<T> ForwardVector() const noexcept;
373
374//}
375
376//{ Euler
377
388 Vec3<Deg<T>> ToEuler() const noexcept;
389
402 static Quat FromEuler(const Vec3<Deg<T>>& _angles) noexcept;
403
404//}
405
406//{ Dot
407
416 static T Dot(const Quat& _lhs, const Quat& _rhs) noexcept;
417
418//}
419
420//{ Lerp
421
435 static Quat Lerp(const Quat& _start, const Quat& _end, float _alpha);
436
450 static Quat LerpUnclamped(const Quat& _start, const Quat& _end, float _alpha);
451
465 static Quat SLerp(const Quat& _start, const Quat& _end, float _alpha);
466
480 static Quat SLerpUnclamped(const Quat& _start, const Quat& _end, float _alpha);
481
482//}
483
484//{ Operators
485
491 Quat& operator=(Quat&&) = default;
492
498 Quat& operator=(const Quat&) = default;
499
500
506 constexpr Quat operator-() const noexcept;
507
515 Quat operator*(T _scale) const noexcept;
516
524 Quat operator/(T _scale) const;
525
533 Quat operator+(const Quat& _rhs) const noexcept;
534
542 Quat operator-(const Quat& _rhs) const noexcept;
543
554 Quat operator*(const Quat& _rhs) const noexcept;
555
566 constexpr Vec3<T> operator*(const Vec3<T>& _rhs) const noexcept;
567
578 Quat operator/(const Quat& _rhs) const noexcept;
579
590 constexpr Vec3<T> operator/(const Vec3<T>& _rhs) const noexcept;
591
599 constexpr T operator|(const Quat& _rhs) const noexcept;
600
609 Quat& operator*=(T _scale) noexcept;
610
619 Quat& operator/=(T _scale);
620
628 Quat& operator+=(const Quat& _rhs) noexcept;
629
637 Quat& operator-=(const Quat& _rhs) noexcept;
638
648 Quat& operator*=(const Quat& _rhs) noexcept;
649
659 Quat& operator/=(const Quat& _rhs) noexcept;
660
661//}
662
663
664#if SA_LOGGING
665
673 std::string ToString() const noexcept;
674
675#endif
676 };
677
678
687 template <typename T>
688 constexpr Quat<T> operator*(typename std::remove_cv<T>::type _lhs, const Quat<T>& _rhs) noexcept;
689
698 template <typename T>
699 Quat<T> operator/(typename std::remove_cv<T>::type _lhs, const Quat<T>& _rhs);
700
712 template <typename T>
713 constexpr Vec3<T> operator*(const Vec3<T>& _lhs, const Quat<T>& _rhs) noexcept;
714
726 template <typename T>
727 constexpr Vec3<T> operator/(const Vec3<T>& _lhs, const Quat<T>& _rhs) noexcept;
728
729
730#if SA_LOGGER_IMPL
731
743 template <typename T>
744 std::string ToString(const Quat<T>& _q);
745
746#endif
747
748
749//{ Aliases
750
753
756
757
759 template <typename T>
761
764
767
768//}
769
770
772
773
774#if SA_MATHS_QUATERNION_SIMD && SA_INTRISC_SSE // SIMD float
775
776 template <>
777 float Quatf::SqrLength() const noexcept;
778
779 template <>
780 Quatf& Quatf::Normalize();
781
782 template <>
783 Quatf Quatf::GetNormalized() const;
784
785
786 template <>
787 Quatf Quatf::Rotate(const Quatf& _other) const noexcept;
788
789
790 template <>
791 float Quatf::Dot(const Quatf& _lhs, const Quatf& _rhs) noexcept;
792
793 template <>
794 Vec3<Degf> Quatf::ToEuler() const noexcept;
795
796 template <>
797 Quatf Quatf::FromEuler(const Vec3<Degf>& _angles) noexcept;
798
799
800 template <>
801 Quatf Quatf::operator*(float _scale) const noexcept;
802
803 template <>
804 Quatf Quatf::operator/(float _scale) const;
805
806 template <>
807 Quatf Quatf::operator+(const Quatf& _rhs) const noexcept;
808
809 template <>
810 Quatf Quatf::operator-(const Quatf& _rhs) const noexcept;
811
812
813 template <>
814 Quatf& Quatf::operator*=(float _scale) noexcept;
815
816 template <>
817 Quatf& Quatf::operator/=(float _scale);
818
819 template <>
820 Quatf& Quatf::operator+=(const Quatf& _rhs) noexcept;
821
822 template <>
823 Quatf& Quatf::operator-=(const Quatf& _rhs) noexcept;
824
825
826 template <>
827 Quatf operator/(float _lhs, const Quatf& _rhs);
828
829#endif
830
831#if SA_MATHS_QUATERNION_SIMD && SA_INTRISC_AVX // SIMD double
832
833 template <>
834 double Quatd::SqrLength() const noexcept;
835
836 template <>
837 Quatd& Quatd::Normalize();
838
839 template <>
840 Quatd Quatd::GetNormalized() const;
841
842
843 template <>
844 Quatd Quatd::Rotate(const Quatd& _other) const noexcept;
845
846
847 template <>
848 double Quatd::Dot(const Quatd& _lhs, const Quatd& _rhs) noexcept;
849
850
851 template <>
852 Vec3<Degd> Quatd::ToEuler() const noexcept;
853
854 template <>
855 Quatd Quatd::FromEuler(const Vec3<Degd>& _angles) noexcept;
856
857
858 template <>
859 Quatd Quatd::operator*(double _scale) const noexcept;
860
861 template <>
862 Quatd Quatd::operator/(double _scale) const;
863
864 template <>
865 Quatd Quatd::operator+(const Quatd& _rhs) const noexcept;
866
867 template <>
868 Quatd Quatd::operator-(const Quatd& _rhs) const noexcept;
869
870
871 template <>
872 Quatd& Quatd::operator*=(double _scale) noexcept;
873
874 template <>
875 Quatd& Quatd::operator/=(double _scale);
876
877 template <>
878 Quatd& Quatd::operator+=(const Quatd& _rhs) noexcept;
879
880 template <>
881 Quatd& Quatd::operator-=(const Quatd& _rhs) noexcept;
882
883
884 template <>
885 Quatd operator/(double _lhs, const Quatd& _rhs);
886
887#endif
888
890}
891
900#include <SA/Maths/Space/Quaternion.inl>
901
902#endif // GUARD
Maths specific config file.
Cosinus method implementation.
Maths module Debug compatibility definition.
Degree type implementation.
Equals method implementation.
Lerp algorithms implementation.
Mat3< T, major > operator*(TIn _lhs, const Mat3< T, major > &_rhs) noexcept
Scale each matrix components by _lhs.
Mat3< T, major > operator/(TIn _lhs, const Mat3< T, major > &_rhs)
Inverse Scale each matrix components by _lhs.
Sinus method implementation.
Square Root algorithm implementation.
Tangent method implementation.
Maths Degree type.
Definition Degree.hpp:37
Quaternion Sapphire-Maths class.
Definition Quaternion.hpp:53
constexpr Quat GetInversed() const noexcept
Inverse this quaternion. Quaternion should be normalized.
Quat(Deg< T > _angle, const Vec3< T > &_axis) noexcept
Value constructor angle and axis.
constexpr Vec3< T > UpVector() const noexcept
Getter of Up vector (Y axis) rotated by this quaternion.
constexpr bool IsZero() const noexcept
Whether this quaternion is a zero quaternion.
static Quat SLerpUnclamped(const Quat &_start, const Quat &_end, float _alpha)
Unclamped SLerp from _start to _end at _alpha.
Vec3< Deg< T > > ToEuler() const noexcept
Convert this quaternion into euler angles in degrees.
Quat & Inverse() noexcept
Inverse this quaternion. Quaternion should be normalized.
static Quat LerpUnclamped(const Quat &_start, const Quat &_end, float _alpha)
Unclamped Lerp from _start to _end at _alpha.
constexpr Vec3< T > ForwardVector() const noexcept
Getter of Forward vector (Z axis) rotated by this quaternion.
Quat< T > UnRotate(const Quat< T > &_other) const noexcept
Un-Rotate _other quaternion by this quaternion.
Quat(Quat &&)=default
Default move constructor.
Quat & Normalize()
Normalize this quaternion.
T * Data() noexcept
Getter of quaternion data
constexpr bool Equals(const Quat &_other, T _threshold=std::numeric_limits< T >::epsilon()) const noexcept
Compare 2 quaternion.
constexpr Quat(T _w, T _x, T _y, T _z) noexcept
Value constructor.
static Quat SLerp(const Quat &_start, const Quat &_end, float _alpha)
Clamped SLerp from _start to _end at _alpha.
static Quat FromEuler(const Vec3< Deg< T > > &_angles) noexcept
Create quaternion from euler angles in degrees.
constexpr bool IsIdentity() const noexcept
Whether this quaternion is an identity quaternion.
T SqrLength() const noexcept
Getter of the Squared Length of this quaternion.
static const Quat Identity
Identity quaternion constant { 1, 0, 0, 0 }.
Definition Quaternion.hpp:76
T y
Quaternion's Y component (j axis).
Definition Quaternion.hpp:65
static T Dot(const Quat &_lhs, const Quat &_rhs) noexcept
Compute the Dot product between _lhs and _rhs.
T w
Quaternion's W component (Real value).
Definition Quaternion.hpp:59
constexpr Vec3< T > RightVector() const noexcept
Getter of Right vector (X axis) rotated by this quaternion.
T Length() const
Getter of the length of this quaternion.
T Type
Type of the Quaternion.
Definition Quaternion.hpp:55
static Quat Lerp(const Quat &_start, const Quat &_end, float _alpha)
Clamped Lerp from _start to _end at _alpha.
Quat< T > Rotate(const Quat< T > &_other) const noexcept
Rotate _other quaternion by this quaternion.
constexpr Deg< T > GetAngle() const noexcept
Getter of the angle handled by this quaternion.
static const Quat Zero
Zero quaternion constant { 0, 0, 0, 0 }.
Definition Quaternion.hpp:73
T x
Quaternion's X component (i axis).
Definition Quaternion.hpp:62
Vec3< T > & ImgAxis() noexcept
Getter of quaternion's imaginary axis.
bool IsNormalized() const noexcept
Whether this quaternion is normalized.
Quat()=default
Default constructor.
constexpr Quat(const Quat< TIn > &_other) noexcept
Value constructor from another quaternion type.
Quat(const Quat &)=default
Default copy constructor.
T z
Quaternion's Z component (k axis).
Definition Quaternion.hpp:68
constexpr Vec3< T > GetAxis() const noexcept
Getter of the axis handled by this quaternion.
Quat GetNormalized() const
Normalize this quaternion.
Vector 3 Sapphire-Maths class.
Definition Vector3.hpp:43