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
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
#ifndef MESH_H
#define MESH_H

#include <iostream>
#include <cassert>
#include <fstream>
#include <math.h>
#include <string>
#include <limits>

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <algorithm>

#include <meshkit/array_qm.hpp>

#ifdef HAVE_MESQUITE
#include <Mesquite_all_headers.hpp>
#endif

#ifdef HAVE_VERDICT
#include <verdict.h>
#endif

#include "meshkit/myany.hpp"

#define FOR_EACH(container, iter ) for(iter = container.begin(); iter != container.end(); ++iter)

#include "basic_math.hpp"
#include "tfiblend.hpp"

#define JAAL_SUCCESS 0
#define JAAL_GEOMETRIC_FAILURE    1
#define JAAL_TOP0LOGICAL_FAILURE  100

#define Break() \
{       \
   cout << "Break in file " << __FILE__ << " at " << __LINE__ << endl; \
   getchar(); \
} \

///////////////////////////////////////////////////////////////////////////////

using namespace std;

typedef std::pair<size_t, size_t> FacePair;

#ifdef USE_HASHMAP
#include <tr1/unordered_set>
#include <tr1/unordered_map>
#endif

namespace Jaal {

class Vertex;
class Face;
class Edge;
class Mesh;

typedef Vertex* PNode;
typedef Edge* PEdge;
typedef Face* PFace;

typedef std::vector<PNode> NodeSequence;
typedef std::vector<PEdge> EdgeSequence;
typedef std::vector<PFace> FaceSequence;

typedef std::list<PNode> NodeList;
typedef std::list<PEdge> EdgeList;
typedef std::list<PFace> FaceList;

typedef std::set<PNode> NodeSet;
typedef std::set<PFace> FaceSet;

////////////////////////////////////////////////////////////////////////////////

template <class T>
int split_stl_vector(const std::vector<T> &a, size_t pos, std::vector<T> &b, std::vector<T> &c)
{
     size_t nSize = a.size();
     if (pos >= nSize ) return 1;

     b.resize(pos);

     size_t index = 0;
     for (size_t i = 0; i < pos; i++)
          b[index++] = a[i];

     c.resize(nSize - pos + 1);
     index = 0;

     for (size_t i = pos - 1; i < nSize; i++)
          c[index++] = a[i];

     return 0;
}

///////////////////////////////////////////////////////////////////////////////

template <class T>
int split_stl_vector(const std::deque<T> &a, size_t pos, std::deque<T> &b, std::deque<T> &c)
{
     size_t nSize = a.size();

     if (pos >= nSize) return 1;

     b.resize(pos);

     size_t index = 0;
     for (size_t i = 0; i < pos; i++)
          b[index++] = a[i];

     c.resize(nSize - pos + 1);
     index = 0;
     for (size_t i = pos - 1; i < nSize; i++)
          c[index++] = a[i];

     return 0;
}

////////////////////////////////////////////////////////////////////////////////

struct Attribute {
    public:
        Attribute() {}
        Attribute(const std::string &s, const myany &p) {
            name = s;<--- Variable 'name' is assigned in constructor body. Consider performing initialization in initialization list.
            value = p;
        }

        template <typename T> Attribute(T n) { value = n; }

        std::string name;
        myany value;
};

template<class T>
struct VecAttribute {
     std::vector<T> values;
};

template<class T, int n>
struct ArrayAttribute {     
      meshkit::array <T,n> values;
};

template<class T>
struct ListAttribute {
     std::list<T> values;
};

template<class T>
struct SetAttribute {
     std::set<T> values;
};

///////////////////////////////////////////////////////////////////////////////
struct RelationRep {
     void clearRelations(int t);

     void addRelation(Vertex *vertex);
     void addRelation(Edge *edge);
     void addRelation(Face* face);

     int  getNumRelations( int e ) const {
          if( e == 0) return relations0.size();
          if( e == 1) return relations1.size();
          if( e == 2) return relations2.size();
          return 0;
     }

     bool hasRelation(const Vertex* vertex) const;
     bool hasRelation(const Edge* edge) const;
     bool hasRelation(const Face* face) const;

     void removeRelation(const Vertex *vertex);
     void removeRelation(const Edge *edge);
     void removeRelation(const Face *face);

     int getRelations( NodeSequence &seq, bool cyclic_ordered = 0) const {
          seq.clear();
          size_t nSize = relations0.size();
          if( nSize == 0) return 1;
          seq.resize( nSize );
          for( size_t i = 0; i< nSize; i++)
               seq[i] = relations0[i];
          return 0;
     }

     int getRelations( EdgeSequence &seq, bool cyclic_ordered = 0) const {
          seq.clear();
          size_t nSize = relations1.size();
          if( nSize == 0) return 1;
          seq.resize( nSize );
          for( size_t i = 0; i< nSize; i++)
               seq[i] = relations1[i];
          return 0;
     }


     int getRelations( FaceSequence &seq, bool cyclic_ordered = 0) const {
          seq.clear();
          size_t nSize = relations2.size();
          if( nSize == 0) return 1;
          seq.resize( nSize );
          for( size_t i = 0; i< nSize; i++)
               seq[i] = relations2[i];
          return 0;
     }

     NodeSequence relations0; // vertex-vertex
     EdgeSequence relations1; // vertex-vertex
     FaceSequence relations2; // vertex-face
};

struct AttribRep {
     void removeAll() {
          for( size_t i = 0; i < attributes.size(); i++)
               delete attributes[i];
     }
     template<class T>
     int setAttribute(const string &s, const T &val) {
          int nAttribs = attributes.size();
          for( int i = 0; i < nAttribs; i++) {
               if( attributes[i]->name == s ) {
                    attributes[i]->value = val;
                    return 0;
               }
          }
          Attribute *a = new Attribute(s,val);
          attributes.push_back( a );
          return 0;
     }

     template<class T>
     int getAttribute(const string &s, T &val) const {
          int nAttribs = attributes.size();
          for( int i = 0; i < nAttribs; i++) {
               if( attributes[i]->name == s ) {
                    val = any_cast <T>( attributes[i]->value);
                    return 0;
               }
          }
          return 1;
     }

     int hasAttribute(const string &s) const {
          int nAttribs = attributes.size();
          for( int i = 0; i < nAttribs; i++)
               if( attributes[i]->name == s ) return 1;
          return 0;
     }

     void removeAttribute( const string &s) {
     }

     std::vector<Attribute*> attributes;
};

class MeshEntity {
public:

     typedef size_t idtype;
     static const int ACTIVE   = 0;
     static const int REMOVE   = 1;
     static const int INACTIVE = 2;

     MeshEntity() {<--- Member variable 'MeshEntity::gid' is not initialized in the constructor.
          visitMark = 0; // Default: Not yet visited.
          statusMark = ACTIVE; // Default: Active< not removable.
          constrained = 0; // Default: No Contrainted
          boundarymark = 0; // Default: Internal entity
          relationRep  = NULL;
          attribRep    = NULL;
     }

     ~MeshEntity() {
          if( attribRep ) attribRep->removeAll();
     }

     void setVisitMark(bool r) {
          visitMark = r;
     }

     bool isVisited() const {
          return visitMark;
     }

     int getStatus() const {
          return statusMark;
     }

     bool isRemoved() const {
          if (statusMark == REMOVE) return 1;
          return 0;
     }

     bool isActive() const {
          if (statusMark == ACTIVE) return 1;
          return 0;
     }

     bool isBoundary() const {
          return boundarymark;
     }

     void setBoundaryMark(int b) {
          boundarymark = b;
     }

     int getBoundaryMark() const {
          return boundarymark;
     }

     bool isConstrained() const {
          if (boundarymark > 0 || constrained) return 1;
          return 0;
     }

     void setConstrainedMark(int b) {
          constrained = b;
     }

     void setID(idtype id) {
          gid = id;
     }

     const idtype &getID() const {
          return gid;
     }

