mBox.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 _MBOX_H_
  23. #define _MBOX_H_
  24. #ifndef _MBOXBASE_H_
  25. #include "math/mBoxBase.h"
  26. #endif
  27. #ifndef _MPOINT3_H_
  28. #include "math/mPoint3.h"
  29. #endif
  30. #ifndef _MPOINT2_H_
  31. #include "math/mPoint2.h"
  32. #endif
  33. class MatrixF;
  34. class SphereF;
  35. /// Axis-aligned bounding box (AABB).
  36. ///
  37. /// A helper class for working with boxes. It runs at F32 precision.
  38. ///
  39. /// @see Box3D
  40. class Box3F : public BoxBase
  41. {
  42. public:
  43. Point3F minExtents; ///< Minimum extents of box
  44. Point3F maxExtents; ///< Maximum extents of box
  45. Box3F() { }
  46. /// Create a box from two points.
  47. ///
  48. /// Normally, this function will compensate for mismatched
  49. /// min/max values. If you know your values are valid, you
  50. /// can set in_overrideCheck to true and skip this.
  51. ///
  52. /// @param in_rMin Minimum extents of box.
  53. /// @param in_rMax Maximum extents of box.
  54. /// @param in_overrideCheck Pass true to skip check of extents.
  55. Box3F( const Point3F& in_rMin, const Point3F& in_rMax, const bool in_overrideCheck = false );
  56. /// Create a box from six extent values.
  57. ///
  58. /// No checking is performed as to the validity of these
  59. /// extents, unlike the other constructor.
  60. Box3F( const F32 &xMin, const F32 &yMin, const F32 &zMin,
  61. const F32 &xMax, const F32 &yMax, const F32 &zMax );
  62. Box3F(F32 cubeSize);
  63. void set( const Point3F& in_rMin, const Point3F& in_rMax );
  64. void set( const F32 &xMin, const F32 &yMin, const F32 &zMin,
  65. const F32 &xMax, const F32 &yMax, const F32 &zMax );
  66. /// Create box around origin given lengths
  67. void set( const Point3F& in_Length );
  68. /// Recenter the box
  69. void setCenter( const Point3F& center );
  70. /// Check to see if a point is contained in this box.
  71. bool isContained( const Point3F& in_rContained ) const;
  72. /// Check if the Point2F is within the box xy extents.
  73. bool isContained( const Point2F &pt ) const;
  74. /// Check to see if another box overlaps this box.
  75. bool isOverlapped( const Box3F& in_rOverlap ) const;
  76. /// Check if the given sphere overlaps with the box.
  77. bool isOverlapped( const SphereF& sphere ) const;
  78. /// Check to see if another box is contained in this box.
  79. bool isContained( const Box3F& in_rContained ) const;
  80. /// Returns the length of the x extent.
  81. F32 len_x() const { return maxExtents.x - minExtents.x; }
  82. /// Returns the length of the y extent.
  83. F32 len_y() const { return maxExtents.y - minExtents.y; }
  84. /// Returns the length of the z extent.
  85. F32 len_z() const { return maxExtents.z - minExtents.z; }
  86. /// Returns the minimum box extent.
  87. F32 len_min() const { return getMin( len_x(), getMin( len_y(), len_z() ) ); }
  88. /// Returns the maximum box extent.
  89. F32 len_max() const { return getMax( len_x(), getMax( len_y(), len_z() ) ); }
  90. /// Returns the diagonal box length.
  91. F32 len() const { return ( maxExtents - minExtents ).len(); }
  92. /// Returns the length of extent by axis index.
  93. ///
  94. /// @param axis The axis index of 0, 1, or 2.
  95. ///
  96. F32 len( S32 axis ) const { return maxExtents[axis] - minExtents[axis]; }
  97. /// Returns true if any of the extent axes
  98. /// are less than or equal to zero.
  99. bool isEmpty() const { return len_x() <= 0.0f || len_y() <= 0.0f || len_z() <= 0.0f; }
  100. /// Perform an intersection operation with another box
  101. /// and store the results in this box.
  102. void intersect( const Box3F &in_rIntersect );
  103. void intersect( const Point3F &in_rIntersect );
  104. /// Return the overlap between this box and @a otherBox.
  105. Box3F getOverlap( const Box3F& otherBox ) const;
  106. /// Return the volume of the box.
  107. F32 getVolume() const;
  108. /// Get the center of this box.
  109. ///
  110. /// This is the average of min and max.
  111. void getCenter( Point3F *center ) const;
  112. Point3F getCenter() const;
  113. /// Returns the max minus the min extents.
  114. Point3F getExtents() const { return maxExtents - minExtents; }
  115. /// Collide a line against the box.
  116. ///
  117. /// @param start Start of line.
  118. /// @param end End of line.
  119. /// @param t Value from 0.0-1.0, indicating position
  120. /// along line of collision.
  121. /// @param n Normal of collision.
  122. bool collideLine( const Point3F &start, const Point3F &end, F32 *t, Point3F *n ) const;
  123. /// Collide a line against the box.
  124. ///
  125. /// Returns true on collision.
  126. bool collideLine( const Point3F &start, const Point3F &end ) const;
  127. /// Collide an oriented box against the box.
  128. ///
  129. /// Returns true if "oriented" box collides with us.
  130. /// Assumes incoming box is centered at origin of source space.
  131. ///
  132. /// @param radii The dimension of incoming box (half x,y,z length).
  133. /// @param toUs A transform that takes incoming box into our space.
  134. bool collideOrientedBox( const Point3F &radii, const MatrixF &toUs ) const;
  135. /// Check that the min extents of the box is
  136. /// less than or equal to the max extents.
  137. bool isValidBox() const { return (minExtents.x <= maxExtents.x) &&
  138. (minExtents.y <= maxExtents.y) &&
  139. (minExtents.z <= maxExtents.z); }
  140. /// Return the closest point of the box, relative to the passed point.
  141. Point3F getClosestPoint( const Point3F &refPt ) const;
  142. /// Return distance of closest point on box to refPt.
  143. F32 getDistanceToPoint( const Point3F &refPt ) const;
  144. /// Return the squared distance to closest point on box to refPt.
  145. F32 getSqDistanceToPoint( const Point3F &refPt ) const;
  146. /// Return one of the corner vertices of the box.
  147. Point3F computeVertex( U32 corner ) const;
  148. /// Get the length of the longest diagonal in the box.
  149. F32 getGreatestDiagonalLength() const;
  150. /// Return the bounding sphere that contains this AABB.
  151. SphereF getBoundingSphere() const;
  152. /// Extend the box to include point.
  153. /// @see Invalid
  154. void extend( const Point3F &p );
  155. /// Scale the box by a Point3F or F32
  156. void scale( const Point3F &amt );
  157. void scale( F32 amt );
  158. /// Equality operator.
  159. bool operator ==( const Box3F &b ) const;
  160. /// Inequality operator.
  161. bool operator !=( const Box3F &b ) const;
  162. /// Create an AABB that fits around the given point cloud, i.e.
  163. /// find the minimum and maximum extents of the given point set.
  164. static Box3F aroundPoints( const Point3F* points, U32 numPoints );
  165. public:
  166. /// An inverted bounds where the minimum point is positive
  167. /// and the maximum point is negative. Should be used with
  168. /// extend() to construct a minimum volume box.
  169. /// @see extend
  170. static const Box3F Invalid;
  171. /// A box that covers the entire floating point range.
  172. static const Box3F Max;
  173. /// A null box of zero size.
  174. static const Box3F Zero;
  175. };
  176. inline Box3F::Box3F(const Point3F& in_rMin, const Point3F& in_rMax, const bool in_overrideCheck)
  177. : minExtents(in_rMin),
  178. maxExtents(in_rMax)
  179. {
  180. if (in_overrideCheck == false) {
  181. minExtents.setMin(in_rMax);
  182. maxExtents.setMax(in_rMin);
  183. }
  184. }
  185. inline Box3F::Box3F( const F32 &xMin, const F32 &yMin, const F32 &zMin,
  186. const F32 &xMax, const F32 &yMax, const F32 &zMax )
  187. : minExtents(xMin,yMin,zMin),
  188. maxExtents(xMax,yMax,zMax)
  189. {
  190. }
  191. inline Box3F::Box3F(F32 cubeSize)
  192. : minExtents(-0.5f * cubeSize, -0.5f * cubeSize, -0.5f * cubeSize),
  193. maxExtents(0.5f * cubeSize, 0.5f * cubeSize, 0.5f * cubeSize)
  194. {
  195. }
  196. inline void Box3F::set(const Point3F& in_rMin, const Point3F& in_rMax)
  197. {
  198. minExtents.set(in_rMin);
  199. maxExtents.set(in_rMax);
  200. }
  201. inline void Box3F::set( const F32 &xMin, const F32 &yMin, const F32 &zMin,
  202. const F32 &xMax, const F32 &yMax, const F32 &zMax )
  203. {
  204. minExtents.set( xMin, yMin, zMin );
  205. maxExtents.set( xMax, yMax, zMax );
  206. }
  207. inline void Box3F::set(const Point3F& in_Length)
  208. {
  209. minExtents.set(-in_Length.x * 0.5f, -in_Length.y * 0.5f, -in_Length.z * 0.5f);
  210. maxExtents.set( in_Length.x * 0.5f, in_Length.y * 0.5f, in_Length.z * 0.5f);
  211. }
  212. inline void Box3F::setCenter(const Point3F& center)
  213. {
  214. F32 halflenx = len_x() * 0.5f;
  215. F32 halfleny = len_y() * 0.5f;
  216. F32 halflenz = len_z() * 0.5f;
  217. minExtents.set(center.x-halflenx, center.y-halfleny, center.z-halflenz);
  218. maxExtents.set(center.x+halflenx, center.y+halfleny, center.z+halflenz);
  219. }
  220. inline bool Box3F::isContained(const Point3F& in_rContained) const
  221. {
  222. return (in_rContained.x >= minExtents.x && in_rContained.x < maxExtents.x) &&
  223. (in_rContained.y >= minExtents.y && in_rContained.y < maxExtents.y) &&
  224. (in_rContained.z >= minExtents.z && in_rContained.z < maxExtents.z);
  225. }
  226. inline bool Box3F::isContained( const Point2F &pt ) const
  227. {
  228. return ( pt.x >= minExtents.x && pt.x < maxExtents.x ) &&
  229. ( pt.y >= minExtents.y && pt.y < maxExtents.y );
  230. }
  231. inline bool Box3F::isOverlapped(const Box3F& in_rOverlap) const
  232. {
  233. if (in_rOverlap.minExtents.x > maxExtents.x ||
  234. in_rOverlap.minExtents.y > maxExtents.y ||
  235. in_rOverlap.minExtents.z > maxExtents.z)
  236. return false;
  237. if (in_rOverlap.maxExtents.x < minExtents.x ||
  238. in_rOverlap.maxExtents.y < minExtents.y ||
  239. in_rOverlap.maxExtents.z < minExtents.z)
  240. return false;
  241. return true;
  242. }
  243. inline bool Box3F::isContained(const Box3F& in_rContained) const
  244. {
  245. return (minExtents.x <= in_rContained.minExtents.x) &&
  246. (minExtents.y <= in_rContained.minExtents.y) &&
  247. (minExtents.z <= in_rContained.minExtents.z) &&
  248. (maxExtents.x >= in_rContained.maxExtents.x) &&
  249. (maxExtents.y >= in_rContained.maxExtents.y) &&
  250. (maxExtents.z >= in_rContained.maxExtents.z);
  251. }
  252. inline void Box3F::intersect(const Box3F& in_rIntersect)
  253. {
  254. minExtents.setMin(in_rIntersect.minExtents);
  255. maxExtents.setMax(in_rIntersect.maxExtents);
  256. }
  257. inline void Box3F::intersect(const Point3F& in_rIntersect)
  258. {
  259. minExtents.setMin(in_rIntersect);
  260. maxExtents.setMax(in_rIntersect);
  261. }
  262. inline Box3F Box3F::getOverlap( const Box3F& otherBox ) const
  263. {
  264. Box3F overlap;
  265. for( U32 i = 0; i < 3; ++ i )
  266. {
  267. if( minExtents[ i ] > otherBox.maxExtents[ i ] || otherBox.minExtents[ i ] > maxExtents[ i ] )
  268. {
  269. overlap.minExtents[ i ] = 0.f;
  270. overlap.maxExtents[ i ] = 0.f;
  271. }
  272. else
  273. {
  274. overlap.minExtents[ i ] = getMax( minExtents[ i ], otherBox.minExtents[ i ] );
  275. overlap.maxExtents[ i ] = getMin( maxExtents[ i ], otherBox.maxExtents[ i ] );
  276. }
  277. }
  278. return overlap;
  279. }
  280. inline F32 Box3F::getVolume() const
  281. {
  282. return ( maxExtents.x - minExtents.x ) * ( maxExtents.y - minExtents.y ) * ( maxExtents.z - minExtents.z );
  283. }
  284. inline void Box3F::getCenter(Point3F* center) const
  285. {
  286. center->x = (minExtents.x + maxExtents.x) * 0.5f;
  287. center->y = (minExtents.y + maxExtents.y) * 0.5f;
  288. center->z = (minExtents.z + maxExtents.z) * 0.5f;
  289. }
  290. inline Point3F Box3F::getCenter() const
  291. {
  292. Point3F center;
  293. center.x = (minExtents.x + maxExtents.x) * 0.5f;
  294. center.y = (minExtents.y + maxExtents.y) * 0.5f;
  295. center.z = (minExtents.z + maxExtents.z) * 0.5f;
  296. return center;
  297. }
  298. inline Point3F Box3F::getClosestPoint(const Point3F& refPt) const
  299. {
  300. Point3F closest;
  301. if (refPt.x <= minExtents.x) closest.x = minExtents.x;
  302. else if (refPt.x > maxExtents.x) closest.x = maxExtents.x;
  303. else closest.x = refPt.x;
  304. if (refPt.y <= minExtents.y) closest.y = minExtents.y;
  305. else if (refPt.y > maxExtents.y) closest.y = maxExtents.y;
  306. else closest.y = refPt.y;
  307. if (refPt.z <= minExtents.z) closest.z = minExtents.z;
  308. else if (refPt.z > maxExtents.z) closest.z = maxExtents.z;
  309. else closest.z = refPt.z;
  310. return closest;
  311. }
  312. inline F32 Box3F::getDistanceToPoint(const Point3F& refPt) const
  313. {
  314. return mSqrt( getSqDistanceToPoint( refPt ) );
  315. }
  316. inline F32 Box3F::getSqDistanceToPoint( const Point3F &refPt ) const
  317. {
  318. F32 sqDist = 0.0f;
  319. for ( U32 i=0; i < 3; i++ )
  320. {
  321. const F32 v = refPt[i];
  322. if ( v < minExtents[i] )
  323. sqDist += mSquared( minExtents[i] - v );
  324. else if ( v > maxExtents[i] )
  325. sqDist += mSquared( v - maxExtents[i] );
  326. }
  327. return sqDist;
  328. }
  329. inline void Box3F::extend(const Point3F & p)
  330. {
  331. #define EXTEND_AXIS(AXIS) \
  332. if (p.AXIS < minExtents.AXIS) \
  333. minExtents.AXIS = p.AXIS; \
  334. if (p.AXIS > maxExtents.AXIS) \
  335. maxExtents.AXIS = p.AXIS;
  336. EXTEND_AXIS(x)
  337. EXTEND_AXIS(y)
  338. EXTEND_AXIS(z)
  339. #undef EXTEND_AXIS
  340. }
  341. inline void Box3F::scale( const Point3F &amt )
  342. {
  343. minExtents *= amt;
  344. maxExtents *= amt;
  345. }
  346. inline void Box3F::scale( F32 amt )
  347. {
  348. minExtents *= amt;
  349. maxExtents *= amt;
  350. }
  351. inline bool Box3F::operator ==( const Box3F &b ) const
  352. {
  353. return minExtents.equal( b.minExtents ) && maxExtents.equal( b.maxExtents );
  354. }
  355. inline bool Box3F::operator !=( const Box3F &b ) const
  356. {
  357. return !minExtents.equal( b.minExtents ) || !maxExtents.equal( b.maxExtents );
  358. }
  359. //------------------------------------------------------------------------------
  360. /// Clone of Box3F, using 3D types.
  361. ///
  362. /// 3D types use F64.
  363. ///
  364. /// @see Box3F
  365. class Box3D
  366. {
  367. public:
  368. Point3D minExtents;
  369. Point3D maxExtents;
  370. public:
  371. Box3D() { }
  372. Box3D(const Point3D& in_rMin, const Point3D& in_rMax, const bool in_overrideCheck = false);
  373. bool isContained(const Point3D& in_rContained) const;
  374. bool isOverlapped(const Box3D& in_rOverlap) const;
  375. F64 len_x() const;
  376. F64 len_y() const;
  377. F64 len_z() const;
  378. void intersect(const Box3D& in_rIntersect);
  379. void getCenter(Point3D* center) const;
  380. void extend(const Point3D & p);
  381. };
  382. inline Box3D::Box3D(const Point3D& in_rMin, const Point3D& in_rMax, const bool in_overrideCheck)
  383. : minExtents(in_rMin),
  384. maxExtents(in_rMax)
  385. {
  386. if (in_overrideCheck == false) {
  387. minExtents.setMin(in_rMax);
  388. maxExtents.setMax(in_rMin);
  389. }
  390. }
  391. inline bool Box3D::isContained(const Point3D& in_rContained) const
  392. {
  393. return (in_rContained.x >= minExtents.x && in_rContained.x < maxExtents.x) &&
  394. (in_rContained.y >= minExtents.y && in_rContained.y < maxExtents.y) &&
  395. (in_rContained.z >= minExtents.z && in_rContained.z < maxExtents.z);
  396. }
  397. inline bool Box3D::isOverlapped(const Box3D& in_rOverlap) const
  398. {
  399. if (in_rOverlap.minExtents.x > maxExtents.x ||
  400. in_rOverlap.minExtents.y > maxExtents.y ||
  401. in_rOverlap.minExtents.z > maxExtents.z)
  402. return false;
  403. if (in_rOverlap.maxExtents.x < minExtents.x ||
  404. in_rOverlap.maxExtents.y < minExtents.y ||
  405. in_rOverlap.maxExtents.z < minExtents.z)
  406. return false;
  407. return true;
  408. }
  409. inline F64 Box3D::len_x() const
  410. {
  411. return maxExtents.x - minExtents.x;
  412. }
  413. inline F64 Box3D::len_y() const
  414. {
  415. return maxExtents.y - minExtents.y;
  416. }
  417. inline F64 Box3D::len_z() const
  418. {
  419. return maxExtents.z - minExtents.z;
  420. }
  421. inline void Box3D::intersect(const Box3D& in_rIntersect)
  422. {
  423. minExtents.setMin(in_rIntersect.minExtents);
  424. maxExtents.setMax(in_rIntersect.maxExtents);
  425. }
  426. inline void Box3D::getCenter(Point3D* center) const
  427. {
  428. center->x = (minExtents.x + maxExtents.x) * 0.5;
  429. center->y = (minExtents.y + maxExtents.y) * 0.5;
  430. center->z = (minExtents.z + maxExtents.z) * 0.5;
  431. }
  432. inline void Box3D::extend(const Point3D & p)
  433. {
  434. #define EXTEND_AXIS(AXIS) \
  435. if (p.AXIS < minExtents.AXIS) \
  436. minExtents.AXIS = p.AXIS; \
  437. else if (p.AXIS > maxExtents.AXIS) \
  438. maxExtents.AXIS = p.AXIS;
  439. EXTEND_AXIS(x)
  440. EXTEND_AXIS(y)
  441. EXTEND_AXIS(z)
  442. #undef EXTEND_AXIS
  443. }
  444. /// Bounding Box
  445. ///
  446. /// A helper class for working with boxes. It runs at F32 precision.
  447. ///
  448. /// @see Box3D
  449. class Box3I
  450. {
  451. public:
  452. Point3I minExtents; ///< Minimum extents of box
  453. Point3I maxExtents; ///< Maximum extents of box
  454. public:
  455. Box3I() { }
  456. /// Create a box from two points.
  457. ///
  458. /// Normally, this function will compensate for mismatched
  459. /// min/max values. If you know your values are valid, you
  460. /// can set in_overrideCheck to true and skip this.
  461. ///
  462. /// @param in_rMin Minimum extents of box.
  463. /// @param in_rMax Maximum extents of box.
  464. /// @param in_overrideCheck Pass true to skip check of extents.
  465. Box3I(const Point3I& in_rMin, const Point3I& in_rMax, const bool in_overrideCheck = false);
  466. /// Create a box from six extent values.
  467. ///
  468. /// No checking is performed as to the validity of these
  469. /// extents, unlike the other constructor.
  470. Box3I(S32 xmin, S32 ymin, S32 zmin, S32 max, S32 ymax, S32 zmax);
  471. /// Check to see if a point is contained in this box.
  472. bool isContained(const Point3I& in_rContained) const;
  473. /// Check to see if another box overlaps this box.
  474. bool isOverlapped(const Box3I& in_rOverlap) const;
  475. /// Check to see if another box is contained in this box.
  476. bool isContained(const Box3I& in_rContained) const;
  477. S32 len_x() const;
  478. S32 len_y() const;
  479. S32 len_z() const;
  480. /// Perform an intersection operation with another box
  481. /// and store the results in this box.
  482. void intersect(const Box3I& in_rIntersect);
  483. /// Get the center of this box.
  484. ///
  485. /// This is the average of min and max.
  486. void getCenter(Point3I* center) const;
  487. /// Check that the box is valid.
  488. ///
  489. /// Currently, this just means that min < max.
  490. bool isValidBox() const
  491. {
  492. return (minExtents.x <= maxExtents.x) &&
  493. (minExtents.y <= maxExtents.y) &&
  494. (minExtents.z <= maxExtents.z);
  495. }
  496. void extend(const Point3I & p);
  497. };
  498. inline Box3I::Box3I(const Point3I& in_rMin, const Point3I& in_rMax, const bool in_overrideCheck)
  499. : minExtents(in_rMin),
  500. maxExtents(in_rMax)
  501. {
  502. if (in_overrideCheck == false)
  503. {
  504. minExtents.setMin(in_rMax);
  505. maxExtents.setMax(in_rMin);
  506. }
  507. }
  508. inline Box3I::Box3I(S32 xMin, S32 yMin, S32 zMin, S32 xMax, S32 yMax, S32 zMax)
  509. : minExtents(xMin,yMin,zMin),
  510. maxExtents(xMax,yMax,zMax)
  511. {
  512. }
  513. inline bool Box3I::isContained(const Point3I& in_rContained) const
  514. {
  515. return (in_rContained.x >= minExtents.x && in_rContained.x < maxExtents.x) &&
  516. (in_rContained.y >= minExtents.y && in_rContained.y < maxExtents.y) &&
  517. (in_rContained.z >= minExtents.z && in_rContained.z < maxExtents.z);
  518. }
  519. inline bool Box3I::isOverlapped(const Box3I& in_rOverlap) const
  520. {
  521. if (in_rOverlap.minExtents.x > maxExtents.x ||
  522. in_rOverlap.minExtents.y > maxExtents.y ||
  523. in_rOverlap.minExtents.z > maxExtents.z)
  524. return false;
  525. if (in_rOverlap.maxExtents.x < minExtents.x ||
  526. in_rOverlap.maxExtents.y < minExtents.y ||
  527. in_rOverlap.maxExtents.z < minExtents.z)
  528. return false;
  529. return true;
  530. }
  531. inline bool Box3I::isContained(const Box3I& in_rContained) const
  532. {
  533. return (minExtents.x <= in_rContained.minExtents.x) &&
  534. (minExtents.y <= in_rContained.minExtents.y) &&
  535. (minExtents.z <= in_rContained.minExtents.z) &&
  536. (maxExtents.x >= in_rContained.maxExtents.x) &&
  537. (maxExtents.y >= in_rContained.maxExtents.y) &&
  538. (maxExtents.z >= in_rContained.maxExtents.z);
  539. }
  540. inline S32 Box3I::len_x() const
  541. {
  542. return maxExtents.x - minExtents.x;
  543. }
  544. inline S32 Box3I::len_y() const
  545. {
  546. return maxExtents.y - minExtents.y;
  547. }
  548. inline S32 Box3I::len_z() const
  549. {
  550. return maxExtents.z - minExtents.z;
  551. }
  552. inline void Box3I::intersect(const Box3I& in_rIntersect)
  553. {
  554. minExtents.setMin(in_rIntersect.minExtents);
  555. maxExtents.setMax(in_rIntersect.maxExtents);
  556. }
  557. inline void Box3I::getCenter(Point3I* center) const
  558. {
  559. center->x = (minExtents.x + maxExtents.x) >> 1;
  560. center->y = (minExtents.y + maxExtents.y) >> 1;
  561. center->z = (minExtents.z + maxExtents.z) >> 1;
  562. }
  563. inline void Box3I::extend(const Point3I & p)
  564. {
  565. #define EXTEND_AXIS(AXIS) \
  566. if (p.AXIS < minExtents.AXIS) \
  567. minExtents.AXIS = p.AXIS; \
  568. else if (p.AXIS > maxExtents.AXIS) \
  569. maxExtents.AXIS = p.AXIS;
  570. EXTEND_AXIS(x)
  571. EXTEND_AXIS(y)
  572. EXTEND_AXIS(z)
  573. #undef EXTEND_AXIS
  574. }
  575. #endif // _DBOX_H_