bounds.cpp 47 KB

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