quat.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*************************************************************************/
  2. /* quat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "quat.h"
  31. #include "print_string.h"
  32. void Quat::set_euler(const Vector3 &p_euler) {
  33. real_t half_yaw = p_euler.x * 0.5;
  34. real_t half_pitch = p_euler.y * 0.5;
  35. real_t half_roll = p_euler.z * 0.5;
  36. real_t cos_yaw = Math::cos(half_yaw);
  37. real_t sin_yaw = Math::sin(half_yaw);
  38. real_t cos_pitch = Math::cos(half_pitch);
  39. real_t sin_pitch = Math::sin(half_pitch);
  40. real_t cos_roll = Math::cos(half_roll);
  41. real_t sin_roll = Math::sin(half_roll);
  42. set(cos_roll * sin_pitch * cos_yaw + sin_roll * cos_pitch * sin_yaw,
  43. cos_roll * cos_pitch * sin_yaw - sin_roll * sin_pitch * cos_yaw,
  44. sin_roll * cos_pitch * cos_yaw - cos_roll * sin_pitch * sin_yaw,
  45. cos_roll * cos_pitch * cos_yaw + sin_roll * sin_pitch * sin_yaw);
  46. }
  47. void Quat::operator*=(const Quat &q) {
  48. set(w * q.x + x * q.w + y * q.z - z * q.y,
  49. w * q.y + y * q.w + z * q.x - x * q.z,
  50. w * q.z + z * q.w + x * q.y - y * q.x,
  51. w * q.w - x * q.x - y * q.y - z * q.z);
  52. }
  53. Quat Quat::operator*(const Quat &q) const {
  54. Quat r = *this;
  55. r *= q;
  56. return r;
  57. }
  58. real_t Quat::length() const {
  59. return Math::sqrt(length_squared());
  60. }
  61. void Quat::normalize() {
  62. *this /= length();
  63. }
  64. Quat Quat::normalized() const {
  65. return *this / length();
  66. }
  67. Quat Quat::inverse() const {
  68. return Quat(-x, -y, -z, w);
  69. }
  70. Quat Quat::slerp(const Quat &q, const real_t &t) const {
  71. #if 0
  72. Quat dst=q;
  73. Quat src=*this;
  74. src.normalize();
  75. dst.normalize();
  76. real_t cosine = dst.dot(src);
  77. if (cosine < 0 && true) {
  78. cosine = -cosine;
  79. dst = -dst;
  80. } else {
  81. dst = dst;
  82. }
  83. if (Math::abs(cosine) < 1 - CMP_EPSILON) {
  84. // Standard case (slerp)
  85. real_t sine = Math::sqrt(1 - cosine*cosine);
  86. real_t angle = Math::atan2(sine, cosine);
  87. real_t inv_sine = 1.0f / sine;
  88. real_t coeff_0 = Math::sin((1.0f - t) * angle) * inv_sine;
  89. real_t coeff_1 = Math::sin(t * angle) * inv_sine;
  90. Quat ret= src * coeff_0 + dst * coeff_1;
  91. return ret;
  92. } else {
  93. // There are two situations:
  94. // 1. "rkP" and "q" are very close (cosine ~= +1), so we can do a linear
  95. // interpolation safely.
  96. // 2. "rkP" and "q" are almost invedste of each other (cosine ~= -1), there
  97. // are an infinite number of possibilities interpolation. but we haven't
  98. // have method to fix this case, so just use linear interpolation here.
  99. Quat ret = src * (1.0f - t) + dst *t;
  100. // taking the complement requires renormalisation
  101. ret.normalize();
  102. return ret;
  103. }
  104. #else
  105. real_t to1[4];
  106. real_t omega, cosom, sinom, scale0, scale1;
  107. // calc cosine
  108. cosom = x * q.x + y * q.y + z * q.z + w * q.w;
  109. // adjust signs (if necessary)
  110. if (cosom < 0.0) {
  111. cosom = -cosom;
  112. to1[0] = -q.x;
  113. to1[1] = -q.y;
  114. to1[2] = -q.z;
  115. to1[3] = -q.w;
  116. } else {
  117. to1[0] = q.x;
  118. to1[1] = q.y;
  119. to1[2] = q.z;
  120. to1[3] = q.w;
  121. }
  122. // calculate coefficients
  123. if ((1.0 - cosom) > CMP_EPSILON) {
  124. // standard case (slerp)
  125. omega = Math::acos(cosom);
  126. sinom = Math::sin(omega);
  127. scale0 = Math::sin((1.0 - t) * omega) / sinom;
  128. scale1 = Math::sin(t * omega) / sinom;
  129. } else {
  130. // "from" and "to" quaternions are very close
  131. // ... so we can do a linear interpolation
  132. scale0 = 1.0 - t;
  133. scale1 = t;
  134. }
  135. // calculate final values
  136. return Quat(
  137. scale0 * x + scale1 * to1[0],
  138. scale0 * y + scale1 * to1[1],
  139. scale0 * z + scale1 * to1[2],
  140. scale0 * w + scale1 * to1[3]);
  141. #endif
  142. }
  143. Quat Quat::slerpni(const Quat &q, const real_t &t) const {
  144. const Quat &from = *this;
  145. float dot = from.dot(q);
  146. if (Math::absf(dot) > 0.9999f) return from;
  147. float theta = Math::acos(dot),
  148. sinT = 1.0f / Math::sin(theta),
  149. newFactor = Math::sin(t * theta) * sinT,
  150. invFactor = Math::sin((1.0f - t) * theta) * sinT;
  151. return Quat(invFactor * from.x + newFactor * q.x,
  152. invFactor * from.y + newFactor * q.y,
  153. invFactor * from.z + newFactor * q.z,
  154. invFactor * from.w + newFactor * q.w);
  155. #if 0
  156. real_t to1[4];
  157. real_t omega, cosom, sinom, scale0, scale1;
  158. // calc cosine
  159. cosom = x * q.x + y * q.y + z * q.z
  160. + w * q.w;
  161. // adjust signs (if necessary)
  162. if ( cosom <0.0 && false) {
  163. cosom = -cosom; to1[0] = - q.x;
  164. to1[1] = - q.y;
  165. to1[2] = - q.z;
  166. to1[3] = - q.w;
  167. } else {
  168. to1[0] = q.x;
  169. to1[1] = q.y;
  170. to1[2] = q.z;
  171. to1[3] = q.w;
  172. }
  173. // calculate coefficients
  174. if ( (1.0 - cosom) > CMP_EPSILON ) {
  175. // standard case (slerp)
  176. omega = Math::acos(cosom);
  177. sinom = Math::sin(omega);
  178. scale0 = Math::sin((1.0 - t) * omega) / sinom;
  179. scale1 = Math::sin(t * omega) / sinom;
  180. } else {
  181. // "from" and "to" quaternions are very close
  182. // ... so we can do a linear interpolation
  183. scale0 = 1.0 - t;
  184. scale1 = t;
  185. }
  186. // calculate final values
  187. return Quat(
  188. scale0 * x + scale1 * to1[0],
  189. scale0 * y + scale1 * to1[1],
  190. scale0 * z + scale1 * to1[2],
  191. scale0 * w + scale1 * to1[3]
  192. );
  193. #endif
  194. }
  195. Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const {
  196. //the only way to do slerp :|
  197. float t2 = (1.0 - t) * t * 2;
  198. Quat sp = this->slerp(q, t);
  199. Quat sq = prep.slerpni(postq, t);
  200. return sp.slerpni(sq, t2);
  201. }
  202. Quat::operator String() const {
  203. return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w);
  204. }
  205. Quat::Quat(const Vector3 &axis, const real_t &angle) {
  206. real_t d = axis.length();
  207. if (d == 0)
  208. set(0, 0, 0, 0);
  209. else {
  210. real_t s = Math::sin(-angle * 0.5) / d;
  211. set(axis.x * s, axis.y * s, axis.z * s,
  212. Math::cos(-angle * 0.5));
  213. }
  214. }