boundingbox.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2000 Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4. // AJM:
  5. #ifdef SYSTEM_WIN32
  6. #pragma warning(disable:4305) // truncation from 'const double' to 'float'
  7. #endif
  8. #ifndef BOUNDINGBOX_H__
  9. #define BOUNDINGBOX_H__
  10. #if _MSC_VER >= 1000
  11. #pragma once
  12. #endif
  13. class BoundingBox
  14. {
  15. public:
  16. typedef enum
  17. {
  18. eDisjoint, // neither boxes touch
  19. eUnion, // this box intersects with the other box
  20. eSubset, // this box is inside the other box
  21. eSuperset // this box is completly envelops the other box
  22. } eBoundingState;
  23. // Tests if other box is completely outside of this box
  24. bool testDisjoint(const BoundingBox& other) const
  25. {
  26. if ((m_Mins[0] > other.m_Maxs[0]) ||
  27. (m_Mins[1] > other.m_Maxs[1]) ||
  28. (m_Mins[2] > other.m_Maxs[2]) ||
  29. (m_Maxs[0] < other.m_Mins[0]) ||
  30. (m_Maxs[1] < other.m_Mins[1]) ||
  31. (m_Maxs[2] < other.m_Mins[2]))
  32. {
  33. return true;
  34. }
  35. return false;
  36. }
  37. // returns true if this box is completely inside other box
  38. bool testSubset(const BoundingBox& other) const
  39. {
  40. if (
  41. (m_Mins[0] >= other.m_Mins[0]) &&
  42. (m_Maxs[0] <= other.m_Maxs[0]) &&
  43. (m_Mins[1] >= other.m_Mins[1]) &&
  44. (m_Maxs[1] <= other.m_Maxs[1]) &&
  45. (m_Mins[2] >= other.m_Mins[2]) &&
  46. (m_Maxs[2] <= other.m_Maxs[2])
  47. )
  48. {
  49. return true;
  50. }
  51. return false;
  52. }
  53. // returns true if this box contains the other box completely
  54. bool testSuperset(const BoundingBox& other) const
  55. {
  56. return other.testSubset(*this);
  57. }
  58. // returns true if this box partially intersects the other box
  59. bool testUnion(const BoundingBox& other) const
  60. {
  61. BoundingBox tmpBox;
  62. tmpBox.m_Mins[0] = max(m_Mins[0], other.m_Mins[0]);
  63. tmpBox.m_Mins[1] = max(m_Mins[1], other.m_Mins[1]);
  64. tmpBox.m_Mins[2] = max(m_Mins[2], other.m_Mins[2]);
  65. tmpBox.m_Maxs[0] = min(m_Maxs[0], other.m_Maxs[0]);
  66. tmpBox.m_Maxs[1] = min(m_Maxs[1], other.m_Maxs[1]);
  67. tmpBox.m_Maxs[2] = min(m_Maxs[2], other.m_Maxs[2]);
  68. if ((tmpBox.m_Mins[0] > tmpBox.m_Maxs[0]) ||
  69. (tmpBox.m_Mins[1] > tmpBox.m_Maxs[1]) ||
  70. (tmpBox.m_Mins[2] > tmpBox.m_Maxs[2]))
  71. {
  72. return false;
  73. }
  74. return true;
  75. }
  76. eBoundingState test(const BoundingBox& other) const
  77. {
  78. eBoundingState rval;
  79. if (testDisjoint(other))
  80. {
  81. rval = eDisjoint;
  82. }
  83. else if (testSubset(other))
  84. {
  85. rval = eSubset;
  86. }
  87. else if (testSuperset(other))
  88. {
  89. rval = eSuperset;
  90. }
  91. else
  92. {
  93. rval = eUnion;
  94. }
  95. return rval;
  96. }
  97. void set(const vec3_t mins, const vec3_t maxs)
  98. {
  99. VectorCopy(mins, m_Mins);
  100. VectorCopy(maxs, m_Maxs);
  101. }
  102. void reset()
  103. {
  104. VectorFill(m_Mins, 999999999.999);
  105. VectorFill(m_Maxs, -999999999.999);
  106. }
  107. void add(const vec3_t point)
  108. {
  109. m_Mins[0] = min(m_Mins[0], point[0]);
  110. m_Maxs[0] = max(m_Maxs[0], point[0]);
  111. m_Mins[1] = min(m_Mins[1], point[1]);
  112. m_Maxs[1] = max(m_Maxs[1], point[1]);
  113. m_Mins[2] = min(m_Mins[2], point[2]);
  114. m_Maxs[2] = max(m_Maxs[2], point[2]);
  115. }
  116. void add(const BoundingBox& other)
  117. {
  118. add(other.m_Mins);
  119. add(other.m_Maxs);
  120. }
  121. public:
  122. // BoundingBox(const BoundingBox& other) // Default copy constructor ok
  123. // BoundingBox& operator=(const BoundingBox& other); // Default copy operator ok
  124. BoundingBox()
  125. {
  126. reset();
  127. }
  128. BoundingBox(const vec3_t& mins, const vec3_t& maxs)
  129. {
  130. VectorCopy(mins, m_Mins);
  131. VectorCopy(maxs, m_Maxs);
  132. }
  133. ~BoundingBox() {}
  134. public:
  135. // Bounding box
  136. vec3_t m_Mins;
  137. vec3_t m_Maxs;
  138. };
  139. #endif//BOUNDINGBOX_H__