summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/collision_cylinder_trimesh.cpp
blob: fd22e1a7be88f59334bf7e349ce8e20f1da5f7d3 (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
/*************************************************************************
 *                                                                       *
 * 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.                     *
 *                                                                       *
 *************************************************************************/

/*
 *	Cylinder-trimesh collider by Alen Ladavac
 *   Ported to ODE by Nguyen Binh
 */


#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"
#include "util.h"

#if dTRIMESH_ENABLED

#define MAX_REAL	dInfinity
static const int	nCYLINDER_AXIS				= 2;
static const int    nCYLINDER_CIRCLE_SEGMENTS	= 8;
static const int    nMAX_CYLINDER_TRIANGLE_CLIP_POINTS	= 12;

#define OPTIMIZE_CONTACTS 1

// Local contacts data
typedef struct _sLocalContactData
{
    dVector3	vPos;
    dVector3	vNormal;
    dReal		fDepth;
    int			triIndex;
    int			nFlags; // 0 = filtered out, 1 = OK
}sLocalContactData;

struct sCylinderTrimeshColliderData
{
    sCylinderTrimeshColliderData(int flags, int skip): m_iFlags(flags), m_iSkip(skip), m_nContacts(0), m_gLocalContacts(NULL) {}

#ifdef OPTIMIZE_CONTACTS
    void _OptimizeLocalContacts();
#endif
    void _InitCylinderTrimeshData(dxGeom *Cylinder, dxTriMesh *Trimesh);
    int	_ProcessLocalContacts(dContactGeom *contact, dxGeom *Cylinder, dxTriMesh *Trimesh);

    bool _cldTestAxis(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2, 
        dVector3& vAxis, int iAxis, bool bNoFlip = false);
    bool _cldTestCircleToEdgeAxis(
        const dVector3 &v0, const dVector3 &v1, const dVector3 &v2,
        const dVector3 &vCenterPoint, const dVector3 &vCylinderAxis1,
        const dVector3 &vVx0, const dVector3 &vVx1, int iAxis);
    bool _cldTestSeparatingAxes(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2);
    bool _cldClipCylinderEdgeToTriangle(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2);
    void _cldClipCylinderToTriangle(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2);
    void TestOneTriangleVsCylinder(const dVector3 &v0, const dVector3 &v1, const dVector3 &v2, 
        const bool bDoubleSided);
    int TestCollisionForSingleTriangle(int ctContacts0, int Triint, dVector3 dv[3], 
        bool &bOutFinishSearching);

    // cylinder data
    dMatrix3	m_mCylinderRot;
    dQuaternion	m_qCylinderRot;
    dQuaternion	m_qInvCylinderRot;
    dVector3	m_vCylinderPos;
    dVector3	m_vCylinderAxis;
    dReal		m_fCylinderRadius;
    dReal		m_fCylinderSize;
    dVector3	m_avCylinderNormals[nCYLINDER_CIRCLE_SEGMENTS];

    // mesh data
    dQuaternion	m_qTrimeshRot;
    dQuaternion	m_qInvTrimeshRot;
    dMatrix3	m_mTrimeshRot;
    dVector3	m_vTrimeshPos;

    // global collider data
    dVector3	m_vBestPoint;
    dReal		m_fBestDepth;
    dReal		m_fBestCenter;
    dReal		m_fBestrt;
    int			m_iBestAxis;
    dVector3	m_vContactNormal;
    dVector3	m_vNormal;
    dVector3	m_vE0;
    dVector3	m_vE1;
    dVector3	m_vE2;

    // ODE stuff
    int					m_iFlags;
    int					m_iSkip;
    int					m_nContacts;// = 0;
    sLocalContactData*	m_gLocalContacts;
};


#ifdef OPTIMIZE_CONTACTS

// Use to classify contacts to be "near" in position
static const dReal fSameContactPositionEpsilon = REAL(0.0001); // 1e-4
// Use to classify contacts to be "near" in normal direction
static const dReal fSameContactNormalEpsilon = REAL(0.0001); // 1e-4

// If this two contact can be classified as "near"
inline int _IsNearContacts(sLocalContactData& c1,sLocalContactData& c2)
{
    int bPosNear = 0;
    int bSameDir = 0;
    dVector3	vDiff;

    // First check if they are "near" in position
    dVector3Subtract(c1.vPos,c2.vPos,vDiff);
    if (  (dFabs(vDiff[0]) < fSameContactPositionEpsilon)
        &&(dFabs(vDiff[1]) < fSameContactPositionEpsilon)
        &&(dFabs(vDiff[2]) < fSameContactPositionEpsilon))
    {
        bPosNear = 1;
    }

    // Second check if they are "near" in normal direction
    dVector3Subtract(c1.vNormal,c2.vNormal,vDiff);
    if (  (dFabs(vDiff[0]) < fSameContactNormalEpsilon)
        &&(dFabs(vDiff[1]) < fSameContactNormalEpsilon)
        &&(dFabs(vDiff[2]) < fSameContactNormalEpsilon) )
    {
        bSameDir = 1;
    }

    // Will be "near" if position and normal direction are "near"
    return (bPosNear && bSameDir);
}

inline int _IsBetter(sLocalContactData& c1,sLocalContactData& c2)
{
    // The not better will be throw away
    // You can change the selection criteria here
    return (c1.fDepth > c2.fDepth);
}

// iterate through gLocalContacts and filtered out "near contact"
void sCylinderTrimeshColliderData::_OptimizeLocalContacts()
{
    int nContacts = m_nContacts;

    for (int i = 0; i < nContacts-1; i++)
    {
        for (int j = i+1; j < nContacts; j++)
        {
            if (_IsNearContacts(m_gLocalContacts[i],m_gLocalContacts[j]))
            {
                // If they are seem to be the same then filtered 
                // out the least penetrate one
                if (_IsBetter(m_gLocalContacts[j],m_gLocalContacts[i]))
                {
                    m_gLocalContacts[i].nFlags = 0; // filtered 1st contact
                }
                else
                {
                    m_gLocalContacts[j].nFlags = 0; // filtered 2nd contact
                }

                // NOTE
                // There is other way is to add two depth together but
                // it not work so well. Why???
            }
        }
    }
}
#endif // OPTIMIZE_CONTACTS

int	sCylinderTrimeshColliderData::_ProcessLocalContacts(dContactGeom *contact, 
                                                        dxGeom *Cylinder, dxTriMesh *Trimesh)
{
#ifdef OPTIMIZE_CONTACTS
    if (m_nContacts > 1 && !(m_iFlags & CONTACTS_UNIMPORTANT))
    {
        // Can be optimized...
        _OptimizeLocalContacts();
    }
#endif		

    int iContact = 0;
    dContactGeom* Contact = 0;

    int nFinalContact = 0;

    for (iContact = 0; iContact < m_nContacts; iContact ++)
    {
        if (1 == m_gLocalContacts[iContact].nFlags)
        {
            Contact = SAFECONTACT(m_iFlags, contact, nFinalContact, m_iSkip);
            Contact->depth = m_gLocalContacts[iContact].fDepth;
            dVector3Copy(m_gLocalContacts[iContact].vNormal,Contact->normal);
            dVector3Copy(m_gLocalContacts[iContact].vPos,Contact->pos);
            Contact->g1 = Cylinder;
            Contact->g2 = Trimesh;
            Contact->side1 = -1;
            Contact->side2 = m_gLocalContacts[iContact].triIndex;
            dVector3Inv(Contact->normal);

            nFinalContact++;
        }
    }
    // debug
    //if (nFinalContact != m_nContacts)
    //{
    //	printf("[Info] %d contacts generated,%d  filtered.\n",m_nContacts,m_nContacts-nFinalContact);
    //}

    return nFinalContact;
}


bool sCylinderTrimeshColliderData::_cldTestAxis(
    const dVector3 &v0,
    const dVector3 &v1,
    const dVector3 &v2, 
    dVector3& vAxis, 
    int iAxis,
    bool bNoFlip/* = false*/)
{

    // calculate length of separating axis vector
    dReal fL = dVector3Length(vAxis);
    // if not long enough
    if ( fL < REAL(1e-5) )
    {
        // do nothing
        return true;
    }

    // otherwise normalize it
    vAxis[0] /= fL;
    vAxis[1] /= fL;
    vAxis[2] /= fL;

    dReal fdot1 = dVector3Dot(m_vCylinderAxis,vAxis);
    // project capsule on vAxis
    dReal frc;

    if (dFabs(fdot1) > REAL(1.0) ) 
    {
        //		fdot1 = REAL(1.0);
        frc = dFabs(m_fCylinderSize* REAL(0.5));
    }
    else
    {
        frc = dFabs((m_fCylinderSize* REAL(0.5)) * fdot1)
            + m_fCylinderRadius * dSqrt(REAL(1.0)-(fdot1*fdot1));
    }

    dVector3 vV0;
    dVector3Subtract(v0,m_vCylinderPos,vV0);
    dVector3 vV1;
    dVector3Subtract(v1,m_vCylinderPos,vV1);
    dVector3 vV2;
    dVector3Subtract(v2,m_vCylinderPos,vV2);

    // project triangle on vAxis
    dReal afv[3];
    afv[0] = dVector3Dot( vV0 , vAxis );
    afv[1] = dVector3Dot( vV1 , vAxis );
    afv[2] = dVector3Dot( vV2 , vAxis );

    dReal fMin = MAX_REAL;
    dReal fMax = -MAX_REAL;

    // for each vertex 
    for(int i = 0; i < 3; i++) 
    {
        // find minimum
        if (afv[i]<fMin) 
        {
            fMin = afv[i];
        }
        // find maximum
        if (afv[i]>fMax) 
        {
            fMax = afv[i];
        }
    }

    // find capsule's center of interval on axis
    dReal fCenter = (fMin+fMax)* REAL(0.5);
    // calculate triangles halfinterval 
    dReal fTriangleRadius = (fMax-fMin)*REAL(0.5);

    // if they do not overlap, 
    if( dFabs(fCenter) > (frc+fTriangleRadius) ) 
    { 
        // exit, we have no intersection
        return false; 
    }

    // calculate depth 
    dReal fDepth = -(dFabs(fCenter) - (frc + fTriangleRadius ) );

    // if greater then best found so far
    if ( fDepth < m_fBestDepth ) 
    {
        // remember depth
        m_fBestDepth			= fDepth;
        m_fBestCenter		    = fCenter;
        m_fBestrt				= frc;
        dVector3Copy(vAxis,m_vContactNormal);
        m_iBestAxis				= iAxis;

        // flip normal if interval is wrong faced
        if ( fCenter< REAL(0.0) && !bNoFlip) 
        { 
            dVector3Inv(m_vContactNormal);
            m_fBestCenter = -fCenter;
        }
    }

    return true;
}

// intersection test between edge and circle
bool sCylinderTrimeshColliderData::_cldTestCircleToEdgeAxis(
    const dVector3 &v0, const dVector3 &v1, const dVector3 &v2,
    const dVector3 &vCenterPoint, const dVector3 &vCylinderAxis1,
    const dVector3 &vVx0, const dVector3 &vVx1, int iAxis) 
{
    // calculate direction of edge
    dVector3 vkl;
    dVector3Subtract( vVx1 , vVx0 , vkl);
    dNormalize3(vkl);
    // starting point of edge 
    dVector3 vol;
    dVector3Copy(vVx0,vol);

    // calculate angle cosine between cylinder axis and edge
    dReal fdot2 = dVector3Dot(vkl , vCylinderAxis1);

    // if edge is perpendicular to cylinder axis
    if(dFabs(fdot2)<REAL(1e-5))
    {
        // this can't be separating axis, because edge is parallel to circle plane
        return true;
    }

    // find point of intersection between edge line and circle plane
    dVector3 vTemp;
    dVector3Subtract(vCenterPoint,vol,vTemp);
    dReal fdot1 = dVector3Dot(vTemp,vCylinderAxis1);
    dVector3 vpnt;// = vol + vkl * (fdot1/fdot2);
    vpnt[0] = vol[0] + vkl[0] * fdot1/fdot2;
    vpnt[1] = vol[1] + vkl[1] * fdot1/fdot2;
    vpnt[2] = vol[2] + vkl[2] * fdot1/fdot2;

    // find tangent vector on circle with same center (vCenterPoint) that touches point of intersection (vpnt)
    dVector3 vTangent;
    dVector3Subtract(vCenterPoint,vpnt,vTemp);
    dVector3Cross(vTemp,vCylinderAxis1,vTangent);

    // find vector orthogonal both to tangent and edge direction
    dVector3 vAxis;
    dVector3Cross(vTangent,vkl,vAxis);

    // use that vector as separating axis
    return _cldTestAxis( v0, v1, v2, vAxis, iAxis );
}

// helper for less key strokes
// r = ( (v1 - v2) cross v3 ) cross v3
inline void _CalculateAxis(const dVector3& v1,
                           const dVector3& v2,
                           const dVector3& v3,
                           dVector3& r)
{
    dVector3 t1;
    dVector3 t2;

    dVector3Subtract(v1,v2,t1);
    dVector3Cross(t1,v3,t2);
    dVector3Cross(t2,v3,r);
}

bool sCylinderTrimeshColliderData::_cldTestSeparatingAxes(
    const dVector3 &v0,
    const dVector3 &v1,
    const dVector3 &v2) 
{

    // calculate edge vectors
    dVector3Subtract(v1 ,v0 , m_vE0);
    // m_vE1 has been calculated before -> so save some cycles here
    dVector3Subtract(v0 ,v2 , m_vE2);

    // calculate caps centers in absolute space
    dVector3 vCp0;
    vCp0[0] = m_vCylinderPos[0] + m_vCylinderAxis[0]*(m_fCylinderSize* REAL(0.5));
    vCp0[1] = m_vCylinderPos[1] + m_vCylinderAxis[1]*(m_fCylinderSize* REAL(0.5));
    vCp0[2] = m_vCylinderPos[2] + m_vCylinderAxis[2]*(m_fCylinderSize* REAL(0.5));

#if 0
    dVector3 vCp1;
    vCp1[0] = m_vCylinderPos[0] - m_vCylinderAxis[0]*(m_fCylinderSize* REAL(0.5));
    vCp1[1] = m_vCylinderPos[1] - m_vCylinderAxis[1]*(m_fCylinderSize* REAL(0.5));
    vCp1[2] = m_vCylinderPos[2] - m_vCylinderAxis[2]*(m_fCylinderSize* REAL(0.5));
#endif

    // reset best axis
    m_iBestAxis = 0;
    dVector3 vAxis;

    // axis m_vNormal
    //vAxis = -m_vNormal;
    vAxis[0] = -m_vNormal[0];
    vAxis[1] = -m_vNormal[1];
    vAxis[2] = -m_vNormal[2];
    if (!_cldTestAxis(v0, v1, v2, vAxis, 1, true)) 
    { 
        return false; 
    }

    // axis CxE0
    // vAxis = ( m_vCylinderAxis cross m_vE0 );
    dVector3Cross(m_vCylinderAxis, m_vE0,vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 2)) 
    { 
        return false; 
    }

    // axis CxE1
    // vAxis = ( m_vCylinderAxis cross m_vE1 );
    dVector3Cross(m_vCylinderAxis, m_vE1,vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 3)) 
    { 
        return false; 
    }

    // axis CxE2
    // vAxis = ( m_vCylinderAxis cross m_vE2 );
    dVector3Cross(m_vCylinderAxis, m_vE2,vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 4)) 
    { 
        return false; 
    }

    // first vertex on triangle
    // axis ((V0-Cp0) x C) x C
    //vAxis = ( ( v0-vCp0 ) cross m_vCylinderAxis ) cross m_vCylinderAxis;
    _CalculateAxis(v0 , vCp0 , m_vCylinderAxis , vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 11)) 
    { 
        return false; 
    }

    // second vertex on triangle
    // axis ((V1-Cp0) x C) x C
    // vAxis = ( ( v1-vCp0 ) cross m_vCylinderAxis ) cross m_vCylinderAxis;
    _CalculateAxis(v1 , vCp0 , m_vCylinderAxis , vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 12)) 
    { 
        return false; 
    }

    // third vertex on triangle
    // axis ((V2-Cp0) x C) x C
    //vAxis = ( ( v2-vCp0 ) cross m_vCylinderAxis ) cross m_vCylinderAxis;
    _CalculateAxis(v2 , vCp0 , m_vCylinderAxis , vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 13))
    { 
        return false; 
    }

    // test cylinder axis
    // vAxis = m_vCylinderAxis;
    dVector3Copy(m_vCylinderAxis , vAxis);
    if (!_cldTestAxis(v0, v1, v2, vAxis, 14)) 
    { 
        return false; 
    }

    // Test top and bottom circle ring of cylinder for separation
    dVector3 vccATop;
    vccATop[0] = m_vCylinderPos[0] + m_vCylinderAxis[0]*(m_fCylinderSize * REAL(0.5));
    vccATop[1] = m_vCylinderPos[1] + m_vCylinderAxis[1]*(m_fCylinderSize * REAL(0.5));
    vccATop[2] = m_vCylinderPos[2] + m_vCylinderAxis[2]*(m_fCylinderSize * REAL(0.5));

    dVector3 vccABottom;
    vccABottom[0] = m_vCylinderPos[0] - m_vCylinderAxis[0]*(m_fCylinderSize * REAL(0.5));
    vccABottom[1] = m_vCylinderPos[1] - m_vCylinderAxis[1]*(m_fCylinderSize * REAL(0.5));
    vccABottom[2] = m_vCylinderPos[2] - m_vCylinderAxis[2]*(m_fCylinderSize * REAL(0.5));


    if (!_cldTestCircleToEdgeAxis(v0, v1, v2, vccATop, m_vCylinderAxis, v0, v1, 15)) 
    {
        return false;
    }

    if (!_cldTestCircleToEdgeAxis(v0, v1, v2, vccATop, m_vCylinderAxis, v1, v2, 16)) 
    {
        return false;
    }

    if (!_cldTestCircleToEdgeAxis(v0, v1, v2, vccATop, m_vCylinderAxis, v0, v2, 17)) 
    {
        return false;
    }

    if (!_cldTestCircleToEdgeAxis(v0, v1, v2, vccABottom, m_vCylinderAxis, v0, v1, 18)) 
    {
        return false;
    }

    if (!_cldTestCircleToEdgeAxis(v0, v1, v2, vccABottom, m_vCylinderAxis, v1, v2, 19)) 
    {
        return false;
    }

    if (!_cldTestCircleToEdgeAxis(v0, v1, v2, vccABottom, m_vCylinderAxis, v0, v2, 20)) 
    {
        return false;
    }

    return true;
}

