14 __m128 abca = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(0, 2, 1, 0));
15 __m128 bcab = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(1, 0, 2, 1));
16 __m128 cabc = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(2, 1, 0, 2));
17 __m128 dddd = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(3, 3, 3, 3));
19 __m128 wwwx = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(0, 3, 3, 3));
20 __m128 zxyy = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(1, 1, 0, 2));
21 __m128 yzxz = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(2, 0, 2, 1));
23 __m128 m2 = _mm_mul_ps(bcab, zxyy);
25 __m128 m3 = _mm_fmadd_ps(abca, wwwx, m2);
27 __m128 m1 = _mm_mul_ps(abca, wwwx);
28 __m128 m3 = _mm_add_ps(m1, m2);
32 m3 = _mm_xor_ps(_mm_set_ps(-0.0f, 0.0f, 0.0f, 0.0f), m3);
35 __m128 m5 = _mm_fnmadd_ps(cabc, yzxz, m3);
36 __m128 m7 = _mm_fmadd_ps(dddd, xyzw, m5);
38 __m128 m4 = _mm_mul_ps(dddd, xyzw);
39 __m128 m5 = _mm_mul_ps(cabc, yzxz);
40 __m128 m6 = _mm_sub_ps(m4, m5);
41 __m128 m7 = _mm_add_ps(m3, m6);
46#elif defined(JPH_USE_NEON)
50 float32x4_t abca = vcopyq_laneq_f32(abcd, 3, abcd, 0);
51 float32x4_t bcab = JPH_NEON_SHUFFLE_F32x4(abcd, abcd, 1, 2, 0, 1);
52 float32x4_t cabc = JPH_NEON_SHUFFLE_F32x4(abcd, abcd, 2, 0, 1, 2);
53 float32x4_t dddd = vdupq_laneq_f32(abcd, 3);
55 float32x4_t wwwx = vcopyq_laneq_f32(vdupq_laneq_f32(xyzw, 3), 3, xyzw, 0);
56 float32x4_t zxyy = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 2, 0, 1, 1);
57 float32x4_t yzxz = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 1, 2, 0, 2);
59 float32x4_t m1 = vmulq_f32(abca, wwwx);
60 float32x4_t m2 = vmulq_f32(bcab, zxyy);
61 float32x4_t m3 = vaddq_f32(m1, m2);
63 uint32x4_t w_neg_mask = JPH_NEON_UINT32x4(0, 0, 0, 0x80000000u);
64 m3 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(m3), w_neg_mask));
66 float32x4_t m4 = vmulq_f32(dddd, xyzw);
67 float32x4_t m5 = vmulq_f32(cabc, yzxz);
68 float32x4_t m6 = vsubq_f32(m4, m5);
69 float32x4_t m7 = vaddq_f32(m3, m6);
83 return Quat((a * w + b * z) + (d * x - c * y),
84 (b * w + c * x) + (d * y - a * z),
85 (c * w + a * y) + (d * z - b * x),
86 -(a * x + b * y) + (d * w - c * z));
93 __m128 abc0 = inLHS.
mValue;
97 __m128 abca = _mm_shuffle_ps(abc0, abc0, _MM_SHUFFLE(0, 2, 1, 0));
98 __m128 bcab = _mm_shuffle_ps(abc0, abc0, _MM_SHUFFLE(1, 0, 2, 1));
99 __m128 cabc = _mm_shuffle_ps(abc0, abc0, _MM_SHUFFLE(2, 1, 0, 2));
101 __m128 wwwx = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(0, 3, 3, 3));
102 __m128 zxyy = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(1, 1, 0, 2));
103 __m128 yzxz = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(2, 0, 2, 1));
105 __m128 m2 = _mm_mul_ps(bcab, zxyy);
107 __m128 m3 = _mm_fmadd_ps(abca, wwwx, m2);
109 __m128 m1 = _mm_mul_ps(abca, wwwx);
110 __m128 m3 = _mm_add_ps(m1, m2);
114 m3 = _mm_xor_ps(_mm_set_ps(-0.0f, 0.0f, 0.0f, 0.0f), m3);
116 __m128 m4 = _mm_mul_ps(cabc, yzxz);
119 return Quat(
Vec4(_mm_sub_ps(m3, m4)));
120#elif defined(JPH_USE_NEON)
121 float32x4_t abc0 = inLHS.
mValue;
124 float32x4_t abca = vcopyq_laneq_f32(abc0, 3, abc0, 0);
125 float32x4_t bcab = JPH_NEON_SHUFFLE_F32x4(abc0, abc0, 1, 2, 0, 1);
126 float32x4_t cabc = JPH_NEON_SHUFFLE_F32x4(abc0, abc0, 2, 0, 1, 2);
128 float32x4_t wwwx = vcopyq_laneq_f32(vdupq_laneq_f32(xyzw, 3), 3, xyzw, 0);
129 float32x4_t zxyy = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 2, 0, 1, 1);
130 float32x4_t yzxz = JPH_NEON_SHUFFLE_F32x4(xyzw, xyzw, 1, 2, 0, 2);
132 float32x4_t m1 = vmulq_f32(abca, wwwx);
133 float32x4_t m2 = vmulq_f32(bcab, zxyy);
134 float32x4_t m3 = vaddq_f32(m1, m2);
136 uint32x4_t w_neg_mask = JPH_NEON_UINT32x4(0, 0, 0, 0x80000000u);
137 m3 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(m3), w_neg_mask));
139 float32x4_t m4 = vmulq_f32(cabc, yzxz);
140 float32x4_t m7 = vsubq_f32(m3, m4);
144 float a = inLHS.
GetX();
145 float b = inLHS.
GetY();
146 float c = inLHS.
GetZ();
153 return Quat((a * w + b * z) - c * y,
154 (b * w + c * x) - a * z,
155 (c * w + a * y) - b * x,
156 -(a * x + b * y) - c * z);
173 float abs_w = w_pos.
GetW();
181 outAngle = 2.0f *
ACos(abs_w);
197 if (xyz_len_sq < 4.0e-4f)
198 return (2.0f / inDeltaTime) *
xyz;
202 float angle = 2.0f *
ACos(w_pos.
GetW());
203 return (
xyz / (
Sqrt(xyz_len_sq) * inDeltaTime)) * angle;
239 float w = len_v1_v2 + inFrom.
Dot(inTo);
243 if (len_v1_v2 == 0.0f)
259template <
class Random>
262 std::uniform_real_distribution<float> zero_to_one(0.0f, 1.0f);
263 float x0 = zero_to_one(inRandom);
264 float r1 =
Sqrt(1.0f - x0), r2 =
Sqrt(x0);
265 std::uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
267 Vec4(zero_to_two_pi(inRandom), zero_to_two_pi(inRandom), 0, 0).
SinCos(s, c);
273 Vec4 half(0.5f * inAngles);
285 cz * sx * cy - sz * cx * sy,
286 cz * cx * sy + sz * sx * cy,
287 sz * cx * cy - cz * sx * sy,
288 cz * cx * cy + sz * sx * sy);
297 float t1 = 1.0f - 2.0f * (
GetX() *
GetX() + y_sq);
301 t2 = t2 > 1.0f? 1.0f : t2;
302 t2 = t2 < -1.0f? -1.0f : t2;
306 float t4 = 1.0f - 2.0f * (y_sq +
GetZ() *
GetZ());
315 if (twist_len != 0.0f)
316 return twist /
Sqrt(twist_len);
327 outTwist =
Quat(x / s, 0, 0, w / s);
328 outSwing =
Quat(0, (w * y - x * z) / s, (w * z + x * y) / s, s);
340 float scale0 = 1.0f - inFraction;
347 const float delta = 0.0001f;
350 float sign_scale1 = 1.0f;
351 float cos_omega =
Dot(inDestination);
354 if (cos_omega < 0.0f)
356 cos_omega = -cos_omega;
361 float scale0, scale1;
362 if (1.0f - cos_omega > delta)
365 float omega =
ACos(cos_omega);
366 float sin_omega =
Sin(omega);
367 scale0 =
Sin((1.0f - inFraction) * omega) / sin_omega;
368 scale1 = sign_scale1 *
Sin(inFraction * omega) / sin_omega;
373 scale0 = 1.0f - inFraction;
374 scale1 = sign_scale1 * inFraction;
402 return inValue + (v + v);
413 return inValue + (v + v);
#define JPH_NAMESPACE_END
Definition Core.h:428
#define JPH_NAMESPACE_BEGIN
Definition Core.h:422
#define xyz
Definition HLSLToCPP.h:513
#define yzx
Definition HLSLToCPP.h:516
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition Math.h:55
JPH_INLINE float Sqrt(float inV)
Take the square root of a float value.
Definition Math.h:61
@ 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 ACos(float inX)
Definition Trigonometry.h:42
JPH_INLINE float ATan2(float inY, float inX)
Arc tangent of y / x using the signs of the arguments to determine the correct quadrant (returns valu...
Definition Trigonometry.h:74
JPH_NAMESPACE_BEGIN JPH_INLINE float Sin(float inX)
Sine of x (input in radians)
Definition Trigonometry.h:12
JPH_INLINE float ASin(float inX)
Definition Trigonometry.h:35
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
static Quat sRandom(Random &inRandom)
Random unit quaternion.
Definition Quat.inl:260
JPH_INLINE Vec3 InverseRotate(Vec3Arg inValue) const
Rotate a vector by the inverse of this quaternion.
Definition Quat.inl:405
JPH_INLINE float GetW() const
Get W component (real part)
Definition Quat.h:79
static JPH_INLINE Quat sMultiplyImaginary(Vec3Arg inLHS, QuatArg inRHS)
Multiply a quaternion with imaginary components and no real component (x, y, z, 0) with a quaternion.
Definition Quat.inl:90
static Quat sEulerAngles(Vec3Arg inAngles)
Conversion from Euler angles. Rotation order is X then Y then Z (RotZ * RotY * RotX)....
Definition Quat.inl:271
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
static JPH_INLINE Quat sRotation(Vec3Arg inAxis, float inAngle)
Rotation from axis and angle.
Definition Quat.inl:160
JPH_INLINE float GetX() const
Get X component (imaginary part i)
Definition Quat.h:70
JPH_INLINE Vec3 GetAngularVelocity(float inDeltaTime) const
Calculate angular velocity given that this quaternion represents the rotation that is reached after i...
Definition Quat.inl:186
JPH_INLINE Quat LERP(QuatArg inDestination, float inFraction) const
Definition Quat.inl:338
static JPH_INLINE Quat sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (X, Y and Z component and then calculates W) reads 32 bits extra which it d...
Definition Quat.inl:451
JPH_INLINE float LengthSq() const
Definition Quat.h:136
JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const
Get axis and angle that represents this quaternion, outAngle will always be in the range .
Definition Quat.inl:169
static JPH_INLINE Quat sIdentity()
Definition Quat.h:104
JPH_INLINE void GetSwingTwist(Quat &outSwing, Quat &outTwist) const
Definition Quat.inl:321
JPH_INLINE void StoreFloat4(Float4 *outV) const
Store as 4 floats.
Definition Quat.inl:446
static JPH_INLINE Quat sFromTo(Vec3Arg inFrom, Vec3Arg inTo)
Definition Quat.inl:206
friend Quat operator*(float inValue, QuatArg inRHS)
Definition Quat.h:158
JPH_INLINE Quat EnsureWPositive() const
Ensures that the W component is positive by negating the entire quaternion if it is not....
Definition Quat.h:191
JPH_INLINE Quat GetTwist(Vec3Arg inAxis) const
Definition Quat.inl:311
JPH_INLINE float Dot(QuatArg inRHS) const
Dot product.
Definition Quat.h:182
Quat()=default
Intentionally not initialized for performance reasons.
JPH_INLINE Vec3 RotateAxisZ() const
Rotate a the vector (0, 0, 1) with this quaternion.
Definition Quat.inl:432
JPH_INLINE Vec3 RotateAxisX() const
Rotate a the vector (1, 0, 0) with this quaternion.
Definition Quat.inl:416
JPH_INLINE Vec3 RotateAxisY() const
Rotate a the vector (0, 1, 0) with this quaternion.
Definition Quat.inl:424
Vec3 GetEulerAngles() const
Conversion to Euler angles. Rotation order is X then Y then Z (RotZ * RotY * RotX)....
Definition Quat.inl:291
bool IsNormalized(float inTolerance=1.0e-5f) const
If the length of this quaternion is 1 +/- inTolerance.
Definition Quat.h:60
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store as 3 floats to memory (X, Y and Z component). Ensures that W is positive before storing.
Definition Quat.inl:440
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:264
JPH_INLINE Quat SLERP(QuatArg inDestination, float inFraction) const
Definition Quat.inl:344
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:943
JPH_INLINE Vec3 Normalized() const
Normalize vector.
Definition Vec3.inl:1044
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:841
JPH_INLINE Vec3 GetNormalizedPerpendicular() const
Get normalized vector that is perpendicular to this vector.
Definition Vec3.inl:1218
JPH_INLINE float GetX() const
Get individual components.
Definition Vec3.h:127
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition Vec3.inl:1129
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition Vec3.inl:1076
Type mValue
Definition Vec3.h:299
JPH_INLINE float GetY() const
Definition Vec3.h:128
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition Vec3.inl:1154
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:972
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:125
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition Vec3.inl:167
JPH_INLINE float GetZ() const
Definition Vec3.h:129
JPH_INLINE Vec3 Swizzle() const
Swizzle the elements in inV.
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec4.inl:808
JPH_INLINE Vec3 SplatW3() const
Replicate the W component to all components.
Definition Vec4.inl:920
JPH_INLINE Vec4 FlipSign() const
Flips the signs of the components, e.g. FlipSign<-1, 1, -1, 1>() will flip the signs of the X and Z c...
Definition Vec4.inl:1104
JPH_INLINE float GetW() const
Definition Vec4.h:122
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec4.inl:824
JPH_INLINE Vec4 SplatZ() const
Replicate the Z component to all components.
Definition Vec4.inl:840
JPH_INLINE Vec4 SplatW() const
Replicate the W component to all components.
Definition Vec4.inl:856
JPH_INLINE float GetX() const
Get individual components.
Definition Vec4.h:119
JPH_INLINE Vec4 Swizzle() const
Swizzle the elements in inV.
Type mValue
Definition Vec4.h:314
JPH_INLINE float GetZ() const
Definition Vec4.h:121
static JPH_INLINE Vec4 sSelect(Vec4Arg inNotSet, Vec4Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition Vec4.inl:377
JPH_INLINE float GetY() const
Definition Vec4.h:120
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition Vec4.inl:97
void SinCos(Vec4 &outSin, Vec4 &outCos) const
Calculate the sine and cosine for each element of this vector (input in radians)
Definition Vec4.inl:1221
JPH_INLINE void StoreFloat4(Float4 *outV) const
Store 4 floats to memory.
Definition Vec4.inl:1138