bounds.h 3.8 KB

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