Quat.inl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. JPH_NAMESPACE_BEGIN
  5. Quat Quat::operator * (QuatArg inRHS) const
  6. {
  7. #if defined(JPH_USE_SSE4_1)
  8. // Taken from: http://momchil-velikov.blogspot.nl/2013/10/fast-sse-quternion-multiplication.html
  9. __m128 abcd = mValue.mValue;
  10. __m128 xyzw = inRHS.mValue.mValue;
  11. __m128 t0 = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(3, 3, 3, 3));
  12. __m128 t1 = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(2, 3, 0, 1));
  13. __m128 t3 = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(0, 0, 0, 0));
  14. __m128 t4 = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(1, 0, 3, 2));
  15. __m128 t5 = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(1, 1, 1, 1));
  16. __m128 t6 = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(2, 0, 3, 1));
  17. // [d,d,d,d] * [z,w,x,y] = [dz,dw,dx,dy]
  18. __m128 m0 = _mm_mul_ps(t0, t1);
  19. // [a,a,a,a] * [y,x,w,z] = [ay,ax,aw,az]
  20. __m128 m1 = _mm_mul_ps(t3, t4);
  21. // [b,b,b,b] * [z,x,w,y] = [bz,bx,bw,by]
  22. __m128 m2 = _mm_mul_ps(t5, t6);
  23. // [c,c,c,c] * [w,z,x,y] = [cw,cz,cx,cy]
  24. __m128 t7 = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(2, 2, 2, 2));
  25. __m128 t8 = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(3, 2, 0, 1));
  26. __m128 m3 = _mm_mul_ps(t7, t8);
  27. // [dz,dw,dx,dy] + -[ay,ax,aw,az] = [dz+ay,dw-ax,dx+aw,dy-az]
  28. __m128 e = _mm_addsub_ps(m0, m1);
  29. // [dx+aw,dz+ay,dy-az,dw-ax]
  30. e = _mm_shuffle_ps(e, e, _MM_SHUFFLE(1, 3, 0, 2));
  31. // [dx+aw,dz+ay,dy-az,dw-ax] + -[bz,bx,bw,by] = [dx+aw+bz,dz+ay-bx,dy-az+bw,dw-ax-by]
  32. e = _mm_addsub_ps(e, m2);
  33. // [dz+ay-bx,dw-ax-by,dy-az+bw,dx+aw+bz]
  34. e = _mm_shuffle_ps(e, e, _MM_SHUFFLE(2, 0, 1, 3));
  35. // [dz+ay-bx,dw-ax-by,dy-az+bw,dx+aw+bz] + -[cw,cz,cx,cy] = [dz+ay-bx+cw,dw-ax-by-cz,dy-az+bw+cx,dx+aw+bz-cy]
  36. e = _mm_addsub_ps(e, m3);
  37. // [dw-ax-by-cz,dz+ay-bx+cw,dy-az+bw+cx,dx+aw+bz-cy]
  38. return Quat(Vec4(_mm_shuffle_ps(e, e, _MM_SHUFFLE(2, 3, 1, 0))));
  39. #else
  40. float lx = mValue.GetX();
  41. float ly = mValue.GetY();
  42. float lz = mValue.GetZ();
  43. float lw = mValue.GetW();
  44. float rx = inRHS.mValue.GetX();
  45. float ry = inRHS.mValue.GetY();
  46. float rz = inRHS.mValue.GetZ();
  47. float rw = inRHS.mValue.GetW();
  48. float x = lw * rx + lx * rw + ly * rz - lz * ry;
  49. float y = lw * ry - lx * rz + ly * rw + lz * rx;
  50. float z = lw * rz + lx * ry - ly * rx + lz * rw;
  51. float w = lw * rw - lx * rx - ly * ry - lz * rz;
  52. return Quat(x, y, z, w);
  53. #endif
  54. }
  55. Quat Quat::sRotation(Vec3Arg inAxis, float inAngle)
  56. {
  57. // returns [inAxis * sin(0.5f * inAngle), cos(0.5f * inAngle)]
  58. JPH_ASSERT(inAxis.IsNormalized());
  59. Vec4 s, c;
  60. Vec4::sReplicate(0.5f * inAngle).SinCos(s, c);
  61. return Quat(Vec4::sSelect(Vec4(inAxis) * s, c, UVec4(0, 0, 0, 0xffffffffU)));
  62. }
  63. void Quat::GetAxisAngle(Vec3 &outAxis, float &outAngle) const
  64. {
  65. JPH_ASSERT(IsNormalized());
  66. Quat w_pos = EnsureWPositive();
  67. float abs_w = w_pos.GetW();
  68. if (abs_w >= 1.0f)
  69. {
  70. outAxis = Vec3::sZero();
  71. outAngle = 0.0f;
  72. }
  73. else
  74. {
  75. outAngle = 2.0f * ACos(abs_w);
  76. outAxis = w_pos.GetXYZ().NormalizedOr(Vec3::sZero());
  77. }
  78. }
  79. Quat Quat::sFromTo(Vec3Arg inFrom, Vec3Arg inTo)
  80. {
  81. /*
  82. Uses (inFrom = v1, inTo = v2):
  83. angle = arcos(v1 . v2 / |v1||v2|)
  84. axis = normalize(v1 x v2)
  85. Quaternion is then:
  86. s = sin(angle / 2)
  87. x = axis.x * s
  88. y = axis.y * s
  89. z = axis.z * s
  90. w = cos(angle / 2)
  91. Using identities:
  92. sin(2 * a) = 2 * sin(a) * cos(a)
  93. cos(2 * a) = cos(a)^2 - sin(a)^2
  94. sin(a)^2 + cos(a)^2 = 1
  95. This reduces to:
  96. x = (v1 x v2).x
  97. y = (v1 x v2).y
  98. z = (v1 x v2).z
  99. w = |v1||v2| + v1 . v2
  100. which then needs to be normalized because the whole equation was multiplied by 2 cos(angle / 2)
  101. */
  102. float len_v1_v2 = sqrt(inFrom.LengthSq() * inTo.LengthSq());
  103. float w = len_v1_v2 + inFrom.Dot(inTo);
  104. if (w == 0.0f)
  105. {
  106. if (len_v1_v2 == 0.0f)
  107. {
  108. // If either of the vectors has zero length, there is no rotation and we return identity
  109. return Quat::sIdentity();
  110. }
  111. else
  112. {
  113. // If vectors are perpendicular, take one of the many 180 degree rotations that exist
  114. return Quat(Vec4(inFrom.GetNormalizedPerpendicular(), 0));
  115. }
  116. }
  117. Vec3 v = inFrom.Cross(inTo);
  118. return Quat(Vec4(v, w)).Normalized();
  119. }
  120. template <class Random>
  121. Quat Quat::sRandom(Random &inRandom)
  122. {
  123. std::uniform_real_distribution<float> zero_to_one(0.0f, 1.0f);
  124. float x0 = zero_to_one(inRandom);
  125. float r1 = sqrt(1.0f - x0), r2 = sqrt(x0);
  126. std::uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
  127. Vec4 s, c;
  128. Vec4(zero_to_two_pi(inRandom), zero_to_two_pi(inRandom), 0, 0).SinCos(s, c);
  129. return Quat(s.GetX() * r1, c.GetX() * r1, s.GetY() * r2, c.GetY() * r2);
  130. }
  131. Quat Quat::sEulerAngles(Vec3Arg inAngles)
  132. {
  133. Vec4 half(0.5f * inAngles);
  134. Vec4 s, c;
  135. half.SinCos(s, c);
  136. float cx = c.GetX();
  137. float sx = s.GetX();
  138. float cy = c.GetY();
  139. float sy = s.GetY();
  140. float cz = c.GetZ();
  141. float sz = s.GetZ();
  142. return Quat(
  143. cz * sx * cy - sz * cx * sy,
  144. cz * cx * sy + sz * sx * cy,
  145. sz * cx * cy - cz * sx * sy,
  146. cz * cx * cy + sz * sx * sy);
  147. }
  148. Vec3 Quat::GetEulerAngles() const
  149. {
  150. float y_sq = GetY() * GetY();
  151. // X
  152. float t0 = 2.0f * (GetW() * GetX() + GetY() * GetZ());
  153. float t1 = 1.0f - 2.0f * (GetX() * GetX() + y_sq);
  154. // Y
  155. float t2 = 2.0f * (GetW() * GetY() - GetZ() * GetX());
  156. t2 = t2 > 1.0f? 1.0f : t2;
  157. t2 = t2 < -1.0f? -1.0f : t2;
  158. // Z
  159. float t3 = 2.0f * (GetW() * GetZ() + GetX() * GetY());
  160. float t4 = 1.0f - 2.0f * (y_sq + GetZ() * GetZ());
  161. return Vec3(ATan2(t0, t1), ASin(t2), ATan2(t3, t4));
  162. }
  163. Quat Quat::GetTwist(Vec3Arg inAxis) const
  164. {
  165. Quat twist(Vec4(GetXYZ().Dot(inAxis) * inAxis, GetW()));
  166. float twist_len = twist.LengthSq();
  167. if (twist_len != 0.0f)
  168. return twist / sqrt(twist_len);
  169. else
  170. return Quat::sIdentity();
  171. }
  172. void Quat::GetSwingTwist(Quat &outSwing, Quat &outTwist) const
  173. {
  174. float x = GetX(), y = GetY(), z = GetZ(), w = GetW();
  175. float s = sqrt(Square(w) + Square(x));
  176. if (s != 0.0f)
  177. {
  178. outTwist = Quat(x / s, 0, 0, w / s);
  179. outSwing = Quat(0, (w * y - x * z) / s, (w * z + x * y) / s, s);
  180. }
  181. else
  182. {
  183. // If both x and w are zero, this must be a 180 degree rotation around either y or z
  184. outTwist = Quat::sIdentity();
  185. outSwing = *this;
  186. }
  187. }
  188. Quat Quat::LERP(QuatArg inDestination, float inFraction) const
  189. {
  190. float scale0 = 1.0f - inFraction;
  191. return Quat(Vec4::sReplicate(scale0) * mValue + Vec4::sReplicate(inFraction) * inDestination.mValue);
  192. }
  193. Quat Quat::SLERP(QuatArg inDestination, float inFraction) const
  194. {
  195. // Difference at which to LERP instead of SLERP
  196. const float delta = 0.0001f;
  197. // Calc cosine
  198. float sign_scale1 = 1.0f;
  199. float cos_omega = Dot(inDestination);
  200. // Adjust signs (if necessary)
  201. if (cos_omega < 0.0f)
  202. {
  203. cos_omega = -cos_omega;
  204. sign_scale1 = -1.0f;
  205. }
  206. // Calculate coefficients
  207. float scale0, scale1;
  208. if (1.0f - cos_omega > delta)
  209. {
  210. // Standard case (slerp)
  211. float omega = ACos(cos_omega);
  212. float sin_omega = Sin(omega);
  213. scale0 = Sin((1.0f - inFraction) * omega) / sin_omega;
  214. scale1 = sign_scale1 * Sin(inFraction * omega) / sin_omega;
  215. }
  216. else
  217. {
  218. // Quaternions are very close so we can do a linear interpolation
  219. scale0 = 1.0f - inFraction;
  220. scale1 = sign_scale1 * inFraction;
  221. }
  222. // Interpolate between the two quaternions
  223. return Quat(Vec4::sReplicate(scale0) * mValue + Vec4::sReplicate(scale1) * inDestination.mValue).Normalized();
  224. }
  225. Vec3 Quat::operator * (Vec3Arg inValue) const
  226. {
  227. // Rotating a vector by a quaternion is done by: p' = q * p * q^-1 (q^-1 = conjugated(q) for a unit quaternion)
  228. JPH_ASSERT(IsNormalized());
  229. return Vec3((*this * Quat(Vec4(inValue, 0)) * Conjugated()).mValue);
  230. }
  231. Vec3 Quat::InverseRotate(Vec3Arg inValue) const
  232. {
  233. JPH_ASSERT(IsNormalized());
  234. return Vec3((Conjugated() * Quat(Vec4(inValue, 0)) * *this).mValue);
  235. }
  236. Vec3 Quat::RotateAxisX() const
  237. {
  238. // This is *this * Vec3::sAxisX() written out:
  239. JPH_ASSERT(IsNormalized());
  240. float x = GetX(), y = GetY(), z = GetZ(), w = GetW();
  241. float tx = 2.0f * x, tw = 2.0f * w;
  242. return Vec3(tx * x + tw * w - 1.0f, tx * y + z * tw, tx * z - y * tw);
  243. }
  244. Vec3 Quat::RotateAxisY() const
  245. {
  246. // This is *this * Vec3::sAxisY() written out:
  247. JPH_ASSERT(IsNormalized());
  248. float x = GetX(), y = GetY(), z = GetZ(), w = GetW();
  249. float ty = 2.0f * y, tw = 2.0f * w;
  250. return Vec3(x * ty - z * tw, tw * w + ty * y - 1.0f, x * tw + ty * z);
  251. }
  252. Vec3 Quat::RotateAxisZ() const
  253. {
  254. // This is *this * Vec3::sAxisZ() written out:
  255. JPH_ASSERT(IsNormalized());
  256. float x = GetX(), y = GetY(), z = GetZ(), w = GetW();
  257. float tz = 2.0f * z, tw = 2.0f * w;
  258. return Vec3(x * tz + y * tw, y * tz - x * tw, tw * w + tz * z - 1.0f);
  259. }
  260. void Quat::StoreFloat3(Float3 *outV) const
  261. {
  262. JPH_ASSERT(IsNormalized());
  263. EnsureWPositive().GetXYZ().StoreFloat3(outV);
  264. }
  265. Quat Quat::sLoadFloat3Unsafe(const Float3 &inV)
  266. {
  267. Vec3 v = Vec3::sLoadFloat3Unsafe(inV);
  268. float w = sqrt(max(1.0f - v.LengthSq(), 0.0f)); // It is possible that the length of v is a fraction above 1, and we don't want to introduce NaN's in that case so we clamp to 0
  269. return Quat(Vec4(v, w));
  270. }
  271. JPH_NAMESPACE_END