     template<class T>
     int setAttribute(const string &s, const T &val) {
          if( attribRep == NULL ) attribRep = new AttribRep;
          return attribRep->setAttribute(s,val);
     }

     template<class T>
     int getAttribute(const string &s, T &val) const {
          if( attribRep )
               return attribRep->getAttribute(s,val);
          return 1;
     }

     int hasAttribute(const string &s) const {
          if( attribRep )
               return attribRep->hasAttribute(s);
          return 0;
     }

     void  removeAttribute(const string &s) const {
          if( attribRep ) attribRep->removeAttribute(s);
     }

     void clearRelations(int e) {
          if( relationRep ) relationRep->clearRelations(e);
     }

     void addRelation(Vertex *vertex) {
          if( relationRep  == NULL ) relationRep = new RelationRep;
          relationRep->addRelation(vertex);
     }

     void addRelation(Edge  *edge) {
          if( relationRep  == NULL ) relationRep = new RelationRep;
          relationRep->addRelation(edge);
     }

     void addRelation(Face* face) {
          if( relationRep  == NULL ) relationRep = new RelationRep;
          relationRep->addRelation(face);
     }

     int  getNumRelations( int e ) const {
          if( relationRep )
               return relationRep->getNumRelations(e);
          return 0;
     }

     bool hasRelation(const Vertex* vertex) const {
          if( relationRep )
               return relationRep->hasRelation( vertex );
          return 0;
     }

     bool hasRelation(const Edge* edge) const {
          if( relationRep )
               return relationRep->hasRelation( edge );
          return 0;
     }

     bool hasRelation(const Face* face) const {
          if( relationRep )
               return relationRep->hasRelation( face );
          return 0;
     }

     void removeRelation(const Vertex *vertex) {
          if( relationRep ) relationRep->removeRelation( vertex );
     }
     void removeRelation(const Face *face) {
          if( relationRep ) relationRep->removeRelation( face );
     }

     int getRelations( NodeSequence &seq, bool cyclic_ordered = 0) const {
          seq.clear();
          if( relationRep )
               return relationRep->getRelations(seq,cyclic_ordered);
          return 1;
     }

     int getRelations( EdgeSequence &seq, bool cyclic_ordered = 0) const {
          seq.clear();
          if( relationRep )
               return relationRep->getRelations(seq,cyclic_ordered);
          return 1;
     }

     int getRelations( FaceSequence &seq, bool cyclic_ordered = 0) const {
          seq.clear();
          if( relationRep )
               return relationRep->getRelations(seq,cyclic_ordered);
          return 1;
     }

     void setStatus(int a) {
          statusMark = a;
     }

protected:
     idtype gid;
     int boundarymark;
     bool constrained;
     volatile bool visitMark;
     volatile short int statusMark;
     RelationRep *relationRep;<--- Class 'MeshEntity' is unsafe, 'MeshEntity::relationRep' can leak by wrong usage.
     AttribRep   *attribRep;
};

struct EntityRemovedPred {

     bool operator() (const MeshEntity * entity) const {
          if (entity) return entity->isRemoved();
          return 0;
     }
};

///////////////////////////////////////////////////////////////////////////////

struct BaseVertex : public MeshEntity {
};

class Vertex : public BaseVertex {
     static size_t global_id;
public:

     static PNode newObject();

     Vertex() {<--- Member variable 'Vertex::xyz' is not initialized in the constructor.
          visitMark = 0;
          statusMark = MeshEntity::ACTIVE;
          boundarymark = 0;
     }

     static double length(const Vertex *v0, const Vertex *v1);
     static double length2(const Vertex *v0, const Vertex *v1);
     static void   mid_point(const Vertex *v0, const Vertex *v1, Point3D &p, double alpha = 0.5);
     static Vertex* mid_node(const Vertex *v0, const Vertex *v1, double alpha = 0.5);
     static double point_orient( const Point3D &p0, const Point3D &p1, const Point3D &qpoint);

     void setXYZCoords(const Point3D &p) {
          xyz[0] = p[0];
          xyz[1] = p[1];
          xyz[2] = p[2];
     }

     const Point3D &getXYZCoords() const {
          return xyz;
     }

     Vertex* getClone() const;

     double getSpanAngle() const;
     double getFeatureLength() const;
     int get_ideal_face_degree( int n ) const;

private:
     Point3D xyz;
};

////////////////////////////////////////////////////////////////////////////////

struct LowVertexDegreeCompare {
     bool operator() (const Vertex *vertex1, const Vertex * vertex2) const {
          size_t d1 = vertex1->getNumRelations(0);
          size_t d2 = vertex2->getNumRelations(0);
          return d1 < d2;
     }
};
////////////////////////////////////////////////////////////////////////////////

struct HighVertexDegreeCompare {
     bool operator() (const Vertex *vertex1, const Vertex * vertex2) const {
          size_t d1 = vertex1->getNumRelations(0);
          size_t d2 = vertex2->getNumRelations(0);
          return d1 > d2;
     }
};
////////////////////////////////////////////////////////////////////////////////

struct LowLayerCompare {

     bool operator() (const Vertex *vertex1, const Vertex * vertex2) const {
          int val1, val2;
          vertex1->getAttribute("Layer", val1);
          vertex2->getAttribute("Layer", val2);
          return val1 < val2;
     }
};

///////////////////////////////////////////////////////////////////////////////

struct BaseEdge : public MeshEntity {
};

class Edge : public BaseEdge {
public:
     static PEdge newObject();

     Edge() {
     }

     Edge(PNode n1, PNode n2) {
          setNodes(n1, n2);
     }

     Vertex* getHashNode() const {
          return min(connect[0], connect[1]);
     }

     void setNodes(const NodeSequence &v) {
          assert(v.size() == 2);
          setNodes(v[0], v[1]);
     }

     void setNodes(Vertex *v1, Vertex *v2) {
          assert(v1 != v2);
          connect[0] = v1;
          connect[1] = v2;
     }

     const PNode &getNodeAt(int id) const {
          return connect[id];
     }

     bool isSameAs(const Edge &rhs) {
          if ((connect[0] == rhs.connect[0]) && (connect[1] == rhs.connect[1])) return 1;
          if ((connect[0] == rhs.connect[1]) && (connect[1] == rhs.connect[0])) return 1;
          return 0;
     }

     bool hasCrease() const {
          return 0;
     }

     Edge* getClone() const {
          Edge *newedge = new Edge(connect[0], connect[1]);
          return newedge;
     }

private:
     //  void * operator new( size_t size, void *);

     PNode connect[2];
};

///////////////////////////////////////////////////////////////////////////////

struct BaseFace : public MeshEntity {
};

class Face : public BaseFace {
public:
     static const int POLYGON = 0;
     static const int TRIANGLE = 3;
     static const int QUADRILATERAL = 4;

     static PFace newObject();

     static PFace create_quad(const PFace t1, const PFace t2, int replace = 0);

     // This function is for concave quads. Arrange vertices so that OpenGL can render them
     // correctly. When you save the mesh, probably the connectivity may change.
     static int quad_tessalate(const NodeSequence &orgNodes, NodeSequence &rotatedNodes);
     static int hexagon_2_quads(const NodeSequence &hexnodes, FaceSequence &quads, int start_from);

     static int is_3_sided_convex_loop_quad_meshable(const int *s, int *sdiv);
     static int is_5_sided_convex_loop_quad_meshable(const int *s, int *sdiv);
     static int is_cyclic_quad(const Point3D &p0, const Point3D &p1, const Point3D &p2, const Point3D &p3);

     // Centroid of Triangle element ...
     static Vertex *centroid(const Vertex *v0, const Vertex *v1, const Vertex *v2);

     // Centroid of Quad  element ...
     static Vertex *centroid(const Vertex *v0, const Vertex *v1, const Vertex *v2,
                             const Vertex *v3, const Vertex *v4);

     // Distortion of a triangle element ...
     static double distortion(const Vertex *v0, const Vertex *v1, const Vertex *v2);

