ConvexHullShape.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/Util/WeakArray.h>
  8. namespace anki {
  9. /// @addtogroup collision
  10. /// @{
  11. /// Convex hull collision shape
  12. class ConvexHullShape
  13. {
  14. public:
  15. static constexpr CollisionShapeType kClassType = CollisionShapeType::kConvexHull;
  16. /// Will not initialize any memory, nothing.
  17. ConvexHullShape()
  18. {
  19. }
  20. ConvexHullShape(const ConvexHullShape& other)
  21. {
  22. operator=(other);
  23. }
  24. /// In initializes the storage that holds the point cloud using a predefined buffer. The convex hull is not the
  25. /// owner of the storage.
  26. /// @param points The base of the storage buffer. Size should be @a pointCount * sizeof(Vec4)
  27. /// @param pointCount The number of points
  28. ConvexHullShape(const Vec4* points, U32 pointCount)
  29. : m_trf(Transform::getIdentity())
  30. , m_invTrf(Transform::getIdentity())
  31. , m_points(points)
  32. , m_pointCount(pointCount)
  33. , m_trfIdentity(true)
  34. {
  35. check();
  36. }
  37. ConvexHullShape& operator=(const ConvexHullShape& b)
  38. {
  39. b.check();
  40. m_trf = b.m_trf;
  41. m_invTrf = b.m_invTrf;
  42. m_points = b.m_points;
  43. m_pointCount = b.m_pointCount;
  44. m_trfIdentity = b.m_trfIdentity;
  45. return *this;
  46. }
  47. /// Get points in local space.
  48. ConstWeakArray<Vec4> getPoints() const
  49. {
  50. check();
  51. return ConstWeakArray<Vec4>(m_points, m_pointCount);
  52. }
  53. /// Get current transform.
  54. const Transform& getTransform() const
  55. {
  56. check();
  57. return m_trf;
  58. }
  59. const Transform& getInvertTransform() const
  60. {
  61. check();
  62. return m_invTrf;
  63. }
  64. Bool isTransformIdentity() const
  65. {
  66. return m_trfIdentity;
  67. }
  68. void setTransform(const Transform& trf);
  69. /// Get a transformed.
  70. [[nodiscard]] ConvexHullShape getTransformed(const Transform& trf) const;
  71. /// Compute the GJK support.
  72. [[nodiscard]] Vec4 computeSupport(const Vec4& dir) const;
  73. private:
  74. Transform m_trf;
  75. Transform m_invTrf;
  76. const Vec4* m_points
  77. #if ANKI_ASSERTIONS_ENABLED
  78. = nullptr
  79. #endif
  80. ;
  81. U32 m_pointCount
  82. #if ANKI_ASSERTIONS_ENABLED
  83. = 0
  84. #endif
  85. ;
  86. Bool m_trfIdentity; ///< Optimization.
  87. void check() const
  88. {
  89. ANKI_ASSERT(m_points && m_pointCount > 0);
  90. }
  91. };
  92. /// @}
  93. } // end namespace anki