mPoint3.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MPOINT3_H_
  23. #define _MPOINT3_H_
  24. #ifndef _MMATHFN_H_
  25. #include "math/mMathFn.h"
  26. #endif
  27. #ifndef _MPOINT2_H_
  28. #include "math/mPoint2.h"
  29. #endif
  30. //------------------------------------------------------------------------------
  31. /// 3D integer point
  32. ///
  33. /// Uses S32 internally.
  34. class Point3I
  35. {
  36. //-------------------------------------- Public data
  37. public:
  38. S32 x; ///< X co-ordinate
  39. S32 y; ///< Y co-ordinate
  40. S32 z; ///< Z co-ordinate
  41. //-------------------------------------- Public interface
  42. public:
  43. Point3I(); ///< Create an uninitialized point.
  44. Point3I(const Point3I&); ///< Copy constructor.
  45. explicit Point3I(S32 xyz); ///< Initializes all elements to the same value.
  46. Point3I(S32 in_x, S32 in_y, S32 in_z); ///< Create a point from co-ordinates.
  47. //-------------------------------------- Non-math mutators and misc functions
  48. void set(S32 xyz); ///< Initializes all elements to the same value.
  49. void set(S32 in_x, S32 in_y, S32 in_z); ///< Set co-ordinates.
  50. void setMin(const Point3I&); ///< Store lesser co-ordinates in this point.
  51. void setMax(const Point3I&); ///< Store greater co-ordinates in this point.
  52. void zero(); ///< Zero all values
  53. //-------------------------------------- Math mutators
  54. void neg(); ///< Invert co-ordinate's signs.
  55. void convolve(const Point3I&); ///< Convolve by parameter.
  56. //-------------------------------------- Queries
  57. bool isZero() const; ///< Check for point at origin. (No epsilon.)
  58. F32 len() const; ///< Get length.
  59. //-------------------------------------- Overloaded operators
  60. public:
  61. operator S32*() { return &x; }
  62. operator const S32*() const { return &x; }
  63. // Comparison operators
  64. bool operator==(const Point3I&) const;
  65. bool operator!=(const Point3I&) const;
  66. // Arithmetic w/ other points
  67. Point3I operator+(const Point3I&) const;
  68. Point3I operator-(const Point3I&) const;
  69. Point3I& operator+=(const Point3I&);
  70. Point3I& operator-=(const Point3I&);
  71. // Arithmetic w/ scalars
  72. Point3I operator*(S32) const;
  73. Point3I& operator*=(S32);
  74. Point3I operator/(S32) const;
  75. Point3I& operator/=(S32);
  76. // Unary operators
  77. Point3I operator-() const;
  78. //-------------------------------------- Public static constants
  79. public:
  80. const static Point3I One;
  81. const static Point3I Zero;
  82. };
  83. class Point3D;
  84. //------------------------------------------------------------------------------
  85. class Point3F
  86. {
  87. //-------------------------------------- Public data
  88. public:
  89. F32 x;
  90. F32 y;
  91. F32 z;
  92. public:
  93. Point3F();
  94. Point3F(const Point3F&);
  95. Point3F(F32 _x, F32 _y, F32 _z);
  96. explicit Point3F(F32 xyz);
  97. //-------------------------------------- Non-math mutators and misc functions
  98. public:
  99. void set(F32 xyz);
  100. void set(F32 _x, F32 _y, F32 _z);
  101. void set(const Point3F&);
  102. void setMin(const Point3F&);
  103. void setMax(const Point3F&);
  104. void interpolate(const Point3F&, const Point3F&, F32);
  105. void zero();
  106. /// Returns the smallest absolute value.
  107. F32 least() const;
  108. /// Returns the greatest absolute value.
  109. F32 most() const;
  110. operator F32*() { return &x; }
  111. operator const F32*() const { return &x; }
  112. /// Returns the x and y coords as a Point2F.
  113. Point2F asPoint2F() const { return Point2F( x, y ); }
  114. //-------------------------------------- Queries
  115. public:
  116. bool isZero() const;
  117. bool isUnitLength() const;
  118. F32 len() const;
  119. F32 lenSquared() const;
  120. F32 magnitudeSafe() const;
  121. bool equal( const Point3F &compare, F32 epsilon = POINT_EPSILON ) const;
  122. U32 getLeastComponentIndex() const;
  123. U32 getGreatestComponentIndex() const;
  124. //-------------------------------------- Mathematical mutators
  125. public:
  126. void neg();
  127. void normalize();
  128. void normalizeSafe();
  129. void normalize(F32 val);
  130. void convolve(const Point3F&);
  131. void convolveInverse(const Point3F&);
  132. //-------------------------------------- Overloaded operators
  133. public:
  134. // Comparison operators
  135. bool operator==(const Point3F&) const;
  136. bool operator!=(const Point3F&) const;
  137. // Arithmetic w/ other points
  138. Point3F operator+(const Point3F&) const;
  139. Point3F operator-(const Point3F&) const;
  140. Point3F& operator+=(const Point3F&);
  141. Point3F& operator-=(const Point3F&);
  142. // Arithmetic w/ scalars
  143. Point3F operator*(F32) const;
  144. Point3F operator/(F32) const;
  145. Point3F& operator*=(F32);
  146. Point3F& operator/=(F32);
  147. Point3F operator*(const Point3F&) const;
  148. Point3F& operator*=(const Point3F&);
  149. Point3F operator/(const Point3F&) const;
  150. Point3F& operator/=(const Point3F&);
  151. // Unary operators
  152. Point3F operator-() const;
  153. Point3F& operator=(const Point3D&);
  154. //-------------------------------------- Public static constants
  155. public:
  156. const static Point3F One;
  157. const static Point3F Zero;
  158. const static Point3F Max;
  159. const static Point3F Min;
  160. const static Point3F UnitX;
  161. const static Point3F UnitY;
  162. const static Point3F UnitZ;
  163. };
  164. typedef Point3F VectorF;
  165. typedef Point3F EulerF;
  166. //------------------------------------------------------------------------------
  167. class Point3D
  168. {
  169. //-------------------------------------- Public data
  170. public:
  171. F64 x;
  172. F64 y;
  173. F64 z;
  174. public:
  175. Point3D();
  176. Point3D(const Point3D&);
  177. Point3D(const Point3F&);
  178. explicit Point3D(F64 xyz);
  179. Point3D(F64 _x, F64 _y, F64 _z);
  180. //-------------------------------------- Non-math mutators and misc functions
  181. public:
  182. void set(F64 xyz);
  183. void set(F64 _x, F64 _y, F64 _z);
  184. void setMin(const Point3D&);
  185. void setMax(const Point3D&);
  186. void interpolate(const Point3D&, const Point3D&, F64);
  187. void zero();
  188. operator F64*() { return (&x); }
  189. operator const F64*() const { return &x; }
  190. //-------------------------------------- Queries
  191. public:
  192. bool isZero() const;
  193. F64 len() const;
  194. F64 lenSquared() const;
  195. //-------------------------------------- Mathematical mutators
  196. public:
  197. void neg();
  198. void normalize();
  199. void normalize(F64 val);
  200. void convolve(const Point3D&);
  201. void convolveInverse(const Point3D&);
  202. //-------------------------------------- Overloaded operators
  203. public:
  204. Point3F toPoint3F() const;
  205. // Comparison operators
  206. bool operator==(const Point3D&) const;
  207. bool operator!=(const Point3D&) const;
  208. // Arithmetic w/ other points
  209. Point3D operator+(const Point3D&) const;
  210. Point3D operator-(const Point3D&) const;
  211. Point3D& operator+=(const Point3D&);
  212. Point3D& operator-=(const Point3D&);
  213. // Arithmetic w/ scalars
  214. Point3D operator*(F64) const;
  215. Point3D operator/(F64) const;
  216. Point3D& operator*=(F64);
  217. Point3D& operator/=(F64);
  218. // Unary operators
  219. Point3D operator-() const;
  220. //-------------------------------------- Public static constants
  221. public:
  222. const static Point3D One;
  223. const static Point3D Zero;
  224. };
  225. //------------------------------------------------------------------------------
  226. //-------------------------------------- Point3I
  227. //
  228. inline Point3I::Point3I()
  229. {
  230. //
  231. }
  232. inline Point3I::Point3I(const Point3I& _copy)
  233. : x(_copy.x), y(_copy.y), z(_copy.z)
  234. {
  235. //
  236. }
  237. inline Point3I::Point3I(S32 xyz)
  238. : x(xyz), y(xyz), z(xyz)
  239. {
  240. //
  241. }
  242. inline Point3I::Point3I(S32 _x, S32 _y, S32 _z)
  243. : x(_x), y(_y), z(_z)
  244. {
  245. //
  246. }
  247. inline void Point3I::set(S32 xyz)
  248. {
  249. x = y = z = xyz;
  250. }
  251. inline void Point3I::set(S32 _x, S32 _y, S32 _z)
  252. {
  253. x = _x;
  254. y = _y;
  255. z = _z;
  256. }
  257. inline void Point3I::setMin(const Point3I& _test)
  258. {
  259. x = (_test.x < x) ? _test.x : x;
  260. y = (_test.y < y) ? _test.y : y;
  261. z = (_test.z < z) ? _test.z : z;
  262. }
  263. inline void Point3I::setMax(const Point3I& _test)
  264. {
  265. x = (_test.x > x) ? _test.x : x;
  266. y = (_test.y > y) ? _test.y : y;
  267. z = (_test.z > z) ? _test.z : z;
  268. }
  269. inline void Point3I::zero()
  270. {
  271. x = y = z = 0;
  272. }
  273. inline void Point3I::neg()
  274. {
  275. x = -x;
  276. y = -y;
  277. z = -z;
  278. }
  279. inline F32 Point3I::len() const
  280. {
  281. return mSqrt(F32(x*x + y*y + z*z));
  282. }
  283. inline void Point3I::convolve(const Point3I& c)
  284. {
  285. x *= c.x;
  286. y *= c.y;
  287. z *= c.z;
  288. }
  289. inline bool Point3I::isZero() const
  290. {
  291. return ((x == 0) && (y == 0) && (z == 0));
  292. }
  293. inline bool Point3I::operator==(const Point3I& _test) const
  294. {
  295. return ((x == _test.x) && (y == _test.y) && (z == _test.z));
  296. }
  297. inline bool Point3I::operator!=(const Point3I& _test) const
  298. {
  299. return (operator==(_test) == false);
  300. }
  301. inline Point3I Point3I::operator+(const Point3I& _add) const
  302. {
  303. return Point3I(x + _add.x, y + _add.y, z + _add.z);
  304. }
  305. inline Point3I Point3I::operator-(const Point3I& _rSub) const
  306. {
  307. return Point3I(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  308. }
  309. inline Point3I& Point3I::operator+=(const Point3I& _add)
  310. {
  311. x += _add.x;
  312. y += _add.y;
  313. z += _add.z;
  314. return *this;
  315. }
  316. inline Point3I& Point3I::operator-=(const Point3I& _rSub)
  317. {
  318. x -= _rSub.x;
  319. y -= _rSub.y;
  320. z -= _rSub.z;
  321. return *this;
  322. }
  323. inline Point3I Point3I::operator-() const
  324. {
  325. return Point3I(-x, -y, -z);
  326. }
  327. inline Point3I Point3I::operator*(S32 mul) const
  328. {
  329. return Point3I(x * mul, y * mul, z * mul);
  330. }
  331. inline Point3I Point3I::operator/(S32 div) const
  332. {
  333. AssertFatal(div != 0, "Error, div by zero attempted");
  334. return Point3I(x/div, y/div, z/div);
  335. }
  336. inline Point3I& Point3I::operator*=(S32 mul)
  337. {
  338. x *= mul;
  339. y *= mul;
  340. z *= mul;
  341. return *this;
  342. }
  343. inline Point3I& Point3I::operator/=(S32 div)
  344. {
  345. AssertFatal(div != 0, "Error, div by zero attempted");
  346. x /= div;
  347. y /= div;
  348. z /= div;
  349. return *this;
  350. }
  351. //------------------------------------------------------------------------------
  352. //-------------------------------------- Point3F
  353. //
  354. inline Point3F::Point3F()
  355. #if defined(TORQUE_OS_LINUX)
  356. : x(0.f), y(0.f), z(0.f)
  357. #endif
  358. {
  359. // Uninitialized points are definitely a problem.
  360. // Enable the following code to see how often they crop up.
  361. #ifdef DEBUG_MATH
  362. *(U32 *)&x = 0x7FFFFFFA;
  363. *(U32 *)&y = 0x7FFFFFFB;
  364. *(U32 *)&z = 0x7FFFFFFC;
  365. #endif
  366. }
  367. inline Point3F::Point3F(const Point3F& _copy)
  368. : x(_copy.x), y(_copy.y), z(_copy.z)
  369. {
  370. //
  371. }
  372. inline Point3F::Point3F(F32 _x, F32 _y, F32 _z)
  373. : x(_x), y(_y), z(_z)
  374. {
  375. //
  376. }
  377. inline Point3F::Point3F(F32 xyz)
  378. : x(xyz), y(xyz), z(xyz)
  379. {
  380. //
  381. }
  382. inline void Point3F::set(F32 xyz)
  383. {
  384. x = y = z = xyz;
  385. }
  386. inline void Point3F::set(F32 _x, F32 _y, F32 _z)
  387. {
  388. x = _x;
  389. y = _y;
  390. z = _z;
  391. }
  392. inline void Point3F::set(const Point3F& copy)
  393. {
  394. x = copy.x;
  395. y = copy.y;
  396. z = copy.z;
  397. }
  398. inline void Point3F::setMin(const Point3F& _test)
  399. {
  400. x = (_test.x < x) ? _test.x : x;
  401. y = (_test.y < y) ? _test.y : y;
  402. z = (_test.z < z) ? _test.z : z;
  403. }
  404. inline void Point3F::setMax(const Point3F& _test)
  405. {
  406. x = (_test.x > x) ? _test.x : x;
  407. y = (_test.y > y) ? _test.y : y;
  408. z = (_test.z > z) ? _test.z : z;
  409. }
  410. inline void Point3F::interpolate(const Point3F& _from, const Point3F& _to, F32 _factor)
  411. {
  412. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  413. m_point3F_interpolate( _from, _to, _factor, *this);
  414. }
  415. inline void Point3F::zero()
  416. {
  417. x = y = z = 0.0f;
  418. }
  419. inline bool Point3F::isZero() const
  420. {
  421. return ((x*x) <= POINT_EPSILON) && ((y*y) <= POINT_EPSILON) && ((z*z) <= POINT_EPSILON );
  422. }
  423. inline bool Point3F::isUnitLength() const
  424. {
  425. return ( mFabs( 1.0f - ( x*x + y*y + z*z ) ) < POINT_EPSILON );
  426. }
  427. inline bool Point3F::equal( const Point3F &compare, F32 epsilon ) const
  428. {
  429. return( ( mFabs( x - compare.x ) < epsilon ) &&
  430. ( mFabs( y - compare.y ) < epsilon ) &&
  431. ( mFabs( z - compare.z ) < epsilon ) );
  432. }
  433. inline U32 Point3F::getLeastComponentIndex() const
  434. {
  435. U32 idx;
  436. if ( mFabs( x ) < mFabs( y ) )
  437. {
  438. if ( mFabs( x ) < mFabs( z ) )
  439. idx = 0;
  440. else
  441. idx = 2;
  442. }
  443. else
  444. {
  445. if ( mFabs( y ) < mFabs( z ) )
  446. idx = 1;
  447. else
  448. idx = 2;
  449. }
  450. return idx;
  451. }
  452. inline U32 Point3F::getGreatestComponentIndex() const
  453. {
  454. U32 idx;
  455. if ( mFabs( x ) > mFabs( y ) )
  456. {
  457. if ( mFabs( x ) > mFabs( z ) )
  458. idx = 0;
  459. else
  460. idx = 2;
  461. }
  462. else
  463. {
  464. if ( mFabs( y ) > mFabs( z ) )
  465. idx = 1;
  466. else
  467. idx = 2;
  468. }
  469. return idx;
  470. }
  471. inline F32 Point3F::least() const
  472. {
  473. return getMin( mFabs( x ), getMin( mFabs( y ), mFabs( z ) ) );
  474. }
  475. inline F32 Point3F::most() const
  476. {
  477. return getMax( mFabs( x ), getMax( mFabs( y ), mFabs( z ) ) );
  478. }
  479. inline void Point3F::neg()
  480. {
  481. x = -x;
  482. y = -y;
  483. z = -z;
  484. }
  485. inline void Point3F::convolve(const Point3F& c)
  486. {
  487. x *= c.x;
  488. y *= c.y;
  489. z *= c.z;
  490. }
  491. inline void Point3F::convolveInverse(const Point3F& c)
  492. {
  493. x /= c.x;
  494. y /= c.y;
  495. z /= c.z;
  496. }
  497. inline F32 Point3F::lenSquared() const
  498. {
  499. return (x * x) + (y * y) + (z * z);
  500. }
  501. inline F32 Point3F::len() const
  502. {
  503. return mSqrt(x*x + y*y + z*z);
  504. }
  505. inline void Point3F::normalize()
  506. {
  507. m_point3F_normalize(*this);
  508. }
  509. inline F32 Point3F::magnitudeSafe() const
  510. {
  511. if( isZero() )
  512. {
  513. return 0.0f;
  514. }
  515. else
  516. {
  517. return len();
  518. }
  519. }
  520. inline void Point3F::normalizeSafe()
  521. {
  522. F32 vmag = magnitudeSafe();
  523. if( vmag > POINT_EPSILON )
  524. {
  525. *this *= F32(1.0 / vmag);
  526. }
  527. }
  528. inline void Point3F::normalize(F32 val)
  529. {
  530. m_point3F_normalize_f(*this, val);
  531. }
  532. inline bool Point3F::operator==(const Point3F& _test) const
  533. {
  534. return (x == _test.x) && (y == _test.y) && (z == _test.z);
  535. }
  536. inline bool Point3F::operator!=(const Point3F& _test) const
  537. {
  538. return operator==(_test) == false;
  539. }
  540. inline Point3F Point3F::operator+(const Point3F& _add) const
  541. {
  542. return Point3F(x + _add.x, y + _add.y, z + _add.z);
  543. }
  544. inline Point3F Point3F::operator-(const Point3F& _rSub) const
  545. {
  546. return Point3F(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  547. }
  548. inline Point3F& Point3F::operator+=(const Point3F& _add)
  549. {
  550. x += _add.x;
  551. y += _add.y;
  552. z += _add.z;
  553. return *this;
  554. }
  555. inline Point3F& Point3F::operator-=(const Point3F& _rSub)
  556. {
  557. x -= _rSub.x;
  558. y -= _rSub.y;
  559. z -= _rSub.z;
  560. return *this;
  561. }
  562. inline Point3F Point3F::operator*(F32 _mul) const
  563. {
  564. return Point3F(x * _mul, y * _mul, z * _mul);
  565. }
  566. inline Point3F Point3F::operator/(F32 _div) const
  567. {
  568. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  569. F32 inv = 1.0f / _div;
  570. return Point3F(x * inv, y * inv, z * inv);
  571. }
  572. inline Point3F& Point3F::operator*=(F32 _mul)
  573. {
  574. x *= _mul;
  575. y *= _mul;
  576. z *= _mul;
  577. return *this;
  578. }
  579. inline Point3F& Point3F::operator/=(F32 _div)
  580. {
  581. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  582. F32 inv = 1.0f / _div;
  583. x *= inv;
  584. y *= inv;
  585. z *= inv;
  586. return *this;
  587. }
  588. inline Point3F Point3F::operator*(const Point3F &_vec) const
  589. {
  590. return Point3F(x * _vec.x, y * _vec.y, z * _vec.z);
  591. }
  592. inline Point3F& Point3F::operator*=(const Point3F &_vec)
  593. {
  594. x *= _vec.x;
  595. y *= _vec.y;
  596. z *= _vec.z;
  597. return *this;
  598. }
  599. inline Point3F Point3F::operator/(const Point3F &_vec) const
  600. {
  601. return Point3F(x / _vec.x, y / _vec.y, z / _vec.z);
  602. }
  603. inline Point3F& Point3F::operator/=(const Point3F &_vec)
  604. {
  605. x /= _vec.x;
  606. y /= _vec.y;
  607. z /= _vec.z;
  608. return *this;
  609. }
  610. inline Point3F Point3F::operator-() const
  611. {
  612. return Point3F(-x, -y, -z);
  613. }
  614. inline Point3F& Point3F::operator=(const Point3D &_vec)
  615. {
  616. x = (F32)_vec.x;
  617. y = (F32)_vec.y;
  618. z = (F32)_vec.z;
  619. return *this;
  620. }
  621. //------------------------------------------------------------------------------
  622. //-------------------------------------- Point3D
  623. //
  624. inline Point3D::Point3D()
  625. {
  626. //
  627. }
  628. inline Point3D::Point3D(const Point3D& _copy)
  629. : x(_copy.x), y(_copy.y), z(_copy.z)
  630. {
  631. //
  632. }
  633. inline Point3D::Point3D(const Point3F& _copy)
  634. : x(_copy.x), y(_copy.y), z(_copy.z)
  635. {
  636. //
  637. }
  638. inline Point3D::Point3D(F64 xyz)
  639. : x(xyz), y(xyz), z(xyz)
  640. {
  641. //
  642. }
  643. inline Point3D::Point3D(F64 _x, F64 _y, F64 _z)
  644. : x(_x), y(_y), z(_z)
  645. {
  646. //
  647. }
  648. inline void Point3D::set( F64 xyz )
  649. {
  650. x = y = z = xyz;
  651. }
  652. inline void Point3D::set(F64 _x, F64 _y, F64 _z)
  653. {
  654. x = _x;
  655. y = _y;
  656. z = _z;
  657. }
  658. inline void Point3D::setMin(const Point3D& _test)
  659. {
  660. x = (_test.x < x) ? _test.x : x;
  661. y = (_test.y < y) ? _test.y : y;
  662. z = (_test.z < z) ? _test.z : z;
  663. }
  664. inline void Point3D::setMax(const Point3D& _test)
  665. {
  666. x = (_test.x > x) ? _test.x : x;
  667. y = (_test.y > y) ? _test.y : y;
  668. z = (_test.z > z) ? _test.z : z;
  669. }
  670. inline void Point3D::interpolate(const Point3D& _from, const Point3D& _to, F64 _factor)
  671. {
  672. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  673. m_point3D_interpolate( _from, _to, _factor, *this);
  674. }
  675. inline void Point3D::zero()
  676. {
  677. x = y = z = 0.0;
  678. }
  679. inline bool Point3D::isZero() const
  680. {
  681. return (x == 0.0f) && (y == 0.0f) && (z == 0.0f);
  682. }
  683. inline void Point3D::neg()
  684. {
  685. x = -x;
  686. y = -y;
  687. z = -z;
  688. }
  689. inline void Point3D::convolve(const Point3D& c)
  690. {
  691. x *= c.x;
  692. y *= c.y;
  693. z *= c.z;
  694. }
  695. inline void Point3D::convolveInverse(const Point3D& c)
  696. {
  697. x /= c.x;
  698. y /= c.y;
  699. z /= c.z;
  700. }
  701. inline F64 Point3D::lenSquared() const
  702. {
  703. return (x * x) + (y * y) + (z * z);
  704. }
  705. inline F64 Point3D::len() const
  706. {
  707. return mSqrtD(x*x + y*y + z*z);
  708. }
  709. inline void Point3D::normalize()
  710. {
  711. m_point3D_normalize(*this);
  712. }
  713. inline void Point3D::normalize(F64 val)
  714. {
  715. m_point3D_normalize_f(*this, val);
  716. }
  717. inline bool Point3D::operator==(const Point3D& _test) const
  718. {
  719. return (x == _test.x) && (y == _test.y) && (z == _test.z);
  720. }
  721. inline bool Point3D::operator!=(const Point3D& _test) const
  722. {
  723. return operator==(_test) == false;
  724. }
  725. inline Point3D Point3D::operator+(const Point3D& _add) const
  726. {
  727. return Point3D(x + _add.x, y + _add.y, z + _add.z);
  728. }
  729. inline Point3D Point3D::operator-(const Point3D& _rSub) const
  730. {
  731. return Point3D(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  732. }
  733. inline Point3D& Point3D::operator+=(const Point3D& _add)
  734. {
  735. x += _add.x;
  736. y += _add.y;
  737. z += _add.z;
  738. return *this;
  739. }
  740. inline Point3D& Point3D::operator-=(const Point3D& _rSub)
  741. {
  742. x -= _rSub.x;
  743. y -= _rSub.y;
  744. z -= _rSub.z;
  745. return *this;
  746. }
  747. inline Point3D Point3D::operator*(F64 _mul) const
  748. {
  749. return Point3D(x * _mul, y * _mul, z * _mul);
  750. }
  751. inline Point3D Point3D::operator/(F64 _div) const
  752. {
  753. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  754. F64 inv = 1.0f / _div;
  755. return Point3D(x * inv, y * inv, z * inv);
  756. }
  757. inline Point3D& Point3D::operator*=(F64 _mul)
  758. {
  759. x *= _mul;
  760. y *= _mul;
  761. z *= _mul;
  762. return *this;
  763. }
  764. inline Point3D& Point3D::operator/=(F64 _div)
  765. {
  766. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  767. F64 inv = 1.0f / _div;
  768. x *= inv;
  769. y *= inv;
  770. z *= inv;
  771. return *this;
  772. }
  773. inline Point3D Point3D::operator-() const
  774. {
  775. return Point3D(-x, -y, -z);
  776. }
  777. inline Point3F Point3D::toPoint3F() const
  778. {
  779. return Point3F((F32)x,(F32)y,(F32)z);
  780. }
  781. //-------------------------------------------------------------------
  782. // Non-Member Operators
  783. //-------------------------------------------------------------------
  784. inline Point3I operator*(S32 mul, const Point3I& multiplicand)
  785. {
  786. return multiplicand * mul;
  787. }
  788. inline Point3F operator*(F32 mul, const Point3F& multiplicand)
  789. {
  790. return multiplicand * mul;
  791. }
  792. inline Point3D operator*(F64 mul, const Point3D& multiplicand)
  793. {
  794. return multiplicand * mul;
  795. }
  796. inline F32 mDot(const Point3F &p1, const Point3F &p2)
  797. {
  798. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
  799. }
  800. inline F64 mDot(const Point3D &p1, const Point3D &p2)
  801. {
  802. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
  803. }
  804. inline void mCross(const Point3F &a, const Point3F &b, Point3F *res)
  805. {
  806. res->x = (a.y * b.z) - (a.z * b.y);
  807. res->y = (a.z * b.x) - (a.x * b.z);
  808. res->z = (a.x * b.y) - (a.y * b.x);
  809. }
  810. inline void mCross(const Point3D &a, const Point3D &b, Point3D *res)
  811. {
  812. res->x = (a.y * b.z) - (a.z * b.y);
  813. res->y = (a.z * b.x) - (a.x * b.z);
  814. res->z = (a.x * b.y) - (a.y * b.x);
  815. }
  816. inline Point3F mCross(const Point3F &a, const Point3F &b)
  817. {
  818. Point3F r;
  819. mCross( a, b, &r );
  820. return r;
  821. }
  822. inline Point3D mCross(const Point3D &a, const Point3D &b)
  823. {
  824. Point3D r;
  825. mCross( a, b, &r );
  826. return r;
  827. }
  828. /// Returns the vector normalized.
  829. inline Point3F mNormalize( const Point3F &vec )
  830. {
  831. Point3F out( vec );
  832. out.normalize();
  833. return out;
  834. }
  835. /// Returns true if the point is NaN.
  836. inline bool mIsNaN( const Point3F &p )
  837. {
  838. return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z );
  839. }
  840. /// Returns a copy of the vector reflected by a normal
  841. inline Point3F mReflect( const Point3F &v, const Point3F &n )
  842. {
  843. return v - 2 * n * mDot( v, n );
  844. }
  845. /// Returns a perpendicular vector to the unit length input vector.
  846. extern Point3F mPerp( const Point3F &normal );
  847. #endif // _MPOINT3_H_