Quat.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Mat3.h"
  24. #include "Mat4.h"
  25. #include "Types.h"
  26. #include "MathUtils.h"
  27. #include "Quat.h"
  28. #include "Vec3.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. Quat::Quat()
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. Quat::Quat(float angle, const Vec3& v)
  37. {
  38. this->w = math::cos((float)(angle * 0.5));
  39. this->v = v * math::sin((float)(angle * 0.5));
  40. }
  41. //-----------------------------------------------------------------------------
  42. void Quat::negate()
  43. {
  44. w = -w;
  45. v.negate();
  46. }
  47. //-----------------------------------------------------------------------------
  48. void Quat::load_identity()
  49. {
  50. w = 1.0;
  51. v.x = 0.0;
  52. v.y = 0.0;
  53. v.z = 0.0;
  54. }
  55. //-----------------------------------------------------------------------------
  56. float Quat::length() const
  57. {
  58. return math::sqrt(w * w + v.x * v.x + v.y * v.y + v.z * v.z);
  59. }
  60. //-----------------------------------------------------------------------------
  61. void Quat::conjugate()
  62. {
  63. v = -v;
  64. }
  65. //-----------------------------------------------------------------------------
  66. Quat Quat::get_conjugate() const
  67. {
  68. return Quat(w, -v);
  69. }
  70. //-----------------------------------------------------------------------------
  71. Quat Quat::get_inverse() const
  72. {
  73. return get_conjugate() * ((float)(1.0 / length()));
  74. }
  75. //-----------------------------------------------------------------------------
  76. Mat3 Quat::to_mat3() const
  77. {
  78. Mat3 tmp;
  79. float x = v.x;
  80. float y = v.y;
  81. float z = v.z;
  82. tmp.m[0] = (float)(1.0 - 2.0*y*y - 2.0*z*z);
  83. tmp.m[1] = (float)(2.0*x*y + 2.0*w*z);
  84. tmp.m[2] = (float)(2.0*x*z - 2.0*w*y);
  85. tmp.m[3] = (float)(2.0*x*y - 2.0*w*z);
  86. tmp.m[4] = (float)(1.0 - 2.0*x*x - 2.0*z*z);
  87. tmp.m[5] = (float)(2.0*y*z + 2.0*w*x);
  88. tmp.m[6] = (float)(2.0*x*z + 2.0*w*y);
  89. tmp.m[7] = (float)(2.0*y*z - 2.0*w*x);
  90. tmp.m[8] = (float)(1.0 - 2.0*x*x - 2.0*y*y);
  91. return tmp;
  92. }
  93. //-----------------------------------------------------------------------------
  94. Mat4 Quat::to_mat4() const
  95. {
  96. Mat4 tmp;
  97. float x = v.x;
  98. float y = v.y;
  99. float z = v.z;
  100. tmp.m[0] = (float)(1.0 - 2.0*y*y - 2.0*z*z);
  101. tmp.m[1] = (float)(2.0*x*y + 2.0*w*z);
  102. tmp.m[2] = (float)(2.0*x*z - 2.0*w*y);
  103. tmp.m[3] = 0;
  104. tmp.m[4] = (float)(2.0*x*y - 2.0*w*z);
  105. tmp.m[5] = (float)(1.0 - 2.0*x*x - 2.0*z*z);
  106. tmp.m[6] = (float)(2.0*y*z + 2.0*w*x);
  107. tmp.m[7] = 0.0;
  108. tmp.m[8] = (float)(2.0*x*z + 2.0*w*y);
  109. tmp.m[9] = (float)(2.0*y*z - 2.0*w*x);
  110. tmp.m[10] = (float)(1.0 - 2.0*x*x - 2.0*y*y);
  111. tmp.m[11] = 0.0;
  112. tmp.m[12] = 0.0;
  113. tmp.m[13] = 0.0;
  114. tmp.m[14] = 0.0;
  115. tmp.m[15] = 1.0;
  116. return tmp;
  117. }
  118. //-----------------------------------------------------------------------------
  119. Quat Quat::operator*(const Quat& b) const
  120. {
  121. Quat tmp;
  122. tmp.w = w * b.w - v.dot(b.v);
  123. tmp.v = w * b.v + b.w * v + b.v.cross(v);
  124. return tmp;
  125. }
  126. //-----------------------------------------------------------------------------
  127. Quat Quat::operator*(const float& k) const
  128. {
  129. Quat tmp;
  130. tmp.w = w * k;
  131. tmp.v = v * k;
  132. return tmp;
  133. }
  134. //-----------------------------------------------------------------------------
  135. Quat Quat::power(float exp)
  136. {
  137. Quat tmp;
  138. if (math::abs(w) < 0.9999)
  139. {
  140. float alpha = math::acos(w); // alpha = theta/2
  141. float newAlpha = alpha * exp;
  142. tmp.w = math::cos(newAlpha);
  143. float mult = math::sin(newAlpha) / math::sin(alpha);
  144. tmp.v.x = v.x * mult;
  145. tmp.v.y = v.y * mult;
  146. tmp.v.z = v.z * mult;
  147. return tmp;
  148. }
  149. tmp.w = w;
  150. tmp.v = v;
  151. return tmp;
  152. }
  153. /*
  154. The geometric interpretation of the Quat dot product is similar to the int32_terpretation of
  155. the vector dot product; the larger the absolute value of the Quat dot product axb, the more
  156. "similar" the angular displacements represented by a and b.
  157. */
  158. //-----------------------------------------------------------------------------
  159. float dot(const Quat& a, const Quat& b)
  160. {
  161. return a.w * b.w + a.v.dot(b.v);
  162. }
  163. // Spherical Linear intERPolation
  164. //-----------------------------------------------------------------------------
  165. Quat slerp(const Quat& start, const Quat& end, float t)
  166. {
  167. Quat delta = end * start.get_inverse();
  168. delta = delta.power(t);
  169. return delta * start;
  170. }
  171. } // namespace crown