bool sCylinderTrimeshColliderData::_cldClipCylinderEdgeToTriangle(
    const dVector3 &v0, const dVector3 &/*v1*/, const dVector3 &/*v2*/)
{
    // translate cylinder
    dReal fTemp = dVector3Dot(m_vCylinderAxis , m_vContactNormal);
    dVector3 vN2;
    vN2[0] = m_vContactNormal[0] - m_vCylinderAxis[0]*fTemp;
    vN2[1] = m_vContactNormal[1] - m_vCylinderAxis[1]*fTemp;
    vN2[2] = m_vContactNormal[2] - m_vCylinderAxis[2]*fTemp;

    fTemp = dVector3Length(vN2);
    if (fTemp < REAL(1e-5))
    {
        return false;
    }

    // Normalize it
    vN2[0] /= fTemp;
    vN2[1] /= fTemp;
    vN2[2] /= fTemp;

    // calculate caps centers in absolute space
    dVector3 vCposTrans;
    vCposTrans[0] = m_vCylinderPos[0] + vN2[0]*m_fCylinderRadius;
    vCposTrans[1] = m_vCylinderPos[1] + vN2[1]*m_fCylinderRadius;
    vCposTrans[2] = m_vCylinderPos[2] + vN2[2]*m_fCylinderRadius;

    dVector3 vCEdgePoint0;
    vCEdgePoint0[0]  = vCposTrans[0] + m_vCylinderAxis[0] * (m_fCylinderSize* REAL(0.5));
    vCEdgePoint0[1]  = vCposTrans[1] + m_vCylinderAxis[1] * (m_fCylinderSize* REAL(0.5));
    vCEdgePoint0[2]  = vCposTrans[2] + m_vCylinderAxis[2] * (m_fCylinderSize* REAL(0.5));

    dVector3 vCEdgePoint1;
    vCEdgePoint1[0]  = vCposTrans[0] - m_vCylinderAxis[0] * (m_fCylinderSize* REAL(0.5));
    vCEdgePoint1[1]  = vCposTrans[1] - m_vCylinderAxis[1] * (m_fCylinderSize* REAL(0.5));
    vCEdgePoint1[2]  = vCposTrans[2] - m_vCylinderAxis[2] * (m_fCylinderSize* REAL(0.5));

    // transform cylinder edge points into triangle space
    vCEdgePoint0[0] -= v0[0];
    vCEdgePoint0[1] -= v0[1];
    vCEdgePoint0[2] -= v0[2];

    vCEdgePoint1[0] -= v0[0];
    vCEdgePoint1[1] -= v0[1];
    vCEdgePoint1[2] -= v0[2];

    dVector4 plPlane;
    dVector3 vPlaneNormal;

    // triangle plane
    //plPlane = Plane4f( -m_vNormal, 0);
    vPlaneNormal[0] = -m_vNormal[0];
    vPlaneNormal[1] = -m_vNormal[1];
    vPlaneNormal[2] = -m_vNormal[2];
    dConstructPlane(vPlaneNormal,REAL(0.0),plPlane);
    if(!dClipEdgeToPlane( vCEdgePoint0, vCEdgePoint1, plPlane )) 
    { 
        return false; 
    }

    // plane with edge 0
    //plPlane = Plane4f( ( m_vNormal cross m_vE0 ), REAL(1e-5));
    dVector3Cross(m_vNormal,m_vE0,vPlaneNormal);
    dConstructPlane(vPlaneNormal,REAL(1e-5),plPlane);
    if(!dClipEdgeToPlane( vCEdgePoint0, vCEdgePoint1, plPlane )) 
    { 
        return false; 
    }

    // plane with edge 1
    //dVector3 vTemp = ( m_vNormal cross m_vE1 );
    dVector3Cross(m_vNormal,m_vE1,vPlaneNormal);
    fTemp = dVector3Dot(m_vE0 , vPlaneNormal) - REAL(1e-5);
    //plPlane = Plane4f( vTemp, -(( m_vE0 dot vTemp )-REAL(1e-5)));
    dConstructPlane(vPlaneNormal,-fTemp,plPlane);
    if(!dClipEdgeToPlane( vCEdgePoint0, vCEdgePoint1, plPlane )) 
    {
        return false;
    }

    // plane with edge 2
    // plPlane = Plane4f( ( m_vNormal cross m_vE2 ), REAL(1e-5));
    dVector3Cross(m_vNormal,m_vE2,vPlaneNormal);
    dConstructPlane(vPlaneNormal,REAL(1e-5),plPlane);
    if(!dClipEdgeToPlane( vCEdgePoint0, vCEdgePoint1, plPlane )) 
    { 
        return false; 
    }

    // return capsule edge points into absolute space
    vCEdgePoint0[0] += v0[0];
    vCEdgePoint0[1] += v0[1];
    vCEdgePoint0[2] += v0[2];

    vCEdgePoint1[0] += v0[0];
    vCEdgePoint1[1] += v0[1];
    vCEdgePoint1[2] += v0[2];

    // calculate depths for both contact points
    dVector3 vTemp;
    dVector3Subtract(vCEdgePoint0,m_vCylinderPos, vTemp);
    dReal fRestDepth0 = -dVector3Dot(vTemp,m_vContactNormal) + m_fBestrt;
    dVector3Subtract(vCEdgePoint1,m_vCylinderPos, vTemp);
    dReal fRestDepth1 = -dVector3Dot(vTemp,m_vContactNormal) + m_fBestrt;

    dReal fDepth0 = m_fBestDepth - (fRestDepth0);
    dReal fDepth1 = m_fBestDepth - (fRestDepth1);

    // clamp depths to zero
    if(fDepth0 < REAL(0.0) ) 
    {
        fDepth0 = REAL(0.0);
    }

    if(fDepth1<REAL(0.0)) 
    {
        fDepth1 = REAL(0.0);
    }

    // Generate contact 0
    {
        m_gLocalContacts[m_nContacts].fDepth = fDepth0;
        dVector3Copy(m_vContactNormal,m_gLocalContacts[m_nContacts].vNormal);
        dVector3Copy(vCEdgePoint0,m_gLocalContacts[m_nContacts].vPos);
        m_gLocalContacts[m_nContacts].nFlags = 1;
        m_nContacts++;
        if(m_nContacts >= (m_iFlags & NUMC_MASK)) 
            return true;
    }

    // Generate contact 1
    {
        // generate contacts
        m_gLocalContacts[m_nContacts].fDepth = fDepth1;
        dVector3Copy(m_vContactNormal,m_gLocalContacts[m_nContacts].vNormal);
        dVector3Copy(vCEdgePoint1,m_gLocalContacts[m_nContacts].vPos);
        m_gLocalContacts[m_nContacts].nFlags = 1;
        m_nContacts++;		
    }

    return true;
}

