BoundingBox.cpp 8.7 KB

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