w3dquat.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/max2w3d/w3dquat.h 27 2/03/00 4:55p Jason_a $ */
  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 "wwmatrix3.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. Quaternion(void) {};
  57. explicit Quaternion(bool init) { if (init) { X = 0.0f; Y = 0.0f; Z = 0.0f; W = 1.0f; } }
  58. explicit Quaternion(float a, float b, float c, float d) { X=a; Y=b; Z=c; W=d; }
  59. explicit Quaternion(const Vector3 & axis,float angle);
  60. Quaternion & operator=(const Quaternion & source);
  61. 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. void Make_Identity(void) { Set(); };
  63. void Scale(float s) { X = (float)(s*X); Y = (float)(s*Y); Z = (float)(s*Z); W = (float)(s*W); }
  64. // Array access
  65. float & operator [](int i) { return (&X)[i]; }
  66. const float & operator [](int i) const { return (&X)[i]; }
  67. // Unary operators.
  68. // Remember that q and -q represent the same 3D rotation.
  69. Quaternion operator-() const { return(Quaternion(-X,-Y,-Z,-W)); }
  70. 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. float Length2(void) const { return (X*X + Y*Y + Z*Z + W*W); }
  77. // Magnitude of the quaternion
  78. 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. Vector3 Rotate_Vector(const Vector3 & v) const;
  89. 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. inline Quaternion Inverse(const Quaternion & a)
  95. {
  96. return Quaternion(-a[0],-a[1],-a[2],a[3]);
  97. }
  98. // Conjugate of the quaternion
  99. inline Quaternion Conjugate(const Quaternion & a)
  100. {
  101. return Quaternion(-a[0],-a[1],-a[2],a[3]);
  102. }
  103. // Add two quaternions
  104. inline 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. inline 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. inline 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. inline Quaternion operator * (const Quaternion & a, float scl)
  120. {
  121. return scl*a;
  122. }
  123. // Multiply two quaternions
  124. inline 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. inline Quaternion operator / (const Quaternion & a,const Quaternion & b)
  136. {
  137. return a * Inverse(b);
  138. }
  139. // Normalized version of the quaternion
  140. inline 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. // Convert a rotation matrix into a quaternion
  163. Quaternion Build_Quaternion(const Matrix3 & matrix);
  164. Quaternion Build_Quaternion(const Matrix3D & matrix);
  165. Quaternion Build_Quaternion(const Matrix4 & matrix);
  166. // Convert a quaternion into a rotation matrix
  167. Matrix3 Build_Matrix3(const Quaternion & quat);
  168. Matrix3D Build_Matrix3D(const Quaternion & quat);
  169. Matrix4 Build_Matrix4(const Quaternion & quat);
  170. // Some values can be cached if you are performing multiple slerps
  171. // between the same two quaternions...
  172. struct SlerpInfoStruct
  173. {
  174. float SinT;
  175. float Theta;
  176. bool Flip;
  177. bool Linear;
  178. };
  179. // Cached slerp implementation
  180. void Slerp_Setup(const Quaternion & p,const Quaternion & q,SlerpInfoStruct * slerpinfo);
  181. void Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo,Quaternion * set_q);
  182. Quaternion Cached_Slerp(const Quaternion & p,const Quaternion & q,float alpha,SlerpInfoStruct * slerpinfo);
  183. inline Vector3 Quaternion::Rotate_Vector(const Vector3 & v) const
  184. {
  185. float x = W*v.X + (Y*v.Z - v.Y*Z);
  186. float y = W*v.Y - (X*v.Z - v.X*Z);
  187. float z = W*v.Z + (X*v.Y - v.X*Y);
  188. float w = -(X*v.X + Y*v.Y + Z*v.Z);
  189. return Vector3
  190. (
  191. w*(-X) + W*x + (y*(-Z) - (-Y)*z),
  192. w*(-Y) + W*y - (x*(-Z) - (-X)*z),
  193. w*(-Z) + W*z + (x*(-Y) - (-X)*y)
  194. );
  195. }
  196. inline void Quaternion::Rotate_Vector(const Vector3 & v,Vector3 * result) const
  197. {
  198. assert(result != NULL);
  199. float x = W*v.X + (Y*v.Z - v.Y*Z);
  200. float y = W*v.Y - (X*v.Z - v.X*Z);
  201. float z = W*v.Z + (X*v.Y - v.X*Y);
  202. float w = -(X*v.X + Y*v.Y + Z*v.Z);
  203. result->X = w*(-X) + W*x + (y*(-Z) - (-Y)*z);
  204. result->Y = w*(-Y) + W*y - (x*(-Z) - (-X)*z);
  205. result->Z = w*(-Z) + W*z + (x*(-Y) - (-X)*y);
  206. }
  207. inline bool Quaternion::Is_Valid(void) const
  208. {
  209. return ( WWMath::Is_Valid_Float(X) &&
  210. WWMath::Is_Valid_Float(Y) &&
  211. WWMath::Is_Valid_Float(Z) &&
  212. WWMath::Is_Valid_Float(W) );
  213. }
  214. #endif /* QUAT_H */