BsAABox.cpp 12 KB

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