BsAABox.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "BsAABox.h"
  2. #include "BsRay.h"
  3. #include "BsPlane.h"
  4. #include "BsSphere.h"
  5. namespace BansheeEngine
  6. {
  7. const AABox AABox::BOX_EMPTY;
  8. AABox::AABox()
  9. :mMinimum(Vector3::ZERO), mMaximum(Vector3::ONE)
  10. {
  11. // Default to a null box
  12. setMin(Vector3(-0.5f, -0.5f, -0.5f));
  13. setMax(Vector3(0.5f, 0.5f, 0.5f));
  14. }
  15. AABox::AABox(const AABox& copy)
  16. :mMinimum(Vector3::ZERO), mMaximum(Vector3::ONE)
  17. {
  18. setExtents(copy.mMinimum, copy.mMaximum);
  19. }
  20. AABox::AABox(const Vector3& min, const Vector3& max)
  21. :mMinimum(Vector3::ZERO), mMaximum(Vector3::ONE)
  22. {
  23. setExtents(min, max);
  24. }
  25. AABox& AABox::operator=(const AABox& rhs)
  26. {
  27. setExtents(rhs.mMinimum, rhs.mMaximum);
  28. return *this;
  29. }
  30. void AABox::setExtents(const Vector3& min, const Vector3& max)
  31. {
  32. assert( (min.x <= max.x && min.y <= max.y && min.z <= max.z) &&
  33. "The minimum corner of the box must be less than or equal to maximum corner" );
  34. mMinimum = min;
  35. mMaximum = max;
  36. }
  37. void AABox::scale(const Vector3& s)
  38. {
  39. Vector3 center = getCenter();
  40. Vector3 min = center + (mMinimum - center) * s;
  41. Vector3 max = center + (mMaximum - center) * s;
  42. setExtents(min, max);
  43. }
  44. Vector3 AABox::getCorner(CornerEnum cornerToGet) const
  45. {
  46. switch(cornerToGet)
  47. {
  48. case FAR_LEFT_BOTTOM:
  49. return mMinimum;
  50. case FAR_LEFT_TOP:
  51. return Vector3(mMinimum.x, mMaximum.y, mMinimum.z);
  52. case FAR_RIGHT_TOP:
  53. return Vector3(mMaximum.x, mMaximum.y, mMinimum.z);
  54. case FAR_RIGHT_BOTTOM:
  55. return Vector3(mMaximum.x, mMinimum.y, mMinimum.z);
  56. case NEAR_RIGHT_BOTTOM:
  57. return Vector3(mMaximum.x, mMinimum.y, mMaximum.z);
  58. case NEAR_LEFT_BOTTOM:
  59. return Vector3(mMinimum.x, mMinimum.y, mMaximum.z);
  60. case NEAR_LEFT_TOP:
  61. return Vector3(mMinimum.x, mMaximum.y, mMaximum.z);
  62. case NEAR_RIGHT_TOP:
  63. return mMaximum;
  64. default:
  65. return Vector3();
  66. }
  67. }
  68. void AABox::merge(const AABox& rhs)
  69. {
  70. Vector3 min = mMinimum;
  71. Vector3 max = mMaximum;
  72. max.ceil(rhs.mMaximum);
  73. min.floor(rhs.mMinimum);
  74. setExtents(min, max);
  75. }
  76. void AABox::merge(const Vector3& point)
  77. {
  78. mMaximum.ceil(point);
  79. mMinimum.floor(point);
  80. }
  81. void AABox::transform(const Matrix4& matrix)
  82. {
  83. Vector3 oldMin, oldMax, currentCorner;
  84. // Getting the old values so that we can use the existing merge method.
  85. oldMin = mMinimum;
  86. oldMax = mMaximum;
  87. // We sequentially compute the corners in the following order :
  88. // 0, 6, 5, 1, 2, 4, 7, 3
  89. // This sequence allows us to only change one member at a time to get at all corners.
  90. // For each one, we transform it using the matrix
  91. // Which gives the resulting point and merge the resulting point.
  92. // First corner
  93. // min min min
  94. currentCorner = oldMin;
  95. merge(matrix.multiply3x4(currentCorner));
  96. // min,min,max
  97. currentCorner.z = oldMax.z;
  98. merge(matrix.multiply3x4(currentCorner));
  99. // min max max
  100. currentCorner.y = oldMax.y;
  101. merge(matrix.multiply3x4(currentCorner));
  102. // min max min
  103. currentCorner.z = oldMin.z;
  104. merge(matrix.multiply3x4(currentCorner));
  105. // max max min
  106. currentCorner.x = oldMax.x;
  107. merge(matrix.multiply3x4(currentCorner));
  108. // max max max
  109. currentCorner.z = oldMax.z;
  110. merge(matrix.multiply3x4(currentCorner));
  111. // max min max
  112. currentCorner.y = oldMin.y;
  113. merge(matrix.multiply3x4(currentCorner));
  114. // max min min
  115. currentCorner.z = oldMin.z;
  116. merge(matrix.multiply3x4(currentCorner));
  117. }
  118. void AABox::transformAffine(const Matrix4& m)
  119. {
  120. assert(m.isAffine());
  121. Vector3 centre = getCenter();
  122. Vector3 halfSize = getHalfSize();
  123. Vector3 newCentre = m.multiply3x4(centre);
  124. Vector3 newHalfSize(
  125. Math::abs(m[0][0]) * halfSize.x + Math::abs(m[0][1]) * halfSize.y + Math::abs(m[0][2]) * halfSize.z,
  126. Math::abs(m[1][0]) * halfSize.x + Math::abs(m[1][1]) * halfSize.y + Math::abs(m[1][2]) * halfSize.z,
  127. Math::abs(m[2][0]) * halfSize.x + Math::abs(m[2][1]) * halfSize.y + Math::abs(m[2][2]) * halfSize.z);
  128. setExtents(newCentre - newHalfSize, newCentre + newHalfSize);
  129. }
  130. bool AABox::intersects(const AABox& b2) const
  131. {
  132. // Use up to 6 separating planes
  133. if (mMaximum.x < b2.mMinimum.x)
  134. return false;
  135. if (mMaximum.y < b2.mMinimum.y)
  136. return false;
  137. if (mMaximum.z < b2.mMinimum.z)
  138. return false;
  139. if (mMinimum.x > b2.mMaximum.x)
  140. return false;
  141. if (mMinimum.y > b2.mMaximum.y)
  142. return false;
  143. if (mMinimum.z > b2.mMaximum.z)
  144. return false;
  145. // Otherwise, must be intersecting
  146. return true;
  147. }
  148. bool AABox::intersects(const Sphere& sphere) const
  149. {
  150. // Use splitting planes
  151. const Vector3& center = sphere.getCenter();
  152. float radius = sphere.getRadius();
  153. const Vector3& min = getMin();
  154. const Vector3& max = getMax();
  155. // Arvo's algorithm
  156. float s, d = 0;
  157. for (int i = 0; i < 3; ++i)
  158. {
  159. if (center[i] < min[i])
  160. {
  161. s = center[i] - min[i];
  162. d += s * s;
  163. }
  164. else if(center[i] > max[i])
  165. {
  166. s = center[i] - max[i];
  167. d += s * s;
  168. }
  169. }
  170. return d <= radius * radius;
  171. }
  172. bool AABox::intersects(const Plane& p) const
  173. {
  174. return (p.getSide(*this) == Plane::BOTH_SIDE);
  175. }
  176. std::pair<bool, float> AABox::intersects(const Ray& ray) const
  177. {
  178. float lowt = 0.0f;
  179. float t;
  180. bool hit = false;
  181. Vector3 hitpoint;
  182. const Vector3& min = getMin();
  183. const Vector3& max = getMax();
  184. const Vector3& rayorig = ray.getOrigin();
  185. const Vector3& raydir = ray.getDirection();
  186. // Check origin inside first
  187. if ((rayorig.x > min.x && rayorig.y > min.y && rayorig.z > min.z) && (rayorig.x < max.x && rayorig.y < max.y && rayorig.z < max.z))
  188. {
  189. return std::pair<bool, float>(true, 0.0f);
  190. }
  191. // Check each face in turn, only check closest 3
  192. // Min x
  193. if (rayorig.x <= min.x && raydir.x > 0)
  194. {
  195. t = (min.x - rayorig.x) / raydir.x;
  196. if (t >= 0)
  197. {
  198. // Substitute t back into ray and check bounds and dist
  199. hitpoint = rayorig + raydir * t;
  200. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  201. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  202. (!hit || t < lowt))
  203. {
  204. hit = true;
  205. lowt = t;
  206. }
  207. }
  208. }
  209. // Max x
  210. if (rayorig.x >= max.x && raydir.x < 0)
  211. {
  212. t = (max.x - rayorig.x) / raydir.x;
  213. if (t >= 0)
  214. {
  215. // Substitute t back into ray and check bounds and dist
  216. hitpoint = rayorig + raydir * t;
  217. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  218. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  219. (!hit || t < lowt))
  220. {
  221. hit = true;
  222. lowt = t;
  223. }
  224. }
  225. }
  226. // Min y
  227. if (rayorig.y <= min.y && raydir.y > 0)
  228. {
  229. t = (min.y - rayorig.y) / raydir.y;
  230. if (t >= 0)
  231. {
  232. // Substitute t back into ray and check bounds and dist
  233. hitpoint = rayorig + raydir * t;
  234. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  235. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  236. (!hit || t < lowt))
  237. {
  238. hit = true;
  239. lowt = t;
  240. }
  241. }
  242. }
  243. // Max y
  244. if (rayorig.y >= max.y && raydir.y < 0)
  245. {
  246. t = (max.y - rayorig.y) / raydir.y;
  247. if (t >= 0)
  248. {
  249. // Substitute t back into ray and check bounds and dist
  250. hitpoint = rayorig + raydir * t;
  251. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  252. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  253. (!hit || t < lowt))
  254. {
  255. hit = true;
  256. lowt = t;
  257. }
  258. }
  259. }
  260. // Min z
  261. if (rayorig.z <= min.z && raydir.z > 0)
  262. {
  263. t = (min.z - rayorig.z) / raydir.z;
  264. if (t >= 0)
  265. {
  266. // Substitute t back into ray and check bounds and dist
  267. hitpoint = rayorig + raydir * t;
  268. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  269. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  270. (!hit || t < lowt))
  271. {
  272. hit = true;
  273. lowt = t;
  274. }
  275. }
  276. }
  277. // Max z
  278. if (rayorig.z >= max.z && raydir.z < 0)
  279. {
  280. t = (max.z - rayorig.z) / raydir.z;
  281. if (t >= 0)
  282. {
  283. // Substitute t back into ray and check bounds and dist
  284. hitpoint = rayorig + raydir * t;
  285. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  286. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  287. (!hit || t < lowt))
  288. {
  289. hit = true;
  290. lowt = t;
  291. }
  292. }
  293. }
  294. return std::pair<bool, float>(hit, lowt);
  295. }
  296. bool AABox::intersects(const Ray& ray, float& d1, float& d2) const
  297. {
  298. const Vector3& min = getMin();
  299. const Vector3& max = getMax();
  300. const Vector3& rayorig = ray.getOrigin();
  301. const Vector3& raydir = ray.getDirection();
  302. Vector3 absDir;
  303. absDir[0] = Math::abs(raydir[0]);
  304. absDir[1] = Math::abs(raydir[1]);
  305. absDir[2] = Math::abs(raydir[2]);
  306. // Sort the axis, ensure check minimise floating error axis first
  307. int imax = 0, imid = 1, imin = 2;
  308. if (absDir[0] < absDir[2])
  309. {
  310. imax = 2;
  311. imin = 0;
  312. }
  313. if (absDir[1] < absDir[imin])
  314. {
  315. imid = imin;
  316. imin = 1;
  317. }
  318. else if (absDir[1] > absDir[imax])
  319. {
  320. imid = imax;
  321. imax = 1;
  322. }
  323. float start = 0, end = Math::POS_INFINITY;
  324. #define _CALC_AXIS(i) \
  325. do { \
  326. float denom = 1 / raydir[i]; \
  327. float newstart = (min[i] - rayorig[i]) * denom; \
  328. float newend = (max[i] - rayorig[i]) * denom; \
  329. if (newstart > newend) std::swap(newstart, newend); \
  330. if (newstart > end || newend < start) return false; \
  331. if (newstart > start) start = newstart; \
  332. if (newend < end) end = newend; \
  333. } while(0)
  334. // Check each axis in turn
  335. _CALC_AXIS(imax);
  336. if (absDir[imid] < std::numeric_limits<float>::epsilon())
  337. {
  338. // Parallel with middle and minimise axis, check bounds only
  339. if (rayorig[imid] < min[imid] || rayorig[imid] > max[imid] ||
  340. rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
  341. return false;
  342. }
  343. else
  344. {
  345. _CALC_AXIS(imid);
  346. if (absDir[imin] < std::numeric_limits<float>::epsilon())
  347. {
  348. // Parallel with minimise axis, check bounds only
  349. if (rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
  350. return false;
  351. }
  352. else
  353. {
  354. _CALC_AXIS(imin);
  355. }
  356. }
  357. #undef _CALC_AXIS
  358. d1 = start;
  359. d2 = end;
  360. return true;
  361. }
  362. Vector3 AABox::getCenter() const
  363. {
  364. return Vector3(
  365. (mMaximum.x + mMinimum.x) * 0.5f,
  366. (mMaximum.y + mMinimum.y) * 0.5f,
  367. (mMaximum.z + mMinimum.z) * 0.5f);
  368. }
  369. Vector3 AABox::getSize() const
  370. {
  371. return mMaximum - mMinimum;
  372. }
  373. Vector3 AABox::getHalfSize() const
  374. {
  375. return (mMaximum - mMinimum) * 0.5;
  376. }
  377. float AABox::getRadius() const
  378. {
  379. return (mMaximum - mMinimum).length();
  380. }
  381. float AABox::getVolume() const
  382. {
  383. Vector3 diff = mMaximum - mMinimum;
  384. return diff.x * diff.y * diff.z;
  385. }
  386. bool AABox::contains(const Vector3& v) const
  387. {
  388. return mMinimum.x <= v.x && v.x <= mMaximum.x &&
  389. mMinimum.y <= v.y && v.y <= mMaximum.y &&
  390. mMinimum.z <= v.z && v.z <= mMaximum.z;
  391. }
  392. bool AABox::contains(const AABox& other) const
  393. {
  394. return this->mMinimum.x <= other.mMinimum.x &&
  395. this->mMinimum.y <= other.mMinimum.y &&
  396. this->mMinimum.z <= other.mMinimum.z &&
  397. other.mMaximum.x <= this->mMaximum.x &&
  398. other.mMaximum.y <= this->mMaximum.y &&
  399. other.mMaximum.z <= this->mMaximum.z;
  400. }
  401. bool AABox::operator== (const AABox& rhs) const
  402. {
  403. return this->mMinimum == rhs.mMinimum &&
  404. this->mMaximum == rhs.mMaximum;
  405. }
  406. bool AABox::operator!= (const AABox& rhs) const
  407. {
  408. return !(*this == rhs);
  409. }
  410. }