bounds.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef BOUNDS_H_HEADER_GUARD
  6. #define BOUNDS_H_HEADER_GUARD
  7. #include <bx/math.h>
  8. ///
  9. struct Aabb
  10. {
  11. bx::Vec3 min;
  12. bx::Vec3 max;
  13. };
  14. ///
  15. struct Capsule
  16. {
  17. bx::Vec3 pos;
  18. bx::Vec3 end;
  19. float radius;
  20. };
  21. ///
  22. struct Cone
  23. {
  24. bx::Vec3 pos;
  25. bx::Vec3 end;
  26. float radius;
  27. };
  28. ///
  29. struct Cylinder
  30. {
  31. bx::Vec3 pos;
  32. bx::Vec3 end;
  33. float radius;
  34. };
  35. ///
  36. struct Disk
  37. {
  38. bx::Vec3 center;
  39. bx::Vec3 normal;
  40. float radius;
  41. };
  42. ///
  43. struct Obb
  44. {
  45. float mtx[16];
  46. };
  47. ///
  48. struct Sphere
  49. {
  50. bx::Vec3 center;
  51. float radius;
  52. };
  53. ///
  54. struct Triangle
  55. {
  56. bx::Vec3 v0;
  57. bx::Vec3 v1;
  58. bx::Vec3 v2;
  59. };
  60. ///
  61. struct Ray
  62. {
  63. bx::Vec3 pos;
  64. bx::Vec3 dir;
  65. };
  66. ///
  67. struct Hit
  68. {
  69. bx::Vec3 pos;
  70. bx::Plane plane;
  71. };
  72. ///
  73. bx::Vec3 getCenter(const Aabb& _aabb);
  74. ///
  75. bx::Vec3 getExtents(const Aabb& _aabb);
  76. ///
  77. bx::Vec3 getCenter(const Triangle& _triangle);
  78. ///
  79. void toAabb(Aabb& _outAabb, const bx::Vec3& _extents);
  80. ///
  81. void toAabb(Aabb& _outAabb, const bx::Vec3& _center, const bx::Vec3& _extents);
  82. /// Convert cylinder to axis aligned bounding box.
  83. void toAabb(Aabb& _outAabb, const Cylinder& _cylinder);
  84. /// Convert disk to axis aligned bounding box.
  85. void toAabb(Aabb& _outAabb, const Disk& _disk);
  86. /// Convert oriented bounding box to axis aligned bounding box.
  87. void toAabb(Aabb& _outAabb, const Obb& _obb);
  88. /// Convert sphere to axis aligned bounding box.
  89. void toAabb(Aabb& _outAabb, const Sphere& _sphere);
  90. /// Convert triangle to axis aligned bounding box.
  91. void toAabb(Aabb& _outAabb, const Triangle& _triangle);
  92. /// Calculate axis aligned bounding box.
  93. void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
  94. /// Transform vertices and calculate axis aligned bounding box.
  95. void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
  96. /// Expand AABB.
  97. void aabbExpand(Aabb& _outAabb, float _factor);
  98. /// Expand AABB with xyz.
  99. void aabbExpand(Aabb& _outAabb, const bx::Vec3& _pos);
  100. /// Calculate surface area of axis aligned bounding box.
  101. float calcAreaAabb(const Aabb& _aabb);
  102. /// Convert axis aligned bounding box to oriented bounding box.
  103. void toObb(Obb& _outObb, const Aabb& _aabb);
  104. /// Calculate oriented bounding box.
  105. void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);
  106. /// Calculate maximum bounding sphere.
  107. void calcMaxBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
  108. /// Calculate minimum bounding sphere.
  109. void calcMinBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f);
  110. /// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes.
  111. void buildFrustumPlanes(bx::Plane* _outPlanes, const float* _viewProj);
  112. /// Returns point from 3 intersecting planes.
  113. bx::Vec3 intersectPlanes(const bx::Plane& _pa, const bx::Plane& _pb, const bx::Plane& _pc);
  114. /// Make screen space ray from x, y coordinate and inverse view-projection matrix.
  115. Ray makeRay(float _x, float _y, const float* _invVp);
  116. /// Intersect ray / AABB.
  117. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL);
  118. /// Intersect ray / OBB.
  119. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL);
  120. /// Intersect ray / cylinder.
  121. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL);
  122. /// Intersect ray / capsule.
  123. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL);
  124. /// Intersect ray / cone.
  125. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL);
  126. /// Intersect ray / disk.
  127. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL);
  128. /// Intersect ray / plane.
  129. bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit = NULL);
  130. /// Intersect ray / sphere.
  131. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL);
  132. /// Intersect ray / triangle.
  133. bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL);
  134. ///
  135. bool overlap(const Aabb& _aabb, const bx::Vec3& _pos);
  136. ///
  137. bool overlap(const Aabb& _aabb, const Sphere& _sphere);
  138. ///
  139. bool overlap(const Aabb& _aabbA, const Aabb& _aabbB);
  140. ///
  141. bool overlap(const Aabb& _aabb, const bx::Plane& _plane);
  142. ///
  143. bool overlap(const Aabb& _aabb, const Triangle& _triangle);
  144. ///
  145. bool overlap(const Aabb& _aabb, const Cylinder& _cylinder);
  146. ///
  147. bool overlap(const Aabb& _aabb, const Capsule& _capsule);
  148. ///
  149. bool overlap(const Aabb& _aabb, const Cone& _cone);
  150. ///
  151. bool overlap(const Aabb& _aabb, const Disk& _disk);
  152. ///
  153. bool overlap(const Aabb& _aabb, const Obb& _obb);
  154. ///
  155. bool overlap(const Capsule& _capsule, const bx::Vec3& _pos);
  156. ///
  157. bool overlap(const Capsule& _capsule, const Sphere& _sphere);
  158. ///
  159. bool overlap(const Capsule& _capsule, const Aabb& _aabb);
  160. ///
  161. bool overlap(const Capsule& _capsule, const bx::Plane& _plane);
  162. ///
  163. bool overlap(const Capsule& _capsule, const Triangle& _triangle);
  164. ///
  165. bool overlap(const Capsule& _capsule, const Cylinder& _cylinder);
  166. ///
  167. bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB);
  168. ///
  169. bool overlap(const Capsule& _capsule, const Cone& _cone);
  170. ///
  171. bool overlap(const Capsule& _capsule, const Disk& _disk);
  172. ///
  173. bool overlap(const Capsule& _capsule, const Obb& _obb);
  174. ///
  175. bool overlap(const Cone& _cone, const bx::Vec3& _pos);
  176. ///
  177. bool overlap(const Cone& _cone, const Sphere& _sphere);
  178. ///
  179. bool overlap(const Cone& _cone, const Aabb& _aabb);
  180. ///
  181. bool overlap(const Cone& _cone, const bx::Plane& _plane);
  182. ///
  183. bool overlap(const Cone& _cone, const Triangle& _triangle);
  184. ///
  185. bool overlap(const Cone& _cone, const Cylinder& _cylinder);
  186. ///
  187. bool overlap(const Cone& _cone, const Capsule& _capsule);
  188. ///
  189. bool overlap(const Cone& _coneA, const Cone& _coneB);
  190. ///
  191. bool overlap(const Cone& _cone, const Disk& _disk);
  192. ///
  193. bool overlap(const Cone& _cone, const Obb& _obb);
  194. ///
  195. bool overlap(const Cylinder& _cylinder, const bx::Vec3& _pos);
  196. ///
  197. bool overlap(const Cylinder& _cylinder, const Sphere& _sphere);
  198. ///
  199. bool overlap(const Cylinder& _cylinder, const Aabb& _aabb);
  200. ///
  201. bool overlap(const Cylinder& _cylinder, const bx::Plane& _plane);
  202. ///
  203. bool overlap(const Cylinder& _cylinder, const Triangle& _triangle);
  204. ///
  205. bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB);
  206. ///
  207. bool overlap(const Cylinder& _cylinder, const Capsule& _capsule);
  208. ///
  209. bool overlap(const Cylinder& _cylinder, const Cone& _cone);
  210. ///
  211. bool overlap(const Cylinder& _cylinder, const Disk& _disk);
  212. ///
  213. bool overlap(const Cylinder& _cylinder, const Obb& _obb);
  214. ///
  215. bool overlap(const Disk& _disk, const bx::Vec3& _pos);
  216. ///
  217. bool overlap(const Disk& _disk, const Sphere& _sphere);
  218. ///
  219. bool overlap(const Disk& _disk, const Aabb& _aabb);
  220. ///
  221. bool overlap(const Disk& _disk, const bx::Plane& _plane);
  222. ///
  223. bool overlap(const Disk& _disk, const Triangle& _triangle);
  224. ///
  225. bool overlap(const Disk& _disk, const Cylinder& _cylinder);
  226. ///
  227. bool overlap(const Disk& _disk, const Capsule& _capsule);
  228. ///
  229. bool overlap(const Disk& _disk, const Cone& _cone);
  230. ///
  231. bool overlap(const Disk& _diskA, const Disk& _diskB);
  232. ///
  233. bool overlap(const Disk& _disk, const Obb& _obb);
  234. ///
  235. bool overlap(const Obb& _obb, const bx::Vec3& _pos);
  236. ///
  237. bool overlap(const Obb& _obb, const Sphere& _sphere);
  238. ///
  239. bool overlap(const Obb& _obb, const Aabb& _aabb);
  240. ///
  241. bool overlap(const Obb& _obb, const bx::Plane& _plane);
  242. ///
  243. bool overlap(const Obb& _obb, const Triangle& _triangle);
  244. ///
  245. bool overlap(const Obb& _obb, const Cylinder& _cylinder);
  246. ///
  247. bool overlap(const Obb& _obb, const Capsule& _capsule);
  248. ///
  249. bool overlap(const Obb& _obb, const Cone& _cone);
  250. ///
  251. bool overlap(const Obb& _obb, const Disk& _disk);
  252. ///
  253. bool overlap(const Obb& _obbA, const Obb& _obbB);
  254. ///
  255. bool overlap(const bx::Plane& _plane, const bx::Vec3& _pos);
  256. ///
  257. bool overlap(const bx::Plane& _plane, const Sphere& _sphere);
  258. ///
  259. bool overlap(const bx::Plane& _plane, const Aabb& _aabb);
  260. ///
  261. bool overlap(const bx::Plane& _planeA, const bx::Plane& _planeB);
  262. ///
  263. bool overlap(const bx::Plane& _plane, const Triangle& _triangle);
  264. ///
  265. bool overlap(const bx::Plane& _plane, const Cylinder& _cylinder);
  266. ///
  267. bool overlap(const bx::Plane& _plane, const Capsule& _capsule);
  268. ///
  269. bool overlap(const bx::Plane& _plane, const Cone& _cone);
  270. ///
  271. bool overlap(const bx::Plane& _plane, const Disk& _disk);
  272. ///
  273. bool overlap(const bx::Plane& _plane, const Obb& _obb);
  274. ///
  275. bool overlap(const Sphere& _sphere, const bx::Vec3& _pos);
  276. ///
  277. bool overlap(const Sphere& _sphereA, const Sphere& _sphereB);
  278. ///
  279. bool overlap(const Sphere& _sphere, const Aabb& _aabb);
  280. ///
  281. bool overlap(const Sphere& _sphere, const bx::Plane& _plane);
  282. ///
  283. bool overlap(const Sphere& _sphere, const Triangle& _triangle);
  284. ///
  285. bool overlap(const Sphere& _sphere, const Cylinder& _cylinder);
  286. ///
  287. bool overlap(const Sphere& _sphere, const Capsule& _capsule);
  288. ///
  289. bool overlap(const Sphere& _sphere, const Cone& _cone);
  290. ///
  291. bool overlap(const Sphere& _sphere, const Disk& _disk);
  292. ///
  293. bool overlap(const Sphere& _sphere, const Obb& _obb);
  294. ///
  295. bool overlap(const Triangle& _triangle, const bx::Vec3& _pos);
  296. ///
  297. bool overlap(const Triangle& _triangle, const Sphere& _sphere);
  298. ///
  299. bool overlap(const Triangle& _triangle, const Aabb& _aabb);
  300. ///
  301. bool overlap(const Triangle& _triangle, const bx::Plane& _plane);
  302. ///
  303. bool overlap(const Triangle& _triangleA, const Triangle& _triangleB);
  304. ///
  305. bool overlap(const Triangle& _triangle, const Cylinder& _cylinder);
  306. ///
  307. bool overlap(const Triangle& _triangle, const Capsule& _capsule);
  308. ///
  309. bool overlap(const Triangle& _triangle, const Cone& _cone);
  310. ///
  311. bool overlap(const Triangle& _triangle, const Disk& _disk);
  312. ///
  313. bool overlap(const Triangle& _triangle, const Obb& _obb);
  314. #endif // BOUNDS_H_HEADER_GUARD