Quaternion.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  27. {
  28. const Quaternion Quaternion::IDENTITY;
  29. void Quaternion::FromAngleAxis(float angle, const Vector3& axis)
  30. {
  31. Vector3 normAxis = axis.Normalized();
  32. float sinAngle = sinf((angle * M_DEGTORAD) * 0.5f);
  33. float cosAngle = cosf((angle * M_DEGTORAD) * 0.5f);
  34. w_ = cosAngle;
  35. x_ = normAxis.x_ * sinAngle;
  36. y_ = normAxis.y_ * sinAngle;
  37. z_ = normAxis.z_ * sinAngle;
  38. }
  39. void Quaternion::FromEulerAngles(float x, float y, float z)
  40. {
  41. // Order of rotations: Z first, then X, then Y (mimics typical FPS camera with gimbal lock at top/bottom)
  42. float sinX = sinf((x * M_DEGTORAD) * 0.5f);
  43. float cosX = cosf((x * M_DEGTORAD) * 0.5f);
  44. float sinY = sinf((y * M_DEGTORAD) * 0.5f);
  45. float cosY = cosf((y * M_DEGTORAD) * 0.5f);
  46. float sinZ = sinf((z * M_DEGTORAD) * 0.5f);
  47. float cosZ = cosf((z * M_DEGTORAD) * 0.5f);
  48. w_ = cosY * cosX * cosZ + sinY * sinX * sinZ;
  49. x_ = cosY * sinX * cosZ + sinY * cosX * sinZ;
  50. y_ = sinY * cosX * cosZ - cosY * sinX * sinZ;
  51. z_ = cosY * cosX * sinZ - sinY * sinX * cosZ;
  52. }
  53. void Quaternion::FromRotationTo(const Vector3& start, const Vector3& end)
  54. {
  55. Vector3 normStart = start.Normalized();
  56. Vector3 normEnd = end.Normalized();
  57. float d = normStart.DotProduct(normEnd);
  58. if (d > -1.0f + M_EPSILON)
  59. {
  60. Vector3 c = normStart.CrossProduct(normEnd);
  61. float s = sqrtf((1.0f + d) * 2.0f);
  62. float invS = 1.0f / s;
  63. x_ = c.x_ * invS;
  64. y_ = c.y_ * invS;
  65. z_ = c.z_ * invS;
  66. w_ = 0.5f * s;
  67. }
  68. else
  69. {
  70. Vector3 axis = Vector3::RIGHT.CrossProduct(normStart);
  71. if (axis.Length() < M_EPSILON)
  72. axis = Vector3::UP.CrossProduct(normStart);
  73. float angle = 180.0f;
  74. Vector3 normAxis = axis.Normalized();
  75. float sinAngle = sinf((angle * M_DEGTORAD) * 0.5f);
  76. float cosAngle = cosf((angle * M_DEGTORAD) * 0.5f);
  77. w_ = cosAngle;
  78. x_ = normAxis.x_ * sinAngle;
  79. y_ = normAxis.y_ * sinAngle;
  80. z_ = normAxis.z_ * sinAngle;
  81. }
  82. }
  83. void Quaternion::FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
  84. {
  85. Matrix3 matrix(
  86. xAxis.x_, yAxis.x_, zAxis.x_,
  87. xAxis.y_, yAxis.y_, zAxis.y_,
  88. xAxis.z_, yAxis.z_, zAxis.z_
  89. );
  90. FromRotationMatrix(matrix);
  91. }
  92. void Quaternion::FromRotationMatrix(const Matrix3& matrix)
  93. {
  94. float t = matrix.m00_ + matrix.m11_ + matrix.m22_;
  95. if (t > 0.0f)
  96. {
  97. float s = 0.5f / sqrtf(1.0f + t);
  98. x_ = (matrix.m21_ - matrix.m12_) * s;
  99. y_ = (matrix.m02_ - matrix.m20_) * s;
  100. z_ = (matrix.m10_ - matrix.m01_) * s;
  101. w_ = 0.25f / s;
  102. }
  103. else
  104. {
  105. if (matrix.m00_ > matrix.m11_ && matrix.m00_ > matrix.m22_)
  106. {
  107. float s = sqrtf(1.0f + matrix.m00_ - matrix.m11_ - matrix.m22_) * 2.0f;
  108. float invS = 1.0f / s;
  109. x_ = 0.25f * s;
  110. y_ = (matrix.m01_ + matrix.m10_) * invS;
  111. z_ = (matrix.m20_ + matrix.m02_) * invS;
  112. w_ = (matrix.m21_ - matrix.m12_) * invS;
  113. }
  114. else if (matrix.m11_ > matrix.m22_)
  115. {
  116. float s = sqrtf(1.0f + matrix.m11_ - matrix.m00_ - matrix.m22_) * 2.0f;
  117. float invS = 1.0f / s;
  118. x_ = (matrix.m01_ + matrix.m10_) * invS;
  119. y_ = 0.25f * s;
  120. z_ = (matrix.m12_ + matrix.m21_) * invS;
  121. w_ = (matrix.m02_ - matrix.m20_) * invS;
  122. }
  123. else
  124. {
  125. float s = sqrtf(1.0f + matrix.m22_ - matrix.m00_ - matrix.m11_) * 2.0f;
  126. float invS = 1.0f / s;
  127. x_ = (matrix.m02_ + matrix.m20_) * invS;
  128. y_ = (matrix.m12_ + matrix.m21_) * invS;
  129. z_ = 0.25f * s;
  130. w_ = (matrix.m10_ - matrix.m01_) * invS;
  131. }
  132. }
  133. }
  134. Vector3 Quaternion::EulerAngles() const
  135. {
  136. // Derivation from http://www.geometrictools.com/Documentation/EulerAngles.pdf
  137. // Order of rotations: Z first, then X, then Y
  138. float check = 2.0f * (-y_ * z_ + w_ * x_);
  139. if (check < -0.995f)
  140. {
  141. return Vector3(
  142. -90.0f,
  143. 0.0f,
  144. -atan2f(2.0f * (x_ * z_ - w_ * y_), 1.0f - 2.0f * (y_ * y_ + z_ * z_)) * M_RADTODEG
  145. );
  146. }
  147. else if (check > 0.995f)
  148. {
  149. return Vector3(
  150. 90.0f,
  151. 0.0f,
  152. atan2f(2.0f * (x_ * z_ - w_ * y_), 1.0f - 2.0f * (y_ * y_ + z_ * z_)) * M_RADTODEG
  153. );
  154. }
  155. else
  156. {
  157. return Vector3(
  158. asinf(check) * M_RADTODEG,
  159. atan2f(2.0f * (x_ * z_ + w_ * y_), 1.0f - 2.0f * (x_ * x_ + y_ * y_)) * M_RADTODEG,
  160. atan2f(2.0f * (x_ * y_ + w_ * z_), 1.0f - 2.0f * (x_ * x_ + z_ * z_)) * M_RADTODEG
  161. );
  162. }
  163. }
  164. float Quaternion::YawAngle() const
  165. {
  166. return EulerAngles().y_;
  167. }
  168. float Quaternion::PitchAngle() const
  169. {
  170. return EulerAngles().x_;
  171. }
  172. float Quaternion::RollAngle() const
  173. {
  174. return EulerAngles().z_;
  175. }
  176. Matrix3 Quaternion::RotationMatrix() const
  177. {
  178. return Matrix3(
  179. 1.0f - 2.0f * y_ * y_ - 2.0f * z_ * z_,
  180. 2.0f * x_ * y_ - 2.0f * w_ * z_,
  181. 2.0f * x_ * z_ + 2.0f * w_ * y_,
  182. 2.0f * x_ * y_ + 2.0f * w_ * z_,
  183. 1.0f - 2.0f * x_ * x_ - 2.0f * z_ * z_,
  184. 2.0f * y_ * z_ - 2.0f * w_ * x_,
  185. 2.0f * x_ * z_ - 2.0f * w_ * y_,
  186. 2.0f * y_ * z_ + 2.0f * w_ * x_,
  187. 1.0f - 2.0f * x_ * x_ - 2.0f * y_ * y_
  188. );
  189. }
  190. Quaternion Quaternion::Slerp(Quaternion rhs, float t) const
  191. {
  192. float cosAngle = DotProduct(rhs);
  193. // Enable shortest path rotation
  194. if (cosAngle < 0.0f)
  195. {
  196. cosAngle = -cosAngle;
  197. rhs = -rhs;
  198. }
  199. float angle = acosf(cosAngle);
  200. float sinAngle = sinf(angle);
  201. float t1, t2;
  202. if (sinAngle > 0.001f)
  203. {
  204. float invSinAngle = 1.0f / sinAngle;
  205. t1 = sinf((1.0f - t) * angle) * invSinAngle;
  206. t2 = sinf(t * angle) * invSinAngle;
  207. }
  208. else
  209. {
  210. t1 = 1.0f - t;
  211. t2 = t;
  212. }
  213. return *this * t1 + rhs * t2;
  214. }
  215. String Quaternion::ToString() const
  216. {
  217. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  218. sprintf(tempBuffer, "%g %g %g %g", w_, x_, y_, z_);
  219. return String(tempBuffer);
  220. }
  221. }