summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/collision_trimesh_box.cpp
blob: 521ed437605a27aa4a45193156adcfcc3d491ac4 (plain)
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
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/


/*************************************************************************
 *                                                                       *
 * Triangle-box collider by Alen Ladavac and Vedran Klanac.              *
 * Ported to ODE by Oskari Nyman.                                        *
 *                                                                       *
 *************************************************************************/


#include <ode/collision.h>
#include <ode/rotation.h>
#include "config.h"
#include "matrix.h"
#include "odemath.h"
#include "collision_util.h"
#include "collision_trimesh_internal.h"

#if dTRIMESH_ENABLED


// largest number, double or float
#if defined(dSINGLE)
#define MAXVALUE FLT_MAX
#else
#define MAXVALUE DBL_MAX
#endif


// dVector3
// r=a-b
#define SUBTRACT(a,b,r) dSubtractVectors3(r, a, b)


// dVector3
// a=b
#define SET(a,b) dCopyVector3(a, b)


// dMatrix3
// a=b
#define SETM(a,b) dCopyMatrix4x4(a, b)


// dVector3
// r=a+b
#define ADD(a,b,r) dAddVectors3(r, a, b)


// dMatrix3, int, dVector3
// v=column a from m
#define GETCOL(m,a,v) dGetMatrixColumn3(v, m, a)


// dVector4, dVector3
// distance between plane p and point v
#define POINTDISTANCE(p,v) dPointPlaneDistance(v, p)


// dVector4, dVector3, dReal
// construct plane from normal and d
#define CONSTRUCTPLANE(plane,normal,d) dConstructPlane(normal, d, plane)


// dVector3
// length of vector a
#define LENGTHOF(a) dCalcVectorLength3(a)


struct sTrimeshBoxColliderData
{
    sTrimeshBoxColliderData(): m_iBestAxis(0), m_iExitAxis(0), m_ctContacts(0) {}

    void SetupInitialContext(dxTriMesh *TriMesh, dxGeom *BoxGeom,
        int Flags, dContactGeom* Contacts, int Stride);
    void TestCollisionForSingleTriangle(int Triint, dVector3 dv[3], bool &bOutFinishSearching);

    bool _cldTestNormal(dReal fp0, dReal fR, dVector3 vNormal, int iAxis);
    bool _cldTestFace(dReal fp0, dReal fp1, dReal fp2, dReal fR, dReal fD,
        dVector3 vNormal, int iAxis);
    bool _cldTestEdge(dReal fp0, dReal fp1, dReal fR, dReal fD,
        dVector3 vNormal, int iAxis);
    bool _cldTestSeparatingAxes(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2);
    void _cldClipping(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2, int TriIndex);
    bool _cldTestOneTriangle(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2, int TriIndex);

    void GenerateContact(int TriIndex, const dVector3 in_ContactPos, const dVector3 in_Normal, dReal in_Depth);

    // box data
    dMatrix3 m_mHullBoxRot;
    dVector3 m_vHullBoxPos;
    dVector3 m_vBoxHalfSize;

    // mesh data
    dVector3   m_vHullDstPos;

    // global collider data
    dVector3 m_vBestNormal;
    dReal    m_fBestDepth;
    int    m_iBestAxis;
    int    m_iExitAxis;
    dVector3 m_vE0, m_vE1, m_vE2, m_vN;

    // global info for contact creation
    int m_iFlags;
    dContactGeom *m_ContactGeoms;
    int m_iStride;
    dxGeom *m_Geom1;
    dxGeom *m_Geom2;
    int m_ctContacts;
};

// Test normal of mesh face as separating axis for intersection
bool sTrimeshBoxColliderData::_cldTestNormal(dReal fp0, dReal fR, dVector3 vNormal, int iAxis)
{
    // calculate overlapping interval of box and triangle
    dReal fDepth = fR+fp0;

    // if we do not overlap
    if ( fDepth<0 ) {
        // do nothing
        return false;
    }

    // calculate normal's length
    dReal fLength = LENGTHOF(vNormal);
    // if long enough
    if ( fLength > 0.0f ) {

        dReal fOneOverLength = 1.0f/fLength;
        // normalize depth
        fDepth = fDepth*fOneOverLength;

        // get minimum depth
        if (fDepth < m_fBestDepth) {
            m_vBestNormal[0] = -vNormal[0]*fOneOverLength;
            m_vBestNormal[1] = -vNormal[1]*fOneOverLength;
            m_vBestNormal[2] = -vNormal[2]*fOneOverLength;
            m_iBestAxis = iAxis;
            //dAASSERT(fDepth>=0);
            m_fBestDepth = fDepth;
        }
    }

    return true;
}




// Test box axis as separating axis
bool sTrimeshBoxColliderData::_cldTestFace(dReal fp0, dReal fp1, dReal fp2, dReal fR, dReal fD,
                                           dVector3 vNormal, int iAxis)
{
    dReal fMin, fMax;

    // find min of triangle interval
    if ( fp0 < fp1 ) {
        if ( fp0 < fp2 ) {
            fMin = fp0;
        } else {
            fMin = fp2;
        }
    } else {
        if( fp1 < fp2 ) {
            fMin = fp1;
        } else {
            fMin = fp2;
        }
    }

    // find max of triangle interval
    if ( fp0 > fp1 ) {
        if ( fp0 > fp2 ) {
            fMax = fp0;
        } else {
            fMax = fp2;
        }
    } else {
        if( fp1 > fp2 ) {
            fMax = fp1;
        } else {
            fMax = fp2;
        }
    }

    // calculate minimum and maximum depth
    dReal fDepthMin = fR - fMin;
    dReal fDepthMax = fMax + fR;

    // if we dont't have overlapping interval
    if ( fDepthMin < 0 || fDepthMax < 0 ) {
        // do nothing
        return false;
    }

    dReal fDepth = 0;

    // if greater depth is on negative side
    if ( fDepthMin > fDepthMax ) {
        // use smaller depth (one from positive side)
        fDepth = fDepthMax;
        // flip normal direction
        vNormal[0] = -vNormal[0];
        vNormal[1] = -vNormal[1];
        vNormal[2] = -vNormal[2];
        fD = -fD;
        // if greater depth is on positive side
    } else {
        // use smaller depth (one from negative side)
        fDepth = fDepthMin;
    }

    // if lower depth than best found so far
    if (fDepth < m_fBestDepth) {
        // remember current axis as best axis
        m_vBestNormal[0]  = vNormal[0];
        m_vBestNormal[1]  = vNormal[1];
        m_vBestNormal[2]  = vNormal[2];
        m_iBestAxis    = iAxis;
        //dAASSERT(fDepth>=0);
        m_fBestDepth   = fDepth;
    }

    return true;
}

