BsAABox.h 3.5 KB

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