bounds.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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 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 mad(_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 vv = (dot11*dot02 - dot01*dot12)*invDenom;
  676. const float ww = (dot00*dot12 - dot01*dot02)*invDenom;
  677. const float uu = 1.0f - vv - ww;
  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. inline Vec3 getPointAt(const LineSegment& _line, float _t)
  820. {
  821. return lerp(_line.pos, _line.end, _t);
  822. }
  823. bool nearZero(float _v)
  824. {
  825. return bx::abs(_v) < 0.0001f;
  826. }
  827. bool nearZero(const Vec3& _v)
  828. {
  829. return nearZero(dot(_v, _v) );
  830. }
  831. bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment _b)
  832. {
  833. // Reference(s):
  834. //
  835. // - The shortest line between two lines in 3D
  836. // https://web.archive.org/web/20120309093234/http://paulbourke.net/geometry/lineline3d/
  837. const Vec3 bd = sub(_b.end, _b.pos);
  838. if (nearZero(bd) )
  839. {
  840. return false;
  841. }
  842. const Vec3 ad = sub(_a.end, _a.pos);
  843. if (nearZero(ad) )
  844. {
  845. return false;
  846. }
  847. const Vec3 ab = sub(_a.pos, _b.pos);
  848. const float d0 = projectToAxis(ab, bd);
  849. const float d1 = projectToAxis(ad, bd);
  850. const float d2 = projectToAxis(ab, ad);
  851. const float d3 = projectToAxis(bd, bd);
  852. const float d4 = projectToAxis(ad, ad);
  853. const float denom = d4*d3 - square(d1);
  854. float ta = 0.0f;
  855. if (!nearZero(denom) )
  856. {
  857. ta = (d0*d1 - d2*d3)/denom;
  858. }
  859. _outTa = ta;
  860. _outTb = (d0+d1*ta)/d3;
  861. return true;
  862. }
  863. float distance(const Plane& _plane, const LineSegment& _line)
  864. {
  865. const float pd = distance(_plane, _line.pos);
  866. const float ed = distance(_plane, _line.end);
  867. return min(max(pd*ed, 0.0f), bx::abs(pd), bx::abs(ed) );
  868. }
  869. Vec3 closestPoint(const LineSegment& _line, const Vec3& _point, float& _outT)
  870. {
  871. const Vec3 axis = sub(_line.end, _line.pos);
  872. const float lengthSq = dot(axis, axis);
  873. const float tt = clamp(projectToAxis(axis, sub(_point, _line.pos) ) / lengthSq, 0.0f, 1.0f);
  874. _outT = tt;
  875. return mad(axis, tt, _line.pos);
  876. }
  877. Vec3 closestPoint(const LineSegment& _line, const Vec3& _point)
  878. {
  879. float ignore;
  880. return closestPoint(_line, _point, ignore);
  881. }
  882. Vec3 closestPoint(const Plane& _plane, const Vec3& _point)
  883. {
  884. const float dist = distance(_plane, _point);
  885. return sub(_point, mul(_plane.normal, dist) );
  886. }
  887. Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point)
  888. {
  889. return clamp(_point, _aabb.min, _aabb.max);
  890. }
  891. Vec3 closestPoint(const Obb& _obb, const Vec3& _point)
  892. {
  893. Srt srt = toSrt(_obb.mtx);
  894. Aabb aabb;
  895. toAabb(aabb, srt.scale);
  896. const Quaternion invRotation = invert(srt.rotation);
  897. const Vec3 obbSpacePos = mul(sub(_point, srt.translation), invRotation);
  898. const Vec3 pos = closestPoint(aabb, obbSpacePos);
  899. return add(mul(pos, srt.rotation), srt.translation);
  900. }
  901. Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point)
  902. {
  903. Plane plane;
  904. calcPlane(plane, _triangle);
  905. const Vec3 pos = closestPoint(plane, _point);
  906. const Vec3 uvw = barycentric(_triangle, pos);
  907. return cartesian(_triangle, clamp<Vec3>(uvw, 0.0f, 1.0f) );
  908. }
  909. bool overlap(const Aabb& _aabb, const Vec3& _pos)
  910. {
  911. const Vec3 ac = getCenter(_aabb);
  912. const Vec3 ae = getExtents(_aabb);
  913. const Vec3 abc = bx::abs(sub(ac, _pos) );
  914. return abc.x <= ae.x
  915. && abc.y <= ae.y
  916. && abc.z <= ae.z
  917. ;
  918. }
  919. bool overlap(const Aabb& _aabb, const Sphere& _sphere)
  920. {
  921. return overlap(_sphere, _aabb);
  922. }
  923. uint32_t overlapTestMask(const Aabb& _aabbA, const Aabb& _aabbB)
  924. {
  925. /// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
  926. /// test.
  927. const uint32_t ltMinX = _aabbA.max.x < _aabbB.min.x;
  928. const uint32_t gtMaxX = _aabbA.min.x > _aabbB.max.x;
  929. const uint32_t ltMinY = _aabbA.max.y < _aabbB.min.y;
  930. const uint32_t gtMaxY = _aabbA.min.y > _aabbB.max.y;
  931. const uint32_t ltMinZ = _aabbA.max.z < _aabbB.min.z;
  932. const uint32_t gtMaxZ = _aabbA.min.z > _aabbB.max.z;
  933. return 0
  934. | (ltMinX << 0)
  935. | (gtMaxX << 1)
  936. | (ltMinY << 2)
  937. | (gtMaxY << 3)
  938. | (ltMinZ << 4)
  939. | (gtMaxZ << 5)
  940. ;
  941. }
  942. bool overlap(const Aabb& _aabbA, const Aabb& _aabbB)
  943. {
  944. #if 0
  945. return 0 != overlapTestMask(_aabbA, _aabbB);
  946. #else
  947. const Vec3 ac = getCenter(_aabbA);
  948. const Vec3 bc = getCenter(_aabbB);
  949. const Vec3 abc = bx::abs(sub(ac, bc) );
  950. const Vec3 ae = getExtents(_aabbA);
  951. const Vec3 be = getExtents(_aabbB);
  952. const Vec3 abe = add(ae, be);
  953. return abc.x <= abe.x
  954. && abc.y <= abe.y
  955. && abc.z <= abe.z
  956. ;
  957. #endif // 0
  958. }
  959. bool overlap(const Aabb& _aabb, const Plane& _plane)
  960. {
  961. const Vec3 center = getCenter(_aabb);
  962. const float dist = distance(_plane, center);
  963. const Vec3 extents = getExtents(_aabb);
  964. const Vec3 normal = bx::abs(_plane.normal);
  965. const float radius = dot(extents, normal);
  966. return bx::abs(dist) <= radius;
  967. }
  968. static constexpr Vec3 kAxis[] =
  969. {
  970. { 1.0f, 0.0f, 0.0f },
  971. { 0.0f, 1.0f, 0.0f },
  972. { 0.0f, 0.0f, 1.0f },
  973. };
  974. bool overlap(const Aabb& _aabb, const Triangle& _triangle)
  975. {
  976. Aabb triAabb;
  977. toAabb(triAabb, _triangle);
  978. if (!overlap(_aabb, triAabb) )
  979. {
  980. return false;
  981. }
  982. Plane plane;
  983. calcPlane(plane, _triangle);
  984. if (!overlap(_aabb, plane) )
  985. {
  986. return false;
  987. }
  988. const Vec3 center = getCenter(_aabb);
  989. const Vec3 v0 = sub(_triangle.v0, center);
  990. const Vec3 v1 = sub(_triangle.v1, center);
  991. const Vec3 v2 = sub(_triangle.v2, center);
  992. const Vec3 edge[] =
  993. {
  994. sub(v1, v0),
  995. sub(v2, v1),
  996. sub(v0, v2),
  997. };
  998. for (uint32_t ii = 0; ii < 3; ++ii)
  999. {
  1000. for (uint32_t jj = 0; jj < 3; ++jj)
  1001. {
  1002. const Vec3 axis = cross(kAxis[ii], edge[jj]);
  1003. const Range1 aabbR = projectToAxis(axis, _aabb);
  1004. const Range1 triR = projectToAxis(axis, _triangle);
  1005. if (!overlap(aabbR, triR) )
  1006. {
  1007. return false;
  1008. }
  1009. }
  1010. }
  1011. return true;
  1012. }
  1013. bool overlap(const Aabb& _aabb, const Cylinder& _cylinder)
  1014. {
  1015. BX_UNUSED(_aabb, _cylinder);
  1016. return false;
  1017. }
  1018. bool overlap(const Aabb& _aabb, const Capsule& _capsule)
  1019. {
  1020. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, getCenter(_aabb) );
  1021. return overlap(_aabb, Sphere{pos, _capsule.radius});
  1022. }
  1023. bool overlap(const Aabb& _aabb, const Cone& _cone)
  1024. {
  1025. float tt;
  1026. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, getCenter(_aabb), tt);
  1027. return overlap(_aabb, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
  1028. }
  1029. bool overlap(const Aabb& _aabb, const Disk& _disk)
  1030. {
  1031. if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) )
  1032. {
  1033. return false;
  1034. }
  1035. Plane plane;
  1036. calcPlane(plane, _disk.normal, _disk.center);
  1037. return overlap(_aabb, plane);
  1038. }
  1039. bool overlap(const Aabb& _aabb, const Obb& _obb)
  1040. {
  1041. BX_UNUSED(_aabb, _obb);
  1042. return false;
  1043. }
  1044. bool overlap(const Capsule& _capsule, const Vec3& _pos)
  1045. {
  1046. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _pos);
  1047. return overlap(Sphere{pos, _capsule.radius}, _pos);
  1048. }
  1049. bool overlap(const Capsule& _capsule, const Sphere& _sphere)
  1050. {
  1051. return overlap(_sphere, _capsule);
  1052. }
  1053. bool overlap(const Capsule& _capsule, const Aabb& _aabb)
  1054. {
  1055. return overlap(_aabb, _capsule);
  1056. }
  1057. bool overlap(const Capsule& _capsule, const Plane& _plane)
  1058. {
  1059. return distance(_plane, LineSegment{_capsule.pos, _capsule.end}) <= _capsule.radius;
  1060. }
  1061. bool overlap(const Capsule& _capsule, const Triangle& _triangle)
  1062. {
  1063. return overlap(_triangle, _capsule);
  1064. }
  1065. bool overlap(const Capsule& _capsule, const Cylinder& _cylinder)
  1066. {
  1067. BX_UNUSED(_capsule, _cylinder);
  1068. return false;
  1069. }
  1070. bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB)
  1071. {
  1072. float ta, tb;
  1073. if (!intersect(ta, tb, {_capsuleA.pos, _capsuleA.end}, {_capsuleB.pos, _capsuleB.end}) )
  1074. {
  1075. return false;
  1076. }
  1077. if (0.0f <= ta
  1078. && 1.0f >= ta
  1079. && 0.0f <= tb
  1080. && 1.0f >= tb)
  1081. {
  1082. const Vec3 ad = sub(_capsuleA.end, _capsuleA.pos);
  1083. const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos);
  1084. return overlap(
  1085. Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
  1086. , Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius}
  1087. );
  1088. }
  1089. if (0.0f <= ta
  1090. && 1.0f >= ta)
  1091. {
  1092. return overlap(_capsuleA, Sphere{0.0f >= tb ? _capsuleB.pos : _capsuleB.end, _capsuleB.radius});
  1093. }
  1094. if (0.0f <= tb
  1095. && 1.0f >= tb)
  1096. {
  1097. return overlap(_capsuleB, Sphere{0.0f >= ta ? _capsuleA.pos : _capsuleA.end, _capsuleA.radius});
  1098. }
  1099. const Vec3 pa = 0.0f > ta ? _capsuleA.pos : _capsuleA.end;
  1100. const Vec3 pb = 0.0f > tb ? _capsuleB.pos : _capsuleB.end;
  1101. const Vec3 closestA = closestPoint(LineSegment{_capsuleA.pos, _capsuleA.end}, pb);
  1102. const Vec3 closestB = closestPoint(LineSegment{_capsuleB.pos, _capsuleB.end}, pa);
  1103. if (dot(closestA, pb) <= dot(closestB, pa) )
  1104. {
  1105. return overlap(_capsuleA, Sphere{closestB, _capsuleB.radius});
  1106. }
  1107. return overlap(_capsuleB, Sphere{closestA, _capsuleA.radius});
  1108. }
  1109. bool overlap(const Capsule& _capsule, const Cone& _cone)
  1110. {
  1111. BX_UNUSED(_capsule, _cone);
  1112. return false;
  1113. }
  1114. bool overlap(const Capsule& _capsule, const Disk& _disk)
  1115. {
  1116. BX_UNUSED(_capsule, _disk);
  1117. return false;
  1118. }
  1119. bool overlap(const Capsule& _capsule, const Obb& _obb)
  1120. {
  1121. return overlap(_obb, _capsule);
  1122. }
  1123. bool overlap(const Cone& _cone, const Vec3& _pos)
  1124. {
  1125. BX_UNUSED(_cone, _pos);
  1126. return false;
  1127. }
  1128. bool overlap(const Cone& _cone, const Sphere& _sphere)
  1129. {
  1130. return overlap(_sphere, _cone);
  1131. }
  1132. bool overlap(const Cone& _cone, const Aabb& _aabb)
  1133. {
  1134. return overlap(_aabb, _cone);
  1135. }
  1136. bool overlap(const Cone& _cone, const Plane& _plane)
  1137. {
  1138. BX_UNUSED(_cone, _plane);
  1139. return false;
  1140. }
  1141. bool overlap(const Cone& _cone, const Triangle& _triangle)
  1142. {
  1143. BX_UNUSED(_cone, _triangle);
  1144. return false;
  1145. }
  1146. bool overlap(const Cone& _cone, const Cylinder& _cylinder)
  1147. {
  1148. BX_UNUSED(_cone, _cylinder);
  1149. return false;
  1150. }
  1151. bool overlap(const Cone& _cone, const Capsule& _capsule)
  1152. {
  1153. BX_UNUSED(_cone, _capsule);
  1154. return false;
  1155. }
  1156. bool overlap(const Cone& _coneA, const Cone& _coneB)
  1157. {
  1158. BX_UNUSED(_coneA, _coneB);
  1159. return false;
  1160. }
  1161. bool overlap(const Cone& _cone, const Disk& _disk)
  1162. {
  1163. BX_UNUSED(_cone, _disk);
  1164. return false;
  1165. }
  1166. bool overlap(const Cone& _cone, const Obb& _obb)
  1167. {
  1168. BX_UNUSED(_cone, _obb);
  1169. return false;
  1170. }
  1171. bool overlap(const Cylinder& _cylinder, const Vec3& _pos)
  1172. {
  1173. BX_UNUSED(_cylinder, _pos);
  1174. return false;
  1175. }
  1176. bool overlap(const Cylinder& _cylinder, const Sphere& _sphere)
  1177. {
  1178. BX_UNUSED(_cylinder, _sphere);
  1179. return false;
  1180. }
  1181. bool overlap(const Cylinder& _cylinder, const Aabb& _aabb)
  1182. {
  1183. BX_UNUSED(_cylinder, _aabb);
  1184. return false;
  1185. }
  1186. bool overlap(const Cylinder& _cylinder, const Plane& _plane)
  1187. {
  1188. BX_UNUSED(_cylinder, _plane);
  1189. return false;
  1190. }
  1191. bool overlap(const Cylinder& _cylinder, const Triangle& _triangle)
  1192. {
  1193. BX_UNUSED(_cylinder, _triangle);
  1194. return false;
  1195. }
  1196. bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB)
  1197. {
  1198. BX_UNUSED(_cylinderA, _cylinderB);
  1199. return false;
  1200. }
  1201. bool overlap(const Cylinder& _cylinder, const Capsule& _capsule)
  1202. {
  1203. BX_UNUSED(_cylinder, _capsule);
  1204. return false;
  1205. }
  1206. bool overlap(const Cylinder& _cylinder, const Cone& _cone)
  1207. {
  1208. BX_UNUSED(_cylinder, _cone);
  1209. return false;
  1210. }
  1211. bool overlap(const Cylinder& _cylinder, const Disk& _disk)
  1212. {
  1213. BX_UNUSED(_cylinder, _disk);
  1214. return false;
  1215. }
  1216. bool overlap(const Cylinder& _cylinder, const Obb& _obb)
  1217. {
  1218. BX_UNUSED(_cylinder, _obb);
  1219. return false;
  1220. }
  1221. bool overlap(const Disk& _disk, const Vec3& _pos)
  1222. {
  1223. Plane plane;
  1224. calcPlane(plane, _disk.normal, _disk.center);
  1225. if (!nearZero(distance(plane, _pos) ) )
  1226. {
  1227. return false;
  1228. }
  1229. return distanceSq(_disk.center, _pos) <= square(_disk.radius);
  1230. }
  1231. bool overlap(const Disk& _disk, const Sphere& _sphere)
  1232. {
  1233. return overlap(_sphere, _disk);
  1234. }
  1235. bool overlap(const Disk& _disk, const Aabb& _aabb)
  1236. {
  1237. return overlap(_aabb, _disk);
  1238. }
  1239. bool overlap(const Disk& _disk, const Plane& _plane)
  1240. {
  1241. BX_UNUSED(_disk, _plane);
  1242. return false;
  1243. }
  1244. bool overlap(const Disk& _disk, const Triangle& _triangle)
  1245. {
  1246. return overlap(_triangle, _disk);
  1247. }
  1248. bool overlap(const Disk& _disk, const Cylinder& _cylinder)
  1249. {
  1250. BX_UNUSED(_disk, _cylinder);
  1251. return false;
  1252. }
  1253. bool overlap(const Disk& _disk, const Capsule& _capsule)
  1254. {
  1255. return overlap(_capsule, _disk);
  1256. }
  1257. bool overlap(const Disk& _disk, const Cone& _cone)
  1258. {
  1259. BX_UNUSED(_disk, _cone);
  1260. return false;
  1261. }
  1262. bool overlap(const Disk& _diskA, const Disk& _diskB)
  1263. {
  1264. BX_UNUSED(_diskA, _diskB);
  1265. return false;
  1266. }
  1267. bool overlap(const Disk& _disk, const Obb& _obb)
  1268. {
  1269. BX_UNUSED(_disk, _obb);
  1270. return false;
  1271. }
  1272. bool overlap(const Obb& _obb, const Vec3& _pos)
  1273. {
  1274. Srt srt = toSrt(_obb.mtx);
  1275. Aabb aabb;
  1276. toAabb(aabb, srt.scale);
  1277. const Quaternion invRotation = invert(srt.rotation);
  1278. const Vec3 pos = mul(sub(_pos, srt.translation), invRotation);
  1279. return overlap(aabb, pos);
  1280. }
  1281. bool overlap(const Obb& _obb, const Sphere& _sphere)
  1282. {
  1283. return overlap(_sphere, _obb);
  1284. }
  1285. bool overlap(const Obb& _obb, const Aabb& _aabb)
  1286. {
  1287. return overlap(_aabb, _obb);
  1288. }
  1289. bool overlap(const Obb& _obb, const Plane& _plane)
  1290. {
  1291. BX_UNUSED(_obb, _plane);
  1292. return false;
  1293. }
  1294. bool overlap(const Obb& _obb, const Triangle& _triangle)
  1295. {
  1296. return overlap(_triangle, _obb);
  1297. }
  1298. bool overlap(const Obb& _obb, const Cylinder& _cylinder)
  1299. {
  1300. BX_UNUSED(_obb, _cylinder);
  1301. return false;
  1302. }
  1303. bool overlap(const Obb& _obb, const Capsule& _capsule)
  1304. {
  1305. Srt srt = toSrt(_obb.mtx);
  1306. Aabb aabb;
  1307. toAabb(aabb, srt.scale);
  1308. const Quaternion invRotation = invert(srt.rotation);
  1309. Capsule capsule =
  1310. {
  1311. mul(sub(_capsule.pos, srt.translation), invRotation),
  1312. mul(sub(_capsule.end, srt.translation), invRotation),
  1313. _capsule.radius,
  1314. };
  1315. return overlap(aabb, capsule);
  1316. }
  1317. bool overlap(const Obb& _obb, const Cone& _cone)
  1318. {
  1319. BX_UNUSED(_obb, _cone);
  1320. return false;
  1321. }
  1322. bool overlap(const Obb& _obb, const Disk& _disk)
  1323. {
  1324. BX_UNUSED(_obb, _disk);
  1325. return false;
  1326. }
  1327. bool overlap(const Obb& _obbA, const Obb& _obbB)
  1328. {
  1329. BX_UNUSED(_obbA, _obbB);
  1330. return false;
  1331. }
  1332. bool overlap(const Plane& _plane, const Vec3& _pos)
  1333. {
  1334. BX_UNUSED(_plane, _pos);
  1335. return false;
  1336. }
  1337. bool overlap(const Plane& _plane, const Sphere& _sphere)
  1338. {
  1339. return overlap(_sphere, _plane);
  1340. }
  1341. bool overlap(const Plane& _plane, const Aabb& _aabb)
  1342. {
  1343. return overlap(_aabb, _plane);
  1344. }
  1345. bool overlap(const Plane& _planeA, const Plane& _planeB)
  1346. {
  1347. BX_UNUSED(_planeA, _planeB);
  1348. return false;
  1349. }
  1350. bool overlap(const Plane& _plane, const Triangle& _triangle)
  1351. {
  1352. return overlap(_triangle, _plane);
  1353. }
  1354. bool overlap(const Plane& _plane, const Cylinder& _cylinder)
  1355. {
  1356. BX_UNUSED(_plane, _cylinder);
  1357. return false;
  1358. }
  1359. bool overlap(const Plane& _plane, const Capsule& _capsule)
  1360. {
  1361. return overlap(_capsule, _plane);
  1362. }
  1363. bool overlap(const Plane& _plane, const Cone& _cone)
  1364. {
  1365. BX_UNUSED(_plane, _cone);
  1366. return false;
  1367. }
  1368. bool overlap(const Plane& _plane, const Disk& _disk)
  1369. {
  1370. BX_UNUSED(_plane, _disk);
  1371. return false;
  1372. }
  1373. bool overlap(const Plane& _plane, const Obb& _obb)
  1374. {
  1375. return overlap(_obb, _plane);
  1376. }
  1377. bool overlap(const Sphere& _sphere, const Vec3& _pos)
  1378. {
  1379. const Vec3 ba = sub(_sphere.center, _pos);
  1380. const float rsq = square(_sphere.radius);
  1381. return dot(ba, ba) <= rsq;
  1382. }
  1383. bool overlap(const Sphere& _sphereA, const Sphere& _sphereB)
  1384. {
  1385. const Vec3 ba = sub(_sphereA.center, _sphereB.center);
  1386. const float rsq = square(_sphereA.radius + _sphereB.radius);
  1387. return dot(ba, ba) <= rsq;
  1388. }
  1389. bool overlap(const Sphere& _sphere, const Aabb& _aabb)
  1390. {
  1391. const Vec3 pos = closestPoint(_aabb, _sphere.center);
  1392. return overlap(_sphere, pos);
  1393. }
  1394. bool overlap(const Sphere& _sphere, const Plane& _plane)
  1395. {
  1396. return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius;
  1397. }
  1398. bool overlap(const Sphere& _sphere, const Triangle& _triangle)
  1399. {
  1400. Plane plane;
  1401. calcPlane(plane, _triangle);
  1402. if (!overlap(_sphere, plane) )
  1403. {
  1404. return false;
  1405. }
  1406. const Vec3 pos = closestPoint(plane, _sphere.center);
  1407. const Vec3 uvw = barycentric(_triangle, pos);
  1408. const float nr = -_sphere.radius;
  1409. return uvw.x >= nr
  1410. && uvw.y >= nr
  1411. && uvw.z >= nr
  1412. ;
  1413. }
  1414. bool overlap(const Sphere& _sphere, const Cylinder& _cylinder)
  1415. {
  1416. BX_UNUSED(_sphere, _cylinder);
  1417. return false;
  1418. }
  1419. bool overlap(const Sphere& _sphere, const Capsule& _capsule)
  1420. {
  1421. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center);
  1422. return overlap(_sphere, Sphere{pos, _capsule.radius});
  1423. }
  1424. bool overlap(const Sphere& _sphere, const Cone& _cone)
  1425. {
  1426. float tt;
  1427. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt);
  1428. return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
  1429. }
  1430. bool overlap(const Sphere& _sphere, const Disk& _disk)
  1431. {
  1432. if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) )
  1433. {
  1434. return false;
  1435. }
  1436. Plane plane;
  1437. calcPlane(plane, _disk.normal, _disk.center);
  1438. return overlap(_sphere, plane);
  1439. }
  1440. bool overlap(const Sphere& _sphere, const Obb& _obb)
  1441. {
  1442. const Vec3 pos = closestPoint(_obb, _sphere.center);
  1443. return overlap(_sphere, pos);
  1444. }
  1445. bool overlap(const Triangle& _triangle, const Vec3& _pos)
  1446. {
  1447. const Vec3 uvw = barycentric(_triangle, _pos);
  1448. return uvw.x >= 0.0f
  1449. && uvw.y >= 0.0f
  1450. && uvw.z >= 0.0f
  1451. ;
  1452. }
  1453. bool overlap(const Triangle& _triangle, const Sphere& _sphere)
  1454. {
  1455. return overlap(_sphere, _triangle);
  1456. }
  1457. bool overlap(const Triangle& _triangle, const Aabb& _aabb)
  1458. {
  1459. return overlap(_aabb, _triangle);
  1460. }
  1461. bool overlap(const Triangle& _triangle, const Plane& _plane)
  1462. {
  1463. const float dist0 = distance(_plane, _triangle.v0);
  1464. const float dist1 = distance(_plane, _triangle.v1);
  1465. const float dist2 = distance(_plane, _triangle.v2);
  1466. const float minDist = min(dist0, dist1, dist2);
  1467. const float maxDist = max(dist0, dist1, dist2);
  1468. return 0.0f > minDist
  1469. && 0.0f < maxDist
  1470. ;
  1471. }
  1472. bool overlap(const Triangle& _triangleA, const Triangle& _triangleB)
  1473. {
  1474. BX_UNUSED(_triangleA, _triangleB);
  1475. return false;
  1476. }
  1477. bool overlap(const Triangle& _triangle, const Cylinder& _cylinder)
  1478. {
  1479. BX_UNUSED(_triangle, _cylinder);
  1480. return false;
  1481. }
  1482. bool intersect(const LineSegment& _line, const Plane& _plane, Hit* _hit)
  1483. {
  1484. const float dist = distance(_plane, _line.pos);
  1485. const float flip = sign(dist);
  1486. const Vec3 dir = normalize(sub(_line.end, _line.pos) );
  1487. const float ndotd = dot(dir, _plane.normal);
  1488. const float tt = -dist/ndotd;
  1489. const float len = length(sub(_line.end, _line.pos) );
  1490. if (tt < 0.0f || tt > len)
  1491. {
  1492. return false;
  1493. }
  1494. if (NULL != _hit)
  1495. {
  1496. _hit->pos = mad(dir, tt, _line.pos);
  1497. _hit->plane.normal = mul(_plane.normal, flip);
  1498. _hit->plane.dist = -dot(_hit->plane.normal, _hit->pos);
  1499. }
  1500. return true;
  1501. }
  1502. bool overlap(const Triangle& _triangle, const Capsule& _capsule)
  1503. {
  1504. Plane plane;
  1505. calcPlane(plane, _triangle);
  1506. plane.normal = neg(plane.normal);
  1507. plane.dist = -plane.dist;
  1508. const LineSegment line =
  1509. {
  1510. _capsule.pos,
  1511. _capsule.end,
  1512. };
  1513. Hit hit;
  1514. if (intersect(line, plane, &hit) )
  1515. {
  1516. return true;
  1517. }
  1518. const Vec3 pos = closestPoint(plane, hit.pos);
  1519. const Vec3 uvw = barycentric(_triangle, pos);
  1520. const float nr = -_capsule.radius;
  1521. if (uvw.x >= nr
  1522. && uvw.y >= nr
  1523. && uvw.z >= nr)
  1524. {
  1525. return true;
  1526. }
  1527. const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1};
  1528. const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2};
  1529. const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0};
  1530. float ta0, tb0;
  1531. const bool i0 = intersect(ta0, tb0, ab, line);
  1532. float ta1, tb1;
  1533. const bool i1 = intersect(ta1, tb1, bc, line);
  1534. float ta2, tb2;
  1535. const bool i2 = intersect(ta2, tb2, ca, line);
  1536. if (!i0
  1537. || !i1
  1538. || !i2)
  1539. {
  1540. return false;
  1541. }
  1542. ta0 = clamp(ta0, 0.0f, 1.0f);
  1543. ta1 = clamp(ta1, 0.0f, 1.0f);
  1544. ta2 = clamp(ta2, 0.0f, 1.0f);
  1545. tb0 = clamp(tb0, 0.0f, 1.0f);
  1546. tb1 = clamp(tb1, 0.0f, 1.0f);
  1547. tb2 = clamp(tb2, 0.0f, 1.0f);
  1548. const Vec3 pa0 = getPointAt(ab, ta0);
  1549. const Vec3 pa1 = getPointAt(bc, ta1);
  1550. const Vec3 pa2 = getPointAt(ca, ta2);
  1551. const Vec3 pb0 = getPointAt(line, tb0);
  1552. const Vec3 pb1 = getPointAt(line, tb1);
  1553. const Vec3 pb2 = getPointAt(line, tb2);
  1554. const float d0 = distanceSq(pa0, pb0);
  1555. const float d1 = distanceSq(pa1, pb1);
  1556. const float d2 = distanceSq(pa2, pb2);
  1557. if (d0 <= d1
  1558. && d0 <= d2)
  1559. {
  1560. return overlap(_capsule, pa0);
  1561. }
  1562. else if (d1 <= d2)
  1563. {
  1564. return overlap(_capsule, pa1);
  1565. }
  1566. return overlap(_capsule, pa2);
  1567. }
  1568. bool overlap(const Triangle& _triangle, const Cone& _cone)
  1569. {
  1570. BX_UNUSED(_triangle, _cone);
  1571. return false;
  1572. }
  1573. bool overlap(const Triangle& _triangle, const Disk& _disk)
  1574. {
  1575. if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) )
  1576. {
  1577. return false;
  1578. }
  1579. Plane plane;
  1580. calcPlane(plane, _disk.normal, _disk.center);
  1581. return overlap(_triangle, plane);
  1582. }
  1583. bool overlap(const Triangle& _triangle, const Obb& _obb)
  1584. {
  1585. Srt srt = toSrt(_obb.mtx);
  1586. Aabb aabb;
  1587. toAabb(aabb, srt.scale);
  1588. const Quaternion invRotation = invert(srt.rotation);
  1589. const Triangle triangle =
  1590. {
  1591. mul(sub(_triangle.v0, srt.translation), invRotation),
  1592. mul(sub(_triangle.v1, srt.translation), invRotation),
  1593. mul(sub(_triangle.v2, srt.translation), invRotation),
  1594. };
  1595. return overlap(triangle, aabb);
  1596. }