CompoundShape.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/CollisionShape.h>
  7. #include <anki/util/NonCopyable.h>
  8. namespace anki
  9. {
  10. /// @addtogroup collision
  11. /// @{
  12. /// A shape that contains other shapes
  13. class CompoundShape : public CollisionShape, public NonCopyable
  14. {
  15. public:
  16. static Bool classof(const CollisionShape& c)
  17. {
  18. return c.getType() == Type::COMPOUND;
  19. }
  20. CompoundShape();
  21. /// Implements CollisionShape::testPlane
  22. F32 testPlane(const Plane& p) const;
  23. /// Implements CollisionShape::transform
  24. void transform(const Transform& trf);
  25. /// Implements CollisionShape::computeAabb
  26. void computeAabb(Aabb& b) const;
  27. /// Implements CollisionShape::accept
  28. void accept(MutableVisitor& v);
  29. /// Implements CollisionShape::accept
  30. void accept(ConstVisitor& v) const;
  31. /// The compound shape will not take ownership of the object
  32. void addShape(CollisionShape* shape);
  33. template<typename TFunc>
  34. ANKI_USE_RESULT Error iterateShapes(TFunc f) const;
  35. private:
  36. static const U SHAPES_PER_CHUNK_COUNT = 8;
  37. class Chunk
  38. {
  39. public:
  40. Array<CollisionShape*, SHAPES_PER_CHUNK_COUNT> m_shapes;
  41. Chunk* m_next = nullptr;
  42. };
  43. Chunk m_dflt;
  44. };
  45. //==============================================================================
  46. template<typename TFunc>
  47. Error CompoundShape::iterateShapes(TFunc f) const
  48. {
  49. Error err = ErrorCode::NONE;
  50. U count = 0;
  51. const Chunk* chunk = &m_dflt;
  52. do
  53. {
  54. U idx = SHAPES_PER_CHUNK_COUNT;
  55. while(idx-- != 0)
  56. {
  57. if(chunk->m_shapes[idx])
  58. {
  59. err = f(*const_cast<CollisionShape*>(chunk->m_shapes[idx]));
  60. if(err)
  61. {
  62. return err;
  63. }
  64. ++count;
  65. }
  66. }
  67. chunk = chunk->m_next;
  68. } while(chunk);
  69. ANKI_ASSERT(count > 0 && "Empty CompoundShape");
  70. (void)count;
  71. return err;
  72. }
  73. /// @}
  74. } // end namespace anki