BsAABox.cpp 11 KB

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