IslandBuilder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Body/BodyID.h>
  5. #include <Jolt/Core/NonCopyable.h>
  6. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  7. #include <atomic>
  8. JPH_SUPPRESS_WARNINGS_STD_END
  9. JPH_NAMESPACE_BEGIN
  10. class TempAllocator;
  11. //#define JPH_VALIDATE_ISLAND_BUILDER
  12. /// Keeps track of connected bodies and builds islands for multithreaded velocity/position update
  13. class IslandBuilder : public NonCopyable
  14. {
  15. public:
  16. /// Destructor
  17. ~IslandBuilder();
  18. /// Initialize the island builder with the maximum amount of bodies that could be active
  19. void Init(uint32 inMaxActiveBodies);
  20. /// Prepare for simulation step by allocating space for the contact constraints
  21. void PrepareContactConstraints(uint32 inMaxContactConstraints, TempAllocator *inTempAllocator);
  22. /// Prepare for simulation step by allocating space for the non-contact constraints
  23. void PrepareNonContactConstraints(uint32 inNumConstraints, TempAllocator *inTempAllocator);
  24. /// Link two bodies by their index in the BodyManager::mActiveBodies list to form islands
  25. void LinkBodies(uint32 inFirst, uint32 inSecond);
  26. /// Link a constraint to a body by their index in the BodyManager::mActiveBodies
  27. void LinkConstraint(uint32 inConstraintIndex, uint32 inFirst, uint32 inSecond);
  28. /// Link a contact to a body by their index in the BodyManager::mActiveBodies
  29. void LinkContact(uint32 inContactIndex, uint32 inFirst, uint32 inSecond);
  30. /// Finalize the islands after all bodies have been Link()-ed
  31. void Finalize(const BodyID *inActiveBodies, uint32 inNumActiveBodies, uint32 inNumContacts, TempAllocator *inTempAllocator);
  32. /// Get the amount of islands formed
  33. uint32 GetNumIslands() const { return mNumIslands; }
  34. /// Get iterator for a particular island, return false if there are no constraints
  35. void GetBodiesInIsland(uint32 inIslandIndex, BodyID *&outBodiesBegin, BodyID *&outBodiesEnd) const;
  36. bool GetConstraintsInIsland(uint32 inIslandIndex, uint32 *&outConstraintsBegin, uint32 *&outConstraintsEnd) const;
  37. bool GetContactsInIsland(uint32 inIslandIndex, uint32 *&outContactsBegin, uint32 *&outContactsEnd) const;
  38. /// After you're done calling the three functions above, call this function to free associated data
  39. void ResetIslands(TempAllocator *inTempAllocator);
  40. private:
  41. /// Returns the index of the lowest body in the group
  42. uint32 GetLowestBodyIndex(uint32 inActiveBodyIndex) const;
  43. #ifdef JPH_VALIDATE_ISLAND_BUILDER
  44. /// Helper function to validate all islands so far generated
  45. void ValidateIslands(uint32 inNumActiveBodies) const;
  46. #endif
  47. // Helper functions to build various islands
  48. void BuildBodyIslands(const BodyID *inActiveBodies, uint32 inNumActiveBodies, TempAllocator *inTempAllocator);
  49. void BuildConstraintIslands(const uint32 *inConstraintToBody, uint32 inNumConstraints, uint32 *&outConstraints, uint32 *&outConstraintsEnd, TempAllocator *inTempAllocator) const;
  50. /// Sorts the islands so that the islands with most constraints go first
  51. void SortIslands(TempAllocator *inTempAllocator);
  52. /// Intermediate data structure that for each body keeps track what the lowest index of the body is that it is connected to
  53. struct BodyLink
  54. {
  55. atomic<uint32> mLinkedTo; ///< An index in mBodyLinks pointing to another body in this island with a lower index than this body
  56. uint32 mIslandIndex; ///< The island index of this body (filled in during Finalize)
  57. };
  58. // Intermediate data
  59. BodyLink * mBodyLinks = nullptr; ///< Maps bodies to the first body in the island
  60. uint32 * mConstraintLinks = nullptr; ///< Maps constraint index to body index (which maps to island index)
  61. uint32 * mContactLinks = nullptr; ///< Maps contact constraint index to body index (which maps to island index)
  62. // Final data
  63. BodyID * mBodyIslands = nullptr; ///< Bodies ordered by island
  64. uint32 * mBodyIslandEnds = nullptr; ///< End index of each body island
  65. uint32 * mConstraintIslands = nullptr; ///< Constraints ordered by island
  66. uint32 * mConstraintIslandEnds = nullptr; ///< End index of each constraint island
  67. uint32 * mContactIslands = nullptr; ///< Contacts ordered by island
  68. uint32 * mContactIslandEnds = nullptr; ///< End index of each contact island
  69. uint32 * mIslandsSorted = nullptr; ///< A list of island indices in order of most constraints first
  70. // Counters
  71. uint32 mMaxActiveBodies; ///< Maximum size of the active bodies list (see BodyManager::mActiveBodies)
  72. uint32 mNumActiveBodies = 0; ///< Number of active bodies passed to
  73. uint32 mNumConstraints = 0; ///< Size of the constraint list (see ConstraintManager::mConstraints)
  74. uint32 mMaxContacts = 0; ///< Maximum amount of contacts supported
  75. uint32 mNumContacts = 0; ///< Size of the contacts list (see ContactConstraintManager::mNumConstraints)
  76. uint32 mNumIslands = 0; ///< Final number of islands
  77. #ifdef JPH_VALIDATE_ISLAND_BUILDER
  78. /// Structure to keep track of all added links to validate that islands were generated correctly
  79. struct LinkValidation
  80. {
  81. uint32 mFirst;
  82. uint32 mSecond;
  83. };
  84. LinkValidation * mLinkValidation = nullptr;
  85. atomic<uint32> mNumLinkValidation;
  86. #endif
  87. };
  88. JPH_NAMESPACE_END