Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Quat.h
Go to the documentation of this file.
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
11
32class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Quat
33{
34public:
36
39 inline Quat() = default;
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(const Float4 &inV) : mValue(Vec4::sLoadFloat4(&inV)) { }
44 inline explicit Quat(Vec4Arg inV) : mValue(inV) { }
46
49
51 inline bool operator == (QuatArg inRHS) const { return mValue == inRHS.mValue; }
52
54 inline bool operator != (QuatArg inRHS) const { return mValue != inRHS.mValue; }
55
57 inline bool IsClose(QuatArg inRHS, float inMaxDistSq = 1.0e-12f) const { return mValue.IsClose(inRHS.mValue, inMaxDistSq); }
58
60 inline bool IsNormalized(float inTolerance = 1.0e-5f) const { return mValue.IsNormalized(inTolerance); }
61
63 inline bool IsNaN() const { return mValue.IsNaN(); }
64
68
70 JPH_INLINE float GetX() const { return mValue.GetX(); }
71
73 JPH_INLINE float GetY() const { return mValue.GetY(); }
74
76 JPH_INLINE float GetZ() const { return mValue.GetZ(); }
77
79 JPH_INLINE float GetW() const { return mValue.GetW(); }
80
82 JPH_INLINE Vec3 GetXYZ() const { return Vec3(mValue); }
83
85 JPH_INLINE Vec4 GetXYZW() const { return mValue; }
86
88 JPH_INLINE void SetX(float inX) { mValue.SetX(inX); }
89 JPH_INLINE void SetY(float inY) { mValue.SetY(inY); }
90 JPH_INLINE void SetZ(float inZ) { mValue.SetZ(inZ); }
91 JPH_INLINE void SetW(float inW) { mValue.SetW(inW); }
92
94 JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { mValue.Set(inX, inY, inZ, inW); }
95
99
101 JPH_INLINE static Quat sZero() { return Quat(Vec4::sZero()); }
102
104 JPH_INLINE static Quat sIdentity() { return Quat(0, 0, 0, 1); }
105
107
109 JPH_INLINE static Quat sRotation(Vec3Arg inAxis, float inAngle);
110
112 JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const;
113
116 JPH_INLINE static Quat sFromTo(Vec3Arg inFrom, Vec3Arg inTo);
117
119 template <class Random>
120 inline static Quat sRandom(Random &inRandom);
121
123 inline static Quat sEulerAngles(Vec3Arg inAngles);
124
126 inline Vec3 GetEulerAngles() const;
127
130
133 JPH_INLINE float LengthSq() const { return mValue.LengthSq(); }
134
137 JPH_INLINE float Length() const { return mValue.Length(); }
138
140 JPH_INLINE Quat Normalized() const { return Quat(mValue.Normalized()); }
141
145
146 JPH_INLINE void operator += (QuatArg inRHS) { mValue += inRHS.mValue; }
147 JPH_INLINE void operator -= (QuatArg inRHS) { mValue -= inRHS.mValue; }
148 JPH_INLINE void operator *= (float inValue) { mValue *= inValue; }
149 JPH_INLINE void operator /= (float inValue) { mValue /= inValue; }
150 JPH_INLINE Quat operator - () const { return Quat(-mValue); }
151 JPH_INLINE Quat operator + (QuatArg inRHS) const { return Quat(mValue + inRHS.mValue); }
152 JPH_INLINE Quat operator - (QuatArg inRHS) const { return Quat(mValue - inRHS.mValue); }
153 JPH_INLINE Quat operator * (QuatArg inRHS) const;
154 JPH_INLINE Quat operator * (float inValue) const { return Quat(mValue * inValue); }
155 inline friend Quat operator * (float inValue, QuatArg inRHS) { return Quat(inRHS.mValue * inValue); }
156 JPH_INLINE Quat operator / (float inValue) const { return Quat(mValue / inValue); }
157
159
161 JPH_INLINE Vec3 operator * (Vec3Arg inValue) const;
162
164 static JPH_INLINE Quat sMultiplyImaginary(Vec3Arg inLHS, QuatArg inRHS);
165
167 JPH_INLINE Vec3 InverseRotate(Vec3Arg inValue) const;
168
170 JPH_INLINE Vec3 RotateAxisX() const;
171
173 JPH_INLINE Vec3 RotateAxisY() const;
174
176 JPH_INLINE Vec3 RotateAxisZ() const;
177
179 JPH_INLINE float Dot(QuatArg inRHS) const { return mValue.Dot(inRHS.mValue); }
180
182 JPH_INLINE Quat Conjugated() const { return Quat(mValue.FlipSign<-1, -1, -1, 1>()); }
183
185 JPH_INLINE Quat Inversed() const { return Conjugated() / Length(); }
186
188 JPH_INLINE Quat EnsureWPositive() const { return Quat(Vec4::sXor(mValue, Vec4::sAnd(mValue.SplatW(), UVec4::sReplicate(0x80000000).ReinterpretAsFloat()))); }
189
191 JPH_INLINE Quat GetPerpendicular() const { return Quat(mValue.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_W, SWIZZLE_Z>().FlipSign<1, -1, 1, -1>()); }
192
194 JPH_INLINE float GetRotationAngle(Vec3Arg inAxis) const { return GetW() == 0.0f? JPH_PI : 2.0f * ATan(GetXYZ().Dot(inAxis) / GetW()); }
195
213 JPH_INLINE Quat GetTwist(Vec3Arg inAxis) const;
214
227 JPH_INLINE void GetSwingTwist(Quat &outSwing, Quat &outTwist) const;
228
233 JPH_INLINE Quat LERP(QuatArg inDestination, float inFraction) const;
234
240 JPH_INLINE Quat SLERP(QuatArg inDestination, float inFraction) const;
241
243 static JPH_INLINE Quat sLoadFloat3Unsafe(const Float3 &inV);
244
246 JPH_INLINE void StoreFloat3(Float3 *outV) const;
247
249 JPH_INLINE void StoreFloat4(Float4 *outV) const;
250
252 JPH_INLINE uint32 CompressUnitQuat() const { return mValue.CompressUnitVector(); }
253
255 JPH_INLINE static Quat sDecompressUnitQuat(uint32 inValue) { return Quat(Vec4::sDecompressUnitVector(inValue)); }
256
258 friend ostream & operator << (ostream &inStream, QuatArg inQ) { inStream << inQ.mValue; return inStream; }
259
262};
263
264static_assert(std::is_trivial<Quat>(), "Is supposed to be a trivial type!");
265
267
268#include "Quat.inl"
#define JPH_NAMESPACE_END
Definition Core.h:419
std::uint32_t uint32
Definition Core.h:495
#define JPH_NAMESPACE_BEGIN
Definition Core.h:413
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition DVec3.inl:456
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:43
@ SWIZZLE_Z
Use the Z component.
Definition Swizzle.h:14
@ SWIZZLE_W
Use the W component.
Definition Swizzle.h:15
@ SWIZZLE_X
Use the X component.
Definition Swizzle.h:12
@ SWIZZLE_Y
Use the Y component.
Definition Swizzle.h:13
JPH_INLINE float ATan(float inX)
Arc tangent of x (returns value in the range [-PI / 2, PI / 2])
Definition Trigonometry.h:68
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition Float4.h:11
Definition Quat.h:33
Quat(const Quat &inRHS)=default
JPH_INLINE float GetW() const
Get W component (real part)
Definition Quat.h:79
JPH_INLINE float GetY() const
Get Y component (imaginary part j)
Definition Quat.h:73
JPH_INLINE float GetZ() const
Get Z component (imaginary part k)
Definition Quat.h:76
JPH_INLINE void SetZ(float inZ)
Definition Quat.h:90
JPH_INLINE float GetX() const
Get X component (imaginary part i)
Definition Quat.h:70
JPH_INLINE void SetY(float inY)
Definition Quat.h:89
JPH_INLINE uint32 CompressUnitQuat() const
Compress a unit quaternion to a 32 bit value, precision is around 0.5 degree.
Definition Quat.h:252
JPH_INLINE float LengthSq() const
Definition Quat.h:133
bool IsNaN() const
If any component of this quaternion is a NaN (not a number)
Definition Quat.h:63
static JPH_INLINE Quat sIdentity()
Definition Quat.h:104
JPH_INLINE Vec4 GetXYZW() const
Get the quaternion as a Vec4.
Definition Quat.h:85
static JPH_INLINE Quat sDecompressUnitQuat(uint32 inValue)
Decompress a unit quaternion from a 32 bit value.
Definition Quat.h:255
Quat(const Float4 &inV)
Definition Quat.h:43
bool IsClose(QuatArg inRHS, float inMaxDistSq=1.0e-12f) const
If this quaternion is close to inRHS. Note that q and -q represent the same rotation,...
Definition Quat.h:57
JPH_INLINE Quat Normalized() const
Normalize the quaternion (make it length 1)
Definition Quat.h:140
JPH_INLINE Quat EnsureWPositive() const
Ensures that the W component is positive by negating the entire quaternion if it is not....
Definition Quat.h:188
JPH_INLINE void Set(float inX, float inY, float inZ, float inW)
Set all components.
Definition Quat.h:94
Quat(Vec4Arg inV)
Definition Quat.h:44
JPH_INLINE float Dot(QuatArg inRHS) const
Dot product.
Definition Quat.h:179
Quat()=default
Intentionally not initialized for performance reasons.
JPH_INLINE float Length() const
Definition Quat.h:137
JPH_INLINE Quat GetPerpendicular() const
Get a quaternion that is perpendicular to this quaternion.
Definition Quat.h:191
JPH_INLINE Quat Inversed() const
Get inverse quaternion.
Definition Quat.h:185
JPH_INLINE Quat Conjugated() const
The conjugate [w, -x, -y, -z] is the same as the inverse for unit quaternions.
Definition Quat.h:182
JPH_INLINE float GetRotationAngle(Vec3Arg inAxis) const
Get rotation angle around inAxis (uses Swing Twist Decomposition to get the twist quaternion and uses...
Definition Quat.h:194
Quat(float inX, float inY, float inZ, float inW)
Definition Quat.h:42
JPH_INLINE void SetW(float inW)
Definition Quat.h:91
JPH_INLINE void SetX(float inX)
Set individual components.
Definition Quat.h:88
bool IsNormalized(float inTolerance=1.0e-5f) const
If the length of this quaternion is 1 +/- inTolerance.
Definition Quat.h:60
static JPH_INLINE Quat sZero()
Definition Quat.h:101
JPH_INLINE Vec3 GetXYZ() const
Get the imaginary part of the quaternion.
Definition Quat.h:82
Vec4 mValue
4 vector that stores [x, y, z, w] parts of the quaternion
Definition Quat.h:261
static JPH_INLINE UVec4 sReplicate(uint32 inV)
Replicate int inV across all components.
Definition UVec4.inl:56
JPH_INLINE Vec4 ReinterpretAsFloat() const
Reinterpret UVec4 as a Vec4 (doesn't change the bits)
Definition UVec4.inl:367
Definition Vec3.h:17
Definition Vec4.h:14
static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2)
Logical and (component wise)
Definition Vec4.inl:303
static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2)
Logical xor (component wise)
Definition Vec4.inl:292
static JPH_INLINE Vec4 sZero()
Vector with all zeros.
Definition Vec4.inl:63
static JPH_INLINE Vec4 sDecompressUnitVector(uint32 inValue)
Decompress a unit vector from a 32 bit value.
Definition Vec4.inl:1112