void sCylinderTrimeshColliderData::_cldClipCylinderToTriangle(
    const dVector3 &v0, const dVector3 &v1, const dVector3 &v2)
{
    int i = 0;
    dVector3 avPoints[3];
    dVector3 avTempArray1[nMAX_CYLINDER_TRIANGLE_CLIP_POINTS];
    dVector3 avTempArray2[nMAX_CYLINDER_TRIANGLE_CLIP_POINTS];

    dSetZero(&avTempArray1[0][0],nMAX_CYLINDER_TRIANGLE_CLIP_POINTS * 4);
    dSetZero(&avTempArray2[0][0],nMAX_CYLINDER_TRIANGLE_CLIP_POINTS * 4);

    // setup array of triangle vertices
    dVector3Copy(v0,avPoints[0]);
    dVector3Copy(v1,avPoints[1]);
    dVector3Copy(v2,avPoints[2]);

    dVector3 vCylinderCirclePos, vCylinderCircleNormal_Rel;
    dSetZero(vCylinderCircleNormal_Rel,4);
    // check which circle from cylinder we take for clipping
    if ( dVector3Dot(m_vCylinderAxis , m_vContactNormal) > REAL(0.0)) 
    {
        // get top circle
        vCylinderCirclePos[0] = m_vCylinderPos[0] + m_vCylinderAxis[0]*(m_fCylinderSize*REAL(0.5));
        vCylinderCirclePos[1] = m_vCylinderPos[1] + m_vCylinderAxis[1]*(m_fCylinderSize*REAL(0.5));
        vCylinderCirclePos[2] = m_vCylinderPos[2] + m_vCylinderAxis[2]*(m_fCylinderSize*REAL(0.5));

        vCylinderCircleNormal_Rel[nCYLINDER_AXIS] = REAL(-1.0);
    } 
    else 
    {
        // get bottom circle
        vCylinderCirclePos[0] = m_vCylinderPos[0] - m_vCylinderAxis[0]*(m_fCylinderSize*REAL(0.5));
        vCylinderCirclePos[1] = m_vCylinderPos[1] - m_vCylinderAxis[1]*(m_fCylinderSize*REAL(0.5));
        vCylinderCirclePos[2] = m_vCylinderPos[2] - m_vCylinderAxis[2]*(m_fCylinderSize*REAL(0.5));

        vCylinderCircleNormal_Rel[nCYLINDER_AXIS] = REAL(1.0);
    }

    dVector3 vTemp;
    dQuatInv(m_qCylinderRot , m_qInvCylinderRot);
    // transform triangle points to space of cylinder circle
    for(i=0; i<3; i++) 
    {
        dVector3Subtract(avPoints[i] , vCylinderCirclePos , vTemp);
        dQuatTransform(m_qInvCylinderRot,vTemp,avPoints[i]);
    }

    int iTmpCounter1 = 0;
    int iTmpCounter2 = 0;
    dVector4 plPlane;

    // plane of cylinder that contains circle for intersection
    //plPlane = Plane4f( vCylinderCircleNormal_Rel, 0.0f );
    dConstructPlane(vCylinderCircleNormal_Rel,REAL(0.0),plPlane);
    dClipPolyToPlane(avPoints, 3, avTempArray1, iTmpCounter1, plPlane);

    // Body of base circle of Cylinder
    int nCircleSegment = 0;
    for (nCircleSegment = 0; nCircleSegment < nCYLINDER_CIRCLE_SEGMENTS; nCircleSegment++)
    {
        dConstructPlane(m_avCylinderNormals[nCircleSegment],m_fCylinderRadius,plPlane);

        if (0 == (nCircleSegment % 2))
        {
            dClipPolyToPlane( avTempArray1 , iTmpCounter1 , avTempArray2, iTmpCounter2, plPlane);
        }
        else
        {
            dClipPolyToPlane( avTempArray2, iTmpCounter2, avTempArray1 , iTmpCounter1 , plPlane );
        }

        dIASSERT( iTmpCounter1 >= 0 && iTmpCounter1 <= nMAX_CYLINDER_TRIANGLE_CLIP_POINTS );
        dIASSERT( iTmpCounter2 >= 0 && iTmpCounter2 <= nMAX_CYLINDER_TRIANGLE_CLIP_POINTS );
    }

    // back transform clipped points to absolute space
    dReal ftmpdot;	
    dReal fTempDepth;
    dVector3 vPoint;

    if (nCircleSegment %2)
    {
        for( i=0; i<iTmpCounter2; i++)
        {
            dQuatTransform(m_qCylinderRot,avTempArray2[i], vPoint);
            vPoint[0] += vCylinderCirclePos[0];
            vPoint[1] += vCylinderCirclePos[1];
            vPoint[2] += vCylinderCirclePos[2];

            dVector3Subtract(vPoint,m_vCylinderPos,vTemp);
            ftmpdot	 = dFabs(dVector3Dot(vTemp, m_vContactNormal));
            fTempDepth = m_fBestrt - ftmpdot;
            // Depth must be positive
            if (fTempDepth > REAL(0.0))
            {
                m_gLocalContacts[m_nContacts].fDepth = fTempDepth;
                dVector3Copy(m_vContactNormal,m_gLocalContacts[m_nContacts].vNormal);
                dVector3Copy(vPoint,m_gLocalContacts[m_nContacts].vPos);
                m_gLocalContacts[m_nContacts].nFlags = 1;
                m_nContacts++;
                if(m_nContacts >= (m_iFlags & NUMC_MASK)) 
                    return;;
            }
        }
    }
    else
    {
        for( i=0; i<iTmpCounter1; i++)
        {
            dQuatTransform(m_qCylinderRot,avTempArray1[i], vPoint);
            vPoint[0] += vCylinderCirclePos[0];
            vPoint[1] += vCylinderCirclePos[1];
            vPoint[2] += vCylinderCirclePos[2];

            dVector3Subtract(vPoint,m_vCylinderPos,vTemp);
            ftmpdot	 = dFabs(dVector3Dot(vTemp, m_vContactNormal));
            fTempDepth = m_fBestrt - ftmpdot;
            // Depth must be positive
            if (fTempDepth > REAL(0.0))
            {
                m_gLocalContacts[m_nContacts].fDepth = fTempDepth;
                dVector3Copy(m_vContactNormal,m_gLocalContacts[m_nContacts].vNormal);
                dVector3Copy(vPoint,m_gLocalContacts[m_nContacts].vPos);
                m_gLocalContacts[m_nContacts].nFlags = 1;
                m_nContacts++;
                if(m_nContacts >= (m_iFlags & NUMC_MASK)) 
                    return;;
            }
        }
    }
}

