File: Wm5IncrementalDelaunay2.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 90,112 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 39
file content (1634 lines) | stat: -rw-r--r-- 50,602 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2010/10/01)

#include "Wm5MathematicsPCH.h"
#include "Wm5IncrementalDelaunay2.h"

namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
IncrementalDelaunay2<Real>::IncrementalDelaunay2 (Real xmin, Real ymin,
    Real xmax, Real ymax, Real uncertainty)
    :
    mXMin(xmin),
    mXMax(xmax),
    mYMin(ymin),
    mYMax(ymax),
    mUncertainty(uncertainty),
    mNumTriangles(0),
    mIndices(0),
    mAdjacencies(0),
    mPathLast(-1),
    mPath(0),
    mLastEdgeV0(-1),
    mLastEdgeV1(-1),
    mLastEdgeOpposite(-1),
    mLastEdgeOppositeIndex(-1)
{
    assertion(mXMin < mXMax && mYMin < mYMax,
        "Invalid bounding rectangle\n");
    assertion((Real)0 <= mUncertainty && mUncertainty <= (Real)1,
        "Invalid uncertainty\n");

    if (mUncertainty > (Real)0)
    {
        mRatVertexPool = new0 std::vector<QRVector>();
        mRatVertexEvaluated = new0 std::vector<bool>();
    }
    else
    {
        mRatVertexPool = 0;
        mRatVertexEvaluated = 0;
    }

    // Create a supertriangle that contains the input rectangle.
    Real x0 = ((Real)2)*xmin - xmax;
    Real y0 = ((Real)2)*ymin - ymax;
    Real x1 = ((Real)2)*xmax - xmin + ((Real)3)*(ymax - ymin);
    Real y1 = y0;
    Real x2 = x0;
    Real y2 = ((Real)2)*ymax - ymin + ((Real)3)*(xmax - xmin);
    Vector2<Real> superVertex0(x0, y0);
    Vector2<Real> superVertex1(x1, y1);
    Vector2<Real> superVertex2(x2, y2);

    // Insert the supertriangle vertices into the vertex storage.
    mVMap[superVertex0] = 0;
    mVMap[superVertex1] = 1;
    mVMap[superVertex2] = 2;
    mVertexPool.push_back(superVertex0);
    mVertexPool.push_back(superVertex1);
    mVertexPool.push_back(superVertex2);

    // Inert the supertriangle into the mesh.
    mTriangle.insert(new0 Triangle(0, 1, 2));
}
//----------------------------------------------------------------------------
template <typename Real>
IncrementalDelaunay2<Real>::~IncrementalDelaunay2 ()
{
    delete0(mRatVertexPool);
    delete0(mRatVertexEvaluated);
    delete1(mIndices);
    delete1(mAdjacencies);
    delete1(mPath);

    typename std::set<Triangle*>::iterator iter = mTriangle.begin();
    typename std::set<Triangle*>::iterator end = mTriangle.end();
    for (/**/; iter != end; ++iter)
    {
        Triangle* tri = *iter;
        delete0(tri);
    }
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::Insert (const Vector2<Real>& position)
{
    if (position[0] < mXMin || position[0] > mXMax
    ||  position[1] < mYMin || position[1] > mYMax)
    {
        // The vertex is outside the domain specified in the constructor.
        return -1;
    }

    typename VertexMap::iterator viter = mVMap.find(position);
    if (viter != mVMap.end())
    {
        // The vertex already exists, so just return its index.
        return viter->second;
    }

    // Store the position in the various pools.
    int posIndex = (int)mVertexPool.size();
    mVMap[position] = posIndex;
    mVertexPool.push_back(position);
    if (mUncertainty > (Real)0)
    {
        (*mRatVertexPool).push_back(QRVector());
        (*mRatVertexEvaluated).push_back(false);
    }

    Triangle* tri = GetContainingTriangleInternal(position);
    if (!tri)
    {
        // All points must lie in the supertriangle, so each point must have
        // a containing triangle.
        assertion(false, "Unexpected condition\n");
        return -1;
    }

    // Locate and remove the triangles forming the insertion polygon.
    std::stack<Triangle*> triStack;
    VEManifoldMesh polygon(0, Edge::ECreator);
    triStack.push(tri);
    tri->OnStack = true;
    int j, v0, v1;
    Edge* edge;
    while (!triStack.empty())
    {
        tri = triStack.top();
        triStack.pop();
        tri->OnStack = false;
        for (j = 0; j < 3; ++j)
        {
            Triangle* adj = tri->Adj[j];
            if (adj)
            {
                // Detach triangle and adjacent triangle from each other.
                int nullIndex = tri->DetachFrom(j, adj);

                if (adj->IsInsertionComponent(posIndex, position, tri, this))
                {
                    if (!adj->OnStack)
                    {
                        // Adjacent triangle inside insertion polygon.
                        triStack.push(adj);
                        adj->OnStack = true;
                    }
                }
                else
                {
                    // Adjacent triangle outside insertion polygon.
                    v0 = tri->V[j];
                    v1 = tri->V[(j+1)%3];
                    edge = (Edge*)polygon.InsertEdge(v0, v1);
                    edge->NullIndex = nullIndex;
                    edge->Tri = adj;
                }
            }
            else
            {
                // The triangle is in the insertion polygon, but the adjacent
                // one does not exist.  This means one of two things:
                // (1) We are at an edge of the supertriangle, and that edge
                //     is part of the insertion polygon.
                // (2) We are at an edge that was recently shared by the
                //     triangle and the adjacent, but we detached those
                //     triangles from each other.  These edges should be
                //     ignored.
                v0 = tri->V[j];
                if (0 <= v0 && v0 <= 2)
                {
                    // v0 is a supervertex index.
                    v1 = tri->V[(j+1)%3];
                    if (0 <= v1 && v1 <= 2)
                    {
                        // v1 is a supervertex index.
                        edge = (Edge*)polygon.InsertEdge(v0, v1);
                        edge->NullIndex = -1;
                        edge->Tri = 0;
                    }
                }
            }
        }
        mTriangle.erase(tri);
        delete0(tri);
    }

    // Insert the new triangles formed by the input point and the edges of
    // the insertion polygon.
    const VEManifoldMesh::EMap& edgeMap = polygon.GetEdges();
    assertion(edgeMap.size() >= 3 && polygon.IsClosed(),
                "Polygon must be at least a triangle\n");
    typename VEManifoldMesh::EMapCIterator iter = edgeMap.begin();
    typename VEManifoldMesh::EMapCIterator end = edgeMap.end();
    for (/**/; iter != end; ++iter)
    {
        edge = (Edge*)iter->second;

        // Create and insert the new triangle.
        tri = new0 Triangle(posIndex, edge->V[0], edge->V[1]);
        mTriangle.insert(tri);

        // Establish the adjacency links across the polygon edge.
        tri->Adj[1] = edge->Tri;
        if (edge->Tri)
        {
            edge->Tri->Adj[edge->NullIndex] = tri;
        }

        // Update the edge's triangle pointer to point to the newly created
        // triangle.  This information is used later to establish the links
        // between the new triangles.
        edge->Tri = tri;
    }

    // Establish the adjacency links between the new triangles.
    Edge* adjEdge;
    iter = edgeMap.begin();
    end = edgeMap.end();
    for (/**/; iter != end; ++iter)
    {
        edge = (Edge*)iter->second;
        adjEdge = (Edge*)edge->E[0];
        edge->Tri->Adj[0] = adjEdge->Tri;
        adjEdge = (Edge*)edge->E[1];
        edge->Tri->Adj[2] = adjEdge->Tri;
    }

    return posIndex;
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::Remove (const Vector2<Real>& position)
{
    typename VertexMap::iterator iter = mVMap.find(position);
    if (iter == mVMap.end())
    {
        // The vertex does not exists, so return an invalid index.
        return -1;
    }
    int posIndex = iter->second;

    Triangle* initialTri = GetContainingTriangleInternal(position);
    if (!initialTri)
    {
        // All points must lie in the supertriangle, so each point must have
        // a containing triangle.  Moreover, in the Remove operation, the
        // point must be a vertex of a triangle.
        assertion(false, "Unexpected condition\n");
        return -1;
    }

    // Construct the removal polygon.
    std::vector<RPVertex> polygon;
    Triangle* tri = initialTri;
    do
    {
        // Locate the vertex for the removal point.  The opposite edge is an
        // edge of the removal polygon.
        int i;
        for (i = 0; i < 3; ++i)
        {
            if (tri->V[i] == posIndex)
            {
                break;
            }
        }
        if (i == 3)
        {
            assertion(false, "Removal point must be a triangle vertex\n");
            return -1;
        }

        // The removal point is P = Tri.V[i].  The edge of the removal polygon
        // is <V1,V2>, where V1 = Tri.V[(i+1)%3] and V2 = Tri.V[(i+2)%3)].
        // The edge <P,V1> is shared by Tri and Adj = Tri.A[i].
        polygon.push_back(RPVertex(tri->V[(i+1)%3], tri,
            tri->Adj[i]));
        tri = tri->Adj[(i+2)%3];
    }
    while (tri != initialTri);

    // Triangulate the removal polygon.
    Triangulate(polygon, posIndex, this);

    mVMap.erase(iter);
    return posIndex;
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::GetAllTriangles (int& numTriangles,
    int*& indices)
{
    numTriangles = (int)mTriangle.size();
    indices = new1<int>(3*numTriangles);

    int* currIndex = indices;
    typename std::set<Triangle*>::iterator iter = mTriangle.begin();
    typename std::set<Triangle*>::iterator end = mTriangle.end();
    for (/**/; iter != end; ++iter)
    {
        Triangle* tri = *iter;
        for (int i = 0; i < 3; ++i)
        {
            *currIndex++ = tri->V[i];
        }
    }
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::GenerateRepresentation ()
{
    delete1(mIndices);
    mIndices = 0;
    delete1(mAdjacencies);
    mAdjacencies = 0;
    delete1(mPath);
    mPath = 0;

    // Assign integer values to the triangles for use by the caller.
    std::map<Triangle*,int> permute;
    typename std::set<Triangle*>::iterator iter = mTriangle.begin();
    typename std::set<Triangle*>::iterator end = mTriangle.end();
    mNumTriangles = (int)mTriangle.size();
    Triangle* tri;
    int i;
    for (i = 0; iter != end; ++iter)
    {
        tri = *iter;

        // Skip triangles that share a supervertex.
        if ((0 <= tri->V[0] && tri->V[0] <= 2)
        ||  (0 <= tri->V[1] && tri->V[1] <= 2)
        ||  (0 <= tri->V[2] && tri->V[2] <= 2))
        {
            --mNumTriangles;
            continue;
        }

        permute[tri] = i;
        ++i;
    }
    permute[(Triangle*)0] = -1;

    // Put Delaunay triangles into an array (vertices and adjacency info).
    if (mNumTriangles > 0)
    {
        mIndices = new1<int>(3*mNumTriangles);
        mAdjacencies = new1<int>(3*mNumTriangles);
        i = 0;
        iter = mTriangle.begin();
        end = mTriangle.end();
        for (/**/; iter != end; ++iter)
        {
            tri = *iter;

            // Skip triangles that share a supervertex.
            if ((0 <= tri->V[0] && tri->V[0] <= 2)
            ||  (0 <= tri->V[1] && tri->V[1] <= 2)
            ||  (0 <= tri->V[2] && tri->V[2] <= 2))
            {
                continue;
            }

            mIndices[i] = tri->V[0];
            if (ContainsSupervertex(tri->Adj[0]))
            {
                mAdjacencies[i++] = -1;
            }
            else
            {
                mAdjacencies[i++] = permute[tri->Adj[0]];
            }

            mIndices[i] = tri->V[1];
            if (ContainsSupervertex(tri->Adj[1]))
            {
                mAdjacencies[i++] = -1;
            }
            else
            {
                mAdjacencies[i++] = permute[tri->Adj[1]];
            }

            mIndices[i] = tri->V[2];
            if (ContainsSupervertex(tri->Adj[2]))
            {
                mAdjacencies[i++] = -1;
            }
            else
            {
                mAdjacencies[i++] = permute[tri->Adj[2]];
            }
        }
        assertion(i == 3*mNumTriangles, "Inconsistent condition\n");

        mPathLast = -1;
        mPath = new1<int>(mNumTriangles + 1);
    }
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::GetNumTriangles () const
{
    return mNumTriangles;
}
//----------------------------------------------------------------------------
template <typename Real>
const int* IncrementalDelaunay2<Real>::GetIndices () const
{
    return mIndices;
}
//----------------------------------------------------------------------------
template <typename Real>
const int* IncrementalDelaunay2<Real>::GetAdjacencies () const
{
    return mAdjacencies;
}
//----------------------------------------------------------------------------
template <typename Real>
const std::vector<Vector2<Real> >&
IncrementalDelaunay2<Real>::GetVertices () const
{
    return mVertexPool;
}
//----------------------------------------------------------------------------
template <typename Real>
const std::map<Vector2<Real>,int>&
IncrementalDelaunay2<Real>::GetUniqueVertices () const
{
    return mVMap;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::GetHull (int& numEdges, int*& indices)
{
    numEdges = 0;
    indices = 0;

    // Count the number of edges that are shared by triangles containing a
    // supervertex.
    int i, adjQuantity = 3*mNumTriangles;
    for (i = 0; i < adjQuantity; ++i)
    {
        if (mAdjacencies[i] == -1)
        {
            ++numEdges;
        }
    }
    assertion(numEdges > 0, "Unexpected condition\n");
    if (numEdges == 0)
    {
        return false;
    }

    // Enumerate the edges.
    indices = new1<int>(2*numEdges);
    int* currentIndex = indices;
    for (i = 0; i < adjQuantity; ++i)
    {
        if (mAdjacencies[i] == -1)
        {
            int tri = i/3, j = i%3;
            *currentIndex++ = mIndices[3*tri + j];
            *currentIndex++ = mIndices[3*tri + ((j+1)%3)];
        }
    }

    return true;
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::GetContainingTriangle (
    const Vector2<Real>& test) const
{
    // The mesh might not have any triangles (only collinear points were
    // inserted).
    if (!mPath)
    {
        return -1;
    }

    // Start at first triangle in mesh.
    int iIndex = (mPathLast >= 0 ? mPath[mPathLast] : 0);
    mPathLast = -1;
    mLastEdgeV0 = -1;
    mLastEdgeV1 = -1;
    mLastEdgeOpposite = -1;
    mLastEdgeOppositeIndex = -1;

    // Use triangle edges as binary separating lines.
    for (int i = 0; i < mNumTriangles; i++)
    {
        mPath[++mPathLast] = iIndex;

        int* aiV = &mIndices[3*iIndex];

        if (ToLine(test,aiV[0],aiV[1]) > 0)
        {
            iIndex = mAdjacencies[3*iIndex];
            if (iIndex == -1)
            {
                mLastEdgeV0 = aiV[0];
                mLastEdgeV1 = aiV[1];
                mLastEdgeOpposite = aiV[2];
                mLastEdgeOppositeIndex = 2;
                return -1;
            }
            continue;
        }

        if (ToLine(test,aiV[1],aiV[2]) > 0)
        {
            iIndex = mAdjacencies[3*iIndex+1];
            if (iIndex == -1)
            {
                mLastEdgeV0 = aiV[1];
                mLastEdgeV1 = aiV[2];
                mLastEdgeOpposite = aiV[0];
                mLastEdgeOppositeIndex = 0;
                return -1;
            }
            continue;
        }

        if (ToLine(test,aiV[2],aiV[0]) > 0)
        {
            iIndex = mAdjacencies[3*iIndex+2];
            if (iIndex == -1)
            {
                mLastEdgeV0 = aiV[2];
                mLastEdgeV1 = aiV[0];
                mLastEdgeOpposite = aiV[1];
                mLastEdgeOppositeIndex = 1;
                return -1;
            }
            continue;
        }

        mLastEdgeV0 = -1;
        mLastEdgeV1 = -1;
        mLastEdgeOpposite = -1;
        mLastEdgeOppositeIndex = -1;
        return iIndex;
    }

    return -1;
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::GetPathLast () const
{
    return mPathLast;
}
//----------------------------------------------------------------------------
template <typename Real>
const int* IncrementalDelaunay2<Real>::GetPath () const
{
    return mPath;
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::GetLastEdge (int& riV0, int& riV1, int& riV2)
    const
{
    riV0 = mLastEdgeV0;
    riV1 = mLastEdgeV1;
    riV2 = mLastEdgeOpposite;
    return mLastEdgeOppositeIndex;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::GetVertexSet (int i, Vector2<Real> akV[3])
    const
{
    if (0 <= i && i < mNumTriangles)
    {
        akV[0] = mVertexPool[mIndices[3*i  ]];
        akV[1] = mVertexPool[mIndices[3*i+1]];
        akV[2] = mVertexPool[mIndices[3*i+2]];
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::GetIndexSet (int i, int aiIndex[3]) const
{
    if (0 <= i && i < mNumTriangles)
    {
        aiIndex[0] = mIndices[3*i  ];
        aiIndex[1] = mIndices[3*i+1];
        aiIndex[2] = mIndices[3*i+2];
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::GetAdjacentSet (int i, int aiAdjacent[3])
    const
{
    if (0 <= i && i < mNumTriangles)
    {
        aiAdjacent[0] = mAdjacencies[3*i  ];
        aiAdjacent[1] = mAdjacencies[3*i+1];
        aiAdjacent[2] = mAdjacencies[3*i+2];
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::GetBarycentricSet (int i,
    const Vector2<Real>& test, Real afBary[3]) const
{
    if (0 <= i && i < mNumTriangles)
    {
        Vector2<Real> kV0 = mVertexPool[mIndices[3*i  ]];
        Vector2<Real> kV1 = mVertexPool[mIndices[3*i+1]];
        Vector2<Real> kV2 = mVertexPool[mIndices[3*i+2]];
        test.GetBarycentrics(kV0,kV1,kV2,afBary);
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::ToLine (const Vector2<Real>& test, int v0,
    int v1) const
{
    if (mUncertainty < (Real)1)
    {
        // Order the points so that ToLine(test,v0,v1) and ToLine(test,v1,v0)
        // return the same geometric result.
        Vector2<Real> vertex0 = mVertexPool[v0];
        Vector2<Real> vertex1 = mVertexPool[v1];
        bool positive;
        if (vertex0 < vertex1)
        {
            positive = true;
        }
        else
        {
            Vector2<Real> kSave = vertex0;
            vertex0 = vertex1;
            vertex1 = kSave;
            positive = false;
        }

        Real x0 = test[0] - vertex0[0];
        Real y0 = test[1] - vertex0[1];
        Real x1 = vertex1[0] - vertex0[0];
        Real y1 = vertex1[1] - vertex0[1];
        Real det = x0*y1 - x1*y0;
        if (!positive)
        {
            det = -det;
        }

        if (mUncertainty == (Real)0)
        {
            // Compute the sign test using floating-point arithmetic.
            return (det > (Real)0 ? +1 : (det < (Real)0 ? -1 : 0));
        }

        // Use filtered predicates.
        Real length0 = Math<Real>::Sqrt(x0*x0 + y0*y0);
        Real length1 = Math<Real>::Sqrt(x1*x1 + y1*y1);
        Real scaledUncertainty = mUncertainty*length0*length1;
        if (Math<Real>::FAbs(det) >= scaledUncertainty)
        {
            // The floating-point sign test is deemed to be certain.
            return (det > (Real)0 ? +1 : (det < (Real)0 ? -1 : 0));
        }
    }

    // Compute the determinant using exact rational arithmetic.
    QRVector ratTest;
    ratTest[0] = QRational(test[0]);
    ratTest[1] = QRational(test[1]);
    int indices[2] = { v0, v1 };
    for (int i = 0; i < 2; ++i)
    {
        int j = indices[i];
        if (!(*mRatVertexEvaluated)[j])
        {
            (*mRatVertexEvaluated)[j] = true;
            (*mRatVertexPool)[j][0] = QRational(mVertexPool[j][0]);
            (*mRatVertexPool)[j][1] = QRational(mVertexPool[j][1]);
        }
    }

    // Compute the sign test using rational arithmetic.
    const QRVector& ratV0 = (*mRatVertexPool)[v0];
    const QRVector& ratV1 = (*mRatVertexPool)[v1];
    QRational ratX0 = ratTest[0] - ratV0[0];
    QRational ratY0 = ratTest[1] - ratV0[1];
    QRational ratX1 = ratV1[0] - ratV0[0];
    QRational ratY1 = ratV1[1] - ratV0[1];
    QRational ratDet = ratX0*ratY1 - ratX1*ratY0;
    return (ratDet > 0 ? +1 : (ratDet < 0 ? -1 : 0));
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::ToTriangle (const Vector2<Real>& test,
    int v0, int v1, int v2) const
{
    int sign0 = ToLine(test, v1, v2);
    if (sign0 > 0)
    {
        return +1;
    }

    int sign1 = ToLine(test, v0, v2);
    if (sign1 < 0)
    {
        return +1;
    }

    int sign2 = ToLine(test, v0, v1);
    if (sign2 > 0)
    {
        return +1;
    }

    return ((sign0 && sign1 && sign2) ? -1 : 0);
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::ToCircumcircle (const Vector2<Real>& test,
    int v0, int v1, int v2) const
{
    if (mUncertainty < (Real)1)
    {
        // Order the points so that ToCircumcircle(test,u0,u1,u2) returns the
        // same containment result for any permutation (u0,u1,u2) of
        // (v0,v1,v2).
        Vector2<Real> vertex0 = mVertexPool[v0];
        Vector2<Real> vertex1 = mVertexPool[v1];
        Vector2<Real> vertex2 = mVertexPool[v2];
        Vector2<Real> save;
        bool positive;
        if (vertex0 < vertex1)
        {
            if (vertex2 < vertex0)
            {
                // (2,0,1)
                save = vertex2;
                vertex2 = vertex1;
                vertex1 = vertex0;
                vertex0 = save;
                positive = true;
            }
            else if (vertex2 < vertex1)
            {
                // (0,2,1)
                save = vertex1;
                vertex1 = vertex2;
                vertex2 = save;
                positive = false;
            }
            else
            {
                // (0,1,2)
                positive = true;
            }
        }
        else
        {
            if (vertex2 < vertex1)
            {
                // (2,1,0)
                save = vertex0;
                vertex0 = vertex2;
                vertex2 = save;
                positive = false;
            }
            else if (vertex2 < vertex0)
            {
                // (1,2,0)
                save = vertex0;
                vertex0 = vertex1;
                vertex1 = vertex2;
                vertex2 = save;
                positive = true;
            }
            else
            {
                // (1,0,2)
                save = vertex0;
                vertex0 = vertex1;
                vertex1 = save;
                positive = false;
            }
        }

        Real s0x = vertex0[0] + test[0];
        Real d0x = vertex0[0] - test[0];
        Real s0y = vertex0[1] + test[1];
        Real d0y = vertex0[1] - test[1];
        Real s1x = vertex1[0] + test[0];
        Real d1x = vertex1[0] - test[0];
        Real s1y = vertex1[1] + test[1];
        Real d1y = vertex1[1] - test[1];
        Real s2x = vertex2[0] + test[0];
        Real d2x = vertex2[0] - test[0];
        Real s2y = vertex2[1] + test[1];
        Real d2y = vertex2[1] - test[1];
        Real z0 = s0x*d0x + s0y*d0y;
        Real z1 = s1x*d1x + s1y*d1y;
        Real z2 = s2x*d2x + s2y*d2y;
        Real c00 = d1y*z2 - d2y*z1;
        Real c01 = d2y*z0 - d0y*z2;
        Real c02 = d0y*z1 - d1y*z0;
        Real det = d0x*c00 + d1x*c01 + d2x*c02;
        if (!positive)
        {
            det = -det;
        }

        if (mUncertainty == (Real)0)
        {
            // Compute the sign test using floating-point arithmetic.
            return (det < (Real)0 ? +1 : (det > (Real)0 ? -1 : 0));
        }

        // Use filtered predicates.
        Real length0 = Math<Real>::Sqrt(d0x*d0x + d0y*d0y + z0*z0);
        Real length1 = Math<Real>::Sqrt(d1x*d1x + d1y*d1y + z1*z1);
        Real length2 = Math<Real>::Sqrt(d2x*d2x + d2y*d2y + z2*z2);
        Real scaledUncertainty = mUncertainty*length0*length1*length2;
        if (Math<Real>::FAbs(det) >= scaledUncertainty)
        {
            return (det < (Real)0 ? 1 : (det > (Real)0 ? -1 : 0));
        }
    }

    // Compute the sign test using rational arithmetic.
    QRVector ratTest;
    ratTest[0] = QRational(test[0]);
    ratTest[1] = QRational(test[1]);
    int indices[3] = { v0, v1, v2 };
    for (int i = 0; i < 3; ++i)
    {
        int j = indices[i];
        if (!(*mRatVertexEvaluated)[j])
        {
            (*mRatVertexEvaluated)[j] = true;
            (*mRatVertexPool)[j][0] = QRational(mVertexPool[j][0]);
            (*mRatVertexPool)[j][1] = QRational(mVertexPool[j][1]);
        }
    }

    QRVector& ratV0 = (*mRatVertexPool)[v0];
    QRVector& ratV1 = (*mRatVertexPool)[v1];
    QRVector& ratV2 = (*mRatVertexPool)[v2];
    QRational ratS0x = ratV0[0] + ratTest[0];
    QRational ratD0x = ratV0[0] - ratTest[0];
    QRational ratS0y = ratV0[1] + ratTest[1];
    QRational ratD0y = ratV0[1] - ratTest[1];
    QRational ratS1x = ratV1[0] + ratTest[0];
    QRational ratD1x = ratV1[0] - ratTest[0];
    QRational ratS1y = ratV1[1] + ratTest[1];
    QRational ratD1y = ratV1[1] - ratTest[1];
    QRational ratS2x = ratV2[0] + ratTest[0];
    QRational ratD2x = ratV2[0] - ratTest[0];
    QRational ratS2y = ratV2[1] + ratTest[1];
    QRational ratD2y = ratV2[1] - ratTest[1];
    QRational ratZ0 = ratS0x*ratD0x + ratS0y*ratD0y;
    QRational ratZ1 = ratS1x*ratD1x + ratS1y*ratD1y;
    QRational ratZ2 = ratS2x*ratD2x + ratS2y*ratD2y;
    QRational ratC00 = ratD1y*ratZ2 - ratD2y*ratZ1;
    QRational ratC01 = ratD2y*ratZ0 - ratD0y*ratZ2;
    QRational ratC02 = ratD0y*ratZ1 - ratD1y*ratZ0;
    QRational ratDet = ratD0x*ratC00 + ratD1x*ratC01 + ratD2x*ratC02;
    return (ratDet < 0 ? +1 : (ratDet > 0 ? -1 : 0));
}
//----------------------------------------------------------------------------
template <typename Real>
typename IncrementalDelaunay2<Real>::Triangle*
IncrementalDelaunay2<Real>::GetContainingTriangleInternal (
    const Vector2<Real>& position) const
{
    // Locate which triangle in the current mesh contains vertex i.  By
    // construction, there must be such a triangle (the vertex cannot be
    // outside the supertriangle).

    Triangle* tri = *mTriangle.begin();
    int numTriangles = (int)mTriangle.size();
    for (int t = 0; t < numTriangles; ++t)
    {
        int* vertices = tri->V;

        if (ToLine(position, vertices[0], vertices[1]) > 0)
        {
            tri = tri->Adj[0];
            if (!tri)
            {
                break;
            }
            continue;
        }

        if (ToLine(position, vertices[1], vertices[2]) > 0)
        {
            tri = tri->Adj[1];
            if (!tri)
            {
                break;
            }
            continue;
        }

        if (ToLine(position, vertices[2], vertices[0]) > 0)
        {
            tri = tri->Adj[2];
            if (!tri)
            {
                break;
            }
            continue;
        }

        return tri;
    }

    assertion(false, "Delaunay vertices must lie in some triangle\n");
    return 0;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::ContainsSupervertex (Triangle* tri) const
{
    for (int i = 0; i < 3; ++i)
    {
        if (0 <= tri->V[i] && tri->V[i] <= 2)
        {
            return true;
        }
    }
    return false;
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::SwapEdge (Triangle* tri0, Triangle* tri1)
{
    int i0, i0p1, i0p2, i1, i1p1, i1p2, j;
    Triangle* adj;

    // Locate the indices of the shared edge.
    for (i0 = 0; i0 < 3; ++i0)
    {
        if (tri1 == tri0->Adj[i0])
        {
            break;
        }
    }
    if (i0 == 3)
    {
        assertion(false, "Unexpected condition.\n");
        return;
    }
    i0p1 = (i0 + 1) % 3;
    i0p2 = (i0 + 2) % 3;

    for (i1 = 0; i1 < 3; ++i1)
    {
        if (tri0 == tri1->Adj[i1])
        {
            break;
        }
    }
    if (i1 == 3)
    {
        assertion(false, "Unexpected condition.\n");
        return;
    }
    i1p1 = (i1 + 1) % 3;
    i1p2 = (i1 + 2) % 3;

    tri0->V[i0p1] = tri1->V[i1p2];
    tri1->V[i1p1] = tri0->V[i0p2];

    adj = tri1->Adj[i1p1];
    tri0->Adj[i0] = adj;
    if (adj)
    {
        for (j = 0; j < 3; ++j)
        {
            if (adj->Adj[j] == tri1)
            {
                adj->Adj[j] = tri0;
                break;
            }
        }
        if (j == 3)
        {
            assertion(false, "Unexpected condition.\n");
            return;
        }
    }

    adj = tri0->Adj[i0p1];
    tri1->Adj[i1] = adj;
    if (adj)
    {
        for (j = 0; j < 3; ++j)
        {
            if (adj->Adj[j] == tri0)
            {
                adj->Adj[j] = tri1;
                break;
            }
        }
        if (j == 3)
        {
            assertion(false, "Unexpected condition.\n");
            return;
        }
    }

    tri0->Adj[i0p1] = tri1;
    tri1->Adj[i1p1] = tri0;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// IncrementalDelaunay2::Triangle
//----------------------------------------------------------------------------
template <typename Real>
IncrementalDelaunay2<Real>::Triangle::Triangle (int v0, int v1, int v2)
    :
    Time(-1),
    IsComponent(false),
    OnStack(false)
{
    V[0] = v0;
    V[1] = v1;
    V[2] = v2;
    Adj[0] = 0;
    Adj[1] = 0;
    Adj[2] = 0;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::Triangle::IsInsertionComponent (int posIndex,
    const Vector2<Real>& test, Triangle* adj,
    const IncrementalDelaunay2* delaunay)
{
    if (posIndex != Time)
    {
        Time = posIndex;

        // Determine the number of vertices in common with the supertriangle.
        // The supertriangle vertices have indices VQ-3, VQ-2, and VQ-1, where
        // VQ is the quantity of input vertices.
        int common = 0, svIndex = -1, j;
        for (j = 0; j < 3; ++j)
        {
            // The supervertices are at indices 0, 1, and 2, so loop counter
            // 'k' is the index into the supervertices.
            for (int k = 0; k < 3; ++k)
            {
                if (V[j] == k)
                {
                    common++;
                    svIndex = j;
                }
            }
        }

        int relation;
        if (common == 0)
        {
            // The classic case is that a point is in the mesh formed only by
            // the input vertices, in which case we only test for containment
            // in the circumcircle of the triangle.
            relation = delaunay->ToCircumcircle(test, V[0], V[1],
                V[2]);
        }
        else
        {
            // The classic problem is that points outside the mesh formed
            // only by the input vertices must be handled from a visibility
            // perspective rather than using circumcircles (compare with
            // convex hull construction).  By not doing this, you can run into
            // the pitfall that has snared many folks--the boundary edges of
            // the final triangulation do not form a convex polygon.
            int v0, v1;
            if (common == 1)
            {
                v0 = V[(svIndex + 1) % 3];
                v1 = V[(svIndex + 2) % 3];
            }
            else  // iCommon == 2
            {
                for (j = 0; j < 3; ++j)
                {
                    if (Adj[j] != 0 && Adj[j] != adj)
                    {
                        break;
                    }
                }
                v0 = V[j];
                v1 = V[(j + 1) % 3];
            }
            relation = delaunay->ToLine(test, v0, v1);
        }

        IsComponent = (relation < 0);
    }

    return IsComponent;
}
//----------------------------------------------------------------------------
template <typename Real>
int IncrementalDelaunay2<Real>::Triangle::DetachFrom (int adjIndex,
    Triangle* adj)
{
    assertion(0 <= adjIndex && adjIndex < 3 && Adj[adjIndex] == adj,
        "Invalid inputs\n");
    Adj[adjIndex] = 0;
    for (int i = 0; i < 3; ++i)
    {
        if (adj->Adj[i] == this)
        {
            adj->Adj[i] = 0;
            return i;
        }
    }
    return -1;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// IncrementalDelaunay2::Edge
//----------------------------------------------------------------------------
template <typename Real>
IncrementalDelaunay2<Real>::Edge::Edge (int v0, int v1, int nullIndex,
    Triangle* tri)
    :
    VEManifoldMesh::Edge(v0, v1)
{
    NullIndex = nullIndex;
    Tri = tri;
}
//----------------------------------------------------------------------------
template <typename Real>
VEManifoldMesh::EPtr IncrementalDelaunay2<Real>::Edge::ECreator (int v0,
    int v1)
{
    return new0 Edge(v0, v1, 0, 0);
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// IncrementalDelaunay2::RPVertex
//----------------------------------------------------------------------------
template <typename Real>
IncrementalDelaunay2<Real>::RPVertex::RPVertex (int index, Triangle* tri,
    Triangle* adj)
    :
    Index(index),
    Tri(tri),
    Adj(adj),
    IsConvex(false),
    IsEarTip(false),
    IsSuperVertex(false),
    Weight(Math<Real>::MAX_REAL),
    VPrev(-1),
    VNext(-1),
    SPrev(-1),
    SNext(-1),
    EarRecord(0)
{
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// IncrementalDelaunay2::Triangulate
//----------------------------------------------------------------------------
template <typename Real>
IncrementalDelaunay2<Real>::Triangulate::Triangulate (
    std::vector<RPVertex>& polygon, int removal,
    IncrementalDelaunay2* delaunay)
    :
    mPolygon(polygon),
    mNumVertices((int)polygon.size()),
    mDelaunay(delaunay),
    mCFirst(-1),
    mCLast(-1),
    mRFirst(-1),
    mRLast(-1),
    mEHeap((int)polygon.size(), 1, Math<Real>::MAX_REAL)
{
    // Create a circular list of the polygon vertices for dynamic removal of
    // vertices.
    int numVerticesM1 = mNumVertices - 1;
    int i;
    for (i = 0; i <= numVerticesM1; ++i)
    {
        RPVertex& vertex = V(i);
        vertex.VPrev = (i > 0 ? i-1 : numVerticesM1);
        vertex.VNext = (i < numVerticesM1 ? i+1 : 0);
    }

    // Create a circular list of the polygon vertices for dynamic removal of
    // vertices.  Keep track of two linear sublists, one for the convex
    // vertices and one for the reflex vertices.  This is an O(N) process
    // where N is the number of polygon vertices.
    for (i = 0; i <= numVerticesM1; ++i)
    {
        if (IsConvex(i))
        {
            InsertAfterC(i);
        }
        else
        {
            InsertAfterR(i);
        }
    }

    // Identify the ear tips and build a circular list of them.  Let V0, V1,
    // and/ V2 be consecutive vertices forming a triangle T (the ear).  The
    // vertex V1 is an ear tip if no other vertices of the polygon lie inside
    // T.  Although it is enough to show that V1 is not an ear by finding at
    // least one other vertex inside T, it is sufficient to search only the
    // reflex vertices.  This is an O(C*R) process, where C is the number of
    // convex vertices and R is the number of reflex vertices with N = C+R.
    // The order is O(N^2), for example when C = R = N/2.
    Real weight;
    int vPrev, vNext;
    for (i = mCFirst; i != -1; i = V(i).SNext)
    {
        if (IsEarTip(i))
        {
            weight = ComputeWeight(i, removal);
            V(i).EarRecord = mEHeap.Insert(i, weight);
            V(i).IsEarTip = true;
        }
    }

    // Remove the ears, one at a time.
    while (mNumVertices >= 3)
    {
        if (mNumVertices == 3)
        {
            // Only one triangle remains.  Erase the three subtriangles
            // linked to removal point P and then insert the remaining
            // triangle.
            assertion(mEHeap.GetNumElements() == 3,
                "Unexpected condition\n");

            mEHeap.Remove(i, weight);
            RPVertex& vertex0 = V(i);
            vPrev = vertex0.VPrev;
            vNext = vertex0.VNext;
            RPVertex& vertexP = V(vPrev);
            RPVertex& vertexN = V(vNext);
            Triangle* tri0 = vertex0.Tri;
            Triangle* triP = vertexP.Tri;
            Triangle* triN = vertexN.Tri;

            int i0;
            for (i0 = 0; i0 < 3; ++i0)
            {
                if (tri0->V[i0] == removal)
                {
                    break;
                }
            }
            if (i0 == 3)
            {
                assertion(false, "Unexpected condition\n");
                break;
            }
            tri0->V[i0] = vertexP.Index;

            int ip;
            for (ip = 0; ip < 3; ++ip)
            {
                if (triP->V[ip] == vertexP.Index)
                {
                    break;
                }
            }
            if (ip == 3)
            {
                assertion(false, "Unexpected condition\n");
                break;
            }
            Triangle* adj = triP->Adj[ip];
            tri0->Adj[i0] = adj;
            if (adj)
            {
                for (i = 0; i < 3; ++i)
                {
                    if (adj->V[i] == vertex0.Index)
                    {
                        adj->Adj[i] = tri0;
                        break;
                    }
                }
                if (i == 3)
                {
                    assertion(false, "Unexpected condition\n");
                    break;
                }
            }


            int in;
            for (in = 0; in < 3; ++in)
            {
                if (triN->V[in] == vertexN.Index)
                {
                    break;
                }
            }
            if (in == 3)
            {
                assertion(false, "Unexpected condition\n");
                break;
            }
            adj = triN->Adj[in];
            tri0->Adj[(i0 + 2) % 3] = adj;
            if (adj)
            {
                for (i = 0; i < 3; ++i)
                {
                    if (adj->V[i] == vertexP.Index)
                    {
                        adj->Adj[i] = tri0;
                        break;
                    }
                }
                if (i == 3)
                {
                    assertion(false, "Unexpected condition\n");
                    break;
                }
            }

            mDelaunay->mTriangle.erase(triP);
            mDelaunay->mTriangle.erase(triN);
            delete0(triP);
            delete0(triN);
            break;
        }

        mEHeap.Remove(i, weight);
        vPrev = V(i).VPrev;
        vNext = V(i).VNext;
        mDelaunay->SwapEdge(V(i).Adj, V(i).Tri);
        V(vPrev).Tri = V(i).Tri;
        RemoveV(i);

        // Removal of the ear can cause an adjacent vertex to become an ear
        // or to stop being an ear.
        RPVertex& vertexP = V(vPrev);
        if (vertexP.IsEarTip)
        {
            if (!IsEarTip(vPrev))
            {
                mEHeap.Update(V(vPrev).EarRecord, (Real)-1);
                mEHeap.Remove(i, weight);
                assertion(i == vPrev && weight == (Real)-1,
                    "Unexpected condition\n");
            }
        }
        else
        {
            bool wasReflex = !vertexP.IsConvex;
            if (IsConvex(vPrev))
            {
                if (wasReflex)
                {
                    RemoveR(vPrev);
                }

                if (IsEarTip(vPrev))
                {
                    weight = ComputeWeight(vPrev, removal);
                    V(vPrev).EarRecord = mEHeap.Insert(vPrev, weight);
                    V(vPrev).IsEarTip = true;
                }
            }
        }

        RPVertex& vertexN = V(vNext);
        if (vertexN.IsEarTip)
        {
            if (!IsEarTip(vNext))
            {
                mEHeap.Update(V(vNext).EarRecord, (Real)-1);
                mEHeap.Remove(i, weight);
                assertion(i == vNext && weight == (Real)-1,
                    "Unexpected condition\n");
            }
        }
        else
        {
            bool wasReflex = !vertexN.IsConvex;
            if (IsConvex(vNext))
            {
                if (wasReflex)
                {
                    RemoveR(vNext);
                }

                if (IsEarTip(vNext))
                {
                    weight = ComputeWeight(vNext, removal);
                    V(vNext).EarRecord = mEHeap.Insert(vNext, weight);
                    V(vNext).IsEarTip = true;
                }
            }
        }
    }
}
//----------------------------------------------------------------------------
template <typename Real>
typename IncrementalDelaunay2<Real>::RPVertex&
IncrementalDelaunay2<Real>::Triangulate::V (int i)
{
    return mPolygon[i];
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::Triangulate::IsConvex (int i)
{
    RPVertex& vertex = V(i);
    Vector2<Real> curr = mDelaunay->GetVertices()[vertex.Index];
    int prev = V(vertex.VPrev).Index;
    int next = V(vertex.VNext).Index;
    vertex.IsConvex = (mDelaunay->ToLine(curr, prev, next) > 0);
    return vertex.IsConvex;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IncrementalDelaunay2<Real>::Triangulate::IsEarTip (int i)
{
    RPVertex& vertex = V(i);

    if (mRFirst == -1)
    {
        // The remaining polygon is convex.
        vertex.IsEarTip = true;
        return true;
    }

    // Search the reflex vertices and test if any are in the triangle
    // <V[prev],V[curr],V[next]>.
    int prev = V(vertex.VPrev).Index;
    int curr = vertex.Index;
    int next = V(vertex.VNext).Index;
    vertex.IsEarTip = true;
    for (int j = mRFirst; j != -1; j = V(j).SNext)
    {
        // Check if the test vertex is already one of the triangle vertices.
        if (j == vertex.VPrev || j == i || j == vertex.VNext)
        {
            continue;
        }

        // Test if the vertex is inside or on the triangle.  When it is, it
        // causes V[curr] not to be an ear.
        Vector2<Real> test = mDelaunay->GetVertices()[V(j).Index];
        if (mDelaunay->ToTriangle(test, prev, curr, next) <= 0)
        {
            vertex.IsEarTip = false;
            break;
        }
    }

    return vertex.IsEarTip;
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::Triangulate::InsertAfterC (int i)
{
    if (mCFirst == -1)
    {
        // add first convex vertex
        mCFirst = i;
    }
    else
    {
        V(mCLast).SNext = i;
        V(i).SPrev = mCLast;
    }
    mCLast = i;
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::Triangulate::InsertAfterR (int i)
{
    if (mRFirst == -1)
    {
        // add first reflex vertex
        mRFirst = i;
    }
    else
    {
        V(mRLast).SNext = i;
        V(i).SPrev = mRLast;
    }
    mRLast = i;
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::Triangulate::RemoveV (int i)
{
    int currVPrev = V(i).VPrev;
    int currVNext = V(i).VNext;
    V(currVPrev).VNext = currVNext;
    V(currVNext).VPrev = currVPrev;
    --mNumVertices;
}
//----------------------------------------------------------------------------
template <typename Real>
void IncrementalDelaunay2<Real>::Triangulate::RemoveR (int i)
{
    assertion(mRFirst != -1 && mRLast != -1, "List must be nonempty\n");

    if (i == mRFirst)
    {
        mRFirst = V(i).SNext;
        if (mRFirst != -1)
        {
            V(mRFirst).SPrev = -1;
        }
        V(i).SNext = -1;
    }
    else if (i == mRLast)
    {
        mRLast = V(i).SPrev;
        if (mRLast != -1)
        {
            V(mRLast).SNext = -1;
        }
        V(i).SPrev = -1;
    }
    else
    {
        int currSPrev = V(i).SPrev;
        int currSNext = V(i).SNext;
        V(currSPrev).SNext = currSNext;
        V(currSNext).SPrev = currSPrev;
        V(i).SNext = -1;
        V(i).SPrev = -1;
    }
}
//----------------------------------------------------------------------------
template <typename Real>
Real IncrementalDelaunay2<Real>::Triangulate::ComputeWeight (int v0, int p)
{
    RPVertex& vertex0 = V(v0);
    assertion(vertex0.IsEarTip, "Vertex must be an ear tip\n");
    if (0 <= vertex0.Index && vertex0.Index <= 2)
    {
        // The vertex is a supervertex.  Return infinite weight so that the
        // supervertices are processed last.
        vertex0.IsSuperVertex = true;
        vertex0.Weight = Math<Real>::MAX_REAL;
        return vertex0.Weight;
    }

    // Get the adjacent vertices.
    int prev = vertex0.VPrev;
    int next = vertex0.VNext;
    RPVertex& vertexP = V(prev);
    RPVertex& vertexN = V(next);

    const Vector2<Real>& posP = mDelaunay->GetVertices()[vertexP.Index];
    const Vector2<Real>& pos0 = mDelaunay->GetVertices()[vertex0.Index];
    const Vector2<Real>& posN = mDelaunay->GetVertices()[vertexN.Index];
    const Vector2<Real>& posR = mDelaunay->GetVertices()[p];

    // Compute D.
    Real x0 = pos0[0] - posP[0];
    Real y0 = pos0[1] - posP[1];
    Real x1 = posN[0] - posP[0];
    Real y1 = posN[1] - posP[1];
    Real denom = x0*y1 - x1*y0;

    // Compute H.
    Real s0x = posP[0] + posR[0];
    Real d0x = posP[0] - posR[0];
    Real s0y = posP[1] + posR[1];
    Real d0y = posP[1] - posR[1];
    Real s1x = pos0[0] + posR[0];
    Real d1x = pos0[0] - posR[0];
    Real s1y = pos0[1] + posR[1];
    Real d1y = pos0[1] - posR[1];
    Real s2x = posN[0] + posR[0];
    Real d2x = posN[0] - posR[0];
    Real s2y = posN[1] + posR[1];
    Real d2y = posN[1] - posR[1];
    Real z0 = s0x*d0x + s0y*d0y;
    Real z1 = s1x*d1x + s1y*d1y;
    Real z2 = s2x*d2x + s2y*d2y;
    Real c00 = d1y*z2 - d2y*z1;
    Real c01 = d2y*z0 - d0y*z2;
    Real c02 = d0y*z1 - d1y*z0;
    Real numer = d0x*c00 + d1x*c01 + d2x*c02;

    vertex0.Weight = numer/denom;
    return vertex0.Weight;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class IncrementalDelaunay2<float>;

template WM5_MATHEMATICS_ITEM
class IncrementalDelaunay2<double>;
//----------------------------------------------------------------------------
}