BsAABox.h 4.6 KB

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