mPoint3.h 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. : x(0), y(0), z(0)
  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. : x(0.0f), y(0.0f), z(0.0f)
  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. AssertFatal(_vec.x != 0.0f && _vec.y != 0.0f && _vec.z != 0.0f, "Error, div by zero attempted");
  602. return Point3F(x / _vec.x, y / _vec.y, z / _vec.z);
  603. }
  604. inline Point3F& Point3F::operator/=(const Point3F &_vec)
  605. {
  606. AssertFatal(_vec.x != 0.0f && _vec.y != 0.0f && _vec.z != 0.0f, "Error, div by zero attempted");
  607. x /= _vec.x;
  608. y /= _vec.y;
  609. z /= _vec.z;
  610. return *this;
  611. }
  612. inline Point3F Point3F::operator-() const
  613. {
  614. return Point3F(-x, -y, -z);
  615. }
  616. inline Point3F& Point3F::operator=(const Point3D &_vec)
  617. {
  618. x = (F32)_vec.x;
  619. y = (F32)_vec.y;
  620. z = (F32)_vec.z;
  621. return *this;
  622. }
  623. //------------------------------------------------------------------------------
  624. //-------------------------------------- Point3D
  625. //
  626. inline Point3D::Point3D()
  627. : x(0.0), y(0.0), z(0.0)
  628. {
  629. //
  630. }
  631. inline Point3D::Point3D(const Point3D& _copy)
  632. : x(_copy.x), y(_copy.y), z(_copy.z)
  633. {
  634. //
  635. }
  636. inline Point3D::Point3D(const Point3F& _copy)
  637. : x(_copy.x), y(_copy.y), z(_copy.z)
  638. {
  639. //
  640. }
  641. inline Point3D::Point3D(F64 xyz)
  642. : x(xyz), y(xyz), z(xyz)
  643. {
  644. //
  645. }
  646. inline Point3D::Point3D(F64 _x, F64 _y, F64 _z)
  647. : x(_x), y(_y), z(_z)
  648. {
  649. //
  650. }
  651. inline void Point3D::set( F64 xyz )
  652. {
  653. x = y = z = xyz;
  654. }
  655. inline void Point3D::set(F64 _x, F64 _y, F64 _z)
  656. {
  657. x = _x;
  658. y = _y;
  659. z = _z;
  660. }
  661. inline void Point3D::setMin(const Point3D& _test)
  662. {
  663. x = (_test.x < x) ? _test.x : x;
  664. y = (_test.y < y) ? _test.y : y;
  665. z = (_test.z < z) ? _test.z : z;
  666. }
  667. inline void Point3D::setMax(const Point3D& _test)
  668. {
  669. x = (_test.x > x) ? _test.x : x;
  670. y = (_test.y > y) ? _test.y : y;
  671. z = (_test.z > z) ? _test.z : z;
  672. }
  673. inline void Point3D::interpolate(const Point3D& _from, const Point3D& _to, F64 _factor)
  674. {
  675. AssertFatal(_factor >= 0.0f && _factor <= 1.0f, "Out of bound interpolation factor");
  676. m_point3D_interpolate( _from, _to, _factor, *this);
  677. }
  678. inline void Point3D::zero()
  679. {
  680. x = y = z = 0.0;
  681. }
  682. inline bool Point3D::isZero() const
  683. {
  684. return (x == 0.0f) && (y == 0.0f) && (z == 0.0f);
  685. }
  686. inline void Point3D::neg()
  687. {
  688. x = -x;
  689. y = -y;
  690. z = -z;
  691. }
  692. inline void Point3D::convolve(const Point3D& c)
  693. {
  694. x *= c.x;
  695. y *= c.y;
  696. z *= c.z;
  697. }
  698. inline void Point3D::convolveInverse(const Point3D& c)
  699. {
  700. x /= c.x;
  701. y /= c.y;
  702. z /= c.z;
  703. }
  704. inline F64 Point3D::lenSquared() const
  705. {
  706. return (x * x) + (y * y) + (z * z);
  707. }
  708. inline F64 Point3D::len() const
  709. {
  710. F64 temp = x*x + y*y + z*z;
  711. return (temp > 0.0) ? mSqrtD(temp) : 0.0;
  712. }
  713. inline void Point3D::normalize()
  714. {
  715. m_point3D_normalize(*this);
  716. }
  717. inline F64 Point3D::magnitudeSafe() const
  718. {
  719. if( isZero() )
  720. {
  721. return 0.0;
  722. }
  723. else
  724. {
  725. return len();
  726. }
  727. }
  728. inline void Point3D::normalizeSafe()
  729. {
  730. F64 vmag = magnitudeSafe();
  731. if( vmag > POINT_EPSILON )
  732. {
  733. *this *= F64(1.0 / vmag);
  734. }
  735. }
  736. inline void Point3D::normalize(F64 val)
  737. {
  738. m_point3D_normalize_f(*this, val);
  739. }
  740. inline bool Point3D::operator==(const Point3D& _test) const
  741. {
  742. return (x == _test.x) && (y == _test.y) && (z == _test.z);
  743. }
  744. inline bool Point3D::operator!=(const Point3D& _test) const
  745. {
  746. return operator==(_test) == false;
  747. }
  748. inline Point3D Point3D::operator+(const Point3D& _add) const
  749. {
  750. return Point3D(x + _add.x, y + _add.y, z + _add.z);
  751. }
  752. inline Point3D Point3D::operator-(const Point3D& _rSub) const
  753. {
  754. return Point3D(x - _rSub.x, y - _rSub.y, z - _rSub.z);
  755. }
  756. inline Point3D& Point3D::operator+=(const Point3D& _add)
  757. {
  758. x += _add.x;
  759. y += _add.y;
  760. z += _add.z;
  761. return *this;
  762. }
  763. inline Point3D& Point3D::operator-=(const Point3D& _rSub)
  764. {
  765. x -= _rSub.x;
  766. y -= _rSub.y;
  767. z -= _rSub.z;
  768. return *this;
  769. }
  770. inline Point3D Point3D::operator*(F64 _mul) const
  771. {
  772. return Point3D(x * _mul, y * _mul, z * _mul);
  773. }
  774. inline Point3D Point3D::operator/(F64 _div) const
  775. {
  776. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  777. F64 inv = 1.0f / _div;
  778. return Point3D(x * inv, y * inv, z * inv);
  779. }
  780. inline Point3D& Point3D::operator*=(F64 _mul)
  781. {
  782. x *= _mul;
  783. y *= _mul;
  784. z *= _mul;
  785. return *this;
  786. }
  787. inline Point3D& Point3D::operator/=(F64 _div)
  788. {
  789. AssertFatal(_div != 0.0f, "Error, div by zero attempted");
  790. F64 inv = 1.0f / _div;
  791. x *= inv;
  792. y *= inv;
  793. z *= inv;
  794. return *this;
  795. }
  796. inline Point3D Point3D::operator-() const
  797. {
  798. return Point3D(-x, -y, -z);
  799. }
  800. inline Point3F Point3D::toPoint3F() const
  801. {
  802. return Point3F((F32)x,(F32)y,(F32)z);
  803. }
  804. //-------------------------------------------------------------------
  805. // Non-Member Operators
  806. //-------------------------------------------------------------------
  807. inline Point3I operator*(S32 mul, const Point3I& multiplicand)
  808. {
  809. return multiplicand * mul;
  810. }
  811. inline Point3F operator*(F32 mul, const Point3F& multiplicand)
  812. {
  813. return multiplicand * mul;
  814. }
  815. inline Point3D operator*(F64 mul, const Point3D& multiplicand)
  816. {
  817. return multiplicand * mul;
  818. }
  819. inline F32 mDot(const Point3F &p1, const Point3F &p2)
  820. {
  821. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
  822. }
  823. inline F64 mDot(const Point3D &p1, const Point3D &p2)
  824. {
  825. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
  826. }
  827. inline void mCross(const Point3F &a, const Point3F &b, Point3F *res)
  828. {
  829. res->x = (a.y * b.z) - (a.z * b.y);
  830. res->y = (a.z * b.x) - (a.x * b.z);
  831. res->z = (a.x * b.y) - (a.y * b.x);
  832. }
  833. inline void mCross(const Point3D &a, const Point3D &b, Point3D *res)
  834. {
  835. res->x = (a.y * b.z) - (a.z * b.y);
  836. res->y = (a.z * b.x) - (a.x * b.z);
  837. res->z = (a.x * b.y) - (a.y * b.x);
  838. }
  839. inline Point3F mCross(const Point3F &a, const Point3F &b)
  840. {
  841. Point3F r;
  842. mCross( a, b, &r );
  843. return r;
  844. }
  845. inline Point3D mCross(const Point3D &a, const Point3D &b)
  846. {
  847. Point3D r;
  848. mCross( a, b, &r );
  849. return r;
  850. }
  851. /// Returns the vector normalized.
  852. inline Point3F mNormalize( const Point3F &vec )
  853. {
  854. Point3F out( vec );
  855. out.normalize();
  856. return out;
  857. }
  858. /// Returns true if the point is NaN.
  859. inline bool mIsNaN( const Point3F &p )
  860. {
  861. return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z );
  862. }
  863. /// Returns a copy of the vector reflected by a normal
  864. inline Point3F mReflect( const Point3F &v, const Point3F &n )
  865. {
  866. return v - 2 * n * mDot( v, n );
  867. }
  868. /// Returns a perpendicular vector to the unit length input vector.
  869. extern Point3F mPerp( const Point3F &normal );
  870. #endif // _MPOINT3_H_