     // Distortion of a quad element ...
     static double distortion(const Vertex *v0, const Vertex *v1, const Vertex *v2,
                              const Vertex *v3, const Vertex *v4);

     /////////////////////////////////////////////////////////////////////////////
     // Use modified Heron formula for find out the area of a triangle
     /////////////////////////////////////////////////////////////////////////////

     static double tri_area(const Point3D &p0, const Point3D &p1, const Point3D &p2);

     static Vec3D normal(const Vertex *v0, const Vertex *v1, const Vertex *v2);
     static Vec3D normal(const Point3D &p0, const Point3D &p1, const Point3D &v2);

     static void bilinear_weights(double xi, double eta, vector<double> &weight);
     static double linear_interpolation(const vector<double> &x, const vector<double> &w);

     /////////////////////////////////////////////////////////////////////////////
     // Calculate Area of Quadrilateral:
     // Example : Convex Case
     //           Coorindates  (0.0,0.0)  ( 1.0, 1.0), (2.0, 0.0), (1,0, -1.0)
     //           Result :  2*( 0.5*2.0*1.0 ) = 2.0;
     //           Concave case:
     //           Coodinates: ( 0.0, 0.0), 10, 2), (2.0, 0.0), (10, -2)
     //           Result : 2 *( 0.5*2.0* 2.0) = 4.0
     /////////////////////////////////////////////////////////////////////////////
     static double quad_area(const Point3D &p0, const Point3D &p1,
                             const Point3D &p2, const Point3D &p3);

     static bool is_convex_quad(const Point3D &p0, const Point3D &p1,
                                const Point3D &p2, const Point3D &p3);

     static PNode opposite_node(const PFace quad, const PNode n1);
     static PNode opposite_node(const PFace tri, PNode n1, PNode n2);
     static void opposite_nodes(const PFace quad, PNode n1, PNode n2,
                                PNode &n3, PNode &n4);

     static int check_on_boundary(const PFace tri);

     Face() {
          statusMark = 0;
          boundarymark = 0;
          visitMark = 0;
     }

     Face( const Vertex *v0, const Vertex *v1, const Vertex *v2) {
          NodeSequence seq(3);
          seq[0] = const_cast<Vertex*>(v0);
          seq[1] = const_cast<Vertex*>(v1);
          seq[2] = const_cast<Vertex*>(v2);
          setNodes(seq);
     }

     Face( const Vertex *v0, const Vertex *v1, const Vertex *v2, const Vertex *v3) {
          NodeSequence seq(4);
          seq[0] = const_cast<Vertex*>(v0);
          seq[1] = const_cast<Vertex*>(v1);
          seq[2] = const_cast<Vertex*>(v2);
          seq[3] = const_cast<Vertex*>(v3);
          setNodes(seq);
     }

     Vertex* getHashNode() const {
          return *min_element(connect.begin(), connect.end());
     }

     int getType() const {
          if (connect.size() == 3) return Face::TRIANGLE;
          if (connect.size() == 4) return Face::QUADRILATERAL;
          return Face::POLYGON;
     }

     int concaveAt() const;
     bool isSimple() const;

     bool isSameAs(const Face *rhs) const {
          if (rhs->getSize(0) != getSize(0)) return 0;

          for (int i = 0; i < getSize(0); i++)
               if (!rhs->hasNode(connect[i])) return 0;

          return 1;
     }

     bool hasNode(const PNode &vertex) const {
          if (find(connect.begin(), connect.end(), vertex) != connect.end()) return 1;
          return 0;
     }

     int getPosOf(const Vertex *vertex) const {
          int nSize = connect.size();
          for (int i = 0; i < nSize; i++)
               if (connect[i] == vertex) return i;

          return -1;
     }

     void reverse() {
          std::reverse(connect.begin(), connect.end());
     }

     int getOrientation(const Vertex *ev0, const Vertex *ev1) const {
          int  nSize = connect.size();

          for (int i = 0; i < nSize; i++) {
               Vertex *v0 = connect[(i + 0) % nSize];
               Vertex *v1 = connect[(i + 1) % nSize];
               if (v0 == ev0 && v1 == ev1) return +1;
               if (v0 == ev1 && v1 == ev0) return -1;
          }
          return 0;
     }

     int replaceNode(Vertex *oldvertex, Vertex *newvertex) {

          int  nSize = connect.size();
          for (int i = 0; i < nSize; i++) {
               if (connect[i] == oldvertex) {
                    connect[i] = newvertex;
                    return 0;
               }
          }
          return 1;
     }

     int getSize(int etype) const {
          if (etype == 0) return connect.size();
          return 0;
     }

     int setNodes(const NodeSequence &v) {
          int nSize = v.size();
          bool err = 0;
          for (int i = 0; i < nSize; i++) {
               for (int j = i+1; j < nSize; j++)
                    if(v[i] == v[j]) err = 1;
          }
          if( !err ) {
               connect = v;
          }
          return err;
     }

     const NodeSequence &getNodes() const {
          return connect;
     }

     const PNode &getNodeAt(size_t id) const {
          return connect[id%connect.size()];
     }

     void getAvgPos( Point3D &p) const;

     bool hasBoundaryNode() const {
          int nSize = connect.size();
          for (int i = 0; i < nSize; i++)
               if (connect[i]->isBoundary()) return 1;
          return 0;
     }

     bool has_all_bound_nodes() const;
     bool has_boundary_edge() const;

     int getRelations02( FaceSequence &seq);
     int getRelations12( FaceSequence &seq);
     int getRelations( NodeSequence &seq);

     bool isConvex() const {
          if (connect.size() <= 3) return 1;

          if (connect.size() == 4)
               return is_convex_quad(connect[0]->getXYZCoords(),
                                     connect[1]->getXYZCoords(),
                                     connect[2]->getXYZCoords(),
                                     connect[3]->getXYZCoords());

          cout << "Warning: General Polygon not supported " << endl;
          return 0;
     }

     double getAngleAt(const Vertex *v) const;

     int get_interior_angles(vector<double> &angles) const;

     double getAspectRatio();

     double getArea() {
          if (connect.size() == 3) {
               return tri_area(connect[0]->getXYZCoords(),
                               connect[1]->getXYZCoords(),
                               connect[2]->getXYZCoords());
          }

          if (connect.size() == 4) {
               return quad_area(connect[0]->getXYZCoords(),
                                connect[1]->getXYZCoords(),
                                connect[2]->getXYZCoords(),
                                connect[3]->getXYZCoords());
          }
          return 0.0;
     }

     int triangulate( vector<Face> &f) const;

     bool isValid() const;

     int refine_quad14(NodeSequence &newnodes, FaceSequence &newfaces);
     int refine_quad15(NodeSequence &newnodes, FaceSequence &newfaces);

     void start_from_concave_corner();

     Face* getClone() const {
          Face *newface = Face::newObject();<--- The extra qualification 'Face::' is unnecessary and is considered an error by many compilers.
          newface->setNodes(connect);
          return newface;
     }

private:
     NodeSequence connect;

     int refine_convex_quad15(NodeSequence &newnodes,  FaceSequence &newfaces);
     int refine_concave_quad15(NodeSequence &newnodes, FaceSequence &newfaces);
};

///////////////////////////////////////////////////////////////////////////////

struct BoundingBox {<--- The struct 'BoundingBox' does not have a constructor.

     double getLength(int d) {
          assert(d >= 0 && d < 3);
          return fabs(upperRightCorner[d] - lowerLeftCorner[d]);
     }

     void setLowerLeftCorner(const Point3D & p) {
          lowerLeftCorner = p;
     }

     const Point3D & getLowerLeftCorner() const {
          return lowerLeftCorner;
     }

     void setUpperRightCorner(const Point3D & p) {
          upperRightCorner = p;
     }

