1 /******************************************************************************
2  *
3  * Project:  GDAL algorithms
4  * Purpose:  Delaunay triangulation
5  * Author:   Even Rouault, even.rouault at spatialys.com
6  *
7  ******************************************************************************
8  * Copyright (c) 2015, Even Rouault <even.rouault at spatialys.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #if defined(__MINGW32__) || defined(__MINGW64__)
30 /*  This avoids i586-mingw32msvc/include/direct.h from including libqhull/io.h ... */
31 #define _DIRECT_H_
32 /* For __MINGW64__ */
33 #define _INC_DIRECT
34 #define _INC_STAT
35 #endif
36 
37 #if defined(INTERNAL_QHULL) && !defined(DONT_DEPRECATE_SPRINTF)
38 #define DONT_DEPRECATE_SPRINTF
39 #endif
40 
41 #include "cpl_error.h"
42 #include "cpl_conv.h"
43 #include "cpl_multiproc.h"
44 #include "gdal_alg.h"
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <math.h>
51 
52 CPL_CVSID("$Id: delaunay.c d38086d97f76b283f7c6ba57abd60da9e279cbde 2018-10-06 19:02:19 +0200 Even Rouault $")
53 
54 #if defined(INTERNAL_QHULL) || defined(EXTERNAL_QHULL)
55 #define HAVE_INTERNAL_OR_EXTERNAL_QHULL 1
56 #endif
57 
58 #if HAVE_INTERNAL_OR_EXTERNAL_QHULL
59 #ifdef INTERNAL_QHULL
60 
61 #include "internal_qhull_headers.h"
62 
63 #else /* INTERNAL_QHULL */
64 
65 #if !defined(QHULL_INCLUDE_SUBDIR_IS_LIBQHULL)
66 #include "libqhull.h"
67 #include "qset.h"
68 #elif QHULL_INCLUDE_SUBDIR_IS_LIBQHULL
69 #include "libqhull/libqhull.h"
70 #include "libqhull/qset.h"
71 #else
72 #include "qhull/libqhull.h"
73 #include "qhull/qset.h"
74 #endif
75 
76 #endif /* INTERNAL_QHULL */
77 
78 #endif /* HAVE_INTERNAL_OR_EXTERNAL_QHULL*/
79 
80 
81 #if HAVE_INTERNAL_OR_EXTERNAL_QHULL
82 static CPLMutex* hMutex = NULL;
83 #endif
84 
85 /************************************************************************/
86 /*                       GDALHasTriangulation()                         */
87 /************************************************************************/
88 
89 /** Returns if GDAL is built with Delaunay triangulation support.
90  *
91  * @return TRUE if GDAL is built with Delaunay triangulation support.
92  *
93  * @since GDAL 2.1
94  */
GDALHasTriangulation()95 int GDALHasTriangulation()
96 {
97 #if HAVE_INTERNAL_OR_EXTERNAL_QHULL
98     return TRUE;
99 #else
100     return FALSE;
101 #endif
102 }
103 
104 /************************************************************************/
105 /*                   GDALTriangulationCreateDelaunay()                  */
106 /************************************************************************/
107 
108 /** Computes a Delaunay triangulation of the passed points
109  *
110  * @param nPoints number of points
111  * @param padfX x coordinates of the points.
112  * @param padfY y coordinates of the points.
113  * @return triangulation that must be freed with GDALTriangulationFree(), or
114  *         NULL in case of error.
115  *
116  * @since GDAL 2.1
117  */
GDALTriangulationCreateDelaunay(int nPoints,const double * padfX,const double * padfY)118 GDALTriangulation* GDALTriangulationCreateDelaunay(int nPoints,
119                                                    const double* padfX,
120                                                    const double* padfY)
121 {
122 #if HAVE_INTERNAL_OR_EXTERNAL_QHULL
123     coordT* points;
124     int i, j;
125     GDALTriangulation* psDT = NULL;
126     facetT *facet;
127     GDALTriFacet* pasFacets;
128     int* panMapQHFacetIdToFacetIdx; /* map from QHull facet ID to the index of our GDALTriFacet* array */
129     int curlong, totlong;     /* memory remaining after qh_memfreeshort */
130 
131     /* QHull is not thread safe, so we need to protect all operations with a mutex */
132     CPLCreateOrAcquireMutex(&hMutex, 1000);
133 
134 #if qh_QHpointer  /* see user.h */
135     if (qh_qh)
136     {
137         fprintf (stderr, "QH6238: Qhull link error.  The global variable qh_qh was not initialized\n\
138                  to NULL by global.c.  Please compile this program with -Dqh_QHpointer_dllimport\n\
139                  as well as -Dqh_QHpointer, or use libqhullstatic, or use a different tool chain.\n\n");
140         CPLReleaseMutex(hMutex);
141         return NULL;
142     }
143 #endif
144 
145     points = (coordT*)VSI_MALLOC2_VERBOSE(sizeof(double)*2, nPoints);
146     if( points == NULL )
147     {
148         CPLReleaseMutex(hMutex);
149         return NULL;
150     }
151     for(i=0;i<nPoints;i++)
152     {
153         points[2*i] = padfX[i];
154         points[2*i+1] = padfY[i];
155     }
156 
157     /* d: Delaunay */
158     /* Qbb: scale last coordinate to [0,m] for Delaunay */
159     /* Qc: keep coplanar points with nearest facet */
160     /* Qz: add a point-at-infinity for Delaunay triangulation */
161     /* Qt: triangulated output */
162     if( qh_new_qhull(2, nPoints, points, FALSE /* ismalloc */,
163                       "qhull d Qbb Qc Qz Qt", NULL, stderr) != 0 )
164     {
165         VSIFree(points);
166         CPLError(CE_Failure, CPLE_AppDefined, "Delaunay triangulation failed");
167         goto end;
168     }
169 
170     VSIFree(points);
171     points = NULL;
172 
173 #if qh_QHpointer  /* see user.h */
174     if (qh_qh == NULL)
175     {
176         CPLReleaseMutex(hMutex);
177         return NULL;
178     }
179 #endif
180 
181     /* Establish a map from QHull facet id to the index in our array of sequential facets */
182     panMapQHFacetIdToFacetIdx = (int*)VSI_MALLOC2_VERBOSE(sizeof(int), qh facet_id);
183     if( panMapQHFacetIdToFacetIdx == NULL )
184     {
185         goto end;
186     }
187     memset(panMapQHFacetIdToFacetIdx, 0xFF, sizeof(int) * qh facet_id);
188 
189     for(j = 0, facet = qh facet_list;
190         facet != NULL && facet->next != NULL;
191         facet = facet->next)
192     {
193         if( facet->upperdelaunay != qh UPPERdelaunay )
194             continue;
195 
196         if( qh_setsize(facet->vertices) != 3 ||
197             qh_setsize(facet->neighbors) != 3 )
198         {
199             CPLError(CE_Failure, CPLE_AppDefined,
200                      "Triangulation resulted in non triangular facet %d: vertices=%d",
201                      facet->id, qh_setsize(facet->vertices));
202             VSIFree(panMapQHFacetIdToFacetIdx);
203             goto end;
204         }
205 
206         CPLAssert(facet->id < qh facet_id);
207         panMapQHFacetIdToFacetIdx[facet->id] = j++;
208     }
209 
210     pasFacets = (GDALTriFacet*) VSI_MALLOC2_VERBOSE( j, sizeof(GDALTriFacet) );
211     if(pasFacets == NULL )
212     {
213         VSIFree(panMapQHFacetIdToFacetIdx);
214         goto end;
215     }
216 
217     psDT = (GDALTriangulation*)CPLCalloc(1, sizeof(GDALTriangulation));
218     psDT->nFacets = j;
219     psDT->pasFacets = pasFacets;
220 
221     // Store vertex and neighbor information for each triangle.
222     for(facet = qh facet_list;
223         facet != NULL && facet->next != NULL;
224         facet = facet->next)
225     {
226         int k;
227         if( facet->upperdelaunay != qh UPPERdelaunay )
228             continue;
229         k = panMapQHFacetIdToFacetIdx[facet->id];
230         pasFacets[k].anVertexIdx[0] =
231             qh_pointid(((vertexT*) facet->vertices->e[0].p)->point);
232         pasFacets[k].anVertexIdx[1] =
233             qh_pointid(((vertexT*) facet->vertices->e[1].p)->point);
234         pasFacets[k].anVertexIdx[2] =
235             qh_pointid(((vertexT*) facet->vertices->e[2].p)->point);
236         pasFacets[k].anNeighborIdx[0] =
237             panMapQHFacetIdToFacetIdx[((facetT*) facet->neighbors->e[0].p)->id];
238         pasFacets[k].anNeighborIdx[1] =
239             panMapQHFacetIdToFacetIdx[((facetT*) facet->neighbors->e[1].p)->id];
240         pasFacets[k].anNeighborIdx[2] =
241             panMapQHFacetIdToFacetIdx[((facetT*) facet->neighbors->e[2].p)->id];
242     }
243 
244     VSIFree(panMapQHFacetIdToFacetIdx);
245 
246 end:
247     qh_freeqhull(!qh_ALL);
248     qh_memfreeshort(&curlong, &totlong);
249 
250     CPLReleaseMutex(hMutex);
251 
252     return psDT;
253 #else /* HAVE_INTERNAL_OR_EXTERNAL_QHULL */
254 
255     /* Suppress unused argument warnings. */
256     (void)nPoints;
257     (void)padfX;
258     (void)padfY;
259 
260     CPLError(CE_Failure, CPLE_NotSupported,
261              "GDALTriangulationCreateDelaunay() unavailable since GDAL built without QHull support");
262     return NULL;
263 #endif /* HAVE_INTERNAL_OR_EXTERNAL_QHULL */
264 }
265 
266 /************************************************************************/
267 /*                       GDALTriangulationFree()                        */
268 /************************************************************************/
269 
270 /** Free a triangulation.
271  *
272  * @param psDT triangulation.
273  * @since GDAL 2.1
274  */
GDALTriangulationFree(GDALTriangulation * psDT)275 void GDALTriangulationFree(GDALTriangulation* psDT)
276 {
277     if( psDT )
278     {
279         VSIFree(psDT->pasFacets);
280         VSIFree(psDT->pasFacetCoefficients);
281         VSIFree(psDT);
282     }
283 }
284 
285 /************************************************************************/
286 /*               GDALTriangulationComputeBarycentricCoefficients()      */
287 /************************************************************************/
288 
289 /** Computes barycentric coefficients for each triangles of the triangulation.
290  *
291  * @param psDT triangulation.
292  * @param padfX x coordinates of the points. Must be identical to the one passed
293  *              to GDALTriangulationCreateDelaunay().
294  * @param padfY y coordinates of the points. Must be identical to the one passed
295  *              to GDALTriangulationCreateDelaunay().
296  *
297  * @return TRUE in case of success.
298  *
299  * @since GDAL 2.1
300  */
GDALTriangulationComputeBarycentricCoefficients(GDALTriangulation * psDT,const double * padfX,const double * padfY)301 int  GDALTriangulationComputeBarycentricCoefficients(GDALTriangulation* psDT,
302                                                      const double* padfX,
303                                                      const double* padfY)
304 {
305     int i;
306 
307     if( psDT->pasFacetCoefficients != NULL )
308     {
309         return TRUE;
310     }
311     psDT->pasFacetCoefficients = (GDALTriBarycentricCoefficients*)VSI_MALLOC2_VERBOSE(
312         sizeof(GDALTriBarycentricCoefficients), psDT->nFacets);
313     if( psDT->pasFacetCoefficients == NULL )
314     {
315         return FALSE;
316     }
317 
318     for(i = 0; i < psDT->nFacets; i++)
319     {
320         GDALTriFacet* psFacet = &(psDT->pasFacets[i]);
321         GDALTriBarycentricCoefficients* psCoeffs = &(psDT->pasFacetCoefficients[i]);
322         double dfX1 = padfX[psFacet->anVertexIdx[0]];
323         double dfY1 = padfY[psFacet->anVertexIdx[0]];
324         double dfX2 = padfX[psFacet->anVertexIdx[1]];
325         double dfY2 = padfY[psFacet->anVertexIdx[1]];
326         double dfX3 = padfX[psFacet->anVertexIdx[2]];
327         double dfY3 = padfY[psFacet->anVertexIdx[2]];
328         /* See https://en.wikipedia.org/wiki/Barycentric_coordinate_system */
329         double dfDenom = (dfY2 - dfY3) * (dfX1 - dfX3) + (dfX3 - dfX2) * (dfY1 - dfY3);
330         if( fabs(dfDenom) < 1e-5 )
331         {
332             // Degenerate triangle
333             psCoeffs->dfMul1X = 0.0;
334             psCoeffs->dfMul1Y = 0.0;
335             psCoeffs->dfMul2X = 0.0;
336             psCoeffs->dfMul2Y = 0.0;
337             psCoeffs->dfCstX = 0.0;
338             psCoeffs->dfCstY = 0.0;
339         }
340         else
341         {
342             psCoeffs->dfMul1X = (dfY2  - dfY3) / dfDenom;
343             psCoeffs->dfMul1Y = (dfX3  - dfX2) / dfDenom;
344             psCoeffs->dfMul2X = (dfY3  - dfY1) / dfDenom;
345             psCoeffs->dfMul2Y = (dfX1  - dfX3) / dfDenom;
346             psCoeffs->dfCstX = dfX3;
347             psCoeffs->dfCstY = dfY3;
348         }
349     }
350     return TRUE;
351 }
352 
353 /************************************************************************/
354 /*               GDALTriangulationComputeBarycentricCoordinates()       */
355 /************************************************************************/
356 
357 #define BARYC_COORD_L1(psCoeffs, dfX, dfY) \
358         (psCoeffs->dfMul1X * ((dfX) - psCoeffs->dfCstX) + psCoeffs->dfMul1Y * ((dfY) - psCoeffs->dfCstY))
359 #define BARYC_COORD_L2(psCoeffs, dfX, dfY) \
360         (psCoeffs->dfMul2X * ((dfX) - psCoeffs->dfCstX) + psCoeffs->dfMul2Y * ((dfY) - psCoeffs->dfCstY))
361 #define BARYC_COORD_L3(l1, l2)  (1 - (l1) - (l2))
362 
363 /** Computes the barycentric coordinates of a point.
364  *
365  * @param psDT triangulation.
366  * @param nFacetIdx index of the triangle in the triangulation
367  * @param dfX x coordinate of the point.
368  * @param dfY y coordinate of the point.
369  * @param pdfL1 (output) pointer to the 1st barycentric coordinate.
370  * @param pdfL2 (output) pointer to the 2nd barycentric coordinate.
371  * @param pdfL3 (output) pointer to the 2nd barycentric coordinate.
372  *
373  * @return TRUE in case of success.
374  *
375  * @since GDAL 2.1
376  */
377 
GDALTriangulationComputeBarycentricCoordinates(const GDALTriangulation * psDT,int nFacetIdx,double dfX,double dfY,double * pdfL1,double * pdfL2,double * pdfL3)378 int  GDALTriangulationComputeBarycentricCoordinates(const GDALTriangulation* psDT,
379                                                     int nFacetIdx,
380                                                     double dfX,
381                                                     double dfY,
382                                                     double* pdfL1,
383                                                     double* pdfL2,
384                                                     double* pdfL3)
385 {
386     const GDALTriBarycentricCoefficients* psCoeffs;
387     if( psDT->pasFacetCoefficients == NULL )
388     {
389         CPLError(CE_Failure, CPLE_AppDefined,
390                  "GDALTriangulationComputeBarycentricCoefficients() should be called before");
391         return FALSE;
392     }
393     CPLAssert(nFacetIdx >= 0 && nFacetIdx < psDT->nFacets);
394     psCoeffs = &(psDT->pasFacetCoefficients[nFacetIdx]);
395     *pdfL1 = BARYC_COORD_L1(psCoeffs, dfX, dfY);
396     *pdfL2 = BARYC_COORD_L2(psCoeffs, dfX, dfY);
397     *pdfL3 = BARYC_COORD_L3(*pdfL1, *pdfL2);
398 
399     return TRUE;
400 }
401 
402 /************************************************************************/
403 /*               GDALTriangulationFindFacetBruteForce()                 */
404 /************************************************************************/
405 
406 #define EPS     1e-10
407 
408 /** Returns the index of the triangle that contains the point by iterating
409  * over all triangles.
410  *
411  * If the function returns FALSE and *panOutputFacetIdx >= 0, then it means
412  * the point is outside the hull of the triangulation, and *panOutputFacetIdx
413  * is the closest triangle to the point.
414  *
415  * @param psDT triangulation.
416  * @param dfX x coordinate of the point.
417  * @param dfY y coordinate of the point.
418  * @param panOutputFacetIdx (output) pointer to the index of the triangle,
419  *                          or -1 in case of failure.
420  *
421  * @return index >= 0 of the triangle in case of success, -1 otherwise.
422  *
423  * @since GDAL 2.1
424  */
425 
GDALTriangulationFindFacetBruteForce(const GDALTriangulation * psDT,double dfX,double dfY,int * panOutputFacetIdx)426 int GDALTriangulationFindFacetBruteForce(const GDALTriangulation* psDT,
427                                          double dfX,
428                                          double dfY,
429                                          int* panOutputFacetIdx)
430 {
431     int nFacetIdx;
432     *panOutputFacetIdx = -1;
433     if( psDT->pasFacetCoefficients == NULL )
434     {
435         CPLError(CE_Failure, CPLE_AppDefined,
436                  "GDALTriangulationComputeBarycentricCoefficients() should be called before");
437         return FALSE;
438     }
439     for(nFacetIdx=0;nFacetIdx<psDT->nFacets;nFacetIdx++)
440     {
441         double l1, l2, l3;
442         const GDALTriBarycentricCoefficients* psCoeffs =
443                                     &(psDT->pasFacetCoefficients[nFacetIdx]);
444         if( psCoeffs->dfMul1X == 0.0 && psCoeffs->dfMul2X == 0.0 &&
445             psCoeffs->dfMul1Y == 0.0 && psCoeffs->dfMul2Y == 0.0 )
446         {
447             // Degenerate triangle
448             continue;
449         }
450         l1 = BARYC_COORD_L1(psCoeffs, dfX, dfY);
451         if( l1 < -EPS )
452         {
453             int neighbor = psDT->pasFacets[nFacetIdx].anNeighborIdx[0];
454             if( neighbor < 0 )
455             {
456                 *panOutputFacetIdx = nFacetIdx;
457                 return FALSE;
458             }
459             continue;
460         }
461         if( l1 > 1 + EPS )
462             continue;
463         l2 = BARYC_COORD_L2(psCoeffs, dfX, dfY);
464         if( l2 < -EPS )
465         {
466             int neighbor = psDT->pasFacets[nFacetIdx].anNeighborIdx[1];
467             if( neighbor < 0 )
468             {
469                 *panOutputFacetIdx = nFacetIdx;
470                 return FALSE;
471             }
472             continue;
473         }
474         if( l2 > 1 + EPS )
475             continue;
476         l3 = BARYC_COORD_L3(l1, l2);
477         if( l3 < -EPS )
478         {
479             int neighbor = psDT->pasFacets[nFacetIdx].anNeighborIdx[2];
480             if( neighbor < 0 )
481             {
482                 *panOutputFacetIdx = nFacetIdx;
483                 return FALSE;
484             }
485             continue;
486         }
487         if( l3 > 1 + EPS )
488             continue;
489         *panOutputFacetIdx = nFacetIdx;
490         return TRUE;
491     }
492     return FALSE;
493 }
494 
495 /************************************************************************/
496 /*               GDALTriangulationFindFacetDirected()                   */
497 /************************************************************************/
498 
499 #define EPS     1e-10
500 
501 /** Returns the index of the triangle that contains the point by walking in
502  * the triangulation.
503  *
504  * If the function returns FALSE and *panOutputFacetIdx >= 0, then it means
505  * the point is outside the hull of the triangulation, and *panOutputFacetIdx
506  * is the closest triangle to the point.
507  *
508  * @param psDT triangulation.
509  * @param nFacetIdx index of first triangle to start with.
510  *                  Must be >= 0 && < psDT->nFacets
511  * @param dfX x coordinate of the point.
512  * @param dfY y coordinate of the point.
513  * @param panOutputFacetIdx (output) pointer to the index of the triangle,
514  *                          or -1 in case of failure.
515  *
516  * @return TRUE in case of success, FALSE otherwise.
517  *
518  * @since GDAL 2.1
519  */
520 
GDALTriangulationFindFacetDirected(const GDALTriangulation * psDT,int nFacetIdx,double dfX,double dfY,int * panOutputFacetIdx)521 int GDALTriangulationFindFacetDirected(const GDALTriangulation* psDT,
522                                        int nFacetIdx,
523                                        double dfX,
524                                        double dfY,
525                                        int* panOutputFacetIdx)
526 {
527 #ifdef DEBUG_VERBOSE
528     const int nFacetIdxInitial = nFacetIdx;
529 #endif
530     int k, nIterMax;
531     *panOutputFacetIdx = -1;
532     if( psDT->pasFacetCoefficients == NULL )
533     {
534         CPLError(CE_Failure, CPLE_AppDefined,
535                  "GDALTriangulationComputeBarycentricCoefficients() should be called before");
536         return FALSE;
537     }
538     CPLAssert(nFacetIdx >= 0 && nFacetIdx < psDT->nFacets);
539 
540     nIterMax = 2 + psDT->nFacets / 4;
541     for(k=0;k<nIterMax;k++)
542     {
543         double l1, l2, l3;
544         int bMatch = TRUE;
545         const GDALTriFacet* psFacet = &(psDT->pasFacets[nFacetIdx]);
546         const GDALTriBarycentricCoefficients* psCoeffs =
547                                 &(psDT->pasFacetCoefficients[nFacetIdx]);
548         if( psCoeffs->dfMul1X == 0.0 && psCoeffs->dfMul2X == 0.0 &&
549             psCoeffs->dfMul1Y == 0.0 && psCoeffs->dfMul2Y == 0.0 )
550         {
551             // Degenerate triangle
552             break;
553         }
554         l1 = BARYC_COORD_L1(psCoeffs, dfX, dfY);
555         if( l1 < -EPS )
556         {
557             int neighbor = psFacet->anNeighborIdx[0];
558             if( neighbor < 0 )
559             {
560 #ifdef DEBUG_VERBOSE
561                 CPLDebug("GDAL", "Outside %d in %d iters (initial = %d)",
562                          nFacetIdx, k, nFacetIdxInitial);
563 #endif
564                 *panOutputFacetIdx = nFacetIdx;
565                 return FALSE;
566             }
567             nFacetIdx = neighbor;
568             continue;
569         }
570         else if( l1 > 1 + EPS )
571             bMatch = FALSE; // outside or degenerate
572 
573         l2 = BARYC_COORD_L2(psCoeffs, dfX, dfY);
574         if( l2 < -EPS )
575         {
576             int neighbor = psFacet->anNeighborIdx[1];
577             if( neighbor < 0 )
578             {
579 #ifdef DEBUG_VERBOSE
580                 CPLDebug("GDAL", "Outside %d in %d iters (initial = %d)",
581                          nFacetIdx, k, nFacetIdxInitial);
582 #endif
583                 *panOutputFacetIdx = nFacetIdx;
584                 return FALSE;
585             }
586             nFacetIdx = neighbor;
587             continue;
588         }
589         else if( l2 > 1 + EPS )
590             bMatch = FALSE; // outside or degenerate
591 
592         l3 = BARYC_COORD_L3(l1, l2);
593         if( l3 < -EPS )
594         {
595             int neighbor = psFacet->anNeighborIdx[2];
596             if( neighbor < 0 )
597             {
598 #ifdef DEBUG_VERBOSE
599                 CPLDebug("GDAL", "Outside %d in %d iters (initial = %d)",
600                          nFacetIdx, k, nFacetIdxInitial);
601 #endif
602                 *panOutputFacetIdx = nFacetIdx;
603                 return FALSE;
604             }
605             nFacetIdx = neighbor;
606             continue;
607         }
608         else if( l3 > 1 + EPS )
609             bMatch = FALSE; // outside or degenerate
610 
611         if( bMatch )
612         {
613 #ifdef DEBUG_VERBOSE
614             CPLDebug("GDAL", "Inside %d in %d iters (initial = %d)",
615                      nFacetIdx, k, nFacetIdxInitial);
616 #endif
617             *panOutputFacetIdx = nFacetIdx;
618             return TRUE;
619         }
620         else
621         {
622             break;
623         }
624     }
625 
626     CPLDebug("GDAL", "Using brute force lookup");
627     return GDALTriangulationFindFacetBruteForce(psDT, dfX, dfY, panOutputFacetIdx);
628 }
629 
630 /************************************************************************/
631 /*                         GDALTriangulationTerminate()                 */
632 /************************************************************************/
633 
GDALTriangulationTerminate()634 void GDALTriangulationTerminate()
635 {
636 #if HAVE_INTERNAL_OR_EXTERNAL_QHULL
637     if( hMutex != NULL )
638     {
639         CPLDestroyMutex(hMutex);
640         hMutex = NULL;
641     }
642 #endif
643 }
644