mPoint3.h 21 KB

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