bounds.cpp 47 KB

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