BsAABox.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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. /**
  39. * @brief Gets the corner of the box with minimum values (opposite to maximum corner).
  40. */
  41. const Vector3& getMin() const { return mMinimum; }
  42. /**
  43. * @brief Gets the corner of the box with maximum values (opposite to minimum corner).
  44. */
  45. const Vector3& getMax() const { return mMaximum; }
  46. /**
  47. * @brief Sets the corner of the box with minimum values (opposite to maximum corner).
  48. */
  49. void setMin(const Vector3& vec) { mMinimum = vec; }
  50. /**
  51. * @brief Sets the corner of the box with maximum values (opposite to minimum corner).
  52. */
  53. void setMax(const Vector3& vec) { mMaximum = vec; }
  54. /**
  55. * @brief Sets the minimum and maximum corners.
  56. */
  57. void setExtents(const Vector3& min, const Vector3& max);
  58. /**
  59. * @brief Scales the box around the center by multiplying
  60. * its extends with the provided scale.
  61. */
  62. void scale(const Vector3& s);
  63. /**
  64. * @brief Returns the coordinates of a specific corner.
  65. */
  66. Vector3 getCorner(CornerEnum cornerToGet) const;
  67. /**
  68. * @brief Merges the two boxes, creating a new
  69. * bounding box that encapsulates them both.
  70. */
  71. void merge(const AABox& rhs);
  72. /**
  73. * @brief Expands the bounding box so it includes
  74. * the provided point.
  75. */
  76. void merge(const Vector3& point);
  77. /**
  78. * @brief Transforms the bounding box by the given matrix.
  79. *
  80. * @note As the resulting box will no longer be axis aligned, an axis align box
  81. * is instead created by encompassing the transformed oriented bounding box.
  82. * Retrieving the value as an actual OBB would provide a tighter fit.
  83. */
  84. void transform(const Matrix4& matrix);
  85. /**
  86. * @brief Transforms the bounding box by the given matrix.
  87. *
  88. * @note As the resulting box will no longer be axis aligned, an axis align box
  89. * is instead created by encompassing the transformed oriented bounding box.
  90. * Retrieving the value as an actual OBB would provide a tighter fit.
  91. *
  92. * Provided matrix must be affine.
  93. */
  94. void transformAffine(const Matrix4& matrix);
  95. /**
  96. * @brief Returns true if this and the provided box intersect.
  97. */
  98. bool intersects(const AABox& b2) const;
  99. /**
  100. * @brief Returns true if the sphere intersects the bounding box.
  101. */
  102. bool intersects(const Sphere& s) const;
  103. /**
  104. * @brief Returns true if the plane intersects the bounding box.
  105. */
  106. bool intersects(const Plane& p) const;
  107. /**
  108. * @brief Ray / box intersection, returns a boolean result and nearest distance
  109. * to intersection.
  110. */
  111. std::pair<bool, float> intersects(const Ray& ray) const;
  112. /**
  113. * @brief Ray / box intersection, returns boolean result and near and far intersection distance.
  114. */
  115. bool intersects(const Ray& ray, float& d1, float& d2) const;
  116. Vector3 getCenter() const;
  117. Vector3 getSize() const;
  118. Vector3 getHalfSize() const;
  119. float getRadius() const;
  120. float getVolume() const;
  121. /**
  122. * @brief Returns true if the provided point is inside the bounding box.
  123. */
  124. bool contains(const Vector3& v) const;
  125. /**
  126. * @brief Returns true if the provided bounding box is completely inside the bounding box.
  127. */
  128. bool contains(const AABox& other) const;
  129. bool operator== (const AABox& rhs) const;
  130. bool operator!= (const AABox& rhs) const;
  131. static const AABox BOX_EMPTY;
  132. protected:
  133. Vector3 mMinimum;
  134. Vector3 mMaximum;
  135. };
  136. BS_ALLOW_MEMCPY_SERIALIZATION(AABox);
  137. }