-
Notifications
You must be signed in to change notification settings - Fork 2
/
FastCollisionDetectionLib.h
2455 lines (1843 loc) · 59.5 KB
/
FastCollisionDetectionLib.h
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FastCollisionDetectionLib.h
*
* Created on: Mar 11, 2022
* Author: tugrul
*/
#ifndef FASTCOLLISIONDETECTIONLIB_H_
#define FASTCOLLISIONDETECTIONLIB_H_
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_map>
#include<chrono>
#include<memory>
#include<math.h>
#include<queue>
#include<stack>
#include<thread>
#include<mutex>
#include<set>
#include<functional>
#include<condition_variable>
#include<unordered_set>
#include<cmath>
#include<iostream>
namespace FastColDetLib
{
template<typename T>
class SyncQueue
{
public:
SyncQueue(){}
void push(T t)
{
std::unique_lock<std::mutex> lc(m);
q.push(t);
c.notify_one();
}
void push2(T t)
{
std::unique_lock<std::mutex> lc(m);
q.push(t);
c.notify_all();
}
T pop()
{
std::unique_lock<std::mutex> lc(m);
while(q.empty())
{
c.wait(lc);
}
T result = q.front();
q.pop();
return result;
}
int size()
{
std::unique_lock<std::mutex> lc(m);
return q.size();
}
private:
std::queue<T> q;
std::mutex m;
std::condition_variable c;
};
struct MutexWithoutFalseSharing
{
std::mutex mut;
char padding[(64-sizeof(std::mutex))>0?(64-sizeof(std::mutex)):64];
};
inline
const int intersectDim(const float minx, const float maxx, const float minx2, const float maxx2) noexcept
{
return !((maxx < minx2) || (maxx2 < minx));
}
inline
void comp4vs4( const int * const __restrict__ partId1, const int * const __restrict__ partId2,
const float * const __restrict__ minx1, const float * const __restrict__ minx2,
const float * const __restrict__ miny1, const float * const __restrict__ miny2,
const float * const __restrict__ minz1, const float * const __restrict__ minz2,
const float * const __restrict__ maxx1, const float * const __restrict__ maxx2,
const float * const __restrict__ maxy1, const float * const __restrict__ maxy2,
const float * const __restrict__ maxz1, const float * const __restrict__ maxz2,
int * const __restrict__ out
) noexcept
{
alignas(32)
const int tileId2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
partId2[0],partId2[0],partId2[0],partId2[0],
partId2[1],partId2[1],partId2[1],partId2[1],
partId2[2],partId2[2],partId2[2],partId2[2],
partId2[3],partId2[3],partId2[3],partId2[3]
};
alignas(32)
const float tileMinX2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
minx2[0],minx2[0],minx2[0],minx2[0],
minx2[1],minx2[1],minx2[1],minx2[1],
minx2[2],minx2[2],minx2[2],minx2[2],
minx2[3],minx2[3],minx2[3],minx2[3]
};
alignas(32)
const float tileMinY2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
miny2[0],miny2[0],miny2[0],miny2[0],
miny2[1],miny2[1],miny2[1],miny2[1],
miny2[2],miny2[2],miny2[2],miny2[2],
miny2[3],miny2[3],miny2[3],miny2[3]
};
alignas(32)
const float tileMinZ2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
minz2[0],minz2[0],minz2[0],minz2[0],
minz2[1],minz2[1],minz2[1],minz2[1],
minz2[2],minz2[2],minz2[2],minz2[2],
minz2[3],minz2[3],minz2[3],minz2[3]
};
alignas(32)
const float tileMaxX2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
maxx2[0],maxx2[0],maxx2[0],maxx2[0],
maxx2[1],maxx2[1],maxx2[1],maxx2[1],
maxx2[2],maxx2[2],maxx2[2],maxx2[2],
maxx2[3],maxx2[3],maxx2[3],maxx2[3]
};
alignas(32)
const float tileMaxY2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
maxy2[0],maxy2[0],maxy2[0],maxy2[0],
maxy2[1],maxy2[1],maxy2[1],maxy2[1],
maxy2[2],maxy2[2],maxy2[2],maxy2[2],
maxy2[3],maxy2[3],maxy2[3],maxy2[3]
};
alignas(32)
const float tileMaxZ2[16]={
// 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
maxz2[0],maxz2[0],maxz2[0],maxz2[0],
maxz2[1],maxz2[1],maxz2[1],maxz2[1],
maxz2[2],maxz2[2],maxz2[2],maxz2[2],
maxz2[3],maxz2[3],maxz2[3],maxz2[3]
};
for(int i=0;i<16;i++)
{
const int o1 = (partId1[i] < tileId2[i]);
const int o2 = intersectDim(minx1[i], maxx1[i], tileMinX2[i], tileMaxX2[i]);
const int o3 = intersectDim(miny1[i], maxy1[i], tileMinY2[i], tileMaxY2[i]);
const int o4 = intersectDim(minz1[i], maxz1[i], tileMinZ2[i], tileMaxZ2[i]);
out[i] = o1 && o2 && o3 && o4;
}
};
/*
* interface to build various objects that can collide each other
*
*/
template<typename CoordType>
class IParticle
{
public:
virtual const CoordType getMaxX()const =0;
virtual const CoordType getMaxY()const =0;
virtual const CoordType getMaxZ()const =0;
virtual const CoordType getMinX()const =0;
virtual const CoordType getMinY()const =0;
virtual const CoordType getMinZ()const =0;
virtual const int getId()const =0;
const bool intersectX(IParticle<CoordType>* p)
{
return !((getMaxX() < p->getMinX()) || (p->getMaxX() < getMinX()));
}
const bool intersectY(IParticle<CoordType>* p)
{
return !((getMaxY() < p->getMinY()) || (p->getMaxY() < getMinY()));
}
const bool intersectZ(IParticle<CoordType>* p)
{
return !((getMaxZ() < p->getMinZ()) || (p->getMaxZ() < getMinZ()));
}
virtual ~IParticle(){};
};
class Bench
{
public:
Bench(size_t * targetPtr)
{
target=targetPtr;
t1 = std::chrono::duration_cast< std::chrono::nanoseconds >(std::chrono::high_resolution_clock::now().time_since_epoch());
}
~Bench()
{
t2 = std::chrono::duration_cast< std::chrono::nanoseconds >(std::chrono::high_resolution_clock::now().time_since_epoch());
*target= t2.count() - t1.count();
}
private:
size_t * target;
std::chrono::nanoseconds t1,t2;
};
// keeps record of unique values inserted
// works for positive integers (-1 reserved for first comparisons)
template<typename SignedIntegralType, int n>
struct FastUnique
{
public:
FastUnique()
{
it=0;
for(int i=0;i<n;i++)
dict[i]=-1;
}
inline
void reset()
{
it=0;
for(int i=0;i<n;i++)
dict[i]=-1;
}
inline
void insert(const SignedIntegralType val)
{
const bool result = testImpl(val);
dict[it]=(result?val:dict[it]);
it+=(result?1:0);
}
inline
const SignedIntegralType get(const int index) const noexcept
{
return dict[index];
}
inline
const bool test(const SignedIntegralType val) noexcept
{
return testImpl(val);
}
inline
const void iterateSet(const SignedIntegralType val) noexcept
{
dict[it++]=val;
}
const int size()
{
return it;
}
SignedIntegralType * begin()
{
return dict;
}
SignedIntegralType * end()
{
return dict + it;
}
private:
alignas(32)
SignedIntegralType dict[n];
alignas(32)
SignedIntegralType c[n];
int it;
inline
bool testImpl(const int val) noexcept
{
for(int i=0;i<n;i++)
c[i]=(dict[i]==val);
SignedIntegralType s = 0;
for(int i=0;i<n;i++)
s+=c[i];
return s==0;
}
};
template<typename DataType>
class Memory
{
public:
Memory()
{
memory=std::make_shared<std::vector<DataType>>();
allocPtr=std::make_shared<int>();
*allocPtr = 0;
allocPtrPtr=allocPtr.get();
memory->resize(1024);
ptr=memory->data();
}
inline
DataType * getPtr(const int index) const noexcept
{
return ptr+index;
}
inline
DataType& getRef(const int index) const noexcept
{
return ((DataType* __restrict__ const)ptr)[index];
}
inline
const DataType get(const int index) const noexcept
{
return ((DataType* __restrict__ const)ptr)[index];
}
inline
DataType get(const int index) noexcept
{
return ((DataType* __restrict__ const)ptr)[index];
}
inline
void set(const int index, const DataType data) const noexcept
{
((DataType* __restrict__ const)ptr)[index]=data;
}
inline
void readFrom(Memory<DataType>& mem, const int index, const int indexThis, const int n)
{
std::copy(mem.ptr+index,mem.ptr+index+n,ptr+indexThis);
}
inline
void writeTo(std::vector<DataType>& vec)
{
std::copy(ptr,ptr+*allocPtrPtr,vec.data());
}
inline
const int allocate(const int size)
{
const int result = *allocPtrPtr;
while(size + *allocPtrPtr >= memory->size())
{
memory->resize(memory->size()*2);
}
*allocPtrPtr += size;
ptr=memory->data();
return result;
}
inline
const int capacity()
{
return memory->size();
}
inline
const int size()
{
return *allocPtrPtr;
}
inline
void reset()
{
*allocPtrPtr = 0;
}
private:
DataType* ptr;
std::shared_ptr<int> allocPtr;
int* allocPtrPtr;
std::shared_ptr<std::vector<DataType>> memory;
};
constexpr int testParticleLimit = 128; // maximum particle AABB overlapping allowed on same cell
constexpr int testUniqueLimit = 32; // maximum particle AABB overlapping allowed per particle
struct MemoryPool
{
void clear()
{
nodeCollisionMask.reset();
childNodeCount.reset();
index.reset();
indexParticle.reset();
orderParticle.reset();
minX.reset();
maxX.reset();
minY.reset();
maxY.reset();
minZ.reset();
maxZ.reset();
nodeMinX.reset();
nodeMinY.reset();
nodeMinZ.reset();
nodeInvWidth.reset();
nodeInvHeight.reset();
nodeInvDepth.reset();
leafOffset.reset();
allPairsColl.reset();
allPairsCollmapping.reset();
for(int i=0;i<64;i++)
{
idTmp[i].reset();
orderTmp[i].reset();
}
}
// node-particle collision
Memory<uint64_t> nodeCollisionMask;
Memory<char> childNodeCount;
Memory<int> index;
Memory<int> indexParticle;
Memory<int> orderParticle;
Memory<float> nodeMinX;
Memory<float> nodeMinY;
Memory<float> nodeMinZ;
Memory<float> nodeInvWidth;
Memory<float> nodeInvHeight;
Memory<float> nodeInvDepth;
Memory<float> minX;
Memory<float> maxX;
Memory<float> minY;
Memory<float> maxY;
Memory<float> minZ;
Memory<float> maxZ;
Memory<int> idTmp[64];
Memory<int> orderTmp[64];
Memory<std::pair<int,int>> allPairsColl;
Memory<FastUnique<int32_t, testUniqueLimit>> allPairsCollmapping;
Memory<int> leafOffset;
};
struct AdaptiveGridV2Fields
{
AdaptiveGridV2Fields(MemoryPool mPool, const float minx, const float miny, const float minz,
const float maxx, const float maxy, const float maxz):mem(mPool),
minCornerX(minx),minCornerY(miny),minCornerZ(minz),maxCornerX(maxx),maxCornerY(maxy),maxCornerZ(maxz),
cellWidth ((maxx-minx)*0.25f),
cellHeight ((maxy-miny)*0.25f),
cellDepth ((maxz-minz)*0.25f),
cellWidthInv (1.0f/((maxx-minx)*0.25f)),
cellHeightInv(1.0f/((maxy-miny)*0.25f)),
cellDepthInv (1.0f/((maxz-minz)*0.25f))
{
}
MemoryPool mem;
const float minCornerX;
const float minCornerY;
const float minCornerZ;
const float maxCornerX;
const float maxCornerY;
const float maxCornerZ;
const float cellWidth;
const float cellHeight;
const float cellDepth;
const float cellWidthInv;
const float cellHeightInv;
const float cellDepthInv;
};
class AdaptiveGridV2
{
private:
// stores a bit in a byte at a position
inline void storeBit(uint64_t & data, const uint64_t value, const int pos) noexcept
{
data = (value << pos) | (data & ~(((uint64_t)1) << pos));
}
public:
AdaptiveGridV2(MemoryPool mem, const float minx, const float miny, const float minz,
const float maxx, const float maxy, const float maxz)
{
fields = std::make_shared<AdaptiveGridV2Fields>(mem,minx,miny,minz,maxx,maxy,maxz);
}
void clear()
{
fields->mem.clear();
// set current (root) node's particle start index to 0
const int indexParticleStart = fields->mem.index.allocate(1);
fields->mem.index.set(indexParticleStart,0);
// set current (root) node's number of particles to 0
const int indexNumParticles = fields->mem.index.allocate(1);
fields->mem.index.set(indexNumParticles,0);
// set current (root) node's child node start
const int indexChildNodeStart = fields->mem.index.allocate(1);
fields->mem.index.set(indexChildNodeStart,3);
// set AABB of current (root) node
// X
const int indexBoundMinXFloat = fields->mem.nodeMinX.allocate(1);
fields->mem.nodeMinX.set(indexBoundMinXFloat,fields->minCornerX);
// Y
const int indexBoundMinYFloat = fields->mem.nodeMinY.allocate(1);
fields->mem.nodeMinY.set(indexBoundMinYFloat,fields->minCornerY);
// Z
const int indexBoundMinZFloat = fields->mem.nodeMinZ.allocate(1);
fields->mem.nodeMinZ.set(indexBoundMinZFloat,fields->minCornerZ);
// cell inverse width
const int indexWidthFloat = fields->mem.nodeInvWidth.allocate(1);
fields->mem.nodeInvWidth.set(indexWidthFloat,fields->cellWidthInv);
// cell inverse height
const int indexHeightFloat = fields->mem.nodeInvHeight.allocate(1);
fields->mem.nodeInvHeight.set(indexHeightFloat,fields->cellHeightInv);
// cell inverse depth
const int indexDepthFloat = fields->mem.nodeInvDepth.allocate(1);
fields->mem.nodeInvDepth.set(indexDepthFloat,fields->cellDepthInv);
fields->mem.childNodeCount.set(fields->mem.childNodeCount.allocate(1),0);
fields->mem.nodeCollisionMask.set(fields->mem.nodeCollisionMask.allocate(1),0);
}
template<typename Derived>
inline void addParticles(const int numParticlesToAdd, Derived * const __restrict__ particles)
{
const int pId = fields->mem.indexParticle.allocate(numParticlesToAdd);
const int oId = fields->mem.orderParticle.allocate(numParticlesToAdd);
const int maxXId = fields->mem.maxX.allocate(numParticlesToAdd);
const int maxYId = fields->mem.maxY.allocate(numParticlesToAdd);
const int maxZId = fields->mem.maxZ.allocate(numParticlesToAdd);
const int minXId = fields->mem.minX.allocate(numParticlesToAdd);
const int minYId = fields->mem.minY.allocate(numParticlesToAdd);
const int minZId = fields->mem.minZ.allocate(numParticlesToAdd);
fields->mem.index.set(1,fields->mem.index.get(1)+numParticlesToAdd);
for(int i=0;i<numParticlesToAdd;i++)
{
const IParticle<float> * const curPtr = static_cast<const IParticle<float>* const>(particles+i);
fields->mem.indexParticle.set(pId+i,curPtr->getId());
fields->mem.orderParticle.set(oId+i,oId+i);
fields->mem.maxX.set(maxXId+i,curPtr->getMaxX());
fields->mem.maxY.set(maxYId+i,curPtr->getMaxY());
fields->mem.maxZ.set(maxZId+i,curPtr->getMaxZ());
fields->mem.minX.set(minXId+i,curPtr->getMinX());
fields->mem.minY.set(minYId+i,curPtr->getMinY());
fields->mem.minZ.set(minZId+i,curPtr->getMinZ());
}
}
inline void addParticlesWithoutInterface(const int numParticlesToAdd, const int particleOfs, std::vector<int> orders,
Memory<int> ids,
Memory<float> minx0, Memory<float> miny0, Memory<float> minz0,
Memory<float> maxx0, Memory<float> maxy0, Memory<float> maxz0
)
{
const int pId = fields->mem.indexParticle.allocate(numParticlesToAdd);
const int oId = fields->mem.orderParticle.allocate(numParticlesToAdd);
const int maxXId = fields->mem.maxX.allocate(numParticlesToAdd);
const int maxYId = fields->mem.maxY.allocate(numParticlesToAdd);
const int maxZId = fields->mem.maxZ.allocate(numParticlesToAdd);
const int minXId = fields->mem.minX.allocate(numParticlesToAdd);
const int minYId = fields->mem.minY.allocate(numParticlesToAdd);
const int minZId = fields->mem.minZ.allocate(numParticlesToAdd);
fields->mem.index.set(1,fields->mem.index.get(1)+numParticlesToAdd);
for(int i=0;i<numParticlesToAdd;i++)
{
const int ord = orders[i+particleOfs];
fields->mem.indexParticle.set(pId+i,ids.get(ord));
fields->mem.orderParticle.set(oId+i,oId+i);
fields->mem.maxX.set(maxXId+i,maxx0.get(ord));
fields->mem.maxY.set(maxYId+i,maxy0.get(ord));
fields->mem.maxZ.set(maxZId+i,maxz0.get(ord));
fields->mem.minX.set(minXId+i,minx0.get(ord));
fields->mem.minY.set(minYId+i,miny0.get(ord));
fields->mem.minZ.set(minZId+i,minz0.get(ord));
}
}
struct NodeTask
{
NodeTask(const int n1=0):nodePointer(n1){ }
const int nodePointer;
};
struct LeafTask
{
LeafTask(const int n1=0):particlePointer(n1){ }
int particlePointer;
};
// returns id values of particles
std::vector<int> findCollisions(const float minx, const float miny, const float minz,
const float maxx, const float maxy, const float maxz)
{
FastUnique<int32_t, testUniqueLimit> fastSet;
std::vector<int> result;
std::stack<NodeTask> nodesToCompute;
std::vector<LeafTask> particlesToCompute;
// push root node to work queue
nodesToCompute.push(NodeTask(0));
// traverse all colliding sparse cells
while(!nodesToCompute.empty() /* stack>=0 */)
{
NodeTask task = nodesToCompute.top();
nodesToCompute.pop();
const int pointer = fields->mem.index.get(task.nodePointer+2);
const int npdiv3 = task.nodePointer/3;
const int numChildNodes = fields->mem.childNodeCount.get(npdiv3);
// if this is not a leaf node, traverse all child nodes (they are sparse, so may be less than 8(8bit mask) or 64(64 bit mask))
if(pointer<0)
{
// get current node's information
const float minCornerX = fields->mem.nodeMinX.get(npdiv3);
const float minCornerY = fields->mem.nodeMinY.get(npdiv3);
const float minCornerZ = fields->mem.nodeMinZ.get(npdiv3);
const float cellWidthInv = fields->mem.nodeInvWidth.get(npdiv3);
const float cellHeightInv = fields->mem.nodeInvHeight.get(npdiv3);
const float cellDepthInv = fields->mem.nodeInvDepth.get(npdiv3);
const int indexStartX = std::floor((minx - minCornerX)*cellWidthInv);
const int indexEndX = std::floor((maxx - minCornerX)*cellWidthInv);
const int indexStartY = std::floor((miny - minCornerY)*cellHeightInv);
const int indexEndY = std::floor((maxy - minCornerY)*cellHeightInv);
const int indexStartZ = std::floor((minz - minCornerZ)*cellDepthInv);
const int indexEndZ = std::floor((maxz - minCornerZ)*cellDepthInv);
// prepare cell indicator mask (1 bit = has object, 0 bit = empty))
uint64_t maskCellsFilled=0;
for(int k=indexStartZ; k<=indexEndZ; k++)
{
if(k<0 || k>=4)
continue;
for(int j=indexStartY; j<=indexEndY; j++)
{
if(j<0 || j>=4)
continue;
for(int i=indexStartX; i<=indexEndX; i++)
{
if(i<0 || i>=4)
continue;
storeBit(maskCellsFilled,1,i+j*4+k*16);
}
}
}
const int nodeOffset = -pointer-1;
for(int i=0;i<numChildNodes;i++)
{
// if there is possible collision (accelerated by bit mask for collisions)
uint64_t cellMask = fields->mem.nodeCollisionMask.get((nodeOffset+i*3)/3);
if(maskCellsFilled & cellMask)
{
nodesToCompute.push(NodeTask(nodeOffset+i*3));
}
}
}
else
{
// this is leaf node
const int ptr = fields->mem.index.get(task.nodePointer);
const int n = fields->mem.index.get(task.nodePointer+1);
for(int i=0;i<n;i++)
{
const int index = ptr+i;
{
particlesToCompute.push_back(LeafTask(index));
}
}
}
}
const int sz = particlesToCompute.size();
//f(particlesToCompute.data(),sz);
for(int i=0;i<sz;i++)
{
const int index = particlesToCompute[i].particlePointer;
const int orderId = fields->mem.orderParticle.get(index);
const int partId = fields->mem.indexParticle.get(orderId);
if(fastSet.test(partId))
{
const float minX = fields->mem.minX.get(orderId);
const float maxX = fields->mem.maxX.get(orderId);
if(intersectDim(minx, maxx, minX, maxX))
{
const float minY = fields->mem.minY.get(orderId);
const float maxY = fields->mem.maxY.get(orderId);
if(intersectDim(miny, maxy, minY, maxY))
{
const float minZ = fields->mem.minZ.get(orderId);
const float maxZ = fields->mem.maxZ.get(orderId);
if(intersectDim(minz, maxz, minZ, maxZ))
{
fastSet.iterateSet(partId);
}
}
}
}
}
const int fsz = fastSet.size();
for(int i=0;i<fsz;i++)
{
result.push_back(fastSet.get(i));
}
return result;
}
std::vector<std::pair<int,int>> findCollisionsAll()
{
const int resetN = fields->mem.indexParticle.size();
fields->mem.allPairsCollmapping.reset();
fields->mem.allPairsCollmapping.allocate(resetN);
for(int i=0;i<resetN;i++)
{
fields->mem.allPairsCollmapping.getRef(i).reset();
}
fields->mem.allPairsColl.reset();
std::vector<std::pair<int,int>> result;
const int numLeaf = fields->mem.leafOffset.size();
for(int leaf=0;leaf<numLeaf;leaf++)
{
{
{
const int leafOfs = fields->mem.leafOffset.get(leaf);
const int ptr = fields->mem.index.get(leafOfs);
const int n = fields->mem.index.get(leafOfs+1);
if(n<2)
continue;
//continue;
alignas(32)
int index[testParticleLimit];
alignas(32)
int orderId[testParticleLimit];
alignas(32)
int partId[testParticleLimit];
alignas(32)
float minx[testParticleLimit];
alignas(32)
float miny[testParticleLimit];
alignas(32)
float minz[testParticleLimit];
alignas(32)
float maxx[testParticleLimit];
alignas(32)
float maxy[testParticleLimit];
alignas(32)
float maxz[testParticleLimit];
constexpr int simd = 4;
constexpr int simd1 = simd-1;
const int n8 = n-(n&simd1);
for(int i=0;i<n8;i+=simd)
{
for(int j=0;j<simd;j++)
index[i+j] = ptr + i + j;
for(int j=0;j<simd;j++)
orderId[i+j] = fields->mem.orderParticle.get(index[i+j]);
for(int j=0;j<simd;j++)
partId[i+j] = fields->mem.indexParticle.get(orderId[i+j]);
for(int j=0;j<simd;j++)
minx[i+j] = fields->mem.minX.get(orderId[i+j]);
for(int j=0;j<simd;j++)
miny[i+j] = fields->mem.minY.get(orderId[i+j]);
for(int j=0;j<simd;j++)
minz[i+j] = fields->mem.minZ.get(orderId[i+j]);
for(int j=0;j<simd;j++)
maxx[i+j] = fields->mem.maxX.get(orderId[i+j]);
for(int j=0;j<simd;j++)
maxy[i+j] = fields->mem.maxY.get(orderId[i+j]);
for(int j=0;j<simd;j++)
maxz[i+j] = fields->mem.maxZ.get(orderId[i+j]);
}
for(int i=n8;i<n;i++)
{
index[i] = ptr + i;
orderId[i] = fields->mem.orderParticle.get(index[i]);
partId[i] = fields->mem.indexParticle.get(orderId[i]);
minx[i] = fields->mem.minX.get(orderId[i]);
miny[i] = fields->mem.minY.get(orderId[i]);
minz[i] = fields->mem.minZ.get(orderId[i]);
maxx[i] = fields->mem.maxX.get(orderId[i]);
maxy[i] = fields->mem.maxY.get(orderId[i]);
maxz[i] = fields->mem.maxZ.get(orderId[i]);
}
for(int i=n;i<testParticleLimit;i++)
{
index[i] = -1;
orderId[i] = -1;
partId[i] = -1;
minx[i] = 1000000000000000000.0f;
miny[i] = 1000000000000000000.0f;
minz[i] = 1000000000000000000.0f;
maxx[i] = 1000000000000000000.0f;
maxy[i] = 1000000000000000000.0f;
maxz[i] = 1000000000000000000.0f;
}
// SIMD computation (tiled computing)
{
for(int i=0;i<testParticleLimit;i+=simd)
{
if(i>=n)
break;
alignas(32)