Aabb.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Collision/Common.h>
  7. namespace anki {
  8. /// @addtogroup collision
  9. /// @{
  10. /// Axis align bounding box collision shape.
  11. class Aabb
  12. {
  13. public:
  14. static constexpr CollisionShapeType kClassType = CollisionShapeType::kAabb;
  15. /// Will not initialize any memory, nothing.
  16. Aabb()
  17. {
  18. }
  19. Aabb(const Vec4& min, const Vec4& max)
  20. : m_min(min)
  21. , m_max(max)
  22. {
  23. check();
  24. }
  25. Aabb(const Vec3& min, const Vec3& max)
  26. : Aabb(Vec4(min, 0.0f), Vec4(max, 0.0f))
  27. {
  28. check();
  29. }
  30. /// Set from point cloud.
  31. Aabb(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
  32. {
  33. setFromPointCloud(pointBuffer, pointCount, pointStride, buffSize);
  34. }
  35. /// Copy.
  36. Aabb(const Aabb& b)
  37. {
  38. operator=(b);
  39. }
  40. /// Copy.
  41. Aabb& operator=(const Aabb& b)
  42. {
  43. b.check();
  44. m_min = b.m_min;
  45. m_max = b.m_max;
  46. return *this;
  47. }
  48. const Vec4& getMin() const
  49. {
  50. check();
  51. return m_min;
  52. }
  53. void setMin(const Vec4& x)
  54. {
  55. ANKI_ASSERT(x.w == 0.0f);
  56. m_min = x;
  57. }
  58. void setMin(const Vec3& x)
  59. {
  60. setMin(Vec4(x, 0.0f));
  61. }
  62. const Vec4& getMax() const
  63. {
  64. check();
  65. return m_max;
  66. }
  67. void setMax(const Vec4& x)
  68. {
  69. ANKI_ASSERT(x.w == 0.0f);
  70. m_max = x;
  71. }
  72. void setMax(const Vec3& x)
  73. {
  74. setMax(Vec4(x, 0.0f));
  75. }
  76. /// Compute the GJK support.
  77. [[nodiscard]] Vec4 computeSupport(const Vec4& dir) const;
  78. /// It uses a nice trick to avoid unwanted calculations
  79. Aabb getTransformed(const Transform& transform) const;
  80. /// Get a collision shape that includes this and the given. Its not very accurate
  81. Aabb getCompoundShape(const Aabb& b) const;
  82. /// Calculate from a set of points
  83. void setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize);
  84. private:
  85. Vec4 m_min
  86. #if ANKI_ASSERTIONS_ENABLED
  87. = Vec4(kMaxF32)
  88. #endif
  89. ;
  90. Vec4 m_max
  91. #if ANKI_ASSERTIONS_ENABLED
  92. = Vec4(kMinF32)
  93. #endif
  94. ;
  95. void check() const
  96. {
  97. ANKI_ASSERT(m_min.xyz < m_max.xyz);
  98. ANKI_ASSERT(m_min.w == 0.0f);
  99. ANKI_ASSERT(m_max.w == 0.0f);
  100. }
  101. };
  102. /// @}
  103. } // end namespace anki