Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Math/Quat.h
9913 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Math/Vec3.h>
8
#include <Jolt/Math/Vec4.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// Quaternion class, quaternions are 4 dimensional vectors which can describe rotations in 3 dimensional
13
/// space if their length is 1.
14
///
15
/// They are written as:
16
///
17
/// \f$q = w + x \: i + y \: j + z \: k\f$
18
///
19
/// or in vector notation:
20
///
21
/// \f$q = [w, v] = [w, x, y, z]\f$
22
///
23
/// Where:
24
///
25
/// w = the real part
26
/// v = the imaginary part, (x, y, z)
27
///
28
/// Note that we store the quaternion in a Vec4 as [x, y, z, w] because that makes
29
/// it easy to extract the rotation axis of the quaternion:
30
///
31
/// q = [cos(angle / 2), sin(angle / 2) * rotation_axis]
32
class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Quat
33
{
34
public:
35
JPH_OVERRIDE_NEW_DELETE
36
37
///@name Constructors
38
///@{
39
inline Quat() = default; ///< Intentionally not initialized for performance reasons
40
Quat(const Quat &inRHS) = default;
41
Quat & operator = (const Quat &inRHS) = default;
42
inline Quat(float inX, float inY, float inZ, float inW) : mValue(inX, inY, inZ, inW) { }
43
inline explicit Quat(Vec4Arg inV) : mValue(inV) { }
44
///@}
45
46
///@name Tests
47
///@{
48
49
/// Check if two quaternions are exactly equal
50
inline bool operator == (QuatArg inRHS) const { return mValue == inRHS.mValue; }
51
52
/// Check if two quaternions are different
53
inline bool operator != (QuatArg inRHS) const { return mValue != inRHS.mValue; }
54
55
/// If this quaternion is close to inRHS. Note that q and -q represent the same rotation, this is not checked here.
56
inline bool IsClose(QuatArg inRHS, float inMaxDistSq = 1.0e-12f) const { return mValue.IsClose(inRHS.mValue, inMaxDistSq); }
57
58
/// If the length of this quaternion is 1 +/- inTolerance
59
inline bool IsNormalized(float inTolerance = 1.0e-5f) const { return mValue.IsNormalized(inTolerance); }
60
61
/// If any component of this quaternion is a NaN (not a number)
62
inline bool IsNaN() const { return mValue.IsNaN(); }
63
64
///@}
65
///@name Get components
66
///@{
67
68
/// Get X component (imaginary part i)
69
JPH_INLINE float GetX() const { return mValue.GetX(); }
70
71
/// Get Y component (imaginary part j)
72
JPH_INLINE float GetY() const { return mValue.GetY(); }
73
74
/// Get Z component (imaginary part k)
75
JPH_INLINE float GetZ() const { return mValue.GetZ(); }
76
77
/// Get W component (real part)
78
JPH_INLINE float GetW() const { return mValue.GetW(); }
79
80
/// Get the imaginary part of the quaternion
81
JPH_INLINE Vec3 GetXYZ() const { return Vec3(mValue); }
82
83
/// Get the quaternion as a Vec4
84
JPH_INLINE Vec4 GetXYZW() const { return mValue; }
85
86
/// Set individual components
87
JPH_INLINE void SetX(float inX) { mValue.SetX(inX); }
88
JPH_INLINE void SetY(float inY) { mValue.SetY(inY); }
89
JPH_INLINE void SetZ(float inZ) { mValue.SetZ(inZ); }
90
JPH_INLINE void SetW(float inW) { mValue.SetW(inW); }
91
92
/// Set all components
93
JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { mValue.Set(inX, inY, inZ, inW); }
94
95
///@}
96
///@name Default quaternions
97
///@{
98
99
/// @return [0, 0, 0, 0]
100
JPH_INLINE static Quat sZero() { return Quat(Vec4::sZero()); }
101
102
/// @return [1, 0, 0, 0] (or in storage format Quat(0, 0, 0, 1))
103
JPH_INLINE static Quat sIdentity() { return Quat(0, 0, 0, 1); }
104
105
///@}
106
107
/// Rotation from axis and angle
108
JPH_INLINE static Quat sRotation(Vec3Arg inAxis, float inAngle);
109
110
/// Get axis and angle that represents this quaternion, outAngle will always be in the range \f$[0, \pi]\f$
111
JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const;
112
113
/// Create quaternion that rotates a vector from the direction of inFrom to the direction of inTo along the shortest path
114
/// @see https://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
115
JPH_INLINE static Quat sFromTo(Vec3Arg inFrom, Vec3Arg inTo);
116
117
/// Random unit quaternion
118
template <class Random>
119
inline static Quat sRandom(Random &inRandom);
120
121
/// Conversion from Euler angles. Rotation order is X then Y then Z (RotZ * RotY * RotX). Angles in radians.
122
inline static Quat sEulerAngles(Vec3Arg inAngles);
123
124
/// Conversion to Euler angles. Rotation order is X then Y then Z (RotZ * RotY * RotX). Angles in radians.
125
inline Vec3 GetEulerAngles() const;
126
127
///@name Length / normalization operations
128
///@{
129
130
/// Squared length of quaternion.
131
/// @return Squared length of quaternion (\f$|v|^2\f$)
132
JPH_INLINE float LengthSq() const { return mValue.LengthSq(); }
133
134
/// Length of quaternion.
135
/// @return Length of quaternion (\f$|v|\f$)
136
JPH_INLINE float Length() const { return mValue.Length(); }
137
138
/// Normalize the quaternion (make it length 1)
139
JPH_INLINE Quat Normalized() const { return Quat(mValue.Normalized()); }
140
141
///@}
142
///@name Additions / multiplications
143
///@{
144
145
JPH_INLINE void operator += (QuatArg inRHS) { mValue += inRHS.mValue; }
146
JPH_INLINE void operator -= (QuatArg inRHS) { mValue -= inRHS.mValue; }
147
JPH_INLINE void operator *= (float inValue) { mValue *= inValue; }
148
JPH_INLINE void operator /= (float inValue) { mValue /= inValue; }
149
JPH_INLINE Quat operator - () const { return Quat(-mValue); }
150
JPH_INLINE Quat operator + (QuatArg inRHS) const { return Quat(mValue + inRHS.mValue); }
151
JPH_INLINE Quat operator - (QuatArg inRHS) const { return Quat(mValue - inRHS.mValue); }
152
JPH_INLINE Quat operator * (QuatArg inRHS) const;
153
JPH_INLINE Quat operator * (float inValue) const { return Quat(mValue * inValue); }
154
inline friend Quat operator * (float inValue, QuatArg inRHS) { return Quat(inRHS.mValue * inValue); }
155
JPH_INLINE Quat operator / (float inValue) const { return Quat(mValue / inValue); }
156
157
///@}
158
159
/// Rotate a vector by this quaternion
160
JPH_INLINE Vec3 operator * (Vec3Arg inValue) const;
161
162
/// Rotate a vector by the inverse of this quaternion
163
JPH_INLINE Vec3 InverseRotate(Vec3Arg inValue) const;
164
165
/// Rotate a the vector (1, 0, 0) with this quaternion
166
JPH_INLINE Vec3 RotateAxisX() const;
167
168
/// Rotate a the vector (0, 1, 0) with this quaternion
169
JPH_INLINE Vec3 RotateAxisY() const;
170
171
/// Rotate a the vector (0, 0, 1) with this quaternion
172
JPH_INLINE Vec3 RotateAxisZ() const;
173
174
/// Dot product
175
JPH_INLINE float Dot(QuatArg inRHS) const { return mValue.Dot(inRHS.mValue); }
176
177
/// The conjugate [w, -x, -y, -z] is the same as the inverse for unit quaternions
178
JPH_INLINE Quat Conjugated() const { return Quat(Vec4::sXor(mValue, UVec4(0x80000000, 0x80000000, 0x80000000, 0).ReinterpretAsFloat())); }
179
180
/// Get inverse quaternion
181
JPH_INLINE Quat Inversed() const { return Conjugated() / Length(); }
182
183
/// Ensures that the W component is positive by negating the entire quaternion if it is not. This is useful when you want to store a quaternion as a 3 vector by discarding W and reconstructing it as sqrt(1 - x^2 - y^2 - z^2).
184
JPH_INLINE Quat EnsureWPositive() const { return Quat(Vec4::sXor(mValue, Vec4::sAnd(mValue.SplatW(), UVec4::sReplicate(0x80000000).ReinterpretAsFloat()))); }
185
186
/// Get a quaternion that is perpendicular to this quaternion
187
JPH_INLINE Quat GetPerpendicular() const { return Quat(Vec4(1, -1, 1, -1) * mValue.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_W, SWIZZLE_Z>()); }
188
189
/// Get rotation angle around inAxis (uses Swing Twist Decomposition to get the twist quaternion and uses q(axis, angle) = [cos(angle / 2), axis * sin(angle / 2)])
190
JPH_INLINE float GetRotationAngle(Vec3Arg inAxis) const { return GetW() == 0.0f? JPH_PI : 2.0f * ATan(GetXYZ().Dot(inAxis) / GetW()); }
191
192
/// Swing Twist Decomposition: any quaternion can be split up as:
193
///
194
/// \f[q = q_{swing} \: q_{twist}\f]
195
///
196
/// where \f$q_{twist}\f$ rotates only around axis v.
197
///
198
/// \f$q_{twist}\f$ is:
199
///
200
/// \f[q_{twist} = \frac{[q_w, q_{ijk} \cdot v \: v]}{\left|[q_w, q_{ijk} \cdot v \: v]\right|}\f]
201
///
202
/// where q_w is the real part of the quaternion and q_i the imaginary part (a 3 vector).
203
///
204
/// The swing can then be calculated as:
205
///
206
/// \f[q_{swing} = q \: q_{twist}^* \f]
207
///
208
/// Where \f$q_{twist}^*\f$ = complex conjugate of \f$q_{twist}\f$
209
JPH_INLINE Quat GetTwist(Vec3Arg inAxis) const;
210
211
/// Decomposes quaternion into swing and twist component:
212
///
213
/// \f$q = q_{swing} \: q_{twist}\f$
214
///
215
/// where \f$q_{swing} \: \hat{x} = q_{twist} \: \hat{y} = q_{twist} \: \hat{z} = 0\f$
216
///
217
/// In other words:
218
///
219
/// - \f$q_{twist}\f$ only rotates around the X-axis.
220
/// - \f$q_{swing}\f$ only rotates around the Y and Z-axis.
221
///
222
/// @see Gino van den Bergen - Rotational Joint Limits in Quaternion Space - GDC 2016
223
JPH_INLINE void GetSwingTwist(Quat &outSwing, Quat &outTwist) const;
224
225
/// Linear interpolation between two quaternions (for small steps).
226
/// @param inFraction is in the range [0, 1]
227
/// @param inDestination The destination quaternion
228
/// @return (1 - inFraction) * this + fraction * inDestination
229
JPH_INLINE Quat LERP(QuatArg inDestination, float inFraction) const;
230
231
/// Spherical linear interpolation between two quaternions.
232
/// @param inFraction is in the range [0, 1]
233
/// @param inDestination The destination quaternion
234
/// @return When fraction is zero this quaternion is returned, when fraction is 1 inDestination is returned.
235
/// When fraction is between 0 and 1 an interpolation along the shortest path is returned.
236
JPH_INLINE Quat SLERP(QuatArg inDestination, float inFraction) const;
237
238
/// Load 3 floats from memory (X, Y and Z component and then calculates W) reads 32 bits extra which it doesn't use
239
static JPH_INLINE Quat sLoadFloat3Unsafe(const Float3 &inV);
240
241
/// Store 3 as floats to memory (X, Y and Z component)
242
JPH_INLINE void StoreFloat3(Float3 *outV) const;
243
244
/// To String
245
friend ostream & operator << (ostream &inStream, QuatArg inQ) { inStream << inQ.mValue; return inStream; }
246
247
/// 4 vector that stores [x, y, z, w] parts of the quaternion
248
Vec4 mValue;
249
};
250
251
static_assert(std::is_trivial<Quat>(), "Is supposed to be a trivial type!");
252
253
JPH_NAMESPACE_END
254
255
#include "Quat.inl"
256
257