BoundingBox.cpp 8.2 KB

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