     const Point3D & getUpperRightCorner() const {
          return upperRightCorner;
     }

private:
     Point3D lowerLeftCorner;
     Point3D upperRightCorner;
};

inline
PNode
Vertex::newObject()
{
     Vertex *v = new Vertex;
     assert(v);
     v->setID(global_id);
     global_id++;
     return v;
}

///////////////////////////////////////////////////////////////////////////////

inline PNode Vertex::getClone() const
{
     Vertex *v = new Vertex;
     assert(v);
     v->setID(global_id);
     v->setXYZCoords(xyz);
     return v;
}

///////////////////////////////////////////////////////////////////////////////

inline PEdge Edge::newObject()
{
     Edge *e = new Edge;
     assert(e);
     return e;
}

///////////////////////////////////////////////////////////////////////////////

inline PFace Face::newObject()
{
     Face *f = new Face;
     assert(f);
     return f;
}

///////////////////////////////////////////////////////////////////////////////
inline double Face:: getAngleAt( const Vertex *v) const
{
     int pos = getPosOf(v);
     assert( pos >= 0);

     int nnodes = getSize(0);

     if( nnodes == 3 ) {
          const Point3D &p0 =  getNodeAt((pos+0)%nnodes)->getXYZCoords();
          const Point3D &p1 =  getNodeAt((pos+1)%nnodes)->getXYZCoords();
          const Point3D &p2 =  getNodeAt((pos+2)%nnodes)->getXYZCoords();
          return Math::getTriAngle(p0, p1, p2);
     }

     if( nnodes == 4 ) {
          const Point3D &p0 =  getNodeAt((pos+0)%nnodes)->getXYZCoords();
          const Point3D &p1 =  getNodeAt((pos+1)%nnodes)->getXYZCoords();
          const Point3D &p2 =  getNodeAt((pos+2)%nnodes)->getXYZCoords();
          const Point3D &p3 =  getNodeAt((pos+3)%nnodes)->getXYZCoords();
          double a1 = Math::getTriAngle(p0, p1, p2);
          double a2 = Math::getTriAngle(p0, p2, p3);
          return a1 + a2;
     }

     cout << "Error: Not implemented for general polygons " << endl;
     exit(0);
     return 0;
}

////////////////////////////////////////////////////////////////////////////////

inline double Vertex :: getSpanAngle() const
{
     FaceSequence vfaces;
     this->getRelations( vfaces );
     int nSize = vfaces.size();
     double sum = 0.0;
     for( int i = 0; i <  nSize; i++) {
          Face *face = vfaces[i];
          sum += face->getAngleAt( this );
     }
     return sum;
}
////////////////////////////////////////////////////////////////////////////////

#define TOPOLOGICAL_DISTANCE  0
#define EUCLIDEAN_DISTANCE    1
#define EUCLIDEAN_DISTANCE2   2
#define CITY_BLOCK_DISTANCE   3

///////////////////////////////////////////////////////////////////////////////

struct MeshFilter {

     virtual bool pass(const Vertex * v) const {
          cout << "Warning: passing the base " << endl;
          return 1;
     }
     virtual bool pass(const Face * v) const {
          return 1;
     }
    virtual ~MeshFilter() {
     }
        ;
};

class Mesh {
public:
     static NodeSequence generate_nodes(size_t n);
     static FaceSequence generate_faces(size_t n);

     static int getRelations112(const PNode v0, const PNode v1, FaceSequence &seq);
     static int getRelations102(const PNode v0, const PNode v1, FaceSequence &seq);
     static int make_chain(vector<Edge> &edges);
     static int is_closed_chain(const vector<Edge> &edges);
     static int is_closeable_chain(const vector<Edge> &edges);
     static int rotate_chain(vector<Edge> &edges, Vertex* start_vertex);
     static NodeSequence boundary_chain_nodes(Vertex *v0, Vertex *v1);
     static NodeSequence chain_nodes(const vector<Edge> &e);

     Mesh() {<--- Member variable 'Mesh::normalize_factor' is not initialized in the constructor.
          for (int i = 0; i < 4; i++) {
               for (int j = 0; j < 4; j++) adjTable[i][j] = 0;
          }
          hasdual = 0;
          meshname = "unknown";
          boundary_known = 0;
     }

     ~Mesh() {
          emptyAll();
          collect_garbage();
     }

     void  objects_from_pool( size_t n, NodeSequence &objects);
     void  objects_from_pool( size_t n, FaceSequence &objects);

     void setName(const string &s) {
          meshname = s;
     }

     string getName() const {
          return meshname;
     }

     void reserve(size_t nSize, int entity) {
          if (entity == 0) nodes.reserve(nSize);
          if (entity == 1) edges.reserve(nSize);
          if (entity == 2) faces.reserve(nSize);
     }

     bool hasDual() const {
          return hasdual;
     }

     void setDual(bool d) {
          hasdual = d;
     }

     int remove_nodes_attribute(const string &s);
     int remove_edges_attribute(const string &s);
     int remove_faces_attribute(const string &s);

     int isHomogeneous() const;

     int hasConvexCells();

     size_t count_convex_faces();
     size_t count_concave_faces();
     size_t count_inverted_faces();
     size_t count_irregular_nodes(int regdef);

     Mesh* deep_copy();
     Mesh* shallow_copy();

     BoundingBox getBoundingBox() const;

     int getNumComponents(bool stop_at_interface = 0);
     Mesh *getComponent(int id);

     int readFromFile(const string &f);
     bool getAdjTable(int i, int j) const {
          return adjTable[i][j];
     }
     Mesh * load(const string &fname, Mesh *m);
     Mesh *getPartMesh( int p);
     int  getNumPartitions(int e );

     int getEulerCharacteristic();

     Vertex* nearest_neighbour(const Vertex *v, double &d);

     size_t getSize(int d) {
          if (d == 0) return nodes.size();
          if (d == 1) return count_edges();
          if (d == 2) return faces.size();
          return 0;
     }

     size_t getCapacity(int d) {
          if (d == 0) return nodes.capacity();
          if (d == 2) return faces.capacity();
          return 0;
     }

     // If the topology of the mesh is changed, make sure to setBoundaryStatus = 0
     // so that it is again determined.

     void setBoundaryKnown(bool v) {
          boundary_known = v;
     }

     // If the boundary nodes and faces are knowm, return the value = 1. otherwise 0

     bool isBoundaryKnown() const {
          return boundary_known;
     }

     // A mesh is simple, when each edge is shared by at the most two faces which is
     // topological simple. A geometrically simple mesh will not have any crossing,
     // or overlapping edges, but this has not been checked right now.
     bool isSimple();

     // A mesh is consistently oriented when an edge shared by two faces, traversed in
     // opposite direction. Such a mesh will have proper normals ( either all inside or
     // or all outside ).
     bool is_consistently_oriented();

     //  Make the mesh consistent.
     void make_consistently_oriented();

     //  How many strongly connected components this mesh has ?
     int getNumConnectedComponents();

     //  Get #of boundary nodes, edges, and faces information.
     size_t getBoundarySize(int d) ;

     // Appends a node in the mesh.

     void addNode(PNode v) {
          nodes.push_back(v);
          v->setStatus(MeshEntity::ACTIVE);
     }

     // Appends a bulk of nodes in the mesh.

     void addNodes(const NodeSequence &vnodes) {
          size_t nSize = vnodes.size();
          for (size_t i = 0; i < nSize; i++)
               addNode(vnodes[i]);
     }

     void addNodes(NodeList &nlist) {
          while (!nlist.empty()) {
               PNode anode = nlist.front();
               nlist.pop_front();
               addNode(anode);
          }
     }

     int remove(PNode v);
     int deactivate(PNode v);
     int reactivate(PNode v);

     // Get the node at the specified position in the container.

     const PNode &getNodeAt(size_t id) const {
          assert(id < nodes.size());
          return nodes[id];
     }

     // Get All the nodes in the mesh after lazy prunning ( Garbage collection ).

     int getNodes(NodeSequence &seq) const {
          size_t nSize = nodes.size();

          seq.clear();
          seq.reserve( nSize );
          for( size_t i = 0; i <  nSize; i++) {
               Vertex *v = getNodeAt(i);
               if( v->isActive() ) seq.push_back(v);
          }
          return 0;
     }

