Plasma Engine  2.0
Loading...
Searching...
No Matches
Quat.h
1#pragma once
2
3#include <Foundation/Math/Vec3.h>
4
17template <typename Type>
19{
20public:
21 // Means this object can be copied using memcpy instead of copy construction.
22 PL_DECLARE_POD_TYPE();
23
24 using ComponentType = Type;
25
26 // *** Data ***
27public:
28 Type x;
29 Type y;
30 Type z;
31 Type w;
32
33 // *** Constructors ***
34public:
35 plQuatTemplate(); // [tested]
36
38 plQuatTemplate(Type x, Type y, Type z, Type w); // [tested]
39
40#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
41 void AssertNotNaN() const
42 {
43 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please check that "
44 "all code-paths properly initialize this object.");
45 }
46#endif
47
49 [[nodiscard]] static const plQuatTemplate<Type> MakeIdentity(); // [tested]
50
51 // *** Functions to create a quaternion ***
52public:
54 void SetIdentity(); // [tested]
55
60 [[nodiscard]] static plQuatTemplate<Type> MakeFromElements(Type x, Type y, Type z, Type w); // [tested]
61
63 [[nodiscard]] static plQuatTemplate<Type> MakeFromAxisAndAngle(const plVec3Template<Type>& vRotationAxis, plAngle angle); // [tested]
64
66 [[nodiscard]] static plQuatTemplate<Type> MakeShortestRotation(const plVec3Template<Type>& vDirFrom, const plVec3Template<Type>& vDirTo); // [tested]
67
69 [[nodiscard]] static plQuatTemplate<Type> MakeFromMat3(const plMat3Template<Type>& m); // [tested]
70
78
83
85 [[nodiscard]] static plQuatTemplate<Type> MakeSlerp(const plQuatTemplate& qFrom, const plQuatTemplate& qTo, Type t); // [tested]
86
87 // *** Common Functions ***
88public:
90 void Normalize(); // [tested]
91
93 const Type* GetData() const { return &x; }
94
96 Type* GetData() { return &x; }
97
99 void GetRotationAxisAndAngle(plVec3Template<Type>& out_vAxis, plAngle& out_angle, Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
100
103
105 const plMat3Template<Type> GetAsMat3() const; // [tested]
106
108 const plMat4Template<Type> GetAsMat4() const; // [tested]
109
111 bool IsValid(Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
112
114 bool IsNaN() const; // [tested]
115
121 bool IsEqualRotation(const plQuatTemplate& qOther, Type fEpsilon) const; // [tested]
122
126 void Invert();
127
129 const plQuatTemplate<Type> GetInverse() const; // [tested]
130
132 const plQuatTemplate<Type> GetNegated() const;
133
135 Type Dot(const plQuatTemplate& rhs) const; // [tested]
136
139
140 // *** Euler Angle Conversions ***
141public:
143 void GetAsEulerAngles(plAngle& out_x, plAngle& out_y, plAngle& out_z) const; // [tested]
144
146 [[nodiscard]] static plQuatTemplate<Type> MakeFromEulerAngles(const plAngle& x, const plAngle& y, const plAngle& z); // [tested]
147};
148
150template <typename Type>
151const plVec3Template<Type> operator*(const plQuatTemplate<Type>& q, const plVec3Template<Type>& v); // [tested]
152
154template <typename Type>
155const plQuatTemplate<Type> operator*(const plQuatTemplate<Type>& q1, const plQuatTemplate<Type>& q2); // [tested]
156
157template <typename Type>
158bool operator==(const plQuatTemplate<Type>& q1, const plQuatTemplate<Type>& q2); // [tested]
159
160template <typename Type>
161bool operator!=(const plQuatTemplate<Type>& q1, const plQuatTemplate<Type>& q2); // [tested]
162
163#include <Foundation/Math/Implementation/Quat_inl.h>
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
A 3x3 component matrix class.
Definition Mat3.h:9
A 4x4 component matrix class.
Definition Mat4.h:11
Quaternions can be used to represent rotations in 3D space.
Definition Quat.h:19
const Type * GetData() const
Returns the data as an array.
Definition Quat.h:93
void ReconstructFromMat4(const plMat4Template< Type > &m)
Reconstructs a rotation quaternion from a matrix that may contain scaling and mirroring.
Definition Quat_inl.h:342
void SetIdentity()
Sets the Quaternion to the identity.
Definition Quat_inl.h:41
plVec3Template< Type > GetVectorPart() const
Returns the x,y,z components as a vector.
Definition Quat.h:102
static plQuatTemplate< Type > MakeSlerp(const plQuatTemplate &qFrom, const plQuatTemplate &qTo, Type t)
Returns a quaternion that is the spherical linear interpolation of the other two.
Definition Quat_inl.h:403
static plQuatTemplate< Type > MakeFromMat3(const plMat3Template< Type > &m)
Creates a quaternion from the given matrix.
Definition Quat_inl.h:275
const plQuatTemplate< Type > GetInverse() const
Returns a quaternion that represents the negative / inverted rotation. E.g. the one that would rotate...
Definition Quat_inl.h:106
const plQuatTemplate< Type > GetNegated() const
Returns the Quaternion with all 4 components negated. This is not the same as the inverted rotation!
Definition Quat_inl.h:114
Type * GetData()
Returns the data as an array.
Definition Quat.h:96
void Normalize()
Normalizes the quaternion to unit length. ALL rotation-quaternions should be normalized at all times ...
Definition Quat_inl.h:61
bool IsEqualRotation(const plQuatTemplate &qOther, Type fEpsilon) const
Determines whether this and qOther represent the same rotation. This is a rather slow operation.
Definition Quat_inl.h:181
static plQuatTemplate< Type > MakeFromElements(Type x, Type y, Type z, Type w)
Sets the individual elements of the quaternion directly. Note that x,y,z do NOT represent a rotation ...
Definition Quat_inl.h:35
void GetRotationAxisAndAngle(plVec3Template< Type > &out_vAxis, plAngle &out_angle, Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Returns the rotation-axis and angle, that this quaternion rotates around.
Definition Quat_inl.h:76
static plQuatTemplate< Type > MakeFromEulerAngles(const plAngle &x, const plAngle &y, const plAngle &z)
Sets the quaternion from Euler angles.
Definition Quat_inl.h:520
plVec3Template< Type > Rotate(const plVec3Template< Type > &v) const
Returns v rotated by the quaternion. Same as operator*.
Definition Quat_inl.h:131
bool IsNaN() const
Checks whether any component is NaN.
Definition Quat_inl.h:175
const plMat4Template< Type > GetAsMat4() const
Returns the Quaternion as a matrix.
Definition Quat_inl.h:236
static const plQuatTemplate< Type > MakeIdentity()
Static function that returns a quaternion that represents the identity rotation (none).
Definition Quat_inl.h:29
void ReconstructFromMat3(const plMat3Template< Type > &m)
Reconstructs a rotation quaternion from a matrix that may contain scaling and mirroring.
Definition Quat_inl.h:327
static plQuatTemplate< Type > MakeShortestRotation(const plVec3Template< Type > &vDirFrom, const plVec3Template< Type > &vDirTo)
Creates a quaternion, that rotates through the shortest arc from "vDirFrom" to "vDirTo".
Definition Quat_inl.h:363
const plMat3Template< Type > GetAsMat3() const
Returns the Quaternion as a matrix.
Definition Quat_inl.h:204
static plQuatTemplate< Type > MakeFromAxisAndAngle(const plVec3Template< Type > &vRotationAxis, plAngle angle)
Creates a quaternion from a rotation-axis and an angle.
Definition Quat_inl.h:50
Type Dot(const plQuatTemplate &rhs) const
Returns the dot-product of the two quaternions (commutative, order does not matter).
Definition Quat_inl.h:122
void GetAsEulerAngles(plAngle &out_x, plAngle &out_y, plAngle &out_z) const
Converts the quaternion to Euler angles.
Definition Quat_inl.h:477
bool IsValid(Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether all components are neither NaN nor infinite and that the quaternion is normalized.
Definition Quat_inl.h:162
void Invert()
Inverts the rotation, so instead of rotating N degrees around an axis, the quaternion will rotate -N ...
Definition Quat_inl.h:98
A 3-component vector class.
Definition Vec3.h:9