Quaternion.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (c) 2008-2014 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. #pragma once
  23. #include "Matrix3.h"
  24. namespace Urho3D
  25. {
  26. /// Rotation represented as a four-dimensional normalized vector.
  27. class URHO3D_API Quaternion
  28. {
  29. public:
  30. /// Construct an identity quaternion.
  31. Quaternion() :
  32. w_(1.0f),
  33. x_(0.0f),
  34. y_(0.0f),
  35. z_(0.0f)
  36. {
  37. }
  38. /// Copy-construct from another quaternion.
  39. Quaternion(const Quaternion& quat) :
  40. w_(quat.w_),
  41. x_(quat.x_),
  42. y_(quat.y_),
  43. z_(quat.z_)
  44. {
  45. }
  46. /// Construct from values.
  47. Quaternion(float w, float x, float y, float z) :
  48. w_(w),
  49. x_(x),
  50. y_(y),
  51. z_(z)
  52. {
  53. }
  54. /// Construct from a float array.
  55. Quaternion(const float* data) :
  56. w_(data[0]),
  57. x_(data[1]),
  58. y_(data[2]),
  59. z_(data[3])
  60. {
  61. }
  62. /// Construct from an angle (in degrees) and axis.
  63. Quaternion(float angle, const Vector3& axis)
  64. {
  65. FromAngleAxis(angle, axis);
  66. }
  67. /// Construct from Euler angles (in degrees.)
  68. Quaternion(float x, float y, float z)
  69. {
  70. FromEulerAngles(x, y, z);
  71. }
  72. /// Construct from the rotation difference between two direction vectors.
  73. Quaternion(const Vector3& start, const Vector3& end)
  74. {
  75. FromRotationTo(start, end);
  76. }
  77. /// Construct from orthonormal axes.
  78. Quaternion(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
  79. {
  80. FromAxes(xAxis, yAxis, zAxis);
  81. }
  82. /// Construct from a rotation matrix.
  83. Quaternion(const Matrix3& matrix)
  84. {
  85. FromRotationMatrix(matrix);
  86. }
  87. /// Assign from another quaternion.
  88. Quaternion& operator = (const Quaternion& rhs)
  89. {
  90. w_ = rhs.w_;
  91. x_ = rhs.x_;
  92. y_ = rhs.y_;
  93. z_ = rhs.z_;
  94. return *this;
  95. }
  96. /// Add-assign a quaternion.
  97. Quaternion& operator += (const Quaternion& rhs)
  98. {
  99. w_ += rhs.w_;
  100. x_ += rhs.x_;
  101. y_ += rhs.y_;
  102. z_ += rhs.z_;
  103. return *this;
  104. }
  105. /// Multiply-assign a scalar.
  106. Quaternion& operator *= (float rhs)
  107. {
  108. w_ *= rhs;
  109. x_ *= rhs;
  110. y_ *= rhs;
  111. z_ *= rhs;
  112. return *this;
  113. }
  114. /// Test for equality with another quaternion without epsilon.
  115. bool operator == (const Quaternion& rhs) const { return w_ == rhs.w_ && x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  116. /// Test for inequality with another quaternion without epsilon.
  117. bool operator != (const Quaternion& rhs) const { return w_ != rhs.w_ || x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
  118. /// Multiply with a scalar.
  119. Quaternion operator * (float rhs) const { return Quaternion(w_ * rhs, x_ * rhs, y_ * rhs, z_ * rhs); }
  120. /// Return negation.
  121. Quaternion operator - () const { return Quaternion(-w_, -x_, -y_, -z_); }
  122. /// Add a quaternion.
  123. Quaternion operator + (const Quaternion& rhs) const { return Quaternion(w_ + rhs.w_, x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  124. /// Subtract a quaternion.
  125. Quaternion operator - (const Quaternion& rhs) const { return Quaternion(w_ - rhs.w_, x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  126. /// Multiply a quaternion.
  127. Quaternion operator * (const Quaternion& rhs) const
  128. {
  129. return Quaternion(
  130. w_ * rhs.w_ - x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_,
  131. w_ * rhs.x_ + x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_,
  132. w_ * rhs.y_ + y_ * rhs.w_ + z_ * rhs.x_ - x_ * rhs.z_,
  133. w_ * rhs.z_ + z_ * rhs.w_ + x_ * rhs.y_ - y_ * rhs.x_
  134. );
  135. }
  136. /// Multiply a Vector3.
  137. Vector3 operator * (const Vector3& rhs) const
  138. {
  139. Vector3 qVec(x_,y_,z_);
  140. Vector3 cross1(qVec.CrossProduct(rhs));
  141. Vector3 cross2(qVec.CrossProduct(cross1));
  142. return rhs + 2.0f * (cross1 * w_ + cross2);
  143. }
  144. /// Define from an angle (in degrees) and axis.
  145. void FromAngleAxis(float angle, const Vector3& axis);
  146. /// Define from Euler angles (in degrees.)
  147. void FromEulerAngles(float x, float y, float z);
  148. /// Define from the rotation difference between two direction vectors.
  149. void FromRotationTo(const Vector3& start, const Vector3& end);
  150. /// Define from orthonormal axes.
  151. void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  152. /// Define from a rotation matrix.
  153. void FromRotationMatrix(const Matrix3& matrix);
  154. /// Define from a direction to look in and an up direction.
  155. void FromLookRotation(const Vector3& direction, const Vector3& up = Vector3::UP);
  156. /// Normalize to unit length.
  157. void Normalize()
  158. {
  159. float lenSquared = LengthSquared();
  160. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  161. {
  162. float invLen = 1.0f / sqrtf(lenSquared);
  163. w_ *= invLen;
  164. x_ *= invLen;
  165. y_ *= invLen;
  166. z_ *= invLen;
  167. }
  168. }
  169. /// Return normalized to unit length.
  170. Quaternion Normalized() const
  171. {
  172. float lenSquared = LengthSquared();
  173. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  174. {
  175. float invLen = 1.0f / sqrtf(lenSquared);
  176. return *this * invLen;
  177. }
  178. else
  179. return *this;
  180. }
  181. /// Return inverse.
  182. Quaternion Inverse() const
  183. {
  184. float lenSquared = LengthSquared();
  185. if (lenSquared == 1.0f)
  186. return Conjugate();
  187. else if (lenSquared >= M_EPSILON)
  188. return Conjugate() * (1.0f / lenSquared);
  189. else
  190. return IDENTITY;
  191. }
  192. /// Return squared length.
  193. float LengthSquared() const { return w_ * w_ + x_ * x_ + y_ * y_ + z_ * z_; }
  194. /// Calculate dot product.
  195. float DotProduct(const Quaternion& rhs) const { return w_ * rhs.w_ + x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  196. /// Test for equality with another quaternion with epsilon.
  197. bool Equals(const Quaternion& rhs) const { return Urho3D::Equals(w_, rhs.w_) && Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_); }
  198. /// Return whether is NaN.
  199. bool IsNaN() const { return Urho3D::IsNaN(w_) || Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_) || Urho3D::IsNaN(z_); }
  200. /// Return conjugate.
  201. Quaternion Conjugate() const { return Quaternion(w_, -x_, -y_, -z_); }
  202. /// Return Euler angles in degrees.
  203. Vector3 EulerAngles() const;
  204. /// Return yaw angle in degrees.
  205. float YawAngle() const;
  206. /// Return pitch angle in degrees.
  207. float PitchAngle() const;
  208. /// Return roll angle in degrees.
  209. float RollAngle() const;
  210. /// Return the rotation matrix that corresponds to this quaternion.
  211. Matrix3 RotationMatrix() const;
  212. /// Spherical interpolation with another quaternion.
  213. Quaternion Slerp(Quaternion rhs, float t) const;
  214. /// Normalized linear interpolation with another quaternion.
  215. Quaternion Nlerp(Quaternion rhs, float t, bool shortestPath = false) const;
  216. /// Return float data.
  217. const float* Data() const { return &w_; }
  218. /// Return as string.
  219. String ToString() const;
  220. /// W coordinate.
  221. float w_;
  222. /// X coordinate.
  223. float x_;
  224. /// Y coordinate.
  225. float y_;
  226. /// Z coordinate.
  227. float z_;
  228. /// Identity quaternion.
  229. static const Quaternion IDENTITY;
  230. };
  231. }