You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

264 line
8.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This library is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. namespace lol
  14. {
  15. template<> quat quat::rotate(float degrees, vec3 const &v)
  16. {
  17. float half_angle = radians(degrees) * 0.5f;
  18. vec3 tmp = normalize(v) * sin(half_angle);
  19. return quat(cos(half_angle), tmp.x, tmp.y, tmp.z);
  20. }
  21. template<> quat quat::rotate(float degrees, float x, float y, float z)
  22. {
  23. return quat::rotate(degrees, vec3(x, y, z));
  24. }
  25. template<> quat quat::rotate(vec3 const &src, vec3 const &dst)
  26. {
  27. /* Algorithm directly taken from Sam Hocevar's article "Quaternion from
  28. * two vectors: the final version".
  29. * http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final */
  30. float magnitude = lol::sqrt(sqlength(src) * sqlength(dst));
  31. float real_part = magnitude + dot(src, dst);
  32. vec3 w;
  33. if (real_part < 1.e-6f * magnitude)
  34. {
  35. /* If src and dst are exactly opposite, rotate 180 degrees
  36. * around an arbitrary orthogonal axis. Axis normalisation
  37. * can happen later, when we normalise the quaternion. */
  38. real_part = 0.0f;
  39. w = abs(src.x) > abs(src.z) ? vec3(-src.y, src.x, 0.f)
  40. : vec3(0.f, -src.z, src.y);
  41. }
  42. else
  43. {
  44. /* Otherwise, build quaternion the standard way. */
  45. w = cross(src, dst);
  46. }
  47. return normalize(quat(real_part, w.x, w.y, w.z));
  48. }
  49. template<> quat slerp(quat const &qa, quat const &qb, float f)
  50. {
  51. float const magnitude = lol::sqrt(sqlength(qa) * sqlength(qb));
  52. float const product = lol::dot(qa, qb) / magnitude;
  53. /* If quaternions are equal or opposite, there is no need
  54. * to slerp anything, just return qa. */
  55. if (std::abs(product) >= 1.0f)
  56. return qa;
  57. float const sign = (product < 0.0f) ? -1.0f : 1.0f;
  58. float const theta = lol::acos(sign * product);
  59. float const s1 = lol::sin(sign * f * theta);
  60. float const s0 = lol::sin((1.0f - f) * theta);
  61. /* This is the same as 1/sin(theta) */
  62. float const d = 1.0f / lol::sqrt(1.f - product * product);
  63. return qa * (s0 * d) + qb * (s1 * d);
  64. }
  65. static inline vec3 quat_toeuler_generic(quat const &q, int i, int j, int k)
  66. {
  67. float n = norm(q);
  68. if (!n)
  69. return vec3::zero;
  70. /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */
  71. float const sign = ((2 + i - j) % 3) ? 1.f : -1.f;
  72. vec3 ret;
  73. /* k == i means X-Y-X style Euler angles; otherwise we’re
  74. * actually handling X-Y-Z style Tait-Bryan angles. */
  75. if (k == i)
  76. {
  77. k = 3 - i - j;
  78. ret[0] = atan2(q[1 + i] * q[1 + j] + sign * (q.w * q[1 + k]),
  79. q.w * q[1 + j] - sign * (q[1 + i] * q[1 + k]));
  80. ret[1] = acos(2.f * (sq(q.w) + sq(q[1 + i])) - 1.f);
  81. ret[2] = atan2(q[1 + i] * q[1 + j] - sign * (q.w * q[1 + k]),
  82. q.w * q[1 + j] + sign * (q[1 + i] * q[1 + k]));
  83. }
  84. else
  85. {
  86. ret[0] = atan2(2.f * (q.w * q[1 + i] - sign * (q[1 + j] * q[1 + k])),
  87. 1.f - 2.f * (sq(q[1 + i]) + sq(q[1 + j])));
  88. ret[1] = asin(2.f * (q.w * q[1 + j] + sign * (q[1 + i] * q[1 + k])));
  89. ret[2] = atan2(2.f * (q.w * q[1 + k] - sign * (q[1 + j] * q[1 + i])),
  90. 1.f - 2.f * (sq(q[1 + k]) + sq(q[1 + j])));
  91. }
  92. return degrees(ret / n);
  93. }
  94. static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k)
  95. {
  96. mat3 ret;
  97. vec3 const w = radians(v);
  98. float const s0 = sin(w[0]), c0 = cos(w[0]);
  99. float const s1 = sin(w[1]), c1 = cos(w[1]);
  100. float const s2 = sin(w[2]), c2 = cos(w[2]);
  101. /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */
  102. float const sign = ((2 + i - j) % 3) ? 1.f : -1.f;
  103. /* k == i means X-Y-X style Euler angles; otherwise we’re
  104. * actually handling X-Y-Z style Tait-Bryan angles. */
  105. if (k == i)
  106. {
  107. k = 3 - i - j;
  108. ret[i][i] = c1;
  109. ret[i][j] = s0 * s1;
  110. ret[i][k] = - sign * (c0 * s1);
  111. ret[j][i] = s1 * s2;
  112. ret[j][j] = c0 * c2 - s0 * c1 * s2;
  113. ret[j][k] = sign * (s0 * c2 + c0 * c1 * s2);
  114. ret[k][i] = sign * (s1 * c2);
  115. ret[k][j] = - sign * (c0 * s2 + s0 * c1 * c2);
  116. ret[k][k] = - s0 * s2 + c0 * c1 * c2;
  117. }
  118. else
  119. {
  120. ret[i][i] = c1 * c2;
  121. ret[i][j] = sign * (c0 * s2) + s0 * s1 * c2;
  122. ret[i][k] = s0 * s2 - sign * (c0 * s1 * c2);
  123. ret[j][i] = - sign * (c1 * s2);
  124. ret[j][j] = c0 * c2 - sign * (s0 * s1 * s2);
  125. ret[j][k] = sign * (s0 * c2) + c0 * s1 * s2;
  126. ret[k][i] = sign * s1;
  127. ret[k][j] = - sign * (s0 * c1);
  128. ret[k][k] = c0 * c1;
  129. }
  130. return ret;
  131. }
  132. static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k)
  133. {
  134. vec3 const half_angles = radians(v * 0.5f);
  135. float const s0 = sin(half_angles[0]), c0 = cos(half_angles[0]);
  136. float const s1 = sin(half_angles[1]), c1 = cos(half_angles[1]);
  137. float const s2 = sin(half_angles[2]), c2 = cos(half_angles[2]);
  138. quat ret;
  139. /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */
  140. float const sign = ((2 + i - j) % 3) ? 1.f : -1.f;
  141. /* k == i means X-Y-X style Euler angles; otherwise we’re
  142. * actually handling X-Y-Z style Tait-Bryan angles. */
  143. if (k == i)
  144. {
  145. k = 3 - i - j;
  146. ret[0] = c1 * (c0 * c2 - s0 * s2);
  147. ret[1 + i] = c1 * (c0 * s2 + s0 * c2);
  148. ret[1 + j] = s1 * (c0 * c2 + s0 * s2);
  149. ret[1 + k] = sign * (s1 * (s0 * c2 - c0 * s2));
  150. }
  151. else
  152. {
  153. ret[0] = c0 * c1 * c2 - sign * (s0 * s1 * s2);
  154. ret[1 + i] = s0 * c1 * c2 + sign * (c0 * s1 * s2);
  155. ret[1 + j] = c0 * s1 * c2 - sign * (s0 * c1 * s2);
  156. ret[1 + k] = c0 * c1 * s2 + sign * (s0 * s1 * c2);
  157. }
  158. return ret;
  159. }
  160. #define DEFINE_GENERIC_EULER_CONVERSIONS(a1, a2, a3) \
  161. DEFINE_GENERIC_EULER_CONVERSIONS_INNER(a1, a2, a3, a1##a2##a3) \
  162. #define DEFINE_GENERIC_EULER_CONVERSIONS_INNER(a1, a2, a3, name) \
  163. /* Create quaternions from Euler angles */ \
  164. template<> quat quat::fromeuler_##name(vec3 const &v) \
  165. { \
  166. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  167. return quat_fromeuler_generic(v, a1, a2, a3); \
  168. } \
  169. \
  170. template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \
  171. { \
  172. return quat::fromeuler_##name(vec3(phi, theta, psi)); \
  173. } \
  174. \
  175. /* Create 3×3 matrices from Euler angles */ \
  176. template<> mat3 mat3::fromeuler_##name(vec3 const &v) \
  177. { \
  178. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  179. return mat3_fromeuler_generic(v, a1, a2, a3); \
  180. } \
  181. \
  182. template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \
  183. { \
  184. return mat3::fromeuler_##name(vec3(phi, theta, psi)); \
  185. } \
  186. \
  187. /* Create 4×4 matrices from Euler angles */ \
  188. template<> mat4 mat4::fromeuler_##name(vec3 const &v) \
  189. { \
  190. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  191. return mat4(mat3_fromeuler_generic(v, a1, a2, a3), 1.f); \
  192. } \
  193. \
  194. template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \
  195. { \
  196. return mat4::fromeuler_##name(vec3(phi, theta, psi)); \
  197. } \
  198. \
  199. /* Retrieve Euler angles from a quaternion */ \
  200. template<> vec3 vec3::toeuler_##name(quat const &q) \
  201. { \
  202. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  203. return quat_toeuler_generic(q, a1, a2, a3); \
  204. }
  205. DEFINE_GENERIC_EULER_CONVERSIONS(x, y, x)
  206. DEFINE_GENERIC_EULER_CONVERSIONS(x, z, x)
  207. DEFINE_GENERIC_EULER_CONVERSIONS(y, x, y)
  208. DEFINE_GENERIC_EULER_CONVERSIONS(y, z, y)
  209. DEFINE_GENERIC_EULER_CONVERSIONS(z, x, z)
  210. DEFINE_GENERIC_EULER_CONVERSIONS(z, y, z)
  211. DEFINE_GENERIC_EULER_CONVERSIONS(x, y, z)
  212. DEFINE_GENERIC_EULER_CONVERSIONS(x, z, y)
  213. DEFINE_GENERIC_EULER_CONVERSIONS(y, x, z)
  214. DEFINE_GENERIC_EULER_CONVERSIONS(y, z, x)
  215. DEFINE_GENERIC_EULER_CONVERSIONS(z, x, y)
  216. DEFINE_GENERIC_EULER_CONVERSIONS(z, y, x)
  217. #undef DEFINE_GENERIC_EULER_CONVERSIONS
  218. #undef DEFINE_GENERIC_EULER_CONVERSIONS_INNER
  219. } /* namespace lol */