void sCylinderTrimeshColliderData::TestOneTriangleVsCylinder(
    const dVector3 &v0, 
    const dVector3 &v1, 
    const dVector3 &v2, 
    const bool bDoubleSided)
{
    // calculate triangle normal
    dVector3Subtract( v2 , v1 , m_vE1);
    dVector3 vTemp;
    dVector3Subtract( v0 , v1 ,vTemp);
    dVector3Cross(m_vE1 , vTemp , m_vNormal );

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

    // create plane from triangle
    //Plane4f plTrianglePlane = Plane4f( vPolyNormal, v0 ); 
    dReal plDistance = -dVector3Dot(v0, m_vNormal);
    dVector4 plTrianglePlane;
    dConstructPlane( m_vNormal,plDistance,plTrianglePlane);

    // calculate sphere distance to plane
    dReal fDistanceCylinderCenterToPlane = dPointPlaneDistance(m_vCylinderPos , plTrianglePlane);

    // Sphere must be over positive side of triangle
    if(fDistanceCylinderCenterToPlane < 0 && !bDoubleSided) 
    {
        // if not don't generate contacts
        return;
    }

    dVector3 vPnt0;
    dVector3 vPnt1;
    dVector3 vPnt2;

    if (fDistanceCylinderCenterToPlane < REAL(0.0) )
    {
        // flip it
        dVector3Copy(v0 , vPnt0);
        dVector3Copy(v1 , vPnt2);
        dVector3Copy(v2 , vPnt1);
    }
    else
    {
        dVector3Copy(v0 , vPnt0);
        dVector3Copy(v1 , vPnt1);
        dVector3Copy(v2 , vPnt2);
    }

    m_fBestDepth = MAX_REAL;

    // do intersection test and find best separating axis
    if(!_cldTestSeparatingAxes(vPnt0, vPnt1, vPnt2) ) 
    {
        // if not found do nothing
        return;
    }

    // if best separation axis is not found
    if ( m_iBestAxis == 0 ) 
    {
        // this should not happen (the function should have already returned in this case)
        dIASSERT(false);
        // do nothing
        return;
    }

    dReal fdot = dVector3Dot( m_vContactNormal , m_vCylinderAxis );

    // choose which clipping method are we going to apply
    if (dFabs(fdot) < REAL(0.9) ) 
    {
        if (!_cldClipCylinderEdgeToTriangle(vPnt0, vPnt1, vPnt2)) 
        {
            return;
        }
    }
    else 
    {
        _cldClipCylinderToTriangle(vPnt0, vPnt1, vPnt2);
    }
}

