quaternion.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #pragma once
  24. #include "types.h"
  25. #include "vector3.h"
  26. #include "math_types.h"
  27. #include "matrix3x3.h"
  28. namespace crown
  29. {
  30. /// Negates the quaternion @a q and returns the result.
  31. Quaternion operator-(const Quaternion& q);
  32. /// Multiplies the quaternions @a and @a b. (i.e. rotates first by @a a then by @a b).
  33. Quaternion operator*(Quaternion a, const Quaternion& b);
  34. /// Multiplies the quaternion @a a by the scalar @a k.
  35. Quaternion operator*(const Quaternion& a, float k);
  36. /// Functions to manipulate Quaternion.
  37. ///
  38. /// @ingroup Math
  39. namespace quaternion
  40. {
  41. const Quaternion IDENTITY = Quaternion(0.0, 0.0, 0.0, 1.0);
  42. /// Returns the dot product between quaternions @a a and @a b.
  43. float dot(const Quaternion& a, const Quaternion& b);
  44. /// Returns the length of @a q.
  45. float length(const Quaternion& q);
  46. /// Returns the conjugate of quaternion @a q.
  47. Quaternion conjugate(const Quaternion& q);
  48. /// Returns the inverse of quaternion @a q.
  49. Quaternion inverse(const Quaternion& q);
  50. /// Returns the quaternion @a q raised to the power of @a exp.
  51. Quaternion power(const Quaternion& q, float exp);
  52. /// Returns the Matrix3x3 representation of the quaternion @a q.
  53. Matrix3x3 to_matrix3x3(const Quaternion& q);
  54. /// Returns the Matrix4x4 representation of the quaternion @a q.
  55. Matrix4x4 to_matrix4x4(const Quaternion& q);
  56. } // namespace quaternion
  57. inline Quaternion operator-(const Quaternion& q)
  58. {
  59. return Quaternion(-q.x, -q.y, -q.z, -q.w);
  60. }
  61. inline Quaternion operator*(Quaternion a, const Quaternion& b)
  62. {
  63. a *= b;
  64. return a;
  65. }
  66. inline Quaternion operator*(const Quaternion& a, float k)
  67. {
  68. return Quaternion(a.x * k, a.y * k, a.z * k, a.w * k);
  69. }
  70. namespace quaternion
  71. {
  72. //-----------------------------------------------------------------------------
  73. inline float dot(const Quaternion& a, const Quaternion& b)
  74. {
  75. return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
  76. }
  77. //-----------------------------------------------------------------------------
  78. inline float length(const Quaternion& q)
  79. {
  80. return math::sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
  81. }
  82. inline Quaternion conjugate(const Quaternion& q)
  83. {
  84. return Quaternion(-q.x, -q.y, -q.z, q.w);
  85. }
  86. inline Quaternion inverse(const Quaternion& q)
  87. {
  88. return conjugate(q) * (1.0f / length(q));
  89. }
  90. inline Quaternion power(const Quaternion& q, float exp)
  91. {
  92. if (math::abs(q.w) < 0.9999)
  93. {
  94. Quaternion tmp;
  95. float alpha = math::acos(q.w); // alpha = theta/2
  96. float new_alpha = alpha * exp;
  97. tmp.w = math::cos(new_alpha);
  98. float mult = math::sin(new_alpha) / math::sin(alpha);
  99. tmp.x = q.x * mult;
  100. tmp.y = q.y * mult;
  101. tmp.z = q.z * mult;
  102. return tmp;
  103. }
  104. return q;
  105. }
  106. inline Matrix3x3 to_matrix3x3(const Quaternion& q)
  107. {
  108. return Matrix3x3(q);
  109. }
  110. inline Matrix4x4 to_matrix4x4(const Quaternion& q)
  111. {
  112. return Matrix4x4(q, Vector3(0, 0, 0));
  113. }
  114. } // namespace quaternion
  115. //-----------------------------------------------------------------------------
  116. inline Quaternion::Quaternion()
  117. {
  118. // Do not initialize
  119. }
  120. //-----------------------------------------------------------------------------
  121. inline Quaternion::Quaternion(float nx, float ny, float nz, float nw)
  122. : x(nx)
  123. , y(ny)
  124. , z(nz)
  125. , w(nw)
  126. {
  127. }
  128. //-----------------------------------------------------------------------------
  129. inline Quaternion::Quaternion(const Vector3& axis, float angle)
  130. : x(axis.x * math::sin(angle * 0.5f))
  131. , y(axis.y * math::sin(angle * 0.5f))
  132. , z(axis.z * math::sin(angle * 0.5f))
  133. , w(math::cos(angle * 0.5f))
  134. {
  135. }
  136. //-----------------------------------------------------------------------------
  137. inline float& Quaternion::operator[](uint32_t i)
  138. {
  139. CE_ASSERT(i < 4, "Index out of bounds");
  140. return (&x)[i];
  141. }
  142. //-----------------------------------------------------------------------------
  143. inline const float& Quaternion::operator[](uint32_t i) const
  144. {
  145. CE_ASSERT(i < 4, "Index out of bounds");
  146. return (&x)[i];
  147. }
  148. //-----------------------------------------------------------------------------
  149. inline Quaternion& Quaternion::operator*=(const Quaternion& a)
  150. {
  151. const float t_x = w * a.x + x * a.w + z * a.y - y * a.z;
  152. const float t_y = w * a.y + y * a.w + x * a.z - z * a.x;
  153. const float t_z = w * a.z + z * a.w + y * a.x - x * a.y;
  154. const float t_w = w * a.w - x * a.x - y * a.y - z * a.z;
  155. x = t_x;
  156. y = t_y;
  157. z = t_z;
  158. w = t_w;
  159. return *this;
  160. }
  161. } // namespace crown