Obb.h 2.6 KB

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