2
0

quat.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*************************************************************************/
  2. /* quat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "quat.h"
  30. #include "print_string.h"
  31. void Quat::set_euler(const Vector3& p_euler) {
  32. real_t half_yaw = p_euler.x * 0.5;
  33. real_t half_pitch = p_euler.y * 0.5;
  34. real_t half_roll = p_euler.z * 0.5;
  35. real_t cos_yaw = Math::cos(half_yaw);
  36. real_t sin_yaw = Math::sin(half_yaw);
  37. real_t cos_pitch = Math::cos(half_pitch);
  38. real_t sin_pitch = Math::sin(half_pitch);
  39. real_t cos_roll = Math::cos(half_roll);
  40. real_t sin_roll = Math::sin(half_roll);
  41. set(cos_roll * sin_pitch * cos_yaw+sin_roll * cos_pitch * sin_yaw,
  42. cos_roll * cos_pitch * sin_yaw - sin_roll * sin_pitch * cos_yaw,
  43. sin_roll * cos_pitch * cos_yaw - cos_roll * sin_pitch * sin_yaw,
  44. cos_roll * cos_pitch * cos_yaw+sin_roll * sin_pitch * sin_yaw);
  45. }
  46. void Quat::operator*=(const Quat& q) {
  47. set(w * q.x+x * q.w+y * q.z - z * q.y,
  48. w * q.y+y * q.w+z * q.x - x * q.z,
  49. w * q.z+z * q.w+x * q.y - y * q.x,
  50. w * q.w - x * q.x - y * q.y - z * q.z);
  51. }
  52. Quat Quat::operator*(const Quat& q) const {
  53. Quat r=*this;
  54. r*=q;
  55. return r;
  56. }
  57. real_t Quat::length() const {
  58. return Math::sqrt(length_squared());
  59. }
  60. void Quat::normalize() {
  61. *this /= length();
  62. }
  63. Quat Quat::normalized() const {
  64. return *this / length();
  65. }
  66. Quat Quat::inverse() const {
  67. return Quat( -x, -y, -z, w );
  68. }
  69. Quat Quat::slerp(const Quat& q, const real_t& t) const {
  70. #if 0
  71. Quat dst=q;
  72. Quat src=*this;
  73. src.normalize();
  74. dst.normalize();
  75. real_t cosine = dst.dot(src);
  76. if (cosine < 0 && true) {
  77. cosine = -cosine;
  78. dst = -dst;
  79. } else {
  80. dst = dst;
  81. }
  82. if (Math::abs(cosine) < 1 - CMP_EPSILON) {
  83. // Standard case (slerp)
  84. real_t sine = Math::sqrt(1 - cosine*cosine);
  85. real_t angle = Math::atan2(sine, cosine);
  86. real_t inv_sine = 1.0f / sine;
  87. real_t coeff_0 = Math::sin((1.0f - t) * angle) * inv_sine;
  88. real_t coeff_1 = Math::sin(t * angle) * inv_sine;
  89. Quat ret= src * coeff_0 + dst * coeff_1;
  90. return ret;
  91. } else {
  92. // There are two situations:
  93. // 1. "rkP" and "q" are very close (cosine ~= +1), so we can do a linear
  94. // interpolation safely.
  95. // 2. "rkP" and "q" are almost invedste of each other (cosine ~= -1), there
  96. // are an infinite number of possibilities interpolation. but we haven't
  97. // have method to fix this case, so just use linear interpolation here.
  98. Quat ret = src * (1.0f - t) + dst *t;
  99. // taking the complement requires renormalisation
  100. ret.normalize();
  101. return ret;
  102. }
  103. #else
  104. real_t to1[4];
  105. real_t omega, cosom, sinom, scale0, scale1;
  106. // calc cosine
  107. cosom = x * q.x + y * q.y + z * q.z
  108. + w * q.w;
  109. // adjust signs (if necessary)
  110. if ( cosom <0.0 ) {
  111. cosom = -cosom; to1[0] = - q.x;
  112. to1[1] = - q.y;
  113. to1[2] = - q.z;
  114. to1[3] = - q.w;
  115. } else {
  116. to1[0] = q.x;
  117. to1[1] = q.y;
  118. to1[2] = q.z;
  119. to1[3] = q.w;
  120. }
  121. // calculate coefficients
  122. if ( (1.0 - cosom) > CMP_EPSILON ) {
  123. // standard case (slerp)
  124. omega = Math::acos(cosom);
  125. sinom = Math::sin(omega);
  126. scale0 = Math::sin((1.0 - t) * omega) / sinom;
  127. scale1 = Math::sin(t * omega) / sinom;
  128. } else {
  129. // "from" and "to" quaternions are very close
  130. // ... so we can do a linear interpolation
  131. scale0 = 1.0 - t;
  132. scale1 = t;
  133. }
  134. // calculate final values
  135. return Quat(
  136. scale0 * x + scale1 * to1[0],
  137. scale0 * y + scale1 * to1[1],
  138. scale0 * z + scale1 * to1[2],
  139. scale0 * w + scale1 * to1[3]
  140. );
  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. }