// Test cross products of box axis and triangle edges as separating axis
bool sTrimeshBoxColliderData::_cldTestEdge(dReal fp0, dReal fp1, dReal fR, dReal fD,
                                           dVector3 vNormal, int iAxis)
{
    dReal fMin, fMax;

    // ===== Begin Patch by Francisco Leon, 2006/10/28 =====

    // Fixed Null Normal. This prevents boxes passing
    // through trimeshes at certain contact angles

    fMin = vNormal[0] * vNormal[0] +
        vNormal[1] * vNormal[1] +
        vNormal[2] * vNormal[2];

    if ( fMin <= dEpsilon ) /// THIS NORMAL WOULD BE DANGEROUS
        return true;

    // ===== Ending Patch by Francisco Leon =====


    // calculate min and max interval values
    if ( fp0 < fp1 ) {
        fMin = fp0;
        fMax = fp1;
    } else {
        fMin = fp1;
        fMax = fp0;
    }

    // check if we overlapp
    dReal fDepthMin = fR - fMin;
    dReal fDepthMax = fMax + fR;

    // if we don't overlapp
    if ( fDepthMin < 0 || fDepthMax < 0 ) {
        // do nothing
        return false;
    }

    dReal fDepth;

    // if greater depth is on negative side
    if ( fDepthMin > fDepthMax ) {
        // use smaller depth (one from positive side)
        fDepth = fDepthMax;
        // flip normal direction
        vNormal[0] = -vNormal[0];
        vNormal[1] = -vNormal[1];
        vNormal[2] = -vNormal[2];
        fD = -fD;
        // if greater depth is on positive side
    } else {
        // use smaller depth (one from negative side)
        fDepth = fDepthMin;
    }

    // calculate normal's length
    dReal fLength = LENGTHOF(vNormal);

    // if long enough
    if ( fLength > 0.0f ) {

        // normalize depth
        dReal fOneOverLength = 1.0f/fLength;
        fDepth = fDepth*fOneOverLength;
        fD*=fOneOverLength;

        // if lower depth than best found so far (favor face over edges)
        if (fDepth*1.5f < m_fBestDepth) {
            // remember current axis as best axis
            m_vBestNormal[0]  = vNormal[0]*fOneOverLength;
            m_vBestNormal[1]  = vNormal[1]*fOneOverLength;
            m_vBestNormal[2]  = vNormal[2]*fOneOverLength;
            m_iBestAxis    = iAxis;
            //dAASSERT(fDepth>=0);
            m_fBestDepth   = fDepth;
        }
    }

    return true;
}


// clip polygon with plane and generate new polygon points
static void _cldClipPolyToPlane( dVector3 avArrayIn[], int ctIn,
                                dVector3 avArrayOut[], int &ctOut,
                                const dVector4 &plPlane )
{
    // start with no output points
    ctOut = 0;

    int i0 = ctIn-1;

    // for each edge in input polygon
    for (int i1=0; i1<ctIn; i0=i1, i1++) {


        // calculate distance of edge points to plane
        dReal fDistance0 = POINTDISTANCE( plPlane ,avArrayIn[i0] );
        dReal fDistance1 = POINTDISTANCE( plPlane ,avArrayIn[i1] );


        // if first point is in front of plane
        if( fDistance0 >= 0 ) {
            // emit point
            avArrayOut[ctOut][0] = avArrayIn[i0][0];
            avArrayOut[ctOut][1] = avArrayIn[i0][1];
            avArrayOut[ctOut][2] = avArrayIn[i0][2];
            ctOut++;
        }

        // if points are on different sides
        if( (fDistance0 > 0 && fDistance1 < 0) || ( fDistance0 < 0 && fDistance1 > 0) ) {

            // find intersection point of edge and plane
            dVector3 vIntersectionPoint;
            vIntersectionPoint[0]= avArrayIn[i0][0] - (avArrayIn[i0][0]-avArrayIn[i1][0])*fDistance0/(fDistance0-fDistance1);
            vIntersectionPoint[1]= avArrayIn[i0][1] - (avArrayIn[i0][1]-avArrayIn[i1][1])*fDistance0/(fDistance0-fDistance1);
            vIntersectionPoint[2]= avArrayIn[i0][2] - (avArrayIn[i0][2]-avArrayIn[i1][2])*fDistance0/(fDistance0-fDistance1);

            // emit intersection point
            avArrayOut[ctOut][0] = vIntersectionPoint[0];
            avArrayOut[ctOut][1] = vIntersectionPoint[1];
            avArrayOut[ctOut][2] = vIntersectionPoint[2];
            ctOut++;
        }
    }

}




