bounds.cpp 32 KB

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