| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Math/Vec3.h> |
| 8 |
#include <Jolt/Math/Vec4.h> |
| 9 |
|
| 10 |
JPH_NAMESPACE_BEGIN |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Quat |
| 33 |
{ |
| 34 |
public: |
| 35 |
JPH_OVERRIDE_NEW_DELETE |
| 36 |
|
| 37 |
|
| 38 |
|
| 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) { } |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
inline bool operator == (QuatArg inRHS) const { return mValue == inRHS.mValue; } |
| 52 |
|
| 53 |
|
| 54 |
inline bool operator != (QuatArg inRHS) const { return mValue != inRHS.mValue; } |
| 55 |
|
| 56 |
|
| 57 |
inline bool IsClose(QuatArg inRHS, float inMaxDistSq = 1.0e-12f) const { return mValue.IsClose(inRHS.mValue, inMaxDistSq); } |
| 58 |
|
| 59 |
|
| 60 |
inline bool IsNormalized(float inTolerance = 1.0e-5f) const { return mValue.IsNormalized(inTolerance); } |
| 61 |
|
| 62 |
|
| 63 |
inline bool IsNaN() const { return mValue.IsNaN(); } |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
JPH_INLINE float GetX() const { return mValue.GetX(); } |
| 71 |
|
| 72 |
|
| 73 |
JPH_INLINE float GetY() const { return mValue.GetY(); } |
| 74 |
|
| 75 |
|
| 76 |
JPH_INLINE float GetZ() const { return mValue.GetZ(); } |
| 77 |
|
| 78 |
|
| 79 |
JPH_INLINE float GetW() const { return mValue.GetW(); } |
| 80 |
|
| 81 |
|
| 82 |
JPH_INLINE Vec3 GetXYZ() const { return Vec3(mValue); } |
| 83 |
|
| 84 |
|
| 85 |
JPH_INLINE Vec4 GetXYZW() const { return mValue; } |
| 86 |
|
| 87 |
|
| 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 |
|
| 93 |
|
| 94 |
JPH_INLINE void Set(float inX, float inY, float inZ, float inW) { mValue.Set(inX, inY, inZ, inW); } |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
JPH_INLINE static Quat sZero() { return Quat(Vec4::sZero()); } |
| 102 |
|
| 103 |
|
| 104 |
JPH_INLINE static Quat sIdentity() { return Quat(0, 0, 0, 1); } |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
JPH_INLINE static Quat sRotation(Vec3Arg inAxis, float inAngle); |
| 110 |
|
| 111 |
|
| 112 |
JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const; |
| 113 |
|
| 114 |
|
| 115 |
JPH_INLINE Vec3 GetAngularVelocity(float inDeltaTime) const; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
JPH_INLINE static Quat sFromTo(Vec3Arg inFrom, Vec3Arg inTo); |
| 120 |
|
| 121 |
|
| 122 |
template <class Random> |
| 123 |
inline static Quat sRandom(Random &inRandom); |
| 124 |
|
| 125 |
|
| 126 |
inline static Quat sEulerAngles(Vec3Arg inAngles); |
| 127 |
|
| 128 |
|
| 129 |
inline Vec3 GetEulerAngles() const; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
JPH_INLINE float LengthSq() const { return mValue.LengthSq(); } |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
JPH_INLINE float Length() const { return mValue.Length(); } |
| 141 |
|
| 142 |
|
| 143 |
JPH_INLINE Quat Normalized() const { return Quat(mValue.Normalized()); } |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
JPH_INLINE void operator += (QuatArg inRHS) { mValue += inRHS.mValue; } |
| 150 |
JPH_INLINE void operator -= (QuatArg inRHS) { mValue -= inRHS.mValue; } |
| 151 |
JPH_INLINE void operator *= (float inValue) { mValue *= inValue; } |
| 152 |
JPH_INLINE void operator /= (float inValue) { mValue /= inValue; } |
| 153 |
JPH_INLINE Quat operator - () const { return Quat(-mValue); } |
| 154 |
JPH_INLINE Quat operator + (QuatArg inRHS) const { return Quat(mValue + inRHS.mValue); } |
| 155 |
JPH_INLINE Quat operator - (QuatArg inRHS) const { return Quat(mValue - inRHS.mValue); } |
| 156 |
JPH_INLINE Quat operator * (QuatArg inRHS) const; |
| 157 |
JPH_INLINE Quat operator * (float inValue) const { return Quat(mValue * inValue); } |
| 158 |
inline friend Quat operator * (float inValue, QuatArg inRHS) { return Quat(inRHS.mValue * inValue); } |
| 159 |
JPH_INLINE Quat operator / (float inValue) const { return Quat(mValue / inValue); } |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
JPH_INLINE Vec3 operator * (Vec3Arg inValue) const; |
| 165 |
|
| 166 |
|
| 167 |
static JPH_INLINE Quat sMultiplyImaginary(Vec3Arg inLHS, QuatArg inRHS); |
| 168 |
|
| 169 |
|
| 170 |
JPH_INLINE Vec3 InverseRotate(Vec3Arg inValue) const; |
| 171 |
|
| 172 |
|
| 173 |
JPH_INLINE Vec3 RotateAxisX() const; |
| 174 |
|
| 175 |
|
| 176 |
JPH_INLINE Vec3 RotateAxisY() const; |
| 177 |
|
| 178 |
|
| 179 |
JPH_INLINE Vec3 RotateAxisZ() const; |
| 180 |
|
| 181 |
|
| 182 |
JPH_INLINE float Dot(QuatArg inRHS) const { return mValue.Dot(inRHS.mValue); } |
| 183 |
|
| 184 |
|
| 185 |
JPH_INLINE Quat Conjugated() const { return Quat(mValue.FlipSign<-1, -1, -1, 1>()); } |
| 186 |
|
| 187 |
|
| 188 |
JPH_INLINE Quat Inversed() const { return Conjugated() / Length(); } |
| 189 |
|
| 190 |
|
| 191 |
JPH_INLINE Quat EnsureWPositive() const { return Quat(Vec4::sXor(mValue, Vec4::sAnd(mValue.SplatW(), UVec4::sReplicate(0x80000000).ReinterpretAsFloat()))); } |
| 192 |
|
| 193 |
|
| 194 |
JPH_INLINE Quat GetPerpendicular() const { return Quat(mValue.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_W, SWIZZLE_Z>().FlipSign<1, -1, 1, -1>()); } |
| 195 |
|
| 196 |
|
| 197 |
JPH_INLINE float GetRotationAngle(Vec3Arg inAxis) const { return GetW() == 0.0f? JPH_PI : 2.0f * ATan(GetXYZ().Dot(inAxis) / GetW()); } |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
JPH_INLINE Quat GetTwist(Vec3Arg inAxis) const; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
JPH_INLINE void GetSwingTwist(Quat &outSwing, Quat &outTwist) const; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
JPH_INLINE Quat LERP(QuatArg inDestination, float inFraction) const; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
JPH_INLINE Quat SLERP(QuatArg inDestination, float inFraction) const; |
| 244 |
|
| 245 |
|
| 246 |
static JPH_INLINE Quat sLoadFloat3Unsafe(const Float3 &inV); |
| 247 |
|
| 248 |
|
| 249 |
JPH_INLINE void StoreFloat3(Float3 *outV) const; |
| 250 |
|
| 251 |
|
| 252 |
JPH_INLINE void StoreFloat4(Float4 *outV) const; |
| 253 |
|
| 254 |
|
| 255 |
JPH_INLINE uint32 CompressUnitQuat() const { return mValue.CompressUnitVector(); } |
| 256 |
|
| 257 |
|
| 258 |
JPH_INLINE static Quat sDecompressUnitQuat(uint32 inValue) { return Quat(Vec4::sDecompressUnitVector(inValue)); } |
| 259 |
|
| 260 |
|
| 261 |
friend ostream & operator << (ostream &inStream, QuatArg inQ) { inStream << inQ.mValue; return inStream; } |
| 262 |
|
| 263 |
|
| 264 |
Vec4 mValue; |
| 265 |
}; |
| 266 |
|
| 267 |
static_assert(std::is_trivial<Quat>(), "Is supposed to be a trivial type!"); |
| 268 |
|
| 269 |
JPH_NAMESPACE_END |
| 270 |
|
| 271 |
#include "Quat.inl" |