bool sTrimeshBoxColliderData::_cldTestSeparatingAxes(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2) {
    // reset best axis
    m_iBestAxis = 0;
    m_iExitAxis = -1;
    m_fBestDepth = MAXVALUE;

    // calculate edges
    SUBTRACT(v1,v0,m_vE0);
    SUBTRACT(v2,v0,m_vE1);
    SUBTRACT(m_vE1,m_vE0,m_vE2);

    // calculate poly normal
    dCalcVectorCross3(m_vN,m_vE0,m_vE1);

    // calculate length of face normal
    dReal fNLen = LENGTHOF(m_vN);

    // Even though all triangles might be initially valid, 
    // a triangle may degenerate into a segment after applying 
    // space transformation.
    if (!fNLen) {
        return false;
    }

    // extract box axes as vectors
    dVector3 vA0,vA1,vA2;
    GETCOL(m_mHullBoxRot,0,vA0);
    GETCOL(m_mHullBoxRot,1,vA1);
    GETCOL(m_mHullBoxRot,2,vA2);

    // box halfsizes
    dReal fa0 = m_vBoxHalfSize[0];
    dReal fa1 = m_vBoxHalfSize[1];
    dReal fa2 = m_vBoxHalfSize[2];

    // calculate relative position between box and triangle
    dVector3 vD;
    SUBTRACT(v0,m_vHullBoxPos,vD);

    dVector3 vL;
    dReal fp0, fp1, fp2, fR, fD;

    // Test separating axes for intersection
    // ************************************************
    // Axis 1 - Triangle Normal
    SET(vL,m_vN);
    fp0  = dCalcVectorDot3(vL,vD);
    fp1  = fp0;
    fp2  = fp0;
    fR=fa0*dFabs( dCalcVectorDot3(m_vN,vA0) ) + fa1 * dFabs( dCalcVectorDot3(m_vN,vA1) ) + fa2 * dFabs( dCalcVectorDot3(m_vN,vA2) );

    if (!_cldTestNormal(fp0, fR, vL, 1)) {
        m_iExitAxis=1;
        return false;
    }

    // ************************************************

    // Test Faces
    // ************************************************
    // Axis 2 - Box X-Axis
    SET(vL,vA0);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 + dCalcVectorDot3(vA0,m_vE0);
    fp2 = fp0 + dCalcVectorDot3(vA0,m_vE1);
    fR  = fa0;

    if (!_cldTestFace(fp0, fp1, fp2, fR, fD, vL, 2)) {
        m_iExitAxis=2;
        return false;
    }
    // ************************************************

    // ************************************************
    // Axis 3 - Box Y-Axis
    SET(vL,vA1);
    fD = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 + dCalcVectorDot3(vA1,m_vE0);
    fp2 = fp0 + dCalcVectorDot3(vA1,m_vE1);
    fR  = fa1;

    if (!_cldTestFace(fp0, fp1, fp2, fR, fD, vL, 3)) {
        m_iExitAxis=3;
        return false;
    }

    // ************************************************

    // ************************************************
    // Axis 4 - Box Z-Axis
    SET(vL,vA2);
    fD = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 + dCalcVectorDot3(vA2,m_vE0);
    fp2 = fp0 + dCalcVectorDot3(vA2,m_vE1);
    fR  = fa2;

    if (!_cldTestFace(fp0, fp1, fp2, fR, fD, vL, 4)) {
        m_iExitAxis=4;
        return false;
    }

    // ************************************************

    // Test Edges
    // ************************************************
    // Axis 5 - Box X-Axis cross Edge0
    dCalcVectorCross3(vL,vA0,m_vE0);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0;
    fp2 = fp0 + dCalcVectorDot3(vA0,m_vN);
    fR  = fa1 * dFabs(dCalcVectorDot3(vA2,m_vE0)) + fa2 * dFabs(dCalcVectorDot3(vA1,m_vE0));

    if (!_cldTestEdge(fp1, fp2, fR, fD, vL, 5)) {
        m_iExitAxis=5;
        return false;
    }
    // ************************************************

    // ************************************************
    // Axis 6 - Box X-Axis cross Edge1
    dCalcVectorCross3(vL,vA0,m_vE1);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 - dCalcVectorDot3(vA0,m_vN);
    fp2 = fp0;
    fR  = fa1 * dFabs(dCalcVectorDot3(vA2,m_vE1)) + fa2 * dFabs(dCalcVectorDot3(vA1,m_vE1));

    if (!_cldTestEdge(fp0, fp1, fR, fD, vL, 6)) {
        m_iExitAxis=6;
        return false;
    }
    // ************************************************

    // ************************************************
    // Axis 7 - Box X-Axis cross Edge2
    dCalcVectorCross3(vL,vA0,m_vE2);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 - dCalcVectorDot3(vA0,m_vN);
    fp2 = fp0 - dCalcVectorDot3(vA0,m_vN);
    fR  = fa1 * dFabs(dCalcVectorDot3(vA2,m_vE2)) + fa2 * dFabs(dCalcVectorDot3(vA1,m_vE2));

    if (!_cldTestEdge(fp0, fp1, fR, fD, vL, 7)) {
        m_iExitAxis=7;
        return false;
    }

    // ************************************************

    // ************************************************
    // Axis 8 - Box Y-Axis cross Edge0
    dCalcVectorCross3(vL,vA1,m_vE0);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0;
    fp2 = fp0 + dCalcVectorDot3(vA1,m_vN);
    fR  = fa0 * dFabs(dCalcVectorDot3(vA2,m_vE0)) + fa2 * dFabs(dCalcVectorDot3(vA0,m_vE0));

    if (!_cldTestEdge(fp0, fp2, fR, fD, vL, 8)) {
        m_iExitAxis=8;
        return false;
    }

    // ************************************************

    // ************************************************
    // Axis 9 - Box Y-Axis cross Edge1
    dCalcVectorCross3(vL,vA1,m_vE1);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 - dCalcVectorDot3(vA1,m_vN);
    fp2 = fp0;
    fR  = fa0 * dFabs(dCalcVectorDot3(vA2,m_vE1)) + fa2 * dFabs(dCalcVectorDot3(vA0,m_vE1));

    if (!_cldTestEdge(fp0, fp1, fR, fD, vL, 9)) {
        m_iExitAxis=9;
        return false;
    }

    // ************************************************

    // ************************************************
    // Axis 10 - Box Y-Axis cross Edge2
    dCalcVectorCross3(vL,vA1,m_vE2);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 - dCalcVectorDot3(vA1,m_vN);
    fp2 = fp0 - dCalcVectorDot3(vA1,m_vN);
    fR  = fa0 * dFabs(dCalcVectorDot3(vA2,m_vE2)) + fa2 * dFabs(dCalcVectorDot3(vA0,m_vE2));

    if (!_cldTestEdge(fp0, fp1, fR, fD, vL, 10)) {
        m_iExitAxis=10;
        return false;
    }

    // ************************************************

    // ************************************************
    // Axis 11 - Box Z-Axis cross Edge0
    dCalcVectorCross3(vL,vA2,m_vE0);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0;
    fp2 = fp0 + dCalcVectorDot3(vA2,m_vN);
    fR  = fa0 * dFabs(dCalcVectorDot3(vA1,m_vE0)) + fa1 * dFabs(dCalcVectorDot3(vA0,m_vE0));

    if (!_cldTestEdge(fp0, fp2, fR, fD, vL, 11)) {
        m_iExitAxis=11;
        return false;
    }
    // ************************************************

    // ************************************************
    // Axis 12 - Box Z-Axis cross Edge1
    dCalcVectorCross3(vL,vA2,m_vE1);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 - dCalcVectorDot3(vA2,m_vN);
    fp2 = fp0;
    fR  = fa0 * dFabs(dCalcVectorDot3(vA1,m_vE1)) + fa1 * dFabs(dCalcVectorDot3(vA0,m_vE1));

    if (!_cldTestEdge(fp0, fp1, fR, fD, vL, 12)) {
        m_iExitAxis=12;
        return false;
    }
    // ************************************************

    // ************************************************
    // Axis 13 - Box Z-Axis cross Edge2
    dCalcVectorCross3(vL,vA2,m_vE2);
    fD  = dCalcVectorDot3(vL,m_vN)/fNLen;
    fp0 = dCalcVectorDot3(vL,vD);
    fp1 = fp0 - dCalcVectorDot3(vA2,m_vN);
    fp2 = fp0 - dCalcVectorDot3(vA2,m_vN);
    fR  = fa0 * dFabs(dCalcVectorDot3(vA1,m_vE2)) + fa1 * dFabs(dCalcVectorDot3(vA0,m_vE2));

    if (!_cldTestEdge(fp0, fp1, fR, fD, vL, 13)) {
        m_iExitAxis=13;
        return false;
    }

    // ************************************************
    return true;
}





