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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
|
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
static const char* mprKernelsCL= \
"/***\n"
" * ---------------------------------\n"
" * Copyright (c)2012 Daniel Fiser <danfis@danfis.cz>\n"
" *\n"
" * This file was ported from mpr.c file, part of libccd.\n"
" * The Minkoski Portal Refinement implementation was ported \n"
" * to OpenCL by Erwin Coumans for the Bullet 3 Physics library.\n"
" * at http://github.com/erwincoumans/bullet3\n"
" *\n"
" * Distributed under the OSI-approved BSD License (the \"License\");\n"
" * see <http://www.opensource.org/licenses/bsd-license.php>.\n"
" * This software is distributed WITHOUT ANY WARRANTY; without even the\n"
" * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
" * See the License for more information.\n"
" */\n"
"#ifndef B3_MPR_PENETRATION_H\n"
"#define B3_MPR_PENETRATION_H\n"
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
"#define B3_PLATFORM_DEFINITIONS_H\n"
"struct MyTest\n"
"{\n"
" int bla;\n"
"};\n"
"#ifdef __cplusplus\n"
"#else\n"
"//keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX\n"
"#define B3_LARGE_FLOAT 1e18f\n"
"#define B3_INFINITY 1e18f\n"
"#define b3Assert(a)\n"
"#define b3ConstArray(a) __global const a*\n"
"#define b3AtomicInc atomic_inc\n"
"#define b3AtomicAdd atomic_add\n"
"#define b3Fabs fabs\n"
"#define b3Sqrt native_sqrt\n"
"#define b3Sin native_sin\n"
"#define b3Cos native_cos\n"
"#define B3_STATIC\n"
"#endif\n"
"#endif\n"
"#ifndef B3_FLOAT4_H\n"
"#define B3_FLOAT4_H\n"
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif\n"
"#endif\n"
"#ifdef __cplusplus\n"
"#else\n"
" typedef float4 b3Float4;\n"
" #define b3Float4ConstArg const b3Float4\n"
" #define b3MakeFloat4 (float4)\n"
" float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
" {\n"
" float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
" float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
" return dot(a1, b1);\n"
" }\n"
" b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
" {\n"
" float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
" float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
" return cross(a1, b1);\n"
" }\n"
" #define b3MinFloat4 min\n"
" #define b3MaxFloat4 max\n"
" #define b3Normalized(a) normalize(a)\n"
"#endif \n"
" \n"
"inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
"{\n"
" if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6) \n"
" return false;\n"
" return true;\n"
"}\n"
"inline int b3MaxDot( b3Float4ConstArg vec, __global const b3Float4* vecArray, int vecLen, float* dotOut )\n"
"{\n"
" float maxDot = -B3_INFINITY;\n"
" int i = 0;\n"
" int ptIndex = -1;\n"
" for( i = 0; i < vecLen; i++ )\n"
" {\n"
" float dot = b3Dot3F4(vecArray[i],vec);\n"
" \n"
" if( dot > maxDot )\n"
" {\n"
" maxDot = dot;\n"
" ptIndex = i;\n"
" }\n"
" }\n"
" b3Assert(ptIndex>=0);\n"
" if (ptIndex<0)\n"
" {\n"
" ptIndex = 0;\n"
" }\n"
" *dotOut = maxDot;\n"
" return ptIndex;\n"
"}\n"
"#endif //B3_FLOAT4_H\n"
"#ifndef B3_RIGIDBODY_DATA_H\n"
"#define B3_RIGIDBODY_DATA_H\n"
"#ifndef B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_FLOAT4_H\n"
"#ifndef B3_QUAT_H\n"
"#define B3_QUAT_H\n"
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif\n"
"#endif\n"
"#ifndef B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
" typedef float4 b3Quat;\n"
" #define b3QuatConstArg const b3Quat\n"
" \n"
" \n"
"inline float4 b3FastNormalize4(float4 v)\n"
"{\n"
" v = (float4)(v.xyz,0.f);\n"
" return fast_normalize(v);\n"
"}\n"
" \n"
"inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
"inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
"inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
"inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
"inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
"inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
"{\n"
" b3Quat ans;\n"
" ans = b3Cross3( a, b );\n"
" ans += a.w*b+b.w*a;\n"
"// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
" ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
" return ans;\n"
"}\n"
"inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
"{\n"
" b3Quat q;\n"
" q=in;\n"
" //return b3FastNormalize4(in);\n"
" float len = native_sqrt(dot(q, q));\n"
" if(len > 0.f)\n"
" {\n"
" q *= 1.f / len;\n"
" }\n"
" else\n"
" {\n"
" q.x = q.y = q.z = 0.f;\n"
" q.w = 1.f;\n"
" }\n"
" return q;\n"
"}\n"
"inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
"{\n"
" b3Quat qInv = b3QuatInvert( q );\n"
" float4 vcpy = vec;\n"
" vcpy.w = 0.f;\n"
" float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
" return out;\n"
"}\n"
"inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
"{\n"
" return (b3Quat)(-q.xyz, q.w);\n"
"}\n"
"inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
"{\n"
" return (b3Quat)(-q.xyz, q.w);\n"
"}\n"
"inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
"{\n"
" return b3QuatRotate( b3QuatInvert( q ), vec );\n"
"}\n"
"inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg orientation)\n"
"{\n"
" return b3QuatRotate( orientation, point ) + (translation);\n"
"}\n"
" \n"
"#endif \n"
"#endif //B3_QUAT_H\n"
"#ifndef B3_MAT3x3_H\n"
"#define B3_MAT3x3_H\n"
"#ifndef B3_QUAT_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_QUAT_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"typedef struct\n"
"{\n"
" b3Float4 m_row[3];\n"
"}b3Mat3x3;\n"
"#define b3Mat3x3ConstArg const b3Mat3x3\n"
"#define b3GetRow(m,row) (m.m_row[row])\n"
"inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
"{\n"
" b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
" b3Mat3x3 out;\n"
" out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
" out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
" out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
" out.m_row[0].w = 0.f;\n"
" out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
" out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
" out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
" out.m_row[1].w = 0.f;\n"
" out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
" out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
" out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
" out.m_row[2].w = 0.f;\n"
" return out;\n"
"}\n"
"inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
"{\n"
" b3Mat3x3 out;\n"
" out.m_row[0] = fabs(matIn.m_row[0]);\n"
" out.m_row[1] = fabs(matIn.m_row[1]);\n"
" out.m_row[2] = fabs(matIn.m_row[2]);\n"
" return out;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtZero();\n"
"__inline\n"
"b3Mat3x3 mtIdentity();\n"
"__inline\n"
"b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
"__inline\n"
"b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
"__inline\n"
"b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
"__inline\n"
"b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
"__inline\n"
"b3Mat3x3 mtZero()\n"
"{\n"
" b3Mat3x3 m;\n"
" m.m_row[0] = (b3Float4)(0.f);\n"
" m.m_row[1] = (b3Float4)(0.f);\n"
" m.m_row[2] = (b3Float4)(0.f);\n"
" return m;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtIdentity()\n"
"{\n"
" b3Mat3x3 m;\n"
" m.m_row[0] = (b3Float4)(1,0,0,0);\n"
" m.m_row[1] = (b3Float4)(0,1,0,0);\n"
" m.m_row[2] = (b3Float4)(0,0,1,0);\n"
" return m;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
"{\n"
" b3Mat3x3 out;\n"
" out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
" out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
" out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
" return out;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
"{\n"
" b3Mat3x3 transB;\n"
" transB = mtTranspose( b );\n"
" b3Mat3x3 ans;\n"
" // why this doesn't run when 0ing in the for{}\n"
" a.m_row[0].w = 0.f;\n"
" a.m_row[1].w = 0.f;\n"
" a.m_row[2].w = 0.f;\n"
" for(int i=0; i<3; i++)\n"
" {\n"
"// a.m_row[i].w = 0.f;\n"
" ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
" ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
" ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
" ans.m_row[i].w = 0.f;\n"
" }\n"
" return ans;\n"
"}\n"
"__inline\n"
"b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
"{\n"
" b3Float4 ans;\n"
" ans.x = b3Dot3F4( a.m_row[0], b );\n"
" ans.y = b3Dot3F4( a.m_row[1], b );\n"
" ans.z = b3Dot3F4( a.m_row[2], b );\n"
" ans.w = 0.f;\n"
" return ans;\n"
"}\n"
"__inline\n"
"b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
"{\n"
" b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
" b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
" b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
" b3Float4 ans;\n"
" ans.x = b3Dot3F4( a, colx );\n"
" ans.y = b3Dot3F4( a, coly );\n"
" ans.z = b3Dot3F4( a, colz );\n"
" return ans;\n"
"}\n"
"#endif\n"
"#endif //B3_MAT3x3_H\n"
"typedef struct b3RigidBodyData b3RigidBodyData_t;\n"
"struct b3RigidBodyData\n"
"{\n"
" b3Float4 m_pos;\n"
" b3Quat m_quat;\n"
" b3Float4 m_linVel;\n"
" b3Float4 m_angVel;\n"
" int m_collidableIdx;\n"
" float m_invMass;\n"
" float m_restituitionCoeff;\n"
" float m_frictionCoeff;\n"
"};\n"
"typedef struct b3InertiaData b3InertiaData_t;\n"
"struct b3InertiaData\n"
"{\n"
" b3Mat3x3 m_invInertiaWorld;\n"
" b3Mat3x3 m_initInvInertia;\n"
"};\n"
"#endif //B3_RIGIDBODY_DATA_H\n"
" \n"
"#ifndef B3_CONVEX_POLYHEDRON_DATA_H\n"
"#define B3_CONVEX_POLYHEDRON_DATA_H\n"
"#ifndef B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_FLOAT4_H\n"
"#ifndef B3_QUAT_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_QUAT_H\n"
"typedef struct b3GpuFace b3GpuFace_t;\n"
"struct b3GpuFace\n"
"{\n"
" b3Float4 m_plane;\n"
" int m_indexOffset;\n"
" int m_numIndices;\n"
" int m_unusedPadding1;\n"
" int m_unusedPadding2;\n"
"};\n"
"typedef struct b3ConvexPolyhedronData b3ConvexPolyhedronData_t;\n"
"struct b3ConvexPolyhedronData\n"
"{\n"
" b3Float4 m_localCenter;\n"
" b3Float4 m_extents;\n"
" b3Float4 mC;\n"
" b3Float4 mE;\n"
" float m_radius;\n"
" int m_faceOffset;\n"
" int m_numFaces;\n"
" int m_numVertices;\n"
" int m_vertexOffset;\n"
" int m_uniqueEdgesOffset;\n"
" int m_numUniqueEdges;\n"
" int m_unused;\n"
"};\n"
"#endif //B3_CONVEX_POLYHEDRON_DATA_H\n"
"#ifndef B3_COLLIDABLE_H\n"
"#define B3_COLLIDABLE_H\n"
"#ifndef B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_FLOAT4_H\n"
"#ifndef B3_QUAT_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_QUAT_H\n"
"enum b3ShapeTypes\n"
"{\n"
" SHAPE_HEIGHT_FIELD=1,\n"
" SHAPE_CONVEX_HULL=3,\n"
" SHAPE_PLANE=4,\n"
" SHAPE_CONCAVE_TRIMESH=5,\n"
" SHAPE_COMPOUND_OF_CONVEX_HULLS=6,\n"
" SHAPE_SPHERE=7,\n"
" MAX_NUM_SHAPE_TYPES,\n"
"};\n"
"typedef struct b3Collidable b3Collidable_t;\n"
"struct b3Collidable\n"
"{\n"
" union {\n"
" int m_numChildShapes;\n"
" int m_bvhIndex;\n"
" };\n"
" union\n"
" {\n"
" float m_radius;\n"
" int m_compoundBvhIndex;\n"
" };\n"
" int m_shapeType;\n"
" int m_shapeIndex;\n"
"};\n"
"typedef struct b3GpuChildShape b3GpuChildShape_t;\n"
"struct b3GpuChildShape\n"
"{\n"
" b3Float4 m_childPosition;\n"
" b3Quat m_childOrientation;\n"
" int m_shapeIndex;\n"
" int m_unused0;\n"
" int m_unused1;\n"
" int m_unused2;\n"
"};\n"
"struct b3CompoundOverlappingPair\n"
"{\n"
" int m_bodyIndexA;\n"
" int m_bodyIndexB;\n"
"// int m_pairType;\n"
" int m_childShapeIndexA;\n"
" int m_childShapeIndexB;\n"
"};\n"
"#endif //B3_COLLIDABLE_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#define B3_MPR_SQRT sqrt\n"
"#endif\n"
"#define B3_MPR_FMIN(x, y) ((x) < (y) ? (x) : (y))\n"
"#define B3_MPR_FABS fabs\n"
"#define B3_MPR_TOLERANCE 1E-6f\n"
"#define B3_MPR_MAX_ITERATIONS 1000\n"
"struct _b3MprSupport_t \n"
"{\n"
" b3Float4 v; //!< Support point in minkowski sum\n"
" b3Float4 v1; //!< Support point in obj1\n"
" b3Float4 v2; //!< Support point in obj2\n"
"};\n"
"typedef struct _b3MprSupport_t b3MprSupport_t;\n"
"struct _b3MprSimplex_t \n"
"{\n"
" b3MprSupport_t ps[4];\n"
" int last; //!< index of last added point\n"
"};\n"
"typedef struct _b3MprSimplex_t b3MprSimplex_t;\n"
"inline b3MprSupport_t* b3MprSimplexPointW(b3MprSimplex_t *s, int idx)\n"
"{\n"
" return &s->ps[idx];\n"
"}\n"
"inline void b3MprSimplexSetSize(b3MprSimplex_t *s, int size)\n"
"{\n"
" s->last = size - 1;\n"
"}\n"
"inline int b3MprSimplexSize(const b3MprSimplex_t *s)\n"
"{\n"
" return s->last + 1;\n"
"}\n"
"inline const b3MprSupport_t* b3MprSimplexPoint(const b3MprSimplex_t* s, int idx)\n"
"{\n"
" // here is no check on boundaries\n"
" return &s->ps[idx];\n"
"}\n"
"inline void b3MprSupportCopy(b3MprSupport_t *d, const b3MprSupport_t *s)\n"
"{\n"
" *d = *s;\n"
"}\n"
"inline void b3MprSimplexSet(b3MprSimplex_t *s, size_t pos, const b3MprSupport_t *a)\n"
"{\n"
" b3MprSupportCopy(s->ps + pos, a);\n"
"}\n"
"inline void b3MprSimplexSwap(b3MprSimplex_t *s, size_t pos1, size_t pos2)\n"
"{\n"
" b3MprSupport_t supp;\n"
" b3MprSupportCopy(&supp, &s->ps[pos1]);\n"
" b3MprSupportCopy(&s->ps[pos1], &s->ps[pos2]);\n"
" b3MprSupportCopy(&s->ps[pos2], &supp);\n"
"}\n"
"inline int b3MprIsZero(float val)\n"
"{\n"
" return B3_MPR_FABS(val) < FLT_EPSILON;\n"
"}\n"
"inline int b3MprEq(float _a, float _b)\n"
"{\n"
" float ab;\n"
" float a, b;\n"
" ab = B3_MPR_FABS(_a - _b);\n"
" if (B3_MPR_FABS(ab) < FLT_EPSILON)\n"
" return 1;\n"
" a = B3_MPR_FABS(_a);\n"
" b = B3_MPR_FABS(_b);\n"
" if (b > a){\n"
" return ab < FLT_EPSILON * b;\n"
" }else{\n"
" return ab < FLT_EPSILON * a;\n"
" }\n"
"}\n"
"inline int b3MprVec3Eq(const b3Float4* a, const b3Float4 *b)\n"
"{\n"
" return b3MprEq((*a).x, (*b).x)\n"
" && b3MprEq((*a).y, (*b).y)\n"
" && b3MprEq((*a).z, (*b).z);\n"
"}\n"
"inline b3Float4 b3LocalGetSupportVertex(b3Float4ConstArg supportVec,__global const b3ConvexPolyhedronData_t* hull, b3ConstArray(b3Float4) verticesA)\n"
"{\n"
" b3Float4 supVec = b3MakeFloat4(0,0,0,0);\n"
" float maxDot = -B3_LARGE_FLOAT;\n"
" if( 0 < hull->m_numVertices )\n"
" {\n"
" const b3Float4 scaled = supportVec;\n"
" int index = b3MaxDot(scaled, &verticesA[hull->m_vertexOffset], hull->m_numVertices, &maxDot);\n"
" return verticesA[hull->m_vertexOffset+index];\n"
" }\n"
" return supVec;\n"
"}\n"
"B3_STATIC void b3MprConvexSupport(int pairIndex,int bodyIndex, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
" b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
" b3ConstArray(b3Collidable_t) cpuCollidables,\n"
" b3ConstArray(b3Float4) cpuVertices,\n"
" __global b3Float4* sepAxis,\n"
" const b3Float4* _dir, b3Float4* outp, int logme)\n"
"{\n"
" //dir is in worldspace, move to local space\n"
" \n"
" b3Float4 pos = cpuBodyBuf[bodyIndex].m_pos;\n"
" b3Quat orn = cpuBodyBuf[bodyIndex].m_quat;\n"
" \n"
" b3Float4 dir = b3MakeFloat4((*_dir).x,(*_dir).y,(*_dir).z,0.f);\n"
" \n"
" const b3Float4 localDir = b3QuatRotate(b3QuatInverse(orn),dir);\n"
" \n"
" //find local support vertex\n"
" int colIndex = cpuBodyBuf[bodyIndex].m_collidableIdx;\n"
" \n"
" b3Assert(cpuCollidables[colIndex].m_shapeType==SHAPE_CONVEX_HULL);\n"
" __global const b3ConvexPolyhedronData_t* hull = &cpuConvexData[cpuCollidables[colIndex].m_shapeIndex];\n"
" \n"
" b3Float4 pInA;\n"
" if (logme)\n"
" {\n"
" b3Float4 supVec = b3MakeFloat4(0,0,0,0);\n"
" float maxDot = -B3_LARGE_FLOAT;\n"
" if( 0 < hull->m_numVertices )\n"
" {\n"
" const b3Float4 scaled = localDir;\n"
" int index = b3MaxDot(scaled, &cpuVertices[hull->m_vertexOffset], hull->m_numVertices, &maxDot);\n"
" pInA = cpuVertices[hull->m_vertexOffset+index];\n"
" \n"
" }\n"
" } else\n"
" {\n"
" pInA = b3LocalGetSupportVertex(localDir,hull,cpuVertices);\n"
" }\n"
" //move vertex to world space\n"
" *outp = b3TransformPoint(pInA,pos,orn);\n"
" \n"
"}\n"
"inline void b3MprSupport(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
" b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
" b3ConstArray(b3Collidable_t) cpuCollidables,\n"
" b3ConstArray(b3Float4) cpuVertices,\n"
" __global b3Float4* sepAxis,\n"
" const b3Float4* _dir, b3MprSupport_t *supp)\n"
"{\n"
" b3Float4 dir;\n"
" dir = *_dir;\n"
" b3MprConvexSupport(pairIndex,bodyIndexA,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,&dir, &supp->v1,0);\n"
" dir = *_dir*-1.f;\n"
" b3MprConvexSupport(pairIndex,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,&dir, &supp->v2,0);\n"
" supp->v = supp->v1 - supp->v2;\n"
"}\n"
"inline void b3FindOrigin(int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, b3MprSupport_t *center)\n"
"{\n"
" center->v1 = cpuBodyBuf[bodyIndexA].m_pos;\n"
" center->v2 = cpuBodyBuf[bodyIndexB].m_pos;\n"
" center->v = center->v1 - center->v2;\n"
"}\n"
"inline void b3MprVec3Set(b3Float4 *v, float x, float y, float z)\n"
"{\n"
" (*v).x = x;\n"
" (*v).y = y;\n"
" (*v).z = z;\n"
" (*v).w = 0.f;\n"
"}\n"
"inline void b3MprVec3Add(b3Float4 *v, const b3Float4 *w)\n"
"{\n"
" (*v).x += (*w).x;\n"
" (*v).y += (*w).y;\n"
" (*v).z += (*w).z;\n"
"}\n"
"inline void b3MprVec3Copy(b3Float4 *v, const b3Float4 *w)\n"
"{\n"
" *v = *w;\n"
"}\n"
"inline void b3MprVec3Scale(b3Float4 *d, float k)\n"
"{\n"
" *d *= k;\n"
"}\n"
"inline float b3MprVec3Dot(const b3Float4 *a, const b3Float4 *b)\n"
"{\n"
" float dot;\n"
" dot = b3Dot3F4(*a,*b);\n"
" return dot;\n"
"}\n"
"inline float b3MprVec3Len2(const b3Float4 *v)\n"
"{\n"
" return b3MprVec3Dot(v, v);\n"
"}\n"
"inline void b3MprVec3Normalize(b3Float4 *d)\n"
"{\n"
" float k = 1.f / B3_MPR_SQRT(b3MprVec3Len2(d));\n"
" b3MprVec3Scale(d, k);\n"
"}\n"
"inline void b3MprVec3Cross(b3Float4 *d, const b3Float4 *a, const b3Float4 *b)\n"
"{\n"
" *d = b3Cross3(*a,*b);\n"
" \n"
"}\n"
"inline void b3MprVec3Sub2(b3Float4 *d, const b3Float4 *v, const b3Float4 *w)\n"
"{\n"
" *d = *v - *w;\n"
"}\n"
"inline void b3PortalDir(const b3MprSimplex_t *portal, b3Float4 *dir)\n"
"{\n"
" b3Float4 v2v1, v3v1;\n"
" b3MprVec3Sub2(&v2v1, &b3MprSimplexPoint(portal, 2)->v,\n"
" &b3MprSimplexPoint(portal, 1)->v);\n"
" b3MprVec3Sub2(&v3v1, &b3MprSimplexPoint(portal, 3)->v,\n"
" &b3MprSimplexPoint(portal, 1)->v);\n"
" b3MprVec3Cross(dir, &v2v1, &v3v1);\n"
" b3MprVec3Normalize(dir);\n"
"}\n"
"inline int portalEncapsulesOrigin(const b3MprSimplex_t *portal,\n"
" const b3Float4 *dir)\n"
"{\n"
" float dot;\n"
" dot = b3MprVec3Dot(dir, &b3MprSimplexPoint(portal, 1)->v);\n"
" return b3MprIsZero(dot) || dot > 0.f;\n"
"}\n"
"inline int portalReachTolerance(const b3MprSimplex_t *portal,\n"
" const b3MprSupport_t *v4,\n"
" const b3Float4 *dir)\n"
"{\n"
" float dv1, dv2, dv3, dv4;\n"
" float dot1, dot2, dot3;\n"
" // find the smallest dot product of dir and {v1-v4, v2-v4, v3-v4}\n"
" dv1 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, dir);\n"
" dv2 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, dir);\n"
" dv3 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, dir);\n"
" dv4 = b3MprVec3Dot(&v4->v, dir);\n"
" dot1 = dv4 - dv1;\n"
" dot2 = dv4 - dv2;\n"
" dot3 = dv4 - dv3;\n"
" dot1 = B3_MPR_FMIN(dot1, dot2);\n"
" dot1 = B3_MPR_FMIN(dot1, dot3);\n"
" return b3MprEq(dot1, B3_MPR_TOLERANCE) || dot1 < B3_MPR_TOLERANCE;\n"
"}\n"
"inline int portalCanEncapsuleOrigin(const b3MprSimplex_t *portal, \n"
" const b3MprSupport_t *v4,\n"
" const b3Float4 *dir)\n"
"{\n"
" float dot;\n"
" dot = b3MprVec3Dot(&v4->v, dir);\n"
" return b3MprIsZero(dot) || dot > 0.f;\n"
"}\n"
"inline void b3ExpandPortal(b3MprSimplex_t *portal,\n"
" const b3MprSupport_t *v4)\n"
"{\n"
" float dot;\n"
" b3Float4 v4v0;\n"
" b3MprVec3Cross(&v4v0, &v4->v, &b3MprSimplexPoint(portal, 0)->v);\n"
" dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &v4v0);\n"
" if (dot > 0.f){\n"
" dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &v4v0);\n"
" if (dot > 0.f){\n"
" b3MprSimplexSet(portal, 1, v4);\n"
" }else{\n"
" b3MprSimplexSet(portal, 3, v4);\n"
" }\n"
" }else{\n"
" dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &v4v0);\n"
" if (dot > 0.f){\n"
" b3MprSimplexSet(portal, 2, v4);\n"
" }else{\n"
" b3MprSimplexSet(portal, 1, v4);\n"
" }\n"
" }\n"
"}\n"
"B3_STATIC int b3DiscoverPortal(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
" b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
" b3ConstArray(b3Collidable_t) cpuCollidables,\n"
" b3ConstArray(b3Float4) cpuVertices,\n"
" __global b3Float4* sepAxis,\n"
" __global int* hasSepAxis,\n"
" b3MprSimplex_t *portal)\n"
"{\n"
" b3Float4 dir, va, vb;\n"
" float dot;\n"
" int cont;\n"
" \n"
" \n"
" // vertex 0 is center of portal\n"
" b3FindOrigin(bodyIndexA,bodyIndexB,cpuBodyBuf, b3MprSimplexPointW(portal, 0));\n"
" // vertex 0 is center of portal\n"
" b3MprSimplexSetSize(portal, 1);\n"
" \n"
" b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
" b3Float4* b3mpr_vec3_origin = &zero;\n"
" if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 0)->v, b3mpr_vec3_origin)){\n"
" // Portal's center lies on origin (0,0,0) => we know that objects\n"
" // intersect but we would need to know penetration info.\n"
" // So move center little bit...\n"
" b3MprVec3Set(&va, FLT_EPSILON * 10.f, 0.f, 0.f);\n"
" b3MprVec3Add(&b3MprSimplexPointW(portal, 0)->v, &va);\n"
" }\n"
" // vertex 1 = support in direction of origin\n"
" b3MprVec3Copy(&dir, &b3MprSimplexPoint(portal, 0)->v);\n"
" b3MprVec3Scale(&dir, -1.f);\n"
" b3MprVec3Normalize(&dir);\n"
" b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 1));\n"
" b3MprSimplexSetSize(portal, 2);\n"
" // test if origin isn't outside of v1\n"
" dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &dir);\n"
" \n"
" if (b3MprIsZero(dot) || dot < 0.f)\n"
" return -1;\n"
" // vertex 2\n"
" b3MprVec3Cross(&dir, &b3MprSimplexPoint(portal, 0)->v,\n"
" &b3MprSimplexPoint(portal, 1)->v);\n"
" if (b3MprIsZero(b3MprVec3Len2(&dir))){\n"
" if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 1)->v, b3mpr_vec3_origin)){\n"
" // origin lies on v1\n"
" return 1;\n"
" }else{\n"
" // origin lies on v0-v1 segment\n"
" return 2;\n"
" }\n"
" }\n"
" b3MprVec3Normalize(&dir);\n"
" b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 2));\n"
" \n"
" dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &dir);\n"
" if (b3MprIsZero(dot) || dot < 0.f)\n"
" return -1;\n"
" b3MprSimplexSetSize(portal, 3);\n"
" // vertex 3 direction\n"
" b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,\n"
" &b3MprSimplexPoint(portal, 0)->v);\n"
" b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,\n"
" &b3MprSimplexPoint(portal, 0)->v);\n"
" b3MprVec3Cross(&dir, &va, &vb);\n"
" b3MprVec3Normalize(&dir);\n"
" // it is better to form portal faces to be oriented \"outside\" origin\n"
" dot = b3MprVec3Dot(&dir, &b3MprSimplexPoint(portal, 0)->v);\n"
" if (dot > 0.f){\n"
" b3MprSimplexSwap(portal, 1, 2);\n"
" b3MprVec3Scale(&dir, -1.f);\n"
" }\n"
" while (b3MprSimplexSize(portal) < 4){\n"
" b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 3));\n"
" \n"
" dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &dir);\n"
" if (b3MprIsZero(dot) || dot < 0.f)\n"
" return -1;\n"
" cont = 0;\n"
" // test if origin is outside (v1, v0, v3) - set v2 as v3 and\n"
" // continue\n"
" b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 1)->v,\n"
" &b3MprSimplexPoint(portal, 3)->v);\n"
" dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);\n"
" if (dot < 0.f && !b3MprIsZero(dot)){\n"
" b3MprSimplexSet(portal, 2, b3MprSimplexPoint(portal, 3));\n"
" cont = 1;\n"
" }\n"
" if (!cont){\n"
" // test if origin is outside (v3, v0, v2) - set v1 as v3 and\n"
" // continue\n"
" b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 3)->v,\n"
" &b3MprSimplexPoint(portal, 2)->v);\n"
" dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);\n"
" if (dot < 0.f && !b3MprIsZero(dot)){\n"
" b3MprSimplexSet(portal, 1, b3MprSimplexPoint(portal, 3));\n"
" cont = 1;\n"
" }\n"
" }\n"
" if (cont){\n"
" b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,\n"
" &b3MprSimplexPoint(portal, 0)->v);\n"
" b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,\n"
" &b3MprSimplexPoint(portal, 0)->v);\n"
" b3MprVec3Cross(&dir, &va, &vb);\n"
" b3MprVec3Normalize(&dir);\n"
" }else{\n"
" b3MprSimplexSetSize(portal, 4);\n"
" }\n"
" }\n"
" return 0;\n"
"}\n"
"B3_STATIC int b3RefinePortal(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
" b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
" b3ConstArray(b3Collidable_t) cpuCollidables,\n"
" b3ConstArray(b3Float4) cpuVertices,\n"
" __global b3Float4* sepAxis,\n"
" b3MprSimplex_t *portal)\n"
"{\n"
" b3Float4 dir;\n"
" b3MprSupport_t v4;\n"
" for (int i=0;i<B3_MPR_MAX_ITERATIONS;i++)\n"
" //while (1)\n"
" {\n"
" // compute direction outside the portal (from v0 throught v1,v2,v3\n"
" // face)\n"
" b3PortalDir(portal, &dir);\n"
" // test if origin is inside the portal\n"
" if (portalEncapsulesOrigin(portal, &dir))\n"
" return 0;\n"
" // get next support point\n"
" \n"
" b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, &v4);\n"
" // test if v4 can expand portal to contain origin and if portal\n"
" // expanding doesn't reach given tolerance\n"
" if (!portalCanEncapsuleOrigin(portal, &v4, &dir)\n"
" || portalReachTolerance(portal, &v4, &dir))\n"
" {\n"
" return -1;\n"
" }\n"
" // v1-v2-v3 triangle must be rearranged to face outside Minkowski\n"
" // difference (direction from v0).\n"
" b3ExpandPortal(portal, &v4);\n"
" }\n"
" return -1;\n"
"}\n"
"B3_STATIC void b3FindPos(const b3MprSimplex_t *portal, b3Float4 *pos)\n"
"{\n"
" b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
" b3Float4* b3mpr_vec3_origin = &zero;\n"
" b3Float4 dir;\n"
" size_t i;\n"
" float b[4], sum, inv;\n"
" b3Float4 vec, p1, p2;\n"
" b3PortalDir(portal, &dir);\n"
" // use barycentric coordinates of tetrahedron to find origin\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,\n"
" &b3MprSimplexPoint(portal, 2)->v);\n"
" b[0] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,\n"
" &b3MprSimplexPoint(portal, 2)->v);\n"
" b[1] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 0)->v,\n"
" &b3MprSimplexPoint(portal, 1)->v);\n"
" b[2] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,\n"
" &b3MprSimplexPoint(portal, 1)->v);\n"
" b[3] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);\n"
" sum = b[0] + b[1] + b[2] + b[3];\n"
" if (b3MprIsZero(sum) || sum < 0.f){\n"
" b[0] = 0.f;\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,\n"
" &b3MprSimplexPoint(portal, 3)->v);\n"
" b[1] = b3MprVec3Dot(&vec, &dir);\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,\n"
" &b3MprSimplexPoint(portal, 1)->v);\n"
" b[2] = b3MprVec3Dot(&vec, &dir);\n"
" b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,\n"
" &b3MprSimplexPoint(portal, 2)->v);\n"
" b[3] = b3MprVec3Dot(&vec, &dir);\n"
" sum = b[1] + b[2] + b[3];\n"
" }\n"
" inv = 1.f / sum;\n"
" b3MprVec3Copy(&p1, b3mpr_vec3_origin);\n"
" b3MprVec3Copy(&p2, b3mpr_vec3_origin);\n"
" for (i = 0; i < 4; i++){\n"
" b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v1);\n"
" b3MprVec3Scale(&vec, b[i]);\n"
" b3MprVec3Add(&p1, &vec);\n"
" b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v2);\n"
" b3MprVec3Scale(&vec, b[i]);\n"
" b3MprVec3Add(&p2, &vec);\n"
" }\n"
" b3MprVec3Scale(&p1, inv);\n"
" b3MprVec3Scale(&p2, inv);\n"
" b3MprVec3Copy(pos, &p1);\n"
" b3MprVec3Add(pos, &p2);\n"
" b3MprVec3Scale(pos, 0.5);\n"
"}\n"
"inline float b3MprVec3Dist2(const b3Float4 *a, const b3Float4 *b)\n"
"{\n"
" b3Float4 ab;\n"
" b3MprVec3Sub2(&ab, a, b);\n"
" return b3MprVec3Len2(&ab);\n"
"}\n"
"inline float _b3MprVec3PointSegmentDist2(const b3Float4 *P,\n"
" const b3Float4 *x0,\n"
" const b3Float4 *b,\n"
" b3Float4 *witness)\n"
"{\n"
" // The computation comes from solving equation of segment:\n"
" // S(t) = x0 + t.d\n"
" // where - x0 is initial point of segment\n"
" // - d is direction of segment from x0 (|d| > 0)\n"
" // - t belongs to <0, 1> interval\n"
" // \n"
" // Than, distance from a segment to some point P can be expressed:\n"
" // D(t) = |x0 + t.d - P|^2\n"
" // which is distance from any point on segment. Minimization\n"
" // of this function brings distance from P to segment.\n"
" // Minimization of D(t) leads to simple quadratic equation that's\n"
" // solving is straightforward.\n"
" //\n"
" // Bonus of this method is witness point for free.\n"
" float dist, t;\n"
" b3Float4 d, a;\n"
" // direction of segment\n"
" b3MprVec3Sub2(&d, b, x0);\n"
" // precompute vector from P to x0\n"
" b3MprVec3Sub2(&a, x0, P);\n"
" t = -1.f * b3MprVec3Dot(&a, &d);\n"
" t /= b3MprVec3Len2(&d);\n"
" if (t < 0.f || b3MprIsZero(t)){\n"
" dist = b3MprVec3Dist2(x0, P);\n"
" if (witness)\n"
" b3MprVec3Copy(witness, x0);\n"
" }else if (t > 1.f || b3MprEq(t, 1.f)){\n"
" dist = b3MprVec3Dist2(b, P);\n"
" if (witness)\n"
" b3MprVec3Copy(witness, b);\n"
" }else{\n"
" if (witness){\n"
" b3MprVec3Copy(witness, &d);\n"
" b3MprVec3Scale(witness, t);\n"
" b3MprVec3Add(witness, x0);\n"
" dist = b3MprVec3Dist2(witness, P);\n"
" }else{\n"
" // recycling variables\n"
" b3MprVec3Scale(&d, t);\n"
" b3MprVec3Add(&d, &a);\n"
" dist = b3MprVec3Len2(&d);\n"
" }\n"
" }\n"
" return dist;\n"
"}\n"
"inline float b3MprVec3PointTriDist2(const b3Float4 *P,\n"
" const b3Float4 *x0, const b3Float4 *B,\n"
" const b3Float4 *C,\n"
" b3Float4 *witness)\n"
"{\n"
" // Computation comes from analytic expression for triangle (x0, B, C)\n"
" // T(s, t) = x0 + s.d1 + t.d2, where d1 = B - x0 and d2 = C - x0 and\n"
" // Then equation for distance is:\n"
" // D(s, t) = | T(s, t) - P |^2\n"
" // This leads to minimization of quadratic function of two variables.\n"
" // The solution from is taken only if s is between 0 and 1, t is\n"
" // between 0 and 1 and t + s < 1, otherwise distance from segment is\n"
" // computed.\n"
" b3Float4 d1, d2, a;\n"
" float u, v, w, p, q, r;\n"
" float s, t, dist, dist2;\n"
" b3Float4 witness2;\n"
" b3MprVec3Sub2(&d1, B, x0);\n"
" b3MprVec3Sub2(&d2, C, x0);\n"
" b3MprVec3Sub2(&a, x0, P);\n"
" u = b3MprVec3Dot(&a, &a);\n"
" v = b3MprVec3Dot(&d1, &d1);\n"
" w = b3MprVec3Dot(&d2, &d2);\n"
" p = b3MprVec3Dot(&a, &d1);\n"
" q = b3MprVec3Dot(&a, &d2);\n"
" r = b3MprVec3Dot(&d1, &d2);\n"
" s = (q * r - w * p) / (w * v - r * r);\n"
" t = (-s * r - q) / w;\n"
" if ((b3MprIsZero(s) || s > 0.f)\n"
" && (b3MprEq(s, 1.f) || s < 1.f)\n"
" && (b3MprIsZero(t) || t > 0.f)\n"
" && (b3MprEq(t, 1.f) || t < 1.f)\n"
" && (b3MprEq(t + s, 1.f) || t + s < 1.f)){\n"
" if (witness){\n"
" b3MprVec3Scale(&d1, s);\n"
" b3MprVec3Scale(&d2, t);\n"
" b3MprVec3Copy(witness, x0);\n"
" b3MprVec3Add(witness, &d1);\n"
" b3MprVec3Add(witness, &d2);\n"
" dist = b3MprVec3Dist2(witness, P);\n"
" }else{\n"
" dist = s * s * v;\n"
" dist += t * t * w;\n"
" dist += 2.f * s * t * r;\n"
" dist += 2.f * s * p;\n"
" dist += 2.f * t * q;\n"
" dist += u;\n"
" }\n"
" }else{\n"
" dist = _b3MprVec3PointSegmentDist2(P, x0, B, witness);\n"
" dist2 = _b3MprVec3PointSegmentDist2(P, x0, C, &witness2);\n"
" if (dist2 < dist){\n"
" dist = dist2;\n"
" if (witness)\n"
" b3MprVec3Copy(witness, &witness2);\n"
" }\n"
" dist2 = _b3MprVec3PointSegmentDist2(P, B, C, &witness2);\n"
" if (dist2 < dist){\n"
" dist = dist2;\n"
" if (witness)\n"
" b3MprVec3Copy(witness, &witness2);\n"
" }\n"
" }\n"
" return dist;\n"
"}\n"
"B3_STATIC void b3FindPenetr(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, \n"
" b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
" b3ConstArray(b3Collidable_t) cpuCollidables,\n"
" b3ConstArray(b3Float4) cpuVertices,\n"
" __global b3Float4* sepAxis,\n"
" b3MprSimplex_t *portal,\n"
" float *depth, b3Float4 *pdir, b3Float4 *pos)\n"
"{\n"
" b3Float4 dir;\n"
" b3MprSupport_t v4;\n"
" unsigned long iterations;\n"
" b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
" b3Float4* b3mpr_vec3_origin = &zero;\n"
" iterations = 1UL;\n"
" for (int i=0;i<B3_MPR_MAX_ITERATIONS;i++)\n"
" //while (1)\n"
" {\n"
" // compute portal direction and obtain next support point\n"
" b3PortalDir(portal, &dir);\n"
" \n"
" b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, &v4);\n"
" // reached tolerance -> find penetration info\n"
" if (portalReachTolerance(portal, &v4, &dir)\n"
" || iterations ==B3_MPR_MAX_ITERATIONS)\n"
" {\n"
" *depth = b3MprVec3PointTriDist2(b3mpr_vec3_origin,&b3MprSimplexPoint(portal, 1)->v,&b3MprSimplexPoint(portal, 2)->v,&b3MprSimplexPoint(portal, 3)->v,pdir);\n"
" *depth = B3_MPR_SQRT(*depth);\n"
" \n"
" if (b3MprIsZero((*pdir).x) && b3MprIsZero((*pdir).y) && b3MprIsZero((*pdir).z))\n"
" {\n"
" \n"
" *pdir = dir;\n"
" } \n"
" b3MprVec3Normalize(pdir);\n"
" \n"
" // barycentric coordinates:\n"
" b3FindPos(portal, pos);\n"
" return;\n"
" }\n"
" b3ExpandPortal(portal, &v4);\n"
" iterations++;\n"
" }\n"
"}\n"
"B3_STATIC void b3FindPenetrTouch(b3MprSimplex_t *portal,float *depth, b3Float4 *dir, b3Float4 *pos)\n"
"{\n"
" // Touching contact on portal's v1 - so depth is zero and direction\n"
" // is unimportant and pos can be guessed\n"
" *depth = 0.f;\n"
" b3Float4 zero = b3MakeFloat4(0,0,0,0);\n"
" b3Float4* b3mpr_vec3_origin = &zero;\n"
" b3MprVec3Copy(dir, b3mpr_vec3_origin);\n"
" b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);\n"
" b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);\n"
" b3MprVec3Scale(pos, 0.5);\n"
"}\n"
"B3_STATIC void b3FindPenetrSegment(b3MprSimplex_t *portal,\n"
" float *depth, b3Float4 *dir, b3Float4 *pos)\n"
"{\n"
" \n"
" // Origin lies on v0-v1 segment.\n"
" // Depth is distance to v1, direction also and position must be\n"
" // computed\n"
" b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);\n"
" b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);\n"
" b3MprVec3Scale(pos, 0.5f);\n"
" \n"
" b3MprVec3Copy(dir, &b3MprSimplexPoint(portal, 1)->v);\n"
" *depth = B3_MPR_SQRT(b3MprVec3Len2(dir));\n"
" b3MprVec3Normalize(dir);\n"
"}\n"
"inline int b3MprPenetration(int pairIndex, int bodyIndexA, int bodyIndexB,\n"
" b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,\n"
" b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData, \n"
" b3ConstArray(b3Collidable_t) cpuCollidables,\n"
" b3ConstArray(b3Float4) cpuVertices,\n"
" __global b3Float4* sepAxis,\n"
" __global int* hasSepAxis,\n"
" float *depthOut, b3Float4* dirOut, b3Float4* posOut)\n"
"{\n"
" \n"
" b3MprSimplex_t portal;\n"
" \n"
"// if (!hasSepAxis[pairIndex])\n"
" // return -1;\n"
" \n"
" hasSepAxis[pairIndex] = 0;\n"
" int res;\n"
" // Phase 1: Portal discovery\n"
" res = b3DiscoverPortal(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,hasSepAxis, &portal);\n"
" \n"
" \n"
" //sepAxis[pairIndex] = *pdir;//or -dir?\n"
" switch (res)\n"
" {\n"
" case 0:\n"
" {\n"
" // Phase 2: Portal refinement\n"
" \n"
" res = b3RefinePortal(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&portal);\n"
" if (res < 0)\n"
" return -1;\n"
" // Phase 3. Penetration info\n"
" b3FindPenetr(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&portal, depthOut, dirOut, posOut);\n"
" hasSepAxis[pairIndex] = 1;\n"
" sepAxis[pairIndex] = -*dirOut;\n"
" break;\n"
" }\n"
" case 1:\n"
" {\n"
" // Touching contact on portal's v1.\n"
" b3FindPenetrTouch(&portal, depthOut, dirOut, posOut);\n"
" break;\n"
" }\n"
" case 2:\n"
" {\n"
" \n"
" b3FindPenetrSegment( &portal, depthOut, dirOut, posOut);\n"
" break;\n"
" }\n"
" default:\n"
" {\n"
" hasSepAxis[pairIndex]=0;\n"
" //if (res < 0)\n"
" //{\n"
" // Origin isn't inside portal - no collision.\n"
" return -1;\n"
" //}\n"
" }\n"
" };\n"
" \n"
" return 0;\n"
"};\n"
"#endif //B3_MPR_PENETRATION_H\n"
"#ifndef B3_CONTACT4DATA_H\n"
"#define B3_CONTACT4DATA_H\n"
"#ifndef B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_FLOAT4_H\n"
"typedef struct b3Contact4Data b3Contact4Data_t;\n"
"struct b3Contact4Data\n"
"{\n"
" b3Float4 m_worldPosB[4];\n"
"// b3Float4 m_localPosA[4];\n"
"// b3Float4 m_localPosB[4];\n"
" b3Float4 m_worldNormalOnB; // w: m_nPoints\n"
" unsigned short m_restituitionCoeffCmp;\n"
" unsigned short m_frictionCoeffCmp;\n"
" int m_batchIdx;\n"
" int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr\n"
" int m_bodyBPtrAndSignBit;\n"
" int m_childIndexA;\n"
" int m_childIndexB;\n"
" int m_unused1;\n"
" int m_unused2;\n"
"};\n"
"inline int b3Contact4Data_getNumPoints(const struct b3Contact4Data* contact)\n"
"{\n"
" return (int)contact->m_worldNormalOnB.w;\n"
"};\n"
"inline void b3Contact4Data_setNumPoints(struct b3Contact4Data* contact, int numPoints)\n"
"{\n"
" contact->m_worldNormalOnB.w = (float)numPoints;\n"
"};\n"
"#endif //B3_CONTACT4DATA_H\n"
"#define AppendInc(x, out) out = atomic_inc(x)\n"
"#define GET_NPOINTS(x) (x).m_worldNormalOnB.w\n"
"#ifdef cl_ext_atomic_counters_32\n"
" #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
"#else\n"
" #define counter32_t volatile __global int*\n"
"#endif\n"
"__kernel void mprPenetrationKernel( __global int4* pairs,\n"
" __global const b3RigidBodyData_t* rigidBodies, \n"
" __global const b3Collidable_t* collidables,\n"
" __global const b3ConvexPolyhedronData_t* convexShapes, \n"
" __global const float4* vertices,\n"
" __global float4* separatingNormals,\n"
" __global int* hasSeparatingAxis,\n"
" __global struct b3Contact4Data* restrict globalContactsOut,\n"
" counter32_t nGlobalContactsOut,\n"
" int contactCapacity,\n"
" int numPairs)\n"
"{\n"
" int i = get_global_id(0);\n"
" int pairIndex = i;\n"
" if (i<numPairs)\n"
" {\n"
" int bodyIndexA = pairs[i].x;\n"
" int bodyIndexB = pairs[i].y;\n"
" int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
" int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
" \n"
" int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
" int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
" \n"
" \n"
" //once the broadphase avoids static-static pairs, we can remove this test\n"
" if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
" {\n"
" return;\n"
" }\n"
" \n"
" if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))\n"
" {\n"
" return;\n"
" }\n"
" float depthOut;\n"
" b3Float4 dirOut;\n"
" b3Float4 posOut;\n"
" int res = b3MprPenetration(pairIndex, bodyIndexA, bodyIndexB,rigidBodies,convexShapes,collidables,vertices,separatingNormals,hasSeparatingAxis,&depthOut, &dirOut, &posOut);\n"
" \n"
" \n"
" \n"
" \n"
" if (res==0)\n"
" {\n"
" //add a contact\n"
" int dstIdx;\n"
" AppendInc( nGlobalContactsOut, dstIdx );\n"
" if (dstIdx<contactCapacity)\n"
" {\n"
" pairs[pairIndex].z = dstIdx;\n"
" __global struct b3Contact4Data* c = globalContactsOut + dstIdx;\n"
" c->m_worldNormalOnB = -dirOut;//normal;\n"
" c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);\n"
" c->m_batchIdx = pairIndex;\n"
" int bodyA = pairs[pairIndex].x;\n"
" int bodyB = pairs[pairIndex].y;\n"
" c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0 ? -bodyA:bodyA;\n"
" c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0 ? -bodyB:bodyB;\n"
" c->m_childIndexA = -1;\n"
" c->m_childIndexB = -1;\n"
" //for (int i=0;i<nContacts;i++)\n"
" posOut.w = -depthOut;\n"
" c->m_worldPosB[0] = posOut;//localPoints[contactIdx[i]];\n"
" GET_NPOINTS(*c) = 1;//nContacts;\n"
" }\n"
" }\n"
" }\n"
"}\n"
"typedef float4 Quaternion;\n"
"#define make_float4 (float4)\n"
"__inline\n"
"float dot3F4(float4 a, float4 b)\n"
"{\n"
" float4 a1 = make_float4(a.xyz,0.f);\n"
" float4 b1 = make_float4(b.xyz,0.f);\n"
" return dot(a1, b1);\n"
"}\n"
"__inline\n"
"float4 cross3(float4 a, float4 b)\n"
"{\n"
" return cross(a,b);\n"
"}\n"
"__inline\n"
"Quaternion qtMul(Quaternion a, Quaternion b)\n"
"{\n"
" Quaternion ans;\n"
" ans = cross3( a, b );\n"
" ans += a.w*b+b.w*a;\n"
"// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
" ans.w = a.w*b.w - dot3F4(a, b);\n"
" return ans;\n"
"}\n"
"__inline\n"
"Quaternion qtInvert(Quaternion q)\n"
"{\n"
" return (Quaternion)(-q.xyz, q.w);\n"
"}\n"
"__inline\n"
"float4 qtRotate(Quaternion q, float4 vec)\n"
"{\n"
" Quaternion qInv = qtInvert( q );\n"
" float4 vcpy = vec;\n"
" vcpy.w = 0.f;\n"
" float4 out = qtMul(qtMul(q,vcpy),qInv);\n"
" return out;\n"
"}\n"
"__inline\n"
"float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)\n"
"{\n"
" return qtRotate( *orientation, *p ) + (*translation);\n"
"}\n"
"__inline\n"
"float4 qtInvRotate(const Quaternion q, float4 vec)\n"
"{\n"
" return qtRotate( qtInvert( q ), vec );\n"
"}\n"
"inline void project(__global const b3ConvexPolyhedronData_t* hull, const float4 pos, const float4 orn, \n"
"const float4* dir, __global const float4* vertices, float* min, float* max)\n"
"{\n"
" min[0] = FLT_MAX;\n"
" max[0] = -FLT_MAX;\n"
" int numVerts = hull->m_numVertices;\n"
" const float4 localDir = qtInvRotate(orn,*dir);\n"
" float offset = dot(pos,*dir);\n"
" for(int i=0;i<numVerts;i++)\n"
" {\n"
" float dp = dot(vertices[hull->m_vertexOffset+i],localDir);\n"
" if(dp < min[0]) \n"
" min[0] = dp;\n"
" if(dp > max[0]) \n"
" max[0] = dp;\n"
" }\n"
" if(min[0]>max[0])\n"
" {\n"
" float tmp = min[0];\n"
" min[0] = max[0];\n"
" max[0] = tmp;\n"
" }\n"
" min[0] += offset;\n"
" max[0] += offset;\n"
"}\n"
"bool findSeparatingAxisUnitSphere( __global const b3ConvexPolyhedronData_t* hullA, __global const b3ConvexPolyhedronData_t* hullB, \n"
" const float4 posA1,\n"
" const float4 ornA,\n"
" const float4 posB1,\n"
" const float4 ornB,\n"
" const float4 DeltaC2,\n"
" __global const float4* vertices,\n"
" __global const float4* unitSphereDirections,\n"
" int numUnitSphereDirections,\n"
" float4* sep,\n"
" float* dmin)\n"
"{\n"
" \n"
" float4 posA = posA1;\n"
" posA.w = 0.f;\n"
" float4 posB = posB1;\n"
" posB.w = 0.f;\n"
" int curPlaneTests=0;\n"
" int curEdgeEdge = 0;\n"
" // Test unit sphere directions\n"
" for (int i=0;i<numUnitSphereDirections;i++)\n"
" {\n"
" float4 crossje;\n"
" crossje = unitSphereDirections[i]; \n"
" if (dot3F4(DeltaC2,crossje)>0)\n"
" crossje *= -1.f;\n"
" {\n"
" float dist;\n"
" bool result = true;\n"
" float Min0,Max0;\n"
" float Min1,Max1;\n"
" project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);\n"
" project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);\n"
" \n"
" if(Max0<Min1 || Max1<Min0)\n"
" return false;\n"
" \n"
" float d0 = Max0 - Min1;\n"
" float d1 = Max1 - Min0;\n"
" dist = d0<d1 ? d0:d1;\n"
" result = true;\n"
" \n"
" if(dist<*dmin)\n"
" {\n"
" *dmin = dist;\n"
" *sep = crossje;\n"
" }\n"
" }\n"
" }\n"
" \n"
" if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
" {\n"
" *sep = -(*sep);\n"
" }\n"
" return true;\n"
"}\n"
"__kernel void findSeparatingAxisUnitSphereKernel( __global const int4* pairs, \n"
" __global const b3RigidBodyData_t* rigidBodies, \n"
" __global const b3Collidable_t* collidables,\n"
" __global const b3ConvexPolyhedronData_t* convexShapes, \n"
" __global const float4* vertices,\n"
" __global const float4* unitSphereDirections,\n"
" __global float4* separatingNormals,\n"
" __global int* hasSeparatingAxis,\n"
" __global float* dmins,\n"
" int numUnitSphereDirections,\n"
" int numPairs\n"
" )\n"
"{\n"
" int i = get_global_id(0);\n"
" \n"
" if (i<numPairs)\n"
" {\n"
" if (hasSeparatingAxis[i])\n"
" {\n"
" \n"
" int bodyIndexA = pairs[i].x;\n"
" int bodyIndexB = pairs[i].y;\n"
" \n"
" int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
" int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
" \n"
" int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
" int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
" \n"
" \n"
" int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
" \n"
" float dmin = dmins[i];\n"
" \n"
" float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
" posA.w = 0.f;\n"
" float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
" posB.w = 0.f;\n"
" float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
" float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
" float4 c0 = transform(&c0local, &posA, &ornA);\n"
" float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
" float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
" float4 c1 = transform(&c1local,&posB,&ornB);\n"
" const float4 DeltaC2 = c0 - c1;\n"
" float4 sepNormal = separatingNormals[i];\n"
" \n"
" int numEdgeEdgeDirections = convexShapes[shapeIndexA].m_numUniqueEdges*convexShapes[shapeIndexB].m_numUniqueEdges;\n"
" if (numEdgeEdgeDirections>numUnitSphereDirections)\n"
" {\n"
" bool sepEE = findSeparatingAxisUnitSphere( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
" posB,ornB,\n"
" DeltaC2,\n"
" vertices,unitSphereDirections,numUnitSphereDirections,&sepNormal,&dmin);\n"
" if (!sepEE)\n"
" {\n"
" hasSeparatingAxis[i] = 0;\n"
" } else\n"
" {\n"
" hasSeparatingAxis[i] = 1;\n"
" separatingNormals[i] = sepNormal;\n"
" }\n"
" }\n"
" } //if (hasSeparatingAxis[i])\n"
" }//(i<numPairs)\n"
"}\n"
;
|