Quaternion.cpp 7.1 KB

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