// find two closest points on two lines
static bool _cldClosestPointOnTwoLines(
    dVector3 vPoint1, dVector3 vLenVec1, dVector3 vPoint2, dVector3 vLenVec2,
    dReal &fvalue1, dReal &fvalue2)
{
    // calculate denominator
    dVector3 vp;
    SUBTRACT(vPoint2,vPoint1,vp);
    dReal fuaub  = dCalcVectorDot3(vLenVec1,vLenVec2);
    dReal fq1    = dCalcVectorDot3(vLenVec1,vp);
    dReal fq2    = -dCalcVectorDot3(vLenVec2,vp);
    dReal fd     = 1.0f - fuaub * fuaub;

    // if denominator is positive
    if (fd > 0.0f) {
        // calculate points of closest approach
        fd = 1.0f/fd;
        fvalue1 = (fq1 + fuaub*fq2)*fd;
        fvalue2 = (fuaub*fq1 + fq2)*fd;
        return true;
        // otherwise
    } else {
        // lines are parallel
        fvalue1 = 0.0f;
        fvalue2 = 0.0f;
        return false;
    }
}





// clip and generate contacts
void sTrimeshBoxColliderData::_cldClipping(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2, int TriIndex) {
    dIASSERT( !(m_iFlags & CONTACTS_UNIMPORTANT) || m_ctContacts < (m_iFlags & NUMC_MASK) ); // Do not call the function if there is no room to store results

    // if we have edge/edge intersection
    if (m_iBestAxis > 4 ) {
        dVector3 vub,vPb,vPa;

        SET(vPa,m_vHullBoxPos);

        // calculate point on box edge
        for( int i=0; i<3; i++) {
            dVector3 vRotCol;
            GETCOL(m_mHullBoxRot,i,vRotCol);
            dReal fSign = dCalcVectorDot3(m_vBestNormal,vRotCol) > 0 ? 1.0f : -1.0f;

            vPa[0] += fSign * m_vBoxHalfSize[i] * vRotCol[0];
            vPa[1] += fSign * m_vBoxHalfSize[i] * vRotCol[1];
            vPa[2] += fSign * m_vBoxHalfSize[i] * vRotCol[2];
        }

        int iEdge = (m_iBestAxis-5)%3;

        // decide which edge is on triangle
        if ( iEdge == 0 ) {
            SET(vPb,v0);
            SET(vub,m_vE0);
        } else if ( iEdge == 1) {
            SET(vPb,v2);
            SET(vub,m_vE1);
        } else {
            SET(vPb,v1);
            SET(vub,m_vE2);
        }


        // setup direction parameter for face edge
        dNormalize3(vub);

        dReal fParam1, fParam2;

        // setup direction parameter for box edge
        dVector3 vua;
        int col=(m_iBestAxis-5)/3;
        GETCOL(m_mHullBoxRot,col,vua);

        // find two closest points on both edges
        _cldClosestPointOnTwoLines( vPa, vua, vPb, vub, fParam1, fParam2 );
        vPa[0] += vua[0]*fParam1;
        vPa[1] += vua[1]*fParam1;
        vPa[2] += vua[2]*fParam1;

        vPb[0] += vub[0]*fParam2;
        vPb[1] += vub[1]*fParam2;
        vPb[2] += vub[2]*fParam2;

        // calculate collision point
        dVector3 vPntTmp;
        ADD(vPa,vPb,vPntTmp);

        vPntTmp[0]*=0.5f;
        vPntTmp[1]*=0.5f;
        vPntTmp[2]*=0.5f;

        // generate contact point between two closest points
        GenerateContact(TriIndex, vPntTmp, m_vBestNormal, m_fBestDepth);


        // if triangle is the referent face then clip box to triangle face
    } else if (m_iBestAxis == 1) {

        dVector3 vNormal2;
        vNormal2[0]=-m_vBestNormal[0];
        vNormal2[1]=-m_vBestNormal[1];
        vNormal2[2]=-m_vBestNormal[2];


        // vNr is normal in box frame, pointing from triangle to box
        dMatrix3 mTransposed;
        mTransposed[0*4+0]=m_mHullBoxRot[0*4+0];
        mTransposed[0*4+1]=m_mHullBoxRot[1*4+0];
        mTransposed[0*4+2]=m_mHullBoxRot[2*4+0];

        mTransposed[1*4+0]=m_mHullBoxRot[0*4+1];
        mTransposed[1*4+1]=m_mHullBoxRot[1*4+1];
        mTransposed[1*4+2]=m_mHullBoxRot[2*4+1];

        mTransposed[2*4+0]=m_mHullBoxRot[0*4+2];
        mTransposed[2*4+1]=m_mHullBoxRot[1*4+2];
        mTransposed[2*4+2]=m_mHullBoxRot[2*4+2];

        dVector3 vNr;
        vNr[0]=mTransposed[0*4+0]*vNormal2[0]+  mTransposed[0*4+1]*vNormal2[1]+  mTransposed[0*4+2]*vNormal2[2];
        vNr[1]=mTransposed[1*4+0]*vNormal2[0]+  mTransposed[1*4+1]*vNormal2[1]+  mTransposed[1*4+2]*vNormal2[2];
        vNr[2]=mTransposed[2*4+0]*vNormal2[0]+  mTransposed[2*4+1]*vNormal2[1]+  mTransposed[2*4+2]*vNormal2[2];


        dVector3 vAbsNormal;
        vAbsNormal[0] = dFabs( vNr[0] );
        vAbsNormal[1] = dFabs( vNr[1] );
        vAbsNormal[2] = dFabs( vNr[2] );

        // get closest face from box
        int iB0, iB1, iB2;
        if (vAbsNormal[1] > vAbsNormal[0]) {
            if (vAbsNormal[1] > vAbsNormal[2]) {
                iB1 = 0;  iB0 = 1;  iB2 = 2;
            } else {
                iB1 = 0;  iB2 = 1;  iB0 = 2;
            }
        } else {

            if (vAbsNormal[0] > vAbsNormal[2]) {
                iB0 = 0;  iB1 = 1;  iB2 = 2;
            } else {
                iB1 = 0;  iB2 = 1;  iB0 = 2;
            }
        }

        // Here find center of box face we are going to project
        dVector3 vCenter;
        dVector3 vRotCol;
        GETCOL(m_mHullBoxRot,iB0,vRotCol);

        if (vNr[iB0] > 0) {
            vCenter[0] = m_vHullBoxPos[0] - v0[0] - m_vBoxHalfSize[iB0] * vRotCol[0];
            vCenter[1] = m_vHullBoxPos[1] - v0[1] - m_vBoxHalfSize[iB0] * vRotCol[1];
            vCenter[2] = m_vHullBoxPos[2] - v0[2] - m_vBoxHalfSize[iB0] * vRotCol[2];
        } else {
            vCenter[0] = m_vHullBoxPos[0] - v0[0] + m_vBoxHalfSize[iB0] * vRotCol[0];
            vCenter[1] = m_vHullBoxPos[1] - v0[1] + m_vBoxHalfSize[iB0] * vRotCol[1];
            vCenter[2] = m_vHullBoxPos[2] - v0[2] + m_vBoxHalfSize[iB0] * vRotCol[2];
        }

        // Here find 4 corner points of box
        dVector3 avPoints[4];

        dVector3 vRotCol2;
        GETCOL(m_mHullBoxRot,iB1,vRotCol);
        GETCOL(m_mHullBoxRot,iB2,vRotCol2);

        for(int x=0;x<3;x++) {
            avPoints[0][x] = vCenter[x] + (m_vBoxHalfSize[iB1] * vRotCol[x]) - (m_vBoxHalfSize[iB2] * vRotCol2[x]);
            avPoints[1][x] = vCenter[x] - (m_vBoxHalfSize[iB1] * vRotCol[x]) - (m_vBoxHalfSize[iB2] * vRotCol2[x]);
            avPoints[2][x] = vCenter[x] - (m_vBoxHalfSize[iB1] * vRotCol[x]) + (m_vBoxHalfSize[iB2] * vRotCol2[x]);
            avPoints[3][x] = vCenter[x] + (m_vBoxHalfSize[iB1] * vRotCol[x]) + (m_vBoxHalfSize[iB2] * vRotCol2[x]);
        }

        // clip Box face with 4 planes of triangle (1 face plane, 3 egde planes)
        dVector3 avTempArray1[9];
        dVector3 avTempArray2[9];
        dVector4 plPlane;

        int iTempCnt1=0;
        int iTempCnt2=0;

        // zeroify vectors - necessary?
        for(int i=0; i<9; i++) {
            avTempArray1[i][0]=0;
            avTempArray1[i][1]=0;
            avTempArray1[i][2]=0;

            avTempArray2[i][0]=0;
            avTempArray2[i][1]=0;
            avTempArray2[i][2]=0;
        }


        // Normal plane
        dVector3 vTemp;
        vTemp[0]=-m_vN[0];
        vTemp[1]=-m_vN[1];
        vTemp[2]=-m_vN[2];
        dNormalize3(vTemp);
        CONSTRUCTPLANE(plPlane,vTemp,0);

        _cldClipPolyToPlane( avPoints, 4, avTempArray1, iTempCnt1, plPlane  );


        // Plane p0
        dVector3 vTemp2;
        SUBTRACT(v1,v0,vTemp2);
        dCalcVectorCross3(vTemp,m_vN,vTemp2);
        dNormalize3(vTemp);
        CONSTRUCTPLANE(plPlane,vTemp,0);

        _cldClipPolyToPlane( avTempArray1, iTempCnt1, avTempArray2, iTempCnt2, plPlane  );

        // Plane p1
        SUBTRACT(v2,v1,vTemp2);
        dCalcVectorCross3(vTemp,m_vN,vTemp2);
        dNormalize3(vTemp);
        SUBTRACT(v0,v2,vTemp2);
        CONSTRUCTPLANE(plPlane,vTemp,dCalcVectorDot3(vTemp2,vTemp));

        _cldClipPolyToPlane( avTempArray2, iTempCnt2, avTempArray1, iTempCnt1, plPlane  );

        // Plane p2
        SUBTRACT(v0,v2,vTemp2);
        dCalcVectorCross3(vTemp,m_vN,vTemp2);
        dNormalize3(vTemp);
        CONSTRUCTPLANE(plPlane,vTemp,0);

        _cldClipPolyToPlane( avTempArray1, iTempCnt1, avTempArray2, iTempCnt2, plPlane  );

        // END of clipping polygons

        // for each generated contact point
        for ( int i=0; i<iTempCnt2; i++ ) {
            // calculate depth
            dReal fTempDepth = dCalcVectorDot3(vNormal2,avTempArray2[i]);

            // clamp depth to zero
            if (fTempDepth > 0) {
                fTempDepth = 0;
            }

            dVector3 vPntTmp;
            ADD(avTempArray2[i],v0,vPntTmp);

            GenerateContact(TriIndex, vPntTmp, m_vBestNormal, -fTempDepth);

            if ((m_ctContacts | CONTACTS_UNIMPORTANT) == (m_iFlags & (NUMC_MASK | CONTACTS_UNIMPORTANT))) {
                break;
            }
        }

        //dAASSERT(m_ctContacts>0);

        // if box face is the referent face, then clip triangle on box face
    } else { // 2 <= if iBestAxis <= 4

        // get normal of box face
        dVector3 vNormal2;
        SET(vNormal2,m_vBestNormal);

        // get indices of box axes in correct order
        int iA0,iA1,iA2;
        iA0 = m_iBestAxis-2;
        if ( iA0 == 0 ) {
            iA1 = 1; iA2 = 2;
        } else if ( iA0 == 1 ) {
            iA1 = 0; iA2 = 2;
        } else {
            iA1 = 0; iA2 = 1;
        }

        dVector3 avPoints[3];
        // calculate triangle vertices in box frame
        SUBTRACT(v0,m_vHullBoxPos,avPoints[0]);
        SUBTRACT(v1,m_vHullBoxPos,avPoints[1]);
        SUBTRACT(v2,m_vHullBoxPos,avPoints[2]);

        // CLIP Polygons
        // define temp data for clipping
        dVector3 avTempArray1[9];
        dVector3 avTempArray2[9];

        int iTempCnt1, iTempCnt2;

        // zeroify vectors - necessary?
        for(int i=0; i<9; i++) {
            avTempArray1[i][0]=0;
            avTempArray1[i][1]=0;
            avTempArray1[i][2]=0;

            avTempArray2[i][0]=0;
            avTempArray2[i][1]=0;
            avTempArray2[i][2]=0;
        }

        // clip triangle with 5 box planes (1 face plane, 4 edge planes)

        dVector4 plPlane;

        // Normal plane
        dVector3 vTemp;
        vTemp[0]=-vNormal2[0];
        vTemp[1]=-vNormal2[1];
        vTemp[2]=-vNormal2[2];
        CONSTRUCTPLANE(plPlane,vTemp,m_vBoxHalfSize[iA0]);

        _cldClipPolyToPlane( avPoints, 3, avTempArray1, iTempCnt1, plPlane );


        // Plane p0
        GETCOL(m_mHullBoxRot,iA1,vTemp);
        CONSTRUCTPLANE(plPlane,vTemp,m_vBoxHalfSize[iA1]);

        _cldClipPolyToPlane( avTempArray1, iTempCnt1, avTempArray2, iTempCnt2, plPlane );


        // Plane p1
        GETCOL(m_mHullBoxRot,iA1,vTemp);
        vTemp[0]=-vTemp[0];
        vTemp[1]=-vTemp[1];
        vTemp[2]=-vTemp[2];
        CONSTRUCTPLANE(plPlane,vTemp,m_vBoxHalfSize[iA1]);

        _cldClipPolyToPlane( avTempArray2, iTempCnt2, avTempArray1, iTempCnt1, plPlane );

        // Plane p2
        GETCOL(m_mHullBoxRot,iA2,vTemp);
        CONSTRUCTPLANE(plPlane,vTemp,m_vBoxHalfSize[iA2]);

        _cldClipPolyToPlane( avTempArray1, iTempCnt1, avTempArray2, iTempCnt2, plPlane );

        // Plane p3
        GETCOL(m_mHullBoxRot,iA2,vTemp);
        vTemp[0]=-vTemp[0];
        vTemp[1]=-vTemp[1];
        vTemp[2]=-vTemp[2];
        CONSTRUCTPLANE(plPlane,vTemp,m_vBoxHalfSize[iA2]);

        _cldClipPolyToPlane( avTempArray2, iTempCnt2, avTempArray1, iTempCnt1, plPlane );


        // for each generated contact point
        for ( int i=0; i<iTempCnt1; i++ ) {
            // calculate depth
            dReal fTempDepth = dCalcVectorDot3(vNormal2,avTempArray1[i])-m_vBoxHalfSize[iA0];

            // clamp depth to zero
            if (fTempDepth > 0) {
                fTempDepth = 0;
            }

            // generate contact data
            dVector3 vPntTmp;
            ADD(avTempArray1[i],m_vHullBoxPos,vPntTmp);

            GenerateContact(TriIndex, vPntTmp, m_vBestNormal, -fTempDepth);

            if ((m_ctContacts | CONTACTS_UNIMPORTANT) == (m_iFlags & (NUMC_MASK | CONTACTS_UNIMPORTANT))) {
                break;
            }
        }

        //dAASSERT(m_ctContacts>0);
    }
}

