CmAxisAlignedBox.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __AxisAlignedBox_H_
  25. #define __AxisAlignedBox_H_
  26. // Precompiler options
  27. #include "CmPrerequisitesUtil.h"
  28. #include "CmVector3.h"
  29. #include "CmMatrix4.h"
  30. namespace CamelotFramework {
  31. /** \addtogroup Core
  32. * @{
  33. */
  34. /** \addtogroup Math
  35. * @{
  36. */
  37. /** A 3D box aligned with the x/y/z axes.
  38. @remarks
  39. This class represents a simple box which is aligned with the
  40. axes. Internally it only stores 2 points as the extremeties of
  41. the box, one which is the minima of all 3 axes, and the other
  42. which is the maxima of all 3 axes. This class is typically used
  43. for an axis-aligned bounding box (AABB) for collision and
  44. visibility determination.
  45. */
  46. class CM_UTILITY_EXPORT AxisAlignedBox
  47. {
  48. public:
  49. enum Extent
  50. {
  51. EXTENT_NULL,
  52. EXTENT_FINITE,
  53. EXTENT_INFINITE
  54. };
  55. protected:
  56. Vector3 mMinimum;
  57. Vector3 mMaximum;
  58. Extent mExtent;
  59. mutable Vector3* mpCorners;
  60. public:
  61. /*
  62. 1-----2
  63. /| /|
  64. / | / |
  65. 5-----4 |
  66. | 0--|--3
  67. | / | /
  68. |/ |/
  69. 6-----7
  70. */
  71. typedef enum {
  72. FAR_LEFT_BOTTOM = 0,
  73. FAR_LEFT_TOP = 1,
  74. FAR_RIGHT_TOP = 2,
  75. FAR_RIGHT_BOTTOM = 3,
  76. NEAR_RIGHT_BOTTOM = 7,
  77. NEAR_LEFT_BOTTOM = 6,
  78. NEAR_LEFT_TOP = 5,
  79. NEAR_RIGHT_TOP = 4
  80. } CornerEnum;
  81. inline AxisAlignedBox() : mMinimum(Vector3::ZERO), mMaximum(Vector3::UNIT_SCALE), mpCorners(0)
  82. {
  83. // Default to a null box
  84. setMinimum( -0.5, -0.5, -0.5 );
  85. setMaximum( 0.5, 0.5, 0.5 );
  86. mExtent = EXTENT_NULL;
  87. }
  88. inline AxisAlignedBox(Extent e) : mMinimum(Vector3::ZERO), mMaximum(Vector3::UNIT_SCALE), mpCorners(0)
  89. {
  90. setMinimum( -0.5, -0.5, -0.5 );
  91. setMaximum( 0.5, 0.5, 0.5 );
  92. mExtent = e;
  93. }
  94. inline AxisAlignedBox(const AxisAlignedBox & rkBox) : mMinimum(Vector3::ZERO), mMaximum(Vector3::UNIT_SCALE), mpCorners(0)
  95. {
  96. if (rkBox.isNull())
  97. setNull();
  98. else if (rkBox.isInfinite())
  99. setInfinite();
  100. else
  101. setExtents( rkBox.mMinimum, rkBox.mMaximum );
  102. }
  103. inline AxisAlignedBox( const Vector3& min, const Vector3& max ) : mMinimum(Vector3::ZERO), mMaximum(Vector3::UNIT_SCALE), mpCorners(0)
  104. {
  105. setExtents( min, max );
  106. }
  107. inline AxisAlignedBox(
  108. float mx, float my, float mz,
  109. float Mx, float My, float Mz ) : mMinimum(Vector3::ZERO), mMaximum(Vector3::UNIT_SCALE), mpCorners(0)
  110. {
  111. setExtents( mx, my, mz, Mx, My, Mz );
  112. }
  113. AxisAlignedBox& operator=(const AxisAlignedBox& rhs)
  114. {
  115. // Specifically override to avoid copying mpCorners
  116. if (rhs.isNull())
  117. setNull();
  118. else if (rhs.isInfinite())
  119. setInfinite();
  120. else
  121. setExtents(rhs.mMinimum, rhs.mMaximum);
  122. return *this;
  123. }
  124. ~AxisAlignedBox()
  125. {
  126. if (mpCorners)
  127. free(mpCorners);
  128. }
  129. /** Gets the minimum corner of the box.
  130. */
  131. inline const Vector3& getMinimum(void) const
  132. {
  133. return mMinimum;
  134. }
  135. /** Gets a modifiable version of the minimum
  136. corner of the box.
  137. */
  138. inline Vector3& getMinimum(void)
  139. {
  140. return mMinimum;
  141. }
  142. /** Gets the maximum corner of the box.
  143. */
  144. inline const Vector3& getMaximum(void) const
  145. {
  146. return mMaximum;
  147. }
  148. /** Gets a modifiable version of the maximum
  149. corner of the box.
  150. */
  151. inline Vector3& getMaximum(void)
  152. {
  153. return mMaximum;
  154. }
  155. /** Sets the minimum corner of the box.
  156. */
  157. inline void setMinimum( const Vector3& vec )
  158. {
  159. mExtent = EXTENT_FINITE;
  160. mMinimum = vec;
  161. }
  162. inline void setMinimum( float x, float y, float z )
  163. {
  164. mExtent = EXTENT_FINITE;
  165. mMinimum.x = x;
  166. mMinimum.y = y;
  167. mMinimum.z = z;
  168. }
  169. /** Changes one of the components of the minimum corner of the box
  170. used to resize only one dimension of the box
  171. */
  172. inline void setMinimumX(float x)
  173. {
  174. mMinimum.x = x;
  175. }
  176. inline void setMinimumY(float y)
  177. {
  178. mMinimum.y = y;
  179. }
  180. inline void setMinimumZ(float z)
  181. {
  182. mMinimum.z = z;
  183. }
  184. /** Sets the maximum corner of the box.
  185. */
  186. inline void setMaximum( const Vector3& vec )
  187. {
  188. mExtent = EXTENT_FINITE;
  189. mMaximum = vec;
  190. }
  191. inline void setMaximum( float x, float y, float z )
  192. {
  193. mExtent = EXTENT_FINITE;
  194. mMaximum.x = x;
  195. mMaximum.y = y;
  196. mMaximum.z = z;
  197. }
  198. /** Changes one of the components of the maximum corner of the box
  199. used to resize only one dimension of the box
  200. */
  201. inline void setMaximumX( float x )
  202. {
  203. mMaximum.x = x;
  204. }
  205. inline void setMaximumY( float y )
  206. {
  207. mMaximum.y = y;
  208. }
  209. inline void setMaximumZ( float z )
  210. {
  211. mMaximum.z = z;
  212. }
  213. /** Sets both minimum and maximum extents at once.
  214. */
  215. inline void setExtents( const Vector3& min, const Vector3& max )
  216. {
  217. assert( (min.x <= max.x && min.y <= max.y && min.z <= max.z) &&
  218. "The minimum corner of the box must be less than or equal to maximum corner" );
  219. mExtent = EXTENT_FINITE;
  220. mMinimum = min;
  221. mMaximum = max;
  222. }
  223. inline void setExtents(
  224. float mx, float my, float mz,
  225. float Mx, float My, float Mz )
  226. {
  227. assert( (mx <= Mx && my <= My && mz <= Mz) &&
  228. "The minimum corner of the box must be less than or equal to maximum corner" );
  229. mExtent = EXTENT_FINITE;
  230. mMinimum.x = mx;
  231. mMinimum.y = my;
  232. mMinimum.z = mz;
  233. mMaximum.x = Mx;
  234. mMaximum.y = My;
  235. mMaximum.z = Mz;
  236. }
  237. /** Returns a pointer to an array of 8 corner points, useful for
  238. collision vs. non-aligned objects.
  239. @remarks
  240. If the order of these corners is important, they are as
  241. follows: The 4 points of the minimum Z face (note that
  242. because Engine uses right-handed coordinates, the minimum Z is
  243. at the 'back' of the box) starting with the minimum point of
  244. all, then anticlockwise around this face (if you are looking
  245. onto the face from outside the box). Then the 4 points of the
  246. maximum Z face, starting with maximum point of all, then
  247. anticlockwise around this face (looking onto the face from
  248. outside the box). Like this:
  249. <pre>
  250. 1-----2
  251. /| /|
  252. / | / |
  253. 5-----4 |
  254. | 0--|--3
  255. | / | /
  256. |/ |/
  257. 6-----7
  258. </pre>
  259. @remarks as this implementation uses a static member, make sure to use your own copy !
  260. */
  261. inline const Vector3* getAllCorners(void) const
  262. {
  263. assert( (mExtent == EXTENT_FINITE) && "Can't get corners of a null or infinite AAB" );
  264. // The order of these items is, using right-handed co-ordinates:
  265. // Minimum Z face, starting with Min(all), then anticlockwise
  266. // around face (looking onto the face)
  267. // Maximum Z face, starting with Max(all), then anticlockwise
  268. // around face (looking onto the face)
  269. // Only for optimization/compatibility.
  270. if (!mpCorners)
  271. mpCorners = (Vector3*)malloc(sizeof(Vector3) * 8);
  272. mpCorners[0] = mMinimum;
  273. mpCorners[1].x = mMinimum.x; mpCorners[1].y = mMaximum.y; mpCorners[1].z = mMinimum.z;
  274. mpCorners[2].x = mMaximum.x; mpCorners[2].y = mMaximum.y; mpCorners[2].z = mMinimum.z;
  275. mpCorners[3].x = mMaximum.x; mpCorners[3].y = mMinimum.y; mpCorners[3].z = mMinimum.z;
  276. mpCorners[4] = mMaximum;
  277. mpCorners[5].x = mMinimum.x; mpCorners[5].y = mMaximum.y; mpCorners[5].z = mMaximum.z;
  278. mpCorners[6].x = mMinimum.x; mpCorners[6].y = mMinimum.y; mpCorners[6].z = mMaximum.z;
  279. mpCorners[7].x = mMaximum.x; mpCorners[7].y = mMinimum.y; mpCorners[7].z = mMaximum.z;
  280. return mpCorners;
  281. }
  282. /** gets the position of one of the corners
  283. */
  284. Vector3 getCorner(CornerEnum cornerToGet) const
  285. {
  286. switch(cornerToGet)
  287. {
  288. case FAR_LEFT_BOTTOM:
  289. return mMinimum;
  290. case FAR_LEFT_TOP:
  291. return Vector3(mMinimum.x, mMaximum.y, mMinimum.z);
  292. case FAR_RIGHT_TOP:
  293. return Vector3(mMaximum.x, mMaximum.y, mMinimum.z);
  294. case FAR_RIGHT_BOTTOM:
  295. return Vector3(mMaximum.x, mMinimum.y, mMinimum.z);
  296. case NEAR_RIGHT_BOTTOM:
  297. return Vector3(mMaximum.x, mMinimum.y, mMaximum.z);
  298. case NEAR_LEFT_BOTTOM:
  299. return Vector3(mMinimum.x, mMinimum.y, mMaximum.z);
  300. case NEAR_LEFT_TOP:
  301. return Vector3(mMinimum.x, mMaximum.y, mMaximum.z);
  302. case NEAR_RIGHT_TOP:
  303. return mMaximum;
  304. default:
  305. return Vector3();
  306. }
  307. }
  308. CM_UTILITY_EXPORT friend std::ostream& operator<<( std::ostream& o, const AxisAlignedBox aab )
  309. {
  310. switch (aab.mExtent)
  311. {
  312. case EXTENT_NULL:
  313. o << "AxisAlignedBox(null)";
  314. return o;
  315. case EXTENT_FINITE:
  316. o << "AxisAlignedBox(min=" << aab.mMinimum << ", max=" << aab.mMaximum << ")";
  317. return o;
  318. case EXTENT_INFINITE:
  319. o << "AxisAlignedBox(infinite)";
  320. return o;
  321. default: // shut up compiler
  322. assert( false && "Never reached" );
  323. return o;
  324. }
  325. }
  326. /** Merges the passed in box into the current box. The result is the
  327. box which encompasses both.
  328. */
  329. void merge( const AxisAlignedBox& rhs )
  330. {
  331. // Do nothing if rhs null, or this is infinite
  332. if ((rhs.mExtent == EXTENT_NULL) || (mExtent == EXTENT_INFINITE))
  333. {
  334. return;
  335. }
  336. // Otherwise if rhs is infinite, make this infinite, too
  337. else if (rhs.mExtent == EXTENT_INFINITE)
  338. {
  339. mExtent = EXTENT_INFINITE;
  340. }
  341. // Otherwise if current null, just take rhs
  342. else if (mExtent == EXTENT_NULL)
  343. {
  344. setExtents(rhs.mMinimum, rhs.mMaximum);
  345. }
  346. // Otherwise merge
  347. else
  348. {
  349. Vector3 min = mMinimum;
  350. Vector3 max = mMaximum;
  351. max.makeCeil(rhs.mMaximum);
  352. min.makeFloor(rhs.mMinimum);
  353. setExtents(min, max);
  354. }
  355. }
  356. /** Extends the box to encompass the specified point (if needed).
  357. */
  358. inline void merge( const Vector3& point )
  359. {
  360. switch (mExtent)
  361. {
  362. case EXTENT_NULL: // if null, use this point
  363. setExtents(point, point);
  364. return;
  365. case EXTENT_FINITE:
  366. mMaximum.makeCeil(point);
  367. mMinimum.makeFloor(point);
  368. return;
  369. case EXTENT_INFINITE: // if infinite, makes no difference
  370. return;
  371. }
  372. assert( false && "Never reached" );
  373. }
  374. /** Transforms the box according to the matrix supplied.
  375. @remarks
  376. By calling this method you get the axis-aligned box which
  377. surrounds the transformed version of this box. Therefore each
  378. corner of the box is transformed by the matrix, then the
  379. extents are mapped back onto the axes to produce another
  380. AABB. Useful when you have a local AABB for an object which
  381. is then transformed.
  382. */
  383. inline void transform( const Matrix4& matrix )
  384. {
  385. // Do nothing if current null or infinite
  386. if( mExtent != EXTENT_FINITE )
  387. return;
  388. Vector3 oldMin, oldMax, currentCorner;
  389. // Getting the old values so that we can use the existing merge method.
  390. oldMin = mMinimum;
  391. oldMax = mMaximum;
  392. // reset
  393. setNull();
  394. // We sequentially compute the corners in the following order :
  395. // 0, 6, 5, 1, 2, 4 ,7 , 3
  396. // This sequence allows us to only change one member at a time to get at all corners.
  397. // For each one, we transform it using the matrix
  398. // Which gives the resulting point and merge the resulting point.
  399. // First corner
  400. // min min min
  401. currentCorner = oldMin;
  402. merge( matrix * currentCorner );
  403. // min,min,max
  404. currentCorner.z = oldMax.z;
  405. merge( matrix * currentCorner );
  406. // min max max
  407. currentCorner.y = oldMax.y;
  408. merge( matrix * currentCorner );
  409. // min max min
  410. currentCorner.z = oldMin.z;
  411. merge( matrix * currentCorner );
  412. // max max min
  413. currentCorner.x = oldMax.x;
  414. merge( matrix * currentCorner );
  415. // max max max
  416. currentCorner.z = oldMax.z;
  417. merge( matrix * currentCorner );
  418. // max min max
  419. currentCorner.y = oldMin.y;
  420. merge( matrix * currentCorner );
  421. // max min min
  422. currentCorner.z = oldMin.z;
  423. merge( matrix * currentCorner );
  424. }
  425. /** Transforms the box according to the affine matrix supplied.
  426. @remarks
  427. By calling this method you get the axis-aligned box which
  428. surrounds the transformed version of this box. Therefore each
  429. corner of the box is transformed by the matrix, then the
  430. extents are mapped back onto the axes to produce another
  431. AABB. Useful when you have a local AABB for an object which
  432. is then transformed.
  433. @note
  434. The matrix must be an affine matrix. @see Matrix4::isAffine.
  435. */
  436. void transformAffine(const Matrix4& m)
  437. {
  438. assert(m.isAffine());
  439. // Do nothing if current null or infinite
  440. if ( mExtent != EXTENT_FINITE )
  441. return;
  442. Vector3 centre = getCenter();
  443. Vector3 halfSize = getHalfSize();
  444. Vector3 newCentre = m.transformAffine(centre);
  445. Vector3 newHalfSize(
  446. Math::Abs(m[0][0]) * halfSize.x + Math::Abs(m[0][1]) * halfSize.y + Math::Abs(m[0][2]) * halfSize.z,
  447. Math::Abs(m[1][0]) * halfSize.x + Math::Abs(m[1][1]) * halfSize.y + Math::Abs(m[1][2]) * halfSize.z,
  448. Math::Abs(m[2][0]) * halfSize.x + Math::Abs(m[2][1]) * halfSize.y + Math::Abs(m[2][2]) * halfSize.z);
  449. setExtents(newCentre - newHalfSize, newCentre + newHalfSize);
  450. }
  451. /** Sets the box to a 'null' value i.e. not a box.
  452. */
  453. inline void setNull()
  454. {
  455. mExtent = EXTENT_NULL;
  456. }
  457. /** Returns true if the box is null i.e. empty.
  458. */
  459. inline bool isNull(void) const
  460. {
  461. return (mExtent == EXTENT_NULL);
  462. }
  463. /** Returns true if the box is finite.
  464. */
  465. bool isFinite(void) const
  466. {
  467. return (mExtent == EXTENT_FINITE);
  468. }
  469. /** Sets the box to 'infinite'
  470. */
  471. inline void setInfinite()
  472. {
  473. mExtent = EXTENT_INFINITE;
  474. }
  475. /** Returns true if the box is infinite.
  476. */
  477. bool isInfinite(void) const
  478. {
  479. return (mExtent == EXTENT_INFINITE);
  480. }
  481. /** Returns whether or not this box intersects another. */
  482. inline bool intersects(const AxisAlignedBox& b2) const
  483. {
  484. // Early-fail for nulls
  485. if (this->isNull() || b2.isNull())
  486. return false;
  487. // Early-success for infinites
  488. if (this->isInfinite() || b2.isInfinite())
  489. return true;
  490. // Use up to 6 separating planes
  491. if (mMaximum.x < b2.mMinimum.x)
  492. return false;
  493. if (mMaximum.y < b2.mMinimum.y)
  494. return false;
  495. if (mMaximum.z < b2.mMinimum.z)
  496. return false;
  497. if (mMinimum.x > b2.mMaximum.x)
  498. return false;
  499. if (mMinimum.y > b2.mMaximum.y)
  500. return false;
  501. if (mMinimum.z > b2.mMaximum.z)
  502. return false;
  503. // otherwise, must be intersecting
  504. return true;
  505. }
  506. /// Calculate the area of intersection of this box and another
  507. inline AxisAlignedBox intersection(const AxisAlignedBox& b2) const
  508. {
  509. if (this->isNull() || b2.isNull())
  510. {
  511. return AxisAlignedBox();
  512. }
  513. else if (this->isInfinite())
  514. {
  515. return b2;
  516. }
  517. else if (b2.isInfinite())
  518. {
  519. return *this;
  520. }
  521. Vector3 intMin = mMinimum;
  522. Vector3 intMax = mMaximum;
  523. intMin.makeCeil(b2.getMinimum());
  524. intMax.makeFloor(b2.getMaximum());
  525. // Check intersection isn't null
  526. if (intMin.x < intMax.x &&
  527. intMin.y < intMax.y &&
  528. intMin.z < intMax.z)
  529. {
  530. return AxisAlignedBox(intMin, intMax);
  531. }
  532. return AxisAlignedBox();
  533. }
  534. /// Calculate the volume of this box
  535. float volume(void) const
  536. {
  537. switch (mExtent)
  538. {
  539. case EXTENT_NULL:
  540. return 0.0f;
  541. case EXTENT_FINITE:
  542. {
  543. Vector3 diff = mMaximum - mMinimum;
  544. return diff.x * diff.y * diff.z;
  545. }
  546. case EXTENT_INFINITE:
  547. return Math::POS_INFINITY;
  548. default: // shut up compiler
  549. assert( false && "Never reached" );
  550. return 0.0f;
  551. }
  552. }
  553. /** Scales the AABB by the vector given. */
  554. inline void scale(const Vector3& s)
  555. {
  556. // Do nothing if current null or infinite
  557. if (mExtent != EXTENT_FINITE)
  558. return;
  559. // NB assumes centered on origin
  560. Vector3 min = mMinimum * s;
  561. Vector3 max = mMaximum * s;
  562. setExtents(min, max);
  563. }
  564. /** Tests whether this box intersects a sphere. */
  565. bool intersects(const Sphere& s) const
  566. {
  567. return Math::intersects(s, *this);
  568. }
  569. /** Tests whether this box intersects a plane. */
  570. bool intersects(const Plane& p) const
  571. {
  572. return Math::intersects(p, *this);
  573. }
  574. /** Tests whether the vector point is within this box. */
  575. bool intersects(const Vector3& v) const
  576. {
  577. switch (mExtent)
  578. {
  579. case EXTENT_NULL:
  580. return false;
  581. case EXTENT_FINITE:
  582. return(v.x >= mMinimum.x && v.x <= mMaximum.x &&
  583. v.y >= mMinimum.y && v.y <= mMaximum.y &&
  584. v.z >= mMinimum.z && v.z <= mMaximum.z);
  585. case EXTENT_INFINITE:
  586. return true;
  587. default: // shut up compiler
  588. assert( false && "Never reached" );
  589. return false;
  590. }
  591. }
  592. /// Gets the centre of the box
  593. Vector3 getCenter(void) const
  594. {
  595. assert( (mExtent == EXTENT_FINITE) && "Can't get center of a null or infinite AAB" );
  596. return Vector3(
  597. (mMaximum.x + mMinimum.x) * 0.5f,
  598. (mMaximum.y + mMinimum.y) * 0.5f,
  599. (mMaximum.z + mMinimum.z) * 0.5f);
  600. }
  601. /// Gets the size of the box
  602. Vector3 getSize(void) const
  603. {
  604. switch (mExtent)
  605. {
  606. case EXTENT_NULL:
  607. return Vector3::ZERO;
  608. case EXTENT_FINITE:
  609. return mMaximum - mMinimum;
  610. case EXTENT_INFINITE:
  611. return Vector3(
  612. Math::POS_INFINITY,
  613. Math::POS_INFINITY,
  614. Math::POS_INFINITY);
  615. default: // shut up compiler
  616. assert( false && "Never reached" );
  617. return Vector3::ZERO;
  618. }
  619. }
  620. /// Gets the half-size of the box
  621. Vector3 getHalfSize(void) const
  622. {
  623. switch (mExtent)
  624. {
  625. case EXTENT_NULL:
  626. return Vector3::ZERO;
  627. case EXTENT_FINITE:
  628. return (mMaximum - mMinimum) * 0.5;
  629. case EXTENT_INFINITE:
  630. return Vector3(
  631. Math::POS_INFINITY,
  632. Math::POS_INFINITY,
  633. Math::POS_INFINITY);
  634. default: // shut up compiler
  635. assert( false && "Never reached" );
  636. return Vector3::ZERO;
  637. }
  638. }
  639. /** Tests whether the given point contained by this box.
  640. */
  641. bool contains(const Vector3& v) const
  642. {
  643. if (isNull())
  644. return false;
  645. if (isInfinite())
  646. return true;
  647. return mMinimum.x <= v.x && v.x <= mMaximum.x &&
  648. mMinimum.y <= v.y && v.y <= mMaximum.y &&
  649. mMinimum.z <= v.z && v.z <= mMaximum.z;
  650. }
  651. /** Tests whether another box contained by this box.
  652. */
  653. bool contains(const AxisAlignedBox& other) const
  654. {
  655. if (other.isNull() || this->isInfinite())
  656. return true;
  657. if (this->isNull() || other.isInfinite())
  658. return false;
  659. return this->mMinimum.x <= other.mMinimum.x &&
  660. this->mMinimum.y <= other.mMinimum.y &&
  661. this->mMinimum.z <= other.mMinimum.z &&
  662. other.mMaximum.x <= this->mMaximum.x &&
  663. other.mMaximum.y <= this->mMaximum.y &&
  664. other.mMaximum.z <= this->mMaximum.z;
  665. }
  666. /** Tests 2 boxes for equality.
  667. */
  668. bool operator== (const AxisAlignedBox& rhs) const
  669. {
  670. if (this->mExtent != rhs.mExtent)
  671. return false;
  672. if (!this->isFinite())
  673. return true;
  674. return this->mMinimum == rhs.mMinimum &&
  675. this->mMaximum == rhs.mMaximum;
  676. }
  677. /** Tests 2 boxes for inequality.
  678. */
  679. bool operator!= (const AxisAlignedBox& rhs) const
  680. {
  681. return !(*this == rhs);
  682. }
  683. // special values
  684. static const AxisAlignedBox BOX_NULL;
  685. static const AxisAlignedBox BOX_INFINITE;
  686. };
  687. /** @} */
  688. /** @} */
  689. } // namespace CamelotFramework
  690. #endif