Quaternion.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "Matrix3x3.h"
  24. #include "Matrix4x4.h"
  25. #include "Types.h"
  26. #include "MathUtils.h"
  27. #include "Quaternion.h"
  28. #include "Vector3.h"
  29. namespace crown
  30. {
  31. const Quaternion Quaternion::IDENTITY = Quaternion(0.0, 0.0, 0.0, 1.0);
  32. //-----------------------------------------------------------------------------
  33. Quaternion::Quaternion()
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. Quaternion::Quaternion(float nx, float ny, float nz, float nw) :
  38. v(nx, ny, nz),
  39. w(nw)
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. Quaternion::Quaternion(const Vector3& axis, float angle) :
  44. v(axis * math::sin(angle * 0.5)),
  45. w(math::cos(angle * 0.5))
  46. {
  47. }
  48. //-----------------------------------------------------------------------------
  49. void Quaternion::negate()
  50. {
  51. w = -w;
  52. v = -v;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void Quaternion::load_identity()
  56. {
  57. w = 1.0;
  58. v.x = 0.0;
  59. v.y = 0.0;
  60. v.z = 0.0;
  61. }
  62. //-----------------------------------------------------------------------------
  63. float Quaternion::length() const
  64. {
  65. return math::sqrt(w * w + v.x * v.x + v.y * v.y + v.z * v.z);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void Quaternion::conjugate()
  69. {
  70. v = -v;
  71. }
  72. //-----------------------------------------------------------------------------
  73. Quaternion Quaternion::get_conjugate() const
  74. {
  75. return Quaternion(-v, w);
  76. }
  77. //-----------------------------------------------------------------------------
  78. Quaternion Quaternion::get_inverse() const
  79. {
  80. return get_conjugate() * (1.0 / length());
  81. }
  82. //-----------------------------------------------------------------------------
  83. Matrix3x3 Quaternion::to_matrix3x3() const
  84. {
  85. const float x = v.x;
  86. const float y = v.y;
  87. const float z = v.z;
  88. Matrix3x3 tmp;
  89. tmp.m[0] = (float)(1.0 - 2.0*y*y - 2.0*z*z);
  90. tmp.m[1] = (float)(2.0*x*y + 2.0*w*z);
  91. tmp.m[2] = (float)(2.0*x*z - 2.0*w*y);
  92. tmp.m[3] = (float)(2.0*x*y - 2.0*w*z);
  93. tmp.m[4] = (float)(1.0 - 2.0*x*x - 2.0*z*z);
  94. tmp.m[5] = (float)(2.0*y*z + 2.0*w*x);
  95. tmp.m[6] = (float)(2.0*x*z + 2.0*w*y);
  96. tmp.m[7] = (float)(2.0*y*z - 2.0*w*x);
  97. tmp.m[8] = (float)(1.0 - 2.0*x*x - 2.0*y*y);
  98. return tmp;
  99. }
  100. //-----------------------------------------------------------------------------
  101. Matrix4x4 Quaternion::to_matrix4x4() const
  102. {
  103. const float x = v.x;
  104. const float y = v.y;
  105. const float z = v.z;
  106. Matrix4x4 tmp;
  107. tmp.m[0] = (float)(1.0 - 2.0*y*y - 2.0*z*z);
  108. tmp.m[1] = (float)(2.0*x*y + 2.0*w*z);
  109. tmp.m[2] = (float)(2.0*x*z - 2.0*w*y);
  110. tmp.m[3] = 0;
  111. tmp.m[4] = (float)(2.0*x*y - 2.0*w*z);
  112. tmp.m[5] = (float)(1.0 - 2.0*x*x - 2.0*z*z);
  113. tmp.m[6] = (float)(2.0*y*z + 2.0*w*x);
  114. tmp.m[7] = 0.0;
  115. tmp.m[8] = (float)(2.0*x*z + 2.0*w*y);
  116. tmp.m[9] = (float)(2.0*y*z - 2.0*w*x);
  117. tmp.m[10] = (float)(1.0 - 2.0*x*x - 2.0*y*y);
  118. tmp.m[11] = 0.0;
  119. tmp.m[12] = 0.0;
  120. tmp.m[13] = 0.0;
  121. tmp.m[14] = 0.0;
  122. tmp.m[15] = 1.0;
  123. return tmp;
  124. }
  125. //-----------------------------------------------------------------------------
  126. Quaternion Quaternion::operator*(const Quaternion& b) const
  127. {
  128. Quaternion tmp;
  129. tmp.w = w * b.w - vector3::dot(v, b.v);
  130. tmp.v = w * b.v + b.w * v + vector3::cross(b.v, v);
  131. return tmp;
  132. }
  133. //-----------------------------------------------------------------------------
  134. Quaternion Quaternion::operator*(const float& k) const
  135. {
  136. Quaternion tmp;
  137. tmp.w = w * k;
  138. tmp.v = v * k;
  139. return tmp;
  140. }
  141. //-----------------------------------------------------------------------------
  142. Quaternion Quaternion::power(float exp)
  143. {
  144. Quaternion tmp;
  145. if (math::abs(w) < 0.9999)
  146. {
  147. float alpha = math::acos(w); // alpha = theta/2
  148. float newAlpha = alpha * exp;
  149. tmp.w = math::cos(newAlpha);
  150. float mult = math::sin(newAlpha) / math::sin(alpha);
  151. tmp.v.x = v.x * mult;
  152. tmp.v.y = v.y * mult;
  153. tmp.v.z = v.z * mult;
  154. return tmp;
  155. }
  156. tmp.w = w;
  157. tmp.v = v;
  158. return tmp;
  159. }
  160. /*
  161. The geometric interpretation of the Quaternion dot product is similar to the interpretation of
  162. the vector dot product; the larger the absolute value of the Quaternion dot product axb, the more
  163. "similar" the angular displacements represented by a and b.
  164. */
  165. //-----------------------------------------------------------------------------
  166. float dot(const Quaternion& a, const Quaternion& b)
  167. {
  168. return a.w * b.w + vector3::dot(a.v, b.v);
  169. }
  170. // Spherical Linear interpolation
  171. //-----------------------------------------------------------------------------
  172. Quaternion slerp(const Quaternion& start, const Quaternion& end, float t)
  173. {
  174. Quaternion delta = end * start.get_inverse();
  175. delta = delta.power(t);
  176. return delta * start;
  177. }
  178. } // namespace crown