CollisionShape.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/collision/Forward.h>
  7. #include <anki/collision/Common.h>
  8. #include <anki/Math.h>
  9. #include <anki/util/StdTypes.h>
  10. #include <anki/util/Visitor.h>
  11. namespace anki
  12. {
  13. /// @addtogroup collision
  14. /// @{
  15. /// Abstract class for collision shapes. It also features a visitor for
  16. /// implementing specific code outside the collision codebase (eg rendering)
  17. class CollisionShape
  18. {
  19. public:
  20. /// Collision shape type
  21. /// @note WARNING: Order is important
  22. enum class Type : U8
  23. {
  24. PLANE,
  25. LINE_SEG,
  26. COMPOUND,
  27. AABB,
  28. SPHERE,
  29. OBB,
  30. CONVEX_HULL,
  31. COUNT,
  32. LAST_CONVEX = CONVEX_HULL
  33. };
  34. /// Generic mutable visitor
  35. class MutableVisitor
  36. {
  37. public:
  38. virtual ~MutableVisitor()
  39. {
  40. }
  41. virtual void visit(LineSegment&) = 0;
  42. virtual void visit(Obb&) = 0;
  43. virtual void visit(Plane&) = 0;
  44. virtual void visit(Sphere&) = 0;
  45. virtual void visit(Aabb&) = 0;
  46. virtual void visit(CompoundShape&) = 0;
  47. virtual void visit(ConvexHullShape&) = 0;
  48. };
  49. /// Generic const visitor
  50. class ConstVisitor
  51. {
  52. public:
  53. virtual ~ConstVisitor()
  54. {
  55. }
  56. virtual void visit(const LineSegment&) = 0;
  57. virtual void visit(const Obb&) = 0;
  58. virtual void visit(const Plane&) = 0;
  59. virtual void visit(const Sphere&) = 0;
  60. virtual void visit(const Aabb&) = 0;
  61. virtual void visit(const CompoundShape&) = 0;
  62. virtual void visit(const ConvexHullShape&) = 0;
  63. };
  64. CollisionShape(Type cid)
  65. : m_cid(cid)
  66. {
  67. }
  68. CollisionShape(const CollisionShape& b)
  69. : m_cid(b.m_cid)
  70. {
  71. operator=(b);
  72. }
  73. virtual ~CollisionShape()
  74. {
  75. }
  76. CollisionShape& operator=(const CollisionShape& b)
  77. {
  78. ANKI_ASSERT(b.m_cid == m_cid);
  79. (void)b;
  80. return *this;
  81. }
  82. Type getType() const
  83. {
  84. return m_cid;
  85. }
  86. /// If the collision shape intersects with the plane then the method
  87. /// returns 0.0, else it returns the distance. If the distance is < 0.0
  88. /// then the collision shape lies behind the plane and if > 0.0 then
  89. /// in front of it
  90. virtual F32 testPlane(const Plane& p) const = 0;
  91. /// Transform
  92. virtual void transform(const Transform& trf) = 0;
  93. /// Get the AABB
  94. virtual void computeAabb(Aabb&) const = 0;
  95. /// Visitor accept
  96. virtual void accept(MutableVisitor&) = 0;
  97. /// Visitor accept
  98. virtual void accept(ConstVisitor&) const = 0;
  99. protected:
  100. /// Function that iterates a point cloud
  101. template<typename TFunc>
  102. void iteratePointCloud(
  103. const void* buff, U count, PtrSize stride, PtrSize buffSize, TFunc func)
  104. {
  105. ANKI_ASSERT(buff);
  106. ANKI_ASSERT(count > 1);
  107. ANKI_ASSERT(stride >= sizeof(Vec3));
  108. ANKI_ASSERT(buffSize >= stride * count);
  109. const U8* ptr = (const U8*)buff;
  110. while(count-- != 0)
  111. {
  112. ANKI_ASSERT(
  113. (((PtrSize)ptr + sizeof(Vec3)) - (PtrSize)buff) <= buffSize);
  114. const Vec3* pos = (const Vec3*)(ptr);
  115. func(*pos);
  116. ptr += stride;
  117. }
  118. }
  119. private:
  120. /// Keep an ID to avoid (in some cases) the visitor and thus the cost of
  121. /// virtuals
  122. Type m_cid;
  123. };
  124. /// @}
  125. } // end namespace anki