bounds.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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. Ray makeRay(float _x, float _y, const float* _invVp)
  328. {
  329. Ray ray;
  330. const Vec3 near = { _x, _y, 0.0f };
  331. ray.pos = mulH(near, _invVp);
  332. const Vec3 far = { _x, _y, 1.0f };
  333. Vec3 tmp = mulH(far, _invVp);
  334. const Vec3 dir = sub(tmp, ray.pos);
  335. ray.dir = normalize(dir);
  336. return ray;
  337. }
  338. inline Vec3 getPointAt(const Ray& _ray, float _t)
  339. {
  340. return mad(_ray.dir, _t, _ray.pos);
  341. }
  342. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
  343. {
  344. const Vec3 invDir = rcp(_ray.dir);
  345. const Vec3 tmp0 = sub(_aabb.min, _ray.pos);
  346. const Vec3 t0 = mul(tmp0, invDir);
  347. const Vec3 tmp1 = sub(_aabb.max, _ray.pos);
  348. const Vec3 t1 = mul(tmp1, invDir);
  349. const Vec3 mn = min(t0, t1);
  350. const Vec3 mx = max(t0, t1);
  351. const float tmin = max(mn.x, mn.y, mn.z);
  352. const float tmax = min(mx.x, mx.y, mx.z);
  353. if (0.0f > tmax
  354. || tmin > tmax)
  355. {
  356. return false;
  357. }
  358. if (NULL != _hit)
  359. {
  360. _hit->plane.normal.x = float( (t1.x == tmin) - (t0.x == tmin) );
  361. _hit->plane.normal.y = float( (t1.y == tmin) - (t0.y == tmin) );
  362. _hit->plane.normal.z = float( (t1.z == tmin) - (t0.z == tmin) );
  363. _hit->plane.dist = tmin;
  364. _hit->pos = getPointAt(_ray, tmin);
  365. }
  366. return true;
  367. }
  368. static constexpr Aabb kUnitAabb =
  369. {
  370. { -1.0f, -1.0f, -1.0f },
  371. { 1.0f, 1.0f, 1.0f },
  372. };
  373. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit)
  374. {
  375. Aabb aabb;
  376. toAabb(aabb, _obb);
  377. if (!intersect(_ray, aabb) )
  378. {
  379. return false;
  380. }
  381. float mtxInv[16];
  382. mtxInverse(mtxInv, _obb.mtx);
  383. Ray obbRay;
  384. obbRay.pos = mul(_ray.pos, mtxInv);
  385. obbRay.dir = mulXyz0(_ray.dir, mtxInv);
  386. if (intersect(obbRay, kUnitAabb, _hit) )
  387. {
  388. if (NULL != _hit)
  389. {
  390. _hit->pos = mul(_hit->pos, _obb.mtx);
  391. const Vec3 tmp = mulXyz0(_hit->plane.normal, _obb.mtx);
  392. _hit->plane.normal = normalize(tmp);
  393. }
  394. return true;
  395. }
  396. return false;
  397. }
  398. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit)
  399. {
  400. Plane plane;
  401. plane.normal = _disk.normal;
  402. plane.dist = -dot(_disk.center, _disk.normal);
  403. Hit tmpHit;
  404. _hit = NULL != _hit ? _hit : &tmpHit;
  405. if (intersect(_ray, plane, _hit) )
  406. {
  407. const Vec3 tmp = sub(_disk.center, _hit->pos);
  408. return dot(tmp, tmp) <= square(_disk.radius);
  409. }
  410. return false;
  411. }
  412. static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit)
  413. {
  414. Vec3 axis = sub(_cylinder.end, _cylinder.pos);
  415. const Vec3 rc = sub(_ray.pos, _cylinder.pos);
  416. const Vec3 dxa = cross(_ray.dir, axis);
  417. const float len = length(dxa);
  418. const Vec3 normal = normalize(dxa);
  419. const float dist = bx::abs(dot(rc, normal) );
  420. if (dist > _cylinder.radius)
  421. {
  422. return false;
  423. }
  424. Vec3 vo = cross(rc, axis);
  425. const float t0 = -dot(vo, normal) / len;
  426. vo = normalize(cross(normal, axis) );
  427. const float rsq = square(_cylinder.radius);
  428. const float ddoto = dot(_ray.dir, vo);
  429. const float ss = t0 - bx::abs(sqrt(rsq - square(dist) ) / ddoto);
  430. if (0.0f > ss)
  431. {
  432. return false;
  433. }
  434. const Vec3 point = getPointAt(_ray, ss);
  435. const float axisLen = length(axis);
  436. axis = normalize(axis);
  437. const float pdota = dot(_cylinder.pos, axis);
  438. const float height = dot(point, axis) - pdota;
  439. if (0.0f < height
  440. && axisLen > height)
  441. {
  442. if (NULL != _hit)
  443. {
  444. const float t1 = height / axisLen;
  445. const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
  446. _hit->pos = point;
  447. const Vec3 tmp = sub(point, pointOnAxis);
  448. _hit->plane.normal = normalize(tmp);
  449. _hit->plane.dist = ss;
  450. }
  451. return true;
  452. }
  453. if (_capsule)
  454. {
  455. const float rdota = dot(_ray.pos, axis);
  456. const float pp = rdota - pdota;
  457. const float t1 = pp / axisLen;
  458. const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
  459. const Vec3 axisToRay = sub(_ray.pos, pointOnAxis);
  460. if (_cylinder.radius < length(axisToRay)
  461. && 0.0f > ss)
  462. {
  463. return false;
  464. }
  465. Sphere sphere;
  466. sphere.radius = _cylinder.radius;
  467. sphere.center = 0.0f >= height
  468. ? _cylinder.pos
  469. : _cylinder.end
  470. ;
  471. return intersect(_ray, sphere, _hit);
  472. }
  473. Plane plane;
  474. Vec3 pos;
  475. if (0.0f >= height)
  476. {
  477. plane.normal = neg(axis);
  478. pos = _cylinder.pos;
  479. }
  480. else
  481. {
  482. plane.normal = axis;
  483. pos = _cylinder.end;
  484. }
  485. plane.dist = -dot(pos, plane.normal);
  486. Hit tmpHit;
  487. _hit = NULL != _hit ? _hit : &tmpHit;
  488. if (intersect(_ray, plane, _hit) )
  489. {
  490. const Vec3 tmp = sub(pos, _hit->pos);
  491. return dot(tmp, tmp) <= rsq;
  492. }
  493. return false;
  494. }
  495. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit)
  496. {
  497. return intersect(_ray, _cylinder, false, _hit);
  498. }
  499. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit)
  500. {
  501. BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) );
  502. return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit);
  503. }
  504. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
  505. {
  506. const Vec3 axis = sub(_cone.pos, _cone.end);
  507. const float len = length(axis);
  508. const Vec3 normal = normalize(axis);
  509. Disk disk;
  510. disk.center = _cone.pos;
  511. disk.normal = normal;
  512. disk.radius = _cone.radius;
  513. Hit tmpInt;
  514. Hit* out = NULL != _hit ? _hit : &tmpInt;
  515. bool hit = intersect(_ray, disk, out);
  516. const Vec3 ro = sub(_ray.pos, _cone.end);
  517. const float hyp = sqrt(square(_cone.radius) + square(len) );
  518. const float cosaSq = square(len/hyp);
  519. const float ndoto = dot(normal, ro);
  520. const float ndotd = dot(normal, _ray.dir);
  521. const float aa = square(ndotd) - cosaSq;
  522. const float bb = 2.0f * (ndotd*ndoto - dot(_ray.dir, ro)*cosaSq);
  523. const float cc = square(ndoto) - dot(ro, ro)*cosaSq;
  524. float det = bb*bb - 4.0f*aa*cc;
  525. if (0.0f > det)
  526. {
  527. return hit;
  528. }
  529. det = sqrt(det);
  530. const float invA2 = 1.0f / (2.0f*aa);
  531. const float t1 = (-bb - det) * invA2;
  532. const float t2 = (-bb + det) * invA2;
  533. float tt = t1;
  534. if (0.0f > t1
  535. || (0.0f < t2 && t2 < t1) )
  536. {
  537. tt = t2;
  538. }
  539. if (0.0f > tt)
  540. {
  541. return hit;
  542. }
  543. const Vec3 hitPos = getPointAt(_ray, tt);
  544. const Vec3 point = sub(hitPos, _cone.end);
  545. const float hh = dot(normal, point);
  546. if (0.0f > hh
  547. || len < hh)
  548. {
  549. return hit;
  550. }
  551. if (NULL != _hit)
  552. {
  553. if (!hit
  554. || tt < _hit->plane.dist)
  555. {
  556. _hit->plane.dist = tt;
  557. _hit->pos = hitPos;
  558. const float scale = hh / dot(point, point);
  559. const Vec3 pointScaled = mul(point, scale);
  560. const Vec3 tmp = sub(pointScaled, normal);
  561. _hit->plane.normal = normalize(tmp);
  562. }
  563. }
  564. return true;
  565. }
  566. bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
  567. {
  568. const float dist = distance(_plane, _ray.pos);
  569. if (0.0f > dist)
  570. {
  571. return false;
  572. }
  573. const float ndotd = dot(_ray.dir, _plane.normal);
  574. if (0.0f < ndotd)
  575. {
  576. return false;
  577. }
  578. if (NULL != _hit)
  579. {
  580. _hit->plane.normal = _plane.normal;
  581. float tt = -dist/ndotd;
  582. _hit->plane.dist = tt;
  583. _hit->pos = getPointAt(_ray, tt);
  584. }
  585. return true;
  586. }
  587. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit)
  588. {
  589. const Vec3 rs = sub(_ray.pos, _sphere.center);
  590. const float bb = dot(rs, _ray.dir);
  591. if (0.0f < bb)
  592. {
  593. return false;
  594. }
  595. const float aa = dot(_ray.dir, _ray.dir);
  596. const float cc = dot(rs, rs) - square(_sphere.radius);
  597. const float discriminant = bb*bb - aa*cc;
  598. if (0.0f >= discriminant)
  599. {
  600. return false;
  601. }
  602. const float sqrtDiscriminant = sqrt(discriminant);
  603. const float invA = 1.0f / aa;
  604. const float tt = -(bb + sqrtDiscriminant)*invA;
  605. if (0.0f >= tt)
  606. {
  607. return false;
  608. }
  609. if (NULL != _hit)
  610. {
  611. _hit->plane.dist = tt;
  612. const Vec3 point = getPointAt(_ray, tt);
  613. _hit->pos = point;
  614. const Vec3 tmp = sub(point, _sphere.center);
  615. _hit->plane.normal = normalize(tmp);
  616. }
  617. return true;
  618. }
  619. bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit)
  620. {
  621. const Vec3 edge10 = sub(_triangle.v1, _triangle.v0);
  622. const Vec3 edge02 = sub(_triangle.v0, _triangle.v2);
  623. const Vec3 normal = cross(edge02, edge10);
  624. const Vec3 vo = sub(_triangle.v0, _ray.pos);
  625. const Vec3 dxo = cross(_ray.dir, vo);
  626. const float det = dot(normal, _ray.dir);
  627. if (0.0f < det)
  628. {
  629. return false;
  630. }
  631. const float invDet = 1.0f/det;
  632. const float bz = dot(dxo, edge02) * invDet;
  633. const float by = dot(dxo, edge10) * invDet;
  634. const float bx = 1.0f - by - bz;
  635. if (0.0f > bx
  636. || 0.0f > by
  637. || 0.0f > bz)
  638. {
  639. return false;
  640. }
  641. if (NULL != _hit)
  642. {
  643. _hit->plane.normal = normalize(normal);
  644. const float tt = dot(normal, vo) * invDet;
  645. _hit->plane.dist = tt;
  646. _hit->pos = getPointAt(_ray, tt);
  647. }
  648. return true;
  649. }
  650. Vec3 barycentric(const Triangle& _triangle, const Vec3& _pos)
  651. {
  652. const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
  653. const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
  654. const Vec3 v2 = sub(_pos, _triangle.v0);
  655. const float dot00 = dot(v0, v0);
  656. const float dot01 = dot(v0, v1);
  657. const float dot02 = dot(v0, v2);
  658. const float dot11 = dot(v1, v1);
  659. const float dot12 = dot(v1, v2);
  660. const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );
  661. const float vv = (dot11*dot02 - dot01*dot12)*invDenom;
  662. const float ww = (dot00*dot12 - dot01*dot02)*invDenom;
  663. const float uu = 1.0f - vv - ww;
  664. return { uu, vv, ww };
  665. }
  666. Vec3 cartesian(const Triangle& _triangle, const Vec3& _uvw)
  667. {
  668. const Vec3 b0 = mul(_triangle.v0, _uvw.x);
  669. const Vec3 b1 = mul(_triangle.v1, _uvw.y);
  670. const Vec3 b2 = mul(_triangle.v2, _uvw.z);
  671. return add(add(b0, b1), b2);
  672. }
  673. void calcPlane(Plane& _outPlane, const Disk& _disk)
  674. {
  675. calcPlane(_outPlane, _disk.normal, _disk.center);
  676. }
  677. void calcPlane(Plane& _outPlane, const Triangle& _triangle)
  678. {
  679. calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2);
  680. }
  681. struct Interval
  682. {
  683. float start;
  684. float end;
  685. };
  686. bool overlap(const Interval& _a, const Interval& _b)
  687. {
  688. return _a.end > _b.start
  689. && _b.end > _a.start
  690. ;
  691. }
  692. float projectToAxis(const Vec3& _axis, const Vec3& _point)
  693. {
  694. return dot(_axis, _point);
  695. }
  696. Interval projectToAxis(const Vec3& _axis, const Aabb& _aabb)
  697. {
  698. const float extent = bx::abs(dot(abs(_axis), getExtents(_aabb) ) );
  699. const float center = dot( _axis , getCenter (_aabb) );
  700. return
  701. {
  702. center - extent,
  703. center + extent,
  704. };
  705. }
  706. Interval projectToAxis(const Vec3& _axis, const Triangle& _triangle)
  707. {
  708. const float a0 = dot(_axis, _triangle.v0);
  709. const float a1 = dot(_axis, _triangle.v1);
  710. const float a2 = dot(_axis, _triangle.v2);
  711. return
  712. {
  713. min(a0, a1, a2),
  714. max(a0, a1, a2),
  715. };
  716. }
  717. struct Srt
  718. {
  719. Quaternion rotation;
  720. Vec3 translation;
  721. Vec3 scale;
  722. };
  723. Srt toSrt(const void* _mtx)
  724. {
  725. Srt result;
  726. const float* mtx = (const float*)_mtx;
  727. result.translation = { mtx[12], mtx[13], mtx[14] };
  728. float xx = mtx[ 0];
  729. float xy = mtx[ 1];
  730. float xz = mtx[ 2];
  731. float yx = mtx[ 4];
  732. float yy = mtx[ 5];
  733. float yz = mtx[ 6];
  734. float zx = mtx[ 8];
  735. float zy = mtx[ 9];
  736. float zz = mtx[10];
  737. result.scale =
  738. {
  739. sqrt(xx*xx + xy*xy + xz*xz),
  740. sqrt(yx*yx + yy*yy + yz*yz),
  741. sqrt(zx*zx + zy*zy + zz*zz),
  742. };
  743. const Vec3 invScale = rcp(result.scale);
  744. xx *= invScale.x;
  745. xy *= invScale.x;
  746. xz *= invScale.x;
  747. yx *= invScale.y;
  748. yy *= invScale.y;
  749. yz *= invScale.y;
  750. zx *= invScale.z;
  751. zy *= invScale.z;
  752. zz *= invScale.z;
  753. const float trace = xx + yy + zz;
  754. if (0.0f < trace)
  755. {
  756. const float invS = 0.5f * rsqrt(trace + 1.0f);
  757. result.rotation =
  758. {
  759. (yz - zy) * invS,
  760. (zx - xz) * invS,
  761. (xy - yx) * invS,
  762. 0.25f / invS,
  763. };
  764. }
  765. else
  766. {
  767. if (xx > yy
  768. && xx > zz)
  769. {
  770. const float invS = 0.5f * sqrt(max(1.0f + xx - yy - zz, 1e-8f) );
  771. result.rotation =
  772. {
  773. 0.25f / invS,
  774. (xy + yx) * invS,
  775. (xz + zx) * invS,
  776. (yz - zy) * invS,
  777. };
  778. }
  779. else if (yy > zz)
  780. {
  781. const float invS = 0.5f * sqrt(max(1.0f + yy - xx - zz, 1e-8f) );
  782. result.rotation =
  783. {
  784. (xy + yx) * invS,
  785. 0.25f / invS,
  786. (yz + zy) * invS,
  787. (zx - xz) * invS,
  788. };
  789. }
  790. else
  791. {
  792. const float invS = 0.5f * sqrt(max(1.0f + zz - xx - yy, 1e-8f) );
  793. result.rotation =
  794. {
  795. (xz + zx) * invS,
  796. (yz + zy) * invS,
  797. 0.25f / invS,
  798. (xy - yx) * invS,
  799. };
  800. }
  801. }
  802. return result;
  803. }
  804. void mtxFromSrt(float* _outMtx, const Srt& _srt)
  805. {
  806. mtxQuat(_outMtx, _srt.rotation);
  807. store<Vec3>(&_outMtx[0], mul(load<Vec3>(&_outMtx[0]), _srt.scale.x) );
  808. store<Vec3>(&_outMtx[4], mul(load<Vec3>(&_outMtx[4]), _srt.scale.y) );
  809. store<Vec3>(&_outMtx[8], mul(load<Vec3>(&_outMtx[8]), _srt.scale.z) );
  810. store<Vec3>(&_outMtx[12], _srt.translation);
  811. }
  812. bool isNearZero(float _v)
  813. {
  814. return equal(_v, 0.0f, 0.00001f);
  815. }
  816. bool isNearZero(const Vec3& _v)
  817. {
  818. return isNearZero(dot(_v, _v) );
  819. }
  820. struct Line
  821. {
  822. Vec3 pos;
  823. Vec3 dir;
  824. };
  825. inline Vec3 getPointAt(const Line& _line, float _t)
  826. {
  827. return mad(_line.dir, _t, _line.pos);
  828. }
  829. bool intersect(Line& _outLine, const Plane& _planeA, const Plane& _planeB)
  830. {
  831. const Vec3 axb = cross(_planeA.normal, _planeB.normal);
  832. const float denom = dot(axb, axb);
  833. if (isNearZero(denom) )
  834. {
  835. return false;
  836. }
  837. const Vec3 bxaxb = cross(_planeB.normal, axb);
  838. const Vec3 axbxa = cross(axb, _planeA.normal);
  839. const Vec3 tmp0 = mul(bxaxb, _planeA.dist);
  840. const Vec3 tmp1 = mul(axbxa, _planeB.dist);
  841. const Vec3 tmp2 = add(tmp0, tmp1);
  842. _outLine.pos = mul(tmp2, -1.0f/denom);
  843. _outLine.dir = normalize(axb);
  844. return true;
  845. }
  846. Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc)
  847. {
  848. const Vec3 axb = cross(_pa.normal, _pb.normal);
  849. const Vec3 bxc = cross(_pb.normal, _pc.normal);
  850. const Vec3 cxa = cross(_pc.normal, _pa.normal);
  851. const Vec3 tmp0 = mul(bxc, _pa.dist);
  852. const Vec3 tmp1 = mul(cxa, _pb.dist);
  853. const Vec3 tmp2 = mul(axb, _pc.dist);
  854. const Vec3 tmp3 = add(tmp0, tmp1);
  855. const Vec3 tmp4 = add(tmp3, tmp2);
  856. const float denom = dot(_pa.normal, bxc);
  857. const Vec3 result = mul(tmp4, -1.0f/denom);
  858. return result;
  859. }
  860. struct LineSegment
  861. {
  862. Vec3 pos;
  863. Vec3 end;
  864. };
  865. inline Vec3 getPointAt(const LineSegment& _line, float _t)
  866. {
  867. return lerp(_line.pos, _line.end, _t);
  868. }
  869. bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment& _b)
  870. {
  871. // Reference(s):
  872. //
  873. // - The shortest line between two lines in 3D
  874. // https://web.archive.org/web/20120309093234/http://paulbourke.net/geometry/lineline3d/
  875. const Vec3 bd = sub(_b.end, _b.pos);
  876. if (isNearZero(bd) )
  877. {
  878. return false;
  879. }
  880. const Vec3 ad = sub(_a.end, _a.pos);
  881. if (isNearZero(ad) )
  882. {
  883. return false;
  884. }
  885. const Vec3 ab = sub(_a.pos, _b.pos);
  886. const float d0 = projectToAxis(ab, bd);
  887. const float d1 = projectToAxis(ad, bd);
  888. const float d2 = projectToAxis(ab, ad);
  889. const float d3 = projectToAxis(bd, bd);
  890. const float d4 = projectToAxis(ad, ad);
  891. const float denom = d4*d3 - square(d1);
  892. float ta = 0.0f;
  893. if (!isNearZero(denom) )
  894. {
  895. ta = (d0*d1 - d2*d3)/denom;
  896. }
  897. _outTa = ta;
  898. _outTb = (d0+d1*ta)/d3;
  899. return true;
  900. }
  901. bool intersect(const LineSegment& _a, const LineSegment& _b)
  902. {
  903. float ta, tb;
  904. if (!intersect(ta, tb, _a, _b) )
  905. {
  906. return false;
  907. }
  908. return 0.0f >= ta
  909. && 1.0f <= ta
  910. && 0.0f >= tb
  911. && 1.0f <= tb
  912. ;
  913. }
  914. bool intersect(const LineSegment& _line, const Plane& _plane, Hit* _hit)
  915. {
  916. const float dist = distance(_plane, _line.pos);
  917. const float flip = sign(dist);
  918. const Vec3 dir = normalize(sub(_line.end, _line.pos) );
  919. const float ndotd = dot(dir, _plane.normal);
  920. const float tt = -dist/ndotd;
  921. const float len = length(sub(_line.end, _line.pos) );
  922. if (tt < 0.0f || tt > len)
  923. {
  924. return false;
  925. }
  926. if (NULL != _hit)
  927. {
  928. _hit->pos = mad(dir, tt, _line.pos);
  929. _hit->plane.normal = mul(_plane.normal, flip);
  930. _hit->plane.dist = -dot(_hit->plane.normal, _hit->pos);
  931. }
  932. return true;
  933. }
  934. float distance(const Plane& _plane, const LineSegment& _line)
  935. {
  936. const float pd = distance(_plane, _line.pos);
  937. const float ed = distance(_plane, _line.end);
  938. return min(max(pd*ed, 0.0f), bx::abs(pd), bx::abs(ed) );
  939. }
  940. Vec3 closestPoint(const Line& _line, const Vec3& _point)
  941. {
  942. const float tt = projectToAxis(_line.dir, sub(_point, _line.pos) );
  943. return getPointAt(_line, tt);
  944. }
  945. Vec3 closestPoint(const LineSegment& _line, const Vec3& _point, float& _outT)
  946. {
  947. const Vec3 axis = sub(_line.end, _line.pos);
  948. const float lengthSq = dot(axis, axis);
  949. const float tt = clamp(projectToAxis(axis, sub(_point, _line.pos) ) / lengthSq, 0.0f, 1.0f);
  950. _outT = tt;
  951. return mad(axis, tt, _line.pos);
  952. }
  953. Vec3 closestPoint(const LineSegment& _line, const Vec3& _point)
  954. {
  955. float ignored;
  956. return closestPoint(_line, _point, ignored);
  957. }
  958. Vec3 closestPoint(const Plane& _plane, const Vec3& _point)
  959. {
  960. const float dist = distance(_plane, _point);
  961. return sub(_point, mul(_plane.normal, dist) );
  962. }
  963. Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point)
  964. {
  965. return clamp(_point, _aabb.min, _aabb.max);
  966. }
  967. Vec3 closestPoint(const Obb& _obb, const Vec3& _point)
  968. {
  969. Srt srt = toSrt(_obb.mtx);
  970. Aabb aabb;
  971. toAabb(aabb, srt.scale);
  972. const Quaternion invRotation = invert(srt.rotation);
  973. const Vec3 obbSpacePos = mul(sub(_point, srt.translation), srt.rotation);
  974. const Vec3 pos = closestPoint(aabb, obbSpacePos);
  975. return add(mul(pos, invRotation), srt.translation);
  976. }
  977. Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point)
  978. {
  979. Plane plane;
  980. calcPlane(plane, _triangle);
  981. const Vec3 pos = closestPoint(plane, _point);
  982. const Vec3 uvw = barycentric(_triangle, pos);
  983. return cartesian(_triangle, clamp<Vec3>(uvw, 0.0f, 1.0f) );
  984. }
  985. bool overlap(const Aabb& _aabb, const Vec3& _pos)
  986. {
  987. const Vec3 ac = getCenter(_aabb);
  988. const Vec3 ae = getExtents(_aabb);
  989. const Vec3 abc = bx::abs(sub(ac, _pos) );
  990. return abc.x <= ae.x
  991. && abc.y <= ae.y
  992. && abc.z <= ae.z
  993. ;
  994. }
  995. bool overlap(const Aabb& _aabb, const Sphere& _sphere)
  996. {
  997. return overlap(_sphere, _aabb);
  998. }
  999. uint32_t overlapTestMask(const Aabb& _aabbA, const Aabb& _aabbB)
  1000. {
  1001. /// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
  1002. /// test.
  1003. const uint32_t ltMinX = _aabbA.max.x < _aabbB.min.x;
  1004. const uint32_t gtMaxX = _aabbA.min.x > _aabbB.max.x;
  1005. const uint32_t ltMinY = _aabbA.max.y < _aabbB.min.y;
  1006. const uint32_t gtMaxY = _aabbA.min.y > _aabbB.max.y;
  1007. const uint32_t ltMinZ = _aabbA.max.z < _aabbB.min.z;
  1008. const uint32_t gtMaxZ = _aabbA.min.z > _aabbB.max.z;
  1009. return 0
  1010. | (ltMinX << 0)
  1011. | (gtMaxX << 1)
  1012. | (ltMinY << 2)
  1013. | (gtMaxY << 3)
  1014. | (ltMinZ << 4)
  1015. | (gtMaxZ << 5)
  1016. ;
  1017. }
  1018. bool overlap(const Aabb& _aabbA, const Aabb& _aabbB)
  1019. {
  1020. #if 0
  1021. return 0 != overlapTestMask(_aabbA, _aabbB);
  1022. #else
  1023. const Vec3 ac = getCenter(_aabbA);
  1024. const Vec3 bc = getCenter(_aabbB);
  1025. const Vec3 abc = bx::abs(sub(ac, bc) );
  1026. const Vec3 ae = getExtents(_aabbA);
  1027. const Vec3 be = getExtents(_aabbB);
  1028. const Vec3 abe = add(ae, be);
  1029. return abc.x <= abe.x
  1030. && abc.y <= abe.y
  1031. && abc.z <= abe.z
  1032. ;
  1033. #endif // 0
  1034. }
  1035. bool overlap(const Aabb& _aabb, const Plane& _plane)
  1036. {
  1037. const Vec3 center = getCenter(_aabb);
  1038. const float dist = distance(_plane, center);
  1039. const Vec3 extents = getExtents(_aabb);
  1040. const Vec3 normal = bx::abs(_plane.normal);
  1041. const float radius = dot(extents, normal);
  1042. return bx::abs(dist) <= radius;
  1043. }
  1044. static constexpr Vec3 kAxis[] =
  1045. {
  1046. { 1.0f, 0.0f, 0.0f },
  1047. { 0.0f, 1.0f, 0.0f },
  1048. { 0.0f, 0.0f, 1.0f },
  1049. };
  1050. bool overlap(const Aabb& _aabb, const Triangle& _triangle)
  1051. {
  1052. Aabb triAabb;
  1053. toAabb(triAabb, _triangle);
  1054. if (!overlap(_aabb, triAabb) )
  1055. {
  1056. return false;
  1057. }
  1058. Plane plane;
  1059. calcPlane(plane, _triangle);
  1060. if (!overlap(_aabb, plane) )
  1061. {
  1062. return false;
  1063. }
  1064. const Vec3 center = getCenter(_aabb);
  1065. const Vec3 v0 = sub(_triangle.v0, center);
  1066. const Vec3 v1 = sub(_triangle.v1, center);
  1067. const Vec3 v2 = sub(_triangle.v2, center);
  1068. const Vec3 edge[] =
  1069. {
  1070. sub(v1, v0),
  1071. sub(v2, v1),
  1072. sub(v0, v2),
  1073. };
  1074. for (uint32_t ii = 0; ii < 3; ++ii)
  1075. {
  1076. for (uint32_t jj = 0; jj < 3; ++jj)
  1077. {
  1078. const Vec3 axis = cross(kAxis[ii], edge[jj]);
  1079. const Interval aabbR = projectToAxis(axis, _aabb);
  1080. const Interval triR = projectToAxis(axis, _triangle);
  1081. if (!overlap(aabbR, triR) )
  1082. {
  1083. return false;
  1084. }
  1085. }
  1086. }
  1087. return true;
  1088. }
  1089. bool overlap(const Aabb& _aabb, const Cylinder& _cylinder)
  1090. {
  1091. return overlap(_cylinder, _aabb);
  1092. }
  1093. bool overlap(const Aabb& _aabb, const Capsule& _capsule)
  1094. {
  1095. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, getCenter(_aabb) );
  1096. return overlap(_aabb, Sphere{pos, _capsule.radius});
  1097. }
  1098. bool overlap(const Aabb& _aabb, const Cone& _cone)
  1099. {
  1100. float tt;
  1101. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, getCenter(_aabb), tt);
  1102. return overlap(_aabb, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
  1103. }
  1104. bool overlap(const Aabb& _aabb, const Disk& _disk)
  1105. {
  1106. if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) )
  1107. {
  1108. return false;
  1109. }
  1110. Plane plane;
  1111. calcPlane(plane, _disk.normal, _disk.center);
  1112. return overlap(_aabb, plane);
  1113. }
  1114. bool overlap(const Aabb& _aabb, const Obb& _obb)
  1115. {
  1116. BX_UNUSED(_aabb, _obb);
  1117. return false;
  1118. }
  1119. bool overlap(const Capsule& _capsule, const Vec3& _pos)
  1120. {
  1121. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _pos);
  1122. return overlap(Sphere{pos, _capsule.radius}, _pos);
  1123. }
  1124. bool overlap(const Capsule& _capsule, const Sphere& _sphere)
  1125. {
  1126. return overlap(_sphere, _capsule);
  1127. }
  1128. bool overlap(const Capsule& _capsule, const Aabb& _aabb)
  1129. {
  1130. return overlap(_aabb, _capsule);
  1131. }
  1132. bool overlap(const Capsule& _capsule, const Plane& _plane)
  1133. {
  1134. return distance(_plane, LineSegment{_capsule.pos, _capsule.end}) <= _capsule.radius;
  1135. }
  1136. bool overlap(const Capsule& _capsule, const Triangle& _triangle)
  1137. {
  1138. return overlap(_triangle, _capsule);
  1139. }
  1140. bool overlap(const Capsule& _capsule, const Cylinder& _cylinder)
  1141. {
  1142. return overlap(_cylinder, _capsule);
  1143. }
  1144. bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB)
  1145. {
  1146. float ta, tb;
  1147. if (!intersect(ta, tb, {_capsuleA.pos, _capsuleA.end}, {_capsuleB.pos, _capsuleB.end}) )
  1148. {
  1149. return false;
  1150. }
  1151. if (0.0f <= ta
  1152. && 1.0f >= ta
  1153. && 0.0f <= tb
  1154. && 1.0f >= tb)
  1155. {
  1156. const Vec3 ad = sub(_capsuleA.end, _capsuleA.pos);
  1157. const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos);
  1158. return overlap(
  1159. Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
  1160. , Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius}
  1161. );
  1162. }
  1163. if (0.0f <= ta
  1164. && 1.0f >= ta)
  1165. {
  1166. return overlap(_capsuleA, Sphere{0.0f >= tb ? _capsuleB.pos : _capsuleB.end, _capsuleB.radius});
  1167. }
  1168. if (0.0f <= tb
  1169. && 1.0f >= tb)
  1170. {
  1171. return overlap(_capsuleB, Sphere{0.0f >= ta ? _capsuleA.pos : _capsuleA.end, _capsuleA.radius});
  1172. }
  1173. const Vec3 pa = 0.0f > ta ? _capsuleA.pos : _capsuleA.end;
  1174. const Vec3 pb = 0.0f > tb ? _capsuleB.pos : _capsuleB.end;
  1175. const Vec3 closestA = closestPoint(LineSegment{_capsuleA.pos, _capsuleA.end}, pb);
  1176. const Vec3 closestB = closestPoint(LineSegment{_capsuleB.pos, _capsuleB.end}, pa);
  1177. if (dot(closestA, pb) <= dot(closestB, pa) )
  1178. {
  1179. return overlap(_capsuleA, Sphere{closestB, _capsuleB.radius});
  1180. }
  1181. return overlap(_capsuleB, Sphere{closestA, _capsuleA.radius});
  1182. }
  1183. bool overlap(const Capsule& _capsule, const Cone& _cone)
  1184. {
  1185. BX_UNUSED(_capsule, _cone);
  1186. return false;
  1187. }
  1188. bool overlap(const Capsule& _capsule, const Disk& _disk)
  1189. {
  1190. return overlap(_disk, _capsule);
  1191. }
  1192. bool overlap(const Capsule& _capsule, const Obb& _obb)
  1193. {
  1194. return overlap(_obb, _capsule);
  1195. }
  1196. bool overlap(const Cone& _cone, const Vec3& _pos)
  1197. {
  1198. float tt;
  1199. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _pos, tt);
  1200. return overlap(Disk{pos, normalize(sub(_cone.end, _cone.pos) ), lerp(_cone.radius, 0.0f, tt)}, _pos);
  1201. }
  1202. bool overlap(const Cone& _cone, const Sphere& _sphere)
  1203. {
  1204. return overlap(_sphere, _cone);
  1205. }
  1206. bool overlap(const Cone& _cone, const Aabb& _aabb)
  1207. {
  1208. return overlap(_aabb, _cone);
  1209. }
  1210. bool overlap(const Cone& _cone, const Plane& _plane)
  1211. {
  1212. BX_UNUSED(_cone, _plane);
  1213. return false;
  1214. }
  1215. bool overlap(const Cone& _cone, const Triangle& _triangle)
  1216. {
  1217. return overlap(_triangle, _cone);
  1218. }
  1219. bool overlap(const Cone& _cone, const Cylinder& _cylinder)
  1220. {
  1221. BX_UNUSED(_cone, _cylinder);
  1222. return false;
  1223. }
  1224. bool overlap(const Cone& _cone, const Capsule& _capsule)
  1225. {
  1226. BX_UNUSED(_cone, _capsule);
  1227. return false;
  1228. }
  1229. bool overlap(const Cone& _coneA, const Cone& _coneB)
  1230. {
  1231. BX_UNUSED(_coneA, _coneB);
  1232. return false;
  1233. }
  1234. bool overlap(const Cone& _cone, const Disk& _disk)
  1235. {
  1236. BX_UNUSED(_cone, _disk);
  1237. return false;
  1238. }
  1239. bool overlap(const Cone& _cone, const Obb& _obb)
  1240. {
  1241. BX_UNUSED(_cone, _obb);
  1242. return false;
  1243. }
  1244. bool overlap(const Cylinder& _cylinder, const Vec3& _pos)
  1245. {
  1246. const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, _pos);
  1247. return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _pos);
  1248. }
  1249. bool overlap(const Cylinder& _cylinder, const Sphere& _sphere)
  1250. {
  1251. const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, _sphere.center);
  1252. return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _sphere);
  1253. }
  1254. bool overlap(const Cylinder& _cylinder, const Aabb& _aabb)
  1255. {
  1256. const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, getCenter(_aabb) );
  1257. return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _aabb);
  1258. }
  1259. bool overlap(const Cylinder& _cylinder, const Plane& _plane)
  1260. {
  1261. BX_UNUSED(_cylinder, _plane);
  1262. return false;
  1263. }
  1264. bool overlap(const Cylinder& _cylinder, const Triangle& _triangle)
  1265. {
  1266. return overlap(_triangle, _cylinder);
  1267. }
  1268. bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB)
  1269. {
  1270. BX_UNUSED(_cylinderA, _cylinderB);
  1271. return false;
  1272. }
  1273. bool overlap(const Cylinder& _cylinder, const Capsule& _capsule)
  1274. {
  1275. BX_UNUSED(_cylinder, _capsule);
  1276. return false;
  1277. }
  1278. bool overlap(const Cylinder& _cylinder, const Cone& _cone)
  1279. {
  1280. BX_UNUSED(_cylinder, _cone);
  1281. return false;
  1282. }
  1283. bool overlap(const Cylinder& _cylinder, const Disk& _disk)
  1284. {
  1285. BX_UNUSED(_cylinder, _disk);
  1286. return false;
  1287. }
  1288. bool overlap(const Cylinder& _cylinder, const Obb& _obb)
  1289. {
  1290. BX_UNUSED(_cylinder, _obb);
  1291. return false;
  1292. }
  1293. bool overlap(const Disk& _disk, const Vec3& _pos)
  1294. {
  1295. Plane plane;
  1296. calcPlane(plane, _disk.normal, _disk.center);
  1297. if (!isNearZero(distance(plane, _pos) ) )
  1298. {
  1299. return false;
  1300. }
  1301. return distanceSq(_disk.center, _pos) <= square(_disk.radius);
  1302. }
  1303. bool overlap(const Disk& _disk, const Sphere& _sphere)
  1304. {
  1305. return overlap(_sphere, _disk);
  1306. }
  1307. bool overlap(const Disk& _disk, const Aabb& _aabb)
  1308. {
  1309. return overlap(_aabb, _disk);
  1310. }
  1311. bool overlap(const Disk& _disk, const Plane& _plane)
  1312. {
  1313. Plane plane;
  1314. calcPlane(plane, _disk.normal, _disk.center);
  1315. if (!overlap(plane, _plane) )
  1316. {
  1317. return false;
  1318. }
  1319. return overlap(_plane, Sphere{_disk.center, _disk.radius});
  1320. }
  1321. bool overlap(const Disk& _disk, const Triangle& _triangle)
  1322. {
  1323. return overlap(_triangle, _disk);
  1324. }
  1325. bool overlap(const Disk& _disk, const Cylinder& _cylinder)
  1326. {
  1327. return overlap(_cylinder, _disk);
  1328. }
  1329. bool overlap(const Disk& _disk, const Capsule& _capsule)
  1330. {
  1331. if (!overlap(_capsule, Sphere{_disk.center, _disk.radius}) )
  1332. {
  1333. return false;
  1334. }
  1335. Plane plane;
  1336. calcPlane(plane, _disk.normal, _disk.center);
  1337. return overlap(_capsule, plane);
  1338. }
  1339. bool overlap(const Disk& _disk, const Cone& _cone)
  1340. {
  1341. BX_UNUSED(_disk, _cone);
  1342. return false;
  1343. }
  1344. bool overlap(const Disk& _diskA, const Disk& _diskB)
  1345. {
  1346. Plane planeA;
  1347. calcPlane(planeA, _diskA.normal, _diskA.center);
  1348. Plane planeB;
  1349. calcPlane(planeB, _diskB);
  1350. Line line;
  1351. if (!intersect(line, planeA, planeB) )
  1352. {
  1353. return false;
  1354. }
  1355. const Vec3 pa = closestPoint(line, _diskA.center);
  1356. const Vec3 pb = closestPoint(line, _diskB.center);
  1357. const float lenA = distance(pa, _diskA.center);
  1358. const float lenB = distance(pb, _diskB.center);
  1359. return sqrt(square(_diskA.radius) - square(lenA) )
  1360. + sqrt(square(_diskB.radius) - square(lenB) )
  1361. >= distance(pa, pb)
  1362. ;
  1363. }
  1364. bool overlap(const Disk& _disk, const Obb& _obb)
  1365. {
  1366. if (!overlap(_obb, Sphere{_disk.center, _disk.radius}) )
  1367. {
  1368. return false;
  1369. }
  1370. Plane plane;
  1371. calcPlane(plane, _disk.normal, _disk.center);
  1372. return overlap(_obb, plane);
  1373. }
  1374. bool overlap(const Obb& _obb, const Vec3& _pos)
  1375. {
  1376. Srt srt = toSrt(_obb.mtx);
  1377. Aabb aabb;
  1378. toAabb(aabb, srt.scale);
  1379. const Quaternion invRotation = invert(srt.rotation);
  1380. const Vec3 pos = mul(sub(_pos, srt.translation), invRotation);
  1381. return overlap(aabb, pos);
  1382. }
  1383. bool overlap(const Obb& _obb, const Sphere& _sphere)
  1384. {
  1385. return overlap(_sphere, _obb);
  1386. }
  1387. bool overlap(const Obb& _obb, const Aabb& _aabb)
  1388. {
  1389. return overlap(_aabb, _obb);
  1390. }
  1391. bool overlap(const Obb& _obb, const Plane& _plane)
  1392. {
  1393. Srt srt = toSrt(_obb.mtx);
  1394. const Quaternion invRotation = invert(srt.rotation);
  1395. const Vec3 axis =
  1396. {
  1397. projectToAxis(_plane.normal, mul(Vec3{1.0f, 0.0f, 0.0f}, invRotation) ),
  1398. projectToAxis(_plane.normal, mul(Vec3{0.0f, 1.0f, 0.0f}, invRotation) ),
  1399. projectToAxis(_plane.normal, mul(Vec3{0.0f, 0.0f, 1.0f}, invRotation) ),
  1400. };
  1401. const float dist = bx::abs(distance(_plane, srt.translation) );
  1402. const float radius = dot(srt.scale, bx::abs(axis) );
  1403. return dist <= radius;
  1404. }
  1405. bool overlap(const Obb& _obb, const Triangle& _triangle)
  1406. {
  1407. return overlap(_triangle, _obb);
  1408. }
  1409. bool overlap(const Obb& _obb, const Cylinder& _cylinder)
  1410. {
  1411. BX_UNUSED(_obb, _cylinder);
  1412. return false;
  1413. }
  1414. bool overlap(const Obb& _obb, const Capsule& _capsule)
  1415. {
  1416. Srt srt = toSrt(_obb.mtx);
  1417. Aabb aabb;
  1418. toAabb(aabb, srt.scale);
  1419. const Quaternion invRotation = invert(srt.rotation);
  1420. const Capsule capsule =
  1421. {
  1422. mul(sub(_capsule.pos, srt.translation), invRotation),
  1423. mul(sub(_capsule.end, srt.translation), invRotation),
  1424. _capsule.radius,
  1425. };
  1426. return overlap(aabb, capsule);
  1427. }
  1428. bool overlap(const Obb& _obb, const Cone& _cone)
  1429. {
  1430. BX_UNUSED(_obb, _cone);
  1431. return false;
  1432. }
  1433. bool overlap(const Obb& _obb, const Disk& _disk)
  1434. {
  1435. return overlap(_disk, _obb);
  1436. }
  1437. bool overlap(const Obb& _obbA, const Obb& _obbB)
  1438. {
  1439. BX_UNUSED(_obbA, _obbB);
  1440. return false;
  1441. }
  1442. bool overlap(const Plane& _plane, const Vec3& _pos)
  1443. {
  1444. return isNearZero(distance(_plane, _pos) );
  1445. }
  1446. bool overlap(const Plane& _plane, const Sphere& _sphere)
  1447. {
  1448. return overlap(_sphere, _plane);
  1449. }
  1450. bool overlap(const Plane& _plane, const Aabb& _aabb)
  1451. {
  1452. return overlap(_aabb, _plane);
  1453. }
  1454. bool overlap(const Plane& _planeA, const Plane& _planeB)
  1455. {
  1456. const Vec3 dir = cross(_planeA.normal, _planeB.normal);
  1457. const float len = length(dir);
  1458. return !isNearZero(len);
  1459. }
  1460. bool overlap(const Plane& _plane, const Triangle& _triangle)
  1461. {
  1462. return overlap(_triangle, _plane);
  1463. }
  1464. bool overlap(const Plane& _plane, const Cylinder& _cylinder)
  1465. {
  1466. return overlap(_cylinder, _plane);
  1467. }
  1468. bool overlap(const Plane& _plane, const Capsule& _capsule)
  1469. {
  1470. return overlap(_capsule, _plane);
  1471. }
  1472. bool overlap(const Plane& _plane, const Cone& _cone)
  1473. {
  1474. BX_UNUSED(_plane, _cone);
  1475. return false;
  1476. }
  1477. bool overlap(const Plane& _plane, const Disk& _disk)
  1478. {
  1479. return overlap(_disk, _plane);
  1480. }
  1481. bool overlap(const Plane& _plane, const Obb& _obb)
  1482. {
  1483. return overlap(_obb, _plane);
  1484. }
  1485. bool overlap(const Sphere& _sphere, const Vec3& _pos)
  1486. {
  1487. const float distSq = distanceSq(_sphere.center, _pos);
  1488. const float radiusSq = square(_sphere.radius);
  1489. return distSq <= radiusSq;
  1490. }
  1491. bool overlap(const Sphere& _sphereA, const Sphere& _sphereB)
  1492. {
  1493. const float distSq = distanceSq(_sphereA.center, _sphereB.center);
  1494. const float radiusSq = square(_sphereA.radius + _sphereB.radius);
  1495. return distSq <= radiusSq;
  1496. }
  1497. bool overlap(const Sphere& _sphere, const Aabb& _aabb)
  1498. {
  1499. const Vec3 pos = closestPoint(_aabb, _sphere.center);
  1500. return overlap(_sphere, pos);
  1501. }
  1502. bool overlap(const Sphere& _sphere, const Plane& _plane)
  1503. {
  1504. return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius;
  1505. }
  1506. bool overlap(const Sphere& _sphere, const Triangle& _triangle)
  1507. {
  1508. Plane plane;
  1509. calcPlane(plane, _triangle);
  1510. if (!overlap(_sphere, plane) )
  1511. {
  1512. return false;
  1513. }
  1514. const Vec3 pos = closestPoint(plane, _sphere.center);
  1515. const Vec3 uvw = barycentric(_triangle, pos);
  1516. const float nr = -_sphere.radius;
  1517. return uvw.x >= nr
  1518. && uvw.y >= nr
  1519. && uvw.z >= nr
  1520. ;
  1521. }
  1522. bool overlap(const Sphere& _sphere, const Cylinder& _cylinder)
  1523. {
  1524. return overlap(_cylinder, _sphere);
  1525. }
  1526. bool overlap(const Sphere& _sphere, const Capsule& _capsule)
  1527. {
  1528. const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center);
  1529. return overlap(_sphere, Sphere{pos, _capsule.radius});
  1530. }
  1531. bool overlap(const Sphere& _sphere, const Cone& _cone)
  1532. {
  1533. float tt;
  1534. const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt);
  1535. return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)});
  1536. }
  1537. bool overlap(const Sphere& _sphere, const Disk& _disk)
  1538. {
  1539. if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) )
  1540. {
  1541. return false;
  1542. }
  1543. Plane plane;
  1544. calcPlane(plane, _disk.normal, _disk.center);
  1545. return overlap(_sphere, plane);
  1546. }
  1547. bool overlap(const Sphere& _sphere, const Obb& _obb)
  1548. {
  1549. const Vec3 pos = closestPoint(_obb, _sphere.center);
  1550. return overlap(_sphere, pos);
  1551. }
  1552. bool overlap(const Triangle& _triangle, const Vec3& _pos)
  1553. {
  1554. const Vec3 uvw = barycentric(_triangle, _pos);
  1555. return uvw.x >= 0.0f
  1556. && uvw.y >= 0.0f
  1557. && uvw.z >= 0.0f
  1558. ;
  1559. }
  1560. bool overlap(const Triangle& _triangle, const Sphere& _sphere)
  1561. {
  1562. return overlap(_sphere, _triangle);
  1563. }
  1564. bool overlap(const Triangle& _triangle, const Aabb& _aabb)
  1565. {
  1566. return overlap(_aabb, _triangle);
  1567. }
  1568. bool overlap(const Triangle& _triangle, const Plane& _plane)
  1569. {
  1570. const float dist0 = distance(_plane, _triangle.v0);
  1571. const float dist1 = distance(_plane, _triangle.v1);
  1572. const float dist2 = distance(_plane, _triangle.v2);
  1573. const float minDist = min(dist0, dist1, dist2);
  1574. const float maxDist = max(dist0, dist1, dist2);
  1575. return 0.0f > minDist
  1576. && 0.0f < maxDist
  1577. ;
  1578. }
  1579. inline bool overlap(const Triangle& _triangleA, const Triangle& _triangleB, const Vec3& _axis)
  1580. {
  1581. const Interval ia = projectToAxis(_axis, _triangleA);
  1582. const Interval ib = projectToAxis(_axis, _triangleB);
  1583. return overlap(ia, ib);
  1584. }
  1585. bool overlap(const Triangle& _triangleA, const Triangle& _triangleB)
  1586. {
  1587. const Vec3 baA = sub(_triangleA.v1, _triangleA.v0);
  1588. const Vec3 cbA = sub(_triangleA.v2, _triangleA.v1);
  1589. const Vec3 acA = sub(_triangleA.v0, _triangleA.v2);
  1590. const Vec3 baB = sub(_triangleB.v1, _triangleB.v0);
  1591. const Vec3 cbB = sub(_triangleB.v2, _triangleB.v1);
  1592. const Vec3 acB = sub(_triangleB.v0, _triangleB.v2);
  1593. return overlap(_triangleA, _triangleB, cross(baA, cbA) )
  1594. && overlap(_triangleA, _triangleB, cross(baB, cbB) )
  1595. && overlap(_triangleA, _triangleB, cross(baB, baA) )
  1596. && overlap(_triangleA, _triangleB, cross(baB, cbA) )
  1597. && overlap(_triangleA, _triangleB, cross(baB, acA) )
  1598. && overlap(_triangleA, _triangleB, cross(cbB, baA) )
  1599. && overlap(_triangleA, _triangleB, cross(cbB, cbA) )
  1600. && overlap(_triangleA, _triangleB, cross(cbB, acA) )
  1601. && overlap(_triangleA, _triangleB, cross(acB, baA) )
  1602. && overlap(_triangleA, _triangleB, cross(acB, cbA) )
  1603. && overlap(_triangleA, _triangleB, cross(acB, acA) )
  1604. ;
  1605. }
  1606. template<typename Ty>
  1607. bool overlap(const Triangle& _triangle, const Ty& _ty)
  1608. {
  1609. Plane plane;
  1610. calcPlane(plane, _triangle);
  1611. plane.normal = neg(plane.normal);
  1612. plane.dist = -plane.dist;
  1613. const LineSegment line =
  1614. {
  1615. _ty.pos,
  1616. _ty.end,
  1617. };
  1618. Hit hit;
  1619. if (intersect(line, plane, &hit) )
  1620. {
  1621. return true;
  1622. }
  1623. const Vec3 pos = closestPoint(plane, hit.pos);
  1624. const Vec3 uvw = barycentric(_triangle, pos);
  1625. const float nr = -_ty.radius;
  1626. if (uvw.x >= nr
  1627. && uvw.y >= nr
  1628. && uvw.z >= nr)
  1629. {
  1630. return true;
  1631. }
  1632. const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1};
  1633. const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2};
  1634. const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0};
  1635. float ta0, tb0;
  1636. const bool i0 = intersect(ta0, tb0, ab, line);
  1637. float ta1, tb1;
  1638. const bool i1 = intersect(ta1, tb1, bc, line);
  1639. float ta2, tb2;
  1640. const bool i2 = intersect(ta2, tb2, ca, line);
  1641. if (!i0
  1642. || !i1
  1643. || !i2)
  1644. {
  1645. return false;
  1646. }
  1647. ta0 = clamp(ta0, 0.0f, 1.0f);
  1648. ta1 = clamp(ta1, 0.0f, 1.0f);
  1649. ta2 = clamp(ta2, 0.0f, 1.0f);
  1650. tb0 = clamp(tb0, 0.0f, 1.0f);
  1651. tb1 = clamp(tb1, 0.0f, 1.0f);
  1652. tb2 = clamp(tb2, 0.0f, 1.0f);
  1653. const Vec3 pa0 = getPointAt(ab, ta0);
  1654. const Vec3 pa1 = getPointAt(bc, ta1);
  1655. const Vec3 pa2 = getPointAt(ca, ta2);
  1656. const Vec3 pb0 = getPointAt(line, tb0);
  1657. const Vec3 pb1 = getPointAt(line, tb1);
  1658. const Vec3 pb2 = getPointAt(line, tb2);
  1659. const float d0 = distanceSq(pa0, pb0);
  1660. const float d1 = distanceSq(pa1, pb1);
  1661. const float d2 = distanceSq(pa2, pb2);
  1662. if (d0 <= d1
  1663. && d0 <= d2)
  1664. {
  1665. return overlap(_ty, pa0);
  1666. }
  1667. else if (d1 <= d2)
  1668. {
  1669. return overlap(_ty, pa1);
  1670. }
  1671. return overlap(_ty, pa2);
  1672. }
  1673. bool overlap(const Triangle& _triangle, const Cylinder& _cylinder)
  1674. {
  1675. return overlap<Cylinder>(_triangle, _cylinder);
  1676. }
  1677. bool overlap(const Triangle& _triangle, const Capsule& _capsule)
  1678. {
  1679. return overlap<Capsule>(_triangle, _capsule);
  1680. }
  1681. bool overlap(const Triangle& _triangle, const Cone& _cone)
  1682. {
  1683. const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1};
  1684. const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2};
  1685. const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0};
  1686. const LineSegment line =
  1687. {
  1688. _cone.pos,
  1689. _cone.end,
  1690. };
  1691. float ta0, tb0;
  1692. const bool i0 = intersect(ta0, tb0, ab, line);
  1693. float ta1, tb1;
  1694. const bool i1 = intersect(ta1, tb1, bc, line);
  1695. float ta2, tb2;
  1696. const bool i2 = intersect(ta2, tb2, ca, line);
  1697. if (!i0
  1698. || !i1
  1699. || !i2)
  1700. {
  1701. return false;
  1702. }
  1703. ta0 = clamp(ta0, 0.0f, 1.0f);
  1704. ta1 = clamp(ta1, 0.0f, 1.0f);
  1705. ta2 = clamp(ta2, 0.0f, 1.0f);
  1706. tb0 = clamp(tb0, 0.0f, 1.0f);
  1707. tb1 = clamp(tb1, 0.0f, 1.0f);
  1708. tb2 = clamp(tb2, 0.0f, 1.0f);
  1709. const Vec3 pa0 = getPointAt(ab, ta0);
  1710. const Vec3 pa1 = getPointAt(bc, ta1);
  1711. const Vec3 pa2 = getPointAt(ca, ta2);
  1712. const Vec3 pb0 = getPointAt(line, tb0);
  1713. const Vec3 pb1 = getPointAt(line, tb1);
  1714. const Vec3 pb2 = getPointAt(line, tb2);
  1715. const float d0 = distanceSq(pa0, pb0);
  1716. const float d1 = distanceSq(pa1, pb1);
  1717. const float d2 = distanceSq(pa2, pb2);
  1718. if (d0 <= d1
  1719. && d0 <= d2)
  1720. {
  1721. return overlap(_cone, pa0);
  1722. }
  1723. else if (d1 <= d2)
  1724. {
  1725. return overlap(_cone, pa1);
  1726. }
  1727. return overlap(_cone, pa2);
  1728. }
  1729. bool overlap(const Triangle& _triangle, const Disk& _disk)
  1730. {
  1731. if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) )
  1732. {
  1733. return false;
  1734. }
  1735. Plane plane;
  1736. calcPlane(plane, _disk.normal, _disk.center);
  1737. return overlap(_triangle, plane);
  1738. }
  1739. bool overlap(const Triangle& _triangle, const Obb& _obb)
  1740. {
  1741. Srt srt = toSrt(_obb.mtx);
  1742. Aabb aabb;
  1743. toAabb(aabb, srt.scale);
  1744. const Quaternion invRotation = invert(srt.rotation);
  1745. const Triangle triangle =
  1746. {
  1747. mul(sub(_triangle.v0, srt.translation), invRotation),
  1748. mul(sub(_triangle.v1, srt.translation), invRotation),
  1749. mul(sub(_triangle.v2, srt.translation), invRotation),
  1750. };
  1751. return overlap(triangle, aabb);
  1752. }