BsAABox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. Vector3 min = m.getTranslation();
  144. Vector3 max = m.getTranslation();
  145. for(UINT32 i = 0; i < 3; i++)
  146. {
  147. for(UINT32 j = 0; j < 3; j++)
  148. {
  149. float e = m[i][j] * mMinimum[j];
  150. float f = m[i][j] * mMaximum[j];
  151. if(e < f)
  152. {
  153. min[i] += e;
  154. max[i] += f;
  155. }
  156. else
  157. {
  158. min[i] += f;
  159. max[i] += e;
  160. }
  161. }
  162. }
  163. setExtents(min, max);
  164. }
  165. bool AABox::intersects(const AABox& b2) const
  166. {
  167. // Use up to 6 separating planes
  168. if (mMaximum.x < b2.mMinimum.x)
  169. return false;
  170. if (mMaximum.y < b2.mMinimum.y)
  171. return false;
  172. if (mMaximum.z < b2.mMinimum.z)
  173. return false;
  174. if (mMinimum.x > b2.mMaximum.x)
  175. return false;
  176. if (mMinimum.y > b2.mMaximum.y)
  177. return false;
  178. if (mMinimum.z > b2.mMaximum.z)
  179. return false;
  180. // Otherwise, must be intersecting
  181. return true;
  182. }
  183. bool AABox::intersects(const Sphere& sphere) const
  184. {
  185. // Use splitting planes
  186. const Vector3& center = sphere.getCenter();
  187. float radius = sphere.getRadius();
  188. const Vector3& min = getMin();
  189. const Vector3& max = getMax();
  190. // Arvo's algorithm
  191. float s, d = 0;
  192. for (int i = 0; i < 3; ++i)
  193. {
  194. if (center[i] < min[i])
  195. {
  196. s = center[i] - min[i];
  197. d += s * s;
  198. }
  199. else if(center[i] > max[i])
  200. {
  201. s = center[i] - max[i];
  202. d += s * s;
  203. }
  204. }
  205. return d <= radius * radius;
  206. }
  207. bool AABox::intersects(const Plane& p) const
  208. {
  209. return (p.getSide(*this) == Plane::BOTH_SIDE);
  210. }
  211. std::pair<bool, float> AABox::intersects(const Ray& ray) const
  212. {
  213. float lowt = 0.0f;
  214. float t;
  215. bool hit = false;
  216. Vector3 hitpoint(BsZero);
  217. const Vector3& min = getMin();
  218. const Vector3& max = getMax();
  219. const Vector3& rayorig = ray.getOrigin();
  220. const Vector3& raydir = ray.getDirection();
  221. // Check origin inside first
  222. 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))
  223. {
  224. return std::pair<bool, float>(true, 0.0f);
  225. }
  226. // Check each face in turn, only check closest 3
  227. // Min x
  228. if (rayorig.x <= min.x && raydir.x > 0)
  229. {
  230. t = (min.x - rayorig.x) / raydir.x;
  231. if (t >= 0)
  232. {
  233. // Substitute t back into ray and check bounds and dist
  234. hitpoint = rayorig + raydir * t;
  235. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  236. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  237. (!hit || t < lowt))
  238. {
  239. hit = true;
  240. lowt = t;
  241. }
  242. }
  243. }
  244. // Max x
  245. if (rayorig.x >= max.x && raydir.x < 0)
  246. {
  247. t = (max.x - rayorig.x) / raydir.x;
  248. if (t >= 0)
  249. {
  250. // Substitute t back into ray and check bounds and dist
  251. hitpoint = rayorig + raydir * t;
  252. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  253. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  254. (!hit || t < lowt))
  255. {
  256. hit = true;
  257. lowt = t;
  258. }
  259. }
  260. }
  261. // Min y
  262. if (rayorig.y <= min.y && raydir.y > 0)
  263. {
  264. t = (min.y - rayorig.y) / raydir.y;
  265. if (t >= 0)
  266. {
  267. // Substitute t back into ray and check bounds and dist
  268. hitpoint = rayorig + raydir * t;
  269. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  270. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  271. (!hit || t < lowt))
  272. {
  273. hit = true;
  274. lowt = t;
  275. }
  276. }
  277. }
  278. // Max y
  279. if (rayorig.y >= max.y && raydir.y < 0)
  280. {
  281. t = (max.y - rayorig.y) / raydir.y;
  282. if (t >= 0)
  283. {
  284. // Substitute t back into ray and check bounds and dist
  285. hitpoint = rayorig + raydir * t;
  286. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  287. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  288. (!hit || t < lowt))
  289. {
  290. hit = true;
  291. lowt = t;
  292. }
  293. }
  294. }
  295. // Min z
  296. if (rayorig.z <= min.z && raydir.z > 0)
  297. {
  298. t = (min.z - rayorig.z) / raydir.z;
  299. if (t >= 0)
  300. {
  301. // Substitute t back into ray and check bounds and dist
  302. hitpoint = rayorig + raydir * t;
  303. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  304. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  305. (!hit || t < lowt))
  306. {
  307. hit = true;
  308. lowt = t;
  309. }
  310. }
  311. }
  312. // Max z
  313. if (rayorig.z >= max.z && raydir.z < 0)
  314. {
  315. t = (max.z - rayorig.z) / raydir.z;
  316. if (t >= 0)
  317. {
  318. // Substitute t back into ray and check bounds and dist
  319. hitpoint = rayorig + raydir * t;
  320. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  321. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  322. (!hit || t < lowt))
  323. {
  324. hit = true;
  325. lowt = t;
  326. }
  327. }
  328. }
  329. return std::pair<bool, float>(hit, lowt);
  330. }
  331. bool AABox::intersects(const Ray& ray, float& d1, float& d2) const
  332. {
  333. const Vector3& min = getMin();
  334. const Vector3& max = getMax();
  335. const Vector3& rayorig = ray.getOrigin();
  336. const Vector3& raydir = ray.getDirection();
  337. Vector3 absDir;
  338. absDir[0] = Math::abs(raydir[0]);
  339. absDir[1] = Math::abs(raydir[1]);
  340. absDir[2] = Math::abs(raydir[2]);
  341. // Sort the axis, ensure check minimise floating error axis first
  342. int imax = 0, imid = 1, imin = 2;
  343. if (absDir[0] < absDir[2])
  344. {
  345. imax = 2;
  346. imin = 0;
  347. }
  348. if (absDir[1] < absDir[imin])
  349. {
  350. imid = imin;
  351. imin = 1;
  352. }
  353. else if (absDir[1] > absDir[imax])
  354. {
  355. imid = imax;
  356. imax = 1;
  357. }
  358. float start = 0, end = Math::POS_INFINITY;
  359. #define _CALC_AXIS(i) \
  360. do { \
  361. float denom = 1 / raydir[i]; \
  362. float newstart = (min[i] - rayorig[i]) * denom; \
  363. float newend = (max[i] - rayorig[i]) * denom; \
  364. if (newstart > newend) std::swap(newstart, newend); \
  365. if (newstart > end || newend < start) return false; \
  366. if (newstart > start) start = newstart; \
  367. if (newend < end) end = newend; \
  368. } while(0)
  369. // Check each axis in turn
  370. _CALC_AXIS(imax);
  371. if (absDir[imid] < std::numeric_limits<float>::epsilon())
  372. {
  373. // Parallel with middle and minimise axis, check bounds only
  374. if (rayorig[imid] < min[imid] || rayorig[imid] > max[imid] ||
  375. rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
  376. return false;
  377. }
  378. else
  379. {
  380. _CALC_AXIS(imid);
  381. if (absDir[imin] < std::numeric_limits<float>::epsilon())
  382. {
  383. // Parallel with minimise axis, check bounds only
  384. if (rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
  385. return false;
  386. }
  387. else
  388. {
  389. _CALC_AXIS(imin);
  390. }
  391. }
  392. #undef _CALC_AXIS
  393. d1 = start;
  394. d2 = end;
  395. return true;
  396. }
  397. Vector3 AABox::getCenter() const
  398. {
  399. return Vector3(
  400. (mMaximum.x + mMinimum.x) * 0.5f,
  401. (mMaximum.y + mMinimum.y) * 0.5f,
  402. (mMaximum.z + mMinimum.z) * 0.5f);
  403. }
  404. Vector3 AABox::getSize() const
  405. {
  406. return mMaximum - mMinimum;
  407. }
  408. Vector3 AABox::getHalfSize() const
  409. {
  410. return (mMaximum - mMinimum) * 0.5;
  411. }
  412. float AABox::getRadius() const
  413. {
  414. return ((mMaximum - mMinimum) * 0.5).length();
  415. }
  416. float AABox::getVolume() const
  417. {
  418. Vector3 diff = mMaximum - mMinimum;
  419. return diff.x * diff.y * diff.z;
  420. }
  421. bool AABox::contains(const Vector3& v) const
  422. {
  423. return mMinimum.x <= v.x && v.x <= mMaximum.x &&
  424. mMinimum.y <= v.y && v.y <= mMaximum.y &&
  425. mMinimum.z <= v.z && v.z <= mMaximum.z;
  426. }
  427. bool AABox::contains(const AABox& other) const
  428. {
  429. return this->mMinimum.x <= other.mMinimum.x &&
  430. this->mMinimum.y <= other.mMinimum.y &&
  431. this->mMinimum.z <= other.mMinimum.z &&
  432. other.mMaximum.x <= this->mMaximum.x &&
  433. other.mMaximum.y <= this->mMaximum.y &&
  434. other.mMaximum.z <= this->mMaximum.z;
  435. }
  436. bool AABox::operator== (const AABox& rhs) const
  437. {
  438. return this->mMinimum == rhs.mMinimum &&
  439. this->mMaximum == rhs.mMaximum;
  440. }
  441. bool AABox::operator!= (const AABox& rhs) const
  442. {
  443. return !(*this == rhs);
  444. }
  445. }