vector3.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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/vector3.h 40 5/11/01 7:11p Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Westwood 3D *
  24. * *
  25. * File Name : VECTOR3.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. * 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 -- Determine if two vectors are identical *
  41. * Vector Inequality Operator -- Determine if two vectors are identical *
  42. * Equal_Within_Epsilon -- Determine if two vectors are identical within *
  43. * Cross_Product -- compute the cross product of two vectors *
  44. * Vector3::Normalize -- Normalizes the vector. *
  45. * Vector3::Length -- Returns the length of the vector *
  46. * Vector3::Length2 -- Returns the square of the length of the vector *
  47. * Vector3::Quick_Length -- returns a quick approximation of the length *
  48. * Swap -- swap two Vector3's *
  49. * Lerp -- linearly interpolate two Vector3's by an interpolation factor. *
  50. * Lerp -- linearly interpolate two Vector3's without return-by-value *
  51. * Vector3::Add -- Add two vector3's without return-by-value *
  52. * Vector3::Subtract -- Subtract two vector3's without return-by-value *
  53. * Vector3::Update_Min -- sets each component of the vector to the min of this and a *
  54. * Vector3::Update_Max -- Sets each component of the vector to the max of this and a *
  55. * Vector3::Scale -- scale this vector by 3 independent scale factors *
  56. * Vector3::Rotate_X -- rotates this vector around the X axis *
  57. * Vector3::Rotate_X -- Rotates this vector around the x axis *
  58. * Vector3::Rotate_Y -- Rotates this vector around the y axis *
  59. * Vector3::Rotate_Y -- Rotates this vector around the Y axis *
  60. * Vector3::Rotate_Z -- Rotates this vector around the Z axis *
  61. * Vector3::Rotate_Z -- Rotates this vector around the Z axis *
  62. * Vector3::Is_Valid -- Verifies that each component of this vector is a valid float *
  63. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  64. #if defined(_MSC_VER)
  65. #pragma once
  66. #endif
  67. #ifndef VECTOR3_H
  68. #define VECTOR3_H
  69. #include "always.h"
  70. #include "wwmath.h"
  71. #include <assert.h>
  72. #ifdef _UNIX
  73. #include "osdep.h"
  74. #endif
  75. /*
  76. ** Vector3 - 3-Dimensional Vectors
  77. */
  78. class Vector3
  79. {
  80. public:
  81. float X;
  82. float Y;
  83. float Z;
  84. // Constructors
  85. WWINLINE Vector3(void) {};
  86. WWINLINE Vector3(const Vector3 & v) { X = v.X; Y = v.Y; Z = v.Z; }
  87. WWINLINE Vector3(float x, float y, float z) { X = x; Y = y; Z = z; }
  88. WWINLINE Vector3(const float vector[3]) { X = vector[0]; Y = vector[1]; Z = vector[2]; }
  89. // Assignment
  90. WWINLINE Vector3 & operator = (const Vector3 & v) { X = v.X; Y = v.Y; Z = v.Z; return *this; }
  91. WWINLINE void Set(float x, float y, float z) { X = x; Y = y; Z = z; }
  92. WWINLINE void Set(const Vector3 & that) { X = that.X; Y = that.Y; Z = that.Z; }
  93. // Array access
  94. WWINLINE float & operator [](int i) { return (&X)[i]; }
  95. WWINLINE const float & operator [](int i) const { return (&X)[i]; }
  96. // normalize, compute length
  97. void Normalize(void);
  98. WWINLINE float Length(void) const;
  99. WWINLINE float Length2(void) const;
  100. float Quick_Length(void) const;
  101. void Scale(const Vector3 & scale);
  102. // rotation, (warning, modifies this vector!)
  103. WWINLINE void Rotate_X(float angle);
  104. WWINLINE void Rotate_X(float s_angle,float c_angle);
  105. WWINLINE void Rotate_Y(float angle);
  106. WWINLINE void Rotate_Y(float s_angle,float c_angle);
  107. WWINLINE void Rotate_Z(float angle);
  108. WWINLINE void Rotate_Z(float s_angle,float c_angle);
  109. // unary operators
  110. WWINLINE Vector3 operator-() const { return(Vector3(-X,-Y,-Z)); }
  111. WWINLINE Vector3 operator+() const { return *this; }
  112. WWINLINE Vector3 & operator += (const Vector3 & v) { X += v.X; Y += v.Y; Z += v.Z; return *this; }
  113. WWINLINE Vector3 & operator -= (const Vector3 & v) { X -= v.X; Y -= v.Y; Z -= v.Z; return *this; }
  114. WWINLINE Vector3 & operator *= (float k) { X = X*k; Y=Y*k; Z=Z*k; return *this; }
  115. WWINLINE Vector3 & operator /= (float k) { float ook=1.0f/k; X=X*ook; Y=Y*ook; Z=Z*ook; return *this; }
  116. // scalar multiplication, division
  117. WWINLINE friend Vector3 operator * (const Vector3 &a,float k);
  118. WWINLINE friend Vector3 operator * (float k,const Vector3 &a);
  119. WWINLINE friend Vector3 operator / (const Vector3 &a,float k);
  120. // vector addition,subtraction
  121. WWINLINE friend Vector3 operator + (const Vector3 &a,const Vector3 &b);
  122. WWINLINE friend Vector3 operator - (const Vector3 &a,const Vector3 &b);
  123. // Equality operators
  124. friend bool operator == (const Vector3 &a,const Vector3 &b);
  125. friend bool operator != (const Vector3 &a,const Vector3 &b);
  126. WWINLINE friend bool Equal_Within_Epsilon(const Vector3 &a,const Vector3 &b,float epsilon);
  127. // dot product / inner product
  128. WWINLINE friend float operator * (const Vector3 &a,const Vector3 &b);
  129. static WWINLINE float Dot_Product(const Vector3 &a,const Vector3 &b);
  130. // cross product / outer product
  131. static WWINLINE Vector3 Cross_Product(const Vector3 &a,const Vector3 &b);
  132. static WWINLINE void Cross_Product(const Vector3 &a,const Vector3 &b,Vector3 * result);
  133. static WWINLINE float Cross_Product_X(const Vector3 &a,const Vector3 &b);
  134. static WWINLINE float Cross_Product_Y(const Vector3 &a,const Vector3 &b);
  135. static WWINLINE float Cross_Product_Z(const Vector3 &a,const Vector3 &b);
  136. // add and subtract without return by value
  137. static WWINLINE void Add(const Vector3 & a,const Vector3 & b,Vector3 * c);
  138. static WWINLINE void Subtract(const Vector3 & a,const Vector3 & b,Vector3 * c);
  139. // Line intersection functions.
  140. static WWINLINE float Find_X_At_Y(float y, const Vector3 &p1, const Vector3 &p2);
  141. static WWINLINE float Find_X_At_Z(float z, const Vector3 &p1, const Vector3 &p2);
  142. static WWINLINE float Find_Y_At_X(float x, const Vector3 &p1, const Vector3 &p2);
  143. static WWINLINE float Find_Y_At_Z(float z, const Vector3 &p1, const Vector3 &p2);
  144. static WWINLINE float Find_Z_At_X(float x, const Vector3 &p1, const Vector3 &p2);
  145. static WWINLINE float Find_Z_At_Y(float z, const Vector3 &p1, const Vector3 &p2);
  146. // make this vector the min or max of itself and the passed vector
  147. WWINLINE void Update_Min(const Vector3 & a);
  148. WWINLINE void Update_Max(const Vector3 & a);
  149. WWINLINE void Cap_Absolute_To(const Vector3 & a);
  150. // verify that none of the members of this vector are invalid floats
  151. WWINLINE bool Is_Valid(void) const;
  152. static WWINLINE float Quick_Distance(const Vector3 &p1, const Vector3 &p2);
  153. static WWINLINE float Distance(const Vector3 &p1, const Vector3 &p2);
  154. // Linearly interpolate two Vector3's
  155. static void Lerp(const Vector3 & a, const Vector3 & b, float alpha,Vector3 * set_result);
  156. // Color Conversion
  157. WWINLINE unsigned long Convert_To_ABGR( void ) const;
  158. WWINLINE unsigned long Convert_To_ARGB( void ) const;
  159. };
  160. /**************************************************************************
  161. * Scalar Multiply Operator -- Multiply a vector by a scalar *
  162. * *
  163. * INPUT: *
  164. * *
  165. * OUTPUT: *
  166. * *
  167. * WARNINGS: *
  168. * *
  169. * HISTORY: *
  170. * 02/24/1997 GH : Created. *
  171. *========================================================================*/
  172. WWINLINE Vector3 operator * (const Vector3 &a,float k)
  173. {
  174. return Vector3((a.X * k),(a.Y * k),(a.Z * k));
  175. }
  176. WWINLINE Vector3 operator * (float k, const Vector3 &a)
  177. {
  178. return Vector3((a.X * k),(a.Y * k),(a.Z * k));
  179. }
  180. /**************************************************************************
  181. * Scalar Division Operator -- Divide a vector by a scalar *
  182. * *
  183. * INPUT: *
  184. * *
  185. * OUTPUT: *
  186. * *
  187. * WARNINGS: *
  188. * *
  189. * HISTORY: *
  190. *========================================================================*/
  191. WWINLINE Vector3 operator / (const Vector3 &a,float k)
  192. {
  193. float ook = 1.0f/k;
  194. return Vector3((a.X * ook),(a.Y * ook),(a.Z * ook));
  195. }
  196. /**************************************************************************
  197. * Vector Addition Operator -- Add two vectors *
  198. * *
  199. * INPUT: *
  200. * *
  201. * OUTPUT: *
  202. * *
  203. * WARNINGS: *
  204. * *
  205. * HISTORY: *
  206. * 02/24/1997 GH : Created. *
  207. *========================================================================*/
  208. WWINLINE Vector3 operator + (const Vector3 &a,const Vector3 &b)
  209. {
  210. return Vector3(
  211. a.X+b.X,
  212. a.Y+b.Y,
  213. a.Z+b.Z
  214. );
  215. }
  216. /**************************************************************************
  217. * Vector Subtraction Operator -- Subract two vectors *
  218. * *
  219. * INPUT: *
  220. * *
  221. * OUTPUT: *
  222. * *
  223. * WARNINGS: *
  224. * *
  225. * HISTORY: *
  226. * 02/24/1997 GH : Created. *
  227. *========================================================================*/
  228. WWINLINE Vector3 operator - (const Vector3 &a,const Vector3 &b)
  229. {
  230. return Vector3(
  231. a.X-b.X,
  232. a.Y-b.Y,
  233. a.Z-b.Z
  234. );
  235. }
  236. /**************************************************************************
  237. * Vector Inner Product -- Compute the inner or dot product of two vector *
  238. * *
  239. * INPUT: *
  240. * *
  241. * OUTPUT: *
  242. * *
  243. * WARNINGS: *
  244. * *
  245. * HISTORY: *
  246. *========================================================================*/
  247. WWINLINE float operator * (const Vector3 &a,const Vector3 &b)
  248. {
  249. return a.X*b.X +
  250. a.Y*b.Y +
  251. a.Z*b.Z;
  252. }
  253. WWINLINE float Vector3::Dot_Product(const Vector3 &a,const Vector3 &b)
  254. {
  255. return a*b;
  256. }
  257. /**************************************************************************
  258. * Vector Equality Operator -- Determine if two vectors are identical *
  259. * *
  260. * INPUT: *
  261. * *
  262. * OUTPUT: *
  263. * *
  264. * WARNINGS: *
  265. * *
  266. * HISTORY: *
  267. *========================================================================*/
  268. WWINLINE bool operator == (const Vector3 &a,const Vector3 &b)
  269. {
  270. return ( (a.X == b.X) && (a.Y == b.Y) && (a.Z == b.Z));
  271. }
  272. /**************************************************************************
  273. * Vector Inequality Operator -- Determine if two vectors are identical *
  274. * *
  275. * INPUT: *
  276. * *
  277. * OUTPUT: *
  278. * *
  279. * WARNINGS: *
  280. * *
  281. * HISTORY: *
  282. *========================================================================*/
  283. WWINLINE bool operator != (const Vector3 &a,const Vector3 &b)
  284. {
  285. return ( (a.X != b.X) || (a.Y != b.Y) || (a.Z != b.Z));
  286. }
  287. /**************************************************************************
  288. * Equal_Within_Epsilon -- Determine if two vectors are identical within e*
  289. * *
  290. * INPUT: *
  291. * *
  292. * OUTPUT: *
  293. * *
  294. * WARNINGS: *
  295. * *
  296. * HISTORY: *
  297. *========================================================================*/
  298. WWINLINE bool Equal_Within_Epsilon(const Vector3 &a,const Vector3 &b,float epsilon)
  299. {
  300. return( (WWMath::Fabs(a.X - b.X) < epsilon) &&
  301. (WWMath::Fabs(a.Y - b.Y) < epsilon) &&
  302. (WWMath::Fabs(a.Z - b.Z) < epsilon) );
  303. }
  304. /**************************************************************************
  305. * Cross_Product -- compute the cross product of two vectors *
  306. * *
  307. * INPUT: *
  308. * *
  309. * OUTPUT: *
  310. * *
  311. * WARNINGS: *
  312. * *
  313. * HISTORY: *
  314. *========================================================================*/
  315. WWINLINE Vector3 Vector3::Cross_Product(const Vector3 &a,const Vector3 &b)
  316. {
  317. return Vector3(
  318. (a.Y * b.Z - a.Z * b.Y),
  319. (a.Z * b.X - a.X * b.Z),
  320. (a.X * b.Y - a.Y * b.X)
  321. );
  322. }
  323. WWINLINE void Vector3::Cross_Product(const Vector3 &a,const Vector3 &b,Vector3 * set_result)
  324. {
  325. assert(set_result != &a);
  326. set_result->X = (a.Y * b.Z - a.Z * b.Y);
  327. set_result->Y = (a.Z * b.X - a.X * b.Z);
  328. set_result->Z = (a.X * b.Y - a.Y * b.X);
  329. }
  330. WWINLINE float Vector3::Cross_Product_X(const Vector3 &a,const Vector3 &b)
  331. {
  332. return a.Y * b.Z - a.Z * b.Y;
  333. }
  334. WWINLINE float Vector3::Cross_Product_Y(const Vector3 &a,const Vector3 &b)
  335. {
  336. return a.Z * b.X - a.X * b.Z;
  337. }
  338. WWINLINE float Vector3::Cross_Product_Z(const Vector3 &a,const Vector3 &b)
  339. {
  340. return a.X * b.Y - a.Y * b.X;
  341. }
  342. /**************************************************************************
  343. * Vector3::Normalize -- Normalizes the vector. *
  344. * *
  345. * INPUT: *
  346. * *
  347. * OUTPUT: *
  348. * *
  349. * WARNINGS: *
  350. * *
  351. * HISTORY: *
  352. *========================================================================*/
  353. WWINLINE void Vector3::Normalize()
  354. {
  355. float len2 = Length2();
  356. if (len2 != 0.0f)
  357. {
  358. float oolen = WWMath::Inv_Sqrt(len2);
  359. X *= oolen;
  360. Y *= oolen;
  361. Z *= oolen;
  362. }
  363. }
  364. #ifdef ALLOW_TEMPORARIES
  365. WWINLINE Vector3 Normalize(const Vector3 & vec)
  366. {
  367. float len2 = vec.Length2();
  368. if (len2 != 0.0f)
  369. {
  370. float oolen = WWMath::Inv_Sqrt(len2);
  371. return vec * oolen;
  372. }
  373. return vec;
  374. }
  375. #endif
  376. /**************************************************************************
  377. * Vector3::Length -- Returns the length of the vector *
  378. * *
  379. * INPUT: *
  380. * *
  381. * OUTPUT: *
  382. * *
  383. * WARNINGS: *
  384. * *
  385. * HISTORY: *
  386. *========================================================================*/
  387. WWINLINE float Vector3::Length() const
  388. {
  389. return WWMath::Sqrt(Length2());
  390. }
  391. /**************************************************************************
  392. * Vector3::Length2 -- Returns the square of the length of the vector *
  393. * *
  394. * INPUT: *
  395. * *
  396. * OUTPUT: *
  397. * *
  398. * WARNINGS: *
  399. * *
  400. * HISTORY: *
  401. *========================================================================*/
  402. WWINLINE float Vector3::Length2() const
  403. {
  404. return X*X + Y*Y + Z*Z;
  405. }
  406. /***********************************************************************************************
  407. * Vector3::Quick_Length -- returns a quick approximation of the length *
  408. * *
  409. * INPUT: *
  410. * *
  411. * OUTPUT: *
  412. * *
  413. * WARNINGS: *
  414. * *
  415. * HISTORY: *
  416. * 7/15/98 GTH : Created. *
  417. *=============================================================================================*/
  418. WWINLINE float Vector3::Quick_Length(void) const
  419. {
  420. // this method of approximating the length comes from Graphics Gems 1 and
  421. // supposedly gives an error of +/- 8%
  422. float max = WWMath::Fabs(X);
  423. float mid = WWMath::Fabs(Y);
  424. float min = WWMath::Fabs(Z);
  425. float tmp;
  426. if (max < mid) { tmp = max; max = mid; mid = tmp; }
  427. if (max < min) { tmp = max; max = min; min = tmp; }
  428. if (mid < min) { tmp = mid; mid = min; min = mid; }
  429. return max + (11.0f / 32.0f)*mid + (1.0f / 4.0f)*min;
  430. }
  431. /***********************************************************************************************
  432. * Swap -- swap two Vector3's *
  433. * *
  434. * INPUT: *
  435. * *
  436. * OUTPUT: *
  437. * *
  438. * WARNINGS: *
  439. * *
  440. * HISTORY: *
  441. * 08/11/1997 GH : Created. *
  442. *=============================================================================================*/
  443. WWINLINE void Swap(Vector3 & a,Vector3 & b)
  444. {
  445. Vector3 tmp(a);
  446. a = b;
  447. b = tmp;
  448. }
  449. /***********************************************************************************************
  450. * Lerp -- linearly interpolate two Vector3's by an interpolation factor. *
  451. * *
  452. * INPUT: *
  453. * *
  454. * OUTPUT: *
  455. * *
  456. * WARNINGS: No checking is done to ensure that alpha is between 0 and 1. *
  457. * *
  458. * HISTORY: *
  459. * 08/11/1997 GH : Created. *
  460. *=============================================================================================*/
  461. WWINLINE Vector3 Lerp(const Vector3 & a, const Vector3 & b, float alpha)
  462. {
  463. return Vector3(
  464. (a.X + (b.X - a.X)*alpha),
  465. (a.Y + (b.Y - a.Y)*alpha),
  466. (a.Z + (b.Z - a.Z)*alpha)
  467. );
  468. }
  469. /***********************************************************************************************
  470. * Lerp -- linearly interpolate two Vector3's without return-by-value *
  471. * *
  472. * INPUT: *
  473. * *
  474. * OUTPUT: *
  475. * *
  476. * WARNINGS: *
  477. * *
  478. * HISTORY: *
  479. * 10/18/99 gth : Created. *
  480. *=============================================================================================*/
  481. WWINLINE void Lerp(const Vector3 & a, const Vector3 & b, float alpha,Vector3 * set_result)
  482. {
  483. assert(set_result != NULL);
  484. set_result->X = (a.X + (b.X - a.X)*alpha);
  485. set_result->Y = (a.Y + (b.Y - a.Y)*alpha);
  486. set_result->Z = (a.Z + (b.Z - a.Z)*alpha);
  487. }
  488. WWINLINE void Vector3::Lerp(const Vector3 & a, const Vector3 & b, float alpha,Vector3 * set_result)
  489. {
  490. assert(set_result != NULL);
  491. set_result->X = (a.X + (b.X - a.X)*alpha);
  492. set_result->Y = (a.Y + (b.Y - a.Y)*alpha);
  493. set_result->Z = (a.Z + (b.Z - a.Z)*alpha);
  494. }
  495. /***********************************************************************************************
  496. * Vector3::Add -- Add two vector3's without return-by-value *
  497. * *
  498. * INPUT: *
  499. * *
  500. * OUTPUT: *
  501. * *
  502. * WARNINGS: *
  503. * *
  504. * HISTORY: *
  505. * 10/18/99 gth : Created. *
  506. *=============================================================================================*/
  507. WWINLINE void Vector3::Add(const Vector3 &a,const Vector3 &b,Vector3 * set_result)
  508. {
  509. assert(set_result != NULL);
  510. set_result->X = a.X + b.X;
  511. set_result->Y = a.Y + b.Y;
  512. set_result->Z = a.Z + b.Z;
  513. }
  514. /***********************************************************************************************
  515. * Vector3::Subtract -- Subtract two vector3's without return-by-value *
  516. * *
  517. * INPUT: *
  518. * *
  519. * OUTPUT: *
  520. * *
  521. * WARNINGS: *
  522. * *
  523. * HISTORY: *
  524. * 10/18/99 gth : Created. *
  525. *=============================================================================================*/
  526. WWINLINE void Vector3::Subtract(const Vector3 &a,const Vector3 &b,Vector3 * set_result)
  527. {
  528. assert(set_result != NULL);
  529. set_result->X = a.X - b.X;
  530. set_result->Y = a.Y - b.Y;
  531. set_result->Z = a.Z - b.Z;
  532. }
  533. /***********************************************************************************************
  534. * Vector3::Update_Min -- sets each component of the vector to the min of this and a *
  535. * *
  536. * INPUT: *
  537. * *
  538. * OUTPUT: *
  539. * *
  540. * WARNINGS: *
  541. * *
  542. * HISTORY: *
  543. * 10/18/99 gth : Created. *
  544. *=============================================================================================*/
  545. WWINLINE void Vector3::Update_Min(const Vector3 & a)
  546. {
  547. if (a.X < X) X = a.X;
  548. if (a.Y < Y) Y = a.Y;
  549. if (a.Z < Z) Z = a.Z;
  550. }
  551. /***********************************************************************************************
  552. * Vector3::Update_Max -- Sets each component of the vector to the max of this and a *
  553. * *
  554. * INPUT: *
  555. * *
  556. * OUTPUT: *
  557. * *
  558. * WARNINGS: *
  559. * *
  560. * HISTORY: *
  561. * 10/18/99 gth : Created. *
  562. *=============================================================================================*/
  563. WWINLINE void Vector3::Update_Max(const Vector3 & a)
  564. {
  565. if (a.X > X) X = a.X;
  566. if (a.Y > Y) Y = a.Y;
  567. if (a.Z > Z) Z = a.Z;
  568. }
  569. /***********************************************************************************************
  570. * Vector3::Cap_To_Absolute_Of -- Sets each component of the vector to no larger than the -ve or +ve of*
  571. * *
  572. * INPUT: *
  573. * *
  574. * OUTPUT: *
  575. * *
  576. * WARNINGS: *
  577. * *
  578. * HISTORY: *
  579. * 11/29/99 wst : Created. *
  580. *=============================================================================================*/
  581. WWINLINE void Vector3::Cap_Absolute_To(const Vector3 & a)
  582. {
  583. if (X > 0)
  584. {
  585. if (a.X < X) X = a.X;
  586. }
  587. else
  588. {
  589. if (-a.X > X) X = -a.X;
  590. }
  591. if (Y > 0)
  592. {
  593. if (a.Y < Y) Y = a.Y;
  594. }
  595. else
  596. {
  597. if (-a.Y > Y) Y = -a.Y;
  598. }
  599. if (Z > 0)
  600. {
  601. if (a.Z < Z) Z = a.Z;
  602. }
  603. else
  604. {
  605. if (-a.Z > Z) Z = -a.Z;
  606. }
  607. }
  608. /***********************************************************************************************
  609. * Vector3::Scale -- scale this vector by 3 independent scale factors *
  610. * *
  611. * INPUT: *
  612. * *
  613. * OUTPUT: *
  614. * *
  615. * WARNINGS: *
  616. * *
  617. * HISTORY: *
  618. * 10/18/99 gth : Created. *
  619. *=============================================================================================*/
  620. WWINLINE void Vector3::Scale(const Vector3 & scale)
  621. {
  622. X *= scale.X;
  623. Y *= scale.Y;
  624. Z *= scale.Z;
  625. }
  626. /***********************************************************************************************
  627. * Vector3::Rotate_X -- rotates this vector around the X axis *
  628. * *
  629. * INPUT: *
  630. * *
  631. * OUTPUT: *
  632. * *
  633. * WARNINGS: *
  634. * *
  635. * HISTORY: *
  636. * 10/18/99 gth : Created. *
  637. *=============================================================================================*/
  638. WWINLINE void Vector3::Rotate_X(float angle)
  639. {
  640. Rotate_X(sinf(angle),cosf(angle));
  641. }
  642. /***********************************************************************************************
  643. * Vector3::Rotate_X -- Rotates this vector around the x axis *
  644. * *
  645. * INPUT: *
  646. * *
  647. * OUTPUT: *
  648. * *
  649. * WARNINGS: *
  650. * *
  651. * HISTORY: *
  652. * 10/18/99 gth : Created. *
  653. *=============================================================================================*/
  654. WWINLINE void Vector3::Rotate_X(float s_angle,float c_angle)
  655. {
  656. float tmp_y = Y;
  657. float tmp_z = Z;
  658. Y = c_angle * tmp_y - s_angle * tmp_z;
  659. Z = s_angle * tmp_y + c_angle * tmp_z;
  660. }
  661. /***********************************************************************************************
  662. * Vector3::Rotate_Y -- Rotates this vector around the y axis *
  663. * *
  664. * INPUT: *
  665. * *
  666. * OUTPUT: *
  667. * *
  668. * WARNINGS: *
  669. * *
  670. * HISTORY: *
  671. * 10/18/99 gth : Created. *
  672. *=============================================================================================*/
  673. WWINLINE void Vector3::Rotate_Y(float angle)
  674. {
  675. Rotate_Y(sinf(angle),cosf(angle));
  676. }
  677. /***********************************************************************************************
  678. * Vector3::Rotate_Y -- Rotates this vector around the Y axis *
  679. * *
  680. * INPUT: *
  681. * *
  682. * OUTPUT: *
  683. * *
  684. * WARNINGS: *
  685. * *
  686. * HISTORY: *
  687. * 10/18/99 gth : Created. *
  688. *=============================================================================================*/
  689. WWINLINE void Vector3::Rotate_Y(float s_angle,float c_angle)
  690. {
  691. float tmp_x = X;
  692. float tmp_z = Z;
  693. X = c_angle * tmp_x + s_angle * tmp_z;
  694. Z = -s_angle * tmp_x + c_angle * tmp_z;
  695. }
  696. /***********************************************************************************************
  697. * Vector3::Rotate_Z -- Rotates this vector around the Z axis *
  698. * *
  699. * INPUT: *
  700. * *
  701. * OUTPUT: *
  702. * *
  703. * WARNINGS: *
  704. * *
  705. * HISTORY: *
  706. * 10/18/99 gth : Created. *
  707. *=============================================================================================*/
  708. WWINLINE void Vector3::Rotate_Z(float angle)
  709. {
  710. Rotate_Z(sinf(angle),cosf(angle));
  711. }
  712. /***********************************************************************************************
  713. * Vector3::Rotate_Z -- Rotates this vector around the Z axis *
  714. * *
  715. * INPUT: *
  716. * *
  717. * OUTPUT: *
  718. * *
  719. * WARNINGS: *
  720. * *
  721. * HISTORY: *
  722. * 10/18/99 gth : Created. *
  723. *=============================================================================================*/
  724. WWINLINE void Vector3::Rotate_Z(float s_angle,float c_angle)
  725. {
  726. float tmp_x = X;
  727. float tmp_y = Y;
  728. X = c_angle * tmp_x - s_angle * tmp_y;
  729. Y = s_angle * tmp_x + c_angle * tmp_y;
  730. }
  731. /***********************************************************************************************
  732. * Vector3::Is_Valid -- Verifies that each component of this vector is a valid float *
  733. * *
  734. * INPUT: *
  735. * *
  736. * OUTPUT: *
  737. * *
  738. * WARNINGS: *
  739. * *
  740. * HISTORY: *
  741. * 10/18/99 gth : Created. *
  742. *=============================================================================================*/
  743. WWINLINE bool Vector3::Is_Valid(void) const
  744. {
  745. return (WWMath::Is_Valid_Float(X) && WWMath::Is_Valid_Float(Y) && WWMath::Is_Valid_Float(Z));
  746. }
  747. WWINLINE float Vector3::Find_X_At_Y(float y, const Vector3 &p1, const Vector3 &p2)
  748. {
  749. return(p1.X + ((y - p1.Y) * ((p2.X - p1.X) / (p2.Y - p1.Y))));
  750. }
  751. WWINLINE float Vector3::Find_X_At_Z(float z, const Vector3 &p1, const Vector3 &p2)
  752. {
  753. return(p1.X + ((z - p1.Z) * ((p2.X - p1.X) / (p2.Z - p1.Z))));
  754. }
  755. WWINLINE float Vector3::Find_Y_At_X(float x, const Vector3 &p1, const Vector3 &p2)
  756. {
  757. return(p1.Y + ((x - p1.X) * ((p2.Y - p1.Y) / (p2.X - p1.X))));
  758. }
  759. WWINLINE float Vector3::Find_Y_At_Z(float z, const Vector3 &p1, const Vector3 &p2)
  760. {
  761. return(p1.Y + ((z - p1.Z) * ((p2.Y - p1.Y) / (p2.Z - p1.Z))));
  762. }
  763. WWINLINE float Vector3::Find_Z_At_X(float x, const Vector3 &p1, const Vector3 &p2)
  764. {
  765. return(p1.Z + ((x - p1.X) * ((p2.Z - p1.Z) / (p2.X - p1.X))));
  766. }
  767. WWINLINE float Vector3::Find_Z_At_Y(float y, const Vector3 &p1, const Vector3 &p2)
  768. {
  769. return(p1.Z + ((y - p1.Y) * ((p2.Z - p1.Z) / (p2.Y - p1.Y))));
  770. }
  771. /***********************************************************************************************
  772. * Vector3::Distance -- Accurate distance calculation. *
  773. * *
  774. * *
  775. * *
  776. * *
  777. * HISTORY: *
  778. * 11/29/1999MLL: Created. *
  779. *=============================================================================================*/
  780. WWINLINE float Vector3::Distance(const Vector3 &p1, const Vector3 &p2)
  781. {
  782. Vector3 temp;
  783. temp = p1 - p2;
  784. return (temp.Length());
  785. }
  786. /***********************************************************************************************
  787. * Vector3::Quick_Distance -- Fast but inaccurate distance calculation. *
  788. * *
  789. * *
  790. * *
  791. * *
  792. * HISTORY: *
  793. * 11/29/1999MLL: Created. *
  794. *=============================================================================================*/
  795. WWINLINE float Vector3::Quick_Distance(const Vector3 &p1, const Vector3 &p2)
  796. {
  797. Vector3 temp;
  798. temp = p1 - p2;
  799. return (temp.Quick_Length());
  800. }
  801. /***********************************************************************************************
  802. * Vector3::Convert_To_ABGR -- Converts to SR packed color . *
  803. * *
  804. * *
  805. * *
  806. * *
  807. * HISTORY: *
  808. * 11/29/1999MLL: Created. *
  809. *=============================================================================================*/
  810. WWINLINE unsigned long Vector3::Convert_To_ABGR( void ) const
  811. {
  812. return (unsigned(255)<<24) |
  813. (unsigned(Z*255.0f)<<16) |
  814. (unsigned(Y*255.0f)<<8) |
  815. (unsigned(X*255.0f));
  816. }
  817. /***********************************************************************************************
  818. * Vector3::Convert_To_ARGB -- Converts to packed color . *
  819. * *
  820. * *
  821. * *
  822. * *
  823. * HISTORY: *
  824. * 11/29/1999MLL: Created. *
  825. *=============================================================================================*/
  826. WWINLINE unsigned long Vector3::Convert_To_ARGB( void ) const
  827. {
  828. return (unsigned(255)<<24) |
  829. (unsigned(X*255.0f)<<16) |
  830. (unsigned(Y*255.0f)<<8) |
  831. (unsigned(Z*255.0f));
  832. }
  833. #endif /* Vector3_H */