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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* OPCODE - Optimized Collision Detection
* Copyright (C) 2001 Pierre Terdiman
* Homepage: http://www.codercorner.com/Opcode.htm
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Contains an implementation of the sweep-and-prune algorithm (moved from Z-Collide)
* \file OPC_SweepAndPrune.cpp
* \author Pierre Terdiman
* \date January, 29, 2000
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Precompiled Header
#include "Stdafx.h"
using namespace Opcode;
inline_ void Sort(udword& id0, udword& id1)
{
if(id0>id1) Swap(id0, id1);
}
class Opcode::SAP_Element
{
public:
inline_ SAP_Element() {}
inline_ SAP_Element(udword id, SAP_Element* next) : mID(id), mNext(next) {}
inline_ ~SAP_Element() {}
udword mID;
SAP_Element* mNext;
};
class Opcode::SAP_Box
{
public:
SAP_EndPoint* Min[3];
SAP_EndPoint* Max[3];
};
class Opcode::SAP_EndPoint
{
public:
float Value; // Min or Max value
SAP_EndPoint* Previous; // Previous EndPoint whose Value is smaller than ours (or null)
SAP_EndPoint* Next; // Next EndPoint whose Value is greater than ours (or null)
udword Data; // Parent box ID *2 | MinMax flag
inline_ void SetData(udword box_id, BOOL is_max) { Data = (box_id<<1)|is_max; }
inline_ BOOL IsMax() const { return Data & 1; }
inline_ udword GetBoxID() const { return Data>>1; }
inline_ void InsertAfter(SAP_EndPoint* element)
{
if(this!=element && this!=element->Next)
{
// Remove
if(Previous) Previous->Next = Next;
if(Next) Next->Previous = Previous;
// Insert
Next = element->Next;
if(Next) Next->Previous = this;
element->Next = this;
Previous = element;
}
}
inline_ void InsertBefore(SAP_EndPoint* element)
{
if(this!=element && this!=element->Previous)
{
// Remove
if(Previous) Previous->Next = Next;
if(Next) Next->Previous = Previous;
// Insert
Previous = element->Previous;
element->Previous = this;
Next = element;
if(Previous) Previous->Next = this;
}
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SAP_PairData::SAP_PairData() :
mNbElements (0),
mNbUsedElements (0),
mElementPool (null),
mFirstFree (null),
mNbObjects (0),
mArray (null)
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Destructor.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SAP_PairData::~SAP_PairData()
{
Release();
}
void SAP_PairData::Release()
{
mNbElements = 0;
mNbUsedElements = 0;
mNbObjects = 0;
DELETEARRAY(mElementPool);
DELETEARRAY(mArray);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Initializes.
* \param nb_objects [in]
* \return true if success
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SAP_PairData::Init(udword nb_objects)
{
// Make sure everything has been released
Release();
if(!nb_objects) return false;
mArray = new SAP_Element*[nb_objects];
CHECKALLOC(mArray);
ZeroMemory(mArray, nb_objects*sizeof(SAP_Element*));
mNbObjects = nb_objects;
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Remaps a pointer when pool gets resized.
* \param element [in/out] remapped element
* \param delta [in] offset in bytes
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline_ void Remap(SAP_Element*& element, size_t delta)
{
if(element) element = (SAP_Element*)(size_t(element) + delta);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Gets a free element in the pool.
* \param id [in] element id
* \param next [in] next element
* \param remap [out] possible remapping offset
* \return the new element
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SAP_Element* SAP_PairData::GetFreeElem(udword id, SAP_Element* next, udword* remap)
{
if(remap) *remap = 0;
SAP_Element* FreeElem;
if(mFirstFree)
{
// Recycle
FreeElem = mFirstFree;
mFirstFree = mFirstFree->mNext; // First free = next free (or null)
}
else
{
if(mNbUsedElements==mNbElements)
{
// Resize
mNbElements = mNbElements ? (mNbElements<<1) : 2;
SAP_Element* NewElems = new SAP_Element[mNbElements];
if(mNbUsedElements) CopyMemory(NewElems, mElementPool, mNbUsedElements*sizeof(SAP_Element));
// Remap everything
{
size_t Delta = size_t(NewElems) - size_t(mElementPool);
for(udword i=0;i<mNbUsedElements;i++) Remap(NewElems[i].mNext, Delta);
for(udword i=0;i<mNbObjects;i++) Remap(mArray[i], Delta);
Remap(mFirstFree, Delta);
Remap(next, Delta);
if(remap) *remap = Delta;
}
DELETEARRAY(mElementPool);
mElementPool = NewElems;
}
FreeElem = &mElementPool[mNbUsedElements++];
}
FreeElem->mID = id;
FreeElem->mNext = next;
return FreeElem;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Frees an element of the pool.
* \param elem [in] element to free/recycle
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline_ void SAP_PairData::FreeElem(SAP_Element* elem)
{
elem->mNext = mFirstFree; // Next free
mFirstFree = elem;
}
// Add a pair to the set.
void SAP_PairData::AddPair(udword id1, udword id2)
{
// Order the ids
Sort(id1, id2);
ASSERT(id1<mNbObjects);
if(id1>=mNbObjects) return;
// Select the right list from "mArray".
SAP_Element* Current = mArray[id1];
if(!Current)
{
// Empty slot => create new element
mArray[id1] = GetFreeElem(id2, null);
}
else if(Current->mID>id2)
{
// The list is not empty but all elements are greater than id2 => insert id2 in the front.
mArray[id1] = GetFreeElem(id2, mArray[id1]);
}
else
{
// Else find the correct location in the sorted list (ascending order) and insert id2 there.
while(Current->mNext)
{
if(Current->mNext->mID > id2) break;
Current = Current->mNext;
}
if(Current->mID==id2) return; // The pair already exists
// Current->mNext = GetFreeElem(id2, Current->mNext);
udword Delta;
SAP_Element* E = GetFreeElem(id2, Current->mNext, &Delta);
if(Delta) Remap(Current, Delta);
Current->mNext = E;
}
}
// Delete a pair from the set.
void SAP_PairData::RemovePair(udword id1, udword id2)
{
// Order the ids.
Sort(id1, id2);
// Exit if the pair doesn't exist in the set
if(id1>=mNbObjects) return;
// Otherwise, select the correct list.
SAP_Element* Current = mArray[id1];
// If this list is empty, the pair doesn't exist.
if(!Current) return;
// Otherwise, if id2 is the first element, delete it.
if(Current->mID==id2)
{
mArray[id1] = Current->mNext;
FreeElem(Current);
}
else
{
// If id2 is not the first element, start traversing the sorted list.
while(Current->mNext)
{
// If we have moved too far away without hitting id2, then the pair doesn't exist
if(Current->mNext->mID > id2) return;
// Otherwise, delete id2.
if(Current->mNext->mID == id2)
{
SAP_Element* Temp = Current->mNext;
Current->mNext = Temp->mNext;
FreeElem(Temp);
return;
}
Current = Current->mNext;
}
}
}
void SAP_PairData::DumpPairs(Pairs& pairs) const
{
// ### Ugly and slow
for(udword i=0;i<mNbObjects;i++)
{
SAP_Element* Current = mArray[i];
while(Current)
{
ASSERT(Current->mID<mNbObjects);
pairs.AddPair(i, Current->mID);
Current = Current->mNext;
}
}
}
void SAP_PairData::DumpPairs(PairCallback callback, void* user_data) const
{
if(!callback) return;
// ### Ugly and slow
for(udword i=0;i<mNbObjects;i++)
{
SAP_Element* Current = mArray[i];
while(Current)
{
ASSERT(Current->mID<mNbObjects);
if(!(callback)(i, Current->mID, user_data)) return;
Current = Current->mNext;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SweepAndPrune::SweepAndPrune()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Destructor.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SweepAndPrune::~SweepAndPrune()
{
}
void SweepAndPrune::GetPairs(Pairs& pairs) const
{
mPairs.DumpPairs(pairs);
}
void SweepAndPrune::GetPairs(PairCallback callback, void* user_data) const
{
mPairs.DumpPairs(callback, user_data);
}
bool SweepAndPrune::Init(udword nb_objects, const AABB** boxes)
{
// 1) Create sorted lists
mNbObjects = nb_objects;
mBoxes = new SAP_Box[nb_objects];
// for(udword i=0;i<nb_objects;i++) mBoxes[i].Box = *boxes[i];
float* Data = new float[nb_objects*2];
for(udword Axis=0;Axis<3;Axis++)
{
mList[Axis] = new SAP_EndPoint[nb_objects*2];
for(udword i=0;i<nb_objects;i++)
{
Data[i*2+0] = boxes[i]->GetMin(Axis);
Data[i*2+1] = boxes[i]->GetMax(Axis);
}
RadixSort RS;
const udword* Sorted = RS.Sort(Data, nb_objects*2).GetRanks();
SAP_EndPoint* PreviousEndPoint = null;
for(udword i=0;i<nb_objects*2;i++)
{
udword SortedIndex = *Sorted++;
float SortedCoord = Data[SortedIndex];
udword BoxIndex = SortedIndex>>1;
ASSERT(BoxIndex<nb_objects);
SAP_EndPoint* CurrentEndPoint = &mList[Axis][SortedIndex];
CurrentEndPoint->Value = SortedCoord;
// CurrentEndPoint->IsMax = SortedIndex&1; // ### could be implicit ?
// CurrentEndPoint->ID = BoxIndex; // ### could be implicit ?
CurrentEndPoint->SetData(BoxIndex, SortedIndex&1); // ### could be implicit ?
CurrentEndPoint->Previous = PreviousEndPoint;
CurrentEndPoint->Next = null;
if(PreviousEndPoint) PreviousEndPoint->Next = CurrentEndPoint;
if(CurrentEndPoint->IsMax()) mBoxes[BoxIndex].Max[Axis] = CurrentEndPoint;
else mBoxes[BoxIndex].Min[Axis] = CurrentEndPoint;
PreviousEndPoint = CurrentEndPoint;
}
}
DELETEARRAY(Data);
CheckListsIntegrity();
// 2) Quickly find starting pairs
mPairs.Init(nb_objects);
{
Pairs P;
CompleteBoxPruning(nb_objects, boxes, P, Axes(AXES_XZY));
for(udword i=0;i<P.GetNbPairs();i++)
{
const Pair* PP = P.GetPair(i);
udword id0 = PP->id0;
udword id1 = PP->id1;
if(id0!=id1 && boxes[id0]->Intersect(*boxes[id1]))
{
mPairs.AddPair(id0, id1);
}
else ASSERT(0);
}
}
return true;
}
bool SweepAndPrune::CheckListsIntegrity()
{
for(udword Axis=0;Axis<3;Axis++)
{
// Find list head
SAP_EndPoint* Current = mList[Axis];
while(Current->Previous) Current = Current->Previous;
udword Nb = 0;
SAP_EndPoint* Previous = null;
while(Current)
{
Nb++;
if(Previous)
{
ASSERT(Previous->Value <= Current->Value);
if(Previous->Value > Current->Value) return false;
}
ASSERT(Current->Previous==Previous);
if(Current->Previous!=Previous) return false;
Previous = Current;
Current = Current->Next;
}
ASSERT(Nb==mNbObjects*2);
}
return true;
}
inline_ BOOL Intersect(const AABB& a, const SAP_Box& b)
{
if(b.Max[0]->Value < a.GetMin(0) || a.GetMax(0) < b.Min[0]->Value
|| b.Max[1]->Value < a.GetMin(1) || a.GetMax(1) < b.Min[1]->Value
|| b.Max[2]->Value < a.GetMin(2) || a.GetMax(2) < b.Min[2]->Value) return FALSE;
return TRUE;
}
bool SweepAndPrune::UpdateObject(udword i, const AABB& box)
{
for(udword Axis=0;Axis<3;Axis++)
{
// udword Base = (udword)&mList[Axis][0];
// Update min
{
SAP_EndPoint* const CurrentMin = mBoxes[i].Min[Axis];
ASSERT(!CurrentMin->IsMax());
const float Limit = box.GetMin(Axis);
if(Limit == CurrentMin->Value)
{
}
else if(Limit < CurrentMin->Value)
{
CurrentMin->Value = Limit;
// Min is moving left:
SAP_EndPoint* NewPos = CurrentMin;
ASSERT(NewPos);
SAP_EndPoint* tmp;
while((tmp = NewPos->Previous) && tmp->Value > Limit)
{
NewPos = tmp;
if(NewPos->IsMax())
{
// Our min passed a max => start overlap
//udword SortedIndex = (udword(CurrentMin) - Base)/sizeof(NS_EndPoint);
const udword id0 = CurrentMin->GetBoxID();
const udword id1 = NewPos->GetBoxID();
if(id0!=id1 && Intersect(box, mBoxes[id1])) mPairs.AddPair(id0, id1);
}
}
CurrentMin->InsertBefore(NewPos);
}
else// if(Limit > CurrentMin->Value)
{
CurrentMin->Value = Limit;
// Min is moving right:
SAP_EndPoint* NewPos = CurrentMin;
ASSERT(NewPos);
SAP_EndPoint* tmp;
while((tmp = NewPos->Next) && tmp->Value < Limit)
{
NewPos = tmp;
if(NewPos->IsMax())
{
// Our min passed a max => stop overlap
const udword id0 = CurrentMin->GetBoxID();
const udword id1 = NewPos->GetBoxID();
if(id0!=id1) mPairs.RemovePair(id0, id1);
}
}
CurrentMin->InsertAfter(NewPos);
}
}
// Update max
{
SAP_EndPoint* const CurrentMax = mBoxes[i].Max[Axis];
ASSERT(CurrentMax->IsMax());
const float Limit = box.GetMax(Axis);
if(Limit == CurrentMax->Value)
{
}
else if(Limit > CurrentMax->Value)
{
CurrentMax->Value = Limit;
// Max is moving right:
SAP_EndPoint* NewPos = CurrentMax;
ASSERT(NewPos);
SAP_EndPoint* tmp;
while((tmp = NewPos->Next) && tmp->Value < Limit)
{
NewPos = tmp;
if(!NewPos->IsMax())
{
// Our max passed a min => start overlap
const udword id0 = CurrentMax->GetBoxID();
const udword id1 = NewPos->GetBoxID();
if(id0!=id1 && Intersect(box, mBoxes[id1])) mPairs.AddPair(id0, id1);
}
}
CurrentMax->InsertAfter(NewPos);
}
else// if(Limit < CurrentMax->Value)
{
CurrentMax->Value = Limit;
// Max is moving left:
SAP_EndPoint* NewPos = CurrentMax;
ASSERT(NewPos);
SAP_EndPoint* tmp;
while((tmp = NewPos->Previous) && tmp->Value > Limit)
{
NewPos = tmp;
if(!NewPos->IsMax())
{
// Our max passed a min => stop overlap
const udword id0 = CurrentMax->GetBoxID();
const udword id1 = NewPos->GetBoxID();
if(id0!=id1) mPairs.RemovePair(id0, id1);
}
}
CurrentMax->InsertBefore(NewPos);
}
}
}
return true;
}
|