| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/FixedSizeFreeList.h> |
| 8 |
#include <Jolt/Core/Atomics.h> |
| 9 |
#include <Jolt/Core/NonCopyable.h> |
| 10 |
#include <Jolt/Physics/Body/BodyManager.h> |
| 11 |
#include <Jolt/Physics/Collision/BroadPhase/BroadPhase.h> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
JPH_NAMESPACE_BEGIN |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class JPH_EXPORT QuadTree : public NonCopyable |
| 21 |
{ |
| 22 |
public: |
| 23 |
JPH_OVERRIDE_NEW_DELETE |
| 24 |
|
| 25 |
private: |
| 26 |
|
| 27 |
class AtomicNodeID; |
| 28 |
|
| 29 |
|
| 30 |
class NodeID |
| 31 |
{ |
| 32 |
public: |
| 33 |
JPH_OVERRIDE_NEW_DELETE |
| 34 |
|
| 35 |
|
| 36 |
inline NodeID() = default; |
| 37 |
|
| 38 |
|
| 39 |
static inline NodeID sInvalid() { return NodeID(cInvalidNodeIndex); } |
| 40 |
static inline NodeID sFromBodyID(BodyID inID) { NodeID node_id(inID.GetIndexAndSequenceNumber()); JPH_ASSERT(node_id.IsBody()); return node_id; } |
| 41 |
static inline NodeID sFromNodeIndex(uint32 inIdx) { JPH_ASSERT((inIdx & cIsNode) == 0); return NodeID(inIdx | cIsNode); } |
| 42 |
|
| 43 |
|
| 44 |
inline bool IsValid() const { return mID != cInvalidNodeIndex; } |
| 45 |
inline bool IsBody() const { return (mID & cIsNode) == 0; } |
| 46 |
inline bool IsNode() const { return (mID & cIsNode) != 0; } |
| 47 |
|
| 48 |
|
| 49 |
inline BodyID GetBodyID() const { JPH_ASSERT(IsBody()); return BodyID(mID); } |
| 50 |
inline uint32 GetNodeIndex() const { JPH_ASSERT(IsNode()); return mID & ~cIsNode; } |
| 51 |
|
| 52 |
|
| 53 |
inline bool operator == (const BodyID &inRHS) const { return mID == inRHS.GetIndexAndSequenceNumber(); } |
| 54 |
inline bool operator == (const NodeID &inRHS) const { return mID == inRHS.mID; } |
| 55 |
|
| 56 |
private: |
| 57 |
friend class AtomicNodeID; |
| 58 |
|
| 59 |
inline explicit NodeID(uint32 inID) : mID(inID) { } |
| 60 |
|
| 61 |
static const uint32 cIsNode = BodyID::cBroadPhaseBit; |
| 62 |
|
| 63 |
uint32 mID; |
| 64 |
}; |
| 65 |
|
| 66 |
static_assert(sizeof(NodeID) == sizeof(BodyID), "Body id's should have the same size as NodeIDs"); |
| 67 |
|
| 68 |
|
| 69 |
class AtomicNodeID |
| 70 |
{ |
| 71 |
public: |
| 72 |
|
| 73 |
AtomicNodeID() = default; |
| 74 |
explicit AtomicNodeID(const NodeID &inRHS) : mID(inRHS.mID) { } |
| 75 |
|
| 76 |
|
| 77 |
inline void operator = (const NodeID &inRHS) { mID = inRHS.mID; } |
| 78 |
|
| 79 |
|
| 80 |
inline operator NodeID () const { return NodeID(mID); } |
| 81 |
|
| 82 |
|
| 83 |
inline bool IsValid() const { return mID != cInvalidNodeIndex; } |
| 84 |
|
| 85 |
|
| 86 |
inline bool operator == (const BodyID &inRHS) const { return mID == inRHS.GetIndexAndSequenceNumber(); } |
| 87 |
inline bool operator == (const NodeID &inRHS) const { return mID == inRHS.mID; } |
| 88 |
|
| 89 |
|
| 90 |
inline bool CompareExchange(NodeID inOld, NodeID inNew) { return mID.compare_exchange_strong(inOld.mID, inNew.mID); } |
| 91 |
|
| 92 |
private: |
| 93 |
atomic<uint32> mID; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
class Node |
| 98 |
{ |
| 99 |
public: |
| 100 |
|
| 101 |
explicit Node(bool inIsChanged); |
| 102 |
|
| 103 |
|
| 104 |
void GetNodeBounds(AABox &outBounds) const; |
| 105 |
|
| 106 |
|
| 107 |
void GetChildBounds(int inChildIndex, AABox &outBounds) const; |
| 108 |
|
| 109 |
|
| 110 |
void SetChildBounds(int inChildIndex, const AABox &inBounds); |
| 111 |
|
| 112 |
|
| 113 |
void InvalidateChildBounds(int inChildIndex); |
| 114 |
|
| 115 |
|
| 116 |
bool EncapsulateChildBounds(int inChildIndex, const AABox &inBounds); |
| 117 |
|
| 118 |
|
| 119 |
atomic<float> mBoundsMinX[4]; |
| 120 |
atomic<float> mBoundsMinY[4]; |
| 121 |
atomic<float> mBoundsMinZ[4]; |
| 122 |
atomic<float> mBoundsMaxX[4]; |
| 123 |
atomic<float> mBoundsMaxY[4]; |
| 124 |
atomic<float> mBoundsMaxZ[4]; |
| 125 |
|
| 126 |
|
| 127 |
AtomicNodeID mChildNodeID[4]; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
atomic<uint32> mParentNodeIndex = cInvalidNodeIndex; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
atomic<uint32> mIsChanged; |
| 136 |
|
| 137 |
|
| 138 |
uint32 mPadding = 0; |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
static constexpr int cStackSize = 128; |
| 143 |
|
| 144 |
static_assert(sizeof(atomic<float>) == 4, "Assuming that an atomic doesn't add any additional storage"); |
| 145 |
static_assert(sizeof(atomic<uint32>) == 4, "Assuming that an atomic doesn't add any additional storage"); |
| 146 |
static_assert(std::is_trivially_destructible<Node>(), "Assuming that we don't have a destructor"); |
| 147 |
|
| 148 |
public: |
| 149 |
|
| 150 |
using Allocator = FixedSizeFreeList<Node>; |
| 151 |
|
| 152 |
static_assert(Allocator::ObjectStorageSize == 128, "Node should be 128 bytes"); |
| 153 |
|
| 154 |
|
| 155 |
struct Tracking |
| 156 |
{ |
| 157 |
|
| 158 |
Tracking() = default; |
| 159 |
Tracking(const Tracking &inRHS) : mBroadPhaseLayer(inRHS.mBroadPhaseLayer.load()), mObjectLayer(inRHS.mObjectLayer.load()), mBodyLocation(inRHS.mBodyLocation.load()) { } |
| 160 |
|
| 161 |
|
| 162 |
static const uint32 cInvalidBodyLocation = 0xffffffff; |
| 163 |
|
| 164 |
atomic<BroadPhaseLayer::Type> mBroadPhaseLayer = (BroadPhaseLayer::Type)cBroadPhaseLayerInvalid; |
| 165 |
atomic<ObjectLayer> mObjectLayer = cObjectLayerInvalid; |
| 166 |
atomic<uint32> mBodyLocation { cInvalidBodyLocation }; |
| 167 |
}; |
| 168 |
|
| 169 |
using TrackingVector = Array<Tracking>; |
| 170 |
|
| 171 |
|
| 172 |
~QuadTree(); |
| 173 |
|
| 174 |
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED) |
| 175 |
|
| 176 |
void SetName(const char *inName) { mName = inName; } |
| 177 |
inline const char * GetName() const { return mName; } |
| 178 |
#endif |
| 179 |
|
| 180 |
|
| 181 |
inline bool HasBodies() const { return mNumBodies != 0; } |
| 182 |
|
| 183 |
|
| 184 |
inline bool IsDirty() const { return mIsDirty; } |
| 185 |
|
| 186 |
|
| 187 |
inline bool CanBeUpdated() const { return mFreeNodeBatch.mNumObjects == 0; } |
| 188 |
|
| 189 |
|
| 190 |
void Init(Allocator &inAllocator); |
| 191 |
|
| 192 |
struct UpdateState |
| 193 |
{ |
| 194 |
NodeID mRootNodeID; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
void DiscardOldTree(); |
| 199 |
|
| 200 |
|
| 201 |
AABox GetBounds() const; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void UpdatePrepare(const BodyVector &inBodies, TrackingVector &ioTracking, UpdateState &outUpdateState, bool inFullRebuild); |
| 206 |
void UpdateFinalize(const BodyVector &inBodies, const TrackingVector &inTracking, const UpdateState &inUpdateState); |
| 207 |
|
| 208 |
|
| 209 |
struct AddState |
| 210 |
{ |
| 211 |
NodeID mLeafID = NodeID::sInvalid(); |
| 212 |
AABox mLeafBounds; |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
void AddBodiesPrepare(const BodyVector &inBodies, TrackingVector &ioTracking, BodyID *ioBodyIDs, int inNumber, AddState &outState); |
| 219 |
|
| 220 |
|
| 221 |
void AddBodiesFinalize(TrackingVector &ioTracking, int inNumberBodies, const AddState &inState); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
void AddBodiesAbort(TrackingVector &ioTracking, const AddState &inState); |
| 226 |
|
| 227 |
|
| 228 |
void RemoveBodies(const BodyVector &inBodies, TrackingVector &ioTracking, const BodyID *ioBodyIDs, int inNumber); |
| 229 |
|
| 230 |
|
| 231 |
void NotifyBodiesAABBChanged(const BodyVector &inBodies, const TrackingVector &inTracking, const BodyID *ioBodyIDs, int inNumber); |
| 232 |
|
| 233 |
|
| 234 |
void CastRay(const RayCast &inRay, RayCastBodyCollector &ioCollector, const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking) const; |
| 235 |
|
| 236 |
|
| 237 |
void CollideAABox(const AABox &inBox, CollideShapeBodyCollector &ioCollector, const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking) const; |
| 238 |
|
| 239 |
|
| 240 |
void CollideSphere(Vec3Arg inCenter, float inRadius, CollideShapeBodyCollector &ioCollector, const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking) const; |
| 241 |
|
| 242 |
|
| 243 |
void CollidePoint(Vec3Arg inPoint, CollideShapeBodyCollector &ioCollector, const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking) const; |
| 244 |
|
| 245 |
|
| 246 |
void CollideOrientedBox(const OrientedBox &inBox, CollideShapeBodyCollector &ioCollector, const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking) const; |
| 247 |
|
| 248 |
|
| 249 |
void CastAABox(const AABoxCast &inBox, CastShapeBodyCollector &ioCollector, const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking) const; |
| 250 |
|
| 251 |
|
| 252 |
void FindCollidingPairs(const BodyVector &inBodies, const BodyID *inActiveBodies, int inNumActiveBodies, float inSpeculativeContactDistance, BodyPairCollector &ioPairCollector, const ObjectLayerPairFilter &inObjectLayerPairFilter) const; |
| 253 |
|
| 254 |
#ifdef JPH_TRACK_BROADPHASE_STATS |
| 255 |
|
| 256 |
uint64 GetTicks100Pct() const; |
| 257 |
|
| 258 |
|
| 259 |
void ReportStats(uint64 inTicks100Pct) const; |
| 260 |
#endif |
| 261 |
|
| 262 |
private: |
| 263 |
|
| 264 |
static constexpr uint32 cInvalidNodeIndex = 0xffffffff; |
| 265 |
static const AABox cInvalidBounds; |
| 266 |
|
| 267 |
|
| 268 |
struct RootNode |
| 269 |
{ |
| 270 |
|
| 271 |
inline NodeID GetNodeID() const { return NodeID::sFromNodeIndex(mIndex); } |
| 272 |
|
| 273 |
|
| 274 |
atomic<uint32> mIndex { cInvalidNodeIndex }; |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
void GetBodyLocation(const TrackingVector &inTracking, BodyID inBodyID, uint32 &outNodeIdx, uint32 &outChildIdx) const; |
| 279 |
void SetBodyLocation(TrackingVector &ioTracking, BodyID inBodyID, uint32 inNodeIdx, uint32 inChildIdx) const; |
| 280 |
static void sInvalidateBodyLocation(TrackingVector &ioTracking, BodyID inBodyID); |
| 281 |
|
| 282 |
|
| 283 |
JPH_INLINE const RootNode & GetCurrentRoot() const { return mRootNode[mRootNodeIndex]; } |
| 284 |
JPH_INLINE RootNode & GetCurrentRoot() { return mRootNode[mRootNodeIndex]; } |
| 285 |
|
| 286 |
|
| 287 |
inline AABox GetNodeOrBodyBounds(const BodyVector &inBodies, NodeID inNodeID) const; |
| 288 |
|
| 289 |
|
| 290 |
inline void MarkNodeAndParentsChanged(uint32 inNodeIndex); |
| 291 |
|
| 292 |
|
| 293 |
inline void WidenAndMarkNodeAndParentsChanged(uint32 inNodeIndex, const AABox &inNewBounds); |
| 294 |
|
| 295 |
|
| 296 |
inline uint32 AllocateNode(bool inIsChanged); |
| 297 |
|
| 298 |
|
| 299 |
inline bool TryInsertLeaf(TrackingVector &ioTracking, int inNodeIndex, NodeID inLeafID, const AABox &inLeafBounds, int inLeafNumBodies); |
| 300 |
|
| 301 |
|
| 302 |
inline bool TryCreateNewRoot(TrackingVector &ioTracking, atomic<uint32> &ioRootNodeIndex, NodeID inLeafID, const AABox &inLeafBounds, int inLeafNumBodies); |
| 303 |
|
| 304 |
|
| 305 |
NodeID BuildTree(const BodyVector &inBodies, TrackingVector &ioTracking, NodeID *ioNodeIDs, int inNumber, uint inMaxDepthMarkChanged, AABox &outBounds); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static void sPartition(NodeID *ioNodeIDs, Vec3 *ioNodeCenters, int inNumber, int &outMidPoint); |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static void sPartition4(NodeID *ioNodeIDs, Vec3 *ioNodeCenters, int inBegin, int inEnd, int *outSplit); |
| 315 |
|
| 316 |
#ifdef JPH_DEBUG |
| 317 |
|
| 318 |
|
| 319 |
void ValidateTree(const BodyVector &inBodies, const TrackingVector &inTracking, uint32 inNodeIndex, uint32 inNumExpectedBodies) const; |
| 320 |
#endif |
| 321 |
|
| 322 |
#ifdef JPH_DUMP_BROADPHASE_TREE |
| 323 |
|
| 324 |
void DumpTree(const NodeID &inRoot, const char *inFileNamePrefix) const; |
| 325 |
#endif |
| 326 |
|
| 327 |
|
| 328 |
Allocator * mAllocator = nullptr; |
| 329 |
|
| 330 |
|
| 331 |
Allocator::Batch mFreeNodeBatch; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
alignas(JPH_CACHE_LINE_SIZE) atomic<uint32> mNumBodies { 0 }; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
RootNode mRootNode[2]; |
| 341 |
atomic<uint32> mRootNodeIndex { 0 }; |
| 342 |
|
| 343 |
|
| 344 |
atomic<bool> mIsDirty = false; |
| 345 |
|
| 346 |
#ifdef JPH_TRACK_BROADPHASE_STATS |
| 347 |
|
| 348 |
mutable Mutex mStatsMutex; |
| 349 |
|
| 350 |
struct Stat |
| 351 |
{ |
| 352 |
uint64 mNumQueries = 0; |
| 353 |
uint64 mNodesVisited = 0; |
| 354 |
uint64 mBodiesVisited = 0; |
| 355 |
uint64 mHitsReported = 0; |
| 356 |
uint64 mTotalTicks = 0; |
| 357 |
uint64 mCollectorTicks = 0; |
| 358 |
}; |
| 359 |
|
| 360 |
using LayerToStats = UnorderedMap<String, Stat>; |
| 361 |
|
| 362 |
|
| 363 |
uint64 GetTicks100Pct(const LayerToStats &inLayer) const; |
| 364 |
|
| 365 |
|
| 366 |
void ReportStats(const char *inName, const LayerToStats &inLayer, uint64 inTicks100Pct) const; |
| 367 |
|
| 368 |
mutable LayerToStats mCastRayStats; |
| 369 |
mutable LayerToStats mCollideAABoxStats; |
| 370 |
mutable LayerToStats mCollideSphereStats; |
| 371 |
mutable LayerToStats mCollidePointStats; |
| 372 |
mutable LayerToStats mCollideOrientedBoxStats; |
| 373 |
mutable LayerToStats mCastAABoxStats; |
| 374 |
#endif |
| 375 |
|
| 376 |
|
| 377 |
uint GetMaxTreeDepth(const NodeID &inNodeID) const; |
| 378 |
|
| 379 |
|
| 380 |
template <class Visitor> |
| 381 |
JPH_INLINE void WalkTree(const ObjectLayerFilter &inObjectLayerFilter, const TrackingVector &inTracking, Visitor &ioVisitor JPH_IF_TRACK_BROADPHASE_STATS(, LayerToStats &ioStats)) const; |
| 382 |
|
| 383 |
#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED) |
| 384 |
|
| 385 |
const char * mName = "Layer"; |
| 386 |
#endif |
| 387 |
}; |
| 388 |
|
| 389 |
JPH_NAMESPACE_END |