     int getNodes(NodeList &l) const;
     int getFaces(FaceList &l) const;

     //
     // Return irregular nodes in the domain.
     // regular_count = 6   :  For triangle mesh;
     // regular_count = 4   :  For Quad mesh;
     // where         = 0   :  From the inner nodes only
     // where         = 1   :  From the boundary nodes only
     //
     int get_irregular_nodes(NodeSequence &seq, int regular_count, int where = 0);

     const PEdge &getEdgeAt(size_t id) const {
          assert(id < edges.size());
          if( edges[id]->getNodeAt(0)->isRemoved() || edges[id]->getNodeAt(1)->isRemoved()  )
               edges[id]->setStatus( MeshEntity::REMOVE );
          return edges[id];
     }

     int get_sharp_edges(EdgeSequence &e, double angle); //Specify the angle in degree.

     void addFeatureEdge(PEdge e) {
          feature_edges.push_back(e);
          //
          // Feature edge vertices are always constrained, by design. some codes
          // will break if this is modified in future.
          //
          Vertex *v;

          v = e->getNodeAt(0);
          v->setConstrainedMark(1);

          v = e->getNodeAt(1);
          v->setConstrainedMark(1);
     }

     bool hasFeatureEdge(const Edge &query_edge) const {
          size_t nSize = feature_edges.size();
          for (size_t i = 0; i < nSize; i++)
               if (feature_edges[i]->isSameAs(query_edge)) return 1;
          return 0;
     }

     // Add a face in the mesh. No duplication is checked..
     int addFace(PFace f);
     int remove(PFace f);

     int deactivate(PFace f);
     int reactivate(PFace f);

     // Add bulk of faces in the mesh. No duplication is checked..

     void addFaces(FaceSequence &vfaces) {
          size_t nSize = vfaces.size();
          for (size_t i = 0; i < nSize; i++)
               addFace(vfaces[i]);
     }

     void addFaces(FaceList &flist) {
          while (!flist.empty()) {
               PFace f = flist.front();
               flist.pop_front();
               addFace(f);
          }
     }

     // Get the face at the specified position in the container.

     const PFace &getFaceAt(size_t id) const {
          assert(id < faces.size());
          return faces[id];
     }

     bool contains(const Vertex *v) const {
          if (find(nodes.begin(), nodes.end(), v) == nodes.end()) return 0;
          return 1;
     }

     bool contains(const Face *f) const {
          if (find(faces.begin(), faces.end(), f) == faces.end()) return 0;
          return 1;
     }

     // Get rid of entities which are marked "removed" from the mesh.
     // a la Lazy garbage collection.
     //
     void prune();

     // Check if the lazy garbage collection is performed..
     bool isPruned() const;

     // Renumber mesh entities starting from index = 0
     void enumerate(int etype);

     // Search the boundary of the mesh (nodes, edges, and faces).
     int search_boundary();

     // Build entity-entity relations.

     int build_relations(int src, int dst, bool rebuild = 0) {
          if (src == 0 && dst == 0) return build_relations00( rebuild );
          if (src == 0 && dst == 1) return build_relations01( rebuild );
          if (src == 0 && dst == 2) return build_relations02( rebuild );
          if (src == 1 && dst == 2) return build_relations12( rebuild );
          return 1;
     }

     void normalize();

     // clean specified entity-entity relations.
     void clear_relations(int src, int dst);

     // Return all the edges of the primal mesh ...
     int getEdges( EdgeSequence &seq, int directed = 0) {
          if (edges.empty()) build_edges();
          seq = edges;
          return 0;
     }

     // Return all the edges of dual mesh...
     EdgeSequence getDualEdges() const;

     // Returm all the matching edges of the dual graph...
     EdgeSequence getMatchingDuals() const;

     // Filter out face.
     FaceSequence filter(int facetype) const;

     // Save the mesh and all its attributes ( in the simple format ).
     int saveAs(const string &s);

     // Empty every thing in the Mesh, but don't delete the objects.
     void emptyAll();

     // Empty every thing in the Mesh, and also deallocate all the objects.
     void deleteNodes();
     void deleteEdges();
     void deleteFaces();
     void deleteAll();

     // Reverse the connection of all the faces in the mesh. This will be flip
     // the normal of each face.

     void reverse() {

          size_t  nSize = faces.size();
          for (size_t i = 0; i < nSize; i++)
               faces[i]->reverse();
     }

     // Collect topological information. Ideally an internal vertex has 4 faces.
     vector<int> get_topological_statistics(int entity = 0, bool sorted = 1);

     // Collect nodes and faces in Depth First Sequence ...
     int get_depth_first_ordered_nodes(NodeSequence &seq, Vertex *v = NULL, MeshFilter *mf = NULL);
     int get_depth_first_ordered_faces(FaceSequence &seq, Face *f = NULL);

     // Collect nodes and faces in Breadth First Sequence ...
     int get_breadth_first_ordered_nodes(NodeSequence &seq, Vertex *f = NULL, MeshFilter *mf = NULL);
     int get_breadth_first_ordered_faces( FaceSequence &seq, Face *f = NULL);
     //
     // Creates waves of nodes/faces starting from the boundary. Each vertex/face
     // is assigned layerID, denoting the position in the wave fronts.
     // Used in smoothing the nodes in Advancing front style.
     //
     int setWavefront(int ofwhat);
     size_t setNodeWavefront(int layerid);
     size_t setFaceWavefront(int layerid);
     int verify_front_ordering(int ofwhat);

     NodeSet get_nearest_neighbors(const Point3D &p, int n = 1);
     NodeSet get_topological_neighbors(const NodeSequence &n, int k = 1);

     int remove_unreferenced_nodes();

     //
     // Connect a strip of faces. Termination occurs when the face is reached
     // (1) At the boundary (2) Return back to the starting face.
     // There will be two strips per quadrilateral which are orthogonal to each
     // other. Using in the Ring Collapse Simplification
     //
     void get_quad_strips(Face *rootface, FaceSequence &strip1,
                          FaceSequence &strip2);

     //
     void set_strip_markers();

     // Collect all the boundary nodes in the mesh..
     int get_bound_nodes( NodeSequence &seq);

     //
     // Collect all the boundary faces in the mesh. The boundary faces could be
     // defined in two ways, bound_what flag is used for that purpose.
     // bound_what  = 0   At least one vertex is on the boundary
     //             = 1   At least one edge is on the boundary.
     //
     int get_bound_faces( FaceSequence &seq, int bound_what = 1);

     //
     // Get the Aspect ratio of each face in the mesh. Aspect ratio is defined
     // as min_edge_length/max_edge_length..
     //
     vector<double> getAspectRatio(bool sorted = 1);

     //
     // Get unsigned surface area of the mesh. The calculation is for both
     // 2D and 3D surface elements...
     //
     double getSurfaceArea();

     // Check the Convexity..
     int check_convexity();

     // Collect Quadlity Statistics
     int get_quality_statistics(const string &s);

     // Get the nodes coordinates as a stream ( Used for interface with other
     // software. example Mesquite )
     int getCoordsArray( vector<double> &a, vector<size_t> &l2g);

     // Get the node connectivity as a stream ( Used for interface with other
     // software. example Mesquite )
     int getNodesArray( vector<size_t> &a, vector<int> &topo);

     // Reset the coordinates of nodes, ( Generally it comes from optimization
     // modules. The array size must match in order to reflect changes.
     int setCoordsArray(const vector<double> &v, const vector<size_t> &l2g);

     // Check if there are duplicate faces in the mesh;

     int hasDuplicates(int what);

     void getMinMaxVertexDegrees(int &mind, int &maxd);
     void getMinMaxFaceArea(double &mind, double &maxd);

     void setNormals();

