Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/IslandBuilder.h

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