summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/collision_trimesh_internal_impl.h
blob: be41ff5d0269783af36bbd49a142ae55c2ae39e2 (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
/*************************************************************************
 *                                                                       *
 * 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.                     *
 *                                                                       *
 *************************************************************************/

// TriMesh base template method implementations by Oleh Derevenko (C) 2016-2019


#ifndef _ODE_COLLISION_TRIMESH_INTERNAL_IMPL_H_
#define _ODE_COLLISION_TRIMESH_INTERNAL_IMPL_H_


#include "collision_trimesh_internal.h"


#if dTRIMESH_ENABLED


template<typename tcoordfloat, typename tindexint>
/*static */
void dxTriDataBase::retrieveTriangleVertexPoints(dVector3 out_Points[dMTV__MAX], unsigned triangleIndex,
    const tcoordfloat *vertexInstances, int vertexStride, const tindexint *triangleVertexIndices, int triangleStride)
{
    const tindexint *triangleIndicesOfInterest = (const tindexint *)((uint8 *)triangleVertexIndices + (sizeint)triangleIndex * triangleStride);
    for (unsigned trianglePoint = dMTV__MIN; trianglePoint != dMTV__MAX; ++trianglePoint)
    {
        unsigned vertexIndex = triangleIndicesOfInterest[trianglePoint];
        tcoordfloat *pointVertex = (tcoordfloat *)((uint8 *)vertexInstances + (sizeint)vertexIndex * vertexStride);
        dAssignVector3(out_Points[trianglePoint], (dReal)pointVertex[dSA_X], (dReal)pointVertex[dSA_Y], (dReal)pointVertex[dSA_Z]);
        dSASSERT(dSA_X == 0);
        dSASSERT(dSA_Y == 1);
        dSASSERT(dSA_Z == 2);
    }
}


template<class TMeshDataAccessor>
/*static */
void dxTriDataBase::meaningfulPreprocess_SetupEdgeRecords(EdgeRecord *edges, sizeint numEdges, const TMeshDataAccessor &dataAccessor)
{
    unsigned vertexIndices[dMTV__MAX];
    // Make a list of every edge in the mesh
    unsigned triangleIdx = 0;
    for (sizeint edgeIdx = 0; edgeIdx != numEdges; ++triangleIdx, edgeIdx += dMTV__MAX)
    {
        dataAccessor.getTriangleVertexIndices(vertexIndices, triangleIdx);
        edges[edgeIdx + dMTV_FIRST].setupEdge(dMTV_FIRST, triangleIdx, vertexIndices);
        edges[edgeIdx + dMTV_SECOND].setupEdge(dMTV_SECOND, triangleIdx, vertexIndices);
        edges[edgeIdx + dMTV_THIRD].setupEdge(dMTV_THIRD, triangleIdx, vertexIndices);
    }
}

template<class TMeshDataAccessor>
/*static */
void dxTriDataBase::meaningfulPreprocess_buildEdgeFlags(uint8 *useFlags/*=NULL*/, IFaceAngleStorageControl *faceAngles/*=NULL*/, 
    EdgeRecord *edges, sizeint numEdges, VertexRecord *vertices, 
    const dReal *externalNormals/*=NULL*/, const TMeshDataAccessor &dataAccessor)
{
    dIASSERT(useFlags != NULL || faceAngles != NULL);
    dIASSERT(numEdges != 0);

    const bool negativeAnglesStored = faceAngles != NULL && faceAngles->areNegativeAnglesStored();

    // Go through the sorted list of edges and flag all the edges and vertices that we need to use
    EdgeRecord *const lastEdge = edges + (numEdges - 1);
    for (EdgeRecord *currEdge = edges; ; ++currEdge)
    {
        // Handle the last edge separately to have an optimizer friendly loop
        if (currEdge >= lastEdge)
        {
            // This is a boundary edge
            if (currEdge == lastEdge)
            {
                if (faceAngles != NULL)
                {
                    buildBoundaryEdgeAngle(faceAngles, currEdge);
                }

                if (useFlags != NULL)
                {
                    // For the last element EdgeRecord::kAbsVertexUsed assignment can be skipped as noone is going to need it any more
                    useFlags[currEdge[0].m_triIdx] |= ((edges[currEdge[0].m_vertIdx1].m_absVertexFlags & EdgeRecord::AVF_VERTEX_USED) == 0 ? currEdge[0].m_vert1Flags : 0) 
                        | ((edges[currEdge[0].m_vertIdx2].m_absVertexFlags & EdgeRecord::AVF_VERTEX_USED) == 0 ? currEdge[0].m_vert2Flags : 0)
                        | currEdge[0].m_edgeFlags;
                }
            }

            break;
        }

        unsigned vertIdx1 = currEdge[0].m_vertIdx1;
        unsigned vertIdx2 = currEdge[0].m_vertIdx2;

        if (vertIdx2 == currEdge[1].m_vertIdx2 // Check second vertex first as it is more likely to change taking the sorting rules into account
            && vertIdx1 == currEdge[1].m_vertIdx1)
        {
            // We let the dot threshold for concavity get slightly negative to allow for rounding errors
            const float kConcaveThreshold = 0.000001f;

            const dVector3 *pSecondTriangleEdgeToUse = NULL, *pFirstTriangleToUse = NULL;
            dVector3 secondTriangleMatchingEdge;
            dVector3 firstTriangle[dMTV__MAX];
            dVector3 secondOppositeVertexSegment, triangleNormal;
            dReal lengthSquareProduct, secondOppositeSegmentLengthSquare;

            // Calculate orthogonal vector from the matching edge of the second triangle to its opposite point
            {
                dVector3 secondTriangle[dMTV__MAX];
                dataAccessor.getTriangleVertexPoints(secondTriangle, currEdge[1].m_triIdx);

                // Get the vertex opposite this edge in the second triangle
                dMeshTriangleVertex secondOppositeVertex = currEdge[1].getOppositeVertexIndex();
                dMeshTriangleVertex secondEdgeStart = secondOppositeVertex + 1 != dMTV__MAX ? (dMeshTriangleVertex)(secondOppositeVertex + 1) : dMTV__MIN;
                dMeshTriangleVertex secondEdgeEnd = (dMeshTriangleVertex)(dMTV_FIRST + dMTV_SECOND + dMTV_THIRD - secondEdgeStart - secondOppositeVertex);

                dSubtractVectors3(secondTriangleMatchingEdge, secondTriangle[secondEdgeEnd], secondTriangle[secondEdgeStart]);

                if (dSafeNormalize3(secondTriangleMatchingEdge))
                {
                    pSecondTriangleEdgeToUse = &secondTriangleMatchingEdge;

                    dVector3 secondTriangleOppositeEdge;
                    dSubtractVectors3(secondTriangleOppositeEdge, secondTriangle[secondOppositeVertex], secondTriangle[secondEdgeStart]);
                    dReal dProjectionLength = dCalcVectorDot3(secondTriangleOppositeEdge, secondTriangleMatchingEdge);
                    dAddVectorScaledVector3(secondOppositeVertexSegment, secondTriangleOppositeEdge, secondTriangleMatchingEdge, -dProjectionLength);
                }
                else
                {
                    dSubtractVectors3(secondOppositeVertexSegment, secondTriangle[secondOppositeVertex], secondTriangle[secondEdgeStart]);
                }

                secondOppositeSegmentLengthSquare = dCalcVectorLengthSquare3(secondOppositeVertexSegment);
            }

            // Either calculate the normal from triangle vertices...
            if (externalNormals == NULL)
            {
                // Get the normal of the first triangle
                dataAccessor.getTriangleVertexPoints(firstTriangle, currEdge[0].m_triIdx);
                pFirstTriangleToUse = &firstTriangle[dMTV__MIN];

                dVector3 firstEdge, secondEdge;
                dSubtractVectors3(secondEdge, firstTriangle[dMTV_THIRD], firstTriangle[dMTV_SECOND]);
                dSubtractVectors3(firstEdge, firstTriangle[dMTV_FIRST], firstTriangle[dMTV_SECOND]);
                dCalcVectorCross3(triangleNormal, secondEdge, firstEdge);
                dReal normalLengthSuqare = dCalcVectorLengthSquare3(triangleNormal);
                lengthSquareProduct = secondOppositeSegmentLengthSquare * normalLengthSuqare;
            }
            // ...or use the externally supplied normals
            else
            {
                const dReal *pTriangleExternalNormal = externalNormals + currEdge[0].m_triIdx * dSA__MAX;
                dAssignVector3(triangleNormal, pTriangleExternalNormal[dSA_X], pTriangleExternalNormal[dSA_Y], pTriangleExternalNormal[dSA_Z]);
                // normalLengthSuqare = REAL(1.0);
                dUASSERT(dFabs(dCalcVectorLengthSquare3(triangleNormal) - REAL(1.0)) < REAL(0.25) * kConcaveThreshold * kConcaveThreshold, "Mesh triangle normals must be normalized");

                lengthSquareProduct = secondOppositeSegmentLengthSquare/* * normalLengthSuqare*/;
            }

            dReal normalSegmentDot = dCalcVectorDot3(triangleNormal, secondOppositeVertexSegment);

            // This is a concave edge, leave it for the next pass
            // OD: This is the "dot >= kConcaveThresh" check, but since the vectros were not normalized to save on roots and divisions,
            // the check against zero is performed first and then the dot product is squared and compared against the threshold multiplied by lengths' squares
            // OD: Originally, there was dot > -kConcaveThresh check, but this does not seem to be a good idea
            // as it can mark all edges on potentially large (nearly) flat surfaces concave.
            if (normalSegmentDot > REAL(0.0) && normalSegmentDot * normalSegmentDot >= kConcaveThreshold * kConcaveThreshold * lengthSquareProduct)
            {
                if (faceAngles != NULL)
                {
                    buildConcaveEdgeAngle(faceAngles, negativeAnglesStored, currEdge, normalSegmentDot, lengthSquareProduct,
                        triangleNormal, secondOppositeVertexSegment,
                        pSecondTriangleEdgeToUse, pFirstTriangleToUse, dataAccessor);
                }

                if (useFlags != NULL)
                {
                    // Mark the vertices of a concave edge to prevent their use
                    unsigned absVertexFlags1 = edges[vertIdx1].m_absVertexFlags;
                    edges[vertIdx1].m_absVertexFlags |= absVertexFlags1 | EdgeRecord::AVF_VERTEX_HAS_CONCAVE_EDGE | EdgeRecord::AVF_VERTEX_USED;

                    if ((absVertexFlags1 & (EdgeRecord::AVF_VERTEX_HAS_CONCAVE_EDGE | EdgeRecord::AVF_VERTEX_USED)) == EdgeRecord::AVF_VERTEX_USED)
                    {
                        // If the vertex was already used from other triangles but then discovered 
                        // to have a concave edge, unmark the previous use
                        unsigned usedFromEdgeIndex = vertices[vertIdx1].m_UsedFromEdgeIndex;
                        const EdgeRecord *usedFromEdge = edges + usedFromEdgeIndex;
                        unsigned usedInTriangleIndex = usedFromEdge->m_triIdx;
                        uint8 usedVertFlags = usedFromEdge->m_vertIdx1 == vertIdx1 ? usedFromEdge->m_vert1Flags : usedFromEdge->m_vert2Flags;
                        useFlags[usedInTriangleIndex] ^= usedVertFlags;
                        dIASSERT((useFlags[usedInTriangleIndex] & usedVertFlags) == 0);
                    }

                    unsigned absVertexFlags2 = edges[vertIdx2].m_absVertexFlags;
                    edges[vertIdx2].m_absVertexFlags = absVertexFlags2 | EdgeRecord::AVF_VERTEX_HAS_CONCAVE_EDGE | EdgeRecord::AVF_VERTEX_USED;

                    if ((absVertexFlags2 & (EdgeRecord::AVF_VERTEX_HAS_CONCAVE_EDGE | EdgeRecord::AVF_VERTEX_USED)) == EdgeRecord::AVF_VERTEX_USED)
                    {
                        // Similarly unmark the possible previous use of the edge's second vertex
                        unsigned usedFromEdgeIndex = vertices[vertIdx2].m_UsedFromEdgeIndex;
                        const EdgeRecord *usedFromEdge = edges + usedFromEdgeIndex;
                        unsigned usedInTriangleIndex = usedFromEdge->m_triIdx;
                        uint8 usedVertFlags = usedFromEdge->m_vertIdx1 == vertIdx2 ? usedFromEdge->m_vert1Flags : usedFromEdge->m_vert2Flags;
                        useFlags[usedInTriangleIndex] ^= usedVertFlags;
                        dIASSERT((useFlags[usedInTriangleIndex] & usedVertFlags) == 0);
                    }
                }
            }
            // If this is a convex edge, mark its vertices and edge as used
            else
            {
                if (faceAngles != NULL)
                {
                    buildConvexEdgeAngle(faceAngles, currEdge, normalSegmentDot, lengthSquareProduct,
                        triangleNormal, secondOppositeVertexSegment,
                        pSecondTriangleEdgeToUse, pFirstTriangleToUse, dataAccessor);
                }

                if (useFlags != NULL)
                {
                    EdgeRecord *edgeToUse = currEdge;
                    unsigned triIdx = edgeToUse[0].m_triIdx;
                    unsigned triIdx1 = edgeToUse[1].m_triIdx;
                    
                    unsigned triUseFlags = useFlags[triIdx];
                    unsigned triUseFlags1 = useFlags[triIdx1];

                    // Choose to add flags to the bitmask that already has more edges
                    // (to group flags in selected triangles rather than scattering them evenly)
                    if ((triUseFlags1 & CUF__USE_ALL_EDGES) > (triUseFlags & CUF__USE_ALL_EDGES))
                    {
                        triIdx = triIdx1;
                        triUseFlags = triUseFlags1;
                        edgeToUse = edgeToUse + 1;
                    }

                    if ((edges[vertIdx1].m_absVertexFlags & EdgeRecord::AVF_VERTEX_USED) == 0)
                    {
                        // Only add each vertex once and set a mark to prevent further additions
                        edges[vertIdx1].m_absVertexFlags |= EdgeRecord::AVF_VERTEX_USED;
                        // Also remember the index the vertex flags are going to be applied to 
                        // to allow easily clear the vertex from the use flags if any concave edges are found to connect to it
                        vertices[vertIdx1].m_UsedFromEdgeIndex = (unsigned)(edgeToUse - edges);
                        triUseFlags |= edgeToUse[0].m_vert1Flags;
                    }

                    // Same processing for the second vertex...
                    if ((edges[vertIdx2].m_absVertexFlags & EdgeRecord::AVF_VERTEX_USED) == 0)
                    {
                        edges[vertIdx2].m_absVertexFlags |= EdgeRecord::AVF_VERTEX_USED;
                        vertices[vertIdx2].m_UsedFromEdgeIndex = (unsigned)(edgeToUse - edges);
                        triUseFlags |= edgeToUse[0].m_vert2Flags;
                    }

                    // And finally store the use flags adding the edge flags in
                    useFlags[triIdx] = triUseFlags | edgeToUse[0].m_edgeFlags;
                }
            }

            // Skip the second edge
            ++currEdge;
        }
        // This is a boundary edge
        else
        {
            if (faceAngles != NULL)
            {
                buildBoundaryEdgeAngle(faceAngles, currEdge);
            }

            if (useFlags != NULL)
            {
                unsigned triIdx = currEdge[0].m_triIdx;
                unsigned triUseExtraFlags = 0;
                
                if ((edges[vertIdx1].m_absVertexFlags & EdgeRecord::AVF_VERTEX_USED) == 0)
                {
                    edges[vertIdx1].m_absVertexFlags |= EdgeRecord::AVF_VERTEX_USED;
                    vertices[vertIdx1].m_UsedFromEdgeIndex = (unsigned)(currEdge - edges);
                    triUseExtraFlags |= currEdge[0].m_vert1Flags;
                }

                if ((edges[vertIdx2].m_absVertexFlags & EdgeRecord::AVF_VERTEX_USED) == 0)
                {
                    edges[vertIdx2].m_absVertexFlags |= EdgeRecord::AVF_VERTEX_USED;
                    vertices[vertIdx2].m_UsedFromEdgeIndex = (unsigned)(currEdge - edges);
                    triUseExtraFlags |= currEdge[0].m_vert2Flags;
                }

                useFlags[triIdx] |= triUseExtraFlags | currEdge[0].m_edgeFlags;
            }
        }
    }
}

/*static */
void dxTriDataBase::buildBoundaryEdgeAngle(IFaceAngleStorageControl *faceAngles, 
    EdgeRecord *currEdge)
{
    const dReal faceAngle = REAL(0.0);

    dMeshTriangleVertex firstVertexStartIndex = currEdge[0].getEdgeStartVertexIndex();
    faceAngles->assignFacesAngleIntoStorage(currEdge[0].m_triIdx, firstVertexStartIndex, faceAngle);
    // -- For boundary edges, only the first element is valid
    // dMeshTriangleVertex secondVertexStartIndex = currEdge[1].getEdgeStartVertexIndex();
    // faceAngles->assignFacesAngleIntoStorage(currEdge[1].m_TriIdx, secondVertexStartIndex, faceAngle);
}

template<class TMeshDataAccessor>
/*static */
void dxTriDataBase::buildConcaveEdgeAngle(IFaceAngleStorageControl *faceAngles, bool negativeAnglesStored, 
    EdgeRecord *currEdge, const dReal &normalSegmentDot, const dReal &lengthSquareProduct,
    const dVector3 &triangleNormal, const dVector3 &secondOppositeVertexSegment,
    const dVector3 *pSecondTriangleMatchingEdge/*=NULL*/, const dVector3 *pFirstTriangle/*=NULL*/, 
    const TMeshDataAccessor &dataAccessor)
{
    dReal faceAngle;
    dMeshTriangleVertex firstVertexStartIndex = currEdge[0].getEdgeStartVertexIndex();

    // Check if concave angles are stored at all
    if (negativeAnglesStored)
    {
        // The length square product can become zero due to precision loss
        // when both the normal and the opposite edge vectors are very small.
        if (lengthSquareProduct != REAL(0.0))
        {
            faceAngle = -calculateEdgeAngleValidated(firstVertexStartIndex,
                currEdge, normalSegmentDot, lengthSquareProduct, triangleNormal, secondOppositeVertexSegment,
                pSecondTriangleMatchingEdge, pFirstTriangle, dataAccessor);
        }
        else
        {
            faceAngle = REAL(0.0);
        }
    }
    else
    {
        // If concave angles ate not stored, set an arbitrary negative value
        faceAngle = -(dReal)M_PI;
    }

    faceAngles->assignFacesAngleIntoStorage(currEdge[0].m_triIdx, firstVertexStartIndex, faceAngle);
    dMeshTriangleVertex secondVertexStartIndex = currEdge[1].getEdgeStartVertexIndex();
    faceAngles->assignFacesAngleIntoStorage(currEdge[1].m_triIdx, secondVertexStartIndex, faceAngle);
}

template<class TMeshDataAccessor>
/*static */
void dxTriDataBase::buildConvexEdgeAngle(IFaceAngleStorageControl *faceAngles, 
    EdgeRecord *currEdge, const dReal &normalSegmentDot, const dReal &lengthSquareProduct,
    const dVector3 &triangleNormal, const dVector3 &secondOppositeVertexSegment,
    const dVector3 *pSecondTriangleMatchingEdge/*=NULL*/, const dVector3 *pFirstTriangle/*=NULL*/, 
    const TMeshDataAccessor &dataAccessor)
{
    dReal faceAngle;
    dMeshTriangleVertex firstVertexStartIndex = currEdge[0].getEdgeStartVertexIndex();

    // The length square product can become zero due to precision loss
    // when both the normal and the opposite edge vectors are very small.
    if (normalSegmentDot < REAL(0.0) && lengthSquareProduct != REAL(0.0))
    {
        faceAngle = calculateEdgeAngleValidated(firstVertexStartIndex,
            currEdge, -normalSegmentDot, lengthSquareProduct, triangleNormal, secondOppositeVertexSegment,
            pSecondTriangleMatchingEdge, pFirstTriangle, dataAccessor);
    }
    else
    {
        faceAngle = REAL(0.0);
    }

    faceAngles->assignFacesAngleIntoStorage(currEdge[0].m_triIdx, firstVertexStartIndex, faceAngle);
    dMeshTriangleVertex secondVertexStartIndex = currEdge[1].getEdgeStartVertexIndex();
    faceAngles->assignFacesAngleIntoStorage(currEdge[1].m_triIdx, secondVertexStartIndex, faceAngle);
}

template<class TMeshDataAccessor>
/*static */
dReal dxTriDataBase::calculateEdgeAngleValidated(unsigned firstVertexStartIndex,
    EdgeRecord *currEdge, const dReal &normalSegmentDot, const dReal &lengthSquareProduct,
    const dVector3 &triangleNormal, const dVector3 &secondOppositeVertexSegment,
    const dVector3 *pSecondTriangleMatchingEdge/*=NULL*/, const dVector3 *pFirstTriangle/*=NULL*/, 
    const TMeshDataAccessor &dataAccessor)
{
    dIASSERT(lengthSquareProduct >= REAL(0.0));

    dReal result;
    dReal angleCosine = normalSegmentDot / dSqrt(lengthSquareProduct);

    if (angleCosine < REAL(1.0))
    {
        dVector3 normalSecondOppositeSegmentCross;
        dCalcVectorCross3(normalSecondOppositeSegmentCross, triangleNormal, secondOppositeVertexSegment);

        dReal secondTriangleEdgeDirectionCheck;

        if (pSecondTriangleMatchingEdge != NULL)
        {
            // Check the cross product against the second triangle edge, if possible...
            secondTriangleEdgeDirectionCheck = dCalcVectorDot3(normalSecondOppositeSegmentCross, *pSecondTriangleMatchingEdge);
        }
        else
        {
            // ...if not, calculate the supposed direction of the second triangle's edge 
            // as negative of first triangle edge. For that cross-multiply the precomputed
            // first triangle normal by vector from the degenerate edge to its opposite vertex.

            // Retrieve the first triangle points if necessary
            dVector3 firstTriangleStorage[dMTV__MAX];
            const dVector3 *pFirstTriangleToUse = pFirstTriangle;

            if (pFirstTriangle == NULL)
            {
                dataAccessor.getTriangleVertexPoints(firstTriangleStorage, currEdge[0].m_triIdx);
                pFirstTriangleToUse = &firstTriangleStorage[dMTV__MIN];
            }

            // Calculate the opposite vector
            unsigned firstTriangleOppositeIndex = firstVertexStartIndex != dMTV__MIN ? firstVertexStartIndex - 1 : dMTV__MAX - 1;

            dVector3 firstOppositeVertexSegment;
            dSubtractVectors3(firstOppositeVertexSegment, pFirstTriangleToUse[firstTriangleOppositeIndex], pFirstTriangleToUse[firstVertexStartIndex]);

            dVector3 normalFirstOppositeSegmentCross;
            dCalcVectorCross3(normalFirstOppositeSegmentCross, triangleNormal, firstOppositeVertexSegment);

            // And finally calculate the dot product to compare vector directions
            secondTriangleEdgeDirectionCheck = dCalcVectorDot3(normalSecondOppositeSegmentCross, normalFirstOppositeSegmentCross);
        }

        // Negative product means the angle absolute value is less than M_PI_2, positive - greater.
        result = secondTriangleEdgeDirectionCheck < REAL(0.0) ? dAsin(angleCosine) : (dReal)M_PI_2 + dAcos(angleCosine);
    }
    else
    {
        result = (dReal)M_PI_2;
        dIASSERT(angleCosine - REAL(1.0) < 1e-4); // The computational error can not be too high because the dot product had been verified to be greater than the concave threshold above
    }

    return result;
}


#endif // #if dTRIMESH_ENABLED


#endif // #ifndef _ODE_COLLISION_TRIMESH_INTERNAL_IMPL_H_