bounds.cpp 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 bx::mul(bx::add(bx::add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f);
  20. }
  21. void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extent)
  22. {
  23. _outAabb.min = sub(_center, _extent);
  24. _outAabb.max = add(_center, _extent);
  25. }
  26. void toAabb(Aabb& _outAabb, const Obb& _obb)
  27. {
  28. Vec3 xyz = { 1.0f, 1.0f, 1.0f };
  29. Vec3 tmp = mul(xyz, _obb.mtx);
  30. _outAabb.min = tmp;
  31. _outAabb.max = tmp;
  32. for (uint32_t ii = 1; ii < 8; ++ii)
  33. {
  34. xyz.x = ii & 1 ? -1.0f : 1.0f;
  35. xyz.y = ii & 2 ? -1.0f : 1.0f;
  36. xyz.z = ii & 4 ? -1.0f : 1.0f;
  37. tmp = mul(xyz, _obb.mtx);
  38. _outAabb.min = min(_outAabb.min, tmp);
  39. _outAabb.max = max(_outAabb.max, tmp);
  40. }
  41. }
  42. void toAabb(Aabb& _outAabb, const Sphere& _sphere)
  43. {
  44. const float radius = _sphere.radius;
  45. _outAabb.min = sub(_sphere.center, radius);
  46. _outAabb.max = add(_sphere.center, radius);
  47. }
  48. void toAabb(Aabb& _outAabb, const Disk& _disk)
  49. {
  50. // Reference(s):
  51. // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
  52. //
  53. const Vec3 nsq = mul(_disk.normal, _disk.normal);
  54. const Vec3 one = { 1.0f, 1.0f, 1.0f };
  55. const Vec3 tmp = sub(one, nsq);
  56. const float inv = 1.0f / (tmp.x*tmp.y*tmp.z);
  57. const Vec3 extent =
  58. {
  59. _disk.radius * tmp.x * sqrt( (nsq.x + nsq.y * nsq.z) * inv),
  60. _disk.radius * tmp.y * sqrt( (nsq.y + nsq.z * nsq.x) * inv),
  61. _disk.radius * tmp.z * sqrt( (nsq.z + nsq.x * nsq.y) * inv),
  62. };
  63. _outAabb.min = sub(_disk.center, extent);
  64. _outAabb.max = add(_disk.center, extent);
  65. }
  66. void toAabb(Aabb& _outAabb, const Cylinder& _cylinder)
  67. {
  68. // Reference(s):
  69. // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm
  70. //
  71. const Vec3 axis = sub(_cylinder.end, _cylinder.pos);
  72. const Vec3 asq = mul(axis, axis);
  73. const Vec3 nsq = mul(asq, 1.0f/dot(axis, axis) );
  74. const Vec3 one = { 1.0f, 1.0f, 1.0f };
  75. const Vec3 tmp = sub(one, nsq);
  76. const float inv = 1.0f / (tmp.x*tmp.y*tmp.z);
  77. const Vec3 extent =
  78. {
  79. _cylinder.radius * tmp.x * sqrt( (nsq.x + nsq.y * nsq.z) * inv),
  80. _cylinder.radius * tmp.y * sqrt( (nsq.y + nsq.z * nsq.x) * inv),
  81. _cylinder.radius * tmp.z * sqrt( (nsq.z + nsq.x * nsq.y) * inv),
  82. };
  83. const Vec3 minP = sub(_cylinder.pos, extent);
  84. const Vec3 minE = sub(_cylinder.end, extent);
  85. const Vec3 maxP = add(_cylinder.pos, extent);
  86. const Vec3 maxE = add(_cylinder.end, extent);
  87. _outAabb.min = min(minP, minE);
  88. _outAabb.max = max(maxP, maxE);
  89. }
  90. void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx)
  91. {
  92. toObb(_obb, _aabb);
  93. float result[16];
  94. mtxMul(result, _obb.mtx, _mtx);
  95. memCopy(_obb.mtx, result, sizeof(result) );
  96. }
  97. void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  98. {
  99. Vec3 mn, mx;
  100. uint8_t* vertex = (uint8_t*)_vertices;
  101. mn = mx = load<Vec3>(vertex);
  102. vertex += _stride;
  103. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  104. {
  105. const Vec3 pos = load<Vec3>(vertex);
  106. vertex += _stride;
  107. mn = min(pos, mn);
  108. mx = max(pos, mx);
  109. }
  110. _outAabb.min = mn;
  111. _outAabb.max = mx;
  112. }
  113. void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  114. {
  115. Vec3 mn, mx;
  116. uint8_t* vertex = (uint8_t*)_vertices;
  117. mn = mx = mul(load<Vec3>(vertex), _mtx);
  118. vertex += _stride;
  119. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  120. {
  121. Vec3 pos = mul(load<Vec3>(vertex), _mtx);
  122. vertex += _stride;
  123. mn = min(pos, mn);
  124. mx = max(pos, mx);
  125. }
  126. _outAabb.min = mn;
  127. _outAabb.max = mx;
  128. }
  129. float calcAreaAabb(const Aabb& _aabb)
  130. {
  131. const float ww = _aabb.max.x - _aabb.min.x;
  132. const float hh = _aabb.max.y - _aabb.min.y;
  133. const float dd = _aabb.max.z - _aabb.min.z;
  134. return 2.0f * (ww*hh + ww*dd + hh*dd);
  135. }
  136. void aabbExpand(Aabb& _outAabb, float _factor)
  137. {
  138. _outAabb.min.x -= _factor;
  139. _outAabb.min.y -= _factor;
  140. _outAabb.min.z -= _factor;
  141. _outAabb.max.x += _factor;
  142. _outAabb.max.y += _factor;
  143. _outAabb.max.z += _factor;
  144. }
  145. void aabbExpand(Aabb& _outAabb, const Vec3& _pos)
  146. {
  147. _outAabb.min = min(_outAabb.min, _pos);
  148. _outAabb.max = max(_outAabb.max, _pos);
  149. }
  150. void toObb(Obb& _outObb, const Aabb& _aabb)
  151. {
  152. memSet(_outObb.mtx, 0, sizeof(_outObb.mtx) );
  153. _outObb.mtx[ 0] = (_aabb.max.x - _aabb.min.x) * 0.5f;
  154. _outObb.mtx[ 5] = (_aabb.max.y - _aabb.min.y) * 0.5f;
  155. _outObb.mtx[10] = (_aabb.max.z - _aabb.min.z) * 0.5f;
  156. _outObb.mtx[12] = (_aabb.min.x + _aabb.max.x) * 0.5f;
  157. _outObb.mtx[13] = (_aabb.min.y + _aabb.max.y) * 0.5f;
  158. _outObb.mtx[14] = (_aabb.min.z + _aabb.max.z) * 0.5f;
  159. _outObb.mtx[15] = 1.0f;
  160. }
  161. void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps)
  162. {
  163. Aabb aabb;
  164. toAabb(aabb, _vertices, _numVertices, _stride);
  165. float minArea = calcAreaAabb(aabb);
  166. Obb best;
  167. toObb(best, aabb);
  168. float angleStep = float(kPiHalf/_steps);
  169. float ax = 0.0f;
  170. float mtx[16];
  171. for (uint32_t ii = 0; ii < _steps; ++ii)
  172. {
  173. float ay = 0.0f;
  174. for (uint32_t jj = 0; jj < _steps; ++jj)
  175. {
  176. float az = 0.0f;
  177. for (uint32_t kk = 0; kk < _steps; ++kk)
  178. {
  179. mtxRotateXYZ(mtx, ax, ay, az);
  180. float mtxT[16];
  181. mtxTranspose(mtxT, mtx);
  182. toAabb(aabb, mtxT, _vertices, _numVertices, _stride);
  183. float area = calcAreaAabb(aabb);
  184. if (area < minArea)
  185. {
  186. minArea = area;
  187. aabbTransformToObb(best, aabb, mtx);
  188. }
  189. az += angleStep;
  190. }
  191. ay += angleStep;
  192. }
  193. ax += angleStep;
  194. }
  195. memCopy(&_outObb, &best, sizeof(Obb) );
  196. }
  197. void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  198. {
  199. Aabb aabb;
  200. toAabb(aabb, _vertices, _numVertices, _stride);
  201. Vec3 center = getCenter(aabb);
  202. float maxDistSq = 0.0f;
  203. uint8_t* vertex = (uint8_t*)_vertices;
  204. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  205. {
  206. const Vec3& pos = load<Vec3>(vertex);
  207. vertex += _stride;
  208. const Vec3 tmp = sub(pos, center);
  209. const float distSq = dot(tmp, tmp);
  210. maxDistSq = max(distSq, maxDistSq);
  211. }
  212. _sphere.center = center;
  213. _sphere.radius = sqrt(maxDistSq);
  214. }
  215. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
  216. {
  217. RngMwc rng;
  218. uint8_t* vertex = (uint8_t*)_vertices;
  219. Vec3 center;
  220. float* position = (float*)&vertex[0];
  221. center.x = position[0];
  222. center.y = position[1];
  223. center.z = position[2];
  224. position = (float*)&vertex[1*_stride];
  225. center.x += position[0];
  226. center.y += position[1];
  227. center.z += position[2];
  228. center.x *= 0.5f;
  229. center.y *= 0.5f;
  230. center.z *= 0.5f;
  231. float xx = position[0] - center.x;
  232. float yy = position[1] - center.y;
  233. float zz = position[2] - center.z;
  234. float maxDistSq = xx*xx + yy*yy + zz*zz;
  235. float radiusStep = _step * 0.37f;
  236. bool done;
  237. do
  238. {
  239. done = true;
  240. for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
  241. {
  242. position = (float*)&vertex[index*_stride];
  243. xx = position[0] - center.x;
  244. yy = position[1] - center.y;
  245. zz = position[2] - center.z;
  246. float distSq = xx*xx + yy*yy + zz*zz;
  247. if (distSq > maxDistSq)
  248. {
  249. done = false;
  250. center.x += xx * radiusStep;
  251. center.y += yy * radiusStep;
  252. center.z += zz * radiusStep;
  253. maxDistSq = lerp(maxDistSq, distSq, _step);
  254. break;
  255. }
  256. }
  257. } while (!done);
  258. _sphere.center = center;
  259. _sphere.radius = sqrt(maxDistSq);
  260. }
  261. void buildFrustumPlanes(Plane* _result, const float* _viewProj)
  262. {
  263. const float xw = _viewProj[ 3];
  264. const float yw = _viewProj[ 7];
  265. const float zw = _viewProj[11];
  266. const float ww = _viewProj[15];
  267. const float xz = _viewProj[ 2];
  268. const float yz = _viewProj[ 6];
  269. const float zz = _viewProj[10];
  270. const float wz = _viewProj[14];
  271. Plane& near = _result[0];
  272. Plane& far = _result[1];
  273. Plane& left = _result[2];
  274. Plane& right = _result[3];
  275. Plane& top = _result[4];
  276. Plane& bottom = _result[5];
  277. near.normal.x = xw - xz;
  278. near.normal.y = yw - yz;
  279. near.normal.z = zw - zz;
  280. near.dist = ww - wz;
  281. far.normal.x = xw + xz;
  282. far.normal.y = yw + yz;
  283. far.normal.z = zw + zz;
  284. far.dist = ww + wz;
  285. const float xx = _viewProj[ 0];
  286. const float yx = _viewProj[ 4];
  287. const float zx = _viewProj[ 8];
  288. const float wx = _viewProj[12];
  289. left.normal.x = xw - xx;
  290. left.normal.y = yw - yx;
  291. left.normal.z = zw - zx;
  292. left.dist = ww - wx;
  293. right.normal.x = xw + xx;
  294. right.normal.y = yw + yx;
  295. right.normal.z = zw + zx;
  296. right.dist = ww + wx;
  297. const float xy = _viewProj[ 1];
  298. const float yy = _viewProj[ 5];
  299. const float zy = _viewProj[ 9];
  300. const float wy = _viewProj[13];
  301. top.normal.x = xw + xy;
  302. top.normal.y = yw + yy;
  303. top.normal.z = zw + zy;
  304. top.dist = ww + wy;
  305. bottom.normal.x = xw - xy;
  306. bottom.normal.y = yw - yy;
  307. bottom.normal.z = zw - zy;
  308. bottom.dist = ww - wy;
  309. Plane* plane = _result;
  310. for (uint32_t ii = 0; ii < 6; ++ii)
  311. {
  312. const float len = length(plane->normal);
  313. plane->normal = normalize(plane->normal);
  314. float invLen = 1.0f / len;
  315. plane->dist *= invLen;
  316. ++plane;
  317. }
  318. }
  319. Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc)
  320. {
  321. const Vec3 axb = cross(_pa.normal, _pb.normal);
  322. const Vec3 bxc = cross(_pb.normal, _pc.normal);
  323. const Vec3 cxa = cross(_pc.normal, _pa.normal);
  324. const Vec3 tmp0 = mul(bxc, _pa.dist);
  325. const Vec3 tmp1 = mul(cxa, _pb.dist);
  326. const Vec3 tmp2 = mul(axb, _pc.dist);
  327. const Vec3 tmp3 = add(tmp0, tmp1);
  328. const Vec3 tmp4 = add(tmp3, tmp2);
  329. const float denom = dot(_pa.normal, bxc);
  330. const Vec3 result = mul(tmp4, -1.0f/denom);
  331. return result;
  332. }
  333. Ray makeRay(float _x, float _y, const float* _invVp)
  334. {
  335. Ray ray;
  336. const Vec3 near = { _x, _y, 0.0f };
  337. ray.pos = mulH(near, _invVp);
  338. const Vec3 far = { _x, _y, 1.0f };
  339. Vec3 tmp = mulH(far, _invVp);
  340. const Vec3 dir = sub(tmp, ray.pos);
  341. ray.dir = normalize(dir);
  342. return ray;
  343. }
  344. inline Vec3 getPointAt(const Ray& _ray, float _t)
  345. {
  346. return add(mul(_ray.dir, _t), _ray.pos);
  347. }
  348. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
  349. {
  350. const Vec3 invDir = rcp(_ray.dir);
  351. const Vec3 tmp0 = sub(_aabb.min, _ray.pos);
  352. const Vec3 t0 = mul(tmp0, invDir);
  353. const Vec3 tmp1 = sub(_aabb.max, _ray.pos);
  354. const Vec3 t1 = mul(tmp1, invDir);
  355. const Vec3 mn = min(t0, t1);
  356. const Vec3 mx = max(t0, t1);
  357. const float tmin = max(mn.x, mn.y, mn.z);
  358. const float tmax = min(mx.x, mx.y, mx.z);
  359. if (0.0f > tmax
  360. || tmin > tmax)
  361. {
  362. return false;
  363. }
  364. if (NULL != _hit)
  365. {
  366. _hit->plane.normal.x = float( (t1.x == tmin) - (t0.x == tmin) );
  367. _hit->plane.normal.y = float( (t1.y == tmin) - (t0.y == tmin) );
  368. _hit->plane.normal.z = float( (t1.z == tmin) - (t0.z == tmin) );
  369. _hit->plane.dist = tmin;
  370. _hit->pos = getPointAt(_ray, tmin);
  371. }
  372. return true;
  373. }
  374. static constexpr Aabb kUnitAabb =
  375. {
  376. { -1.0f, -1.0f, -1.0f },
  377. { 1.0f, 1.0f, 1.0f },
  378. };
  379. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit)
  380. {
  381. Aabb aabb;
  382. toAabb(aabb, _obb);
  383. if (!intersect(_ray, aabb) )
  384. {
  385. return false;
  386. }
  387. float mtxInv[16];
  388. mtxInverse(mtxInv, _obb.mtx);
  389. Ray obbRay;
  390. obbRay.pos = mul(_ray.pos, mtxInv);
  391. obbRay.dir = mulXyz0(_ray.dir, mtxInv);
  392. if (intersect(obbRay, kUnitAabb, _hit) )
  393. {
  394. if (NULL != _hit)
  395. {
  396. _hit->pos = mul(_hit->pos, _obb.mtx);
  397. const Vec3 tmp = mulXyz0(_hit->plane.normal, _obb.mtx);
  398. _hit->plane.normal = normalize(tmp);
  399. }
  400. return true;
  401. }
  402. return false;
  403. }
  404. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit)
  405. {
  406. Plane plane;
  407. plane.normal = _disk.normal;
  408. plane.dist = -dot(_disk.center, _disk.normal);
  409. Hit tmpHit;
  410. _hit = NULL != _hit ? _hit : &tmpHit;
  411. if (intersect(_ray, plane, _hit) )
  412. {
  413. const Vec3 tmp = sub(_disk.center, _hit->pos);
  414. return dot(tmp, tmp) <= square(_disk.radius);
  415. }
  416. return false;
  417. }
  418. static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit)
  419. {
  420. Vec3 axis = sub(_cylinder.end, _cylinder.pos);
  421. const Vec3 rc = sub(_ray.pos, _cylinder.pos);
  422. const Vec3 dxa = cross(_ray.dir, axis);
  423. const float len = length(dxa);
  424. const Vec3 normal = normalize(dxa);
  425. const float dist = bx::abs(dot(rc, normal) );
  426. if (dist > _cylinder.radius)
  427. {
  428. return false;
  429. }
  430. Vec3 vo = cross(rc, axis);
  431. const float t0 = -dot(vo, normal) / len;
  432. vo = normalize(cross(normal, axis) );
  433. const float rsq = square(_cylinder.radius);
  434. const float ddoto = dot(_ray.dir, vo);
  435. const float ss = t0 - bx::abs(sqrt(rsq - square(dist) ) / ddoto);
  436. if (0.0f > ss)
  437. {
  438. return false;
  439. }
  440. const Vec3 point = getPointAt(_ray, ss);
  441. const float axisLen = length(axis);
  442. axis = normalize(axis);
  443. const float pdota = dot(_cylinder.pos, axis);
  444. const float height = dot(point, axis) - pdota;
  445. if (0.0f < height
  446. && axisLen > height)
  447. {
  448. if (NULL != _hit)
  449. {
  450. const float t1 = height / axisLen;
  451. const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
  452. _hit->pos = point;
  453. const Vec3 tmp = sub(point, pointOnAxis);
  454. _hit->plane.normal = normalize(tmp);
  455. _hit->plane.dist = ss;
  456. }
  457. return true;
  458. }
  459. if (_capsule)
  460. {
  461. const float rdota = dot(_ray.pos, axis);
  462. const float pp = rdota - pdota;
  463. const float t1 = pp / axisLen;
  464. const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1);
  465. const Vec3 axisToRay = sub(_ray.pos, pointOnAxis);
  466. if (_cylinder.radius < length(axisToRay)
  467. && 0.0f > ss)
  468. {
  469. return false;
  470. }
  471. Sphere sphere;
  472. sphere.radius = _cylinder.radius;
  473. sphere.center = 0.0f >= height
  474. ? _cylinder.pos
  475. : _cylinder.end
  476. ;
  477. return intersect(_ray, sphere, _hit);
  478. }
  479. Plane plane;
  480. Vec3 pos;
  481. if (0.0f >= height)
  482. {
  483. plane.normal = neg(axis);
  484. pos = _cylinder.pos;
  485. }
  486. else
  487. {
  488. plane.normal = axis;
  489. pos = _cylinder.end;
  490. }
  491. plane.dist = -dot(pos, plane.normal);
  492. Hit tmpHit;
  493. _hit = NULL != _hit ? _hit : &tmpHit;
  494. if (intersect(_ray, plane, _hit) )
  495. {
  496. const Vec3 tmp = sub(pos, _hit->pos);
  497. return dot(tmp, tmp) <= rsq;
  498. }
  499. return false;
  500. }
  501. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit)
  502. {
  503. return intersect(_ray, _cylinder, false, _hit);
  504. }
  505. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit)
  506. {
  507. BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) );
  508. return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit);
  509. }
  510. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
  511. {
  512. const Vec3 axis = sub(_cone.pos, _cone.end);
  513. const float len = length(axis);
  514. const Vec3 normal = normalize(axis);
  515. Disk disk;
  516. disk.center = _cone.pos;
  517. disk.normal = normal;
  518. disk.radius = _cone.radius;
  519. Hit tmpInt;
  520. Hit* out = NULL != _hit ? _hit : &tmpInt;
  521. bool hit = intersect(_ray, disk, out);
  522. const Vec3 ro = sub(_ray.pos, _cone.end);
  523. const float hyp = sqrt(square(_cone.radius) + square(len) );
  524. const float cosaSq = square(len/hyp);
  525. const float ndoto = dot(normal, ro);
  526. const float ndotd = dot(normal, _ray.dir);
  527. const float aa = square(ndotd) - cosaSq;
  528. const float bb = 2.0f * (ndotd*ndoto - dot(_ray.dir, ro)*cosaSq);
  529. const float cc = square(ndoto) - dot(ro, ro)*cosaSq;
  530. float det = bb*bb - 4.0f*aa*cc;
  531. if (0.0f > det)
  532. {
  533. return hit;
  534. }
  535. det = sqrt(det);
  536. const float invA2 = 1.0f / (2.0f*aa);
  537. const float t1 = (-bb - det) * invA2;
  538. const float t2 = (-bb + det) * invA2;
  539. float tt = t1;
  540. if (0.0f > t1
  541. || (0.0f < t2 && t2 < t1) )
  542. {
  543. tt = t2;
  544. }
  545. if (0.0f > tt)
  546. {
  547. return hit;
  548. }
  549. const Vec3 hitPos = getPointAt(_ray, tt);
  550. const Vec3 point = sub(hitPos, _cone.end);
  551. const float hh = dot(normal, point);
  552. if (0.0f > hh
  553. || len < hh)
  554. {
  555. return hit;
  556. }
  557. if (NULL != _hit)
  558. {
  559. if (!hit
  560. || tt < _hit->plane.dist)
  561. {
  562. _hit->plane.dist = tt;
  563. _hit->pos = hitPos;
  564. const float scale = hh / dot(point, point);
  565. const Vec3 pointScaled = mul(point, scale);
  566. const Vec3 tmp = sub(pointScaled, normal);
  567. _hit->plane.normal = normalize(tmp);
  568. }
  569. }
  570. return true;
  571. }
  572. bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
  573. {
  574. const float dist = distance(_plane, _ray.pos);
  575. if (0.0f > dist)
  576. {
  577. return false;
  578. }
  579. const float ndotd = dot(_ray.dir, _plane.normal);
  580. if (0.0f < ndotd)
  581. {
  582. return false;
  583. }
  584. if (NULL != _hit)
  585. {
  586. _hit->plane.normal = _plane.normal;
  587. float tt = -dist/ndotd;
  588. _hit->plane.dist = tt;
  589. _hit->pos = getPointAt(_ray, tt);
  590. }
  591. return true;
  592. }
  593. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit)
  594. {
  595. const Vec3 rs = sub(_ray.pos, _sphere.center);
  596. const float bb = dot(rs, _ray.dir);
  597. if (0.0f < bb)
  598. {
  599. return false;
  600. }
  601. const float aa = dot(_ray.dir, _ray.dir);
  602. const float cc = dot(rs, rs) - square(_sphere.radius);
  603. const float discriminant = bb*bb - aa*cc;
  604. if (0.0f >= discriminant)
  605. {
  606. return false;
  607. }
  608. const float sqrtDiscriminant = sqrt(discriminant);
  609. const float invA = 1.0f / aa;
  610. const float tt = -(bb + sqrtDiscriminant)*invA;
  611. if (0.0f >= tt)
  612. {
  613. return false;
  614. }
  615. if (NULL != _hit)
  616. {
  617. _hit->plane.dist = tt;
  618. const Vec3 point = getPointAt(_ray, tt);
  619. _hit->pos = point;
  620. const Vec3 tmp = sub(point, _sphere.center);
  621. _hit->plane.normal = normalize(tmp);
  622. }
  623. return true;
  624. }
  625. bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit)
  626. {
  627. const Vec3 edge10 = sub(_triangle.v1, _triangle.v0);
  628. const Vec3 edge02 = sub(_triangle.v0, _triangle.v2);
  629. const Vec3 normal = cross(edge02, edge10);
  630. const Vec3 vo = sub(_triangle.v0, _ray.pos);
  631. const Vec3 dxo = cross(_ray.dir, vo);
  632. const float det = dot(normal, _ray.dir);
  633. if (0.0f < det)
  634. {
  635. return false;
  636. }
  637. const float invDet = 1.0f/det;
  638. const float bz = dot(dxo, edge02) * invDet;
  639. const float by = dot(dxo, edge10) * invDet;
  640. const float bx = 1.0f - by - bz;
  641. if (0.0f > bx
  642. || 0.0f > by
  643. || 0.0f > bz)
  644. {
  645. return false;
  646. }
  647. if (NULL != _hit)
  648. {
  649. _hit->plane.normal = normalize(normal);
  650. const float tt = dot(normal, vo) * invDet;
  651. _hit->plane.dist = tt;
  652. _hit->pos = getPointAt(_ray, tt);
  653. }
  654. return true;
  655. }
  656. void barycentric(float& _outU, float& _outV, float& _outW, const Triangle& _triangle, const Vec3& _pos)
  657. {
  658. const Vec3 v0 = sub(_triangle.v1, _triangle.v0);
  659. const Vec3 v1 = sub(_triangle.v2, _triangle.v0);
  660. const Vec3 v2 = sub(_pos, _triangle.v0);
  661. const float dot00 = dot(v0, v0);
  662. const float dot01 = dot(v0, v1);
  663. const float dot02 = dot(v0, v2);
  664. const float dot11 = dot(v1, v1);
  665. const float dot12 = dot(v1, v2);
  666. const float invDenom = 1.0f/(dot00*dot11 - square(dot01) );
  667. _outU = (dot11*dot02 - dot01*dot12)*invDenom;
  668. _outV = (dot00*dot12 - dot01*dot02)*invDenom;
  669. _outW = 1.0f - _outU - _outV;
  670. }
  671. Vec3 closestPoint(const Plane& _plane, const Vec3 _pos)
  672. {
  673. const float dist = distance(_plane, _pos);
  674. return sub(_pos, mul(_plane.normal, dist) );
  675. }
  676. Vec3 closestPoint(const Aabb& _aabb, const Vec3 _pos)
  677. {
  678. return clamp(_pos, _aabb.min, _aabb.max);
  679. }
  680. bool overlap(const Sphere& _sphere, const Vec3& _pos)
  681. {
  682. const Vec3 ba = sub(_sphere.center, _pos);
  683. const float rsq = square(_sphere.radius);
  684. return dot(ba, ba) <= rsq;
  685. }
  686. bool overlap(const Sphere& _sphereA, const Sphere& _sphereB)
  687. {
  688. const Vec3 ba = sub(_sphereA.center, _sphereB.center);
  689. const float rsq = square(_sphereA.radius + _sphereB.radius);
  690. return dot(ba, ba) <= rsq;
  691. }
  692. bool overlap(const Sphere& _sphere, const Aabb& _aabb)
  693. {
  694. const Vec3 pos = closestPoint(_aabb, _sphere.center);
  695. return overlap(_sphere, pos);
  696. }
  697. bool overlap(const Sphere& _sphere, const Plane& _plane)
  698. {
  699. return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius;
  700. }
  701. bool overlap(const Sphere& _sphere, const Triangle& _triangle)
  702. {
  703. Plane plane;
  704. calcPlane(plane, _triangle.v0, _triangle.v1, _triangle.v2);
  705. if (!overlap(_sphere, plane) )
  706. {
  707. return false;
  708. }
  709. const Vec3 pos = closestPoint(plane, _sphere.center);
  710. float uu, vv, ww;
  711. barycentric(uu, vv, ww, _triangle, pos);
  712. const float nr = -_sphere.radius;
  713. return uu >= nr
  714. && vv >= nr
  715. && ww >= nr
  716. ;
  717. }
  718. bool overlap(const Sphere& _sphere, const Cylinder& _cylinder)
  719. {
  720. BX_UNUSED(_sphere, _cylinder);
  721. return false;
  722. }
  723. bool overlap(const Sphere& _sphere, const Capsule& _capsule)
  724. {
  725. BX_UNUSED(_sphere, _capsule);
  726. return false;
  727. }
  728. bool overlap(const Sphere& _sphere, const Cone& _cone)
  729. {
  730. BX_UNUSED(_sphere, _cone);
  731. return false;
  732. }
  733. bool overlap(const Sphere& _sphere, const Disk& _disk)
  734. {
  735. if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) )
  736. {
  737. return false;
  738. }
  739. bx::Plane plane;
  740. bx::calcPlane(plane, _disk.normal, _disk.center);
  741. return overlap(_sphere, plane);
  742. }
  743. bool overlap(const Sphere& _sphere, const Obb& _obb)
  744. {
  745. BX_UNUSED(_sphere, _obb);
  746. return false;
  747. }
  748. bool overlap(const Aabb& _aabb, const Vec3& _pos)
  749. {
  750. const Vec3 ac = getCenter(_aabb);
  751. const Vec3 ae = getExtents(_aabb);
  752. const Vec3 abc = bx::abs(sub(ac, _pos) );
  753. return abc.x <= ae.x
  754. && abc.y <= ae.y
  755. && abc.z <= ae.z
  756. ;
  757. }
  758. bool overlap(const Aabb& _aabb, const Sphere& _sphere)
  759. {
  760. return overlap(_sphere, _aabb);
  761. }
  762. uint32_t overlapTestMask(const Aabb& _aabbA, const Aabb& _aabbB)
  763. {
  764. /// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
  765. /// test.
  766. const uint32_t ltMinX = _aabbA.max.x < _aabbB.min.x;
  767. const uint32_t gtMaxX = _aabbA.min.x > _aabbB.max.x;
  768. const uint32_t ltMinY = _aabbA.max.y < _aabbB.min.y;
  769. const uint32_t gtMaxY = _aabbA.min.y > _aabbB.max.y;
  770. const uint32_t ltMinZ = _aabbA.max.z < _aabbB.min.z;
  771. const uint32_t gtMaxZ = _aabbA.min.z > _aabbB.max.z;
  772. return 0
  773. | (ltMinX << 0)
  774. | (gtMaxX << 1)
  775. | (ltMinY << 2)
  776. | (gtMaxY << 3)
  777. | (ltMinZ << 4)
  778. | (gtMaxZ << 5)
  779. ;
  780. }
  781. bool overlap(const Aabb& _aabbA, const Aabb& _aabbB)
  782. {
  783. #if 0
  784. return 0 != overlapTestMask(_aabbA, _aabbB);
  785. #else
  786. const Vec3 ac = getCenter(_aabbA);
  787. const Vec3 bc = getCenter(_aabbB);
  788. const Vec3 abc = bx::abs(sub(ac, bc) );
  789. const Vec3 ae = getExtents(_aabbA);
  790. const Vec3 be = getExtents(_aabbB);
  791. const Vec3 abe = add(ae, be);
  792. return abc.x <= abe.x
  793. && abc.y <= abe.y
  794. && abc.z <= abe.z
  795. ;
  796. #endif // 0
  797. }
  798. bool overlap(const Aabb& _aabb, const Plane& _plane)
  799. {
  800. const Vec3 center = getCenter(_aabb);
  801. const float dist = distance(_plane, center);
  802. const Vec3 extents = getExtents(_aabb);
  803. const Vec3 normal = bx::abs(_plane.normal);
  804. const float radius = dot(extents, normal);
  805. return bx::abs(dist) <= radius;
  806. }
  807. bool overlap(const Aabb& _aabb, const Triangle& _triangle)
  808. {
  809. Plane plane;
  810. calcPlane(plane, _triangle.v0, _triangle.v1, _triangle.v2);
  811. if (!overlap(_aabb, plane) )
  812. {
  813. return false;
  814. }
  815. BX_UNUSED(_aabb, _triangle);
  816. return false;
  817. }
  818. bool overlap(const Aabb& _aabb, const Cylinder& _cylinder)
  819. {
  820. BX_UNUSED(_aabb, _cylinder);
  821. return false;
  822. }
  823. bool overlap(const Aabb& _aabb, const Capsule& _capsule)
  824. {
  825. BX_UNUSED(_aabb, _capsule);
  826. return false;
  827. }
  828. bool overlap(const Aabb& _aabb, const Cone& _cone)
  829. {
  830. BX_UNUSED(_aabb, _cone);
  831. return false;
  832. }
  833. bool overlap(const Aabb& _aabb, const Disk& _disk)
  834. {
  835. if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) )
  836. {
  837. return false;
  838. }
  839. bx::Plane plane;
  840. bx::calcPlane(plane, _disk.normal, _disk.center);
  841. return overlap(_aabb, plane);
  842. }
  843. bool overlap(const Aabb& _aabb, const Obb& _obb)
  844. {
  845. BX_UNUSED(_aabb, _obb);
  846. return false;
  847. }
  848. bool overlap(const Triangle& _triangle, const bx::Vec3& _pos)
  849. {
  850. float uu, vv, ww;
  851. barycentric(uu, vv, ww, _triangle, _pos);
  852. return uu >= 0.0f
  853. && vv >= 0.0f
  854. && ww >= 0.0f
  855. ;
  856. }
  857. bool overlap(const Triangle& _triangle, const Sphere& _sphere)
  858. {
  859. return overlap(_sphere, _triangle);
  860. }
  861. bool overlap(const Triangle& _triangle, const Aabb& _aabb)
  862. {
  863. return overlap(_aabb, _triangle);
  864. }
  865. bool overlap(const Triangle& _triangle, const bx::Plane& _plane)
  866. {
  867. const float dist0 = distance(_plane, _triangle.v0);
  868. const float dist1 = distance(_plane, _triangle.v1);
  869. const float dist2 = distance(_plane, _triangle.v2);
  870. const float minDist = min(dist0, dist1, dist2);
  871. const float maxDist = max(dist0, dist1, dist2);
  872. return 0.0f > minDist
  873. && 0.0f < maxDist
  874. ;
  875. }
  876. bool overlap(const Triangle& _triangleA, const Triangle& _triangleB)
  877. {
  878. BX_UNUSED(_triangleA, _triangleB);
  879. return false;
  880. }
  881. bool overlap(const Triangle& _triangle, const Cylinder& _cylinder)
  882. {
  883. BX_UNUSED(_triangle, _cylinder);
  884. return false;
  885. }
  886. bool overlap(const Triangle& _triangle, const Capsule& _capsule)
  887. {
  888. BX_UNUSED(_triangle, _capsule);
  889. return false;
  890. }
  891. bool overlap(const Triangle& _triangle, const Cone& _cone)
  892. {
  893. BX_UNUSED(_triangle, _cone);
  894. return false;
  895. }
  896. bool overlap(const Triangle& _triangle, const Disk& _disk)
  897. {
  898. if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) )
  899. {
  900. return false;
  901. }
  902. bx::Plane plane;
  903. bx::calcPlane(plane, _disk.normal, _disk.center);
  904. return overlap(_triangle, plane);
  905. }
  906. bool overlap(const Triangle& _triangle, const Obb& _obb)
  907. {
  908. BX_UNUSED(_triangle, _obb);
  909. return false;
  910. }