Quaternion.cpp 7.9 KB

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