mPoint3.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. F64 magnitudeSafe() const;
  196. //-------------------------------------- Mathematical mutators
  197. public:
  198. void neg();
  199. void normalize();
  200. void normalizeSafe();
  201. void normalize(F64 val);
  202. void convolve(const Point3D&);
  203. void convolveInverse(const Point3D&);
  204. //-------------------------------------- Overloaded operators
  205. public:
  206. Point3F toPoint3F() const;
  207. // Comparison operators
  208. bool operator==(const Point3D&) const;
  209. bool operator!=(const Point3D&) const;
  210. // Arithmetic w/ other points
  211. Point3D operator+(const Point3D&) const;
  212. Point3D operator-(const Point3D&) const;
  213. Point3D& operator+=(const Point3D&);
  214. Point3D& operator-=(const Point3D&);
  215. // Arithmetic w/ scalars
  216. Point3D operator*(F64) const;
  217. Point3D operator/(F64) const;
  218. Point3D& operator*=(F64);
  219. Point3D& operator/=(F64);
  220. // Unary operators
  221. Point3D operator-() const;
  222. //-------------------------------------- Public static constants
  223. public:
  224. const static Point3D One;
  225. const static Point3D Zero;
  226. };
  227. //------------------------------------------------------------------------------
  228. //-------------------------------------- Point3I
  229. //
  230. inline Point3I::Point3I()
  231. {
  232. //
  233. }
  234. inline Point3I::Point3I(const Point3I& _copy)
  235. : x(_copy.x), y(_copy.y), z(_copy.z)
  236. {
  237. //
  238. }
  239. inline Point3I::Point3I(S32 xyz)
  240. : x(xyz), y(xyz), z(xyz)
  241. {
  242. //
  243. }
  244. inline Point3I::Point3I(S32 _x, S32 _y, S32 _z)
  245. : x(_x), y(_y), z(_z)
  246. {
  247. //
  248. }
  249. inline void Point3I::set(S32 xyz)
  250. {
  251. x = y = z = xyz;
  252. }
  253. inline void Point3I::set(S32 _x, S32 _y, S32 _z)
  254. {
  255. x = _x;
  256. y = _y;
  257. z = _z;
  258. }
  259. inline void Point3I::setMin(const Point3I& _test)
  260. {
  261. x = (_test.x < x) ? _test.x : x;
  262. y = (_test.y < y) ? _test.y : y;
  263. z = (_test.z < z) ? _test.z : z;
  264. }
  265. inline void Point3I::setMax(const Point3I& _test)
  266. {
  267. x = (_test.x > x) ? _test.x : x;
  268. y = (_test.y > y) ? _test.y : y;
  269. z = (_test.z > z) ? _test.z : z;
  270. }
  271. inline void Point3I::zero()
  272. {
  273. x = y = z = 0;
  274. }
  275. inline void Point3I::neg()
  276. {
  277. x = -x;
  278. y = -y;
  279. z = -z;
  280. }
  281. inline F32 Point3I::len() const
  282. {
  283. return mSqrt(F32(x*x + y*y + z*z));
  284. }
  285. inline void Point3I::convolve(const Point3I& c)
  286. {
  287. x *= c.x;
  288. y *= c.y;
  289. z *= c.z;
  290. }
  291. inline bool Point3I::isZero() const
  292. {
  293. return ((x == 0) && (y == 0) && (z == 0));
  294. }
  295. inline bool Point3I::operator==(const Point3I& _test) const
  296. {
  297. return ((x == _test.x) && (y == _test.y) && (z == _test.z));
  298. }
  299. inline bool Point3I::operator!=(const Point3I& _test) const
  300. {
  301. return (operator==(_test) == false);
  302. }
  303. inline Point3I Point3I::operator+(const Point3I& _add) const
  304. {
  305. return Point3I(x + _add.x, y + _add.y, z + _add.z);
  306. }
  307. inline Point3I Point3I::operator-(const Point3I& _rSub) const
  308. {
  309. return Point3I(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  310. }
  311. inline Point3I& Point3I::operator+=(const Point3I& _add)
  312. {
  313. x += _add.x;
  314. y += _add.y;
  315. z += _add.z;
  316. return *this;
  317. }
  318. inline Point3I& Point3I::operator-=(const Point3I& _rSub)
  319. {
  320. x -= _rSub.x;
  321. y -= _rSub.y;
  322. z -= _rSub.z;
  323. return *this;
  324. }
  325. inline Point3I Point3I::operator-() const
  326. {
  327. return Point3I(-x, -y, -z);
  328. }
  329. inline Point3I Point3I::operator*(S32 mul) const
  330. {
  331. return Point3I(x * mul, y * mul, z * mul);
  332. }
  333. inline Point3I Point3I::operator/(S32 div) const
  334. {
  335. AssertFatal(div != 0, "Error, div by zero attempted");
  336. return Point3I(x/div, y/div, z/div);
  337. }
  338. inline Point3I& Point3I::operator*=(S32 mul)
  339. {
  340. x *= mul;
  341. y *= mul;
  342. z *= mul;
  343. return *this;
  344. }
  345. inline Point3I& Point3I::operator/=(S32 div)
  346. {
  347. AssertFatal(div != 0, "Error, div by zero attempted");
  348. x /= div;
  349. y /= div;
  350. z /= div;
  351. return *this;
  352. }
  353. //------------------------------------------------------------------------------
  354. //-------------------------------------- Point3F
  355. //
  356. inline Point3F::Point3F()
  357. #if defined(TORQUE_OS_LINUX)
  358. : x(0.f), y(0.f), z(0.f)
  359. #endif
  360. {
  361. // Uninitialized points are definitely a problem.
  362. // Enable the following code to see how often they crop up.
  363. #ifdef DEBUG_MATH
  364. *(U32 *)&x = 0x7FFFFFFA;
  365. *(U32 *)&y = 0x7FFFFFFB;
  366. *(U32 *)&z = 0x7FFFFFFC;
  367. #endif
  368. }
  369. inline Point3F::Point3F(const Point3F& _copy)
  370. : x(_copy.x), y(_copy.y), z(_copy.z)
  371. {
  372. //
  373. }
  374. inline Point3F::Point3F(F32 _x, F32 _y, F32 _z)
  375. : x(_x), y(_y), z(_z)
  376. {
  377. //
  378. }
  379. inline Point3F::Point3F(F32 xyz)
  380. : x(xyz), y(xyz), z(xyz)
  381. {
  382. //
  383. }
  384. inline void Point3F::set(F32 xyz)
  385. {
  386. x = y = z = xyz;
  387. }
  388. inline void Point3F::set(F32 _x, F32 _y, F32 _z)
  389. {
  390. x = _x;
  391. y = _y;
  392. z = _z;
  393. }
  394. inline void Point3F::set(const Point3F& copy)
  395. {
  396. x = copy.x;
  397. y = copy.y;
  398. z = copy.z;
  399. }
  400. inline void Point3F::setMin(const Point3F& _test)
  401. {
  402. x = (_test.x < x) ? _test.x : x;
  403. y = (_test.y < y) ? _test.y : y;
  404. z = (_test.z < z) ? _test.z : z;
  405. }
  406. inline void Point3F::setMax(const Point3F& _test)
  407. {
  408. x = (_test.x > x) ? _test.x : x;
  409. y = (_test.y > y) ? _test.y : y;
  410. z = (_test.z > z) ? _test.z : z;
  411. }
  412. inline void Point3F::interpolate(const Point3F& _from, const Point3F& _to, F32 _factor)
  413. {
  414. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  415. m_point3F_interpolate( _from, _to, _factor, *this);
  416. }
  417. inline void Point3F::zero()
  418. {
  419. x = y = z = 0.0f;
  420. }
  421. inline bool Point3F::isZero() const
  422. {
  423. return ((x*x) <= POINT_EPSILON) && ((y*y) <= POINT_EPSILON) && ((z*z) <= POINT_EPSILON );
  424. }
  425. inline bool Point3F::isUnitLength() const
  426. {
  427. return ( mFabs( 1.0f - ( x*x + y*y + z*z ) ) < POINT_EPSILON );
  428. }
  429. inline bool Point3F::equal( const Point3F &compare, F32 epsilon ) const
  430. {
  431. return( ( mFabs( x - compare.x ) < epsilon ) &&
  432. ( mFabs( y - compare.y ) < epsilon ) &&
  433. ( mFabs( z - compare.z ) < epsilon ) );
  434. }
  435. inline U32 Point3F::getLeastComponentIndex() const
  436. {
  437. U32 idx;
  438. if ( mFabs( x ) < mFabs( y ) )
  439. {
  440. if ( mFabs( x ) < mFabs( z ) )
  441. idx = 0;
  442. else
  443. idx = 2;
  444. }
  445. else
  446. {
  447. if ( mFabs( y ) < mFabs( z ) )
  448. idx = 1;
  449. else
  450. idx = 2;
  451. }
  452. return idx;
  453. }
  454. inline U32 Point3F::getGreatestComponentIndex() const
  455. {
  456. U32 idx;
  457. if ( mFabs( x ) > mFabs( y ) )
  458. {
  459. if ( mFabs( x ) > mFabs( z ) )
  460. idx = 0;
  461. else
  462. idx = 2;
  463. }
  464. else
  465. {
  466. if ( mFabs( y ) > mFabs( z ) )
  467. idx = 1;
  468. else
  469. idx = 2;
  470. }
  471. return idx;
  472. }
  473. inline F32 Point3F::least() const
  474. {
  475. return getMin( mFabs( x ), getMin( mFabs( y ), mFabs( z ) ) );
  476. }
  477. inline F32 Point3F::most() const
  478. {
  479. return getMax( mFabs( x ), getMax( mFabs( y ), mFabs( z ) ) );
  480. }
  481. inline void Point3F::neg()
  482. {
  483. x = -x;
  484. y = -y;
  485. z = -z;
  486. }
  487. inline void Point3F::convolve(const Point3F& c)
  488. {
  489. x *= c.x;
  490. y *= c.y;
  491. z *= c.z;
  492. }
  493. inline void Point3F::convolveInverse(const Point3F& c)
  494. {
  495. x /= c.x;
  496. y /= c.y;
  497. z /= c.z;
  498. }
  499. inline F32 Point3F::lenSquared() const
  500. {
  501. return (x * x) + (y * y) + (z * z);
  502. }
  503. inline F32 Point3F::len() const
  504. {
  505. return mSqrt(x*x + y*y + z*z);
  506. }
  507. inline void Point3F::normalize()
  508. {
  509. m_point3F_normalize(*this);
  510. }
  511. inline F32 Point3F::magnitudeSafe() const
  512. {
  513. if( isZero() )
  514. {
  515. return 0.0f;
  516. }
  517. else
  518. {
  519. return len();
  520. }
  521. }
  522. inline void Point3F::normalizeSafe()
  523. {
  524. F32 vmag = magnitudeSafe();
  525. if( vmag > POINT_EPSILON )
  526. {
  527. *this *= F32(1.0 / vmag);
  528. }
  529. }
  530. inline void Point3F::normalize(F32 val)
  531. {
  532. m_point3F_normalize_f(*this, val);
  533. }
  534. inline bool Point3F::operator==(const Point3F& _test) const
  535. {
  536. return (x == _test.x) && (y == _test.y) && (z == _test.z);
  537. }
  538. inline bool Point3F::operator!=(const Point3F& _test) const
  539. {
  540. return operator==(_test) == false;
  541. }
  542. inline Point3F Point3F::operator+(const Point3F& _add) const
  543. {
  544. return Point3F(x + _add.x, y + _add.y, z + _add.z);
  545. }
  546. inline Point3F Point3F::operator-(const Point3F& _rSub) const
  547. {
  548. return Point3F(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  549. }
  550. inline Point3F& Point3F::operator+=(const Point3F& _add)
  551. {
  552. x += _add.x;
  553. y += _add.y;
  554. z += _add.z;
  555. return *this;
  556. }
  557. inline Point3F& Point3F::operator-=(const Point3F& _rSub)
  558. {
  559. x -= _rSub.x;
  560. y -= _rSub.y;
  561. z -= _rSub.z;
  562. return *this;
  563. }
  564. inline Point3F Point3F::operator*(F32 _mul) const
  565. {
  566. return Point3F(x * _mul, y * _mul, z * _mul);
  567. }
  568. inline Point3F Point3F::operator/(F32 _div) const
  569. {
  570. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  571. F32 inv = 1.0f / _div;
  572. return Point3F(x * inv, y * inv, z * inv);
  573. }
  574. inline Point3F& Point3F::operator*=(F32 _mul)
  575. {
  576. x *= _mul;
  577. y *= _mul;
  578. z *= _mul;
  579. return *this;
  580. }
  581. inline Point3F& Point3F::operator/=(F32 _div)
  582. {
  583. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  584. F32 inv = 1.0f / _div;
  585. x *= inv;
  586. y *= inv;
  587. z *= inv;
  588. return *this;
  589. }
  590. inline Point3F Point3F::operator*(const Point3F &_vec) const
  591. {
  592. return Point3F(x * _vec.x, y * _vec.y, z * _vec.z);
  593. }
  594. inline Point3F& Point3F::operator*=(const Point3F &_vec)
  595. {
  596. x *= _vec.x;
  597. y *= _vec.y;
  598. z *= _vec.z;
  599. return *this;
  600. }
  601. inline Point3F Point3F::operator/(const Point3F &_vec) const
  602. {
  603. AssertFatal(_vec.x != 0.0f && _vec.y != 0.0f && _vec.z != 0.0f, "Error, div by zero attempted");
  604. return Point3F(x / _vec.x, y / _vec.y, z / _vec.z);
  605. }
  606. inline Point3F& Point3F::operator/=(const Point3F &_vec)
  607. {
  608. AssertFatal(_vec.x != 0.0f && _vec.y != 0.0f && _vec.z != 0.0f, "Error, div by zero attempted");
  609. x /= _vec.x;
  610. y /= _vec.y;
  611. z /= _vec.z;
  612. return *this;
  613. }
  614. inline Point3F Point3F::operator-() const
  615. {
  616. return Point3F(-x, -y, -z);
  617. }
  618. inline Point3F& Point3F::operator=(const Point3D &_vec)
  619. {
  620. x = (F32)_vec.x;
  621. y = (F32)_vec.y;
  622. z = (F32)_vec.z;
  623. return *this;
  624. }
  625. //------------------------------------------------------------------------------
  626. //-------------------------------------- Point3D
  627. //
  628. inline Point3D::Point3D()
  629. {
  630. //
  631. }
  632. inline Point3D::Point3D(const Point3D& _copy)
  633. : x(_copy.x), y(_copy.y), z(_copy.z)
  634. {
  635. //
  636. }
  637. inline Point3D::Point3D(const Point3F& _copy)
  638. : x(_copy.x), y(_copy.y), z(_copy.z)
  639. {
  640. //
  641. }
  642. inline Point3D::Point3D(F64 xyz)
  643. : x(xyz), y(xyz), z(xyz)
  644. {
  645. //
  646. }
  647. inline Point3D::Point3D(F64 _x, F64 _y, F64 _z)
  648. : x(_x), y(_y), z(_z)
  649. {
  650. //
  651. }
  652. inline void Point3D::set( F64 xyz )
  653. {
  654. x = y = z = xyz;
  655. }
  656. inline void Point3D::set(F64 _x, F64 _y, F64 _z)
  657. {
  658. x = _x;
  659. y = _y;
  660. z = _z;
  661. }
  662. inline void Point3D::setMin(const Point3D& _test)
  663. {
  664. x = (_test.x < x) ? _test.x : x;
  665. y = (_test.y < y) ? _test.y : y;
  666. z = (_test.z < z) ? _test.z : z;
  667. }
  668. inline void Point3D::setMax(const Point3D& _test)
  669. {
  670. x = (_test.x > x) ? _test.x : x;
  671. y = (_test.y > y) ? _test.y : y;
  672. z = (_test.z > z) ? _test.z : z;
  673. }
  674. inline void Point3D::interpolate(const Point3D& _from, const Point3D& _to, F64 _factor)
  675. {
  676. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  677. m_point3D_interpolate( _from, _to, _factor, *this);
  678. }
  679. inline void Point3D::zero()
  680. {
  681. x = y = z = 0.0;
  682. }
  683. inline bool Point3D::isZero() const
  684. {
  685. return (x == 0.0f) && (y == 0.0f) && (z == 0.0f);
  686. }
  687. inline void Point3D::neg()
  688. {
  689. x = -x;
  690. y = -y;
  691. z = -z;
  692. }
  693. inline void Point3D::convolve(const Point3D& c)
  694. {
  695. x *= c.x;
  696. y *= c.y;
  697. z *= c.z;
  698. }
  699. inline void Point3D::convolveInverse(const Point3D& c)
  700. {
  701. x /= c.x;
  702. y /= c.y;
  703. z /= c.z;
  704. }
  705. inline F64 Point3D::lenSquared() const
  706. {
  707. return (x * x) + (y * y) + (z * z);
  708. }
  709. inline F64 Point3D::len() const
  710. {
  711. F64 temp = x*x + y*y + z*z;
  712. return (temp > 0.0) ? mSqrtD(temp) : 0.0;
  713. }
  714. inline void Point3D::normalize()
  715. {
  716. m_point3D_normalize(*this);
  717. }
  718. inline F64 Point3D::magnitudeSafe() const
  719. {
  720. if( isZero() )
  721. {
  722. return 0.0;
  723. }
  724. else
  725. {
  726. return len();
  727. }
  728. }
  729. inline void Point3D::normalizeSafe()
  730. {
  731. F64 vmag = magnitudeSafe();
  732. if( vmag > POINT_EPSILON )
  733. {
  734. *this *= F64(1.0 / vmag);
  735. }
  736. }
  737. inline void Point3D::normalize(F64 val)
  738. {
  739. m_point3D_normalize_f(*this, val);
  740. }
  741. inline bool Point3D::operator==(const Point3D& _test) const
  742. {
  743. return (x == _test.x) && (y == _test.y) && (z == _test.z);
  744. }
  745. inline bool Point3D::operator!=(const Point3D& _test) const
  746. {
  747. return operator==(_test) == false;
  748. }
  749. inline Point3D Point3D::operator+(const Point3D& _add) const
  750. {
  751. return Point3D(x + _add.x, y + _add.y, z + _add.z);
  752. }
  753. inline Point3D Point3D::operator-(const Point3D& _rSub) const
  754. {
  755. return Point3D(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  756. }
  757. inline Point3D& Point3D::operator+=(const Point3D& _add)
  758. {
  759. x += _add.x;
  760. y += _add.y;
  761. z += _add.z;
  762. return *this;
  763. }
  764. inline Point3D& Point3D::operator-=(const Point3D& _rSub)
  765. {
  766. x -= _rSub.x;
  767. y -= _rSub.y;
  768. z -= _rSub.z;
  769. return *this;
  770. }
  771. inline Point3D Point3D::operator*(F64 _mul) const
  772. {
  773. return Point3D(x * _mul, y * _mul, z * _mul);
  774. }
  775. inline Point3D Point3D::operator/(F64 _div) const
  776. {
  777. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  778. F64 inv = 1.0f / _div;
  779. return Point3D(x * inv, y * inv, z * inv);
  780. }
  781. inline Point3D& Point3D::operator*=(F64 _mul)
  782. {
  783. x *= _mul;
  784. y *= _mul;
  785. z *= _mul;
  786. return *this;
  787. }
  788. inline Point3D& Point3D::operator/=(F64 _div)
  789. {
  790. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  791. F64 inv = 1.0f / _div;
  792. x *= inv;
  793. y *= inv;
  794. z *= inv;
  795. return *this;
  796. }
  797. inline Point3D Point3D::operator-() const
  798. {
  799. return Point3D(-x, -y, -z);
  800. }
  801. inline Point3F Point3D::toPoint3F() const
  802. {
  803. return Point3F((F32)x,(F32)y,(F32)z);
  804. }
  805. //-------------------------------------------------------------------
  806. // Non-Member Operators
  807. //-------------------------------------------------------------------
  808. inline Point3I operator*(S32 mul, const Point3I& multiplicand)
  809. {
  810. return multiplicand * mul;
  811. }
  812. inline Point3F operator*(F32 mul, const Point3F& multiplicand)
  813. {
  814. return multiplicand * mul;
  815. }
  816. inline Point3D operator*(F64 mul, const Point3D& multiplicand)
  817. {
  818. return multiplicand * mul;
  819. }
  820. inline F32 mDot(const Point3F &p1, const Point3F &p2)
  821. {
  822. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
  823. }
  824. inline F64 mDot(const Point3D &p1, const Point3D &p2)
  825. {
  826. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
  827. }
  828. inline void mCross(const Point3F &a, const Point3F &b, Point3F *res)
  829. {
  830. res->x = (a.y * b.z) - (a.z * b.y);
  831. res->y = (a.z * b.x) - (a.x * b.z);
  832. res->z = (a.x * b.y) - (a.y * b.x);
  833. }
  834. inline void mCross(const Point3D &a, const Point3D &b, Point3D *res)
  835. {
  836. res->x = (a.y * b.z) - (a.z * b.y);
  837. res->y = (a.z * b.x) - (a.x * b.z);
  838. res->z = (a.x * b.y) - (a.y * b.x);
  839. }
  840. inline Point3F mCross(const Point3F &a, const Point3F &b)
  841. {
  842. Point3F r;
  843. mCross( a, b, &r );
  844. return r;
  845. }
  846. inline Point3D mCross(const Point3D &a, const Point3D &b)
  847. {
  848. Point3D r;
  849. mCross( a, b, &r );
  850. return r;
  851. }
  852. /// Returns the vector normalized.
  853. inline Point3F mNormalize( const Point3F &vec )
  854. {
  855. Point3F out( vec );
  856. out.normalize();
  857. return out;
  858. }
  859. /// Returns true if the point is NaN.
  860. inline bool mIsNaN( const Point3F &p )
  861. {
  862. return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z );
  863. }
  864. /// Returns a copy of the vector reflected by a normal
  865. inline Point3F mReflect( const Point3F &v, const Point3F &n )
  866. {
  867. return v - 2 * n * mDot( v, n );
  868. }
  869. /// Returns a perpendicular vector to the unit length input vector.
  870. extern Point3F mPerp( const Point3F &normal );
  871. #endif // _MPOINT3_H_