BoundingBox.cpp 8.3 KB

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