Quaternion.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Quaternion.h"
  25. #include <cstdio>
  26. const Quaternion Quaternion::IDENTITY;
  27. Quaternion::Quaternion(float angle, const Vector3& axis)
  28. {
  29. Vector3 normAxis = axis.GetNormalized();
  30. float sinAngle = sinf((angle * M_DEGTORAD) * 0.5f);
  31. float cosAngle = cosf((angle * M_DEGTORAD) * 0.5f);
  32. w_ = cosAngle;
  33. x_ = normAxis.x_ * sinAngle;
  34. y_ = normAxis.y_ * sinAngle;
  35. z_ = normAxis.z_ * sinAngle;
  36. }
  37. Quaternion::Quaternion(float x, float y, float z)
  38. {
  39. // Order of rotations: Z first, then X, then Y (mimics typical FPS camera with gimbal lock at top/bottom)
  40. float sinX = sinf((x * M_DEGTORAD) * 0.5f);
  41. float cosX = cosf((x * M_DEGTORAD) * 0.5f);
  42. float sinY = sinf((y * M_DEGTORAD) * 0.5f);
  43. float cosY = cosf((y * M_DEGTORAD) * 0.5f);
  44. float sinZ = sinf((z * M_DEGTORAD) * 0.5f);
  45. float cosZ = cosf((z * M_DEGTORAD) * 0.5f);
  46. w_ = cosY * cosX * cosZ + sinY * sinX * sinZ;
  47. x_ = cosY * sinX * cosZ + sinY * cosX * sinZ;
  48. y_ = sinY * cosX * cosZ - cosY * sinX * sinZ;
  49. z_ = cosY * cosX * sinZ - sinY * sinX * cosZ;
  50. }
  51. Quaternion::Quaternion(const Vector3& start, const Vector3& end)
  52. {
  53. Vector3 normStart = start.GetNormalized();
  54. Vector3 normEnd = end.GetNormalized();
  55. float d = normStart.DotProduct(normEnd);
  56. if (d > -1.0f + M_EPSILON)
  57. {
  58. Vector3 c = normStart.CrossProduct(normEnd);
  59. float s = sqrtf((1.0f + d) * 2.0f);
  60. float invS = 1.0f / s;
  61. x_ = c.x_ * invS;
  62. y_ = c.y_ * invS;
  63. z_ = c.z_ * invS;
  64. w_ = 0.5f * s;
  65. }
  66. else
  67. {
  68. Vector3 axis = Vector3::RIGHT.CrossProduct(normStart);
  69. if (axis.GetLength() < M_EPSILON)
  70. axis = Vector3::UP.CrossProduct(normStart);
  71. float angle = 180.0f;
  72. Vector3 normAxis = axis.GetNormalized();
  73. float sinAngle = sinf((angle * M_DEGTORAD) * 0.5f);
  74. float cosAngle = cosf((angle * M_DEGTORAD) * 0.5f);
  75. w_ = cosAngle;
  76. x_ = normAxis.x_ * sinAngle;
  77. y_ = normAxis.y_ * sinAngle;
  78. z_ = normAxis.z_ * sinAngle;
  79. }
  80. }
  81. Quaternion::Quaternion(const Matrix3& matrix)
  82. {
  83. float t = matrix.m00_ + matrix.m11_ + matrix.m22_ + 1.0f;
  84. if (t > 0.001f)
  85. {
  86. float s = sqrtf(t) * 2.0f;
  87. float invS = 1.0f / s;
  88. x_ = (matrix.m21_ - matrix.m12_) * invS;
  89. y_ = (matrix.m02_ - matrix.m20_) * invS;
  90. z_ = (matrix.m10_ - matrix.m01_) * invS;
  91. w_ = 0.25f * s;
  92. }
  93. else
  94. {
  95. if ((matrix.m00_ > matrix.m11_) && (matrix.m00_ > matrix.m22_))
  96. {
  97. float s = sqrtf(1.0f + matrix.m00_ - matrix.m11_ - matrix.m22_) * 2.0f;
  98. float invS = 1.0f / s;
  99. x_ = 0.25f * s;
  100. y_ = (matrix.m01_ + matrix.m10_) * invS;
  101. z_ = (matrix.m20_ + matrix.m02_) * invS;
  102. w_ = (matrix.m21_ - matrix.m12_) * invS;
  103. }
  104. else if (matrix.m11_ > matrix.m22_)
  105. {
  106. float s = sqrtf(1.0f + matrix.m11_ - matrix.m00_ - matrix.m22_) * 2.0f;
  107. float invS = 1.0f / s;
  108. x_ = (matrix.m01_ + matrix.m10_) * invS;
  109. y_ = 0.25f * s;
  110. z_ = (matrix.m12_ + matrix.m21_) * invS;
  111. w_ = (matrix.m02_ - matrix.m20_) * invS;
  112. }
  113. else
  114. {
  115. float s = sqrtf(1.0f + matrix.m22_ - matrix.m00_ - matrix.m11_) * 2.0f;
  116. float invS = 1.0f / s;
  117. x_ = (matrix.m02_ + matrix.m20_) * invS;
  118. y_ = (matrix.m12_ + matrix.m21_) * invS;
  119. z_ = 0.25f * s;
  120. w_ = (matrix.m10_ - matrix.m01_) * invS;
  121. }
  122. }
  123. }
  124. Vector3 Quaternion::GetEulerAngles() const
  125. {
  126. // Derivation from http://www.geometrictools.com/Documentation/EulerAngles.pdf
  127. // Order of rotations: Z first, then X, then Y
  128. float check = 2.0f * (-y_ * z_ + w_ * x_);
  129. if (check < -0.995f)
  130. {
  131. return Vector3(
  132. -90.0f,
  133. 0.0f,
  134. -atan2f(2.0f * (x_ * z_ - w_ * y_), 1.0f - 2.0f * (y_ * y_ + z_ * z_)) * M_RADTODEG
  135. );
  136. }
  137. else if (check > 0.995f)
  138. {
  139. return Vector3(
  140. 90.0f,
  141. 0.0f,
  142. atan2f(2.0f * (x_ * z_ - w_ * y_), 1.0f - 2.0f * (y_ * y_ + z_ * z_)) * M_RADTODEG
  143. );
  144. }
  145. else
  146. {
  147. return Vector3(
  148. asinf(check) * M_RADTODEG,
  149. atan2f(2.0f * (x_ * z_ + w_ * y_), 1.0f - 2.0f * (x_ * x_ + y_ * y_)) * M_RADTODEG,
  150. atan2f(2.0f * (x_ * y_ + w_ * z_), 1.0f - 2.0f * (x_ * x_ + z_ * z_)) * M_RADTODEG
  151. );
  152. }
  153. }
  154. float Quaternion::GetYaw() const
  155. {
  156. return GetEulerAngles().y_;
  157. }
  158. float Quaternion::GetPitch() const
  159. {
  160. return GetEulerAngles().x_;
  161. }
  162. float Quaternion::GetRoll() const
  163. {
  164. return GetEulerAngles().z_;
  165. }
  166. Matrix3 Quaternion::GetRotationMatrix() const
  167. {
  168. return Matrix3(
  169. 1.0f - 2.0f * y_ * y_ - 2.0f * z_ * z_,
  170. 2.0f * x_ * y_ - 2.0f * w_ * z_,
  171. 2.0f * x_ * z_ + 2.0f * w_ * y_,
  172. 2.0f * x_ * y_ + 2.0f * w_ * z_,
  173. 1.0f - 2.0f * x_ * x_ - 2.0f * z_ * z_,
  174. 2.0f * y_ * z_ - 2.0f * w_ * x_,
  175. 2.0f * x_ * z_ - 2.0f * w_ * y_,
  176. 2.0f * y_ * z_ + 2.0f * w_ * x_,
  177. 1.0f - 2.0f * x_ * x_ - 2.0f * y_ * y_
  178. );
  179. }
  180. Quaternion Quaternion::Slerp(Quaternion rhs, float t) const
  181. {
  182. float cosAngle = DotProduct(rhs);
  183. // Enable shortest path rotation
  184. if (cosAngle < 0.0f)
  185. {
  186. cosAngle = -cosAngle;
  187. rhs = -rhs;
  188. }
  189. float angle = acosf(cosAngle);
  190. float sinAngle = sinf(angle);
  191. float t1, t2;
  192. if (sinAngle > 0.001f)
  193. {
  194. t1 = sinf((1.0f - t) * angle) / sinAngle;
  195. t2 = sinf(t * angle) / sinAngle;
  196. }
  197. else
  198. {
  199. t1 = 1.0f - t;
  200. t2 = t;
  201. }
  202. return *this * t1 + rhs * t2;
  203. }
  204. String Quaternion::ToString() const
  205. {
  206. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  207. sprintf(tempBuffer, "%g %g %g %g", w_, x_, y_, z_);
  208. return String(tempBuffer);
  209. }