RavEngine
Loading...
Searching...
No Matches
GuTriangleCache.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_TRIANGLE_CACHE_H
30#define GU_TRIANGLE_CACHE_H
31#include "foundation/PxHash.h"
32#include "foundation/PxUtilities.h"
33
34namespace physx
35{
36 namespace Gu
37 {
39 {
40 protected:
41 PxU32 mId0, mId1;
42 public:
43 CachedEdge(PxU32 i0, PxU32 i1)
44 {
45 mId0 = PxMin(i0, i1);
46 mId1 = PxMax(i0, i1);
47 }
48
50 {
51 }
52
53 PxU32 getId0() const { return mId0; }
54 PxU32 getId1() const { return mId1; }
55
56 bool operator == (const CachedEdge& other) const
57 {
58 return mId0 == other.mId0 && mId1 == other.mId1;
59 }
60
61 PxU32 getHashCode() const
62 {
63 return PxComputeHash(mId0 << 16 | mId1);
64 }
65 };
66
68 {
69 private:
70 PxU32 mId;
71 public:
72 CachedVertex(PxU32 id)
73 {
74 mId = id;
75 }
76
78 {
79 }
80
81 PxU32 getId() const { return mId; }
82
83 PxU32 getHashCode() const
84 {
85 return mId;
86 }
87
88 bool operator == (const CachedVertex& other) const
89 {
90 return mId == other.mId;
91 }
92 };
93
94 template <typename Elem, PxU32 MaxCount>
95 struct CacheMap
96 {
97 PX_COMPILE_TIME_ASSERT(MaxCount < 0xFF);
98 Elem mCache[MaxCount];
99 PxU8 mNextInd[MaxCount];
100 PxU8 mIndex[MaxCount];
101 PxU32 mSize;
102
103 CacheMap() : mSize(0)
104 {
105 for(PxU32 a = 0; a < MaxCount; ++a)
106 {
107 mIndex[a] = 0xFF;
108 }
109 }
110
111 bool addData(const Elem& data)
112 {
113 if(mSize == MaxCount)
114 return false;
115
116 const PxU8 hash = PxU8(data.getHashCode() % MaxCount);
117
118 PxU8 index = hash;
119 PxU8 nextInd = mIndex[hash];
120 while(nextInd != 0xFF)
121 {
122 index = nextInd;
123 if(mCache[index] == data)
124 return false;
125 nextInd = mNextInd[nextInd];
126 }
127
128 if(mIndex[hash] == 0xFF)
129 {
130 mIndex[hash] = PxTo8(mSize);
131 }
132 else
133 {
134 mNextInd[index] = PxTo8(mSize);
135 }
136 mNextInd[mSize] = 0xFF;
137 mCache[mSize++] = data;
138 return true;
139 }
140
141 bool contains(const Elem& data) const
142 {
143 PxU32 hash = (data.getHashCode() % MaxCount);
144 PxU8 index = mIndex[hash];
145
146 while(index != 0xFF)
147 {
148 if(mCache[index] == data)
149 return true;
150 index = mNextInd[index];
151 }
152 return false;
153 }
154
155 const Elem* get(const Elem& data) const
156 {
157 PxU32 hash = (data.getHashCode() % MaxCount);
158 PxU8 index = mIndex[hash];
159
160 while(index != 0xFF)
161 {
162 if(mCache[index] == data)
163 return &mCache[index];
164 index = mNextInd[index];
165 }
166 return NULL;
167 }
168 };
169
170 template <PxU32 MaxTriangles>
172 {
173 PxVec3 mVertices[3*MaxTriangles];
174 PxU32 mIndices[3*MaxTriangles];
175 PxU32 mTriangleIndex[MaxTriangles];
176 PxU8 mEdgeFlags[MaxTriangles];
177 PxU32 mNumTriangles;
178
179 TriangleCache() : mNumTriangles(0)
180 {
181 }
182
183 PX_FORCE_INLINE bool isEmpty() const { return mNumTriangles == 0; }
184 PX_FORCE_INLINE bool isFull() const { return mNumTriangles == MaxTriangles; }
185 PX_FORCE_INLINE void reset() { mNumTriangles = 0; }
186
187 void addTriangle(const PxVec3* verts, const PxU32* indices, PxU32 triangleIndex, PxU8 edgeFlag)
188 {
189 PX_ASSERT(mNumTriangles < MaxTriangles);
190 PxU32 triInd = mNumTriangles++;
191 PxU32 triIndMul3 = triInd*3;
192 mVertices[triIndMul3] = verts[0];
193 mVertices[triIndMul3+1] = verts[1];
194 mVertices[triIndMul3+2] = verts[2];
195 mIndices[triIndMul3] = indices[0];
196 mIndices[triIndMul3+1] = indices[1];
197 mIndices[triIndMul3+2] = indices[2];
198 mTriangleIndex[triInd] = triangleIndex;
199 mEdgeFlags[triInd] = edgeFlag;
200 }
201 };
202
203 template <PxU32 MaxTetrahedrons>
205 {
206 PxVec3 mVertices[4 * MaxTetrahedrons];
207 PxU32 mTetVertIndices[4 * MaxTetrahedrons];
208 PxU32 mTetrahedronIndices[MaxTetrahedrons];
209 PxU32 mNumTetrahedrons;
210
211 TetrahedronCache() : mNumTetrahedrons(0)
212 {
213 }
214
215 PX_FORCE_INLINE bool isEmpty() const { return mNumTetrahedrons == 0; }
216 PX_FORCE_INLINE bool isFull() const { return mNumTetrahedrons == MaxTetrahedrons; }
217 PX_FORCE_INLINE void reset() { mNumTetrahedrons = 0; }
218
219 void addTetrahedrons(const PxVec3* verts, const PxU32* indices, PxU32 tetIndex)
220 {
221 PX_ASSERT(mNumTetrahedrons < MaxTetrahedrons);
222 PxU32 tetInd = mNumTetrahedrons++;
223 PxU32 tetIndMul4 = tetInd * 4;
224 mVertices[tetIndMul4] = verts[0];
225 mVertices[tetIndMul4 + 1] = verts[1];
226 mVertices[tetIndMul4 + 2] = verts[2];
227 mVertices[tetIndMul4 + 3] = verts[3];
228 mTetVertIndices[tetIndMul4] = indices[0];
229 mTetVertIndices[tetIndMul4 + 1] = indices[1];
230 mTetVertIndices[tetIndMul4 + 2] = indices[2];
231 mTetVertIndices[tetIndMul4 + 3] = indices[3];
232 mTetrahedronIndices[tetInd] = tetIndex;
233 }
234 };
235 }
236}
237
238#endif
239
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
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition PxMath.h:72
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
The return value is the lesser of the two specified values.
Definition PxMath.h:88
Definition GuTriangleCache.h:96
Definition GuTriangleCache.h:39
Definition GuTriangleCache.h:68
Definition GuTriangleCache.h:205
Definition GuTriangleCache.h:172