// GenerateContact - Written by Jeff Smith (jeff@burri.to)
//   Generate a "unique" contact.  A unique contact has a unique
//   position or normal.  If the potential contact has the same
//   position and normal as an existing contact, but a larger
//   penetration depth, this new depth is used instead
//
void sTrimeshBoxColliderData::GenerateContact(int TriIndex, const dVector3 in_ContactPos, const dVector3 in_Normal, dReal in_Depth)
{
    int TriCount = m_ctContacts;

    do
    {
        dContactGeom* TgtContact = NULL;
        bool deeper = false;

        if (!(m_iFlags & CONTACTS_UNIMPORTANT))
        {
            dReal MinDepth = dInfinity;
            dContactGeom* MinContact = NULL;

            bool duplicate = false;
            for (int i = 0; i < TriCount; i++)
            {
                dContactGeom* Contact = SAFECONTACT(m_iFlags, m_ContactGeoms, i, m_iStride);

                // same position?
                dVector3 diff;
                dSubtractVectors3(diff, in_ContactPos, Contact->pos);

                if (dCalcVectorDot3(diff, diff) < dEpsilon)
                {
                    // same normal?
                    if (REAL(1.0) - dCalcVectorDot3(in_Normal, Contact->normal) < dEpsilon)
                    {
                        if (in_Depth > Contact->depth)
                        {
                            Contact->depth = in_Depth;
                            Contact->side1 = TriIndex;
                        }

                        duplicate = true;
                        break;
                    }
                }

                if (Contact->depth < MinDepth)
                {
                    MinDepth = Contact->depth;
                    MinContact = Contact;
                }
            }
            if (duplicate)
            {
                break;
            }

            if (TriCount == (m_iFlags & NUMC_MASK))
            {
                if (!(MinDepth < in_Depth))
                {
                    break;
                }

                TgtContact = MinContact;
                deeper = true;
            }
        }
        else
        {
            dIASSERT(TriCount < (m_iFlags & NUMC_MASK));
        }

        if (!deeper)
        {
            // Add a new contact
            TgtContact = SAFECONTACT(m_iFlags, m_ContactGeoms, TriCount, m_iStride);
            TriCount++;

            TgtContact->pos[3] = 0.0;

            TgtContact->normal[3] = 0.0;

            TgtContact->g1 = m_Geom1;
            TgtContact->g2 = m_Geom2;

            TgtContact->side2 = -1;
        }

        TgtContact->pos[0] = in_ContactPos[0];
        TgtContact->pos[1] = in_ContactPos[1];
        TgtContact->pos[2] = in_ContactPos[2];

        TgtContact->normal[0] = in_Normal[0];
        TgtContact->normal[1] = in_Normal[1];
        TgtContact->normal[2] = in_Normal[2];

        TgtContact->depth = in_Depth;

        TgtContact->side1 = TriIndex;

        m_ctContacts = TriCount;
    }
    while (false);
}