void sCylinderTrimeshColliderData::_InitCylinderTrimeshData(dxGeom *Cylinder, dxTriMesh *Trimesh)
{
    // get cylinder information
    // Rotation
    const dReal* pRotCyc = dGeomGetRotation(Cylinder); 
    dMatrix3Copy(pRotCyc,m_mCylinderRot);
    dGeomGetQuaternion(Cylinder,m_qCylinderRot);

    // Position
    const dVector3* pPosCyc = (const dVector3*)dGeomGetPosition(Cylinder);
    dVector3Copy(*pPosCyc,m_vCylinderPos);
    // Cylinder axis
    dMat3GetCol(m_mCylinderRot,nCYLINDER_AXIS,m_vCylinderAxis);
    // get cylinder radius and size
    dGeomCylinderGetParams(Cylinder,&m_fCylinderRadius,&m_fCylinderSize);

    // get trimesh position and orientation
    const dReal* pRotTris = dGeomGetRotation(Trimesh); 
    dMatrix3Copy(pRotTris,m_mTrimeshRot);
    dGeomGetQuaternion(Trimesh,m_qTrimeshRot);

    // Position
    const dVector3* pPosTris = (const dVector3*)dGeomGetPosition(Trimesh);
    dVector3Copy(*pPosTris,m_vTrimeshPos);


    // calculate basic angle for 8-gon
    dReal fAngle = (dReal) (M_PI / nCYLINDER_CIRCLE_SEGMENTS);
    // calculate angle increment
    dReal fAngleIncrement = fAngle*REAL(2.0); 

    // calculate plane normals
    // axis dependant code
    for(int i=0; i<nCYLINDER_CIRCLE_SEGMENTS; i++) 
    {
        m_avCylinderNormals[i][0] = -dCos(fAngle);
        m_avCylinderNormals[i][1] = -dSin(fAngle);
        m_avCylinderNormals[i][2] = REAL(0.0);

        fAngle += fAngleIncrement;
    }

    dSetZero(m_vBestPoint,4);
    // reset best depth
    m_fBestCenter = REAL(0.0);	
}

