Obb.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <AnKi/Math.h>
  8. #include <AnKi/Util/Array.h>
  9. namespace anki {
  10. /// @addtogroup collision
  11. /// @{
  12. /// Object oriented bounding box.
  13. class Obb
  14. {
  15. public:
  16. static constexpr CollisionShapeType kClassType = CollisionShapeType::kObb;
  17. /// Will not initialize any memory, nothing.
  18. Obb()
  19. {
  20. }
  21. Obb(const Obb& b)
  22. {
  23. operator=(b);
  24. }
  25. Obb(const Vec4& center, const Mat3x4& rotation, const Vec4& extend)
  26. : m_center(center)
  27. , m_extend(extend)
  28. , m_rotation(rotation)
  29. {
  30. check();
  31. }
  32. /// Set from point cloud.
  33. Obb(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
  34. {
  35. setFromPointCloud(pointBuffer, pointCount, pointStride, buffSize);
  36. }
  37. Obb& operator=(const Obb& b)
  38. {
  39. m_center = b.m_center;
  40. m_rotation = b.m_rotation;
  41. m_extend = b.m_extend;
  42. return *this;
  43. }
  44. const Vec4& getCenter() const
  45. {
  46. check();
  47. return m_center;
  48. }
  49. void setCenter(const Vec4& x)
  50. {
  51. m_center = x;
  52. }
  53. const Mat3x4& getRotation() const
  54. {
  55. check();
  56. return m_rotation;
  57. }
  58. void setRotation(const Mat3x4& x)
  59. {
  60. m_rotation = x;
  61. }
  62. const Vec4& getExtend() const
  63. {
  64. check();
  65. return m_extend;
  66. }
  67. void setExtend(const Vec4& x)
  68. {
  69. m_extend = x;
  70. }
  71. Obb getTransformed(const Transform& trf) const
  72. {
  73. check();
  74. Obb out;
  75. out.m_extend = m_extend * trf.getScale();
  76. out.m_center = trf.transform(m_center);
  77. out.m_rotation = trf.getRotation().combineTransformations(m_rotation);
  78. return out;
  79. }
  80. /// Get a collision shape that includes this and the given. It's not very accurate.
  81. Obb getCompoundShape(const Obb& b) const;
  82. /// Calculate from a set of points.
  83. void setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize);
  84. /// Get extreme points in 3D space
  85. void getExtremePoints(Array<Vec4, 8>& points) const;
  86. /// Compute the GJK support.
  87. [[nodiscard]] Vec4 computeSupport(const Vec4& dir) const;
  88. private:
  89. Vec4 m_center
  90. #if ANKI_ASSERTIONS_ENABLED
  91. = Vec4(kMaxF32)
  92. #endif
  93. ;
  94. Vec4 m_extend /// With identity rotation this points to max (front, right, top in our case)
  95. #if ANKI_ASSERTIONS_ENABLED
  96. = Vec4(kMaxF32)
  97. #endif
  98. ;
  99. Mat3x4 m_rotation
  100. #if ANKI_ASSERTIONS_ENABLED
  101. = Mat3x4(kMaxF32)
  102. #endif
  103. ;
  104. void check() const
  105. {
  106. ANKI_ASSERT(m_center != Vec4(kMaxF32));
  107. ANKI_ASSERT(m_extend != Vec4(kMaxF32));
  108. ANKI_ASSERT(m_rotation != Mat3x4(kMaxF32));
  109. ANKI_ASSERT(m_center.w == 0.0f && m_extend.w == 0.0f);
  110. }
  111. };
  112. /// @}
  113. } // end namespace anki