void sTrimeshBoxColliderData::SetupInitialContext(dxTriMesh *TriMesh, dxGeom *BoxGeom,
                                                  int Flags, dContactGeom* Contacts, int Stride)
{
    // get source hull position, orientation and half size
    const dMatrix3& mRotBox=*(const dMatrix3*)dGeomGetRotation(BoxGeom);
    const dVector3& vPosBox=*(const dVector3*)dGeomGetPosition(BoxGeom);

    // to global
    SETM(m_mHullBoxRot,mRotBox);
    SET(m_vHullBoxPos,vPosBox);

    dGeomBoxGetLengths(BoxGeom, m_vBoxHalfSize);
    m_vBoxHalfSize[0] *= 0.5f;
    m_vBoxHalfSize[1] *= 0.5f;
    m_vBoxHalfSize[2] *= 0.5f;

    // get destination hull position and orientation
    const dVector3& vPosMesh=*(const dVector3*)dGeomGetPosition(TriMesh);

    // to global
    SET(m_vHullDstPos,vPosMesh);

    // global info for contact creation
    m_ctContacts = 0;
    m_iStride=Stride;
    m_iFlags=Flags;
    m_ContactGeoms=Contacts;
    m_Geom1=TriMesh;
    m_Geom2=BoxGeom;

    // reset stuff
    m_fBestDepth = MAXVALUE;
    m_vBestNormal[0]=0;
    m_vBestNormal[1]=0;
    m_vBestNormal[2]=0;
}

