RavEngine
Loading...
Searching...
No Matches
GuIncrementalAABBTree.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef GU_INCREMENTAL_AABB_TREE_H
30#define GU_INCREMENTAL_AABB_TREE_H
31
32#include "foundation/PxBounds3.h"
33#include "foundation/PxUserAllocated.h"
34#include "foundation/PxHashMap.h"
35#include "foundation/PxVecMath.h"
36#include "foundation/PxPool.h"
37#include "common/PxPhysXCommonConfig.h"
38#include "GuAABBTree.h"
39#include "GuPrunerTypedef.h"
40
41namespace physx
42{
43 using namespace aos;
44
45 namespace Gu
46 {
47 struct BVHNode;
48 class BVH;
49
50 #define INCR_NB_OBJECTS_PER_NODE 4
51
52 // tree indices, can change in runtime
54 {
55 PX_FORCE_INLINE AABBTreeIndices(PoolIndex index) : nbIndices(1)
56 {
57 indices[0] = index;
58 for(PxU32 i=1; i<INCR_NB_OBJECTS_PER_NODE; i++)
59 indices[i] = 0;
60 }
61
62 PxU32 nbIndices;
63 PoolIndex indices[INCR_NB_OBJECTS_PER_NODE];
64 };
65
66 // tree node, has parent information
68 {
69 public:
71 {
72 mChilds[0] = NULL;
73 mChilds[1] = NULL;
74 }
76 {
77 mIndices = indices;
78 mChilds[1] = NULL;
79 }
81
82 PX_FORCE_INLINE PxU32 isLeaf() const { return PxU32(mChilds[1]==0); }
83
84 PX_FORCE_INLINE const PxU32* getPrimitives(const PxU32*) const { return &mIndices->indices[0]; }
85 PX_FORCE_INLINE PxU32* getPrimitives(PxU32*) { return &mIndices->indices[0]; }
86 PX_FORCE_INLINE PxU32 getNbPrimitives() const { return mIndices->nbIndices; }
87 PX_FORCE_INLINE PxU32 getPrimitiveIndex() const { return PX_INVALID_U32; }
88
89 PX_FORCE_INLINE const IncrementalAABBTreeNode* getPos(const IncrementalAABBTreeNode*) const { return mChilds[0]; }
90 PX_FORCE_INLINE const IncrementalAABBTreeNode* getNeg(const IncrementalAABBTreeNode*) const { return mChilds[1]; }
91
94
95 // PT: TODO: these functions are duplicates from the regular AABB tree node
96 PX_FORCE_INLINE void getAABBCenterExtentsV(physx::aos::Vec3V* center, physx::aos::Vec3V* extents) const
97 {
98 const float half = 0.5f;
99 const FloatV halfV = FLoad(half);
100
101 *extents = Vec3V_From_Vec4V((V4Scale(V4Sub(mBVMax, mBVMin), halfV)));
102 *center = Vec3V_From_Vec4V((V4Scale(V4Add(mBVMax, mBVMin), halfV)));
103 }
104
105 PX_FORCE_INLINE void getAABBCenterExtentsV2(physx::aos::Vec3V* center, physx::aos::Vec3V* extents) const
106 {
107 *extents = Vec3V_From_Vec4V((V4Sub(mBVMax, mBVMin)));
108 *center = Vec3V_From_Vec4V((V4Add(mBVMax, mBVMin)));
109 }
110
111 Vec4V mBVMin; // Global bounding-volume min enclosing all the node-related primitives
112 Vec4V mBVMax; // Global bounding-volume max enclosing all the node-related primitives
113 IncrementalAABBTreeNode* mParent; // node parent
114 union
115 {
116 IncrementalAABBTreeNode* mChilds[2]; // childs of node if not a leaf
117 AABBTreeIndices* mIndices; // if leaf, indices information
118 };
119 };
120
126
128
129 // incremental AABB tree, all changes are immediatelly reflected to the tree
131 {
132 public:
133 PX_PHYSX_COMMON_API IncrementalAABBTree();
134 PX_PHYSX_COMMON_API ~IncrementalAABBTree();
135
136 // Build the tree for the first time
137 PX_PHYSX_COMMON_API bool build(const AABBTreeBuildParams& params, PxArray<IncrementalAABBTreeNode*>& mapping);
138
139 // insert a new index into the tree
140 PX_PHYSX_COMMON_API IncrementalAABBTreeNode* insert(const PoolIndex index, const PxBounds3* bounds, NodeList& changedLeaf);
141
142 // update the object in the tree - full update insert/remove
143 PX_PHYSX_COMMON_API IncrementalAABBTreeNode* update(IncrementalAABBTreeNode* node, const PoolIndex index, const PxBounds3* bounds, NodeList& changedLeaf);
144 // update the object in the tree, faster method, that may unbalance the tree
145 PX_PHYSX_COMMON_API IncrementalAABBTreeNode* updateFast(IncrementalAABBTreeNode* node, const PoolIndex index, const PxBounds3* bounds, NodeList& changedLeaf);
146
147 // remove object from the tree
148 PX_PHYSX_COMMON_API IncrementalAABBTreeNode* remove(IncrementalAABBTreeNode* node, const PoolIndex index, const PxBounds3* bounds);
149
150 // fixup the tree indices, if we swapped the objects in the pruning pool
151 PX_PHYSX_COMMON_API void fixupTreeIndices(IncrementalAABBTreeNode* node, const PoolIndex index, const PoolIndex newIndex);
152
153 // origin shift
154 PX_PHYSX_COMMON_API void shiftOrigin(const PxVec3& shift);
155
156 // get the tree root node
157 PX_FORCE_INLINE const IncrementalAABBTreeNode* getNodes() const { return mRoot; }
158
159 // define this function so we can share the scene query code with regular AABBTree
160 PX_FORCE_INLINE const PxU32* getIndices() const { return NULL; }
161
162 // paranoia checks
163 PX_PHYSX_COMMON_API void hierarchyCheck(PoolIndex maxIndex, const PxBounds3* bounds);
164 PX_PHYSX_COMMON_API void hierarchyCheck(const PxBounds3* bounds);
165 PX_PHYSX_COMMON_API void checkTreeLeaf(IncrementalAABBTreeNode* leaf, PoolIndex h);
166 PX_PHYSX_COMMON_API PxU32 getTreeLeafDepth(IncrementalAABBTreeNode* leaf);
167
168 PX_PHYSX_COMMON_API void release();
169
170 PX_PHYSX_COMMON_API void copy(const BVH& bvh, PxArray<IncrementalAABBTreeNode*>& mapping);
171
172 private:
173 // clone the tree from the generic AABB tree that was built
174 void clone(PxArray<IncrementalAABBTreeNode*>& mapping, const PxU32* indices, IncrementalAABBTreeNode** treeNodes);
175
176 void copyNode(IncrementalAABBTreeNode& destNode, const BVHNode& sourceNode, const BVHNode* nodeBase,
177 IncrementalAABBTreeNode* parent, const PxU32* primitivesBase, PxArray<IncrementalAABBTreeNode*>& mapping);
178
179 // split leaf node, the newly added object does not fit in
180 IncrementalAABBTreeNode* splitLeafNode(IncrementalAABBTreeNode* node, const PoolIndex index, const Vec4V& minV, const Vec4V& maxV, const PxBounds3* bounds);
181
182 void rotateTree(IncrementalAABBTreeNode* node, NodeList& changedLeaf, PxU32 largesRotateNode, const PxBounds3* bounds, bool rotateAgain);
183
184 void releaseNode(IncrementalAABBTreeNode* node);
185
186 PxPool<AABBTreeIndices> mIndicesPool;
189
190 NodeAllocator mNodeAllocator;
191 };
192 }
193}
194
195#endif
Contains AABB-tree build parameters.
Definition GuAABBTree.h:57
Represents a BVH.
Definition GuBVH.h:93
Definition GuIncrementalAABBTree.h:68
Definition GuIncrementalAABBTree.h:131
Definition GuAABBTree.h:125
Definition PxArray.h:53
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
Definition PxPool.h:248
Definition PxUserAllocated.h:43
3 Element vector class.
Definition PxVec3.h:50
#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
Definition GuIncrementalAABBTree.h:54
Definition GuAABBTreeNode.h:44
Definition GuIncrementalAABBTree.h:122
Definition PxVecMathAoSScalar.h:52
Definition PxVecMathAoSScalar.h:77
Definition PxVecMathAoSScalar.h:65