RavEngine
Loading...
Searching...
No Matches
GuTriangleMesh.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_TRIANGLEMESH_H
30#define GU_TRIANGLEMESH_H
31
32#include "foundation/PxIO.h"
33#include "geometry/PxTriangle.h"
34#include "geometry/PxTriangleMeshGeometry.h"
35#include "geometry/PxSimpleTriangleMesh.h"
36#include "geometry/PxTriangleMesh.h"
37
38#include "GuMeshData.h"
39#include "GuCenterExtents.h"
40#include "CmScaling.h"
41#include "CmRefCountable.h"
42#include "common/PxRenderOutput.h"
43
44namespace physx
45{
46class PxMeshScale;
47struct PxTriangleMeshInternalData;
48
49namespace Gu
50{
51 PX_FORCE_INLINE void getVertexRefs(PxU32 triangleIndex, PxU32& vref0, PxU32& vref1, PxU32& vref2, const void* indices, bool has16BitIndices)
52 {
53 if(has16BitIndices)
54 {
55 const PxU16* inds = reinterpret_cast<const PxU16*>(indices) + triangleIndex*3;
56 vref0 = inds[0];
57 vref1 = inds[1];
58 vref2 = inds[2];
59 }
60 else
61 {
62 const PxU32* inds = reinterpret_cast<const PxU32*>(indices) + triangleIndex*3;
63 vref0 = inds[0];
64 vref1 = inds[1];
65 vref2 = inds[2];
66 }
67 }
68
69class MeshFactory;
70#if PX_VC
71#pragma warning(push)
72#pragma warning(disable: 4324) // Padding was added at the end of a structure because of a __declspec(align) value.
73#endif
74
75class EdgeList;
76
78{
79//= ATTENTION! =====================================================================================
80// Changing the data layout of this class breaks the binary serialization format. See comments for
81// PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
82// function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
83// accordingly.
84//==================================================================================================
85public:
86
87// PX_SERIALIZATION
88 TriangleMesh(PxBaseFlags baseFlags) : PxTriangleMesh(baseFlags) {}
89
90 void preExportDataReset() { Cm::RefCountable_preExportDataReset(*this); }
91 virtual void exportExtraData(PxSerializationContext& context);
92 void importExtraData(PxDeserializationContext& context);
93 PX_PHYSX_COMMON_API static void getBinaryMetaData(PxOutputStream& stream);
94 virtual void release();
95 virtual void requiresObjects(PxProcessPxBaseCallback&){}
96//~PX_SERIALIZATION
99 virtual ~TriangleMesh();
100
101 // PxBase
102 virtual void onRefCountZero();
103 //~PxBase
104
105 // PxRefCounted
106 virtual void acquireReference() { Cm::RefCountable_incRefCount(*this); }
107 virtual PxU32 getReferenceCount() const { return Cm::RefCountable_getRefCount(*this); }
108 //~PxRefCounted
109
110 // PxTriangleMesh
111 virtual PxU32 getNbVertices() const { return mNbVertices;}
112 virtual const PxVec3* getVertices() const { return mVertices; }
113
115 virtual PxBounds3 refitBVH();
116 virtual PxU32 getNbTriangles() const { return mNbTriangles; }
117 virtual const void* getTriangles() const { return mTriangles; }
119 virtual const PxU32* getTrianglesRemap() const { return mFaceRemap; }
120 virtual void setPreferSDFProjection(bool preferProjection)
121 {
122 if (preferProjection)
124 else
126 }
127
129
130 virtual PxMaterialTableIndex getTriangleMaterialIndex(PxTriangleID triangleIndex) const
131 {
132 return hasPerTriangleMaterials() ? getMaterials()[triangleIndex] : PxMaterialTableIndex(0xffff);
133 }
134
135 virtual PxBounds3 getLocalBounds() const
136 {
137 PX_ASSERT(mAABB.isValid());
138 return PxBounds3::centerExtents(mAABB.mCenter, mAABB.mExtents);
139 }
140
141 virtual const PxReal* getSDF() const
142 {
143 return mSdfData.mSdf;
144 }
145
146 virtual void getSDFDimensions(PxU32& numX, PxU32& numY, PxU32& numZ) const
147 {
148 if(mSdfData.mSdf)
149 {
150 numX = mSdfData.mDims.x;
151 numY = mSdfData.mDims.y;
152 numZ = mSdfData.mDims.z;
153 }
154 else
155 numX = numY = numZ = 0;
156 }
157
158 virtual void getMassInformation(PxReal& mass, PxMat33& localInertia, PxVec3& localCenterOfMass) const
159 {
160 mass = mMass; localInertia = mInertia; localCenterOfMass = mLocalCenterOfMass;
161 }
162
163 //~PxTriangleMesh
164
165 virtual bool getInternalData(PxTriangleMeshInternalData&, bool) const { return false; }
166
167 // PT: this one is just to prevent instancing Gu::TriangleMesh.
168 // But you should use PxBase::getConcreteType() instead to avoid the virtual call.
169 virtual PxMeshMidPhase::Enum getMidphaseID() const = 0;
170
171 PX_FORCE_INLINE const PxU32* getFaceRemap() const { return mFaceRemap; }
172 PX_FORCE_INLINE bool has16BitIndices() const { return (mFlags & PxMeshFlag::e16_BIT_INDICES) ? true : false; }
173 PX_FORCE_INLINE bool hasPerTriangleMaterials() const { return mMaterialIndices != NULL; }
174 PX_FORCE_INLINE PxU32 getNbVerticesFast() const { return mNbVertices; }
175 PX_FORCE_INLINE PxU32 getNbTrianglesFast() const { return mNbTriangles; }
176 PX_FORCE_INLINE const void* getTrianglesFast() const { return mTriangles; }
177 PX_FORCE_INLINE const PxVec3* getVerticesFast() const { return mVertices; }
178 PX_FORCE_INLINE const PxU32* getAdjacencies() const { return mAdjacencies; }
179 PX_FORCE_INLINE PxReal getGeomEpsilon() const { return mGeomEpsilon; }
180 PX_FORCE_INLINE const CenterExtents& getLocalBoundsFast() const { return mAABB; }
181 PX_FORCE_INLINE const PxU16* getMaterials() const { return mMaterialIndices; }
182 PX_FORCE_INLINE const PxU8* getExtraTrigData() const { return mExtraTrigData; }
183
184 PX_FORCE_INLINE const PxU32* getAccumulatedTriangleRef() const { return mAccumulatedTrianglesRef; }
185 PX_FORCE_INLINE const PxU32* getTriangleReferences() const { return mTrianglesReferences; }
186 PX_FORCE_INLINE PxU32 getNbTriangleReferences() const { return mNbTrianglesReferences; }
187
188 PX_FORCE_INLINE const CenterExtentsPadded& getPaddedBounds() const
189 {
190 // PT: see compile-time assert in cpp
191 return static_cast<const CenterExtentsPadded&>(mAABB);
192 }
193
194 PX_FORCE_INLINE void computeWorldTriangle(
195 PxTriangle& worldTri, PxTriangleID triangleIndex, const PxMat34& worldMatrix, bool flipNormal = false,
196 PxU32* PX_RESTRICT vertexIndices=NULL, PxU32* PX_RESTRICT adjacencyIndices=NULL) const;
197 PX_FORCE_INLINE void getLocalTriangle(PxTriangle& localTri, PxTriangleID triangleIndex, bool flipNormal = false) const;
198
199 void setMeshFactory(MeshFactory* factory) { mMeshFactory = factory; }
200
201 // SDF methods
202 PX_FORCE_INLINE const SDF& getSdfDataFast() const { return mSdfData; }
203 //~SDF methods
204
205 PX_FORCE_INLINE PxReal getMass() const { return mMass; }
206
207 // PT: for debug viz
208 PX_PHYSX_COMMON_API const Gu::EdgeList* requestEdgeList() const;
209
210protected:
211 PxU32 mNbVertices;
212 PxU32 mNbTriangles;
213 PxVec3* mVertices;
215 // 16 bytes block
216
217 // PT: WARNING: bounds must be followed by at least 32bits of data for safe SIMD loading
218 CenterExtents mAABB;
219 PxU8* mExtraTrigData; //one per trig
221 // 16 bytes block
222 /*
223 low 3 bits (mask: 7) are the edge flags:
224 b001 = 1 = ignore edge 0 = edge v0-->v1
225 b010 = 2 = ignore edge 1 = edge v0-->v2
226 b100 = 4 = ignore edge 2 = edge v1-->v2
227 */
228 PxU8 mFlags;
231 PxU32* mFaceRemap;
234
235 MeshFactory* mMeshFactory; // PT: changed to pointer for serialization
236 mutable Gu::EdgeList* mEdgeList; // PT: for debug viz
237
238
239 PxReal mMass; //this is mass assuming a unit density that can be scaled by instances!
240 PxMat33 mInertia; //in local space of mesh!
241 PxVec3 mLocalCenterOfMass; //local space com
242public:
243
244 // GRB data -------------------------
246
247 // TODO avoroshilov: cooking - adjacency info - duplicated, remove it and use 'mAdjacencies' and 'mExtraTrigData' see GuTriangleMesh.cpp:325
249
251 PxU32* mGRB_faceRemapInverse;
253 // End of GRB data ------------------
254
255 // SDF data -------------------------
256 SDF mSdfData;
257 // End of SDF data ------------------
258
259 void setAllEdgesActive();
260 //Vertex mapping data
261 PxU32* mAccumulatedTrianglesRef;//runsum
262 PxU32* mTrianglesReferences;
263 PxU32 mNbTrianglesReferences;
264 //End of vertex mapping data
265};
266
267#if PX_VC
268#pragma warning(pop)
269#endif
270
271} // namespace Gu
272
273PX_FORCE_INLINE void Gu::TriangleMesh::computeWorldTriangle(PxTriangle& worldTri, PxTriangleID triangleIndex, const PxMat34& worldMatrix, bool flipNormal,
274 PxU32* PX_RESTRICT vertexIndices, PxU32* PX_RESTRICT adjacencyIndices) const
275{
276 PxU32 vref0, vref1, vref2;
277 getVertexRefs(triangleIndex, vref0, vref1, vref2, mTriangles, has16BitIndices());
278
279 if(flipNormal)
280 PxSwap<PxU32>(vref1, vref2);
281
282 const PxVec3* PX_RESTRICT vertices = getVerticesFast();
283 worldTri.verts[0] = worldMatrix.transform(vertices[vref0]);
284 worldTri.verts[1] = worldMatrix.transform(vertices[vref1]);
285 worldTri.verts[2] = worldMatrix.transform(vertices[vref2]);
286
287 if(vertexIndices)
288 {
289 vertexIndices[0] = vref0;
290 vertexIndices[1] = vref1;
291 vertexIndices[2] = vref2;
292 }
293
294 if(adjacencyIndices)
295 {
296 if(mAdjacencies)
297 {
298 // PT: TODO: is this correct?
299 adjacencyIndices[0] = flipNormal ? mAdjacencies[triangleIndex*3 + 2] : mAdjacencies[triangleIndex*3 + 0];
300 adjacencyIndices[1] = mAdjacencies[triangleIndex*3 + 1];
301 adjacencyIndices[2] = flipNormal ? mAdjacencies[triangleIndex*3 + 0] : mAdjacencies[triangleIndex*3 + 2];
302 }
303 else
304 {
305 adjacencyIndices[0] = 0xffffffff;
306 adjacencyIndices[1] = 0xffffffff;
307 adjacencyIndices[2] = 0xffffffff;
308 }
309 }
310}
311
312PX_FORCE_INLINE void Gu::TriangleMesh::getLocalTriangle(PxTriangle& localTri, PxTriangleID triangleIndex, bool flipNormal) const
313{
314 PxU32 vref0, vref1, vref2;
315 getVertexRefs(triangleIndex, vref0, vref1, vref2, mTriangles, has16BitIndices());
316
317 if(flipNormal)
318 PxSwap<PxU32>(vref1, vref2);
319
320 const PxVec3* PX_RESTRICT vertices = getVerticesFast();
321 localTri.verts[0] = vertices[vref0];
322 localTri.verts[1] = vertices[vref1];
323 localTri.verts[2] = vertices[vref2];
324}
325
326PX_INLINE float computeSweepData(const PxTriangleMeshGeometry& triMeshGeom, /*const Cm::FastVertex2ShapeScaling& scaling,*/ PxVec3& sweepOrigin, PxVec3& sweepExtents, PxVec3& sweepDir, float distance)
327{
328 PX_ASSERT(!Cm::isEmpty(sweepOrigin, sweepExtents));
329
330 const PxVec3 endPt = sweepOrigin + sweepDir*distance;
331 PX_ASSERT(!Cm::isEmpty(endPt, sweepExtents));
332
333 const Cm::FastVertex2ShapeScaling meshScaling(triMeshGeom.scale.getInverse()); // shape to vertex transform
334
335 const PxMat33& vertex2ShapeSkew = meshScaling.getVertex2ShapeSkew();
336
337 const PxVec3 originBoundsCenter = vertex2ShapeSkew * sweepOrigin;
338 const PxVec3 originBoundsExtents = Cm::basisExtent(vertex2ShapeSkew.column0, vertex2ShapeSkew.column1, vertex2ShapeSkew.column2, sweepExtents);
339
340 sweepOrigin = originBoundsCenter;
341 sweepExtents = originBoundsExtents;
342 sweepDir = (vertex2ShapeSkew * endPt) - originBoundsCenter;
343 return sweepDir.normalizeSafe();
344}
345
346PX_FORCE_INLINE const Gu::TriangleMesh* _getMeshData(const PxTriangleMeshGeometry& meshGeom)
347{
348 return static_cast<const Gu::TriangleMesh*>(meshGeom.triangleMesh);
349}
350
351}
352
353#endif
Definition base.h:1940
Definition GuBV32.h:121
Definition GuCenterExtents.h:44
PxU32 y
Size of Y dimension.
Definition GuSDF.h:80
PxU32 z
Size of Z dimension.
Definition GuSDF.h:81
PxU32 x
Size of X dimension.
Definition GuSDF.h:79
Definition GuEdgeList.h:135
Definition GuMeshFactory.h:83
Represents a signed distance field.
Definition GuSDF.h:88
PxReal * mSdf
Array of sdf.
Definition GuSDF.h:137
Dim3 mDims
Dimension of the sdf.
Definition GuSDF.h:135
Definition GuMeshData.h:171
Definition GuTriangleMesh.h:78
Gu::BV32Tree * mGRB_BV32Tree
GRB: BV32 tree.
Definition GuTriangleMesh.h:252
virtual void getSDFDimensions(PxU32 &numX, PxU32 &numY, PxU32 &numZ) const
Returns the resolution of the local-space dense SDF.
Definition GuTriangleMesh.h:146
virtual void setPreferSDFProjection(bool preferProjection)
Sets whether this mesh should be preferred for SDF projection.
Definition GuTriangleMesh.h:120
PxU32 * mFaceRemap
new faces to old faces mapping (after cleaning, etc). Usage: old = faceRemap[new]
Definition GuTriangleMesh.h:231
virtual const PxReal * getSDF() const
Returns the local-space Signed Distance Field for this mesh if it has one.
Definition GuTriangleMesh.h:141
virtual PxBounds3 getLocalBounds() const
Returns the local-space (vertex space) AABB from the triangle mesh.
Definition GuTriangleMesh.h:135
void * mTriangles
16 (<= 0xffff #vertices) or 32 bit trig indices (mNbTriangles * 3)
Definition GuTriangleMesh.h:214
virtual bool getPreferSDFProjection() const
Returns whether this mesh prefers SDF projection.
Definition GuTriangleMesh.h:128
virtual const PxU32 * getTrianglesRemap() const
Returns the triangle remapping table.
Definition GuTriangleMesh.h:119
virtual PxVec3 * getVerticesForModification()
Returns all mesh vertices for modification.
Definition GuTriangleMesh.cpp:271
void * mGRB_triAdjacencies
GRB: adjacency data, with BOUNDARY and NONCONVEX flags (flags replace adj indices where applicable)
Definition GuTriangleMesh.h:248
PxReal mGeomEpsilon
see comments in cooking code referencing this variable
Definition GuTriangleMesh.h:220
virtual void acquireReference()
Acquires a counted reference to this object.
Definition GuTriangleMesh.h:106
PxU32 * mAdjacencies
Definition GuTriangleMesh.h:232
PxU8 mFlags
Definition GuTriangleMesh.h:228
virtual const PxVec3 * getVertices() const
Returns the vertices.
Definition GuTriangleMesh.h:112
PxU16 * mMaterialIndices
the size of the array is numTriangles.
Definition GuTriangleMesh.h:230
virtual PxU32 getNbTriangles() const
Returns the number of triangles.
Definition GuTriangleMesh.h:116
virtual const void * getTriangles() const
Returns the triangle indices.
Definition GuTriangleMesh.h:117
void * mGRB_triIndices
GRB: GPU-friendly tri indices.
Definition GuTriangleMesh.h:245
virtual void release()
Decrements the reference count of a triangle mesh and releases it if the new reference count is zero.
Definition GuTriangleMesh.cpp:266
virtual PxBounds3 refitBVH()
Refits BVH for mesh vertices.
Definition GuTriangleMesh.cpp:278
virtual PxU32 getNbVertices() const
Returns the number of vertices.
Definition GuTriangleMesh.h:111
virtual PxMaterialTableIndex getTriangleMaterialIndex(PxTriangleID triangleIndex) const
Returns material table index of given triangle.
Definition GuTriangleMesh.h:130
PxU32 * mGRB_faceRemap
GRB : gpu to cpu triangle indice remap.
Definition GuTriangleMesh.h:250
virtual PxU32 getReferenceCount() const
Returns the reference count of the object.
Definition GuTriangleMesh.h:107
virtual PxTriangleMeshFlags getTriangleMeshFlags() const
Reads the PxTriangleMesh flags.
Definition GuTriangleMesh.h:118
virtual void getMassInformation(PxReal &mass, PxMat33 &localInertia, PxVec3 &localCenterOfMass) const
Returns the mass properties of the mesh assuming unit density.
Definition GuTriangleMesh.h:158
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
static PX_CUDA_CALLABLE PX_FORCE_INLINE PxBounds3 centerExtents(const PxVec3 &center, const PxVec3 &extent)
returns the AABB from center and extents vectors.
Definition PxBounds3.h:299
Binary deserialization context class.
Definition PxSerialFramework.h:174
3x3 matrix class
Definition PxMat33.h:91
Definition PxMat34.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T< Type > transform(const PxVec3T< Type > &other) const
Transform point by matrix.
Definition PxMat34.h:216
Output stream class for I/O.
Definition PxIO.h:114
Callback class used to process PxBase objects.
Definition PxSerialFramework.h:81
Binary serialization context class.
Definition PxSerialFramework.h:99
A triangle mesh, also called a 'polygon soup'.
Definition PxTriangleMesh.h:118
Triangle class.
Definition PxTriangle.h:47
PxVec3 verts[3]
Array of Vertices.
Definition PxTriangle.h:140
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
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PxFlags< PxTriangleMeshFlag::Enum, PxU8 > PxTriangleMeshFlags
collection of set bits defined in PxTriangleMeshFlag.
Definition PxTriangleMesh.h:86
@ e16_BIT_INDICES
Denotes the use of 16-bit vertex indices.
Definition PxSimpleTriangleMesh.h:66
Enum
Definition PxTriangleMesh.h:58
@ ePREFER_NO_SDF_PROJ
Indicates that this mesh would preferably not be the mesh projected for mesh-mesh collision....
Definition PxTriangleMesh.h:77
Definition PxGeometryInternal.h:46