bounds.cpp 46 KB

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