bounds.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bx/rng.h>
  6. #include <bx/math.h>
  7. #include "bounds.h"
  8. using namespace bx;
  9. Vec3 getCenter(const Aabb& _aabb)
  10. {
  11. return mul(add(_aabb.min, _aabb.max), 0.5f);
  12. }
  13. Vec3 getExtents(const Aabb& _aabb)
  14. {
  15. return mul(sub(_aabb.max, _aabb.min), 0.5f);
  16. }
  17. Vec3 getCenter(const Triangle& _triangle)
  18. {
  19. return mul(add(add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f);
  20. }
  21. void toAabb(Aabb& _outAabb, const bx::Vec3& _extents)
  22. {
  23. _outAabb.min = neg(_extents);
  24. _outAabb.max = _extents;
  25. }
  26. void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extents)
  27. {
  28. _outAabb.min = sub(_center, _extents);
  29. _outAabb.max = add(_center, _extents);
  30. }
  31. void toAabb(Aabb& _outAabb, const Cylinder& _cylinder)
  32. {
  33. // Reference(s):
  34. // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
  35. //
  36. const Vec3 axis = sub(_cylinder.end, _cylinder.pos);
  37. const Vec3 asq = mul(axis, axis);
  38. const Vec3 nsq = mul(asq, 1.0f/dot(axis, axis) );
  39. const Vec3 tmp = sub(1.0f, nsq);
  40. const float inv = 1.0f/(tmp.x*tmp.y*tmp.z);
  41. const Vec3 extent =
  42. {
  43. _cylinder.radius * tmp.x * sqrt( (nsq.x + nsq.y * nsq.z) * inv),
  44. _cylinder.radius * tmp.y * sqrt( (nsq.y + nsq.z * nsq.x) * inv),
  45. _cylinder.radius * tmp.z * sqrt( (nsq.z + nsq.x * nsq.y) * inv),
  46. };
  47. const Vec3 minP = sub(_cylinder.pos, extent);
  48. const Vec3 minE = sub(_cylinder.end, extent);
  49. const Vec3 maxP = add(_cylinder.pos, extent);
  50. const Vec3 maxE = add(_cylinder.end, extent);
  51. _outAabb.min = min(minP, minE);
  52. _outAabb.max = max(maxP, maxE);
  53. }
  54. void toAabb(Aabb& _outAabb, const Disk& _disk)
  55. {
  56. // Reference(s):
  57. // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
  58. //
  59. const Vec3 nsq = mul(_disk.normal, _disk.normal);
  60. const Vec3 one = { 1.0f, 1.0f, 1.0f };
  61. const Vec3 tmp = sub(one, nsq);
  62. const float inv = 1.0f / (tmp.x*tmp.y*tmp.z);
  63. const Vec3 extent =
  64. {
  65. _disk.radius * tmp.x * sqrt( (nsq.x + nsq.y * nsq.z) * inv),
  66. _disk.radius * tmp.y * sqrt( (nsq.y + nsq.z * nsq.x) * inv),
  67. _disk.radius * tmp.z * sqrt( (nsq.z + nsq.x * nsq.y) * inv),
  68. };
  69. _outAabb.min = sub(_disk.center, extent);
  70. _outAabb.max = add(_disk.center, extent);
  71. }
  72. void toAabb(Aabb& _outAabb, const Obb& _obb)
  73. {
  74. Vec3 xyz = { 1.0f, 1.0f, 1.0f };
  75. Vec3 tmp = mul(xyz, _obb.mtx);
  76. _outAabb.min = tmp;
  77. _outAabb.max = tmp;
  78. for (uint32_t ii = 1; ii < 8; ++ii)
  79. {
  80. xyz.x = ii & 1 ? -1.0f : 1.0f;
  81. xyz.y = ii & 2 ? -1.0f : 1.0f;
  82. xyz.z = ii & 4 ? -1.0f : 1.0f;
  83. tmp = mul(xyz, _obb.mtx);
  84. _outAabb.min = min(_outAabb.min, tmp);
  85. _outAabb.max = max(_outAabb.max, tmp);
  86. }
  87. }
  88. void toAabb(Aabb& _outAabb, const Sphere& _sphere)
  89. {
  90. const float radius = _sphere.radius;
  91. _outAabb.min = sub(_sphere.center, radius);
  92. _outAabb.max = add(_sphere.center, radius);
  93. }
  94. void toAabb(Aabb& _outAabb, const Triangle& _triangle)
  95. {
  96. _outAabb.min = min(_triangle.v0, _triangle.v1, _triangle.v2);
  97. _outAabb.max = max(_triangle.v0, _triangle.v1, _triangle.v2);
  98. }
  99. void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx)
  100. {
  101. toObb(_obb, _aabb);
  102. float result[16];
  103. mtxMul(result, _obb.mtx, _mtx);
  104. memCopy(_obb.mtx, result, sizeof(result) );
  105. }
  106. void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  107. {
  108. Vec3 mn, mx;
  109. uint8_t* vertex = (uint8_t*)_vertices;
  110. mn = mx = load<Vec3>(vertex);
  111. vertex += _stride;
  112. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  113. {
  114. const Vec3 pos = load<Vec3>(vertex);
  115. vertex += _stride;
  116. mn = min(pos, mn);
  117. mx = max(pos, mx);
  118. }
  119. _outAabb.min = mn;
  120. _outAabb.max = mx;
  121. }
  122. void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  123. {
  124. Vec3 mn, mx;
  125. uint8_t* vertex = (uint8_t*)_vertices;
  126. mn = mx = mul(load<Vec3>(vertex), _mtx);
  127. vertex += _stride;
  128. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  129. {
  130. Vec3 pos = mul(load<Vec3>(vertex), _mtx);
  131. vertex += _stride;
  132. mn = min(pos, mn);
  133. mx = max(pos, mx);
  134. }
  135. _outAabb.min = mn;
  136. _outAabb.max = mx;
  137. }
  138. float calcAreaAabb(const Aabb& _aabb)
  139. {
  140. const float ww = _aabb.max.x - _aabb.min.x;
  141. const float hh = _aabb.max.y - _aabb.min.y;
  142. const float dd = _aabb.max.z - _aabb.min.z;
  143. return 2.0f * (ww*hh + ww*dd + hh*dd);
  144. }
  145. void aabbExpand(Aabb& _outAabb, float _factor)
  146. {
  147. _outAabb.min.x -= _factor;
  148. _outAabb.min.y -= _factor;
  149. _outAabb.min.z -= _factor;
  150. _outAabb.max.x += _factor;
  151. _outAabb.max.y += _factor;
  152. _outAabb.max.z += _factor;
  153. }
  154. void aabbExpand(Aabb& _outAabb, const Vec3& _pos)
  155. {
  156. _outAabb.min = min(_outAabb.min, _pos);
  157. _outAabb.max = max(_outAabb.max, _pos);
  158. }
  159. void toObb(Obb& _outObb, const Aabb& _aabb)
  160. {
  161. memSet(_outObb.mtx, 0, sizeof(_outObb.mtx) );
  162. _outObb.mtx[ 0] = (_aabb.max.x - _aabb.min.x) * 0.5f;
  163. _outObb.mtx[ 5] = (_aabb.max.y - _aabb.min.y) * 0.5f;
  164. _outObb.mtx[10] = (_aabb.max.z - _aabb.min.z) * 0.5f;
  165. _outObb.mtx[12] = (_aabb.min.x + _aabb.max.x) * 0.5f;
  166. _outObb.mtx[13] = (_aabb.min.y + _aabb.max.y) * 0.5f;
  167. _outObb.mtx[14] = (_aabb.min.z + _aabb.max.z) * 0.5f;
  168. _outObb.mtx[15] = 1.0f;
  169. }
  170. void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps)
  171. {
  172. Aabb aabb;
  173. toAabb(aabb, _vertices, _numVertices, _stride);
  174. float minArea = calcAreaAabb(aabb);
  175. Obb best;
  176. toObb(best, aabb);
  177. float angleStep = float(kPiHalf/_steps);
  178. float ax = 0.0f;
  179. float mtx[16];
  180. for (uint32_t ii = 0; ii < _steps; ++ii)
  181. {
  182. float ay = 0.0f;
  183. for (uint32_t jj = 0; jj < _steps; ++jj)
  184. {
  185. float az = 0.0f;
  186. for (uint32_t kk = 0; kk < _steps; ++kk)
  187. {
  188. mtxRotateXYZ(mtx, ax, ay, az);
  189. float mtxT[16];
  190. mtxTranspose(mtxT, mtx);
  191. toAabb(aabb, mtxT, _vertices, _numVertices, _stride);
  192. float area = calcAreaAabb(aabb);
  193. if (area < minArea)
  194. {
  195. minArea = area;
  196. aabbTransformToObb(best, aabb, mtx);
  197. }
  198. az += angleStep;
  199. }
  200. ay += angleStep;
  201. }
  202. ax += angleStep;
  203. }
  204. memCopy(&_outObb, &best, sizeof(Obb) );
  205. }
  206. void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  207. {
  208. Aabb aabb;
  209. toAabb(aabb, _vertices, _numVertices, _stride);
  210. Vec3 center = getCenter(aabb);
  211. float maxDistSq = 0.0f;
  212. uint8_t* vertex = (uint8_t*)_vertices;
  213. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  214. {
  215. const Vec3& pos = load<Vec3>(vertex);
  216. vertex += _stride;
  217. const Vec3 tmp = sub(pos, center);
  218. const float distSq = dot(tmp, tmp);
  219. maxDistSq = max(distSq, maxDistSq);
  220. }
  221. _sphere.center = center;
  222. _sphere.radius = sqrt(maxDistSq);
  223. }
  224. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
  225. {
  226. RngMwc rng;
  227. uint8_t* vertex = (uint8_t*)_vertices;
  228. Vec3 center;
  229. float* position = (float*)&vertex[0];
  230. center.x = position[0];
  231. center.y = position[1];
  232. center.z = position[2];
  233. position = (float*)&vertex[1*_stride];
  234. center.x += position[0];
  235. center.y += position[1];
  236. center.z += position[2];
  237. center.x *= 0.5f;
  238. center.y *= 0.5f;
  239. center.z *= 0.5f;
  240. float xx = position[0] - center.x;
  241. float yy = position[1] - center.y;
  242. float zz = position[2] - center.z;
  243. float maxDistSq = xx*xx + yy*yy + zz*zz;
  244. float radiusStep = _step * 0.37f;
  245. bool done;
  246. do
  247. {
  248. done = true;
  249. for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
  250. {
  251. position = (float*)&vertex[index*_stride];
  252. xx = position[0] - center.x;
  253. yy = position[1] - center.y;
  254. zz = position[2] - center.z;
  255. float distSq = xx*xx + yy*yy + zz*zz;
  256. if (distSq > maxDistSq)
  257. {
  258. done = false;
  259. center.x += xx * radiusStep;
  260. center.y += yy * radiusStep;
  261. center.z += zz * radiusStep;
  262. maxDistSq = lerp(maxDistSq, distSq, _step);
  263. break;
  264. }
  265. }
  266. } while (!done);
  267. _sphere.center = center;
  268. _sphere.radius = sqrt(maxDistSq);
  269. }
  270. void buildFrustumPlanes(Plane* _result, const float* _viewProj)
  271. {
  272. const float xw = _viewProj[ 3];
  273. const float yw = _viewProj[ 7];
  274. const float zw = _viewProj[11];
  275. const float ww = _viewProj[15];
  276. const float xz = _viewProj[ 2];
  277. const float yz = _viewProj[ 6];
  278. const float zz = _viewProj[10];
  279. const float wz = _viewProj[14];
  280. Plane& near = _result[0];
  281. Plane& far = _result[1];
  282. Plane& left = _result[2];
  283. Plane& right = _result[3];
  284. Plane& top = _result[4];
  285. Plane& bottom = _result[5];
  286. near.normal.x = xw - xz;
  287. near.normal.y = yw - yz;
  288. near.normal.z = zw - zz;
  289. near.dist = ww - wz;
  290. far.normal.x = xw + xz;
  291. far.normal.y = yw + yz;
  292. far.normal.z = zw + zz;
  293. far.dist = ww + wz;
  294. const float xx = _viewProj[ 0];
  295. const float yx = _viewProj[ 4];
  296. const float zx = _viewProj[ 8];
  297. const float wx = _viewProj[12];
  298. left.normal.x = xw - xx;
  299. left.normal.y = yw - yx;
  300. left.normal.z = zw - zx;
  301. left.dist = ww - wx;
  302. right.normal.x = xw + xx;
  303. right.normal.y = yw + yx;
  304. right.normal.z = zw + zx;
  305. right.dist = ww + wx;
  306. const float xy = _viewProj[ 1];
  307. const float yy = _viewProj[ 5];
  308. const float zy = _viewProj[ 9];
  309. const float wy = _viewProj[13];
  310. top.normal.x = xw + xy;
  311. top.normal.y = yw + yy;
  312. top.normal.z = zw + zy;
  313. top.dist = ww + wy;
  314. bottom.normal.x = xw - xy;
  315. bottom.normal.y = yw - yy;
  316. bottom.normal.z = zw - zy;
  317. bottom.dist = ww - wy;
  318. Plane* plane = _result;
  319. for (uint32_t ii = 0; ii < 6; ++ii)
  320. {
  321. const float invLen = 1.0f/length(plane->normal);
  322. plane->normal = normalize(plane->normal);
  323. plane->dist *= invLen;
  324. ++plane;
  325. }
  326. }
  327. Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc)
  328. {
  329. const Vec3 axb = cross(_pa.normal, _pb.normal);
  330. const Vec3 bxc = cross(_pb.normal, _pc.normal);
  331. const Vec3 cxa = cross(_pc.normal, _pa.normal);
  332. const Vec3 tmp0 = mul(bxc, _pa.dist);
  333. const Vec3 tmp1 = mul(cxa, _pb.dist);
  334. const Vec3 tmp2 = mul(axb, _pc.dist);
  335. const Vec3 tmp3 = add(tmp0, tmp1);
  336. const Vec3 tmp4 = add(tmp3, tmp2);
  337. const float denom = dot(_pa.normal, bxc);
  338. const Vec3 result = mul(tmp4, -1.0f/denom);
  339. return result;
  340. }
  341. Ray makeRay(float _x, float _y, const float* _invVp)
  342. {
  343. Ray ray;
  344. const Vec3 near = { _x, _y, 0.0f };
  345. ray.pos = mulH(near, _invVp);
  346. const Vec3 far = { _x, _y, 1.0f };
  347. Vec3 tmp = mulH(far, _invVp);
  348. const Vec3 dir = sub(tmp, ray.pos);
  349. ray.dir = normalize(dir);
  350. return ray;
  351. }
  352. inline Vec3 getPointAt(const Ray& _ray, float _t)
  353. {
  354. return add(mul(_ray.dir, _t), _ray.pos);
  355. }
  356. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
  357. {
  358. const Vec3 invDir = rcp(_ray.dir);
  359. const Vec3 tmp0 = sub(_aabb.min, _ray.pos);
  360. const Vec3 t0 = mul(tmp0, invDir);
  361. const Vec3 tmp1 = sub(_aabb.max, _ray.pos);
  362. const Vec3 t1 = mul(tmp1, invDir);
  363. const Vec3 mn = min(t0, t1);
  364. const Vec3 mx = max(t0, t1);
  365. const float tmin = max(mn.x, mn.y, mn.z);
  366. const float tmax = min(mx.x, mx.y, mx.z);
  367. if (0.0f > tmax
  368. || tmin > tmax)
  369. {
  370. return false;
  371. }
  372. if (NULL != _hit)
  373. {
  374. _hit->plane.normal.x = float( (t1.x == tmin) - (t0.x == tmin) );
  375. _hit->plane.normal.y = float( (t1.y == tmin) - (t0.y == tmin) );
  376. _hit->plane.normal.z = float( (t1.z == tmin) - (t0.z == tmin) );
  377. _hit->plane.dist = tmin;
  378. _hit->pos = getPointAt(_ray, tmin);
  379. }
  380. return true;
  381. }
  382. static constexpr Aabb kUnitAabb =
  383. {
  384. { -1.0f, -1.0f, -1.0f },
  385. { 1.0f, 1.0f, 1.0f },
  386. };
  387. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit)
  388. {
  389. Aabb aabb;
  390. toAabb(aabb, _obb);
  391. if (!intersect(_ray, aabb) )
  392. {
  393. return false;
  394. }
  395. float mtxInv[16];
  396. mtxInverse(mtxInv, _obb.mtx);
  397. Ray obbRay;
  398. obbRay.pos = mul(_ray.pos, mtxInv);
  399. obbRay.dir = mulXyz0(_ray.dir, mtxInv);
  400. if (intersect(obbRay, kUnitAabb, _hit) )
  401. {
  402. if (NULL != _hit)
  403. {
  404. _hit->pos = mul(_hit->pos, _obb.mtx);
  405. const Vec3 tmp = mulXyz0(_hit->plane.normal, _obb.mtx);
  406. _hit->plane.normal = normalize(tmp);
  407. }
  408. return true;
  409. }
  410. return false;
  411. }
  412. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit)
  413. {
  414. Plane plane;
  415. plane.normal = _disk.normal;
  416. plane.dist = -dot(_disk.center, _disk.normal);
  417. Hit tmpHit;
  418. _hit = NULL != _hit ? _hit : &tmpHit;
  419. if (intersect(_ray, plane, _hit) )
  420. {
  421. const Vec3 tmp = sub(_disk.center, _hit->pos);
  422. return dot(tmp, tmp) <= square(_disk.radius);
  423. }
  424. return false;
  425. }
  426. static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit)
  427. {
  428. Vec3 axis = sub(_cylinder.end, _cylinder.pos);
  429. const Vec3 rc = sub(_ray.pos, _cylinder.pos);
  430. const Vec3 dxa = cross(_ray.dir, axis);
  431. const float len = length(dxa);
  432. const Vec3 normal = normalize(dxa);
  433. const float dist = bx::abs(dot(rc, normal) );
  434. if (dist > _cylinder.radius)
  435. {
  436. return false;
  437. }
  438. Vec3 vo = cross(rc, axis);
  439. const float t0 = -dot(vo, normal) / len;
  440. vo = normalize(cross(normal, axis) );
  441. const float rsq = square(_cylinder.radius);
  442. const float ddoto = dot(_ray.dir, vo);
  443. const float ss = t0 - bx::abs(sqrt(rsq - square(dist) ) / ddoto);
  444. if (0.0f > ss)
  445. {
  446. return false;
  447. }
  448. const Vec3 point = getPointAt(_ray, ss);
  449. const float axisLen = length(axis);
  450. axis = normalize(axis);
  451. const float pdota = dot(_cylinder.pos, axis);
  452. const float height = dot(point, axis) - pdota;
  453. if (0.0f < height
  454. && axisLen > height)
  455. {
  456. if (NULL != _hit)
  457. {
  458. const float t1 = height / axisLen;
  459. const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
  460. _hit->pos = point;
  461. const Vec3 tmp = sub(point, pointOnAxis);
  462. _hit->plane.normal = normalize(tmp);
  463. _hit->plane.dist = ss;
  464. }
  465. return true;
  466. }
  467. if (_capsule)
  468. {
  469. const float rdota = dot(_ray.pos, axis);
  470. const float pp = rdota - pdota;
  471. const float t1 = pp / axisLen;
  472. const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
  473. const Vec3 axisToRay = sub(_ray.pos, pointOnAxis);
  474. if (_cylinder.radius < length(axisToRay)
  475. && 0.0f > ss)
  476. {
  477. return false;
  478. }
  479. Sphere sphere;
  480. sphere.radius = _cylinder.radius;
  481. sphere.center = 0.0f >= height
  482. ? _cylinder.pos
  483. : _cylinder.end
  484. ;
  485. return intersect(_ray, sphere, _hit);
  486. }
  487. Plane plane;
  488. Vec3 pos;
  489. if (0.0f >= height)
  490. {
  491. plane.normal = neg(axis);
  492. pos = _cylinder.pos;
  493. }
  494. else
  495. {
  496. plane.normal = axis;
  497. pos = _cylinder.end;
  498. }
  499. plane.dist = -dot(pos, plane.normal);
  500. Hit tmpHit;
  501. _hit = NULL != _hit ? _hit : &tmpHit;
  502. if (intersect(_ray, plane, _hit) )
  503. {
  504. const Vec3 tmp = sub(pos, _hit->pos);
  505. return dot(tmp, tmp) <= rsq;
  506. }
  507. return false;
  508. }
  509. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit)
  510. {
  511. return intersect(_ray, _cylinder, false, _hit);
  512. }
  513. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit)
  514. {
  515. BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) );
  516. return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit);
  517. }
  518. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
  519. {
  520. const Vec3 axis = sub(_cone.pos, _cone.end);
  521. const float len = length(axis);
  522. const Vec3 normal = normalize(axis);
  523. Disk disk;
  524. disk.center = _cone.pos;
  525. disk.normal = normal;
  526. disk.radius = _cone.radius;
  527. Hit tmpInt;
  528. Hit* out = NULL != _hit ? _hit : &tmpInt;
  529. bool hit = intersect(_ray, disk, out);
  530. const Vec3 ro = sub(_ray.pos, _cone.end);
  531. const float hyp = sqrt(square(_cone.radius) + square(len) );
  532. const float cosaSq = square(len/hyp);
  533. const float ndoto = dot(normal, ro);
  534. const float ndotd = dot(normal, _ray.dir);
  535. const float aa = square(ndotd) - cosaSq;
  536. const float bb = 2.0f * (ndotd*ndoto - dot(_ray.dir, ro)*cosaSq);
  537. const float cc = square(ndoto) - dot(ro, ro)*cosaSq;
  538. float det = bb*bb - 4.0f*aa*cc;
  539. if (0.0f > det)
  540. {
  541. return hit;
  542. }
  543. det = sqrt(det);
  544. const float invA2 = 1.0f / (2.0f*aa);
  545. const float t1 = (-bb - det) * invA2;
  546. const float t2 = (-bb + det) * invA2;
  547. float tt = t1;
  548. if (0.0f > t1
  549. || (0.0f < t2 && t2 < t1) )
  550. {
  551. tt = t2;
  552. }
  553. if (0.0f > tt)
  554. {
  555. return hit;
  556. }
  557. const Vec3 hitPos = getPointAt(_ray, tt);
  558. const Vec3 point = sub(hitPos, _cone.end);
  559. const float hh = dot(normal, point);
  560. if (0.0f > hh
  561. || len < hh)
  562. {
  563. return hit;
  564. }
  565. if (NULL != _hit)
  566. {
  567. if (!hit
  568. || tt < _hit->plane.dist)
  569. {
  570. _hit->plane.dist = tt;
  571. _hit->pos = hitPos;
  572. const float scale = hh / dot(point, point);
  573. const Vec3 pointScaled = mul(point, scale);
  574. const Vec3 tmp = sub(pointScaled, normal);
  575. _hit->plane.normal = normalize(tmp);
  576. }
  577. }
  578. return true;
  579. }
  580. bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
  581. {
  582. const float dist = distance(_plane, _ray.pos);
  583. if (0.0f > dist)
  584. {
  585. return false;
  586. }
  587. const float ndotd = dot(_ray.dir, _plane.normal);
  588. if (0.0f < ndotd)
  589. {
  590. return false;
  591. }
  592. if (NULL != _hit)
  593. {
  594. _hit->plane.normal = _plane.normal;
  595. float tt = -dist/ndotd;
  596. _hit->plane.dist = tt;
  597. _hit->pos = getPointAt(_ray, tt);
  598. }
  599. return true;
  600. }
  601. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit)
  602. {
  603. const Vec3 rs = sub(_ray.pos, _sphere.center);
  604. const float bb = dot(rs, _ray.dir);
  605. if (0.0f < bb)
  606. {
  607. return false;
  608. }
  609. const float aa = dot(_ray.dir, _ray.dir);
  610. const float cc = dot(rs, rs) - square(_sphere.radius);
  611. const float discriminant = bb*bb - aa*cc;
  612. if (0.0f >= discriminant)
  613. {
  614. return false;
  615. }
  616. const float sqrtDiscriminant = sqrt(discriminant);
  617. const float invA = 1.0f / aa;
  618. const float tt = -(bb + sqrtDiscriminant)*invA;
  619. if (0.0f >= tt)
  620. {
  621. return false;
  622. }
  623. if (NULL != _hit)
  624. {
  625. _hit->plane.dist = tt;
  626. const Vec3 point = getPointAt(_ray, tt);
  627. _hit->pos = point;
  628. const Vec3 tmp = sub(point, _sphere.center);
  629. _hit->plane.normal = normalize(tmp);
  630. }
  631. return true;
  632. }
  633. bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit)
  634. {
  635. const Vec3 edge10 = sub(_triangle.v1, _triangle.v0);
  636. const Vec3 edge02 = sub(_triangle.v0, _triangle.v2);
  637. const Vec3 normal = cross(edge02, edge10);
  638. const Vec3 vo = sub(_triangle.v0, _ray.pos);
  639. const Vec3 dxo = cross(_ray.dir, vo);
  640. const float det = dot(normal, _ray.dir);
  641. if (0.0f < det)
  642. {
  643. return false;
  644. }
  645. const float invDet = 1.0f/det;
  646. const float bz = dot(dxo, edge02) * invDet;
  647. const float by = dot(dxo, edge10) * invDet;
  648. const float bx = 1.0f - by - bz;
  649. if (0.0f > bx
  650. || 0.0f > by
  651. || 0.0f > bz)
  652. {
  653. return false;
  654. }
  655. if (NULL != _hit)
  656. {
  657. _hit->plane.normal = normalize(normal);
  658. const float tt = dot(normal, vo) * invDet;
  659. _hit->plane.dist = tt;
  660. _hit->pos = getPointAt(_ray, tt);
  661. }
  662. return true;
  663. }
  664. Vec3 barycentric(const Triangle& _triangle, const Vec3& _pos)
  665. {
  666. const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
  667. const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
  668. const Vec3 v2 = sub(_pos, _triangle.v0);
  669. const float dot00 = dot(v0, v0);
  670. const float dot01 = dot(v0, v1);
  671. const float dot02 = dot(v0, v2);
  672. const float dot11 = dot(v1, v1);
  673. const float dot12 = dot(v1, v2);
  674. const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );
  675. const float uu = (dot11*dot02 - dot01*dot12)*invDenom;
  676. const float vv = (dot00*dot12 - dot01*dot02)*invDenom;
  677. const float ww = 1.0f - uu - vv;
  678. return { uu, vv, ww };
  679. }
  680. Vec3 cartesian(const Triangle& _triangle, const Vec3& _uvw)
  681. {
  682. const Vec3 b0 = mul(_triangle.v0, _uvw.x);
  683. const Vec3 b1 = mul(_triangle.v1, _uvw.y);
  684. const Vec3 b2 = mul(_triangle.v2, _uvw.z);
  685. return add(add(b0, b1), b2);
  686. }
  687. void calcPlane(Plane& _outPlane, const Triangle& _triangle)
  688. {
  689. calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2);
  690. }
  691. struct Range1
  692. {
  693. float start;
  694. float end;
  695. };
  696. bool overlap(const Range1& _a, const Range1& _b)
  697. {
  698. return _a.end > _b.start
  699. && _b.end > _a.start
  700. ;
  701. }
  702. float projectToAxis(const Vec3& _axis, const Vec3& _point)
  703. {
  704. return dot(_axis, _point);
  705. }
  706. Range1 projectToAxis(const Vec3& _axis, const Aabb& _aabb)
  707. {
  708. const float extent = bx::abs(dot(abs(_axis), getExtents(_aabb) ) );
  709. const float center = dot( _axis , getCenter (_aabb) );
  710. return
  711. {
  712. center - extent,
  713. center + extent,
  714. };
  715. }
  716. Range1 projectToAxis(const Vec3& _axis, const Triangle& _triangle)
  717. {
  718. const float a0 = dot(_axis, _triangle.v0);
  719. const float a1 = dot(_axis, _triangle.v1);
  720. const float a2 = dot(_axis, _triangle.v2);
  721. return
  722. {
  723. min(a0, a1, a2),
  724. max(a0, a1, a2),
  725. };
  726. }
  727. struct Srt
  728. {
  729. Quaternion rotation;
  730. Vec3 translation;
  731. Vec3 scale;
  732. };
  733. Srt toSrt(const void* _mtx)
  734. {
  735. Srt result;
  736. const float* mtx = (const float*)_mtx;
  737. result.translation = { mtx[12], mtx[13], mtx[14] };
  738. float xx = mtx[ 0];
  739. float xy = mtx[ 1];
  740. float xz = mtx[ 2];
  741. float yx = mtx[ 4];
  742. float yy = mtx[ 5];
  743. float yz = mtx[ 6];
  744. float zx = mtx[ 8];
  745. float zy = mtx[ 9];
  746. float zz = mtx[10];
  747. result.scale =
  748. {
  749. sqrt(xx*xx + xy*xy + xz*xz),
  750. sqrt(yx*yx + yy*yy + yz*yz),
  751. sqrt(zx*zx + zy*zy + zz*zz),
  752. };
  753. const Vec3 invScale = rcp(result.scale);
  754. xx *= invScale.x;
  755. xy *= invScale.x;
  756. xz *= invScale.x;
  757. yx *= invScale.y;
  758. yy *= invScale.y;
  759. yz *= invScale.y;
  760. zx *= invScale.z;
  761. zy *= invScale.z;
  762. zz *= invScale.z;
  763. const float trace = xx + yy + zz;
  764. if (0.0f < trace)
  765. {
  766. const float invS = 0.5f * rsqrt(trace + 1.0f);
  767. result.rotation =
  768. {
  769. (yz - zy) * invS,
  770. (zx - xz) * invS,
  771. (xy - yx) * invS,
  772. 0.25f / invS,
  773. };
  774. }
  775. else
  776. {
  777. if (xx > yy
  778. && xx > zz)
  779. {
  780. const float invS = 0.5f * sqrt(max(1.0f + xx - yy - zz, 1e-8f) );
  781. result.rotation =
  782. {
  783. 0.25f / invS,
  784. (xy + yx) * invS,
  785. (xz + zx) * invS,
  786. (yz - zy) * invS,
  787. };
  788. }
  789. else if (yy > zz)
  790. {
  791. const float invS = 0.5f * sqrt(max(1.0f + yy - xx - zz, 1e-8f) );
  792. result.rotation =
  793. {
  794. (xy + yx) * invS,
  795. 0.25f / invS,
  796. (yz + zy) * invS,
  797. (zx - xz) * invS,
  798. };
  799. }
  800. else
  801. {
  802. const float invS = 0.5f * sqrt(max(1.0f + zz - xx - yy, 1e-8f) );
  803. result.rotation =
  804. {
  805. (xz + zx) * invS,
  806. (yz + zy) * invS,
  807. 0.25f / invS,
  808. (xy - yx) * invS,
  809. };
  810. }
  811. }
  812. return result;
  813. }
  814. struct LineSegment
  815. {
  816. Vec3 pos;
  817. Vec3 end;
  818. };
  819. bool nearZero(float _v)
  820. {
  821. return bx::abs(_v) < 0.0001f;
  822. }
  823. bool nearZero(const Vec3& _v)
  824. {
  825. return nearZero(dot(_v, _v) );
  826. }
  827. bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment _b)
  828. {
  829. // Reference(s):
  830. //
  831. // - The shortest line between two lines in 3D
  832. // https://web.archive.org/web/20120309093234/http://paulbourke.net/geometry/lineline3d/
  833. const Vec3 bd = sub(_b.end, _b.pos);
  834. if (nearZero(bd) )
  835. {
  836. return false;
  837. }
  838. const Vec3 ad = sub(_a.end, _a.pos);
  839. if (nearZero(ad) )
  840. {
  841. return false;
  842. }
  843. const Vec3 ab = sub(_a.pos, _b.pos);
  844. const float d0 = projectToAxis(ab, bd);
  845. const float d1 = projectToAxis(ad, bd);
  846. const float d2 = projectToAxis(ab, ad);
  847. const float d3 = projectToAxis(bd, bd);
  848. const float d4 = projectToAxis(ad, ad);
  849. const float denom = d4*d3 - square(d1);
  850. float ta = 0.0f;
  851. if (!nearZero(denom) )
  852. {
  853. ta = (d0*d1 - d2*d3)/denom;
  854. }
  855. _outTa = ta;
  856. _outTb = (d0+d1*ta)/d3;
  857. return true;
  858. }
  859. Vec3 closestPoint(const LineSegment& _line, const Vec3& _point, float& _outT)
  860. {
  861. const Vec3 axis = sub(_line.end, _line.pos);
  862. const float lengthSq = dot(axis, axis);
  863. const float tt = clamp(projectToAxis(axis, sub(_point, _line.pos) ) / lengthSq, 0.0f, 1.0f);
  864. _outT = tt;
  865. return mad(axis, tt, _line.pos);
  866. }
  867. Vec3 closestPoint(const LineSegment& _line, const Vec3& _point)
  868. {
  869. float ignore;
  870. return closestPoint(_line, _point, ignore);
  871. }
  872. Vec3 closestPoint(const Plane& _plane, const Vec3& _point)
  873. {
  874. const float dist = distance(_plane, _point);
  875. return sub(_point, mul(_plane.normal, dist) );
  876. }
  877. Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point)
  878. {
  879. return clamp(_point, _aabb.min, _aabb.max);
  880. }
  881. Vec3 closestPoint(const Obb& _obb, const Vec3& _point)
  882. {
  883. Srt srt = toSrt(_obb.mtx);
  884. const Vec3 obbSpacePos = mul(sub(_point, srt.translation), invert(srt.rotation) );
  885. Aabb aabb;
  886. toAabb(aabb, srt.scale);
  887. const Vec3 pos = closestPoint(aabb, obbSpacePos);
  888. return add(mul(pos, srt.rotation), srt.translation);
  889. }
  890. Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point)
  891. {
  892. Plane plane;
  893. calcPlane(plane, _triangle);
  894. const Vec3 pos = closestPoint(plane, _point);
  895. const Vec3 uvw = barycentric(_triangle, pos);
  896. return cartesian(_triangle, clamp<Vec3>(uvw, 0.0f, 1.0f) );
  897. }
  898. bool overlap(const Aabb& _aabb, const Vec3& _pos)
  899. {
  900. const Vec3 ac = getCenter(_aabb);
  901. const Vec3 ae = getExtents(_aabb);
  902. const Vec3 abc = bx::abs(sub(ac, _pos) );
  903. return abc.x <= ae.x
  904. && abc.y <= ae.y
  905. && abc.z <= ae.z
  906. ;
  907. }
  908. bool overlap(const Aabb& _aabb, const Sphere& _sphere)
  909. {
  910. return overlap(_sphere, _aabb);
  911. }
  912. uint32_t overlapTestMask(const Aabb& _aabbA, const Aabb& _aabbB)
  913. {
  914. /// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
  915. /// test.
  916. const uint32_t ltMinX = _aabbA.max.x < _aabbB.min.x;
  917. const uint32_t gtMaxX = _aabbA.min.x > _aabbB.max.x;
  918. const uint32_t ltMinY = _aabbA.max.y < _aabbB.min.y;
  919. const uint32_t gtMaxY = _aabbA.min.y > _aabbB.max.y;
  920. const uint32_t ltMinZ = _aabbA.max.z < _aabbB.min.z;
  921. const uint32_t gtMaxZ = _aabbA.min.z > _aabbB.max.z;
  922. return 0
  923. | (ltMinX << 0)
  924. | (gtMaxX << 1)
  925. | (ltMinY << 2)
  926. | (gtMaxY << 3)
  927. | (ltMinZ << 4)
  928. | (gtMaxZ << 5)
  929. ;
  930. }
  931. bool overlap(const Aabb& _aabbA, const Aabb& _aabbB)
  932. {
  933. #if 0
  934. return 0 != overlapTestMask(_aabbA, _aabbB);
  935. #else
  936. const Vec3 ac = getCenter(_aabbA);
  937. const Vec3 bc = getCenter(_aabbB);
  938. const Vec3 abc = bx::abs(sub(ac, bc) );
  939. const Vec3 ae = getExtents(_aabbA);
  940. const Vec3 be = getExtents(_aabbB);
  941. const Vec3 abe = add(ae, be);
  942. return abc.x <= abe.x
  943. && abc.y <= abe.y
  944. && abc.z <= abe.z
  945. ;
  946. #endif // 0
  947. }
  948. bool overlap(const Aabb& _aabb, const Plane& _plane)
  949. {
  950. const Vec3 center = getCenter(_aabb);
  951. const float dist = distance(_plane, center);
  952. const Vec3 extents = getExtents(_aabb);
  953. const Vec3 normal = bx::abs(_plane.normal);
  954. const float radius = dot(extents, normal);
  955. return bx::abs(dist) <= radius;
  956. }
  957. static constexpr Vec3 kAxis[] =
  958. {
  959. { 1.0f, 0.0f, 0.0f },
  960. { 0.0f, 1.0f, 0.0f },
  961. { 0.0f, 0.0f, 1.0f },
  962. };
  963. bool overlap(const Aabb& _aabb, const Triangle& _triangle)
  964. {
  965. Aabb triAabb;
  966. toAabb(triAabb, _triangle);
  967. if (!overlap(_aabb, triAabb) )
  968. {
  969. return false;
  970. }
  971. Plane plane;
  972. calcPlane(plane, _triangle);
  973. if (!overlap(_aabb, plane) )
  974. {
  975. return false;
  976. }
  977. const Vec3 center = getCenter(_aabb);
  978. const Vec3 v0 = sub(_triangle.v0, center);
  979. const Vec3 v1 = sub(_triangle.v1, center);
  980. const Vec3 v2 = sub(_triangle.v2, center);
  981. const Vec3 edge[] =
  982. {
  983. sub(v1, v0),
  984. sub(v2, v1),
  985. sub(v0, v2),
  986. };
  987. for (uint32_t ii = 0; ii < 3; ++ii)
  988. {
  989. for (uint32_t jj = 0; jj < 3; ++jj)
  990. {
  991. const Vec3 axis = cross(kAxis[ii], edge[jj]);
  992. const Range1 aabbR = projectToAxis(axis, _aabb);
  993. const Range1 triR = projectToAxis(axis, _triangle);
  994. if (!overlap(aabbR, triR) )
  995. {
  996. return false;
  997. }
  998. }
  999. }
  1000. return true;
  1001. }
  1002. bool overlap(const Aabb& _aabb, const Cylinder& _cylinder)
  1003. {
  1004. BX_UNUSED(_aabb, _cylinder);
  1005. return false;
  1006. }
  1007. bool overlap(const Aabb& _aabb, const Capsule& _capsule)
  1008. {
  1009. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, getCenter(_aabb) );
  1010. return overlap(_aabb, Sphere{pos, _capsule.radius});
  1011. }
  1012. bool overlap(const Aabb& _aabb, const Cone& _cone)
  1013. {
  1014. float tt;
  1015. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, getCenter(_aabb), tt);
  1016. return overlap(_aabb, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
  1017. }
  1018. bool overlap(const Aabb& _aabb, const Disk& _disk)
  1019. {
  1020. if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) )
  1021. {
  1022. return false;
  1023. }
  1024. Plane plane;
  1025. calcPlane(plane, _disk.normal, _disk.center);
  1026. return overlap(_aabb, plane);
  1027. }
  1028. bool overlap(const Aabb& _aabb, const Obb& _obb)
  1029. {
  1030. BX_UNUSED(_aabb, _obb);
  1031. return false;
  1032. }
  1033. bool overlap(const Capsule& _capsule, const bx::Vec3& _pos)
  1034. {
  1035. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _pos);
  1036. return overlap(Sphere{pos, _capsule.radius}, _pos);
  1037. }
  1038. bool overlap(const Capsule& _capsule, const Sphere& _sphere)
  1039. {
  1040. return overlap(_sphere, _capsule);
  1041. }
  1042. bool overlap(const Capsule& _capsule, const Aabb& _aabb)
  1043. {
  1044. return overlap(_aabb, _capsule);
  1045. }
  1046. bool overlap(const Capsule& _capsule, const bx::Plane& _plane)
  1047. {
  1048. BX_UNUSED(_capsule, _plane);
  1049. return false;
  1050. }
  1051. bool overlap(const Capsule& _capsule, const Triangle& _triangle)
  1052. {
  1053. return overlap(_triangle, _capsule);
  1054. }
  1055. bool overlap(const Capsule& _capsule, const Cylinder& _cylinder)
  1056. {
  1057. BX_UNUSED(_capsule, _cylinder);
  1058. return false;
  1059. }
  1060. bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB)
  1061. {
  1062. float ta, tb;
  1063. if (!intersect(ta, tb, {_capsuleA.pos, _capsuleA.end}, {_capsuleB.pos, _capsuleB.end}) )
  1064. {
  1065. return false;
  1066. }
  1067. if (0.0f <= ta
  1068. && 1.0f >= ta
  1069. && 0.0f <= tb
  1070. && 1.0f >= tb)
  1071. {
  1072. const Vec3 ad = sub(_capsuleA.end, _capsuleA.pos);
  1073. const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos);
  1074. return overlap(
  1075. Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
  1076. , Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius}
  1077. );
  1078. }
  1079. if (0.0f <= ta
  1080. && 1.0f >= ta)
  1081. {
  1082. return overlap(_capsuleA, Sphere{0.0f >= tb ? _capsuleB.pos : _capsuleB.end, _capsuleB.radius});
  1083. }
  1084. if (0.0f <= tb
  1085. && 1.0f >= tb)
  1086. {
  1087. return overlap(_capsuleB, Sphere{0.0f >= ta ? _capsuleA.pos : _capsuleA.end, _capsuleA.radius});
  1088. }
  1089. const Vec3 pa = 0.0f > ta ? _capsuleA.pos : _capsuleA.end;
  1090. const Vec3 pb = 0.0f > tb ? _capsuleB.pos : _capsuleB.end;
  1091. const Vec3 closestA = closestPoint(LineSegment{_capsuleA.pos, _capsuleA.end}, pb);
  1092. const Vec3 closestB = closestPoint(LineSegment{_capsuleB.pos, _capsuleB.end}, pa);
  1093. if (dot(closestA, pb) <= dot(closestB, pa) )
  1094. {
  1095. return overlap(_capsuleA, Sphere{closestB, _capsuleB.radius});
  1096. }
  1097. return overlap(_capsuleB, Sphere{closestA, _capsuleA.radius});
  1098. }
  1099. bool overlap(const Capsule& _capsule, const Cone& _cone)
  1100. {
  1101. BX_UNUSED(_capsule, _cone);
  1102. return false;
  1103. }
  1104. bool overlap(const Capsule& _capsule, const Disk& _disk)
  1105. {
  1106. BX_UNUSED(_capsule, _disk);
  1107. return false;
  1108. }
  1109. bool overlap(const Capsule& _capsule, const Obb& _obb)
  1110. {
  1111. BX_UNUSED(_capsule, _obb);
  1112. return false;
  1113. }
  1114. bool overlap(const Sphere& _sphere, const Vec3& _pos)
  1115. {
  1116. const Vec3 ba = sub(_sphere.center, _pos);
  1117. const float rsq = square(_sphere.radius);
  1118. return dot(ba, ba) <= rsq;
  1119. }
  1120. bool overlap(const Sphere& _sphereA, const Sphere& _sphereB)
  1121. {
  1122. const Vec3 ba = sub(_sphereA.center, _sphereB.center);
  1123. const float rsq = square(_sphereA.radius + _sphereB.radius);
  1124. return dot(ba, ba) <= rsq;
  1125. }
  1126. bool overlap(const Sphere& _sphere, const Aabb& _aabb)
  1127. {
  1128. const Vec3 pos = closestPoint(_aabb, _sphere.center);
  1129. return overlap(_sphere, pos);
  1130. }
  1131. bool overlap(const Sphere& _sphere, const Plane& _plane)
  1132. {
  1133. return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius;
  1134. }
  1135. bool overlap(const Sphere& _sphere, const Triangle& _triangle)
  1136. {
  1137. Plane plane;
  1138. calcPlane(plane, _triangle);
  1139. if (!overlap(_sphere, plane) )
  1140. {
  1141. return false;
  1142. }
  1143. const Vec3 pos = closestPoint(plane, _sphere.center);
  1144. const Vec3 uvw = barycentric(_triangle, pos);
  1145. const float nr = -_sphere.radius;
  1146. return uvw.x >= nr
  1147. && uvw.y >= nr
  1148. && uvw.z >= nr
  1149. ;
  1150. }
  1151. bool overlap(const Sphere& _sphere, const Cylinder& _cylinder)
  1152. {
  1153. BX_UNUSED(_sphere, _cylinder);
  1154. return false;
  1155. }
  1156. bool overlap(const Sphere& _sphere, const Capsule& _capsule)
  1157. {
  1158. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center);
  1159. return overlap(_sphere, Sphere{pos, _capsule.radius});
  1160. }
  1161. bool overlap(const Sphere& _sphere, const Cone& _cone)
  1162. {
  1163. float tt;
  1164. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt);
  1165. return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
  1166. }
  1167. bool overlap(const Sphere& _sphere, const Disk& _disk)
  1168. {
  1169. if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) )
  1170. {
  1171. return false;
  1172. }
  1173. Plane plane;
  1174. calcPlane(plane, _disk.normal, _disk.center);
  1175. return overlap(_sphere, plane);
  1176. }
  1177. bool overlap(const Sphere& _sphere, const Obb& _obb)
  1178. {
  1179. const Vec3 pos = closestPoint(_obb, _sphere.center);
  1180. return overlap(_sphere, pos);
  1181. }
  1182. bool overlap(const Triangle& _triangle, const Vec3& _pos)
  1183. {
  1184. const Vec3 uvw = barycentric(_triangle, _pos);
  1185. return uvw.x >= 0.0f
  1186. && uvw.y >= 0.0f
  1187. && uvw.z >= 0.0f
  1188. ;
  1189. }
  1190. bool overlap(const Triangle& _triangle, const Sphere& _sphere)
  1191. {
  1192. return overlap(_sphere, _triangle);
  1193. }
  1194. bool overlap(const Triangle& _triangle, const Aabb& _aabb)
  1195. {
  1196. return overlap(_aabb, _triangle);
  1197. }
  1198. bool overlap(const Triangle& _triangle, const Plane& _plane)
  1199. {
  1200. const float dist0 = distance(_plane, _triangle.v0);
  1201. const float dist1 = distance(_plane, _triangle.v1);
  1202. const float dist2 = distance(_plane, _triangle.v2);
  1203. const float minDist = min(dist0, dist1, dist2);
  1204. const float maxDist = max(dist0, dist1, dist2);
  1205. return 0.0f > minDist
  1206. && 0.0f < maxDist
  1207. ;
  1208. }
  1209. bool overlap(const Triangle& _triangleA, const Triangle& _triangleB)
  1210. {
  1211. BX_UNUSED(_triangleA, _triangleB);
  1212. return false;
  1213. }
  1214. bool overlap(const Triangle& _triangle, const Cylinder& _cylinder)
  1215. {
  1216. BX_UNUSED(_triangle, _cylinder);
  1217. return false;
  1218. }
  1219. bool overlap(const Triangle& _triangle, const Capsule& _capsule)
  1220. {
  1221. BX_UNUSED(_triangle, _capsule);
  1222. return false;
  1223. }
  1224. bool overlap(const Triangle& _triangle, const Cone& _cone)
  1225. {
  1226. BX_UNUSED(_triangle, _cone);
  1227. return false;
  1228. }
  1229. bool overlap(const Triangle& _triangle, const Disk& _disk)
  1230. {
  1231. if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) )
  1232. {
  1233. return false;
  1234. }
  1235. Plane plane;
  1236. calcPlane(plane, _disk.normal, _disk.center);
  1237. return overlap(_triangle, plane);
  1238. }
  1239. bool overlap(const Triangle& _triangle, const Obb& _obb)
  1240. {
  1241. Srt srt = toSrt(_obb.mtx);
  1242. const Quaternion invRotation = invert(srt.rotation);
  1243. const Triangle triangle =
  1244. {
  1245. mul(sub(_triangle.v0, srt.translation), invRotation),
  1246. mul(sub(_triangle.v1, srt.translation), invRotation),
  1247. mul(sub(_triangle.v2, srt.translation), invRotation),
  1248. };
  1249. Aabb aabb;
  1250. toAabb(aabb, srt.scale);
  1251. return overlap(triangle, aabb);
  1252. }