1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "ApexMeshHash.h"
#include "PxBounds3.h"
#include "PsSort.h"
namespace nvidia
{
namespace apex
{
ApexMeshHash::ApexMeshHash() :
mHashIndex(NULL)
{
mHashIndex = (MeshHashRoot*)PX_ALLOC(sizeof(MeshHashRoot) * HashIndexSize, PX_DEBUG_EXP("ApexMeshHash"));
mTime = 1;
for (uint32_t i = 0; i < HashIndexSize; i++)
{
mHashIndex[i].first = -1;
mHashIndex[i].timeStamp = 0;
}
mSpacing = 0.25f;
mInvSpacing = 1.0f / mSpacing;
}
ApexMeshHash::~ApexMeshHash()
{
if (mHashIndex != NULL)
{
PX_FREE(mHashIndex);
mHashIndex = NULL;
}
}
void ApexMeshHash::setGridSpacing(float spacing)
{
mSpacing = spacing;
mInvSpacing = 1.0f / spacing;
reset();
}
void ApexMeshHash::reset()
{
mTime++;
mEntries.clear();
}
void ApexMeshHash::add(const PxBounds3& bounds, uint32_t itemIndex)
{
int32_t x1, y1, z1;
int32_t x2, y2, z2;
cellCoordOf(bounds.minimum, x1, y1, z1);
cellCoordOf(bounds.maximum, x2, y2, z2);
MeshHashEntry entry;
entry.itemIndex = itemIndex;
for (int32_t x = x1; x <= x2; x++)
{
for (int32_t y = y1; y <= y2; y++)
{
for (int32_t z = z1; z <= z2; z++)
{
uint32_t h = hashFunction(x, y, z);
MeshHashRoot& r = mHashIndex[h];
uint32_t n = mEntries.size();
if (r.timeStamp != mTime || r.first < 0)
{
entry.next = -1;
}
else
{
entry.next = r.first;
}
r.first = (int32_t)n;
r.timeStamp = mTime;
mEntries.pushBack(entry);
}
}
}
}
void ApexMeshHash::add(const PxVec3& pos, uint32_t itemIndex)
{
int x, y, z;
cellCoordOf(pos, x, y, z);
MeshHashEntry entry;
entry.itemIndex = itemIndex;
uint32_t h = hashFunction(x, y, z);
MeshHashRoot& r = mHashIndex[h];
uint32_t n = mEntries.size();
if (r.timeStamp != mTime || r.first < 0)
{
entry.next = -1;
}
else
{
entry.next = r.first;
}
r.first = (int32_t)n;
r.timeStamp = mTime;
mEntries.pushBack(entry);
}
void ApexMeshHash::query(const PxBounds3& bounds, physx::Array<uint32_t>& itemIndices, int32_t maxIndices)
{
int32_t x1, y1, z1;
int32_t x2, y2, z2;
cellCoordOf(bounds.minimum, x1, y1, z1);
cellCoordOf(bounds.maximum, x2, y2, z2);
itemIndices.clear();
for (int32_t x = x1; x <= x2; x++)
{
for (int32_t y = y1; y <= y2; y++)
{
for (int32_t z = z1; z <= z2; z++)
{
uint32_t h = hashFunction(x, y, z);
MeshHashRoot& r = mHashIndex[h];
if (r.timeStamp != mTime)
{
continue;
}
int32_t i = r.first;
while (i >= 0)
{
MeshHashEntry& entry = mEntries[(uint32_t)i];
itemIndices.pushBack(entry.itemIndex);
if (maxIndices >= 0 && itemIndices.size() >= (uint32_t)maxIndices)
{
return;
}
i = entry.next;
}
}
}
}
}
void ApexMeshHash::queryUnique(const PxBounds3& bounds, physx::Array<uint32_t>& itemIndices, int32_t maxIndices)
{
query(bounds, itemIndices, maxIndices);
compressIndices(itemIndices);
}
void ApexMeshHash::query(const PxVec3& pos, physx::Array<uint32_t>& itemIndices, int32_t maxIndices)
{
int32_t x, y, z;
cellCoordOf(pos, x, y, z);
itemIndices.clear();
uint32_t h = hashFunction(x, y, z);
MeshHashRoot& r = mHashIndex[h];
if (r.timeStamp != mTime)
{
return;
}
int32_t i = r.first;
while (i >= 0)
{
MeshHashEntry& entry = mEntries[(uint32_t)i];
itemIndices.pushBack(entry.itemIndex);
if (maxIndices >= 0 && itemIndices.size() >= (uint32_t)maxIndices)
{
return;
}
i = entry.next;
}
}
void ApexMeshHash::queryUnique(const PxVec3& pos, physx::Array<uint32_t>& itemIndices, int32_t maxIndices)
{
query(pos, itemIndices, maxIndices);
compressIndices(itemIndices);
}
class U32Less
{
public:
bool operator()(uint32_t u1, uint32_t u2) const
{
return u1 < u2;
}
};
void ApexMeshHash::compressIndices(physx::Array<uint32_t>& itemIndices)
{
if (itemIndices.empty())
{
return;
}
shdfnd::sort(itemIndices.begin(), itemIndices.size(), U32Less());
// mark duplicates
uint32_t i = 0;
while (i < itemIndices.size())
{
uint32_t j = i + 1;
while (j < itemIndices.size() && itemIndices[i] == itemIndices[j])
{
itemIndices[j] = (uint32_t) - 1;
j++;
}
i = j;
}
// remove duplicates
i = 0;
while (i < itemIndices.size())
{
if (itemIndices[i] == (uint32_t)-1)
{
itemIndices.replaceWithLast(i);
}
else
{
i++;
}
}
}
int32_t ApexMeshHash::getClosestPointNr(const PxVec3* points, uint32_t numPoints, uint32_t pointStride, const PxVec3& pos)
{
PX_ASSERT(numPoints > 0);
PxBounds3 queryBounds;
queryBounds.minimum = pos;
queryBounds.maximum = pos;
PX_ASSERT(!queryBounds.isEmpty());
queryBounds.fattenFast(mSpacing);
query(queryBounds, mTempIndices);
// remove false positives due to hash collisions
uint32_t next = 0;
for (uint32_t i = 0; i < mTempIndices.size(); i++)
{
uint32_t pointNr = mTempIndices[i];
const PxVec3* p = (PxVec3*)((uint8_t*)points + (pointNr * pointStride));
if (pointNr < numPoints && queryBounds.contains(*p))
{
mTempIndices[next++] = pointNr;
}
}
mTempIndices.resize(next);
bool fallBack = mTempIndices.size() == 0;
uint32_t numRes = fallBack ? numPoints : mTempIndices.size();
float min2 = 0.0f;
int minNr = -1;
for (uint32_t j = 0; j < numRes; j++)
{
uint32_t k = fallBack ? j : mTempIndices[j];
const PxVec3* p = (PxVec3*)((uint8_t*)points + (k * pointStride));
float d2 = (pos - *p).magnitudeSquared();
if (minNr < 0 || d2 < min2)
{
min2 = d2;
minNr = (int32_t)k;
}
}
return minNr;
}
}
} // end namespace nvidia::apex
|