Quat.inl 8.6 KB

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