int sCylinderTrimeshColliderData::TestCollisionForSingleTriangle(int ctContacts0, 
                                                                 int Triint, dVector3 dv[3], bool &bOutFinishSearching)
{
    // test this triangle
    TestOneTriangleVsCylinder(dv[0],dv[1],dv[2], false);

    // fill-in tri index for generated contacts
    for (; ctContacts0<m_nContacts; ctContacts0++)
        m_gLocalContacts[ctContacts0].triIndex = Triint;

    // Putting "break" at the end of loop prevents unnecessary checks on first pass and "continue"
    bOutFinishSearching = (m_nContacts >= (m_iFlags & NUMC_MASK));

    return ctContacts0;
}

// OPCODE version of cylinder to mesh collider
#if dTRIMESH_OPCODE
static void dQueryCTLPotentialCollisionTriangles(OBBCollider &Collider, 
                                                 sCylinderTrimeshColliderData &cData, dxGeom *Cylinder, dxTriMesh *Trimesh,
                                                 OBBCache &BoxCache)
{
    Matrix4x4 MeshMatrix;
    const dVector3 vZeroVector3 = { REAL(0.0), };
    MakeMatrix(vZeroVector3, cData.m_mTrimeshRot, MeshMatrix);

    const dVector3 &vCylinderPos = cData.m_vCylinderPos;
    const dMatrix3 &mCylinderRot = cData.m_mCylinderRot;

    dVector3 vCylinderOffsetPos;
    dSubtractVectors3(vCylinderOffsetPos, vCylinderPos, cData.m_vTrimeshPos);

    const dReal fCylinderRadius = cData.m_fCylinderRadius, fCylinderHalfAxis = cData.m_fCylinderSize * REAL(0.5);

    OBB obbCylinder;
    obbCylinder.mCenter.Set(vCylinderOffsetPos[0], vCylinderOffsetPos[1], vCylinderOffsetPos[2]);
    obbCylinder.mExtents.Set(
        0 == nCYLINDER_AXIS ? fCylinderHalfAxis : fCylinderRadius,
        1 == nCYLINDER_AXIS ? fCylinderHalfAxis : fCylinderRadius,
        2 == nCYLINDER_AXIS ? fCylinderHalfAxis : fCylinderRadius);
    obbCylinder.mRot.Set(
        mCylinderRot[0], mCylinderRot[4], mCylinderRot[8],
        mCylinderRot[1], mCylinderRot[5], mCylinderRot[9],
        mCylinderRot[2], mCylinderRot[6], mCylinderRot[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 == Cylinder)
            {
                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 = Cylinder;
            BoxTC->FatCoeff = REAL(1.0);
        }

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

int dCollideCylinderTrimesh(dxGeom *o1, dxGeom *o2, int flags, dContactGeom *contact, int skip)
{
    dIASSERT( skip >= (int)sizeof( dContactGeom ) );
    dIASSERT( o1->type == dCylinderClass );
    dIASSERT( o2->type == dTriMeshClass );
    dIASSERT ((flags & NUMC_MASK) >= 1);

    int nContactCount = 0;

    dxGeom *Cylinder = o1;
    dxTriMesh *Trimesh = (dxTriMesh *)o2;

    // Main data holder
    sCylinderTrimeshColliderData cData(flags, skip);
    cData._InitCylinderTrimeshData(Cylinder, Trimesh);

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

    dQueryCTLPotentialCollisionTriangles(Collider, cData, Cylinder, Trimesh, pccColliderCache->m_DefaultBoxCache);

    // Retrieve data
    int TriCount = Collider.GetNbTouchedPrimitives();

    if (TriCount != 0)
    {
        const int* Triangles = (const int*)Collider.GetTouchedPrimitives();

        if (Trimesh->m_ArrayCallback != NULL)
        {
            Trimesh->m_ArrayCallback(Trimesh, Cylinder, Triangles, TriCount);
        }

        // allocate buffer for local contacts on stack
        cData.m_gLocalContacts = (sLocalContactData*)dALLOCA16(sizeof(sLocalContactData)*(cData.m_iFlags & NUMC_MASK));

        int ctContacts0 = 0;

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


            dVector3 dv[3];
            Trimesh->fetchMeshTriangle(dv, Triint, cData.m_vTrimeshPos, cData.m_mTrimeshRot);

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

            if (bFinishSearching) 
            {
                break;
            }
        }

        if (cData.m_nContacts != 0)
        {
            nContactCount = cData._ProcessLocalContacts(contact, Cylinder, Trimesh);
        }
    }

    return nContactCount;
}
#endif

// GIMPACT version of cylinder to mesh collider
#if dTRIMESH_GIMPACT
int dCollideCylinderTrimesh(dxGeom *o1, dxGeom *o2, int flags, dContactGeom *contact, int skip)
{
    dIASSERT( skip >= (int)sizeof( dContactGeom ) );
    dIASSERT( o1->type == dCylinderClass );
    dIASSERT( o2->type == dTriMeshClass );
    dIASSERT ((flags & NUMC_MASK) >= 1);

    int nContactCount = 0;

    dxGeom *Cylinder = o1;
    dxTriMesh *Trimesh = (dxTriMesh *)o2;

    // Main data holder
    sCylinderTrimeshColliderData cData(flags, skip);
    cData._InitCylinderTrimeshData(Cylinder, Trimesh);

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

    aabb3f test_aabb(o1->aabb[0], o1->aabb[1], o1->aabb[2], o1->aabb[3], o1->aabb[4], o1->aabb[5]);


    GDYNAMIC_ARRAY collision_result;
    GIM_CREATE_BOXQUERY_LIST(collision_result);

    gim_aabbset_box_collision(&test_aabb, &Trimesh->m_collision_trimesh.m_aabbset , &collision_result);

    if (collision_result.m_size != 0)
    {
        //*****Set globals for box collision******//

        int ctContacts0 = 0;
        cData.m_gLocalContacts = (sLocalContactData*)dALLOCA16(sizeof(sLocalContactData)*(cData.m_iFlags & NUMC_MASK));

        GUINT32 * boxesresult = GIM_DYNARRAY_POINTER(GUINT32,collision_result);
        GIM_TRIMESH * ptrimesh = &Trimesh->m_collision_trimesh;

        gim_trimesh_locks_work_data(ptrimesh);

        for(unsigned int i=0;i<collision_result.m_size;i++)
        {
            const int Triint = boxesresult[i];

            dVector3 dv[3];
            gim_trimesh_get_triangle_vertices(ptrimesh, Triint, dv[0], dv[1], dv[2]);

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

            if (bFinishSearching) 
            {
                break;
            }
        }

        gim_trimesh_unlocks_work_data(ptrimesh);

        if (cData.m_nContacts != 0)
        {
            nContactCount = cData._ProcessLocalContacts(contact, Cylinder, Trimesh);
        }
    }

    GIM_DYNARRAY_DESTROY(collision_result);

    return nContactCount;
}
#endif

#endif // dTRIMESH_ENABLED