     //
     // Refine the mesh into "n" segments and return newnodes and newfaces
     // inserted into  the mesh structure. Old faces are reused, i.e.
     // they are deactivated and then reactivated with new connectivity...
     //
     // Requirement: At least two segments must be given...
     int refine_tri_edge( Vertex *v0, Vertex *v1, int n,
                          NodeSequence &newnodes, FaceSequence &newfaces);

     int collapse_tri_edge( Vertex *v0, Vertex *v1);

     int refine_quads14();
     int refine_quads15();

     int refine_quad14(Face *f);
     int refine_quad15(Face *f);

     void collect_garbage();
     int search_quad_patches();

     void build_edges();
private:
     volatile char adjTable[4][4];
     size_t count_edges();
     string meshname;

     double normalize_factor;
     double getLength(int dir) const;

     bool hasdual;
     bool boundary_known;

     // Contains all the mesh entities.
     NodeSequence nodes;
     EdgeSequence edges;
     FaceSequence faces;

     // Contains the nodes, edges and faces as STL list instead of vector.
     // This is dynamic structure to allow efficient refinement purposes.
     //
     // Contains selected mesh entities.
     NodeSequence feature_nodes;
     EdgeSequence feature_edges;
     FaceSequence feature_faces;

     // Lazy garbage collection containers.
     NodeList garbageNodes;
     EdgeList garbageEdges;
     FaceList garbageFaces;

     // Build Topological relations...
     int build_relations00(bool rebuild = 0);
     int build_relations01(bool rebuild = 0);
     int build_relations02(bool rebuild = 0);
     int build_relations12(bool rebuild = 0);

     // Build wavefronts ...
     int setNodeWavefront();
     int setFaceWavefront();
};

//int mesh_unit_tests();

struct MeshImporter {<--- The struct 'MeshImporter' does not have a constructor.
     static const int SIMPLE_FORMAT = 0;
     static const int OFF_FORMAT = 1;
     static const int OBJ_FORMAT = 2;
     static const int VTK_FORMAT = 3;
     static const int TRIANGLE_FORMAT = 4;
     static const int CUBIT_FORMAT = 5;

     Mesh * load(const string &fname, Mesh *m = NULL) {
          mesh = m;
          if (mesh == NULL) mesh = new Mesh;

          int err = 1;
          if (fname.rfind(".vtk") != string::npos) {
               err = vtk_file(fname);
          }

          if (fname.rfind(".off") != string::npos) {
               err = off_file(fname);
          }

          if (fname.rfind(".dat") != string::npos) {
               err = simple_file(fname);
          }

          if (fname.rfind(".cub") != string::npos) {
               err = cubit_file(fname);
          }

          if (err) {
               err = triangle_file(fname);
          }

          if( err ) return NULL;

          if( !mesh->is_consistently_oriented() )
               mesh->make_consistently_oriented();

          global2local.clear();

          return mesh;
     }

private:
     Mesh *mesh;<--- Class 'MeshImporter' is unsafe, 'MeshImporter::mesh' can leak by wrong usage.
     std::map<int, int> global2local;
     void readTriNodes(const string & s);
     void readTriEdges(const string & s);
     void readTriFaces(const string & s);

     // Different format files ...
     int vtk_file(const string & s);
     int off_file(const string & s);
     int simple_file(const string & s);
     int triangle_file(const string & s);
     int xml_file(const string & s);
     int cubit_file(const string & s);
};

struct MeshExporter {

     static const int SIMPLE_FORMAT = 0;
     static const int OFF_FORMAT = 1;
     static const int OBJ_FORMAT = 2;
     static const int VTK_FORMAT = 3;
     static const int TRIANGLE_FORMAT = 4;
     static const int XML_FORMAT = 5;

     int saveAs(Mesh *m, const string & fname) {

          int err = 1;
          if (fname.rfind(".vtk") != string::npos) {
               err = vtk_file(m, fname);
          }

          if (fname.rfind(".off") != string::npos) {
               err = off_file(m, fname);
          }

          if (fname.rfind(".dat") != string::npos) {
               err = simple_file(m, fname);
          }

          if (fname.rfind(".xml") != string::npos) {
               err = xml_file(m, fname);
          }

          return err;
     }

private:
     int off_file(Mesh *m, const string & s);
     int simple_file(Mesh *m, const string & s);
     int vtk_file(Mesh *m, const string & s);
     int xml_file(Mesh *m, const string & s);
};

class MeshOptimization {<--- The class 'MeshOptimization' does not have a constructor.
public:
     static const int STEEPEST_DESCENT   = 0;
     static const int QUASI_NEWTON       = 1;
     static const int TRUST_REGION       = 2;
     static const int FEASIBLE_NEWTON    = 3;
     static const int CONJUGATE_GRADIENT = 4;
     static const int LAPLACIAN          = 5;

     int shape_optimize(Mesh * m, int algo = QUASI_NEWTON ,  int numiter = 10);

     int bandwidth_reduction(Mesh * m);
private:
     Mesh *inmesh;
     int execute(Mesh * m);

     int algorithm;
     int numIter;

     vector<int>     vfixed;
     vector<size_t>  vNodes;
     vector<int>     etopo;
     vector<size_t>  l2g;
     vector<double>  vCoords;

#ifdef HAVE_MESQUITE
     Mesquite::QualityImprover *improver;
     Mesquite::ArrayMesh* jaal_to_mesquite(Mesh *m);
#endif
};

////////////////////////////////////////////////////////////////////////////////

struct SurfPatch {
public:
     void build() {
          search_boundary();
     }

     int count_boundary_segments() {
          return corners.size();
     }

     bool isClosed();
     bool isSimple();

     NodeSequence get_boundary_segment_nodes(int i) {
          int ib = cornerPos[i];
          int ie = cornerPos[i + 1];
          return get_bound_nodes(bound_nodes[ib], bound_nodes[ie]);
     }