void sTrimeshBoxColliderData::TestCollisionForSingleTriangle(int Triint, dVector3 dv[3], bool &bOutFinishSearching)
{
    bool finish = false;

    // test this triangle
    if (_cldTestOneTriangle(dv[0], dv[1], dv[2], Triint))
    {
        /*
        NOTE by Oleh_Derevenko:
        The function continues checking triangles after maximal number
        of contacts is reached because it selects maximal penetration depths.
        See also comments in GenerateContact()
        */
        finish = ((m_ctContacts | CONTACTS_UNIMPORTANT) == (m_iFlags & (NUMC_MASK | CONTACTS_UNIMPORTANT)));
    }

    bOutFinishSearching = finish;
}

// test one mesh triangle on intersection with given box
bool sTrimeshBoxColliderData::_cldTestOneTriangle(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2, int TriIndex)//, void *pvUser)
{
    // do intersection test and find best separating axis
    if (!_cldTestSeparatingAxes(v0, v1, v2)) {
        // if not found do nothing
        return false;
    }

    // if best separation axis is not found
    if (m_iBestAxis == 0) {
        // this should not happen (we should already exit in that case)
        //dMessage (0, "best separation axis not found");
        // do nothing
        return false;
    }

    _cldClipping(v0, v1, v2, TriIndex);
    return true;
}


// OPCODE version of box to mesh collider
#if dTRIMESH_OPCODE
static void dQueryBTLPotentialCollisionTriangles(OBBCollider &Collider, 
                                                 const sTrimeshBoxColliderData &cData, dxTriMesh *TriMesh, dxGeom *BoxGeom,
                                                 OBBCache &BoxCache)
{
    // get destination hull position and orientation
    const dMatrix3& mRotMesh=*(const dMatrix3*)dGeomGetRotation(TriMesh);
    const dVector3& vPosMesh=*(const dVector3*)dGeomGetPosition(TriMesh);

    Matrix4x4 MeshMatrix;
    const dVector3 vZeroVector3 = { REAL(0.0), };
    MakeMatrix(vZeroVector3, mRotMesh, MeshMatrix);

    // get source hull position, orientation and half size
    const dMatrix3& mRotBox=*(const dMatrix3*)dGeomGetRotation(BoxGeom);
    const dVector3& vPosBox=*(const dVector3*)dGeomGetPosition(BoxGeom);

    dVector3 vOffsetPosBox;
    dSubtractVectors3(vOffsetPosBox, vPosBox, vPosMesh);

    // Make OBB
    OBB Box;
    Box.mCenter.Set(vOffsetPosBox[0], vOffsetPosBox[1], vOffsetPosBox[2]);
    Box.mExtents.Set(cData.m_vBoxHalfSize[0], cData.m_vBoxHalfSize[1], cData.m_vBoxHalfSize[2]);
    Box.mRot.Set(
        mRotBox[0], mRotBox[4], mRotBox[8], 
        mRotBox[1], mRotBox[5], mRotBox[9],
        mRotBox[2], mRotBox[6], mRotBox[10]);

    // TC results
    if (TriMesh->getDoTC(dxTriMesh::TTC_BOX)) {
        dxTriMesh::BoxTC* BoxTC = 0;
        const int iBoxCacheSize = TriMesh->m_BoxTCCache.size();
        for (int i = 0; i != iBoxCacheSize; i++){
            if (TriMesh->m_BoxTCCache[i].Geom == BoxGeom){
                BoxTC = &TriMesh->m_BoxTCCache[i];
                break;
            }
        }
        if (!BoxTC){
            TriMesh->m_BoxTCCache.push(dxTriMesh::BoxTC());

            BoxTC = &TriMesh->m_BoxTCCache[TriMesh->m_BoxTCCache.size() - 1];
            BoxTC->Geom = BoxGeom;
            BoxTC->FatCoeff = 1.1f; // Pierre recommends this, instead of 1.0
        }

        // Intersect
        Collider.SetTemporalCoherence(true);
        Collider.Collide(*BoxTC, Box, TriMesh->retrieveMeshBVTreeRef(), null, &MeshMatrix);
    }
    else {
        Collider.SetTemporalCoherence(false);
        Collider.Collide(BoxCache, Box, TriMesh->retrieveMeshBVTreeRef(), null, &MeshMatrix);
    }
}

