2
0

mOrientedBox.h 3.8 KB

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