quat.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/wwmath/quat.h 29 5/11/01 7:11p Jani_p $ */
  19. /***************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***************************************************************************
  22. * *
  23. * Project Name : Voxel Technology *
  24. * *
  25. * File Name : QUAT.H *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 02/24/97 *
  30. * *
  31. * Last Update : February 24, 1997 [GH] *
  32. * *
  33. *-------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef QUAT_H
  40. #define QUAT_H
  41. #include "always.h"
  42. #include "wwmath.h"
  43. #include "matrix3.h"
  44. #include "vector3.h"
  45. class Quaternion
  46. {
  47. private:
  48. public:
  49. // X,Y,Z are the imaginary parts of the quaterion
  50. // W is the real part
  51. float X;
  52. float Y;
  53. float Z;
  54. float W;
  55. public:
  56. WWINLINE Quaternion(void) {};
  57. WWINLINE explicit Quaternion(bool init) { if (init) { X = 0.0f; Y = 0.0f; Z = 0.0f; W = 1.0f; } }
  58. WWINLINE explicit Quaternion(float a, float b, float c, float d) { X=a; Y=b; Z=c; W=d; }
  59. WWINLINE explicit Quaternion(const Vector3 & axis,float angle);
  60. WWINLINE Quaternion & operator=(const Quaternion & source);
  61. WWINLINE void Set(float a = 0.0, float b = 0.0, float c = 0.0, float d = 1.0) { X = a; Y = b; Z = c; W = d; }
  62. WWINLINE void Make_Identity(void) { Set(); };
  63. WWINLINE void Scale(float s) { X = (float)(s*X); Y = (float)(s*Y); Z = (float)(s*Z); W = (float)(s*W); }
  64. // Array access
  65. WWINLINE float & operator [](int i) { return (&X)[i]; }
  66. WWINLINE const float & operator [](int i) const { return (&X)[i]; }
  67. // Unary operators.
  68. // Remember that q and -q represent the same 3D rotation.
  69. WWINLINE Quaternion operator-() const { return(Quaternion(-X,-Y,-Z,-W)); }
  70. WWINLINE Quaternion operator+() const { return *this; }
  71. // Every 3D rotation can be expressed by two different quaternions, This
  72. // function makes the current quaternion convert itself to the representation
  73. // which is closer on the 4D unit-hypersphere to the given quaternion.
  74. Quaternion & Make_Closest(const Quaternion & qto);
  75. // Square of the magnitude of the quaternion
  76. WWINLINE float Length2(void) const { return (X*X + Y*Y + Z*Z + W*W); }
  77. // Magnitude of the quaternion
  78. WWINLINE float Length(void) const { return WWMath::Sqrt(Length2()); }
  79. // Make the quaternion unit length
  80. void Normalize(void);
  81. // post-concatenate rotations about the coordinate axes
  82. void Rotate_X(float theta);
  83. void Rotate_Y(float theta);
  84. void Rotate_Z(float theta);
  85. // initialize this quaternion randomly (creates a random *unit* quaternion)
  86. void Randomize(void);
  87. // transform (rotate) a vector with this quaternion
  88. WWINLINE Vector3 Rotate_Vector(const Vector3 & v) const;
  89. WWINLINE void Rotate_Vector(const Vector3 & v,Vector3 * set_result) const;
  90. // verify that none of the members of this quaternion are invalid floats
  91. bool Is_Valid(void) const;
  92. };
  93. // Inverse of the quaternion (1/q)
  94. WWINLINE Quaternion Inverse(const Quaternion & a)
  95. {
  96. return Quaternion(-a[0],-a[1],-a[2],a[3]);
  97. }
  98. // Conjugate of the quaternion
  99. WWINLINE Quaternion Conjugate(const Quaternion & a)
  100. {
  101. return Quaternion(-a[0],-a[1],-a[2],a[3]);
  102. }
  103. // Add two quaternions
  104. WWINLINE Quaternion operator + (const Quaternion & a,const Quaternion & b)
  105. {
  106. return Quaternion(a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]);
  107. }
  108. // Subract two quaternions
  109. WWINLINE Quaternion operator - (const Quaternion & a,const Quaternion & b)
  110. {
  111. return Quaternion(a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]);
  112. }
  113. // Multiply a quaternion by a scalar:
  114. WWINLINE Quaternion operator * (float scl, const Quaternion & a)
  115. {
  116. return Quaternion(scl*a[0], scl*a[1], scl*a[2], scl*a[3]);
  117. }
  118. // Multiply a quaternion by a scalar
  119. WWINLINE Quaternion operator * (const Quaternion & a, float scl)
  120. {
  121. return scl*a;
  122. }
  123. // Multiply two quaternions
  124. WWINLINE Quaternion operator * (const Quaternion & a,const Quaternion & b)
  125. {
  126. return Quaternion
  127. (
  128. a.W*b.X + b.W*a.X + (a.Y*b.Z - b.Y*a.Z),
  129. a.W*b.Y + b.W*a.Y - (a.X*b.Z - b.X*a.Z),
  130. a.W*b.Z + b.W*a.Z + (a.X*b.Y - b.X*a.Y),
  131. a.W * b.W - (a.X * b.X + a.Y * b.Y + a.Z * b.Z)
  132. );
  133. }
  134. // Divide two quaternions
  135. WWINLINE Quaternion operator / (const Quaternion & a,const Quaternion & b)
  136. {
  137. return a * Inverse(b);
  138. }
  139. // Normalized version of the quaternion
  140. WWINLINE Quaternion Normalize(const Quaternion & a)
  141. {
  142. float mag = a.Length();
  143. if (0.0f == mag) {
  144. return a;
  145. } else {
  146. float oomag = 1.0f / mag;
  147. return Quaternion(a[0] * oomag, a[1] * oomag, a[2] * oomag, a[3] * oomag);
  148. }
  149. }
  150. // This function computes a quaternion based on an axis
  151. // (defined by the given Vector a) and an angle about
  152. // which to rotate. The angle is expressed in radians.
  153. Quaternion Axis_To_Quat(const Vector3 &a, float angle);
  154. // Pass the x and y coordinates of the last and current position
  155. // of the mouse, scaled so they are from -1.0 to 1.0
  156. // The quaternion is the computed as the rotation of a trackball
  157. // between the two points projected onto a sphere. This can
  158. // be used to implement an intuitive viewing control system.
  159. Quaternion Trackball(float x0, float y0, float x1, float y1, float sphsize);
  160. // Spherical Linear interpolation of quaternions
  161. //Quaternion Slerp(const Quaternion & a,const Quaternion & b,float t);
  162. void __cdecl Slerp(Quaternion& result, const Quaternion & a,const Quaternion & b,float t);
  163. // Fast slerp is innaccurate but multiple times faster
  164. void __cdecl Fast_Slerp(Quaternion& result, const Quaternion & a,const Quaternion & b,float t);
  165. // Convert a rotation matrix into a quaternion
  166. Quaternion Build_Quaternion(const Matrix3 & matrix);
  167. Quaternion Build_Quaternion(const Matrix3D & matrix);
  168. Quaternion Build_Quaternion(const Matrix4 & matrix);
  169. // Convert a quaternion into a rotation matrix
  170. Matrix3 Build_Matrix3(const Quaternion & quat);
  171. Matrix3D Build_Matrix3D(const Quaternion & quat);
  172. Matrix4 Build_Matrix4(const Quaternion & quat);
  173. // Some values can be cached if you are performing multiple slerps
  174. // between the same two quaternions...
  175. struct SlerpInfoStruct
  176. {
  177. float SinT;
  178. float Theta;
  179. bool Flip;
  180. bool Linear;
  181. };
  182. // Cached slerp implementation
  183. void Slerp_Setup(const Quaternion & p,const Quaternion & q,SlerpInfoStruct * slerpinfo);
  184. void Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo,Quaternion * set_q);
  185. Quaternion Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo);
  186. WWINLINE Vector3 Quaternion::Rotate_Vector(const Vector3 & v) const
  187. {
  188. float x = W*v.X + (Y*v.Z - v.Y*Z);
  189. float y = W*v.Y - (X*v.Z - v.X*Z);
  190. float z = W*v.Z + (X*v.Y - v.X*Y);
  191. float w = -(X*v.X + Y*v.Y + Z*v.Z);
  192. return Vector3
  193. (
  194. w*(-X) + W*x + (y*(-Z) - (-Y)*z),
  195. w*(-Y) + W*y - (x*(-Z) - (-X)*z),
  196. w*(-Z) + W*z + (x*(-Y) - (-X)*y)
  197. );
  198. }
  199. WWINLINE void Quaternion::Rotate_Vector(const Vector3 & v,Vector3 * result) const
  200. {
  201. assert(result != NULL);
  202. float x = W*v.X + (Y*v.Z - v.Y*Z);
  203. float y = W*v.Y - (X*v.Z - v.X*Z);
  204. float z = W*v.Z + (X*v.Y - v.X*Y);
  205. float w = -(X*v.X + Y*v.Y + Z*v.Z);
  206. result->X = w*(-X) + W*x + (y*(-Z) - (-Y)*z);
  207. result->Y = w*(-Y) + W*y - (x*(-Z) - (-X)*z);
  208. result->Z = w*(-Z) + W*z + (x*(-Y) - (-X)*y);
  209. }
  210. WWINLINE bool Quaternion::Is_Valid(void) const
  211. {
  212. return ( WWMath::Is_Valid_Float(X) &&
  213. WWMath::Is_Valid_Float(Y) &&
  214. WWMath::Is_Valid_Float(Z) &&
  215. WWMath::Is_Valid_Float(W) );
  216. }
  217. WWINLINE bool Equal_Within_Epsilon(const Quaternion &a, const Quaternion &b, float epsilon)
  218. {
  219. return( (WWMath::Fabs(a.X - b.X) < epsilon) &&
  220. (WWMath::Fabs(a.Y - b.Y) < epsilon) &&
  221. (WWMath::Fabs(a.Z - b.Z) < epsilon) &&
  222. (WWMath::Fabs(a.W - b.W) < epsilon) );
  223. }
  224. /***********************************************************************************************
  225. * Quaternion::operator= -- Assignment operator *
  226. * *
  227. * INPUT: *
  228. * *
  229. * OUTPUT: *
  230. * *
  231. * WARNINGS: *
  232. * *
  233. * HISTORY: *
  234. * 02/24/1997 GH : Created. *
  235. *=============================================================================================*/
  236. WWINLINE Quaternion & Quaternion::operator = (const Quaternion & source)
  237. {
  238. X = source[0];
  239. Y = source[1];
  240. Z = source[2];
  241. W = source[3];
  242. return *this;
  243. }
  244. #endif /* QUAT_H */