mOrientedBox.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 _MORIENTEDBOX_H_
  23. #define _MORIENTEDBOX_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 USE_TEMPLATE_MATRIX
  31. class MatrixF;
  32. #else
  33. template<typename DATA_TYPE, U32 rows, U32 cols> class Matrix;
  34. typedef Matrix<F32, 4, 4> MatrixF;
  35. #endif
  36. class Box3F;
  37. /// An oriented bounding box (OBB) described by a center point, three normalizes axis
  38. /// vectors, and half-extents along each of the axes.
  39. class OrientedBox3F : public BoxBase
  40. {
  41. public:
  42. enum Axis
  43. {
  44. RightVector,
  45. ForwardVector,
  46. UpVector
  47. };
  48. protected:
  49. /// Center point.
  50. Point3F mCenter;
  51. /// Normalized axis vectors.
  52. Point3F mAxes[ 3 ];
  53. /// Box half-extents along each axis.
  54. Point3F mHalfExtents;
  55. /// Corner points.
  56. Point3F mPoints[ NUM_POINTS ];
  57. void _initPoints();
  58. public:
  59. OrientedBox3F() {}
  60. OrientedBox3F( const MatrixF& transform, const Point3F& extents ) { set( transform, extents ); }
  61. OrientedBox3F( const MatrixF& transform, const Box3F& aabb ) { set( transform, aabb ); }
  62. /// Return the center point of the bounding box.
  63. const Point3F& getCenter() const { return mCenter; }
  64. /// Return the normalized axis vector for the given world-space axis.
  65. const Point3F& getAxis( U32 i ) const
  66. {
  67. AssertFatal( i < 3, "OrientedBox3F::getAxis - Index out of range" );
  68. return mAxes[ i ];
  69. }
  70. /// Return the half-extents along each axis.
  71. ///
  72. /// Since the OBBs are symmetrical across each axis, we store half-extents
  73. /// instead of full extents as usually half-extents are needed in the computations.
  74. const Point3F& getHalfExtents() const { return mHalfExtents; }
  75. /// Return true if the given point is contained in the OBB.
  76. bool isContained( const Point3F& point ) const;
  77. /// Return the corner points of the box.
  78. const Point3F* getPoints() const { return mPoints; }
  79. /// Return the array of corner points for the box.
  80. operator const Point3F*() const { return getPoints(); }
  81. /// Compute the OBB values from the given transform and extents.
  82. ///
  83. /// @param transform World->object space transform.
  84. /// @param extents Box extent on each axis.
  85. void set( const MatrixF& transform, const Point3F& extents );
  86. /// Compute the OBB from an AABB in the given transform space.
  87. ///
  88. /// @param transform Transform space for the AABB.
  89. /// @param aabb An axis-aligned bounding box in the given transform space.
  90. void set( const MatrixF& transform, const Box3F& aabb );
  91. };
  92. #endif // !_MORIENTEDBOX_H_