Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/IslandBuilder.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Body/BodyID.h>7#include <Jolt/Core/NonCopyable.h>8#include <Jolt/Core/Atomics.h>910JPH_NAMESPACE_BEGIN1112class TempAllocator;1314//#define JPH_VALIDATE_ISLAND_BUILDER1516/// Keeps track of connected bodies and builds islands for multithreaded velocity/position update17class IslandBuilder : public NonCopyable18{19public:20/// Destructor21~IslandBuilder();2223/// Initialize the island builder with the maximum amount of bodies that could be active24void Init(uint32 inMaxActiveBodies);2526/// Prepare for simulation step by allocating space for the contact constraints27void PrepareContactConstraints(uint32 inMaxContactConstraints, TempAllocator *inTempAllocator);2829/// Prepare for simulation step by allocating space for the non-contact constraints30void PrepareNonContactConstraints(uint32 inNumConstraints, TempAllocator *inTempAllocator);3132/// Link two bodies by their index in the BodyManager::mActiveBodies list to form islands33void LinkBodies(uint32 inFirst, uint32 inSecond);3435/// Link a constraint to a body by their index in the BodyManager::mActiveBodies36void LinkConstraint(uint32 inConstraintIndex, uint32 inFirst, uint32 inSecond);3738/// Link a contact to a body by their index in the BodyManager::mActiveBodies39void LinkContact(uint32 inContactIndex, uint32 inFirst, uint32 inSecond);4041/// Finalize the islands after all bodies have been Link()-ed42void Finalize(const BodyID *inActiveBodies, uint32 inNumActiveBodies, uint32 inNumContacts, TempAllocator *inTempAllocator);4344/// Get the amount of islands formed45uint32 GetNumIslands() const { return mNumIslands; }4647/// Get iterator for a particular island, return false if there are no constraints48void GetBodiesInIsland(uint32 inIslandIndex, BodyID *&outBodiesBegin, BodyID *&outBodiesEnd) const;49bool GetConstraintsInIsland(uint32 inIslandIndex, uint32 *&outConstraintsBegin, uint32 *&outConstraintsEnd) const;50bool GetContactsInIsland(uint32 inIslandIndex, uint32 *&outContactsBegin, uint32 *&outContactsEnd) const;5152/// The number of position iterations for each island53void SetNumPositionSteps(uint32 inIslandIndex, uint inNumPositionSteps) { JPH_ASSERT(inIslandIndex < mNumIslands); JPH_ASSERT(inNumPositionSteps < 256); mNumPositionSteps[inIslandIndex] = uint8(inNumPositionSteps); }54uint GetNumPositionSteps(uint32 inIslandIndex) const { JPH_ASSERT(inIslandIndex < mNumIslands); return mNumPositionSteps[inIslandIndex]; }5556/// After you're done calling the three functions above, call this function to free associated data57void ResetIslands(TempAllocator *inTempAllocator);5859private:60/// Returns the index of the lowest body in the group61uint32 GetLowestBodyIndex(uint32 inActiveBodyIndex) const;6263#ifdef JPH_VALIDATE_ISLAND_BUILDER64/// Helper function to validate all islands so far generated65void ValidateIslands(uint32 inNumActiveBodies) const;66#endif6768// Helper functions to build various islands69void BuildBodyIslands(const BodyID *inActiveBodies, uint32 inNumActiveBodies, TempAllocator *inTempAllocator);70void BuildConstraintIslands(const uint32 *inConstraintToBody, uint32 inNumConstraints, uint32 *&outConstraints, uint32 *&outConstraintsEnd, TempAllocator *inTempAllocator) const;7172/// Sorts the islands so that the islands with most constraints go first73void SortIslands(TempAllocator *inTempAllocator);7475/// Intermediate data structure that for each body keeps track what the lowest index of the body is that it is connected to76struct BodyLink77{78JPH_OVERRIDE_NEW_DELETE7980atomic<uint32> mLinkedTo; ///< An index in mBodyLinks pointing to another body in this island with a lower index than this body81uint32 mIslandIndex; ///< The island index of this body (filled in during Finalize)82};8384// Intermediate data85BodyLink * mBodyLinks = nullptr; ///< Maps bodies to the first body in the island86uint32 * mConstraintLinks = nullptr; ///< Maps constraint index to body index (which maps to island index)87uint32 * mContactLinks = nullptr; ///< Maps contact constraint index to body index (which maps to island index)8889// Final data90BodyID * mBodyIslands = nullptr; ///< Bodies ordered by island91uint32 * mBodyIslandEnds = nullptr; ///< End index of each body island9293uint32 * mConstraintIslands = nullptr; ///< Constraints ordered by island94uint32 * mConstraintIslandEnds = nullptr; ///< End index of each constraint island9596uint32 * mContactIslands = nullptr; ///< Contacts ordered by island97uint32 * mContactIslandEnds = nullptr; ///< End index of each contact island9899uint32 * mIslandsSorted = nullptr; ///< A list of island indices in order of most constraints first100101uint8 * mNumPositionSteps = nullptr; ///< Number of position steps for each island102103// Counters104uint32 mMaxActiveBodies; ///< Maximum size of the active bodies list (see BodyManager::mActiveBodies)105uint32 mNumActiveBodies = 0; ///< Number of active bodies passed to106uint32 mNumConstraints = 0; ///< Size of the constraint list (see ConstraintManager::mConstraints)107uint32 mMaxContacts = 0; ///< Maximum amount of contacts supported108uint32 mNumContacts = 0; ///< Size of the contacts list (see ContactConstraintManager::mNumConstraints)109uint32 mNumIslands = 0; ///< Final number of islands110111#ifdef JPH_VALIDATE_ISLAND_BUILDER112/// Structure to keep track of all added links to validate that islands were generated correctly113struct LinkValidation114{115uint32 mFirst;116uint32 mSecond;117};118119LinkValidation * mLinkValidation = nullptr;120atomic<uint32> mNumLinkValidation;121#endif122};123124JPH_NAMESPACE_END125126127