vector4.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. ** Command & Conquer Generals(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/vector4.h 16 5/11/01 7:11p Jani_p $ */
  19. /***************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***************************************************************************
  22. * *
  23. * Project Name : Westwood 3D *
  24. * *
  25. * File Name : VECTOR4.H *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 02/24/97 *
  30. * *
  31. * Last Update : June 2, 1997 [GH] *
  32. * *
  33. *-------------------------------------------------------------------------*
  34. * Functions: *
  35. * Scalar Division Operator -- Divide a vector by a scalar *
  36. * Scalar Multiply Operator -- Multiply a vector by a scalar *
  37. * Vector Addition Operator -- Add two vectors *
  38. * Vector Subtraction Operator -- Subract two vectors *
  39. * Vector Inner Product Operator -- Compute the inner or dot product *
  40. * Vector Equality Operator -- Detemine if two vectors are identical *
  41. * Vector Inequality Operator -- Detemine if two vectors are identical *
  42. * Swap -- swap two Vector4's *
  43. * Vector4::Is_Valid -- Vector4::Is_Valid *
  44. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  45. #if defined(_MSC_VER)
  46. #pragma once
  47. #endif
  48. #ifndef VECTOR4_H
  49. #define VECTOR4_H
  50. #include "always.h"
  51. #include "wwmath.h"
  52. #include <math.h>
  53. /*
  54. ** Vector4 - 4 dimensional vectors
  55. */
  56. class Vector4
  57. {
  58. public:
  59. float X;
  60. float Y;
  61. float Z;
  62. float W;
  63. // Constructors
  64. WWINLINE Vector4(void) {};
  65. WWINLINE Vector4(const Vector4 & v) { X = v.X; Y = v.Y; Z = v.Z; W = v.W; }
  66. WWINLINE Vector4(float x, float y, float z, float w) { X = x; Y = y; Z = z; W = w; }
  67. WWINLINE Vector4(const float vector[4]) { X = vector[0]; Y = vector[1]; Z = vector[2]; W = vector[3]; }
  68. // Assignment
  69. WWINLINE Vector4 & operator = (const Vector4 & v) { X = v.X; Y = v.Y; Z = v.Z; W = v.W; return *this; }
  70. WWINLINE void Set(float x, float y, float z, float w) { X = x; Y = y; Z = z; W = w; }
  71. // Array access
  72. WWINLINE float & operator [](int i) { return (&X)[i]; }
  73. WWINLINE const float & operator [](int i) const { return (&X)[i]; }
  74. // normalize, compute length
  75. void Normalize(void);
  76. WWINLINE float Length(void) const;
  77. WWINLINE float Length2(void) const;
  78. // unary operators
  79. WWINLINE Vector4 operator-() const { return(Vector4(-X,-Y,-Z,-W)); }
  80. WWINLINE Vector4 operator+() const { return *this; }
  81. WWINLINE Vector4 & operator += (const Vector4 & v) { X += v.X; Y += v.Y; Z += v.Z; W += v.W; return *this; }
  82. WWINLINE Vector4 & operator -= (const Vector4 & v) { X -= v.X; Y -= v.Y; Z -= v.Z; W += v.W; return *this; }
  83. WWINLINE Vector4 & operator *= (float k) { X = X*k; Y=Y*k; Z=Z*k; W=W*k; return *this; }
  84. WWINLINE Vector4 & operator /= (float k) { k=1.0f/k; X = X*k; Y=Y*k; Z=Z*k; W=W*k; return *this; }
  85. // scalar multiplication, division
  86. WWINLINE friend Vector4 operator * (const Vector4 &a,float k);
  87. WWINLINE friend Vector4 operator * (float k,const Vector4 &a);
  88. WWINLINE friend Vector4 operator / (const Vector4 &a,float k);
  89. // vector addition,subtraction
  90. WWINLINE friend Vector4 operator + (const Vector4 &a,const Vector4 &b);
  91. WWINLINE friend Vector4 operator - (const Vector4 &a,const Vector4 &b);
  92. // dot product / inner product
  93. WWINLINE friend float operator * (const Vector4 &a,const Vector4 &b);
  94. static WWINLINE float Dot_Product(const Vector4 &a,const Vector4 &b);
  95. // Equality operators
  96. friend bool operator == (const Vector4 &a,const Vector4 &b);
  97. friend bool operator != (const Vector4 &a,const Vector4 &b);
  98. // Linearly interpolate between two Vector4's
  99. static Vector4 Lerp(const Vector4 & a, const Vector4 & b, float alpha);
  100. static void Lerp(const Vector4 & a, const Vector4 & b, float alpha,Vector4 * set_result);
  101. // verify that none of the members of this vector are invalid floats
  102. WWINLINE bool Is_Valid(void) const;
  103. };
  104. /**************************************************************************
  105. * Scalar Multiply Operator -- Multiply a vector by a scalar *
  106. * *
  107. * INPUT: *
  108. * *
  109. * OUTPUT: *
  110. * *
  111. * WARNINGS: *
  112. * *
  113. * HISTORY: *
  114. * 02/24/1997 GH : Created. *
  115. *========================================================================*/
  116. WWINLINE Vector4 operator * (const Vector4 &a,float k)
  117. {
  118. return Vector4((a.X * k),(a.Y * k),(a.Z * k),(a.W * k));
  119. }
  120. WWINLINE Vector4 operator * (float k, const Vector4 &a)
  121. {
  122. return a*k;
  123. }
  124. /**************************************************************************
  125. * Scalar Division Operator -- Divide a vector by a scalar *
  126. * *
  127. * INPUT: *
  128. * *
  129. * OUTPUT: *
  130. * *
  131. * WARNINGS: *
  132. * *
  133. * HISTORY: *
  134. *========================================================================*/
  135. WWINLINE Vector4 operator / (const Vector4 &a,float k)
  136. {
  137. float ook=1.0f/k;
  138. return Vector4((a[0] * ook),(a[1] * ook),(a[2] * ook),(a[3] * ook));
  139. }
  140. /**************************************************************************
  141. * Vector Addition Operator -- Add two vectors *
  142. * *
  143. * INPUT: *
  144. * *
  145. * OUTPUT: *
  146. * *
  147. * WARNINGS: *
  148. * *
  149. * HISTORY: *
  150. * 02/24/1997 GH : Created. *
  151. *========================================================================*/
  152. WWINLINE Vector4 operator + (const Vector4 &a,const Vector4 &b)
  153. {
  154. return Vector4(
  155. a[0]+b[0],
  156. a[1]+b[1],
  157. a[2]+b[2],
  158. a[3]+b[3]
  159. );
  160. }
  161. /**************************************************************************
  162. * Vector Subtraction Operator -- Subract two vectors *
  163. * *
  164. * INPUT: *
  165. * *
  166. * OUTPUT: *
  167. * *
  168. * WARNINGS: *
  169. * *
  170. * HISTORY: *
  171. * 02/24/1997 GH : Created. *
  172. *========================================================================*/
  173. WWINLINE Vector4 operator - (const Vector4 &a,const Vector4 &b)
  174. {
  175. return Vector4(
  176. a[0]-b[0],
  177. a[1]-b[1],
  178. a[2]-b[2],
  179. a[3]-b[3]
  180. );
  181. }
  182. /**************************************************************************
  183. * Vector Inner Product -- Compute the inner or dot product of two vector *
  184. * *
  185. * INPUT: *
  186. * *
  187. * OUTPUT: *
  188. * *
  189. * WARNINGS: *
  190. * *
  191. * HISTORY: *
  192. *========================================================================*/
  193. WWINLINE float operator * (const Vector4 &a,const Vector4 &b)
  194. {
  195. return a[0]*b[0] +
  196. a[1]*b[1] +
  197. a[2]*b[2] +
  198. a[3]*b[3];
  199. }
  200. WWINLINE float Vector4::Dot_Product(const Vector4 &a,const Vector4 &b)
  201. {
  202. return a*b;
  203. }
  204. /**************************************************************************
  205. * Vector Equality Operator -- Detemine if two vectors are identical *
  206. * *
  207. * INPUT: *
  208. * *
  209. * OUTPUT: *
  210. * *
  211. * WARNINGS: *
  212. * *
  213. * HISTORY: *
  214. *========================================================================*/
  215. WWINLINE bool operator == (const Vector4 &a,const Vector4 &b)
  216. {
  217. return ( (a[0] == b[0]) && (a[1] == b[1]) && (a[2] == b[2]) && (a[3] == b[3]));
  218. }
  219. /**************************************************************************
  220. * Vector Inequality Operator -- Detemine if two vectors are identical *
  221. * *
  222. * INPUT: *
  223. * *
  224. * OUTPUT: *
  225. * *
  226. * WARNINGS: *
  227. * *
  228. * HISTORY: *
  229. *========================================================================*/
  230. WWINLINE bool operator != (const Vector4 &a,const Vector4 &b)
  231. {
  232. return ( (a[0] != b[0]) || (a[1] != b[1]) || (a[2] != b[2]) || (a[3] != b[3]));
  233. }
  234. /**************************************************************************
  235. * Vector4::Normalize -- Normalizes the vector. *
  236. * *
  237. * INPUT: *
  238. * *
  239. * OUTPUT: *
  240. * *
  241. * WARNINGS: *
  242. * *
  243. * HISTORY: *
  244. *========================================================================*/
  245. WWINLINE void Vector4::Normalize()
  246. {
  247. float len2 = Length2();
  248. if (len2 != 0.0f) {
  249. float oolen = WWMath::Inv_Sqrt(len2);
  250. X *= oolen;
  251. Y *= oolen;
  252. Z *= oolen;
  253. W *= oolen;
  254. }
  255. }
  256. WWINLINE Vector4 Normalize(const Vector4 & vec)
  257. {
  258. float len2 = vec.Length2();
  259. if (len2 != 0.0f) {
  260. float oolen = WWMath::Inv_Sqrt(len2);
  261. return vec * oolen;
  262. }
  263. return Vector4(0.0f,0.0f,0.0f,0.0f);
  264. }
  265. /**************************************************************************
  266. * Vector4::Length -- Returns the length of the vector *
  267. * *
  268. * INPUT: *
  269. * *
  270. * OUTPUT: *
  271. * *
  272. * WARNINGS: *
  273. * *
  274. * HISTORY: *
  275. *========================================================================*/
  276. WWINLINE float Vector4::Length() const
  277. {
  278. return WWMath::Sqrt(Length2());
  279. }
  280. /**************************************************************************
  281. * Vector4::Length -- Returns the square of the length of the vector *
  282. * *
  283. * INPUT: *
  284. * *
  285. * OUTPUT: *
  286. * *
  287. * WARNINGS: *
  288. * *
  289. * HISTORY: *
  290. *========================================================================*/
  291. WWINLINE float Vector4::Length2() const
  292. {
  293. return X*X + Y*Y + Z*Z + W*W;
  294. }
  295. /***********************************************************************************************
  296. * Swap -- swap two Vector4's *
  297. * *
  298. * INPUT: *
  299. * *
  300. * OUTPUT: *
  301. * *
  302. * WARNINGS: *
  303. * *
  304. * HISTORY: *
  305. * 06/02/1997 GH : Created. *
  306. *=============================================================================================*/
  307. WWINLINE void Swap(Vector4 & a,Vector4 & b)
  308. {
  309. Vector4 tmp(a);
  310. a = b;
  311. b = tmp;
  312. }
  313. /***********************************************************************************************
  314. * Lerp -- linearly interpolate two Vector4's by an interpolation factor. *
  315. * *
  316. * INPUT: *
  317. * *
  318. * OUTPUT: *
  319. * *
  320. * WARNINGS: No checking is done to ensure that alpha is between 0 and 1. *
  321. * *
  322. * HISTORY: *
  323. * 01/14/1999 NH : Created. *
  324. *=============================================================================================*/
  325. WWINLINE Vector4 Lerp(const Vector4 & a, const Vector4 & b, float alpha)
  326. {
  327. return Vector4(
  328. (a.X + (b.X - a.X)*alpha),
  329. (a.Y + (b.Y - a.Y)*alpha),
  330. (a.Z + (b.Z - a.Z)*alpha),
  331. (a.W + (b.W - a.W)*alpha)
  332. );
  333. }
  334. WWINLINE Vector4 Vector4::Lerp(const Vector4 & a, const Vector4 & b, float alpha)
  335. {
  336. return Vector4(
  337. (a.X + (b.X - a.X)*alpha),
  338. (a.Y + (b.Y - a.Y)*alpha),
  339. (a.Z + (b.Z - a.Z)*alpha),
  340. (a.W + (b.W - a.W)*alpha)
  341. );
  342. }
  343. WWINLINE void Vector4::Lerp(const Vector4 & a, const Vector4 & b, float alpha,Vector4 * set_result)
  344. {
  345. set_result->X = (a.X + (b.X - a.X)*alpha);
  346. set_result->Y = (a.Y + (b.Y - a.Y)*alpha);
  347. set_result->Z = (a.Z + (b.Z - a.Z)*alpha);
  348. set_result->X = (a.W + (b.W - a.W)*alpha);
  349. }
  350. /***********************************************************************************************
  351. * Vector4::Is_Valid -- Vector4::Is_Valid *
  352. * *
  353. * verifies that all members of this vector are valid floats *
  354. * *
  355. * *
  356. * INPUT: *
  357. * *
  358. * OUTPUT: *
  359. * *
  360. * WARNINGS: *
  361. * *
  362. * HISTORY: *
  363. * 10/18/99 gth : Created. *
  364. *=============================================================================================*/
  365. WWINLINE bool Vector4::Is_Valid(void) const
  366. {
  367. return (WWMath::Is_Valid_Float(X) && WWMath::Is_Valid_Float(Y) && WWMath::Is_Valid_Float(Z) && WWMath::Is_Valid_Float(W));
  368. }
  369. #endif /* VECTOR4_H */