Quaternion.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 an angle (in degrees, for Urho2D).
  68. Quaternion(float angle)
  69. {
  70. FromAngleAxis(angle, Vector3::FORWARD);
  71. }
  72. /// Construct from Euler angles (in degrees.)
  73. Quaternion(float x, float y, float z)
  74. {
  75. FromEulerAngles(x, y, z);
  76. }
  77. /// Construct from the rotation difference between two direction vectors.
  78. Quaternion(const Vector3& start, const Vector3& end)
  79. {
  80. FromRotationTo(start, end);
  81. }
  82. /// Construct from orthonormal axes.
  83. Quaternion(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
  84. {
  85. FromAxes(xAxis, yAxis, zAxis);
  86. }
  87. /// Construct from a rotation matrix.
  88. Quaternion(const Matrix3& matrix)
  89. {
  90. FromRotationMatrix(matrix);
  91. }
  92. /// Assign from another quaternion.
  93. Quaternion& operator = (const Quaternion& rhs)
  94. {
  95. w_ = rhs.w_;
  96. x_ = rhs.x_;
  97. y_ = rhs.y_;
  98. z_ = rhs.z_;
  99. return *this;
  100. }
  101. /// Add-assign a quaternion.
  102. Quaternion& operator += (const Quaternion& rhs)
  103. {
  104. w_ += rhs.w_;
  105. x_ += rhs.x_;
  106. y_ += rhs.y_;
  107. z_ += rhs.z_;
  108. return *this;
  109. }
  110. /// Multiply-assign a scalar.
  111. Quaternion& operator *= (float rhs)
  112. {
  113. w_ *= rhs;
  114. x_ *= rhs;
  115. y_ *= rhs;
  116. z_ *= rhs;
  117. return *this;
  118. }
  119. /// Test for equality with another quaternion without epsilon.
  120. bool operator == (const Quaternion& rhs) const { return w_ == rhs.w_ && x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  121. /// Test for inequality with another quaternion without epsilon.
  122. bool operator != (const Quaternion& rhs) const { return w_ != rhs.w_ || x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
  123. /// Multiply with a scalar.
  124. Quaternion operator * (float rhs) const { return Quaternion(w_ * rhs, x_ * rhs, y_ * rhs, z_ * rhs); }
  125. /// Return negation.
  126. Quaternion operator - () const { return Quaternion(-w_, -x_, -y_, -z_); }
  127. /// Add a quaternion.
  128. Quaternion operator + (const Quaternion& rhs) const { return Quaternion(w_ + rhs.w_, x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  129. /// Subtract a quaternion.
  130. Quaternion operator - (const Quaternion& rhs) const { return Quaternion(w_ - rhs.w_, x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  131. /// Multiply a quaternion.
  132. Quaternion operator * (const Quaternion& rhs) const
  133. {
  134. return Quaternion(
  135. w_ * rhs.w_ - x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_,
  136. w_ * rhs.x_ + x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_,
  137. w_ * rhs.y_ + y_ * rhs.w_ + z_ * rhs.x_ - x_ * rhs.z_,
  138. w_ * rhs.z_ + z_ * rhs.w_ + x_ * rhs.y_ - y_ * rhs.x_
  139. );
  140. }
  141. /// Multiply a Vector3.
  142. Vector3 operator * (const Vector3& rhs) const
  143. {
  144. Vector3 qVec(x_,y_,z_);
  145. Vector3 cross1(qVec.CrossProduct(rhs));
  146. Vector3 cross2(qVec.CrossProduct(cross1));
  147. return rhs + 2.0f * (cross1 * w_ + cross2);
  148. }
  149. /// Define from an angle (in degrees) and axis.
  150. void FromAngleAxis(float angle, const Vector3& axis);
  151. /// Define from Euler angles (in degrees.)
  152. void FromEulerAngles(float x, float y, float z);
  153. /// Define from the rotation difference between two direction vectors.
  154. void FromRotationTo(const Vector3& start, const Vector3& end);
  155. /// Define from orthonormal axes.
  156. void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  157. /// Define from a rotation matrix.
  158. void FromRotationMatrix(const Matrix3& matrix);
  159. /// Define from a direction to look in and an up direction. Return true if successful, or false if would result in a NaN, in which case the current value remains.
  160. bool FromLookRotation(const Vector3& direction, const Vector3& up = Vector3::UP);
  161. /// Normalize to unit length.
  162. void Normalize()
  163. {
  164. float lenSquared = LengthSquared();
  165. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  166. {
  167. float invLen = 1.0f / sqrtf(lenSquared);
  168. w_ *= invLen;
  169. x_ *= invLen;
  170. y_ *= invLen;
  171. z_ *= invLen;
  172. }
  173. }
  174. /// Return normalized to unit length.
  175. Quaternion Normalized() const
  176. {
  177. float lenSquared = LengthSquared();
  178. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  179. {
  180. float invLen = 1.0f / sqrtf(lenSquared);
  181. return *this * invLen;
  182. }
  183. else
  184. return *this;
  185. }
  186. /// Return inverse.
  187. Quaternion Inverse() const
  188. {
  189. float lenSquared = LengthSquared();
  190. if (lenSquared == 1.0f)
  191. return Conjugate();
  192. else if (lenSquared >= M_EPSILON)
  193. return Conjugate() * (1.0f / lenSquared);
  194. else
  195. return IDENTITY;
  196. }
  197. /// Return squared length.
  198. float LengthSquared() const { return w_ * w_ + x_ * x_ + y_ * y_ + z_ * z_; }
  199. /// Calculate dot product.
  200. float DotProduct(const Quaternion& rhs) const { return w_ * rhs.w_ + x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  201. /// Test for equality with another quaternion with epsilon.
  202. 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_); }
  203. /// Return whether is NaN.
  204. bool IsNaN() const { return Urho3D::IsNaN(w_) || Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_) || Urho3D::IsNaN(z_); }
  205. /// Return conjugate.
  206. Quaternion Conjugate() const { return Quaternion(w_, -x_, -y_, -z_); }
  207. /// Return Euler angles in degrees.
  208. Vector3 EulerAngles() const;
  209. /// Return yaw angle in degrees.
  210. float YawAngle() const;
  211. /// Return pitch angle in degrees.
  212. float PitchAngle() const;
  213. /// Return roll angle in degrees.
  214. float RollAngle() const;
  215. /// Return the rotation matrix that corresponds to this quaternion.
  216. Matrix3 RotationMatrix() const;
  217. /// Spherical interpolation with another quaternion.
  218. Quaternion Slerp(Quaternion rhs, float t) const;
  219. /// Normalized linear interpolation with another quaternion.
  220. Quaternion Nlerp(Quaternion rhs, float t, bool shortestPath = false) const;
  221. /// Return float data.
  222. const float* Data() const { return &w_; }
  223. /// Return as string.
  224. String ToString() const;
  225. /// W coordinate.
  226. float w_;
  227. /// X coordinate.
  228. float x_;
  229. /// Y coordinate.
  230. float y_;
  231. /// Z coordinate.
  232. float z_;
  233. /// Identity quaternion.
  234. static const Quaternion IDENTITY;
  235. };
  236. }