29#ifndef DY_THRESHOLD_TABLE_H
30#define DY_THRESHOLD_TABLE_H
32#include "foundation/PxPinnedArray.h"
33#include "foundation/PxUserAllocated.h"
34#include "foundation/PxHash.h"
35#include "foundation/PxMemory.h"
36#include "PxNodeIndex.h"
45 class ShapeInteraction;
58 PxReal accumulatedForce;
63 return ((nodeIndexA < otherPair.nodeIndexA) ||(nodeIndexA == otherPair.nodeIndexA && nodeIndexB <= otherPair.nodeIndexB));
68 return ((nodeIndexA < otherPair.nodeIndexA) || (nodeIndexA == otherPair.nodeIndexA && nodeIndexB < otherPair.nodeIndexB));
73 return ((nodeIndexA == otherPair.nodeIndexA && nodeIndexB == otherPair.nodeIndexB));
112 bool check(
const ThresholdStream& stream,
const PxU32 nodexIndexA,
const PxU32 nodexIndexB, PxReal dt);
118 static const PxU32 NO_INDEX = 0xffffffff;
122 PxU32 thresholdStreamIndex;
123 PxReal accumulatedForce;
136 PxU32 mPairsCapacity;
141 static PX_FORCE_INLINE PxU32 computeHashKey(
const PxU32 nodeIndexA,
const PxU32 nodeIndexB,
const PxU32 hashCapacity)
143 return (PxComputeHash(PxU64(nodeIndexA)<<32 | PxU64(nodeIndexB)) % hashCapacity);
147inline bool ThresholdTable::check(
const ThresholdStream& stream,
const ThresholdStreamElement& elem, PxU32& thresholdIndex)
153 PX_ASSERT(elem.nodeIndexA < elem.nodeIndexB);
154 PxU32 hashKey = computeHashKey(elem.nodeIndexA.index(), elem.nodeIndexB.index(), mHashSize);
156 PxU32 pairIndex = hashes[hashKey];
158 while(NO_INDEX != pairIndex)
160 Pair& pair = pairs[pairIndex];
161 const PxU32 thresholdStreamIndex = pair.thresholdStreamIndex;
162 PX_ASSERT(thresholdStreamIndex < stream.size());
163 const ThresholdStreamElement& otherElement = stream[thresholdStreamIndex];
164 if(otherElement.nodeIndexA==elem.nodeIndexA && otherElement.nodeIndexB==elem.nodeIndexB && otherElement.shapeInteraction == elem.shapeInteraction)
166 thresholdIndex = thresholdStreamIndex;
169 pairIndex = nextIndices[pairIndex];
172 thresholdIndex = NO_INDEX;
177inline void ThresholdTable::build(
const ThresholdStream& stream)
191 const PxU32 pairsCapacity = stream.size();
192 const PxU32 hashCapacity = pairsCapacity*2+1;
193 if((pairsCapacity > mPairsCapacity) || (pairsCapacity < (mPairsCapacity >> 2)))
196 const PxU32 pairsByteSize =
sizeof(
Pair)*pairsCapacity;
197 const PxU32 nextsByteSize =
sizeof(PxU32)*pairsCapacity;
198 const PxU32 hashByteSize =
sizeof(PxU32)*hashCapacity;
199 const PxU32 totalByteSize = pairsByteSize + nextsByteSize + hashByteSize;
200 mBuffer =
reinterpret_cast<PxU8*
>(PX_ALLOC(totalByteSize,
"PxThresholdStream"));
203 mPairs =
reinterpret_cast<Pair*
>(mBuffer + offset);
204 offset += pairsByteSize;
205 mNexts =
reinterpret_cast<PxU32*
>(mBuffer + offset);
206 offset += nextsByteSize;
207 mHash =
reinterpret_cast<PxU32*
>(mBuffer + offset);
208 offset += hashByteSize;
209 PX_ASSERT(totalByteSize == offset);
211 mPairsCapacity = pairsCapacity;
212 mHashCapactiy = hashCapacity;
217 PxMemSet(mHash, 0xff,
sizeof(PxU32)*hashCapacity);
221 mHashSize = hashCapacity;
229 for(PxU32 i = 0; i < pairsCapacity; i++)
231 const ThresholdStreamElement& element = stream[i];
232 const PxNodeIndex nodeIndexA = element.nodeIndexA;
233 const PxNodeIndex nodeIndexB = element.nodeIndexB;
235 const PxF32 force = element.normalForce;
237 PX_ASSERT(nodeIndexA < nodeIndexB);
239 const PxU32 hashKey = computeHashKey(nodeIndexA.index(), nodeIndexB.index(), hashCapacity);
242 PxU32 prevPairIndex = hashKey;
243 PxU32 pairIndex = hashes[hashKey];
248 while(NO_INDEX != pairIndex)
250 Pair& pair = pairs[pairIndex];
251 const PxU32 thresholdStreamIndex = pair.thresholdStreamIndex;
252 PX_ASSERT(thresholdStreamIndex < stream.size());
253 const ThresholdStreamElement& otherElement = stream[thresholdStreamIndex];
254 if(nodeIndexA == otherElement.nodeIndexA && nodeIndexB==otherElement.nodeIndexB)
256 pair.accumulatedForce += force;
257 prevPairIndex = NO_INDEX;
258 pairIndex = NO_INDEX;
261 prevPairIndex = pairIndex;
262 pairIndex = nextIndices[pairIndex];
265 if(NO_INDEX != prevPairIndex)
267 nextIndices[pairsSize] = hashes[hashKey];
268 hashes[hashKey] = pairsSize;
269 Pair& newPair = pairs[pairsSize];
270 newPair.thresholdStreamIndex = i;
271 newPair.accumulatedForce = force;
275 mPairsSize = pairsSize;
A generic couple structure.
Definition GuCookingConvexPolygonsBuilder.cpp:76
Definition DyThresholdTable.h:81
Definition DyThresholdTable.h:90
PxNodeIndex.
Definition PxNodeIndex.h:51
Definition PxUserAllocated.h:43
Definition PxAllocator.h:140
Definition PxAllocator.h:158
Definition ScShapeInteraction.h:54
#define PX_RESTRICT
Definition PxPreprocessor.h:355
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemSet(void *dest, PxI32 c, PxU32 count)
Sets the bytes of the provided buffer to the specified value.
Definition PxMemory.h:67
Definition DyThresholdTable.h:52
Definition DyThresholdTable.h:121