mBox.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. if( minExtents[ i ] > otherBox.maxExtents[ i ] || otherBox.minExtents[ i ] > maxExtents[ i ] )
  267. overlap.minExtents[ i ] = 0.f;
  268. else
  269. overlap.minExtents[ i ] = getMax( minExtents[ i ], otherBox.minExtents[ i ] );
  270. return overlap;
  271. }
  272. inline F32 Box3F::getVolume() const
  273. {
  274. return ( maxExtents.x - minExtents.x ) * ( maxExtents.y - minExtents.y ) * ( maxExtents.z - minExtents.z );
  275. }
  276. inline void Box3F::getCenter(Point3F* center) const
  277. {
  278. center->x = (minExtents.x + maxExtents.x) * 0.5f;
  279. center->y = (minExtents.y + maxExtents.y) * 0.5f;
  280. center->z = (minExtents.z + maxExtents.z) * 0.5f;
  281. }
  282. inline Point3F Box3F::getCenter() const
  283. {
  284. Point3F center;
  285. center.x = (minExtents.x + maxExtents.x) * 0.5f;
  286. center.y = (minExtents.y + maxExtents.y) * 0.5f;
  287. center.z = (minExtents.z + maxExtents.z) * 0.5f;
  288. return center;
  289. }
  290. inline Point3F Box3F::getClosestPoint(const Point3F& refPt) const
  291. {
  292. Point3F closest;
  293. if (refPt.x <= minExtents.x) closest.x = minExtents.x;
  294. else if (refPt.x > maxExtents.x) closest.x = maxExtents.x;
  295. else closest.x = refPt.x;
  296. if (refPt.y <= minExtents.y) closest.y = minExtents.y;
  297. else if (refPt.y > maxExtents.y) closest.y = maxExtents.y;
  298. else closest.y = refPt.y;
  299. if (refPt.z <= minExtents.z) closest.z = minExtents.z;
  300. else if (refPt.z > maxExtents.z) closest.z = maxExtents.z;
  301. else closest.z = refPt.z;
  302. return closest;
  303. }
  304. inline F32 Box3F::getDistanceToPoint(const Point3F& refPt) const
  305. {
  306. return mSqrt( getSqDistanceToPoint( refPt ) );
  307. }
  308. inline F32 Box3F::getSqDistanceToPoint( const Point3F &refPt ) const
  309. {
  310. F32 sqDist = 0.0f;
  311. for ( U32 i=0; i < 3; i++ )
  312. {
  313. const F32 v = refPt[i];
  314. if ( v < minExtents[i] )
  315. sqDist += mSquared( minExtents[i] - v );
  316. else if ( v > maxExtents[i] )
  317. sqDist += mSquared( v - maxExtents[i] );
  318. }
  319. return sqDist;
  320. }
  321. inline void Box3F::extend(const Point3F & p)
  322. {
  323. #define EXTEND_AXIS(AXIS) \
  324. if (p.AXIS < minExtents.AXIS) \
  325. minExtents.AXIS = p.AXIS; \
  326. else if (p.AXIS > maxExtents.AXIS) \
  327. maxExtents.AXIS = p.AXIS;
  328. EXTEND_AXIS(x)
  329. EXTEND_AXIS(y)
  330. EXTEND_AXIS(z)
  331. #undef EXTEND_AXIS
  332. }
  333. inline void Box3F::scale( const Point3F &amt )
  334. {
  335. minExtents *= amt;
  336. maxExtents *= amt;
  337. }
  338. inline void Box3F::scale( F32 amt )
  339. {
  340. minExtents *= amt;
  341. maxExtents *= amt;
  342. }
  343. inline bool Box3F::operator ==( const Box3F &b ) const
  344. {
  345. return minExtents.equal( b.minExtents ) && maxExtents.equal( b.maxExtents );
  346. }
  347. inline bool Box3F::operator !=( const Box3F &b ) const
  348. {
  349. return !minExtents.equal( b.minExtents ) || !maxExtents.equal( b.maxExtents );
  350. }
  351. //------------------------------------------------------------------------------
  352. /// Clone of Box3F, using 3D types.
  353. ///
  354. /// 3D types use F64.
  355. ///
  356. /// @see Box3F
  357. class Box3D
  358. {
  359. public:
  360. Point3D minExtents;
  361. Point3D maxExtents;
  362. public:
  363. Box3D() { }
  364. Box3D(const Point3D& in_rMin, const Point3D& in_rMax, const bool in_overrideCheck = false);
  365. bool isContained(const Point3D& in_rContained) const;
  366. bool isOverlapped(const Box3D& in_rOverlap) const;
  367. F64 len_x() const;
  368. F64 len_y() const;
  369. F64 len_z() const;
  370. void intersect(const Box3D& in_rIntersect);
  371. void getCenter(Point3D* center) const;
  372. void extend(const Point3D & p);
  373. };
  374. inline Box3D::Box3D(const Point3D& in_rMin, const Point3D& in_rMax, const bool in_overrideCheck)
  375. : minExtents(in_rMin),
  376. maxExtents(in_rMax)
  377. {
  378. if (in_overrideCheck == false) {
  379. minExtents.setMin(in_rMax);
  380. maxExtents.setMax(in_rMin);
  381. }
  382. }
  383. inline bool Box3D::isContained(const Point3D& in_rContained) const
  384. {
  385. return (in_rContained.x >= minExtents.x && in_rContained.x < maxExtents.x) &&
  386. (in_rContained.y >= minExtents.y && in_rContained.y < maxExtents.y) &&
  387. (in_rContained.z >= minExtents.z && in_rContained.z < maxExtents.z);
  388. }
  389. inline bool Box3D::isOverlapped(const Box3D& in_rOverlap) const
  390. {
  391. if (in_rOverlap.minExtents.x > maxExtents.x ||
  392. in_rOverlap.minExtents.y > maxExtents.y ||
  393. in_rOverlap.minExtents.z > maxExtents.z)
  394. return false;
  395. if (in_rOverlap.maxExtents.x < minExtents.x ||
  396. in_rOverlap.maxExtents.y < minExtents.y ||
  397. in_rOverlap.maxExtents.z < minExtents.z)
  398. return false;
  399. return true;
  400. }
  401. inline F64 Box3D::len_x() const
  402. {
  403. return maxExtents.x - minExtents.x;
  404. }
  405. inline F64 Box3D::len_y() const
  406. {
  407. return maxExtents.y - minExtents.y;
  408. }
  409. inline F64 Box3D::len_z() const
  410. {
  411. return maxExtents.z - minExtents.z;
  412. }
  413. inline void Box3D::intersect(const Box3D& in_rIntersect)
  414. {
  415. minExtents.setMin(in_rIntersect.minExtents);
  416. maxExtents.setMax(in_rIntersect.maxExtents);
  417. }
  418. inline void Box3D::getCenter(Point3D* center) const
  419. {
  420. center->x = (minExtents.x + maxExtents.x) * 0.5;
  421. center->y = (minExtents.y + maxExtents.y) * 0.5;
  422. center->z = (minExtents.z + maxExtents.z) * 0.5;
  423. }
  424. inline void Box3D::extend(const Point3D & p)
  425. {
  426. #define EXTEND_AXIS(AXIS) \
  427. if (p.AXIS < minExtents.AXIS) \
  428. minExtents.AXIS = p.AXIS; \
  429. else if (p.AXIS > maxExtents.AXIS) \
  430. maxExtents.AXIS = p.AXIS;
  431. EXTEND_AXIS(x)
  432. EXTEND_AXIS(y)
  433. EXTEND_AXIS(z)
  434. #undef EXTEND_AXIS
  435. }
  436. /// Bounding Box
  437. ///
  438. /// A helper class for working with boxes. It runs at F32 precision.
  439. ///
  440. /// @see Box3D
  441. class Box3I
  442. {
  443. public:
  444. Point3I minExtents; ///< Minimum extents of box
  445. Point3I maxExtents; ///< Maximum extents of box
  446. public:
  447. Box3I() { }
  448. /// Create a box from two points.
  449. ///
  450. /// Normally, this function will compensate for mismatched
  451. /// min/max values. If you know your values are valid, you
  452. /// can set in_overrideCheck to true and skip this.
  453. ///
  454. /// @param in_rMin Minimum extents of box.
  455. /// @param in_rMax Maximum extents of box.
  456. /// @param in_overrideCheck Pass true to skip check of extents.
  457. Box3I(const Point3I& in_rMin, const Point3I& in_rMax, const bool in_overrideCheck = false);
  458. /// Create a box from six extent values.
  459. ///
  460. /// No checking is performed as to the validity of these
  461. /// extents, unlike the other constructor.
  462. Box3I(S32 xmin, S32 ymin, S32 zmin, S32 max, S32 ymax, S32 zmax);
  463. /// Check to see if a point is contained in this box.
  464. bool isContained(const Point3I& in_rContained) const;
  465. /// Check to see if another box overlaps this box.
  466. bool isOverlapped(const Box3I& in_rOverlap) const;
  467. /// Check to see if another box is contained in this box.
  468. bool isContained(const Box3I& in_rContained) const;
  469. S32 len_x() const;
  470. S32 len_y() const;
  471. S32 len_z() const;
  472. /// Perform an intersection operation with another box
  473. /// and store the results in this box.
  474. void intersect(const Box3I& in_rIntersect);
  475. /// Get the center of this box.
  476. ///
  477. /// This is the average of min and max.
  478. void getCenter(Point3I* center) const;
  479. /// Check that the box is valid.
  480. ///
  481. /// Currently, this just means that min < max.
  482. bool isValidBox() const
  483. {
  484. return (minExtents.x <= maxExtents.x) &&
  485. (minExtents.y <= maxExtents.y) &&
  486. (minExtents.z <= maxExtents.z);
  487. }
  488. void extend(const Point3I & p);
  489. };
  490. inline Box3I::Box3I(const Point3I& in_rMin, const Point3I& in_rMax, const bool in_overrideCheck)
  491. : minExtents(in_rMin),
  492. maxExtents(in_rMax)
  493. {
  494. if (in_overrideCheck == false)
  495. {
  496. minExtents.setMin(in_rMax);
  497. maxExtents.setMax(in_rMin);
  498. }
  499. }
  500. inline Box3I::Box3I(S32 xMin, S32 yMin, S32 zMin, S32 xMax, S32 yMax, S32 zMax)
  501. : minExtents(xMin,yMin,zMin),
  502. maxExtents(xMax,yMax,zMax)
  503. {
  504. }
  505. inline bool Box3I::isContained(const Point3I& in_rContained) const
  506. {
  507. return (in_rContained.x >= minExtents.x && in_rContained.x < maxExtents.x) &&
  508. (in_rContained.y >= minExtents.y && in_rContained.y < maxExtents.y) &&
  509. (in_rContained.z >= minExtents.z && in_rContained.z < maxExtents.z);
  510. }
  511. inline bool Box3I::isOverlapped(const Box3I& in_rOverlap) const
  512. {
  513. if (in_rOverlap.minExtents.x > maxExtents.x ||
  514. in_rOverlap.minExtents.y > maxExtents.y ||
  515. in_rOverlap.minExtents.z > maxExtents.z)
  516. return false;
  517. if (in_rOverlap.maxExtents.x < minExtents.x ||
  518. in_rOverlap.maxExtents.y < minExtents.y ||
  519. in_rOverlap.maxExtents.z < minExtents.z)
  520. return false;
  521. return true;
  522. }
  523. inline bool Box3I::isContained(const Box3I& in_rContained) const
  524. {
  525. return (minExtents.x <= in_rContained.minExtents.x) &&
  526. (minExtents.y <= in_rContained.minExtents.y) &&
  527. (minExtents.z <= in_rContained.minExtents.z) &&
  528. (maxExtents.x >= in_rContained.maxExtents.x) &&
  529. (maxExtents.y >= in_rContained.maxExtents.y) &&
  530. (maxExtents.z >= in_rContained.maxExtents.z);
  531. }
  532. inline S32 Box3I::len_x() const
  533. {
  534. return maxExtents.x - minExtents.x;
  535. }
  536. inline S32 Box3I::len_y() const
  537. {
  538. return maxExtents.y - minExtents.y;
  539. }
  540. inline S32 Box3I::len_z() const
  541. {
  542. return maxExtents.z - minExtents.z;
  543. }
  544. inline void Box3I::intersect(const Box3I& in_rIntersect)
  545. {
  546. minExtents.setMin(in_rIntersect.minExtents);
  547. maxExtents.setMax(in_rIntersect.maxExtents);
  548. }
  549. inline void Box3I::getCenter(Point3I* center) const
  550. {
  551. center->x = (minExtents.x + maxExtents.x) >> 1;
  552. center->y = (minExtents.y + maxExtents.y) >> 1;
  553. center->z = (minExtents.z + maxExtents.z) >> 1;
  554. }
  555. inline void Box3I::extend(const Point3I & p)
  556. {
  557. #define EXTEND_AXIS(AXIS) \
  558. if (p.AXIS < minExtents.AXIS) \
  559. minExtents.AXIS = p.AXIS; \
  560. else if (p.AXIS > maxExtents.AXIS) \
  561. maxExtents.AXIS = p.AXIS;
  562. EXTEND_AXIS(x)
  563. EXTEND_AXIS(y)
  564. EXTEND_AXIS(z)
  565. #undef EXTEND_AXIS
  566. }
  567. #endif // _DBOX_H_