RavEngine
Loading...
Searching...
No Matches
GuBVH.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_BVH_H
30#define GU_BVH_H
31
36#include "geometry/PxBVH.h"
37
38#include "CmRefCountable.h"
39#include "foundation/PxVecMath.h"
40#include "foundation/PxUserAllocated.h"
41#include "GuAABBTreeBounds.h"
42#include "GuAABBTree.h"
43
44namespace physx
45{
46 struct PxBVHInternalData;
47
48namespace Gu
49{
50 class MeshFactory;
51 struct BVHNode;
52 class ShapeData;
53
55 {
56 public:
57 BVHData() {}
58
59 BVHData(BVHData& other)
60 {
61 mNbIndices = other.mNbIndices;
62 mNbNodes = other.mNbNodes;
63 mIndices = other.mIndices;
64 mNodes = other.mNodes;
65
66 mBounds.moveFrom(other.mBounds);
67 other.mIndices = NULL;
68 other.mNodes = NULL;
69 }
70
71 ~BVHData()
72 {
73 if(mBounds.ownsMemory())
74 {
75 mBounds.release();
76 PX_FREE(mIndices);
77 PX_FREE(mNodes); // PT: TODO: fix this, unify with AABBTree version
78 }
79 mNbNodes = 0;
80 mNbIndices = 0;
81 }
82
83 PX_PHYSX_COMMON_API bool build(PxU32 nbBounds, const void* boundsData, PxU32 boundsStride, float enlargement, PxU32 numPrimsPerLeaf, BVHBuildStrategy bs);
84 PX_PHYSX_COMMON_API bool save(PxOutputStream& stream, bool endian) const;
85
86 AABBTreeBounds mBounds;
87 };
88
92 class BVH : public PxBVH, public PxUserAllocated, public Cm::RefCountable
93 {
94 public:
95 // PT: TODO: revisit these PX_PHYSX_COMMON_API calls. At the end of the day the issue is that things like PxUserAllocated aren't exported.
96 PX_PHYSX_COMMON_API BVH(MeshFactory* factory);
97 PX_PHYSX_COMMON_API BVH(MeshFactory* factory, BVHData& data);
98 PX_PHYSX_COMMON_API BVH(const PxBVHInternalData& data);
99 virtual ~BVH();
100
101 PX_PHYSX_COMMON_API bool init(PxU32 nbPrims, AABBTreeBounds* bounds, const void* boundsData, PxU32 stride, BVHBuildStrategy bs, PxU32 nbPrimsPerLeaf, float enlargement);
102 bool load(PxInputStream& desc);
103 void release();
104
105 // PxBVH
106 virtual bool raycast(const PxVec3& origin, const PxVec3& unitDir, float distance, RaycastCallback& cb, PxGeometryQueryFlags flags) const /*override*/;
107 virtual bool overlap(const PxGeometry& geom, const PxTransform& pose, OverlapCallback& cb, PxGeometryQueryFlags flags) const /*override*/;
108 virtual bool sweep(const PxGeometry& geom, const PxTransform& pose, const PxVec3& unitDir, float distance, RaycastCallback& cb, PxGeometryQueryFlags flags) const /*override*/;
109 virtual bool cull(PxU32 nbPlanes, const PxPlane* planes, OverlapCallback& cb, PxGeometryQueryFlags flags) const /*override*/;
110
111 virtual PxU32 raycast(const PxVec3& origin, const PxVec3& unitDir, PxReal maxDist, PxU32 maxHits, PxU32* PX_RESTRICT rayHits) const /*override*/;
112 virtual PxU32 sweep(const PxBounds3& aabb, const PxVec3& unitDir, PxReal maxDist, PxU32 maxHits, PxU32* PX_RESTRICT sweepHits) const /*override*/;
113 virtual PxU32 overlap(const PxBounds3& aabb, PxU32 maxHits, PxU32* PX_RESTRICT overlapHits) const /*override*/;
114
115 virtual PxU32 getNbBounds() const /*override*/ { return mData.mNbIndices; }
116 virtual const PxBounds3* getBounds() const /*override*/ { return mData.mBounds.getBounds(); }
117
118 virtual void refit() /*override*/;
119 virtual bool updateBounds(PxU32 boundsIndex, const PxBounds3& newBounds) /*override*/;
120 virtual void partialRefit() /*override*/;
121
122 virtual bool traverse(TraversalCallback& cb) const /*override*/;
123 //~PxBVH
124
125 // Cm::RefCountable
126 virtual void onRefCountZero() /*override*/;
127 //~Cm::RefCountable
128
129 PX_FORCE_INLINE const BVHNode* getNodes() const { return mData.mNodes; }
130 PX_FORCE_INLINE const PxU32* getIndices() const { return mData.mIndices; }
131 PX_FORCE_INLINE const BVHData& getData() const { return mData; }
132
133 bool getInternalData(PxBVHInternalData&, bool) const;
134 bool updateBoundsInternal(PxU32 localIndex, const PxBounds3& bounds);
135 // PT: alternative implementations directly working on shape data
136 bool overlap(const ShapeData& shapeData, OverlapCallback& cb, PxGeometryQueryFlags flags) const;
137 bool sweep(const ShapeData& shapeData, const PxVec3& unitDir, float distance, RaycastCallback& cb, PxGeometryQueryFlags flags) const;
138 private:
139 MeshFactory* mMeshFactory;
140 BVHData mData;
141 };
142}
143
144}
145
147#endif
Definition CmRefCountable.h:86
Definition GuAABBTreeBounds.h:39
PxU32 mNbNodes
Number of nodes in the tree.
Definition GuAABBTree.h:257
PxU32 mNbIndices
Nb indices.
Definition GuAABBTree.h:256
BVHNode * mNodes
Linear pool of nodes.
Definition GuAABBTree.h:258
PxU32 * mIndices
Indices in the app list. Indices are reorganized during build (permutation).
Definition GuAABBTree.h:259
Definition GuBVH.h:55
Definition GuAABBTree.h:263
Represents a BVH.
Definition GuBVH.h:93
virtual void refit()
Refit the BVH.
Definition GuBVH.cpp:700
virtual void onRefCountZero()
Definition GuBVH.cpp:276
void release()
Releases the PxBase instance, please check documentation of release in derived class.
Definition GuBVH.cpp:271
virtual const PxBounds3 * getBounds() const
Retrieve the read-only bounds in the BVH.
Definition GuBVH.h:116
virtual bool cull(PxU32 nbPlanes, const PxPlane *planes, OverlapCallback &cb, PxGeometryQueryFlags flags) const
Frustum culling test against a BVH.
Definition GuBVH.cpp:616
virtual bool updateBounds(PxU32 boundsIndex, const PxBounds3 &newBounds)
Update single bounds.
Definition GuBVH.cpp:731
virtual PxU32 getNbBounds() const
Returns the number of bounds in the BVH.
Definition GuBVH.h:115
virtual bool traverse(TraversalCallback &cb) const
Generic BVH traversal function.
Definition GuBVH.cpp:741
virtual bool raycast(const PxVec3 &origin, const PxVec3 &unitDir, float distance, RaycastCallback &cb, PxGeometryQueryFlags flags) const
Raycast test against a BVH.
Definition GuBVH.cpp:401
virtual void partialRefit()
Refits subset of marked nodes.
Definition GuBVH.cpp:736
virtual bool overlap(const PxGeometry &geom, const PxTransform &pose, OverlapCallback &cb, PxGeometryQueryFlags flags) const
Overlap test against a BVH.
Definition GuBVH.cpp:488
virtual bool sweep(const PxGeometry &geom, const PxTransform &pose, const PxVec3 &unitDir, float distance, RaycastCallback &cb, PxGeometryQueryFlags flags) const
Sweep test against a BVH.
Definition GuBVH.cpp:506
Definition GuMeshFactory.h:83
Class representing a bounding volume hierarchy.
Definition PxBVH.h:63
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
A geometry object.
Definition PxGeometry.h:79
Input stream class for I/O.
Definition PxIO.h:50
Output stream class for I/O.
Definition PxIO.h:114
Representation of a plane.
Definition PxPlane.h:49
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
Definition PxUserAllocated.h:43
3 Element vector class.
Definition PxVec3.h:50
#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
Definition GuAABBTreeNode.h:44
Definition PxGeometryInternal.h:92
Definition PxBVH.h:116
Definition PxBVH.h:104