bounds.h 3.4 KB

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