Quaternion.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Normalized();
  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.Normalized();
  54. Vector3 normEnd = end.Normalized();
  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.Length() < M_EPSILON)
  70. axis = Vector3::UP.CrossProduct(normStart);
  71. float angle = 180.0f;
  72. Vector3 normAxis = axis.Normalized();
  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_;
  84. if (t > 0.0f)
  85. {
  86. float s = 0.5f / sqrtf(1.0f + t);
  87. x_ = (matrix.m21_ - matrix.m12_) * s;
  88. y_ = (matrix.m02_ - matrix.m20_) * s;
  89. z_ = (matrix.m10_ - matrix.m01_) * s;
  90. w_ = 0.25f / s;
  91. }
  92. else
  93. {
  94. if (matrix.m00_ > matrix.m11_ && matrix.m00_ > matrix.m22_)
  95. {
  96. float s = sqrtf(1.0f + matrix.m00_ - matrix.m11_ - matrix.m22_) * 2.0f;
  97. float invS = 1.0f / s;
  98. x_ = 0.25f * s;
  99. y_ = (matrix.m01_ + matrix.m10_) * invS;
  100. z_ = (matrix.m20_ + matrix.m02_) * invS;
  101. w_ = (matrix.m21_ - matrix.m12_) * invS;
  102. }
  103. else if (matrix.m11_ > matrix.m22_)
  104. {
  105. float s = sqrtf(1.0f + matrix.m11_ - matrix.m00_ - matrix.m22_) * 2.0f;
  106. float invS = 1.0f / s;
  107. x_ = (matrix.m01_ + matrix.m10_) * invS;
  108. y_ = 0.25f * s;
  109. z_ = (matrix.m12_ + matrix.m21_) * invS;
  110. w_ = (matrix.m02_ - matrix.m20_) * invS;
  111. }
  112. else
  113. {
  114. float s = sqrtf(1.0f + matrix.m22_ - matrix.m00_ - matrix.m11_) * 2.0f;
  115. float invS = 1.0f / s;
  116. x_ = (matrix.m02_ + matrix.m20_) * invS;
  117. y_ = (matrix.m12_ + matrix.m21_) * invS;
  118. z_ = 0.25f * s;
  119. w_ = (matrix.m10_ - matrix.m01_) * invS;
  120. }
  121. }
  122. }
  123. Vector3 Quaternion::EulerAngles() const
  124. {
  125. // Derivation from http://www.geometrictools.com/Documentation/EulerAngles.pdf
  126. // Order of rotations: Z first, then X, then Y
  127. float check = 2.0f * (-y_ * z_ + w_ * x_);
  128. if (check < -0.995f)
  129. {
  130. return Vector3(
  131. -90.0f,
  132. 0.0f,
  133. -atan2f(2.0f * (x_ * z_ - w_ * y_), 1.0f - 2.0f * (y_ * y_ + z_ * z_)) * M_RADTODEG
  134. );
  135. }
  136. else if (check > 0.995f)
  137. {
  138. return Vector3(
  139. 90.0f,
  140. 0.0f,
  141. atan2f(2.0f * (x_ * z_ - w_ * y_), 1.0f - 2.0f * (y_ * y_ + z_ * z_)) * M_RADTODEG
  142. );
  143. }
  144. else
  145. {
  146. return Vector3(
  147. asinf(check) * M_RADTODEG,
  148. atan2f(2.0f * (x_ * z_ + w_ * y_), 1.0f - 2.0f * (x_ * x_ + y_ * y_)) * M_RADTODEG,
  149. atan2f(2.0f * (x_ * y_ + w_ * z_), 1.0f - 2.0f * (x_ * x_ + z_ * z_)) * M_RADTODEG
  150. );
  151. }
  152. }
  153. float Quaternion::YawAngle() const
  154. {
  155. return EulerAngles().y_;
  156. }
  157. float Quaternion::PitchAngle() const
  158. {
  159. return EulerAngles().x_;
  160. }
  161. float Quaternion::RollAngle() const
  162. {
  163. return EulerAngles().z_;
  164. }
  165. Matrix3 Quaternion::RotationMatrix() const
  166. {
  167. return Matrix3(
  168. 1.0f - 2.0f * y_ * y_ - 2.0f * z_ * z_,
  169. 2.0f * x_ * y_ - 2.0f * w_ * z_,
  170. 2.0f * x_ * z_ + 2.0f * w_ * y_,
  171. 2.0f * x_ * y_ + 2.0f * w_ * z_,
  172. 1.0f - 2.0f * x_ * x_ - 2.0f * z_ * z_,
  173. 2.0f * y_ * z_ - 2.0f * w_ * x_,
  174. 2.0f * x_ * z_ - 2.0f * w_ * y_,
  175. 2.0f * y_ * z_ + 2.0f * w_ * x_,
  176. 1.0f - 2.0f * x_ * x_ - 2.0f * y_ * y_
  177. );
  178. }
  179. Quaternion Quaternion::Slerp(Quaternion rhs, float t) const
  180. {
  181. float cosAngle = DotProduct(rhs);
  182. // Enable shortest path rotation
  183. if (cosAngle < 0.0f)
  184. {
  185. cosAngle = -cosAngle;
  186. rhs = -rhs;
  187. }
  188. float angle = acosf(cosAngle);
  189. float sinAngle = sinf(angle);
  190. float t1, t2;
  191. if (sinAngle > 0.001f)
  192. {
  193. float invSinAngle = 1.0f / sinAngle;
  194. t1 = sinf((1.0f - t) * angle) * invSinAngle;
  195. t2 = sinf(t * angle) * invSinAngle;
  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. }