2
0

bounds.cpp 45 KB

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