2
0

bounds.cpp 38 KB

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