Aabb.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2009-2021, 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. {
  9. /// @addtogroup collision
  10. /// @{
  11. /// Axis align bounding box collision shape.
  12. class Aabb
  13. {
  14. public:
  15. static constexpr CollisionShapeType CLASS_TYPE = CollisionShapeType::AABB;
  16. /// Will not initialize any memory, nothing.
  17. Aabb()
  18. {
  19. }
  20. Aabb(const Vec4& min, const Vec4& max)
  21. : m_min(min)
  22. , m_max(max)
  23. {
  24. check();
  25. }
  26. Aabb(const Vec3& min, const Vec3& max)
  27. : Aabb(Vec4(min, 0.0f), Vec4(max, 0.0f))
  28. {
  29. check();
  30. }
  31. /// Set from point cloud.
  32. Aabb(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
  33. {
  34. setFromPointCloud(pointBuffer, pointCount, pointStride, buffSize);
  35. }
  36. /// Copy.
  37. Aabb(const Aabb& b)
  38. {
  39. operator=(b);
  40. }
  41. /// Copy.
  42. Aabb& operator=(const Aabb& b)
  43. {
  44. b.check();
  45. m_min = b.m_min;
  46. m_max = b.m_max;
  47. return *this;
  48. }
  49. const Vec4& getMin() const
  50. {
  51. check();
  52. return m_min;
  53. }
  54. void setMin(const Vec4& x)
  55. {
  56. ANKI_ASSERT(x.w() == 0.0f);
  57. m_min = x;
  58. }
  59. void setMin(const Vec3& x)
  60. {
  61. setMin(Vec4(x, 0.0f));
  62. }
  63. const Vec4& getMax() const
  64. {
  65. check();
  66. return m_max;
  67. }
  68. void setMax(const Vec4& x)
  69. {
  70. ANKI_ASSERT(x.w() == 0.0f);
  71. m_max = x;
  72. }
  73. void setMax(const Vec3& x)
  74. {
  75. setMax(Vec4(x, 0.0f));
  76. }
  77. /// Compute the GJK support.
  78. ANKI_USE_RESULT Vec4 computeSupport(const Vec4& dir) const;
  79. /// It uses a nice trick to avoid unwanted calculations
  80. Aabb getTransformed(const Transform& transform) const;
  81. /// Get a collision shape that includes this and the given. Its not very accurate
  82. Aabb getCompoundShape(const Aabb& b) const;
  83. /// Calculate from a set of points
  84. void setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize);
  85. private:
  86. Vec4 m_min
  87. #if ANKI_ENABLE_ASSERTIONS
  88. = Vec4(MAX_F32)
  89. #endif
  90. ;
  91. Vec4 m_max
  92. #if ANKI_ENABLE_ASSERTIONS
  93. = Vec4(MIN_F32)
  94. #endif
  95. ;
  96. void check() const
  97. {
  98. ANKI_ASSERT(m_min.xyz() < m_max.xyz());
  99. ANKI_ASSERT(m_min.w() == 0.0f);
  100. ANKI_ASSERT(m_max.w() == 0.0f);
  101. }
  102. };
  103. /// @}
  104. } // end namespace anki