     FaceSet faces;
private:
     vector<int> segSize; // How many nodes on each segment.
     vector<int> cornerPos; // Positions of the corners in the bound_nodes.
     NodeSet corners; // Corners of the Blob
     NodeSet inner_nodes; // Inner nodes ( not on the boundary ) of the blob
     NodeSequence bound_nodes; // Boundary nodes
     vector<Edge> boundary; // boundary of the blob.
     NodeSequence get_bound_nodes(const Vertex *src, const Vertex * dst);
     int search_boundary();
     int getPosOf(const Vertex * v);
     void set_boundary_segments();
     int reorient_4_sided_loop();
     void start_boundary_loop_from(Vertex * v);
};

////////////////////////////////////////////////////////////////////////////////

inline void RelationRep::addRelation(Vertex *vertex)
{
     if (!hasRelation(vertex)) relations0.push_back(vertex);
     sort(relations0.begin(), relations0.end());
}

inline void RelationRep::addRelation(Edge *edge)
{
     if (!hasRelation(edge)) relations1.push_back(edge);
     sort(relations1.begin(), relations1.end());
}

inline void RelationRep ::addRelation(Face* face)
{
     if (!hasRelation(face)) relations2.push_back(face);
     sort(relations2.begin(), relations2.end());
}

inline void RelationRep::removeRelation(const Vertex *vertex)
{
     if (hasRelation(vertex)) {
          NodeSequence::iterator it;
          it = remove(relations0.begin(), relations0.end(), vertex);
          relations0.erase(it, relations0.end());
     }
}

inline void RelationRep::removeRelation(const Edge *edge)
{
     if (hasRelation(edge)) {
          EdgeSequence::iterator it;
          it = remove(relations1.begin(), relations1.end(), edge);
          relations1.erase(it, relations1.end());
     }
}

inline void RelationRep ::removeRelation(const Face *face)
{
     if (hasRelation(face)) {
          FaceSequence::iterator it;
          it = remove(relations2.begin(), relations2.end(), face);
          relations2.erase(it, relations2.end());
     }
}

inline void RelationRep ::clearRelations(int t)
{
     if (t == 0) relations0.clear();
     if (t == 1) relations1.clear();
     if (t == 2) relations2.clear();
}

inline bool RelationRep ::hasRelation(const Vertex* vertex) const
{
     if (relations0.empty()) return 0;

     NodeSequence::const_iterator it;

     it = find(relations0.begin(), relations0.end(), vertex);
     if (it == relations0.end()) return 0;

     return 1;
}

inline bool RelationRep ::hasRelation(const Edge* edge) const
{
     if (relations1.empty()) return 0;

     EdgeSequence::const_iterator it;

     it = find(relations1.begin(), relations1.end(), edge);
     if (it == relations1.end()) return 0;

     return 1;
}
///////////////////////////////////////////////////////////////////////////////

inline bool RelationRep ::hasRelation(const Face* face) const
{
     if (relations2.empty()) return 0;
     FaceSequence::const_iterator it;

     it = find(relations2.begin(), relations2.end(), face);
     if (it == relations2.end()) return 0;

     return 1;
}

///////////////////////////////////////////////////////////////////////////////

inline int Vertex::get_ideal_face_degree(int n) const
{
     if( n == 3 ) {
          if (!isBoundary()) return 6;

          if (getSpanAngle() <= 90.0 ) return 1;
          if (getSpanAngle() <= 220.0) return 3;
          if (getSpanAngle() <= 300.0) return 4;
          return 5;
     }

     if( n == 4 ) {
          if (!isBoundary()) return 4;

          if (getSpanAngle() <= 100 )  return 1;
          if (getSpanAngle() <= 220.0) return 2;
          if (getSpanAngle() <= 300.0) return 3;
          return 4;
     }

     cout << "Error: Ideal vertex degree only for Quad right now " << endl;
     exit(0);
}

///////////////////////////////////////////////////////////////////////////////

inline void Face::start_from_concave_corner()
{
     int pos = concaveAt();
     if (pos < 1) return;

     int nsize = getSize(0);
     NodeSequence rotated(nsize);
     for (int i = 0; i < nsize; i++)
          rotated[i] = connect[ (pos + i) % nsize ];
     connect = rotated;
}
///////////////////////////////////////////////////////////////////////////////

inline NodeSequence Mesh::generate_nodes(size_t n)
{
     assert(n > 0);
     NodeSequence seq(n);
     for (size_t i = 0; i < n; i++)
          seq[i] = Vertex::newObject();
     return seq;
}
///////////////////////////////////////////////////////////////////////////////

inline FaceSequence Mesh::generate_faces(size_t n)
{
     assert(n > 0);
     FaceSequence seq(n);
     for (size_t i = 0; i < n; i++)
          seq[i] = Face::newObject();
     return seq;
}
///////////////////////////////////////////////////////////////////////////////

inline int Mesh::remove(PNode v)
{
     if (v == NULL) return 1;

     if (v->isRemoved()) return 0;

     size_t nSize;<--- The scope of the variable 'nSize' can be reduced.

     FaceSequence vfaces;
     NodeSequence vneighs;

     if( v->isActive() ) {

          if (adjTable[0][2]) {
               v->getRelations( vfaces );
               nSize = vfaces.size();
               for (size_t i = 0; i < nSize; i++) {
                    if (!vfaces[i]->isRemoved()) {
                         cout << "Warning: The vertex is currently used by face: vertex not removed" << endl;
                         cout << "Debug : Vertex ID : " << v->getID() << endl;
                         cout << "Face:  ";
                         for( int j = 0; j < vfaces[i]->getSize(0); j++)
                              cout << vfaces[i]->getNodeAt(j)->getID() << " ";
                         cout << endl;
                         return 1;
                    }
               }
               v->clearRelations(2);
          }

          if (adjTable[0][0]) {
               v->getRelations( vneighs );
               nSize = vneighs.size();
               for (size_t i = 0; i < nSize; i++)
                    vneighs[i]->removeRelation(v);
               v->clearRelations(0);
          }
     }

     v->setStatus(MeshEntity::REMOVE);
     garbageNodes.push_back(v);

     return 0;
}

///////////////////////////////////////////////////////////////////////////////

inline int Mesh::addFace(PFace f)
{
     faces.push_back(f);
     int nSize = f->getSize(0);

     adjTable[2][0] = 1;

     if (adjTable[0][2]) {
          for (int i = 0; i < nSize; i++) {
               Vertex *v = f->getNodeAt(i);
               v->addRelation(f);
          }
     }

     if (adjTable[1][0]) {
          for (int i = 0; i < nSize; i++) {
               Vertex *v0 = f->getNodeAt(i);
               Vertex *v1 = f->getNodeAt(i+1);
               Vertex *vi = min( v0, v1);
               Vertex *vj = max( v0, v1);
               if( !vi->hasRelation(vj) ) {
                    Edge *edge = new Edge(vi, vj);
                    edges.push_back(edge);
                    vi->addRelation(vj);
                    vj->addRelation(vi);
               }
          }
     }

     if (adjTable[0][0]) {
          for (int i = 0; i < nSize; i++) {
               Vertex *vi = f->getNodeAt((i + 0));
               Vertex *vj = f->getNodeAt((i + 1));
               vi->addRelation(vj);
               vj->addRelation(vi);
          }
     }

     f->setStatus(MeshEntity::ACTIVE);
     return 0;
}

///////////////////////////////////////////////////////////////////////////////

inline int Mesh::remove(PFace f)
{
     if( f == NULL ) return 0;
     if( f->isRemoved() ) return 0;

     int nSize = f->getSize(0);

     FaceSequence vfaces;

     if( f->isActive() ) {
          if (adjTable[0][0]) {
               for (int i = 0; i < nSize; i++) {
                    Vertex *vi = f->getNodeAt(i + 0);
                    Vertex *vj = f->getNodeAt(i + 1);
                    Mesh::getRelations112(vi, vj, vfaces);
                    if (vfaces.size() == 1) {
                         vi->removeRelation(vj);
                         vj->removeRelation(vi);
                    }
               }
          }

          if (adjTable[0][2]) {
               for (int i = 0; i <  nSize; i++) {
                    Vertex *v = f->getNodeAt(i);
                    v->removeRelation(f);
               }
          }
     }
     f->setStatus(MeshEntity::REMOVE);
     garbageFaces.push_back(f);

     return 0;
}

///////////////////////////////////////////////////////////////////////////////

inline int Mesh::deactivate(PNode vi)
{
     assert(!vi->isRemoved());

     NodeSequence vneighs;
     if (adjTable[0][0]) {
          vi->getRelations( vneighs );
          int nSize = vneighs.size();
          for (int j = 0; j <  nSize; j++) {
               Vertex *vj = vneighs[j];
               vj->removeRelation(vi);
          }
          vi->clearRelations(0);
     }

     if (adjTable[0][2]) vi->clearRelations(2);

     vi->setStatus(MeshEntity::INACTIVE);

     return 0;
}
///////////////////////////////////////////////////////////////////////////////

inline int Mesh::deactivate(PFace face)
{
     // For the time being. but if you change the API, reimplement it.
     // The "remove" function can remove the elements immediately and
     // free the memory, but deactivate doesn't free it until garbage]
     // collection is called. Therefore, an element can be reused.
     // A deactivate element, looses all the information associated with
     // it.

     assert(!face->isRemoved());

     if (adjTable[0][2]) {
          for (int i = 0; i < face->getSize(0); i++) {
               Vertex *vertex = face->getNodeAt(i);
               vertex->removeRelation(face);
          }
     }

     face->setStatus(MeshEntity::INACTIVE);

     return 0;
}

///////////////////////////////////////////////////////////////////////////////

inline int Mesh::reactivate(PNode vertex)
{
     FaceSequence vfaces;
     if (adjTable[0][0]) {
          vertex->getRelations( vfaces );
          int numFaces = vfaces.size();
          for (int i = 0; i < numFaces; i++) {
               int nsize = vfaces[i]->getSize(0);
               for (int j = 0; j < nsize; j++) {
                    Vertex *vi = vfaces[i]->getNodeAt(i + 0);
                    Vertex *vj = vfaces[i]->getNodeAt(i + 1);
                    vi->addRelation(vj);
                    vj->addRelation(vi);
               }
          }
     }
     vertex->setStatus(MeshEntity::ACTIVE);

     return 0;
}

///////////////////////////////////////////////////////////////////////////////

inline int Mesh::reactivate(PFace face)
{
     int nsize = face->getSize(0);

     if (adjTable[0][0]) {
          for (int i = 0; i < nsize; i++) {
               Vertex *vi = face->getNodeAt(i + 0);
               Vertex *vj = face->getNodeAt(i + 1);
               assert(!vi->isRemoved());
               assert(!vj->isRemoved());
               vi->addRelation(vj);
               vj->addRelation(vi);
          }
     }

     if (adjTable[0][2]) {
          for (int i = 0; i < nsize; i++) {
               Vertex *vi = face->getNodeAt(i);
               assert(!vi->isRemoved());
               vi->addRelation(face);
          }
     }

     face->setStatus(MeshEntity::ACTIVE);
     return 0;
}

///////////////////////////////////////////////////////////////////////////////

Mesh *create_structured_mesh(double *origin, double *length, int *griddim, int spacedim);
Mesh* quad_to_tri4( Mesh *quadmesh, vector<Vertex*> &steiner);
Mesh* quad_to_tri2( Mesh *quadmesh );

void set_tfi_coords(int i, int j, int nx, int ny, vector<Vertex*> &qnodes);

void linear_interpolation(Mesh *m, Vertex *v0, Vertex *v1, int n, NodeSequence &seq);

void advancing_front_triangle_cleanup ( Mesh *mesh );
//
// QTrack is similar to "Motorcycle graph" proposed by Eppestein group.
// But somehow, I don't like this term.
//

struct QTrack {
     const static int END_AT_TERMINALS = 0;
     const static int END_AT_CROSSINGS = 1;

