bounds.h 3.8 KB

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