bounds.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2011-2018 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. struct Aabb
  8. {
  9. float m_min[3];
  10. float m_max[3];
  11. };
  12. struct Cylinder
  13. {
  14. float m_pos[3];
  15. float m_end[3];
  16. float m_radius;
  17. };
  18. struct Capsule
  19. {
  20. float m_pos[3];
  21. float m_end[3];
  22. float m_radius;
  23. };
  24. struct Cone
  25. {
  26. float m_pos[3];
  27. float m_end[3];
  28. float m_radius;
  29. };
  30. struct Disk
  31. {
  32. float m_center[3];
  33. float m_normal[3];
  34. float m_radius;
  35. };
  36. struct Obb
  37. {
  38. float m_mtx[16];
  39. };
  40. struct Plane
  41. {
  42. float m_normal[3];
  43. float m_dist;
  44. };
  45. struct Ray
  46. {
  47. float m_pos[3];
  48. float m_dir[3];
  49. };
  50. struct Sphere
  51. {
  52. float m_center[3];
  53. float m_radius;
  54. };
  55. struct Tris
  56. {
  57. float m_v0[3];
  58. float m_v1[3];
  59. float m_v2[3];
  60. };
  61. struct Hit
  62. {
  63. float m_pos[3];
  64. float m_normal[3];
  65. float m_dist;
  66. };
  67. /// Convert axis aligned bounding box to oriented bounding box.
  68. void aabbToObb(Obb& _obb, const Aabb& _aabb);
  69. /// Convert oriented bounding box to axis aligned bounding box.
  70. void toAabb(Aabb& _aabb, const Obb& _obb);
  71. /// Convert sphere to axis aligned bounding box.
  72. void toAabb(Aabb& _aabb, const Sphere& _sphere);
  73. /// Convert disk to axis aligned bounding box.
  74. void toAabb(Aabb& _aabb, const Disk& _disk);
  75. /// Convert cylinder to axis aligned bounding box.
  76. void toAabb(Aabb& _aabb, const Cylinder& _cylinder);
  77. /// Calculate axis aligned bounding box.
  78. void toAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
  79. /// Transform vertices and calculate axis aligned bounding box.
  80. void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
  81. /// Expand AABB.
  82. void aabbExpand(Aabb& _aabb, float _factor);
  83. /// Expand AABB with xyz.
  84. void aabbExpand(Aabb& _aabb, const float* _pos);
  85. /// Calculate surface area of axis aligned bounding box.
  86. float calcAreaAabb(const Aabb& _aabb);
  87. /// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
  88. /// test.
  89. uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1);
  90. /// Calculate oriented bounding box.
  91. void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);
  92. /// Calculate maximum bounding sphere.
  93. void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
  94. /// Calculate minimum bounding sphere.
  95. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f);
  96. /// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes.
  97. void buildFrustumPlanes(Plane* _planes, const float* _viewProj);
  98. /// Returns point from 3 intersecting planes.
  99. void intersectPlanes(float _result[3], const Plane& _pa, const Plane& _pb, const Plane& _pc);
  100. /// Make screen space ray from x, y coordinate and inverse view-projection matrix.
  101. Ray makeRay(float _x, float _y, const float* _invVp);
  102. /// Intersect ray / AABB.
  103. bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL);
  104. /// Intersect ray / OBB.
  105. bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL);
  106. /// Intersect ray / cylinder.
  107. bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL);
  108. /// Intersect ray / capsule.
  109. bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL);
  110. /// Intersect ray / cone.
  111. bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL);
  112. /// Intersect ray / disk.
  113. bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL);
  114. /// Intersect ray / plane.
  115. bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit = NULL);
  116. /// Intersect ray / sphere.
  117. bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL);
  118. /// Intersect ray / triangle.
  119. bool intersect(const Ray& _ray, const Tris& _triangle, Hit* _hit = NULL);
  120. #endif // BOUNDS_H_HEADER_GUARD