Quaternion.pkg 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. $#include "Quaternion.h"
  2. /// Rotation represented as a four-dimensional normalized vector.
  3. class Quaternion
  4. {
  5. public:
  6. /// Construct identity quaternion.
  7. Quaternion() :
  8. w_(1.0f),
  9. x_(0.0f),
  10. y_(0.0f),
  11. z_(0.0f)
  12. {
  13. }
  14. /// Copy-construct from another quaternion.
  15. Quaternion(const Quaternion& quat) :
  16. w_(quat.w_),
  17. x_(quat.x_),
  18. y_(quat.y_),
  19. z_(quat.z_)
  20. {
  21. }
  22. /// Construct from values.
  23. Quaternion(float w, float x, float y, float z) :
  24. w_(w),
  25. x_(x),
  26. y_(y),
  27. z_(z)
  28. {
  29. }
  30. /// Construct from an angle (in degrees) and axis.
  31. Quaternion(float angle, const Vector3& axis)
  32. {
  33. FromAngleAxis(angle, axis);
  34. }
  35. /// Construct from Euler angles (in degrees.)
  36. Quaternion(float x, float y, float z)
  37. {
  38. FromEulerAngles(x, y, z);
  39. }
  40. /// Construct from the rotation difference between two vectors.
  41. Quaternion(const Vector3& start, const Vector3& end)
  42. {
  43. FromRotationTo(start, end);
  44. }
  45. /// Construct from orthonormal axes.
  46. Quaternion(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis)
  47. {
  48. FromAxes(xAxis, yAxis, zAxis);
  49. }
  50. /// Construct from a rotation matrix.
  51. Quaternion(const Matrix3& matrix)
  52. {
  53. FromRotationMatrix(matrix);
  54. }
  55. /// Test for equality with another quaternion without epsilon.
  56. bool operator == (const Quaternion& rhs) const;
  57. /// Multiply with a scalar.
  58. Quaternion operator * (float rhs) const;
  59. /// Return negation.
  60. Quaternion operator - () const;
  61. /// Test for equality with another quaternion without epsilon.
  62. bool operator == (const Quaternion& rhs) const { return w_ == rhs.w_ && x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  63. /// Multiply with a scalar.
  64. Quaternion operator * (float rhs) const { return Quaternion(w_ * rhs, x_ * rhs, y_ * rhs, z_ * rhs); }
  65. /// Return negation.
  66. Quaternion operator - () const { return Quaternion(-w_, -x_, -y_, -z_); }
  67. /// Add a quaternion.
  68. Quaternion operator + (const Quaternion& rhs) const { return Quaternion(w_ + rhs.w_, x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  69. /// Subtract a quaternion.
  70. Quaternion operator - (const Quaternion& rhs) const { return Quaternion(w_ - rhs.w_, x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  71. /// Multiply a quaternion.
  72. Quaternion operator * (const Quaternion& rhs) const
  73. {
  74. return Quaternion(
  75. w_ * rhs.w_ - x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_,
  76. w_ * rhs.x_ + x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_,
  77. w_ * rhs.y_ + y_ * rhs.w_ + z_ * rhs.x_ - x_ * rhs.z_,
  78. w_ * rhs.z_ + z_ * rhs.w_ + x_ * rhs.y_ - y_ * rhs.x_
  79. );
  80. }
  81. /// Multiply a Vector3.
  82. Vector3 operator * (const Vector3& rhs) const
  83. {
  84. Vector3 qVec(x_,y_,z_);
  85. Vector3 cross1(qVec.CrossProduct(rhs));
  86. Vector3 cross2(qVec.CrossProduct(cross1));
  87. return rhs + 2.0f * (cross1 * w_ + cross2);
  88. }
  89. /// Define from an angle (in degrees) and axis.
  90. void FromAngleAxis(float angle, const Vector3& axis);
  91. /// Define from Euler angles (in degrees.)
  92. void FromEulerAngles(float x, float y, float z);
  93. /// Define from the rotation difference between two vectors.
  94. void FromRotationTo(const Vector3& start, const Vector3& end);
  95. /// Define from orthonormal axes.
  96. void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  97. /// Define from a rotation matrix.
  98. void FromRotationMatrix(const Matrix3& matrix);
  99. /// Normalize to unit length and return the previous length.
  100. float Normalize()
  101. {
  102. float len = sqrtf(LengthSquared());
  103. if (len >= M_EPSILON)
  104. *this *= (1.0f / len);
  105. return len;
  106. }
  107. /// Return normalized to unit length.
  108. Quaternion Normalized() const
  109. {
  110. float lenSquared = LengthSquared();
  111. if (lenSquared >= M_EPSILON * M_EPSILON)
  112. return *this * (1.0f / sqrtf(lenSquared));
  113. else
  114. return IDENTITY;
  115. }
  116. /// Return inverse.
  117. Quaternion Inverse() const
  118. {
  119. float lenSquared = LengthSquared();
  120. if (lenSquared == 1.0f)
  121. return Conjugate();
  122. else if (lenSquared >= M_EPSILON)
  123. return Conjugate() * (1.0f / lenSquared);
  124. else
  125. return IDENTITY;
  126. }
  127. /// Return squared length.
  128. float LengthSquared() const { return w_ * w_ + x_ * x_ + y_ * y_ + z_ * z_; }
  129. /// Calculate dot product.
  130. float DotProduct(const Quaternion& rhs) const { return w_ * rhs.w_ + x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  131. /// Test for equality with another quaternion with epsilon.
  132. 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_); }
  133. /// Return conjugate.
  134. Quaternion Conjugate() const { return Quaternion(w_, -x_, -y_, -z_); }
  135. /// Return Euler angles in degrees.
  136. Vector3 EulerAngles() const;
  137. /// Return yaw angle in degrees.
  138. float YawAngle() const;
  139. /// Return pitch angle in degrees.
  140. float PitchAngle() const;
  141. /// Return roll angle in degrees.
  142. float RollAngle() const;
  143. /// Return the rotation matrix that corresponds to this quaternion.
  144. Matrix3 RotationMatrix() const;
  145. /// Spherical interpolation with another quaternion.
  146. Quaternion Slerp(Quaternion rhs, float t) const;
  147. /// Return as string.
  148. String ToString() const;
  149. /// W coordinate.
  150. float w_ @ w;
  151. /// X coordinate.
  152. float x_ @ x;
  153. /// Y coordinate.
  154. float y_ @ y;
  155. /// Z coordinate.
  156. float z_ @ z;
  157. /// Identity quaternion.
  158. static const Quaternion IDENTITY;
  159. };