BoundingBox.cpp 8.4 KB

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