vector3.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/wwmath/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. float oolen = WWMath::Inv_Sqrt(Length2());
  358. X *= oolen;
  359. Y *= oolen;
  360. Z *= oolen;
  361. }
  362. }
  363. WWINLINE Vector3 Normalize(const Vector3 & vec)
  364. {
  365. float len2 = vec.Length2();
  366. if (len2 != 0.0f) {
  367. float oolen = WWMath::Inv_Sqrt(len2);
  368. return vec * oolen;
  369. }
  370. return vec;
  371. }
  372. /**************************************************************************
  373. * Vector3::Length -- Returns the length of the vector *
  374. * *
  375. * INPUT: *
  376. * *
  377. * OUTPUT: *
  378. * *
  379. * WARNINGS: *
  380. * *
  381. * HISTORY: *
  382. *========================================================================*/
  383. WWINLINE float Vector3::Length() const
  384. {
  385. return WWMath::Sqrt(Length2());
  386. }
  387. /**************************************************************************
  388. * Vector3::Length2 -- Returns the square of the length of the vector *
  389. * *
  390. * INPUT: *
  391. * *
  392. * OUTPUT: *
  393. * *
  394. * WARNINGS: *
  395. * *
  396. * HISTORY: *
  397. *========================================================================*/
  398. WWINLINE float Vector3::Length2() const
  399. {
  400. return X*X + Y*Y + Z*Z;
  401. }
  402. /***********************************************************************************************
  403. * Vector3::Quick_Length -- returns a quick approximation of the length *
  404. * *
  405. * INPUT: *
  406. * *
  407. * OUTPUT: *
  408. * *
  409. * WARNINGS: *
  410. * *
  411. * HISTORY: *
  412. * 7/15/98 GTH : Created. *
  413. *=============================================================================================*/
  414. WWINLINE float Vector3::Quick_Length(void) const
  415. {
  416. // this method of approximating the length comes from Graphics Gems 1 and
  417. // supposedly gives an error of +/- 8%
  418. float max = WWMath::Fabs(X);
  419. float mid = WWMath::Fabs(Y);
  420. float min = WWMath::Fabs(Z);
  421. float tmp;
  422. if (max < mid) { tmp = max; max = mid; mid = tmp; }
  423. if (max < min) { tmp = max; max = min; min = tmp; }
  424. if (mid < min) { tmp = mid; mid = min; min = mid; }
  425. return max + (11.0f / 32.0f)*mid + (1.0f / 4.0f)*min;
  426. }
  427. /***********************************************************************************************
  428. * Swap -- swap two Vector3's *
  429. * *
  430. * INPUT: *
  431. * *
  432. * OUTPUT: *
  433. * *
  434. * WARNINGS: *
  435. * *
  436. * HISTORY: *
  437. * 08/11/1997 GH : Created. *
  438. *=============================================================================================*/
  439. WWINLINE void Swap(Vector3 & a,Vector3 & b)
  440. {
  441. Vector3 tmp(a);
  442. a = b;
  443. b = tmp;
  444. }
  445. /***********************************************************************************************
  446. * Lerp -- linearly interpolate two Vector3's by an interpolation factor. *
  447. * *
  448. * INPUT: *
  449. * *
  450. * OUTPUT: *
  451. * *
  452. * WARNINGS: No checking is done to ensure that alpha is between 0 and 1. *
  453. * *
  454. * HISTORY: *
  455. * 08/11/1997 GH : Created. *
  456. *=============================================================================================*/
  457. WWINLINE Vector3 Lerp(const Vector3 & a, const Vector3 & b, float alpha)
  458. {
  459. return Vector3(
  460. (a.X + (b.X - a.X)*alpha),
  461. (a.Y + (b.Y - a.Y)*alpha),
  462. (a.Z + (b.Z - a.Z)*alpha)
  463. );
  464. }
  465. /***********************************************************************************************
  466. * Lerp -- linearly interpolate two Vector3's without return-by-value *
  467. * *
  468. * INPUT: *
  469. * *
  470. * OUTPUT: *
  471. * *
  472. * WARNINGS: *
  473. * *
  474. * HISTORY: *
  475. * 10/18/99 gth : Created. *
  476. *=============================================================================================*/
  477. WWINLINE void Lerp(const Vector3 & a, const Vector3 & b, float alpha,Vector3 * set_result)
  478. {
  479. assert(set_result != NULL);
  480. set_result->X = (a.X + (b.X - a.X)*alpha);
  481. set_result->Y = (a.Y + (b.Y - a.Y)*alpha);
  482. set_result->Z = (a.Z + (b.Z - a.Z)*alpha);
  483. }
  484. WWINLINE void Vector3::Lerp(const Vector3 & a, const Vector3 & b, float alpha,Vector3 * set_result)
  485. {
  486. assert(set_result != NULL);
  487. set_result->X = (a.X + (b.X - a.X)*alpha);
  488. set_result->Y = (a.Y + (b.Y - a.Y)*alpha);
  489. set_result->Z = (a.Z + (b.Z - a.Z)*alpha);
  490. }
  491. /***********************************************************************************************
  492. * Vector3::Add -- Add two vector3's without return-by-value *
  493. * *
  494. * INPUT: *
  495. * *
  496. * OUTPUT: *
  497. * *
  498. * WARNINGS: *
  499. * *
  500. * HISTORY: *
  501. * 10/18/99 gth : Created. *
  502. *=============================================================================================*/
  503. WWINLINE void Vector3::Add(const Vector3 &a,const Vector3 &b,Vector3 * set_result)
  504. {
  505. assert(set_result != NULL);
  506. set_result->X = a.X + b.X;
  507. set_result->Y = a.Y + b.Y;
  508. set_result->Z = a.Z + b.Z;
  509. }
  510. /***********************************************************************************************
  511. * Vector3::Subtract -- Subtract two vector3's without return-by-value *
  512. * *
  513. * INPUT: *
  514. * *
  515. * OUTPUT: *
  516. * *
  517. * WARNINGS: *
  518. * *
  519. * HISTORY: *
  520. * 10/18/99 gth : Created. *
  521. *=============================================================================================*/
  522. WWINLINE void Vector3::Subtract(const Vector3 &a,const Vector3 &b,Vector3 * set_result)
  523. {
  524. assert(set_result != NULL);
  525. set_result->X = a.X - b.X;
  526. set_result->Y = a.Y - b.Y;
  527. set_result->Z = a.Z - b.Z;
  528. }
  529. /***********************************************************************************************
  530. * Vector3::Update_Min -- sets each component of the vector to the min of this and a *
  531. * *
  532. * INPUT: *
  533. * *
  534. * OUTPUT: *
  535. * *
  536. * WARNINGS: *
  537. * *
  538. * HISTORY: *
  539. * 10/18/99 gth : Created. *
  540. *=============================================================================================*/
  541. WWINLINE void Vector3::Update_Min(const Vector3 & a)
  542. {
  543. if (a.X < X) X = a.X;
  544. if (a.Y < Y) Y = a.Y;
  545. if (a.Z < Z) Z = a.Z;
  546. }
  547. /***********************************************************************************************
  548. * Vector3::Update_Max -- Sets each component of the vector to the max of this and a *
  549. * *
  550. * INPUT: *
  551. * *
  552. * OUTPUT: *
  553. * *
  554. * WARNINGS: *
  555. * *
  556. * HISTORY: *
  557. * 10/18/99 gth : Created. *
  558. *=============================================================================================*/
  559. WWINLINE void Vector3::Update_Max(const Vector3 & a)
  560. {
  561. if (a.X > X) X = a.X;
  562. if (a.Y > Y) Y = a.Y;
  563. if (a.Z > Z) Z = a.Z;
  564. }
  565. /***********************************************************************************************
  566. * Vector3::Cap_To_Absolute_Of -- Sets each component of the vector to no larger than the -ve or +ve of*
  567. * *
  568. * INPUT: *
  569. * *
  570. * OUTPUT: *
  571. * *
  572. * WARNINGS: *
  573. * *
  574. * HISTORY: *
  575. * 11/29/99 wst : Created. *
  576. *=============================================================================================*/
  577. WWINLINE void Vector3::Cap_Absolute_To(const Vector3 & a)
  578. {
  579. if (X > 0)
  580. {
  581. if (a.X < X) X = a.X;
  582. }
  583. else
  584. {
  585. if (-a.X > X) X = -a.X;
  586. }
  587. if (Y > 0)
  588. {
  589. if (a.Y < Y) Y = a.Y;
  590. }
  591. else
  592. {
  593. if (-a.Y > Y) Y = -a.Y;
  594. }
  595. if (Z > 0)
  596. {
  597. if (a.Z < Z) Z = a.Z;
  598. }
  599. else
  600. {
  601. if (-a.Z > Z) Z = -a.Z;
  602. }
  603. }
  604. /***********************************************************************************************
  605. * Vector3::Scale -- scale this vector by 3 independent scale factors *
  606. * *
  607. * INPUT: *
  608. * *
  609. * OUTPUT: *
  610. * *
  611. * WARNINGS: *
  612. * *
  613. * HISTORY: *
  614. * 10/18/99 gth : Created. *
  615. *=============================================================================================*/
  616. WWINLINE void Vector3::Scale(const Vector3 & scale)
  617. {
  618. X *= scale.X;
  619. Y *= scale.Y;
  620. Z *= scale.Z;
  621. }
  622. /***********************************************************************************************
  623. * Vector3::Rotate_X -- rotates this vector around the X axis *
  624. * *
  625. * INPUT: *
  626. * *
  627. * OUTPUT: *
  628. * *
  629. * WARNINGS: *
  630. * *
  631. * HISTORY: *
  632. * 10/18/99 gth : Created. *
  633. *=============================================================================================*/
  634. WWINLINE void Vector3::Rotate_X(float angle)
  635. {
  636. Rotate_X(sinf(angle),cosf(angle));
  637. }
  638. /***********************************************************************************************
  639. * Vector3::Rotate_X -- Rotates this vector around the x axis *
  640. * *
  641. * INPUT: *
  642. * *
  643. * OUTPUT: *
  644. * *
  645. * WARNINGS: *
  646. * *
  647. * HISTORY: *
  648. * 10/18/99 gth : Created. *
  649. *=============================================================================================*/
  650. WWINLINE void Vector3::Rotate_X(float s_angle,float c_angle)
  651. {
  652. float tmp_y = Y;
  653. float tmp_z = Z;
  654. Y = c_angle * tmp_y - s_angle * tmp_z;
  655. Z = s_angle * tmp_y + c_angle * tmp_z;
  656. }
  657. /***********************************************************************************************
  658. * Vector3::Rotate_Y -- Rotates this vector around the y axis *
  659. * *
  660. * INPUT: *
  661. * *
  662. * OUTPUT: *
  663. * *
  664. * WARNINGS: *
  665. * *
  666. * HISTORY: *
  667. * 10/18/99 gth : Created. *
  668. *=============================================================================================*/
  669. WWINLINE void Vector3::Rotate_Y(float angle)
  670. {
  671. Rotate_Y(sinf(angle),cosf(angle));
  672. }
  673. /***********************************************************************************************
  674. * Vector3::Rotate_Y -- Rotates this vector around the Y axis *
  675. * *
  676. * INPUT: *
  677. * *
  678. * OUTPUT: *
  679. * *
  680. * WARNINGS: *
  681. * *
  682. * HISTORY: *
  683. * 10/18/99 gth : Created. *
  684. *=============================================================================================*/
  685. WWINLINE void Vector3::Rotate_Y(float s_angle,float c_angle)
  686. {
  687. float tmp_x = X;
  688. float tmp_z = Z;
  689. X = c_angle * tmp_x + s_angle * tmp_z;
  690. Z = -s_angle * tmp_x + c_angle * tmp_z;
  691. }
  692. /***********************************************************************************************
  693. * Vector3::Rotate_Z -- Rotates this vector around the Z axis *
  694. * *
  695. * INPUT: *
  696. * *
  697. * OUTPUT: *
  698. * *
  699. * WARNINGS: *
  700. * *
  701. * HISTORY: *
  702. * 10/18/99 gth : Created. *
  703. *=============================================================================================*/
  704. WWINLINE void Vector3::Rotate_Z(float angle)
  705. {
  706. Rotate_Z(sinf(angle),cosf(angle));
  707. }
  708. /***********************************************************************************************
  709. * Vector3::Rotate_Z -- Rotates this vector around the Z axis *
  710. * *
  711. * INPUT: *
  712. * *
  713. * OUTPUT: *
  714. * *
  715. * WARNINGS: *
  716. * *
  717. * HISTORY: *
  718. * 10/18/99 gth : Created. *
  719. *=============================================================================================*/
  720. WWINLINE void Vector3::Rotate_Z(float s_angle,float c_angle)
  721. {
  722. float tmp_x = X;
  723. float tmp_y = Y;
  724. X = c_angle * tmp_x - s_angle * tmp_y;
  725. Y = s_angle * tmp_x + c_angle * tmp_y;
  726. }
  727. /***********************************************************************************************
  728. * Vector3::Is_Valid -- Verifies that each component of this vector is a valid float *
  729. * *
  730. * INPUT: *
  731. * *
  732. * OUTPUT: *
  733. * *
  734. * WARNINGS: *
  735. * *
  736. * HISTORY: *
  737. * 10/18/99 gth : Created. *
  738. *=============================================================================================*/
  739. WWINLINE bool Vector3::Is_Valid(void) const
  740. {
  741. return (WWMath::Is_Valid_Float(X) && WWMath::Is_Valid_Float(Y) && WWMath::Is_Valid_Float(Z));
  742. }
  743. WWINLINE float Vector3::Find_X_At_Y(float y, const Vector3 &p1, const Vector3 &p2)
  744. {
  745. return(p1.X + ((y - p1.Y) * ((p2.X - p1.X) / (p2.Y - p1.Y))));
  746. }
  747. WWINLINE float Vector3::Find_X_At_Z(float z, const Vector3 &p1, const Vector3 &p2)
  748. {
  749. return(p1.X + ((z - p1.Z) * ((p2.X - p1.X) / (p2.Z - p1.Z))));
  750. }
  751. WWINLINE float Vector3::Find_Y_At_X(float x, const Vector3 &p1, const Vector3 &p2)
  752. {
  753. return(p1.Y + ((x - p1.X) * ((p2.Y - p1.Y) / (p2.X - p1.X))));
  754. }
  755. WWINLINE float Vector3::Find_Y_At_Z(float z, const Vector3 &p1, const Vector3 &p2)
  756. {
  757. return(p1.Y + ((z - p1.Z) * ((p2.Y - p1.Y) / (p2.Z - p1.Z))));
  758. }
  759. WWINLINE float Vector3::Find_Z_At_X(float x, const Vector3 &p1, const Vector3 &p2)
  760. {
  761. return(p1.Z + ((x - p1.X) * ((p2.Z - p1.Z) / (p2.X - p1.X))));
  762. }
  763. WWINLINE float Vector3::Find_Z_At_Y(float y, const Vector3 &p1, const Vector3 &p2)
  764. {
  765. return(p1.Z + ((y - p1.Y) * ((p2.Z - p1.Z) / (p2.Y - p1.Y))));
  766. }
  767. /***********************************************************************************************
  768. * Vector3::Distance -- Accurate distance calculation. *
  769. * *
  770. * *
  771. * *
  772. * *
  773. * HISTORY: *
  774. * 11/29/1999MLL: Created. *
  775. *=============================================================================================*/
  776. WWINLINE float Vector3::Distance(const Vector3 &p1, const Vector3 &p2)
  777. {
  778. Vector3 temp;
  779. temp = p1 - p2;
  780. return (temp.Length());
  781. }
  782. /***********************************************************************************************
  783. * Vector3::Quick_Distance -- Fast but inaccurate distance calculation. *
  784. * *
  785. * *
  786. * *
  787. * *
  788. * HISTORY: *
  789. * 11/29/1999MLL: Created. *
  790. *=============================================================================================*/
  791. WWINLINE float Vector3::Quick_Distance(const Vector3 &p1, const Vector3 &p2)
  792. {
  793. Vector3 temp;
  794. temp = p1 - p2;
  795. return (temp.Quick_Length());
  796. }
  797. /***********************************************************************************************
  798. * Vector3::Convert_To_ABGR -- Converts to SR packed color . *
  799. * *
  800. * *
  801. * *
  802. * *
  803. * HISTORY: *
  804. * 11/29/1999MLL: Created. *
  805. *=============================================================================================*/
  806. WWINLINE unsigned long Vector3::Convert_To_ABGR( void ) const
  807. {
  808. return (unsigned(255)<<24) |
  809. (unsigned(Z*255.0f)<<16) |
  810. (unsigned(Y*255.0f)<<8) |
  811. (unsigned(X*255.0f));
  812. }
  813. /***********************************************************************************************
  814. * Vector3::Convert_To_ARGB -- Converts to packed color . *
  815. * *
  816. * *
  817. * *
  818. * *
  819. * HISTORY: *
  820. * 11/29/1999MLL: Created. *
  821. *=============================================================================================*/
  822. WWINLINE unsigned long Vector3::Convert_To_ARGB( void ) const
  823. {
  824. return (unsigned(255)<<24) |
  825. (unsigned(X*255.0f)<<16) |
  826. (unsigned(Y*255.0f)<<8) |
  827. (unsigned(Z*255.0f));
  828. }
  829. #endif /* Vector3_H */