int dCollideBTL(dxGeom* g1, dxGeom* BoxGeom, int Flags, dContactGeom* Contacts, int Stride){
    dIASSERT (Stride >= (int)sizeof(dContactGeom));
    dIASSERT (g1->type == dTriMeshClass);
    dIASSERT (BoxGeom->type == dBoxClass);
    dIASSERT ((Flags & NUMC_MASK) >= 1);

    dxTriMesh* TriMesh = (dxTriMesh*)g1;

    sTrimeshBoxColliderData cData;
    cData.SetupInitialContext(TriMesh, BoxGeom, Flags, Contacts, Stride);

    const unsigned uiTLSKind = TriMesh->getParentSpaceTLSKind();
    dIASSERT(uiTLSKind == BoxGeom->getParentSpaceTLSKind()); // The colliding spaces must use matching cleanup method
    TrimeshCollidersCache *pccColliderCache = GetTrimeshCollidersCache(uiTLSKind);
    OBBCollider& Collider = pccColliderCache->m_OBBCollider;

    dQueryBTLPotentialCollisionTriangles(Collider, cData, TriMesh, BoxGeom,
        pccColliderCache->m_DefaultBoxCache);

    if (!Collider.GetContactStatus()) {
        // no collision occurred
        return 0;
    }

    // Retrieve data
    int TriCount = Collider.GetNbTouchedPrimitives();
    const int* Triangles = (const int*)Collider.GetTouchedPrimitives();

    if (TriCount != 0){
        if (TriMesh->m_ArrayCallback != null){
            TriMesh->m_ArrayCallback(TriMesh, BoxGeom, Triangles, TriCount);
        }

        // get destination hull position and orientation
        const dMatrix3& mRotMesh=*(const dMatrix3*)dGeomGetRotation(TriMesh);
        const dVector3& vPosMesh=*(const dVector3*)dGeomGetPosition(TriMesh);

        // loop through all intersecting triangles
        for (int i = 0; i < TriCount; i++){
            const int Triint = Triangles[i];
            if (!TriMesh->invokeCallback(BoxGeom, Triint)) continue;

            dVector3 dv[3];
            TriMesh->fetchMeshTriangle(dv, Triint, vPosMesh, mRotMesh);

            bool bFinishSearching;
            cData.TestCollisionForSingleTriangle(Triint, dv, bFinishSearching);

            if (bFinishSearching) {
                break;
            }
        }
    }

    return cData.m_ctContacts;
}
#endif

// GIMPACT version of box to mesh collider
#if dTRIMESH_GIMPACT
int dCollideBTL(dxGeom* g1, dxGeom* BoxGeom, int Flags, dContactGeom* Contacts, int Stride)
{
    dIASSERT (Stride >= (int)sizeof(dContactGeom));
    dIASSERT (g1->type == dTriMeshClass);
    dIASSERT (BoxGeom->type == dBoxClass);
    dIASSERT ((Flags & NUMC_MASK) >= 1);


    dxTriMesh* TriMesh = (dxTriMesh*)g1;

    g1 -> recomputeAABB();
    BoxGeom -> recomputeAABB();


    sTrimeshBoxColliderData cData;
    cData.SetupInitialContext(TriMesh, BoxGeom, Flags, Contacts, Stride);

    //*****at first , collide box aabb******//

    GIM_TRIMESH * ptrimesh = &TriMesh->m_collision_trimesh;
    aabb3f test_aabb(BoxGeom->aabb[0], BoxGeom->aabb[1], BoxGeom->aabb[2], BoxGeom->aabb[3], BoxGeom->aabb[4], BoxGeom->aabb[5]);

    GDYNAMIC_ARRAY collision_result;
    GIM_CREATE_BOXQUERY_LIST(collision_result);

    gim_aabbset_box_collision(&test_aabb, &ptrimesh->m_aabbset , &collision_result);

    if(collision_result.m_size==0)
    {
        GIM_DYNARRAY_DESTROY(collision_result);
        return 0;
    }
    //*****Set globals for box collision******//

    //collide triangles

    GUINT32 * boxesresult = GIM_DYNARRAY_POINTER(GUINT32,collision_result);
    gim_trimesh_locks_work_data(ptrimesh);

    for(unsigned int i=0;i<collision_result.m_size;i++)
    {
        dVector3 dv[3];

        int Triint = boxesresult[i];
        gim_trimesh_get_triangle_vertices(ptrimesh, Triint, dv[0], dv[1], dv[2]);

        bool bFinishSearching;
        cData.TestCollisionForSingleTriangle(Triint, dv, bFinishSearching);

        if (bFinishSearching)
        {
            break;
        }
    }

    gim_trimesh_unlocks_work_data(ptrimesh);
    GIM_DYNARRAY_DESTROY(collision_result);

    return cData.m_ctContacts;
}
#endif



#endif // dTRIMESH_ENABLED