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
1447
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#include "GuConvexUtilsInternal.h"
#include "GuInternal.h"
#include "GuContactPolygonPolygon.h"
#include "GuConvexEdgeFlags.h"
#include "GuSeparatingAxes.h"
#include "GuContactMethodImpl.h"
#include "GuContactBuffer.h"
#include "GuMidphaseInterface.h"
#include "GuConvexHelper.h"
#include "GuTriangleCache.h"
#include "GuHeightFieldUtil.h"
#include "GuEntityReport.h"
#include "GuGeometryUnion.h"
#include "GuIntersectionTriangleBox.h"
#include "CmUtils.h"
#include "PsAllocator.h"
#include "GuBox.h"
#include "PsFPU.h"
using namespace physx;
using namespace Gu;
using namespace shdfnd::aos;
using namespace intrinsics;
#define LOCAL_TOUCHED_TRIG_SIZE 192
//#define USE_TRIANGLE_NORMAL
#define TEST_INTERNAL_OBJECTS
static PX_FORCE_INLINE void projectTriangle(const PxVec3& localSpaceDirection, const PxVec3* PX_RESTRICT triangle, PxReal& min1, PxReal& max1)
{
const PxReal dp0 = triangle[0].dot(localSpaceDirection);
const PxReal dp1 = triangle[1].dot(localSpaceDirection);
min1 = selectMin(dp0, dp1);
max1 = selectMax(dp0, dp1);
const PxReal dp2 = triangle[2].dot(localSpaceDirection);
min1 = selectMin(min1, dp2);
max1 = selectMax(max1, dp2);
}
#ifdef TEST_INTERNAL_OBJECTS
static PX_FORCE_INLINE void boxSupport(const float extents[3], const PxVec3& sv, float p[3])
{
const PxU32* iextents = reinterpret_cast<const PxU32*>(extents);
const PxU32* isv = reinterpret_cast<const PxU32*>(&sv);
PxU32* ip = reinterpret_cast<PxU32*>(p);
ip[0] = iextents[0]|(isv[0]&PX_SIGN_BITMASK);
ip[1] = iextents[1]|(isv[1]&PX_SIGN_BITMASK);
ip[2] = iextents[2]|(isv[2]&PX_SIGN_BITMASK);
}
#if PX_DEBUG
static const PxReal testInternalObjectsEpsilon = 1.0e-3f;
#endif
static PX_FORCE_INLINE bool testInternalObjects(const PxVec3& localAxis0,
const PolygonalData& polyData0,
const PxVec3* PX_RESTRICT triangleInHullSpace,
float dmin)
{
PxReal min1, max1;
projectTriangle(localAxis0, triangleInHullSpace, min1, max1);
const float dp = polyData0.mCenter.dot(localAxis0);
float p0[3];
boxSupport(polyData0.mInternal.mExtents, localAxis0, p0);
const float Radius0 = p0[0]*localAxis0.x + p0[1]*localAxis0.y + p0[2]*localAxis0.z;
const float bestRadius = selectMax(Radius0, polyData0.mInternal.mRadius);
const PxReal min0 = dp - bestRadius;
const PxReal max0 = dp + bestRadius;
const PxReal d0 = max0 - min1;
const PxReal d1 = max1 - min0;
const float depth = selectMin(d0, d1);
if(depth>dmin)
return false;
return true;
}
#endif
static PX_FORCE_INLINE bool testNormal( const PxVec3& sepAxis, PxReal min0, PxReal max0,
const PxVec3* PX_RESTRICT triangle,
PxReal& depth, PxReal contactDistance)
{
PxReal min1, max1;
projectTriangle(sepAxis, triangle, min1, max1);
if(max0+contactDistance<min1 || max1+contactDistance<min0)
return false;
const PxReal d0 = max0 - min1;
const PxReal d1 = max1 - min0;
depth = selectMin(d0, d1);
return true;
}
static PX_FORCE_INLINE bool testSepAxis(const PxVec3& sepAxis,
const PolygonalData& polyData0,
const PxVec3* PX_RESTRICT triangle,
const Cm::Matrix34& m0to1,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxReal& depth, PxReal contactDistance)
{
PxReal min0, max0;
(polyData0.mProjectHull)(polyData0, sepAxis, m0to1, convexScaling, min0, max0);
PxReal min1, max1;
projectTriangle(sepAxis, triangle, min1, max1);
if(max0+contactDistance < min1 || max1+contactDistance < min0)
return false;
const PxReal d0 = max0 - min1;
const PxReal d1 = max1 - min0;
depth = selectMin(d0, d1);
return true;
}
static bool testFacesSepAxesBackface( const PolygonalData& polyData0,
const Cm::Matrix34& /*world0*/, const Cm::Matrix34& /*world1*/,
const Cm::Matrix34& m0to1, const PxVec3& witness,
const PxVec3* PX_RESTRICT triangle,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxU32& numHullIndices,
PxU32* hullIndices_, PxReal& dmin, PxVec3& sep, PxU32& id, PxReal contactDistance,
bool idtConvexScale
)
{
id = PX_INVALID_U32;
const PxU32 numHullPolys = polyData0.mNbPolygons;
const HullPolygonData* PX_RESTRICT polygons = polyData0.mPolygons;
const PxVec3* PX_RESTRICT vertices = polyData0.mVerts;
const PxVec3& trans = m0to1.p;
{
PxU32* hullIndices = hullIndices_;
// PT: when the center of one object is inside the other object (deep penetrations) this discards everything!
// PT: when this happens, the backup procedure is used to come up with good results anyway.
// PT: it's worth having a special codepath here for identity scales, to skip all the normalizes/division. Lot faster without.
if(idtConvexScale)
{
for(PxU32 i=0; i<numHullPolys; i++)
{
const HullPolygonData& P = polygons[i];
const PxPlane& PL = P.mPlane;
#ifdef USE_TRIANGLE_NORMAL
if(PL.normal.dot(witness) > 0.0f)
#else
// ### this is dubious since the triangle center is likely to be very close to the hull, if not inside. Why not use the triangle normal?
if(PL.distance(witness) < 0.0f)
#endif
continue; //backface culled
*hullIndices++ = i;
const PxVec3 sepAxis = m0to1.rotate(PL.n);
const PxReal dp = sepAxis.dot(trans);
PxReal d;
if(!testNormal(sepAxis, P.getMin(vertices) + dp, P.getMax() + dp, triangle,
d, contactDistance))
return false;
if(d < dmin)
{
dmin = d;
sep = sepAxis;
id = i;
}
}
}
else
{
#ifndef USE_TRIANGLE_NORMAL
//transform delta from hull0 shape into vertex space:
const PxVec3 vertSpaceWitness = convexScaling % witness;
#endif
for(PxU32 i=0; i<numHullPolys; i++)
{
const HullPolygonData& P = polygons[i];
const PxPlane& PL = P.mPlane;
#ifdef USE_TRIANGLE_NORMAL
if(PL.normal.dot(witness) > 0.0f)
#else
// ### this is dubious since the triangle center is likely to be very close to the hull, if not inside. Why not use the triangle normal?
if(PL.distance(vertSpaceWitness) < 0.0f)
#endif
continue; //backface culled
//normals transform by inverse transpose: (and transpose(skew) == skew as its symmetric)
PxVec3 shapeSpaceNormal = convexScaling % PL.n;
//renormalize: (Arr!)
const PxReal magnitude = shapeSpaceNormal.normalize();
*hullIndices++ = i;
const PxVec3 sepAxis = m0to1.rotate(shapeSpaceNormal);
PxReal d;
const PxReal dp = sepAxis.dot(trans);
const float oneOverM = 1.0f / magnitude;
if(!testNormal(sepAxis, P.getMin(vertices) * oneOverM + dp, P.getMax() * oneOverM + dp, triangle,
d, contactDistance))
return false;
if(d < dmin)
{
dmin = d;
sep = sepAxis;
id = i;
}
}
}
numHullIndices = PxU32(hullIndices - hullIndices_);
}
// Backup
if(id == PX_INVALID_U32)
{
if(idtConvexScale)
{
for(PxU32 i=0; i<numHullPolys; i++)
{
const HullPolygonData& P = polygons[i];
const PxPlane& PL = P.mPlane;
const PxVec3 sepAxis = m0to1.rotate(PL.n);
const PxReal dp = sepAxis.dot(trans);
PxReal d;
if(!testNormal(sepAxis, P.getMin(vertices) + dp, P.getMax() + dp, triangle,
d, contactDistance))
return false;
if(d < dmin)
{
dmin = d;
sep = sepAxis;
id = i;
}
hullIndices_[i] = i;
}
}
else
{
for(PxU32 i=0; i<numHullPolys; i++)
{
const HullPolygonData& P = polygons[i];
const PxPlane& PL = P.mPlane;
PxVec3 shapeSpaceNormal = convexScaling % PL.n;
//renormalize: (Arr!)
const PxReal magnitude = shapeSpaceNormal.normalize();
const PxVec3 sepAxis = m0to1.rotate(shapeSpaceNormal);
PxReal d;
const PxReal dp = sepAxis.dot(trans);
const float oneOverM = 1.0f / magnitude;
if(!testNormal(sepAxis, P.getMin(vertices) * oneOverM + dp, P.getMax() * oneOverM + dp, triangle,
d, contactDistance))
return false;
if(d < dmin)
{
dmin = d;
sep = sepAxis;
id = i;
}
hullIndices_[i] = i;
}
}
numHullIndices = numHullPolys;
}
return true;
}
static PX_FORCE_INLINE bool edgeCulling(const PxPlane& plane, const PxVec3& p0, const PxVec3& p1, PxReal contactDistance)
{
return plane.distance(p0)<=contactDistance || plane.distance(p1)<=contactDistance;
}
static bool performEETests(
const PolygonalData& polyData0,
const PxU8 triFlags,
const Cm::Matrix34& m0to1, const Cm::Matrix34& m1to0,
const PxVec3* PX_RESTRICT triangle,
PxU32 numHullIndices, const PxU32* PX_RESTRICT hullIndices,
const PxPlane& localTriPlane,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxVec3& vec, PxReal& dmin, PxReal contactDistance, PxReal toleranceLength,
PxU32 id0, PxU32 /*triangleIndex*/)
{
PX_UNUSED(toleranceLength); // Only used in Debug
// Cull candidate triangle edges vs to hull plane
PxU32 nbTriangleAxes = 0;
PxVec3 triangleAxes[3];
{
const HullPolygonData& P = polyData0.mPolygons[id0];
const PxPlane& vertSpacePlane = P.mPlane;
const PxVec3 newN = m1to0.rotate(vertSpacePlane.n);
PxPlane hullWitness(convexScaling * newN, vertSpacePlane.d - m1to0.p.dot(newN)); //technically not a fully xformed plane, just use property of x|My == Mx|y for symmetric M.
if((triFlags & ETD_CONVEX_EDGE_01) && edgeCulling(hullWitness, triangle[0], triangle[1], contactDistance))
triangleAxes[nbTriangleAxes++] = (triangle[0] - triangle[1]);
if((triFlags & ETD_CONVEX_EDGE_12) && edgeCulling(hullWitness, triangle[1], triangle[2], contactDistance))
triangleAxes[nbTriangleAxes++] = (triangle[1] - triangle[2]);
if((triFlags & ETD_CONVEX_EDGE_20) && edgeCulling(hullWitness, triangle[2], triangle[0], contactDistance))
triangleAxes[nbTriangleAxes++] = (triangle[2] - triangle[0]);
}
//PxcPlane vertexSpacePlane = localTriPlane.getTransformed(m1to0);
//vertexSpacePlane.normal = convexScaling * vertexSpacePlane.normal; //technically not a fully xformed plane, just use property of x|My == Mx|y for symmetric M.
const PxVec3 newN = m1to0.rotate(localTriPlane.n);
PxPlane vertexSpacePlane(convexScaling * newN, localTriPlane.d - m1to0.p.dot(newN));
const PxVec3* PX_RESTRICT hullVerts = polyData0.mVerts;
SeparatingAxes SA;
SA.reset();
const PxU8* PX_RESTRICT vrefBase0 = polyData0.mPolygonVertexRefs;
const HullPolygonData* PX_RESTRICT polygons = polyData0.mPolygons;
while(numHullIndices--)
{
const HullPolygonData& P = polygons[*hullIndices++];
const PxU8* PX_RESTRICT data = vrefBase0 + P.mVRef8;
PxU32 numEdges = nbTriangleAxes;
const PxVec3* edges = triangleAxes;
// TODO: cheap edge culling as in convex/convex!
while(numEdges--)
{
const PxVec3& currentPolyEdge = *edges++;
// Loop through polygon vertices == polygon edges;
PxU32 numVerts = P.mNbVerts;
for(PxU32 j = 0; j < numVerts; j++)
{
PxU32 j1 = j+1;
if(j1>=numVerts) j1 = 0;
const PxU32 VRef0 = data[j];
const PxU32 VRef1 = data[j1];
if(edgeCulling(vertexSpacePlane, hullVerts[VRef0], hullVerts[VRef1], contactDistance))
{
const PxVec3 currentHullEdge = m0to1.rotate(convexScaling * (hullVerts[VRef0] - hullVerts[VRef1])); //matrix mult is distributive!
PxVec3 sepAxis = currentHullEdge.cross(currentPolyEdge);
if(!Ps::isAlmostZero(sepAxis))
SA.addAxis(sepAxis.getNormalized());
}
}
}
}
dmin = PX_MAX_REAL;
PxU32 numAxes = SA.getNumAxes();
const PxVec3* PX_RESTRICT axes = SA.getAxes();
#ifdef TEST_INTERNAL_OBJECTS
PxVec3 triangleInHullSpace[3];
if(numAxes)
{
triangleInHullSpace[0] = m1to0.transform(triangle[0]);
triangleInHullSpace[1] = m1to0.transform(triangle[1]);
triangleInHullSpace[2] = m1to0.transform(triangle[2]);
}
#endif
while(numAxes--)
{
const PxVec3& currentAxis = *axes++;
#ifdef TEST_INTERNAL_OBJECTS
const PxVec3 localAxis0 = m1to0.rotate(currentAxis);
if(!testInternalObjects(localAxis0, polyData0, triangleInHullSpace, dmin))
{
#if PX_DEBUG
PxReal dtest;
if(testSepAxis(currentAxis, polyData0, triangle, m0to1, convexScaling, dtest, contactDistance))
{
PX_ASSERT(dtest + testInternalObjectsEpsilon*toleranceLength >= dmin);
}
#endif
continue;
}
#endif
PxReal d;
if(!testSepAxis(currentAxis, polyData0, triangle,
m0to1, convexScaling, d, contactDistance))
{
return false;
}
if(d < dmin)
{
dmin = d;
vec = currentAxis;
}
}
return true;
}
static bool triangleConvexTest( const PolygonalData& polyData0,
const PxU8 triFlags,
PxU32 index, const PxVec3* PX_RESTRICT localPoints,
const PxPlane& localPlane,
const PxVec3& groupCenterHull,
const Cm::Matrix34& world0, const Cm::Matrix34& world1, const Cm::Matrix34& m0to1, const Cm::Matrix34& m1to0,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxReal contactDistance, PxReal toleranceLength,
PxVec3& groupAxis, PxReal& groupMinDepth, bool& faceContact,
bool idtConvexScale
)
{
PxU32 id0 = PX_INVALID_U32;
PxReal dmin0 = PX_MAX_REAL;
PxVec3 vec0;
PxU32 numHullIndices = 0;
PxU32* PX_RESTRICT const hullIndices = reinterpret_cast<PxU32*>(PxAlloca(polyData0.mNbPolygons*sizeof(PxU32)));
// PT: we test the hull normals first because they don't need any hull projection. If we can early exit thanks
// to those, we completely avoid all hull projections.
bool status = testFacesSepAxesBackface(polyData0, world0, world1, m0to1, groupCenterHull, localPoints,
convexScaling, numHullIndices, hullIndices, dmin0, vec0, id0, contactDistance, idtConvexScale);
if(!status)
return false;
groupAxis = PxVec3(0);
groupMinDepth = PX_MAX_REAL;
const PxReal eps = 0.0001f; // DE7748
//Test in mesh-space
PxVec3 sepAxis;
PxReal depth;
{
// Test triangle normal
PxReal d;
if(!testSepAxis(localPlane.n, polyData0, localPoints,
m0to1, convexScaling, d, contactDistance))
return false;
if(d<dmin0+eps)
// if(d<dmin0)
{
depth = d;
sepAxis = localPlane.n;
faceContact = true;
}
else
{
depth = dmin0;
sepAxis = vec0;
faceContact = false;
}
}
if(depth < groupMinDepth)
{
groupMinDepth = depth;
groupAxis = world1.rotate(sepAxis);
}
if(!performEETests(polyData0, triFlags, m0to1, m1to0,
localPoints,
numHullIndices, hullIndices, localPlane,
convexScaling, sepAxis, depth, contactDistance, toleranceLength,
id0, index))
return false;
if(depth < groupMinDepth)
{
groupMinDepth = depth;
groupAxis = world1.rotate(sepAxis);
faceContact = false;
}
return true;
}
namespace
{
struct ConvexMeshContactGeneration
{
Ps::InlineArray<PxU32,LOCAL_CONTACTS_SIZE>& mDelayedContacts;
Gu::CacheMap<Gu::CachedEdge, 128> mEdgeCache;
Gu::CacheMap<Gu::CachedVertex, 128> mVertCache;
const Cm::Matrix34 m0to1;
const Cm::Matrix34 m1to0;
PxVec3 mHullCenterMesh;
PxVec3 mHullCenterWorld;
const PolygonalData& mPolyData0;
const Cm::Matrix34& mWorld0;
const Cm::Matrix34& mWorld1;
const Cm::FastVertex2ShapeScaling& mConvexScaling;
PxReal mContactDistance;
PxReal mToleranceLength;
bool mIdtMeshScale, mIdtConvexScale;
PxReal mCCDEpsilon;
const PxTransform& mTransform0;
const PxTransform& mTransform1;
ContactBuffer& mContactBuffer;
bool mAnyHits;
ConvexMeshContactGeneration(
Ps::InlineArray<PxU32,LOCAL_CONTACTS_SIZE>& delayedContacts,
const PxTransform& t0to1, const PxTransform& t1to0,
const PolygonalData& polyData0, const Cm::Matrix34& world0, const Cm::Matrix34& world1,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxReal contactDistance,
PxReal toleranceLength,
bool idtConvexScale,
PxReal cCCDEpsilon,
const PxTransform& transform0, const PxTransform& transform1,
ContactBuffer& contactBuffer
);
void processTriangle(const PxVec3* verts, PxU32 triangleIndex, PxU8 triFlags, const PxU32* vertInds);
void generateLastContacts();
bool generateContacts(
const PxPlane& localPlane,
const PxVec3* PX_RESTRICT localPoints,
const PxVec3& triCenter, PxVec3& groupAxis,
PxReal groupMinDepth, PxU32 index) const;
private:
ConvexMeshContactGeneration& operator=(const ConvexMeshContactGeneration&);
};
// 17 entries. 1088/17 = 64 triangles in the local array
struct SavedContactData
{
PxU32 mTriangleIndex;
PxVec3 mVerts[3];
PxU32 mInds[3];
PxVec3 mGroupAxis;
PxReal mGroupMinDepth;
};
}
ConvexMeshContactGeneration::ConvexMeshContactGeneration(
Ps::InlineArray<PxU32,LOCAL_CONTACTS_SIZE>& delayedContacts,
const PxTransform& t0to1, const PxTransform& t1to0,
const PolygonalData& polyData0, const Cm::Matrix34& world0, const Cm::Matrix34& world1,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxReal contactDistance,
PxReal toleranceLength,
bool idtConvexScale,
PxReal cCCDEpsilon,
const PxTransform& transform0, const PxTransform& transform1,
ContactBuffer& contactBuffer
) :
mDelayedContacts(delayedContacts),
m0to1 (t0to1),
m1to0 (t1to0),
mPolyData0 (polyData0),
mWorld0 (world0),
mWorld1 (world1),
mConvexScaling (convexScaling),
mContactDistance(contactDistance),
mToleranceLength(toleranceLength),
mIdtConvexScale (idtConvexScale),
mCCDEpsilon (cCCDEpsilon),
mTransform0 (transform0),
mTransform1 (transform1),
mContactBuffer (contactBuffer)
{
delayedContacts.forceSize_Unsafe(0);
mAnyHits = false;
// Hull center in local space
const PxVec3& hullCenterLocal = mPolyData0.mCenter;
// Hull center in mesh space
mHullCenterMesh = m0to1.transform(hullCenterLocal);
// Hull center in world space
mHullCenterWorld = mWorld0.transform(hullCenterLocal);
}
struct ConvexMeshContactGenerationCallback : MeshHitCallback<PxRaycastHit>
{
ConvexMeshContactGeneration mGeneration;
const Cm::FastVertex2ShapeScaling& mMeshScaling;
const PxU8* PX_RESTRICT mExtraTrigData;
bool mIdtMeshScale;
const TriangleMesh* mMeshData;
const BoxPadded& mBox;
ConvexMeshContactGenerationCallback(
Ps::InlineArray<PxU32,LOCAL_CONTACTS_SIZE>& delayedContacts,
const PxTransform& t0to1, const PxTransform& t1to0,
const PolygonalData& polyData0, const Cm::Matrix34& world0, const Cm::Matrix34& world1,
const TriangleMesh* meshData,
const PxU8* PX_RESTRICT extraTrigData,
const Cm::FastVertex2ShapeScaling& meshScaling,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxReal contactDistance,
PxReal toleranceLength,
bool idtMeshScale, bool idtConvexScale,
PxReal cCCDEpsilon,
const PxTransform& transform0, const PxTransform& transform1,
ContactBuffer& contactBuffer,
const BoxPadded& box
) :
MeshHitCallback<PxRaycastHit> (CallbackMode::eMULTIPLE),
mGeneration (delayedContacts, t0to1, t1to0, polyData0, world0, world1, convexScaling, contactDistance, toleranceLength, idtConvexScale, cCCDEpsilon, transform0, transform1, contactBuffer),
mMeshScaling (meshScaling),
mExtraTrigData (extraTrigData),
mIdtMeshScale (idtMeshScale),
mMeshData (meshData),
mBox (box)
{
}
virtual PxAgain processHit( // all reported coords are in mesh local space including hit.position
const PxRaycastHit& hit, const PxVec3& v0, const PxVec3& v1, const PxVec3& v2, PxReal&, const PxU32* vinds)
{
// PT: this one is safe because incoming vertices from midphase are always safe to V4Load (by design)
// PT: TODO: is this test really needed? Not done in midphase already?
if(!intersectTriangleBox(mBox, v0, v1, v2))
return true;
PxVec3 verts[3];
getScaledVertices(verts, v0, v1, v2, mIdtMeshScale, mMeshScaling);
const PxU32 triangleIndex = hit.faceIndex;
const PxU8 extraData = getConvexEdgeFlags(mExtraTrigData, triangleIndex);
mGeneration.processTriangle(verts, triangleIndex, extraData, vinds);
return true;
}
protected:
ConvexMeshContactGenerationCallback &operator=(const ConvexMeshContactGenerationCallback &);
};
bool ConvexMeshContactGeneration::generateContacts(
const PxPlane& localPlane,
const PxVec3* PX_RESTRICT localPoints,
const PxVec3& triCenter, PxVec3& groupAxis,
PxReal groupMinDepth, PxU32 index) const
{
const PxVec3 worldGroupCenter = mWorld1.transform(triCenter);
const PxVec3 deltaC = mHullCenterWorld - worldGroupCenter;
if(deltaC.dot(groupAxis) < 0.0f)
groupAxis = -groupAxis;
const PxU32 id = (mPolyData0.mSelectClosestEdgeCB)(mPolyData0, mConvexScaling, mWorld0.rotateTranspose(-groupAxis));
const HullPolygonData& HP = mPolyData0.mPolygons[id];
PX_ALIGN(16, PxPlane) shapeSpacePlane0;
if(mIdtConvexScale)
V4StoreA(V4LoadU(&HP.mPlane.n.x), &shapeSpacePlane0.n.x);
else
mConvexScaling.transformPlaneToShapeSpace(HP.mPlane.n, HP.mPlane.d, shapeSpacePlane0.n, shapeSpacePlane0.d);
const PxVec3 hullNormalWorld = mWorld0.rotate(shapeSpacePlane0.n);
const PxReal d0 = PxAbs(hullNormalWorld.dot(groupAxis));
const PxVec3 triNormalWorld = mWorld1.rotate(localPlane.n);
const PxReal d1 = PxAbs(triNormalWorld.dot(groupAxis));
const bool d0biggerd1 = d0 > d1;
////////////////////NEW DIST HANDLING//////////////////////
//TODO: skip this if there is no dist involved!
PxReal separation = - groupMinDepth; //convert to real distance.
separation = fsel(separation, separation, 0.0f); //don't do anything when penetrating!
//printf("\nseparation = %f", separation);
PxReal contactGenPositionShift = separation + mCCDEpsilon; //if we're at a distance, shift so we're within penetration.
PxVec3 contactGenPositionShiftVec = groupAxis * contactGenPositionShift; //shift one of the bodies this distance toward the other just for Pierre's contact generation. Then the bodies should be penetrating exactly by MIN_SEPARATION_FOR_PENALTY - ideal conditions for this contact generator.
//note: for some reason this has to change sign!
//this will make contact gen always generate contacts at about MSP. Shift them back to the true real distance, and then to a solver compliant distance given that
//the solver converges to MSP penetration, while we want it to converge to 0 penetration.
//to real distance:
// PxReal polyPolySeparationShift = separation; //(+ or - depending on which way normal goes)
//The system: We always shift convex 0 (arbitrary). If the contact is attached to convex 0 then we will need to shift the contact point, otherwise not.
//TODO: make these overwrite orig location if its safe to do so.
Cm::Matrix34 world0_(mWorld0);
PxTransform transform0_(mTransform0);
world0_.p -= contactGenPositionShiftVec;
transform0_.p = world0_.p; //reset this too.
PxTransform t0to1_ = mTransform1.transformInv(transform0_);
PxTransform t1to0_ = transform0_.transformInv(mTransform1);
Cm::Matrix34 m0to1_(t0to1_);
Cm::Matrix34 m1to0_(t1to0_);
PxVec3* scaledVertices0;
PxU8* stackIndices0;
GET_SCALEX_CONVEX(scaledVertices0, stackIndices0, mIdtConvexScale, HP.mNbVerts, mConvexScaling, mPolyData0.mVerts, mPolyData0.getPolygonVertexRefs(HP))
const PxU8 indices[3] = {0, 1, 2};
const PxMat33 RotT0 = findRotationMatrixFromZ(shapeSpacePlane0.n);
const PxMat33 RotT1 = findRotationMatrixFromZ(localPlane.n);
if(d0biggerd1)
{
if(contactPolygonPolygonExt(
HP.mNbVerts, scaledVertices0, stackIndices0, world0_, shapeSpacePlane0,
RotT0,
3, localPoints, indices, mWorld1, localPlane,
RotT1,
hullNormalWorld, m0to1_, m1to0_, PXC_CONTACT_NO_FACE_INDEX, index,
mContactBuffer,
true,
contactGenPositionShiftVec, contactGenPositionShift))
{
return true;
}
}
else
{
if(contactPolygonPolygonExt(
3, localPoints, indices, mWorld1, localPlane,
RotT1,
HP.mNbVerts, scaledVertices0, stackIndices0, world0_, shapeSpacePlane0,
RotT0,
triNormalWorld, m1to0_, m0to1_, PXC_CONTACT_NO_FACE_INDEX, index,
mContactBuffer,
false,
contactGenPositionShiftVec, contactGenPositionShift))
{
return true;
}
}
return false;
}
enum FeatureCode
{
FC_VERTEX0,
FC_VERTEX1,
FC_VERTEX2,
FC_EDGE01,
FC_EDGE12,
FC_EDGE20,
FC_FACE,
FC_UNDEFINED
};
static FeatureCode computeFeatureCode(const PxVec3& point, const PxVec3* verts)
{
const PxVec3& triangleOrigin = verts[0];
const PxVec3 triangleEdge0 = verts[1] - verts[0];
const PxVec3 triangleEdge1 = verts[2] - verts[0];
const PxVec3 kDiff = triangleOrigin - point;
const PxReal fA00 = triangleEdge0.magnitudeSquared();
const PxReal fA01 = triangleEdge0.dot(triangleEdge1);
const PxReal fA11 = triangleEdge1.magnitudeSquared();
const PxReal fB0 = kDiff.dot(triangleEdge0);
const PxReal fB1 = kDiff.dot(triangleEdge1);
const PxReal fDet = PxAbs(fA00*fA11 - fA01*fA01);
const PxReal u = fA01*fB1-fA11*fB0;
const PxReal v = fA01*fB0-fA00*fB1;
FeatureCode fc = FC_UNDEFINED;
if(u + v <= fDet)
{
if(u < 0.0f)
{
if(v < 0.0f) // region 4
{
if(fB0 < 0.0f)
{
if(-fB0 >= fA00)
fc = FC_VERTEX1;
else
fc = FC_EDGE01;
}
else
{
if(fB1 >= 0.0f)
fc = FC_VERTEX0;
else if(-fB1 >= fA11)
fc = FC_VERTEX2;
else
fc = FC_EDGE20;
}
}
else // region 3
{
if(fB1 >= 0.0f)
fc = FC_VERTEX0;
else if(-fB1 >= fA11)
fc = FC_VERTEX2;
else
fc = FC_EDGE20;
}
}
else if(v < 0.0f) // region 5
{
if(fB0 >= 0.0f)
fc = FC_VERTEX0;
else if(-fB0 >= fA00)
fc = FC_VERTEX1;
else
fc = FC_EDGE01;
}
else // region 0
{
// minimum at interior PxVec3
if(fDet==0.0f)
fc = FC_VERTEX0;
else
fc = FC_FACE;
}
}
else
{
PxReal fTmp0, fTmp1, fNumer, fDenom;
if(u < 0.0f) // region 2
{
fTmp0 = fA01 + fB0;
fTmp1 = fA11 + fB1;
if(fTmp1 > fTmp0)
{
fNumer = fTmp1 - fTmp0;
fDenom = fA00-2.0f*fA01+fA11;
if(fNumer >= fDenom)
fc = FC_VERTEX1;
else
fc = FC_EDGE12;
}
else
{
if(fTmp1 <= 0.0f)
fc = FC_VERTEX2;
else if(fB1 >= 0.0f)
fc = FC_VERTEX0;
else
fc = FC_EDGE20;
}
}
else if(v < 0.0f) // region 6
{
fTmp0 = fA01 + fB1;
fTmp1 = fA00 + fB0;
if(fTmp1 > fTmp0)
{
fNumer = fTmp1 - fTmp0;
fDenom = fA00-2.0f*fA01+fA11;
if(fNumer >= fDenom)
fc = FC_VERTEX2;
else
fc = FC_EDGE12;
}
else
{
if(fTmp1 <= 0.0f)
fc = FC_VERTEX1;
else if(fB0 >= 0.0f)
fc = FC_VERTEX0;
else
fc = FC_EDGE01;
}
}
else // region 1
{
fNumer = fA11 + fB1 - fA01 - fB0;
if(fNumer <= 0.0f)
{
fc = FC_VERTEX2;
}
else
{
fDenom = fA00-2.0f*fA01+fA11;
if(fNumer >= fDenom)
fc = FC_VERTEX1;
else
fc = FC_EDGE12;
}
}
}
return fc;
}
//static bool validateVertex(PxU32 vref, const PxU32 count, const ContactPoint* PX_RESTRICT contacts, const TriangleMesh& meshData)
//{
// PxU32 previous = 0xffffffff;
// for(PxU32 i=0;i<count;i++)
// {
// if(contacts[i].internalFaceIndex1==previous)
// continue;
// previous = contacts[i].internalFaceIndex1;
//
// const TriangleIndices T(meshData, contacts[i].internalFaceIndex1);
// if( T.mVRefs[0]==vref
// || T.mVRefs[1]==vref
// || T.mVRefs[2]==vref)
// return false;
// }
// return true;
//}
//static PX_FORCE_INLINE bool testEdge(PxU32 vref0, PxU32 vref1, PxU32 tvref0, PxU32 tvref1)
//{
// if(tvref0>tvref1)
// Ps::swap(tvref0, tvref1);
//
// if(tvref0==vref0 && tvref1==vref1)
// return false;
// return true;
//}
//static bool validateEdge(PxU32 vref0, PxU32 vref1, const PxU32 count, const ContactPoint* PX_RESTRICT contacts, const TriangleMesh& meshData)
//{
// if(vref0>vref1)
// Ps::swap(vref0, vref1);
//
// PxU32 previous = 0xffffffff;
// for(PxU32 i=0;i<count;i++)
// {
// if(contacts[i].internalFaceIndex1==previous)
// continue;
// previous = contacts[i].internalFaceIndex1;
//
// const TriangleIndices T(meshData, contacts[i].internalFaceIndex1);
//
///* if(T.mVRefs[0]==vref0 || T.mVRefs[0]==vref1)
// return false;
// if(T.mVRefs[1]==vref0 || T.mVRefs[1]==vref1)
// return false;
// if(T.mVRefs[2]==vref0 || T.mVRefs[2]==vref1)
// return false;*/
// // PT: wow, this was wrong??? ###FIX
// if(!testEdge(vref0, vref1, T.mVRefs[0], T.mVRefs[1]))
// return false;
// if(!testEdge(vref0, vref1, T.mVRefs[1], T.mVRefs[2]))
// return false;
// if(!testEdge(vref0, vref1, T.mVRefs[2], T.mVRefs[0]))
// return false;
// }
// return true;
//}
//static bool validateEdge(PxU32 vref0, PxU32 vref1, const PxU32* vertIndices, const PxU32 nbIndices)
//{
// if(vref0>vref1)
// Ps::swap(vref0, vref1);
//
// for(PxU32 i=0;i<nbIndices;i+=3)
// {
// if(!testEdge(vref0, vref1, vertIndices[i+0], vertIndices[i+1]))
// return false;
// if(!testEdge(vref0, vref1, vertIndices[i+1], vertIndices[i+2]))
// return false;
// if(!testEdge(vref0, vref1, vertIndices[i+2], vertIndices[i+0]))
// return false;
// }
// return true;
//}
//#endif
void ConvexMeshContactGeneration::processTriangle(const PxVec3* verts, PxU32 triangleIndex, PxU8 triFlags, const PxU32* vertInds)
{
const PxPlane localPlane(verts[0], verts[1], verts[2]);
// Backface culling
if(localPlane.distance(mHullCenterMesh)<0.0f)
// if(localPlane.normal.dot(mHullCenterMesh - T.mVerts[0]) <= 0.0f)
return;
//////////////////////////////////////////////////////////////////////////
const PxVec3 triCenter = (verts[0] + verts[1] + verts[2])*(1.0f/3.0f);
// Group center in hull space
#ifdef USE_TRIANGLE_NORMAL
const PxVec3 groupCenterHull = m1to0.rotate(localPlane.normal);
#else
const PxVec3 groupCenterHull = m1to0.transform(triCenter);
#endif
//////////////////////////////////////////////////////////////////////////
PxVec3 groupAxis;
PxReal groupMinDepth;
bool faceContact;
if(!triangleConvexTest( mPolyData0, triFlags,
triangleIndex, verts,
localPlane,
groupCenterHull,
mWorld0, mWorld1, m0to1, m1to0,
mConvexScaling,
mContactDistance, mToleranceLength,
groupAxis, groupMinDepth, faceContact,
mIdtConvexScale
))
return;
if(faceContact)
{
// PT: generate face contacts immediately to save memory & avoid recomputing triangle data later
if(generateContacts(
localPlane,
verts,
triCenter, groupAxis,
groupMinDepth, triangleIndex))
{
mAnyHits = true;
mEdgeCache.addData(CachedEdge(vertInds[0], vertInds[1]));
mEdgeCache.addData(CachedEdge(vertInds[0], vertInds[2]));
mEdgeCache.addData(CachedEdge(vertInds[1], vertInds[2]));
mVertCache.addData(CachedVertex(vertInds[0]));
mVertCache.addData(CachedVertex(vertInds[1]));
mVertCache.addData(CachedVertex(vertInds[2]));
}
else
{
int stop=1;
(void)stop;
}
}
else
{
const PxU32 nb = sizeof(SavedContactData)/sizeof(PxU32);
// PT: no "pushBack" please (useless data copy + LHS)
PxU32 newSize = nb + mDelayedContacts.size();
mDelayedContacts.reserve(newSize);
SavedContactData* PX_RESTRICT cd = reinterpret_cast<SavedContactData*>(mDelayedContacts.end());
mDelayedContacts.forceSize_Unsafe(newSize);
cd->mTriangleIndex = triangleIndex;
cd->mVerts[0] = verts[0];
cd->mVerts[1] = verts[1];
cd->mVerts[2] = verts[2];
cd->mInds[0] = vertInds[0];
cd->mInds[1] = vertInds[1];
cd->mInds[2] = vertInds[2];
cd->mGroupAxis = groupAxis;
cd->mGroupMinDepth = groupMinDepth;
}
}
void ConvexMeshContactGeneration::generateLastContacts()
{
// Process delayed contacts
PxU32 nbEntries = mDelayedContacts.size();
if(nbEntries)
{
nbEntries /= sizeof(SavedContactData)/sizeof(PxU32);
// PT: TODO: replicate this fix in sphere-vs-mesh ###FIX
//const PxU32 count = mContactBuffer.count;
//const ContactPoint* PX_RESTRICT contacts = mContactBuffer.contacts;
const SavedContactData* PX_RESTRICT cd = reinterpret_cast<const SavedContactData*>(mDelayedContacts.begin());
for(PxU32 i=0;i<nbEntries;i++)
{
const SavedContactData& currentContact = cd[i];
const PxU32 triangleIndex = currentContact.mTriangleIndex;
// PT: unfortunately we must recompute this triangle-data here.
// PT: TODO: find a way not to
// const TriangleVertices T(*mMeshData, mMeshScaling, triangleIndex);
// const TriangleIndices T(*mMeshData, triangleIndex);
const PxU32 ref0 = currentContact.mInds[0];
const PxU32 ref1 = currentContact.mInds[1];
const PxU32 ref2 = currentContact.mInds[2];
// PT: TODO: why bother with the feature code at all? Use edge cache directly?
// const FeatureCode FC = computeFeatureCode(mHullCenterMesh, T.mVerts);
const FeatureCode FC = computeFeatureCode(mHullCenterMesh, currentContact.mVerts);
bool generateContact = false;
switch(FC)
{
// PT: trying the same as in sphere-mesh here
case FC_VERTEX0:
generateContact = !mVertCache.contains(CachedVertex(ref0));
break;
case FC_VERTEX1:
generateContact =!mVertCache.contains(CachedVertex(ref1));
break;
case FC_VERTEX2:
generateContact = !mVertCache.contains(CachedVertex(ref2));
break;
case FC_EDGE01:
generateContact = !mEdgeCache.contains(CachedEdge(ref0, ref1));
break;
case FC_EDGE12:
generateContact = !mEdgeCache.contains(CachedEdge(ref1, ref2));
break;
case FC_EDGE20:
generateContact = !mEdgeCache.contains(CachedEdge(ref0, ref2));
break;
case FC_FACE:
generateContact = true;
break;
case FC_UNDEFINED:
break;
};
if(!generateContact)
continue;
// const PxcPlane localPlane(T.mVerts[0], T.mVerts[1], T.mVerts[2]);
const PxPlane localPlane(currentContact.mVerts[0], currentContact.mVerts[1], currentContact.mVerts[2]);
// const PxVec3 triCenter = (T.mVerts[0] + T.mVerts[1] + T.mVerts[2])*(1.0f/3.0f);
const PxVec3 triCenter = (currentContact.mVerts[0] + currentContact.mVerts[1] + currentContact.mVerts[2])*(1.0f/3.0f);
PxVec3 groupAxis = currentContact.mGroupAxis;
if(generateContacts(
localPlane,
// T.mVerts,
currentContact.mVerts,
triCenter, groupAxis,
currentContact.mGroupMinDepth, triangleIndex))
{
mAnyHits = true;
//We don't add the edges to the data - this is important because we don't want to reject triangles
//because we generated an edge contact with an adjacent triangle
/*mEdgeCache.addData(CachedEdge(ref0, ref1));
mEdgeCache.addData(CachedEdge(ref0, ref2));
mEdgeCache.addData(CachedEdge(ref1, ref2));
mVertCache.addData(CachedVertex(ref0));
mVertCache.addData(CachedVertex(ref1));
mVertCache.addData(CachedVertex(ref2));*/
}
}
}
}
/////////////
static bool contactHullMesh2(const PolygonalData& polyData0, const PxBounds3& hullAABB, const PxTriangleMeshGeometryLL& shape1,
const PxTransform& transform0, const PxTransform& transform1,
const NarrowPhaseParams& params, ContactBuffer& contactBuffer,
const Cm::FastVertex2ShapeScaling& convexScaling, const Cm::FastVertex2ShapeScaling& meshScaling,
bool idtConvexScale, bool idtMeshScale)
{
//Just a sanity-check in debug-mode
PX_ASSERT(shape1.getType() == PxGeometryType::eTRIANGLEMESH);
////////////////////
// Compute matrices
const Cm::Matrix34 world0(transform0);
const Cm::Matrix34 world1(transform1);
// Compute relative transforms
const PxTransform t0to1 = transform1.transformInv(transform0);
const PxTransform t1to0 = transform0.transformInv(transform1);
BoxPadded hullOBB;
computeHullOBB(hullOBB, hullAABB, params.mContactDistance, world0, world1, meshScaling, idtMeshScale);
// Setup the collider
const TriangleMesh* PX_RESTRICT meshData = shape1.meshData;
Ps::InlineArray<PxU32,LOCAL_CONTACTS_SIZE> delayedContacts;
ConvexMeshContactGenerationCallback blockCallback(
delayedContacts,
t0to1, t1to0, polyData0, world0, world1, meshData, meshData->getExtraTrigData(), meshScaling,
convexScaling, params.mContactDistance, params.mToleranceLength,
idtMeshScale, idtConvexScale, params.mMeshContactMargin,
transform0, transform1,
contactBuffer, hullOBB
);
Midphase::intersectOBB(meshData, hullOBB, blockCallback, false);
blockCallback.mGeneration.generateLastContacts();
return blockCallback.mGeneration.mAnyHits;
}
/////////////
bool Gu::contactConvexMesh(GU_CONTACT_METHOD_ARGS)
{
PX_UNUSED(cache);
PX_UNUSED(renderOutput);
const PxTriangleMeshGeometryLL& shapeMesh = shape1.get<const PxTriangleMeshGeometryLL>();
const bool idtScaleMesh = shapeMesh.scale.isIdentity();
Cm::FastVertex2ShapeScaling meshScaling;
if(!idtScaleMesh)
meshScaling.init(shapeMesh.scale);
Cm::FastVertex2ShapeScaling convexScaling;
PxBounds3 hullAABB;
PolygonalData polyData0;
const bool idtScaleConvex = getConvexData(shape0, convexScaling, hullAABB, polyData0);
return contactHullMesh2(polyData0, hullAABB, shapeMesh, transform0, transform1, params, contactBuffer, convexScaling, meshScaling, idtScaleConvex, idtScaleMesh);
}
bool Gu::contactBoxMesh(GU_CONTACT_METHOD_ARGS)
{
PX_UNUSED(cache);
PX_UNUSED(renderOutput);
const PxBoxGeometry& shapeBox = shape0.get<const PxBoxGeometry>();
const PxTriangleMeshGeometryLL& shapeMesh = shape1.get<const PxTriangleMeshGeometryLL>();
PolygonalData polyData0;
PolygonalBox polyBox(shapeBox.halfExtents);
polyBox.getPolygonalData(&polyData0);
const PxBounds3 hullAABB(-shapeBox.halfExtents, shapeBox.halfExtents);
const bool idtScaleMesh = shapeMesh.scale.isIdentity();
Cm::FastVertex2ShapeScaling meshScaling;
if(!idtScaleMesh)
meshScaling.init(shapeMesh.scale);
Cm::FastVertex2ShapeScaling idtScaling;
return contactHullMesh2(polyData0, hullAABB, shapeMesh, transform0, transform1, params, contactBuffer, idtScaling, meshScaling, true, idtScaleMesh);
}
/////////////
namespace
{
struct ConvexVsHeightfieldContactGenerationCallback : EntityReport<PxU32>
{
ConvexMeshContactGeneration mGeneration;
HeightFieldUtil& mHfUtil;
ConvexVsHeightfieldContactGenerationCallback(
HeightFieldUtil& hfUtil,
Ps::InlineArray<PxU32,LOCAL_CONTACTS_SIZE>& delayedContacts,
const PxTransform& t0to1, const PxTransform& t1to0,
const PolygonalData& polyData0, const Cm::Matrix34& world0, const Cm::Matrix34& world1,
const Cm::FastVertex2ShapeScaling& convexScaling,
PxReal contactDistance,
PxReal toleranceLength,
bool idtConvexScale,
PxReal cCCDEpsilon,
const PxTransform& transform0, const PxTransform& transform1,
ContactBuffer& contactBuffer
) : mGeneration(delayedContacts, t0to1, t1to0, polyData0, world0, world1, convexScaling, contactDistance, toleranceLength, idtConvexScale, cCCDEpsilon, transform0,
transform1, contactBuffer),
mHfUtil(hfUtil)
{
}
// PT: TODO: refactor/unify with similar code in other places
virtual bool onEvent(PxU32 nb, PxU32* indices)
{
const PxU8 nextInd[] = {2,0,1};
while(nb--)
{
const PxU32 triangleIndex = *indices++;
PxU32 vertIndices[3];
PxTriangle currentTriangle; // in world space
PxU32 adjInds[3];
mHfUtil.getTriangle(mGeneration.mTransform1, currentTriangle, vertIndices, adjInds, triangleIndex, false, false);
PxVec3 normal;
currentTriangle.normal(normal);
PxU8 triFlags = 0; //KS - temporary until we can calculate triFlags for HF
for(PxU32 a = 0; a < 3; ++a)
{
if(adjInds[a] != 0xFFFFFFFF)
{
PxTriangle adjTri;
mHfUtil.getTriangle(mGeneration.mTransform1, adjTri, NULL, NULL, adjInds[a], false, false);
//We now compare the triangles to see if this edge is active
PxVec3 adjNormal;
adjTri.denormalizedNormal(adjNormal);
PxU32 otherIndex = nextInd[a];
PxF32 projD = adjNormal.dot(currentTriangle.verts[otherIndex] - adjTri.verts[0]);
if(projD < 0.f)
{
adjNormal.normalize();
PxF32 proj = adjNormal.dot(normal);
if(proj < 0.999f)
{
triFlags |= 1 << (a+3);
}
}
}
else
triFlags |= (1 << (a+3));
}
mGeneration.processTriangle(currentTriangle.verts, triangleIndex, triFlags, vertIndices);
}
return true;
}
protected:
ConvexVsHeightfieldContactGenerationCallback &operator=(const ConvexVsHeightfieldContactGenerationCallback &);
};
}
static bool contactHullHeightfield2(const PolygonalData& polyData0, const PxBounds3& hullAABB, const PxHeightFieldGeometry& shape1,
const PxTransform& transform0, const PxTransform& transform1,
const NarrowPhaseParams& params, ContactBuffer& contactBuffer,
const Cm::FastVertex2ShapeScaling& convexScaling,
bool idtConvexScale)
{
//We need to create a callback that fills triangles from the HF
HeightFieldUtil hfUtil(shape1);
const Cm::Matrix34 world0(transform0);
const Cm::Matrix34 world1(transform1);
////////////////////
// Compute relative transforms
const PxTransform t0to1 = transform1.transformInv(transform0);
const PxTransform t1to0 = transform0.transformInv(transform1);
Ps::InlineArray<PxU32, LOCAL_CONTACTS_SIZE> delayedContacts;
ConvexVsHeightfieldContactGenerationCallback blockCallback(hfUtil, delayedContacts, t0to1, t1to0, polyData0, world0, world1, convexScaling, params.mContactDistance, params.mToleranceLength,
idtConvexScale, params.mMeshContactMargin, transform0, transform1, contactBuffer);
hfUtil.overlapAABBTriangles(transform1, PxBounds3::transformFast(t0to1, hullAABB), 0, &blockCallback);
blockCallback.mGeneration.generateLastContacts();
return blockCallback.mGeneration.mAnyHits;
}
bool Gu::contactConvexHeightfield(GU_CONTACT_METHOD_ARGS)
{
PX_UNUSED(cache);
PX_UNUSED(renderOutput);
//Create a triangle cache from the HF triangles and then feed triangles to NP mesh methods
const physx::PxHeightFieldGeometryLL& shapeMesh = shape1.get<const PxHeightFieldGeometryLL>();
Cm::FastVertex2ShapeScaling convexScaling;
PxBounds3 hullAABB;
PolygonalData polyData0;
const bool idtScaleConvex = getConvexData(shape0, convexScaling, hullAABB, polyData0);
const PxVec3 inflation(params.mContactDistance);
hullAABB.minimum -= inflation;
hullAABB.maximum += inflation;
return contactHullHeightfield2(polyData0, hullAABB, shapeMesh, transform0, transform1, params, contactBuffer, convexScaling, idtScaleConvex);
}
bool Gu::contactBoxHeightfield(GU_CONTACT_METHOD_ARGS)
{
PX_UNUSED(cache);
PX_UNUSED(renderOutput);
//Create a triangle cache from the HF triangles and then feed triangles to NP mesh methods
const physx::PxHeightFieldGeometryLL& shapeMesh = shape1.get<const PxHeightFieldGeometryLL>();
const PxBoxGeometry& shapeBox = shape0.get<const PxBoxGeometry>();
PolygonalData polyData0;
PolygonalBox polyBox(shapeBox.halfExtents);
polyBox.getPolygonalData(&polyData0);
const PxVec3 inflatedExtents = shapeBox.halfExtents + PxVec3(params.mContactDistance);
const PxBounds3 hullAABB = PxBounds3(-inflatedExtents, inflatedExtents);
const Cm::FastVertex2ShapeScaling idtScaling;
return contactHullHeightfield2(polyData0, hullAABB, shapeMesh, transform0, transform1, params, contactBuffer, idtScaling, true);
}
|