vector3.h 49 KB

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