BoundingBox.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include "Base.h"
  2. #include "BoundingBox.h"
  3. #include "BoundingSphere.h"
  4. #include "Plane.h"
  5. namespace gameplay
  6. {
  7. BoundingBox::BoundingBox()
  8. {
  9. }
  10. BoundingBox::BoundingBox(const Vector3& min, const Vector3& max)
  11. {
  12. set(min, max);
  13. }
  14. BoundingBox::BoundingBox(const BoundingBox& copy)
  15. {
  16. set(copy);
  17. }
  18. BoundingBox::~BoundingBox()
  19. {
  20. }
  21. const BoundingBox& BoundingBox::empty()
  22. {
  23. static BoundingBox b;
  24. return b;
  25. }
  26. void BoundingBox::getCorners(Vector3* dst) const
  27. {
  28. assert(dst);
  29. // Near face, specified counter-clockwise looking towards the origin from the positive z-axis.
  30. // Left-top-front.
  31. dst[0].set(min.x, max.y, max.z);
  32. // Left-bottom-front.
  33. dst[1].set(min.x, min.y, max.z);
  34. // Right-bottom-front.
  35. dst[2].set(max.x, min.y, max.z);
  36. // Right-top-front.
  37. dst[3].set(max.x, max.y, max.z);
  38. // Far face, specified counter-clockwise looking towards the origin from the negative z-axis.
  39. // Right-top-back.
  40. dst[4].set(max.x, max.y, min.z);
  41. // Right-bottom-back.
  42. dst[5].set(max.x, min.y, min.z);
  43. // Left-bottom-back.
  44. dst[6].set(min.x, min.y, min.z);
  45. // Left-top-back.
  46. dst[7].set(min.x, max.y, min.z);
  47. }
  48. Vector3 BoundingBox::getCenter() const
  49. {
  50. Vector3 center;
  51. getCenter(&center);
  52. return center;
  53. }
  54. void BoundingBox::getCenter(Vector3* dst) const
  55. {
  56. dst->set(min, max);
  57. dst->scale(0.5f);
  58. dst->add(min);
  59. }
  60. bool BoundingBox::intersects(const BoundingSphere& sphere) const
  61. {
  62. return sphere.intersects(*this);
  63. }
  64. bool BoundingBox::intersects(const BoundingBox& box) const
  65. {
  66. return ((min.x >= box.min.x && min.x <= box.max.x) || (box.min.x >= min.x && box.min.x <= max.x)) &&
  67. ((min.y >= box.min.y && min.y <= box.max.y) || (box.min.y >= min.y && box.min.y <= max.y)) &&
  68. ((min.z >= box.min.z && min.z <= box.max.z) || (box.min.z >= min.z && box.min.z <= max.z));
  69. }
  70. bool BoundingBox::intersects(const Frustum& frustum) const
  71. {
  72. // The box must either intersect or be in the positive half-space of all six planes of the frustum.
  73. return (intersects(frustum.getNear()) != Plane::INTERSECTS_BACK &&
  74. intersects(frustum.getFar()) != Plane::INTERSECTS_BACK &&
  75. intersects(frustum.getLeft()) != Plane::INTERSECTS_BACK &&
  76. intersects(frustum.getRight()) != Plane::INTERSECTS_BACK &&
  77. intersects(frustum.getBottom()) != Plane::INTERSECTS_BACK &&
  78. intersects(frustum.getTop()) != Plane::INTERSECTS_BACK);
  79. }
  80. float BoundingBox::intersects(const Plane& plane) const
  81. {
  82. // Calculate the distance from the center of the box to the plane.
  83. Vector3 center((min.x + max.x) * 0.5f, (min.y + max.y) * 0.5f, (min.z + max.z) * 0.5f);
  84. float distance = plane.distance(center);
  85. // Get the extents of the box from its center along each axis.
  86. float extentX = (max.x - min.x) * 0.5f;
  87. float extentY = (max.y - min.y) * 0.5f;
  88. float extentZ = (max.z - min.z) * 0.5f;
  89. const Vector3& planeNormal = plane.getNormal();
  90. if (fabsf(distance) <= (fabsf(extentX * planeNormal.x) + fabsf(extentY * planeNormal.y) + fabsf(
  91. extentZ * planeNormal.z)))
  92. {
  93. return Plane::INTERSECTS_INTERSECTING;
  94. }
  95. return (distance > 0.0f) ? (float)Plane::INTERSECTS_FRONT : (float)Plane::INTERSECTS_BACK;
  96. }
  97. float BoundingBox::intersects(const Ray& ray) const
  98. {
  99. // Intermediate calculation variables.
  100. float dnear = 0.0f;
  101. float dfar = 0.0f;
  102. float tmin = 0.0f;
  103. float tmax = 0.0f;
  104. const Vector3& origin = ray.getOrigin();
  105. const Vector3& direction = ray.getDirection();
  106. // X direction.
  107. float div = 1.0f / direction.x;
  108. if (div >= 0.0f)
  109. {
  110. tmin = (min.x - origin.x) * div;
  111. tmax = (max.x - origin.x) * div;
  112. }
  113. else
  114. {
  115. tmin = (max.x - origin.x) * div;
  116. tmax = (min.x - origin.x) * div;
  117. }
  118. dnear = tmin;
  119. dfar = tmax;
  120. // Check if the ray misses the box.
  121. if (dnear > dfar || dfar < 0.0f)
  122. {
  123. return Ray::INTERSECTS_NONE;
  124. }
  125. // Y direction.
  126. div = 1.0f / direction.y;
  127. if (div >= 0.0f)
  128. {
  129. tmin = (min.y - origin.y) * div;
  130. tmax = (max.y - origin.y) * div;
  131. }
  132. else
  133. {
  134. tmin = (max.y - origin.y) * div;
  135. tmax = (min.y - origin.y) * div;
  136. }
  137. // Update the near and far intersection distances.
  138. if (tmin > dnear)
  139. {
  140. dnear = tmin;
  141. }
  142. if (tmax < dfar)
  143. {
  144. dfar = tmax;
  145. }
  146. // Check if the ray misses the box.
  147. if (dnear > dfar || dfar < 0.0f)
  148. {
  149. return Ray::INTERSECTS_NONE;
  150. }
  151. // Z direction.
  152. div = 1.0f / direction.z;
  153. if (div >= 0.0f)
  154. {
  155. tmin = (min.z - origin.z) * div;
  156. tmax = (max.z - origin.z) * div;
  157. }
  158. else
  159. {
  160. tmin = (max.z - origin.z) * div;
  161. tmax = (min.z - origin.z) * div;
  162. }
  163. // Update the near and far intersection distances.
  164. if (tmin > dnear)
  165. {
  166. dnear = tmin;
  167. }
  168. if (tmax < dfar)
  169. {
  170. dfar = tmax;
  171. }
  172. // Check if the ray misses the box.
  173. if (dnear > dfar || dfar < 0.0f)
  174. {
  175. return Ray::INTERSECTS_NONE;
  176. }
  177. // The ray intersects the box (and since the direction of a Ray is normalized, dnear is the distance to the ray).
  178. return dnear;
  179. }
  180. bool BoundingBox::isEmpty() const
  181. {
  182. return min.x == max.x && min.y == max.y && min.z == max.z;
  183. }
  184. void BoundingBox::merge(const BoundingBox& box)
  185. {
  186. // Calculate the new minimum point.
  187. min.x = std::min(min.x, box.min.x);
  188. min.y = std::min(min.y, box.min.y);
  189. min.z = std::min(min.z, box.min.z);
  190. // Calculate the new maximum point.
  191. max.x = std::max(max.x, box.max.x);
  192. max.y = std::max(max.y, box.max.y);
  193. max.z = std::max(max.z, box.max.z);
  194. }
  195. void BoundingBox::merge(const BoundingSphere& sphere)
  196. {
  197. const Vector3& center = sphere.center;
  198. float radius = sphere.radius;
  199. // Calculate the new minimum point for the merged bounding box.
  200. min.x = std::min(min.x, center.x - radius);
  201. min.y = std::min(min.y, center.y - radius);
  202. min.z = std::min(min.z, center.z - radius);
  203. // Calculate the new maximum point for the merged bounding box.
  204. max.x = std::max(max.x, center.x + radius);
  205. max.y = std::max(max.y, center.y + radius);
  206. max.z = std::max(max.z, center.z + radius);
  207. }
  208. void BoundingBox::set(const Vector3& min, const Vector3& max)
  209. {
  210. this->min = min;
  211. this->max = max;
  212. }
  213. void updateMinMax(Vector3* point, Vector3* min, Vector3* max)
  214. {
  215. // Leftmost point.
  216. if (point->x < min->x)
  217. {
  218. min->x = point->x;
  219. }
  220. // Rightmost point.
  221. if (point->x > max->x)
  222. {
  223. max->x = point->x;
  224. }
  225. // Lowest point.
  226. if (point->y < min->y)
  227. {
  228. min->y = point->y;
  229. }
  230. // Highest point.
  231. if (point->y > max->y)
  232. {
  233. max->y = point->y;
  234. }
  235. // Farthest point.
  236. if (point->z < min->z)
  237. {
  238. min->z = point->z;
  239. }
  240. // Nearest point.
  241. if (point->z > max->z)
  242. {
  243. max->z = point->z;
  244. }
  245. }
  246. void BoundingBox::set(const BoundingBox& box)
  247. {
  248. min = box.min;
  249. max = box.max;
  250. }
  251. void BoundingBox::set(const BoundingSphere& sphere)
  252. {
  253. std::vector<int> v;
  254. v.push_back(0);
  255. std::vector<int> v2 = v;
  256. const Vector3& center = sphere.center;
  257. float radius = sphere.radius;
  258. // Calculate the minimum point for the box.
  259. min.x = center.x - radius;
  260. min.y = center.y - radius;
  261. min.z = center.z - radius;
  262. // Calculate the maximum point for the box.
  263. max.x = center.x + radius;
  264. max.y = center.y + radius;
  265. max.z = center.z + radius;
  266. }
  267. void BoundingBox::transform(const Matrix& matrix)
  268. {
  269. // Calculate the corners.
  270. Vector3 corners[8];
  271. getCorners(corners);
  272. // Transform the corners, recalculating the min and max points along the way.
  273. matrix.transformPoint(&corners[0]);
  274. Vector3 newMin = corners[0];
  275. Vector3 newMax = corners[0];
  276. for (int i = 1; i < 8; i++)
  277. {
  278. matrix.transformPoint(&corners[i]);
  279. updateMinMax(&corners[i], &newMin, &newMax);
  280. }
  281. this->min.x = newMin.x;
  282. this->min.y = newMin.y;
  283. this->min.z = newMin.z;
  284. this->max.x = newMax.x;
  285. this->max.y = newMax.y;
  286. this->max.z = newMax.z;
  287. }
  288. }