BsAABox.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. #include "BsMatrix4.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Math
  8. * @{
  9. */
  10. /** Axis aligned box represented by minimum and maximum point. */
  11. class BS_UTILITY_EXPORT AABox
  12. {
  13. public:
  14. /** Different corners of a box. */
  15. /*
  16. 1-----2
  17. /| /|
  18. / | / |
  19. 5-----4 |
  20. | 0--|--3
  21. | / | /
  22. |/ |/
  23. 6-----7
  24. */
  25. enum CornerEnum
  26. {
  27. FAR_LEFT_BOTTOM = 0,
  28. FAR_LEFT_TOP = 1,
  29. FAR_RIGHT_TOP = 2,
  30. FAR_RIGHT_BOTTOM = 3,
  31. NEAR_RIGHT_BOTTOM = 7,
  32. NEAR_LEFT_BOTTOM = 6,
  33. NEAR_LEFT_TOP = 5,
  34. NEAR_RIGHT_TOP = 4
  35. };
  36. AABox();
  37. AABox(const AABox& copy);
  38. AABox(const Vector3& min, const Vector3& max);
  39. AABox& operator=(const AABox& rhs);
  40. ~AABox() { }
  41. /** Gets the corner of the box with minimum values (opposite to maximum corner). */
  42. const Vector3& getMin() const { return mMinimum; }
  43. /** Gets the corner of the box with maximum values (opposite to minimum corner). */
  44. const Vector3& getMax() const { return mMaximum; }
  45. /** Sets the corner of the box with minimum values (opposite to maximum corner). */
  46. void setMin(const Vector3& vec) { mMinimum = vec; }
  47. /** Sets the corner of the box with maximum values (opposite to minimum corner). */
  48. void setMax(const Vector3& vec) { mMaximum = vec; }
  49. /** Sets the minimum and maximum corners. */
  50. void setExtents(const Vector3& min, const Vector3& max);
  51. /** Scales the box around the center by multiplying its extents with the provided scale. */
  52. void scale(const Vector3& s);
  53. /** Returns the coordinates of a specific corner. */
  54. Vector3 getCorner(CornerEnum cornerToGet) const;
  55. /** Merges the two boxes, creating a new bounding box that encapsulates them both. */
  56. void merge(const AABox& rhs);
  57. /** Expands the bounding box so it includes the provided point. */
  58. void merge(const Vector3& point);
  59. /**
  60. * Transforms the bounding box by the given matrix.
  61. *
  62. * @note
  63. * As the resulting box will no longer be axis aligned, an axis align box
  64. * is instead created by encompassing the transformed oriented bounding box.
  65. * Retrieving the value as an actual OBB would provide a tighter fit.
  66. */
  67. void transform(const Matrix4& matrix);
  68. /**
  69. * Transforms the bounding box by the given matrix.
  70. *
  71. * @note
  72. * As the resulting box will no longer be axis aligned, an axis align box
  73. * is instead created by encompassing the transformed oriented bounding box.
  74. * Retrieving the value as an actual OBB would provide a tighter fit.
  75. *
  76. * @note
  77. * Provided matrix must be affine.
  78. */
  79. void transformAffine(const Matrix4& matrix);
  80. /** Returns true if this and the provided box intersect. */
  81. bool intersects(const AABox& b2) const;
  82. /** Returns true if the sphere intersects the bounding box. */
  83. bool intersects(const Sphere& s) const;
  84. /** Returns true if the plane intersects the bounding box. */
  85. bool intersects(const Plane& p) const;
  86. /** Ray / box intersection, returns a boolean result and nearest distance to intersection. */
  87. std::pair<bool, float> intersects(const Ray& ray) const;
  88. /** Ray / box intersection, returns boolean result and near and far intersection distance. */
  89. bool intersects(const Ray& ray, float& d1, float& d2) const;
  90. /** Center of the box. */
  91. Vector3 getCenter() const;
  92. /** Size of the box (difference between minimum and maximum corners) */
  93. Vector3 getSize() const;
  94. /** Extents of the box (distance from center to one of the corners) */
  95. Vector3 getHalfSize() const;
  96. /** Radius of a sphere that fully encompasses the box. */
  97. float getRadius() const;
  98. /** Size of the volume in the box. */
  99. float getVolume() const;
  100. /** Returns true if the provided point is inside the bounding box. */
  101. bool contains(const Vector3& v) const;
  102. /** Returns true if the provided bounding box is completely inside the bounding box. */
  103. bool contains(const AABox& other) const;
  104. bool operator== (const AABox& rhs) const;
  105. bool operator!= (const AABox& rhs) const;
  106. static const AABox BOX_EMPTY;
  107. protected:
  108. Vector3 mMinimum;
  109. Vector3 mMaximum;
  110. };
  111. /** @} */
  112. /** @cond SPECIALIZATIONS */
  113. BS_ALLOW_MEMCPY_SERIALIZATION(AABox);
  114. /** @endcond */
  115. }