quat.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/Tools/W3DShellExt/External/quat.h 1 1/02/02 1:18p Moumine_ballo $ */
  19. /***************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***************************************************************************
  22. * *
  23. * Project Name : WW3D PS2 *
  24. * *
  25. * File Name : QUAT.H *
  26. * *
  27. * Programmer : Kenny Mitchell *
  28. * *
  29. * Start Date : 11/16/99 *
  30. * *
  31. * Last Update : 11/16/99 *
  32. * *
  33. *-------------------------------------------------------------------------*
  34. * Based on Greg Hjelstrom 97 *
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #ifndef QUAT_H
  38. #define QUAT_H
  39. #include "matrix3.h"
  40. #include "vector3.h"
  41. class Quaternion
  42. {
  43. private:
  44. public:
  45. // X,Y,Z are the imaginary parts of the quaterion
  46. // W is the real part
  47. float X;
  48. float Y;
  49. float Z;
  50. float W;
  51. public:
  52. Quaternion(void) {};
  53. explicit Quaternion(bool init) { if (init) { X = 0.0f; Y = 0.0f; Z = 0.0f; W = 1.0f; } }
  54. explicit Quaternion(float a, float b, float c, float d) { X=a; Y=b; Z=c; W=d; }
  55. explicit Quaternion(const Vector3 & axis,float angle);
  56. Quaternion & operator=(const Quaternion & source);
  57. void Set(float a = 0.0f, float b = 0.0f, float c = 0.0f, float d = 1.0f) { X = a; Y = b; Z = c; W = d; }
  58. void Make_Identity(void) { Set(); };
  59. void Scale(float s) { X = (float)(s*X); Y = (float)(s*Y); Z = (float)(s*Z); W = (float)(s*W); }
  60. // Array access
  61. float & operator [](int i) { return (&X)[i]; }
  62. const float & operator [](int i) const { return (&X)[i]; }
  63. // Unary operators.
  64. // Remember that q and -q represent the same 3D rotation.
  65. Quaternion operator-() const { return(Quaternion(-X,-Y,-Z,-W)); }
  66. Quaternion operator+() const { return *this; }
  67. // Every 3D rotation can be expressed by two different quaternions, This
  68. // function makes the current quaternion convert itself to the representation
  69. // which is closer on the 4D unit-hypersphere to the given quaternion.
  70. Quaternion & Make_Closest(const Quaternion & qto);
  71. // Square of the magnitude of the quaternion
  72. float Length2(void) const { return(X*X + Y*Y + Z*Z + W*W); }
  73. // Magnitude of the quaternion
  74. float Length(void) const { return (float)sqrt(Length2()); }
  75. // Make the quaternion unit length
  76. void Normalize(void);
  77. // post-concatenate rotations about the coordinate axes
  78. void Rotate_X(float theta);
  79. void Rotate_Y(float theta);
  80. void Rotate_Z(float theta);
  81. // initialize this quaternion randomly (creates a random *unit* quaternion)
  82. void Randomize(void);
  83. // transform (rotate) a vector with this quaternion
  84. Vector3 Rotate_Vector(const Vector3 & v) const;
  85. void Rotate_Vector(const Vector3 & v,Vector3 * set_result) const;
  86. // verify that none of the members of this quaternion are invalid floats
  87. bool Is_Valid(void) const;
  88. };
  89. // Inverse of the quaternion (1/q)
  90. inline Quaternion Inverse(const Quaternion & a)
  91. {
  92. return Quaternion(-a[0],-a[1],-a[2],a[3]);
  93. }
  94. // Conjugate of the quaternion
  95. inline Quaternion Conjugate(const Quaternion & a)
  96. {
  97. return Quaternion(-a[0],-a[1],-a[2],a[3]);
  98. }
  99. // Add two quaternions
  100. inline Quaternion operator + (const Quaternion & a,const Quaternion & b)
  101. {
  102. return Quaternion(a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]);
  103. }
  104. // Subract two quaternions
  105. inline Quaternion operator - (const Quaternion & a,const Quaternion & b)
  106. {
  107. return Quaternion(a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]);
  108. }
  109. // Multiply a quaternion by a scalar:
  110. inline Quaternion operator * (float scl, const Quaternion & a)
  111. {
  112. return Quaternion(scl*a[0], scl*a[1], scl*a[2], scl*a[3]);
  113. }
  114. // Multiply a quaternion by a scalar
  115. inline Quaternion operator * (const Quaternion & a, float scl)
  116. {
  117. return scl*a;
  118. }
  119. // Multiply two quaternions
  120. inline Quaternion operator * (const Quaternion & a,const Quaternion & b)
  121. {
  122. return Quaternion
  123. (
  124. a.W*b.X + b.W*a.X + (a.Y*b.Z - b.Y*a.Z),
  125. a.W*b.Y + b.W*a.Y - (a.X*b.Z - b.X*a.Z),
  126. a.W*b.Z + b.W*a.Z + (a.X*b.Y - b.X*a.Y),
  127. a.W * b.W - (a.X * b.X + a.Y * b.Y + a.Z * b.Z)
  128. );
  129. }
  130. // Divide two quaternions
  131. inline Quaternion operator / (const Quaternion & a,const Quaternion & b)
  132. {
  133. return a * Inverse(b);
  134. }
  135. // Normalized version of the quaternion
  136. inline Quaternion Normalize(const Quaternion & a)
  137. {
  138. float mag = a.Length();
  139. if (0.0f == mag) {
  140. return a;
  141. } else {
  142. float oomag = 1.0f / mag;
  143. return Quaternion(a[0] * oomag, a[1] * oomag, a[2] * oomag, a[3] * oomag);
  144. }
  145. }
  146. // This function computes a quaternion based on an axis
  147. // (defined by the given Vector a) and an angle about
  148. // which to rotate. The angle is expressed in radians.
  149. Quaternion Axis_To_Quat(const Vector3 &a, float angle);
  150. // Pass the x and y coordinates of the last and current position
  151. // of the mouse, scaled so they are from -1.0 to 1.0
  152. // The quaternion is the computed as the rotation of a trackball
  153. // between the two points projected onto a sphere. This can
  154. // be used to implement an intuitive viewing control system.
  155. Quaternion Trackball(float x0, float y0, float x1, float y1, float sphsize);
  156. // Spherical Linear interpolation of quaternions
  157. Quaternion Slerp(const Quaternion & a,const Quaternion & b,float t);
  158. // Convert a rotation matrix into a quaternion
  159. Quaternion Build_Quaternion(const Matrix3 & matrix);
  160. Quaternion Build_Quaternion(const Matrix3D & matrix);
  161. Quaternion Build_Quaternion(const Matrix4 & matrix);
  162. // Convert a quaternion into a rotation matrix
  163. Matrix3 Build_Matrix3(const Quaternion & quat);
  164. Matrix3D Build_Matrix3D(const Quaternion & quat);
  165. Matrix4 Build_Matrix4(const Quaternion & quat);
  166. // Some values can be cached if you are performing multiple slerps
  167. // between the same two quaternions...
  168. struct SlerpInfoStruct
  169. {
  170. float SinT;
  171. float Theta;
  172. bool Flip;
  173. bool Linear;
  174. };
  175. // Cached slerp implementation
  176. void Slerp_Setup(const Quaternion & p,const Quaternion & q,SlerpInfoStruct * slerpinfo);
  177. void Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo,Quaternion * set_q);
  178. Quaternion Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo);
  179. inline Vector3 Quaternion::Rotate_Vector(const Vector3 & v) const
  180. {
  181. float x = W*v.X + (Y*v.Z - v.Y*Z);
  182. float y = W*v.Y - (X*v.Z - v.X*Z);
  183. float z = W*v.Z + (X*v.Y - v.X*Y);
  184. float w = -(X*v.X + Y*v.Y + Z*v.Z);
  185. return Vector3
  186. (
  187. w*(-X) + W*x + (y*(-Z) - (-Y)*z),
  188. w*(-Y) + W*y - (x*(-Z) - (-X)*z),
  189. w*(-Z) + W*z + (x*(-Y) - (-X)*y)
  190. );
  191. }
  192. inline void Quaternion::Rotate_Vector(const Vector3 & v,Vector3 * result) const
  193. {
  194. assert(result != NULL);
  195. float x = W*v.X + (Y*v.Z - v.Y*Z);
  196. float y = W*v.Y - (X*v.Z - v.X*Z);
  197. float z = W*v.Z + (X*v.Y - v.X*Y);
  198. float w = -(X*v.X + Y*v.Y + Z*v.Z);
  199. result->X = w*(-X) + W*x + (y*(-Z) - (-Y)*z);
  200. result->Y = w*(-Y) + W*y - (x*(-Z) - (-X)*z);
  201. result->Z = w*(-Z) + W*z + (x*(-Y) - (-X)*y);
  202. }
  203. inline bool Quaternion::Is_Valid(void) const
  204. {
  205. #if 1
  206. return (1);
  207. #else
  208. return ( Is_Valid_Float(X) &&
  209. Is_Valid_Float(Y) &&
  210. Is_Valid_Float(Z) &&
  211. Is_Valid_Float(W) );
  212. #endif
  213. }
  214. #endif /* QUAT_H */