This repository has been archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUniformGrid.cpp
392 lines (302 loc) · 12.7 KB
/
UniformGrid.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* UniformGrid.cpp
* SpatialTest Project
*
* Uniform Grid Datastructure.
*
* Created by radix on 08/04/08.
* Copyright Mykola Konyk, <mykola@konyk.org>, 2008.
*
* This code is under Microsoft Reciprocal License (Ms-RL)
* Please see http://www.opensource.org/licenses/ms-rl.html
*
* Important points about the license (from Ms-RL):
*
* [A] For any file you distribute that contains code from the software (in source code or binary format), you must provide
* recipients the source code to that file along with a copy of this license, which license will govern that file.
* You may license other files that are entirely your own work and do not contain code from the software under any terms
* you choose.
*
* [B] No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
*
* [C] If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your
* patent license from such contributor to the software ends automatically.
*
* [D] If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices
* that are present in the software.
*
* [E] If you distribute any portion of the software in source code form, you may do so only under this license by including a
* complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
* code form, you may only do so under a license that complies with this license.
*
* [F] The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
* or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
* permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
* purpose and non-infringement.
*
*/
#include "UniformGrid.h"
// [rad] We need floorf, ceilf
#include <math.h>
#include <cstdlib>
namespace SpatialTest
{
//--
float UniformGrid::s_f32ObjectCellRatio = 4.0f;
float UniformGrid::s_f32Epsilon = 0.0005f;
//--
unsigned int
UniformGrid::ComputeHashValue(int i32HashBuckets, int i32X, int i32Y, int i32Z)
{
// [rad] Here we pick some large primes
const int i32Prime1 = 563300407;
const int i32Prime2 = 495250453;
const int i32Prime3 = 236350427;
// [rad] Compute hash value
int i32Hash = i32Prime1 * i32X + i32Prime2 * i32Y + i32Prime3 * i32Z;
// [rad] Find the corresponding bucket
i32Hash %= i32HashBuckets;
if(i32Hash < 0)
{
i32Hash += i32HashBuckets;
}
return(i32Hash);
}
//--
UniformGrid::UniformGrid(int i32HashBuckets) :
ISpatialStructure(),
m_i32HashBuckets(i32HashBuckets),
m_i32FrameCount(0),
m_f32GridSize(1.0f)
{
UniformGridHashBucket* pBucket;
// [rad] Initialize hashing buckets
for(int i32Index = 0; i32Index < m_i32HashBuckets; i32Index++)
{
// [rad] Create new bucket
pBucket = new UniformGridHashBucket();
// [rad] Store this bucket
m_vecHashBuckets.push_back(pBucket);
}
}
//--
UniformGrid::~UniformGrid()
{
UniformGridHashBucket* pBucket;
// [rad] Delete all buckets
std::vector<UniformGridHashBucket*>::iterator iter_bucket;
for(iter_bucket = m_vecHashBuckets.begin(); iter_bucket != m_vecHashBuckets.end(); iter_bucket++)
{
pBucket = (*iter_bucket);
delete(pBucket);
pBucket = NULL;
}
}
//--
void
UniformGrid::VAddObjects(const std::vector<ISpatialObject*>& refObjects)
{
// [rad] We need to determine the size of the grid (size of each cell)
int i32Hash;
float f32Diameter;
ISpatialObject* pObject;
// [rad] Iterate through all objects, and find the biggest diameter
std::vector<ISpatialObject*>::const_iterator iter_object;
for(iter_object = refObjects.begin(); iter_object != refObjects.end(); iter_object++)
{
pObject = (*iter_object);
f32Diameter = pObject->VGetRadius() * 2.0f;
if(f32Diameter > m_f32GridSize)
{
// [rad] Found a bigger object, update
m_f32GridSize = f32Diameter;
}
// [rad] We also locally store object pointers, so we could do a bulk update
m_vecObjects.push_back(pObject);
}
// [rad] We'll make it so that cell's is 'x' times as big as the largest object
m_f32GridSize *= s_f32ObjectCellRatio;
// [rad] For each object, determine the cell and insert
for(iter_object = refObjects.begin(); iter_object != refObjects.end(); iter_object++)
{
pObject = (*iter_object);
// [rad] Compute hash for this object - we use object's center to
// determine the proper cell, and then hash that value
i32Hash = ComputeHashValue(m_i32HashBuckets,
static_cast<int>(pObject->VGetPosition().x / m_f32GridSize),
static_cast<int>(pObject->VGetPosition().y / m_f32GridSize),
static_cast<int>(pObject->VGetPosition().z / m_f32GridSize));
// [rad] Insert at the end of this bucket
(m_vecHashBuckets[i32Hash])->InsertObject(pObject);
}
}
//--
void
UniformGrid::VUpdate()
{
int i32X1, i32X2;
int i32Y1, i32Y2;
int i32Z1, i32Z2;
int i32Hash;
float f32Delta;
UniformGridHashBucket* pBucket;
std::vector<ISpatialObject*>::iterator iter_object;
for(iter_object = m_vecObjects.begin(); iter_object != m_vecObjects.end(); iter_object++)
{
ISpatialObject* pObject = (*iter_object);
// [rad] Retrieve the bucket in which this object is stored
pBucket = static_cast<UniformGridHashBucket*>(pObject->VGetCell());
// [rad] Retrieve new object position
const Vector3& vec3Position = pObject->VGetPosition();
// [rad] Check if we need bucket update (compute hash)
i32Hash = ComputeHashValue(m_i32HashBuckets,
static_cast<int>(vec3Position.x / m_f32GridSize),
static_cast<int>(vec3Position.y / m_f32GridSize),
static_cast<int>(vec3Position.z / m_f32GridSize));
// [rad] Check if we need to switch buckets
if(m_vecHashBuckets[i32Hash] != pBucket)
{
// [rad] Remove from old bucket
pBucket->RemoveObject(pObject);
// [rad] Add to new (other) bucket
(m_vecHashBuckets[i32Hash])->InsertObject(pObject);
}
// [rad] Update frame
++m_i32FrameCount;
// [rad] Check collisions within the current bucket
pBucket->CheckCollisions(m_i32FrameCount, pObject);
// [rad] Now we need to check adjacent buckets:
// compute all cells which this object might overlap.
// Because initially we picked size for our cells big enough to
// encompass any object, we have to check at most 8 cells (3D).
f32Delta = pObject->VGetRadius() + m_f32GridSize / s_f32ObjectCellRatio + s_f32Epsilon;
i32X1 = static_cast<int>(floorf((vec3Position.x - f32Delta) / m_f32GridSize));
i32X2 = static_cast<int>(ceilf((vec3Position.x + f32Delta) / m_f32GridSize));
i32Y1 = static_cast<int>(floorf((vec3Position.y - f32Delta) / m_f32GridSize));
i32Y2 = static_cast<int>(ceilf((vec3Position.y + f32Delta) / m_f32GridSize));
i32Z1 = static_cast<int>(floorf((vec3Position.z - f32Delta) / m_f32GridSize));
i32Z2 = static_cast<int>(ceilf((vec3Position.z + f32Delta) / m_f32GridSize));
// [rad] Check all grid cells
for(int i32XIndex = i32X1; i32XIndex <= i32X2; i32XIndex++)
{
for(int i32YIndex = i32Y1; i32YIndex <= i32Y2; i32YIndex++)
{
for(int i32ZIndex = i32Z1; i32ZIndex <= i32Z2; i32ZIndex++)
{
i32Hash = ComputeHashValue(m_i32HashBuckets, i32XIndex, i32YIndex, i32ZIndex);
// [rad] Check if we have checked this bucket already
if(m_vecHashBuckets[i32Hash]->GetLastFrame() == m_i32FrameCount)
{
continue;
}
// [rad] Otherwise check collisions
(m_vecHashBuckets[i32Hash])->CheckCollisions(m_i32FrameCount, pObject);
}
}
}
}
}
//--
UniformGridHashBucket::UniformGridHashBucket() :
ISpatialCell(),
m_i32LastFrame(0),
m_i32ObjectCount(0),
m_pObjects(NULL)
{
}
//--
inline
int
UniformGridHashBucket::GetLastFrame() const
{
return(m_i32LastFrame);
}
//--
void
UniformGridHashBucket::InsertObject(ISpatialObject* pObject)
{
// [rad] Set owner, for fast reverse look-up
pObject->VSetCell(this);
// [rad] Store object in this bucket
//m_vecObjects.push_back(pObject);
pObject->VSetNext(m_pObjects);
m_pObjects = pObject;
}
//--
void
UniformGridHashBucket::RemoveObject(ISpatialObject* pObject)
{
/*
// [rad] Iterate over all objects in this bucket
std::vector<ISpatialObject*>::iterator iter_object;
for(iter_object = m_vecObjects.begin(); iter_object != m_vecObjects.end(); iter_object++)
{
if(pObject == (*iter_object))
{
// [rad] Remove this object
m_vecObjects.erase(iter_object);
return;
}
}
*/
if(m_pObjects == pObject)
{
m_pObjects = m_pObjects->VGetNext();
}
else
{
// [rad] traverse list and remove
ISpatialObject* pIter = m_pObjects;
ISpatialObject* pPrev;
while(pIter)
{
pPrev = pIter;
pIter = pIter->VGetNext();
if(pIter == pObject)
{
pPrev->VSetNext(pIter->VGetNext());
break;
}
}
}
}
//--
void
UniformGridHashBucket::CheckCollisions(int i32LastFrame, ISpatialObject* pObject)
{
// [rad] Update timestamp
m_i32LastFrame = i32LastFrame;
/*
// [rad] Iterate over all objects in this bucket
std::vector<ISpatialObject*>::iterator iter_object;
for(iter_object = m_vecObjects.begin(); iter_object != m_vecObjects.end(); iter_object++)
{
// [rad] Skip self
if(pObject != (*iter_object))
{
// [rad] Check and report collision if there's one..
if((*iter_object)->VCheckCollision(pObject))
{
// [rad] Mark both as in collision
pObject->VCollisionOn();
(*iter_object)->VCollisionOn();
}
}
}
*/
// [rad] Iterate over all objects in this bucket
ISpatialObject* pIter = m_pObjects;
while(pIter)
{
if(pIter != pObject)
{
if(pIter->VCheckCollision(pObject))
{
pObject->VCollisionOn();
pIter->VCollisionOn();
}
}
pIter = pIter->VGetNext();
}
}
}