     Mesh *mesh;

     NodeSequence sequence;

     bool operator ==(const QTrack & rhs) const {
          size_t nSize = sequence.size();
          if (nSize != rhs.sequence.size()) return 0;

          Vertex *v0src = sequence.front();
          Vertex *v0dst = sequence.back();
          Vertex *v1src = rhs.sequence.front();
          Vertex *v1dst = rhs.sequence.back();

          if (v0src == v1src && v0dst == v1dst) return 1;
          if (v0src == v1dst && v0dst == v1src) return 1;

          return 0;
     }

     bool operator<(const QTrack & rhs) const {
          return sequence.size() < rhs.sequence.size();
     }

     /////////////////////////////////////////////////////////////////////////////
     // There are two ways to advance along the track.
     // (1) Incremental:  All track propagate simulateneously and
     //                   at the intersection, only one is allowed
     //                   to proceed towards other irregular node.
     // (11) Greedy   :   Start from one vertex and complete its
     //                   track.
     // It is not clear which method is better, but "greedy" may likely
     // give very high aspect ratio quad patches. Incremental on the
     // other hand may produce many small patches..
     //
     /////////////////////////////////////////////////////////////////////////////

     void advance(int endat);
private:
     int advance_single_step(int endat);<--- Unused private function: 'QTrack::advance_single_step'
};

vector<QTrack> generate_quad_partitioning(Mesh *mesh);

///////////////////////////////////////////////////////////////////////////////

struct StructuredMesh2D {

     StructuredMesh2D() {<--- Member variable 'StructuredMesh2D::myID' is not initialized in the constructor.
          nx = ny = 0;
     }
     int nx, ny;

     FaceSequence faces;
     NodeSequence nodes;

     NodeSet cornerNodes;

     void clearAll() {
          nodes.clear();
          faces.clear();
          neighs.clear();
          cornerNodes.clear();
          nx = 0;
          ny = 0;
     }

     bool operator<(const StructuredMesh2D & rhs) const {
          return this->getSize(2) < rhs.getSize(2);
     }

     size_t getSize(int e) const {
          if (e == 0) return nodes.size();
          if (e == 2) return faces.size();
          return 0;
     }

     int myID;
     vector<int> neighs;
};

///////////////////////////////////////////////////////////////////////////////
// Set Tag Values for Visualzation and sometimes debugging
///////////////////////////////////////////////////////////////////////////////
void set_layer_tag(Mesh *m, const string &s = "Layer");
void set_inverted_tag(Mesh *m, const string &s = "Inverted" );
void set_boundary_tag(Mesh *m, const string &s = "Boundary");
void set_partition_tag(Mesh *m, const string &s = "Partition");
void set_convexity_tag(Mesh *m , const string &s = "Convexity" );
void set_regular_node_tag(Mesh *m, const string &s = "Regularity" );

void set_visit_tags(Mesh *m);
void set_allboundnodes_tag(Mesh *m);
void set_constrained_tag(Mesh *m);
void set_feature_angle_tag(Mesh *m);
void set_ideal_node_tag(Mesh *m, int elemtype);
void set_large_area_tag(Mesh *m);
void set_tiny_area_tag(Mesh *m, double val = 1.0E-06);
void set_irregular_path_tag(Mesh *m, vector<QTrack> &qp);

///////////////////////////////////////////////////////////////////////////////
// Graph Matching operations ....
///////////////////////////////////////////////////////////////////////////////

int quadrangulate(Mesh *mesh);

////////////////////////////////////////////////////////////////////////////////
//Helper functions ....
////////////////////////////////////////////////////////////////////////////////
Mesh *struct_tri_grid(int nx, int ny);
Mesh *struct_quad_grid(int nx, int ny);

////////////////////////////////////////////////////////////////////////////////
// Mesh Optimization ...
////////////////////////////////////////////////////////////////////////////////

struct LaplaceWeight {
     virtual double get(const Vertex *apex, const Vertex * neigh) = 0;
};

struct LaplaceNoWeight : public LaplaceWeight {

     double get(const Vertex *apex, const Vertex * neigh) {
          return 1.0;
     }
};

struct LaplaceLengthWeight : public LaplaceWeight {

     double get(const Vertex *apex, const Vertex * neigh) {
          double len = Vertex::length(apex, neigh);
          return len;
     }
};

struct LaplaceAreaWeight : public LaplaceWeight {

     FaceSequence  vfaces;
     double get(const Vertex *apex, const Vertex * neighs) {
          neighs->getRelations( vfaces );
          double area = 0.0;
          for (size_t i = 0; i < vfaces.size(); i++)
               area += vfaces[i]->getArea();
          return area;
     }
};

class LaplaceSmoothing {
public:

     LaplaceSmoothing(Mesh *m, int n = 100) {
          mesh = m;
          numIters = n;
          verbose = 0;
          lambda = 1.0;
          lapweight = NULL;
          method = 0;
          maxerror = 0;
     }

     void setMethod(int m) {
          method = m;
     }

     void setNumIterations(int n) {
          numIters = n;
     }

     int execute();

     void setWeight(LaplaceWeight *w) {
          lapweight = w;
     }

     int localized_at(const NodeSequence &q);

     int convexify();

     double getError() {
          return maxerror;
     }

private:
     Mesh *mesh;
     int global_smoothing();

     struct LVertex {
          Vertex *apex;
          map<Vertex*, NodeSequence> neighs;
     };

     vector<LVertex> lnodes;
     LaplaceWeight *lapweight;
     vector<double> backupCoordsArray;
     vector<double> facearea;
     int method;
     int verbose;
     int numIters;
     double lambda;
     double maxerror;
     Point3D displace(const Point3D &src, const Point3D &dst, double r);

     double update_vertex_position(Vertex *vertex);
     double update_vertex_position(Vertex *vertex, const Point3D &p);
};

int quad_concave_tests();
} // namespace Jaal

void plot_all_quad_quality_measures( Jaal::Mesh *mesh );
void plot_all_tri_quality_measures( Jaal::Mesh *mesh );

#endif