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
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
//----------------------------------------------------------
//Class: GeomMeasureTool
//Description: Does Geometric measureing and statistical
//             gathering on various topologies.
//Creator: David R. White
//Date: 7/9/2002
//----------------------------------------------------------


#include "GeomMeasureTool.hpp"
#include "RefEntity.hpp"
#include "RefVertex.hpp"
#include "RefEdge.hpp"
#include "RefFace.hpp"
#include "RefVolume.hpp"
#include "Body.hpp"
#include "RefGroup.hpp"
#include "RefEntityFactory.hpp"
#include "CoEdge.hpp"
#include "Loop.hpp"
#include "Shell.hpp"
#include "Chain.hpp"
#include "CastTo.hpp"
#include "GMem.hpp"
#include "Curve.hpp"
#include "Surface.hpp"
#include "GeometryQueryEngine.hpp"
#include "GeomSeg.hpp"
#include "GeomPoint.hpp"
#include "RTree.hpp"
#include "AbstractTree.hpp"
#include "IntersectionTool.hpp"
#include "CpuTimer.hpp"
#include "GeometryQueryTool.hpp"
#include "GeometryModifyTool.hpp"
#include "MergeTool.hpp"
#include "GeomDataObserver.hpp"
#include "Lump.hpp"
#include "ModelQueryEngine.hpp"
#include "ProgressTool.hpp"
#include "AppUtil.hpp"

#include <set>
#include <map>
#include <stack>
#include <algorithm>

void GeomMeasureTool::get_edges_from_list(DLIList <RefEntity*> &entity_list,
                                          DLIList <RefEdge*> &ref_edges )
{
    //assume the mark is zero.
  int ii,jj;
  RefEntity *entity_ptr;
  RefEdge *ref_edge;
  
  TopologyEntity *topo_ent;
  DLIList <RefEdge*> tmp_edges;
  for ( ii = entity_list.size(); ii > 0; ii-- )
  {
    tmp_edges.clean_out();
    entity_ptr = entity_list.get_and_step();
    topo_ent = CAST_TO(entity_ptr, TopologyEntity);
    if ( topo_ent == NULL )
      continue;
    topo_ent->ref_edges(tmp_edges);
    for ( jj = tmp_edges.size(); jj > 0; jj-- )
    {
      ref_edge = tmp_edges.pop();
      if ( !ref_edge->marked() )
      {
        ref_edge->marked(1);
        ref_edges.append(ref_edge);
      }
    }
  }
  for ( ii = ref_edges.size(); ii > 0; ii-- )
    ref_edges.get_and_step()->marked(0);
}

void GeomMeasureTool::get_bodies_from_list(DLIList <RefVolume*> &entity_list,
                                           DLIList <Body*> &ref_bodies )
{
    //assume the mark is zero.
  int ii, jj;
  RefVolume *entity_ptr;
  Body *ref_body;
  
  TopologyEntity *topo_ent;
  DLIList <Body*> tmp_bodies;
  for ( ii = entity_list.size(); ii > 0; ii-- )
  {
    tmp_bodies.clean_out();
    entity_ptr = entity_list.get_and_step();
    topo_ent = CAST_TO(entity_ptr, TopologyEntity);
    if ( topo_ent == NULL )
      continue;
    topo_ent->bodies(tmp_bodies);
    for ( jj = tmp_bodies.size(); jj > 0; jj-- )
    {
      ref_body = tmp_bodies.pop();
      if ( !ref_body->marked() )
      {
        ref_body->marked(1);
        ref_bodies.append(ref_body);
      }
    }
  }
  for ( ii = ref_bodies.size(); ii > 0; ii-- )
    ref_bodies.get_and_step()->marked(0);
}

void GeomMeasureTool::get_faces_from_list(DLIList <RefEntity*> &entity_list,
                                          DLIList <RefFace*> &ref_faces )
{
    //assume the mark is zero.
  int ii,jj;
  RefEntity *entity_ptr;
  RefFace *ref_face;
  
  TopologyEntity *topo_ent;
  DLIList <RefFace*> tmp_faces;
  for ( ii = entity_list.size(); ii > 0; ii-- )
  {
    tmp_faces.clean_out();
    entity_ptr = entity_list.get_and_step();
    topo_ent = CAST_TO(entity_ptr, TopologyEntity);
    topo_ent->ref_faces(tmp_faces);
    for ( jj = tmp_faces.size(); jj > 0; jj-- )
    {
      ref_face = tmp_faces.pop();
      if ( !ref_face->marked() )
      {
        ref_face->marked(1);
        ref_faces.append(ref_face);
      }
    }
  }
  for ( ii = ref_faces.size(); ii > 0; ii-- )
    ref_faces.get_and_step()->marked(0);
}
void GeomMeasureTool::get_volumes_from_list(DLIList <RefEntity*> &entity_list,
                                            DLIList <RefVolume*> &ref_volumes )
{
    //assume the mark is zero.
  int ii,jj;
  RefEntity *entity_ptr;
  RefVolume *ref_volume;
  
  TopologyEntity *topo_ent;
  DLIList <RefVolume*> tmp_volumes;
  for ( ii = entity_list.size(); ii > 0; ii-- )
  {
    tmp_volumes.clean_out();
    entity_ptr = entity_list.get_and_step();
    topo_ent = CAST_TO(entity_ptr, TopologyEntity);
    topo_ent->ref_volumes(tmp_volumes);
    for ( jj = tmp_volumes.size(); jj > 0; jj-- )
    {
      ref_volume = tmp_volumes.pop();
      if ( !ref_volume->marked() )
      {
        ref_volume->marked(1);
        ref_volumes.append(ref_volume);
      }
    }
  }
  for ( ii = ref_volumes.size(); ii > 0; ii-- )
    ref_volumes.get_and_step()->marked(0);
}
void GeomMeasureTool::measure_curve_length(DLIList <RefEntity*> &entity_list,
                                           double &smallest,
                                           RefEdge *&smallest_edge,
                                           double &largest,
                                           RefEdge *&largest_edge,
                                           double &average,
                                           double &sum)
{
  DLIList<RefEdge*> ref_edges;
  get_edges_from_list(entity_list, ref_edges);
  measure_curve_length(ref_edges, smallest, smallest_edge,
                       largest, largest_edge, average, sum);
  
}

void GeomMeasureTool::measure_curve_length(DLIList <RefEdge*> &ref_edges,
                                           double &smallest,
                                           RefEdge *&smallest_edge,
                                           double &largest,
                                           RefEdge *&largest_edge,
                                           double &average,
                                           double &sum)
{
  int ii;
  double curr_length, total_length = 0.0;
  smallest = CUBIT_DBL_MAX;
  largest = 0.0;
  RefEdge *curr_edge;
  for ( ii = ref_edges.size(); ii > 0; ii-- )
  {
    curr_edge = ref_edges.get_and_step();
    if( curr_edge->geometry_type() == POINT_CURVE_TYPE )
      continue;
    curr_length = curr_edge->measure();
    if ( curr_length < smallest )
    {
      smallest = curr_length;
      smallest_edge = curr_edge;
    }
    if ( curr_length > largest )
    {
      largest = curr_length;
      largest_edge = curr_edge;
    }
    total_length += curr_length;
  }
  if ( ref_edges.size() != 0 )
    average = total_length/ref_edges.size();
  sum = total_length;
  
}
void GeomMeasureTool::measure_face_curves( RefFace *ref_face,
                                           double &min_curve_length,
                                           double &max_curve_ratio,
                                           RefEdge *&min_ref_edge)
{
  DLIList<DLIList<CoEdge*> > co_edge_loops;
  ref_face->co_edge_loops(co_edge_loops);
  CoEdge *curr_co_edge, *next_co_edge;
  min_curve_length = CUBIT_DBL_MAX;
  double curve_length, next_curve_length, ratio;
  max_curve_ratio = -CUBIT_DBL_MAX;
  CubitBoolean ratio_set = CUBIT_FALSE;
  int ii, jj;
  for ( ii = co_edge_loops.size(); ii > 0; ii-- )
  {
    DLIList <CoEdge*> &co_edge_list = co_edge_loops.get_and_step();
    curve_length = co_edge_list.get()->get_ref_edge_ptr()->measure();
    for ( jj = co_edge_list.size(); jj > 0; jj-- )
    {
      curr_co_edge = co_edge_list.get_and_step();
      next_co_edge = co_edge_list.get();
      next_curve_length = next_co_edge->get_ref_edge_ptr()->measure();
        //find the smallest curve
      if ( curve_length < min_curve_length )
      {
        min_curve_length = curve_length;
        min_ref_edge = curr_co_edge->get_ref_edge_ptr();
      }
      if ( co_edge_list.size() > 1 )
      {
          //find the biggest change in curve lengths.
        if ( curve_length > next_curve_length )
        {
          if ( next_curve_length < CUBIT_RESABS )
            ratio = CUBIT_DBL_MAX;
          else
            ratio = curve_length / next_curve_length;
        }
        else
        {
          if ( curve_length < CUBIT_RESABS )
            ratio = CUBIT_DBL_MAX;
          else
            ratio = next_curve_length /curve_length;
        }
        curve_length = next_curve_length;
        if ( ratio > max_curve_ratio )
        {
          ratio_set = CUBIT_TRUE;
          max_curve_ratio = ratio;
        }
      }
    }
  }
  if ( !ratio_set )
    max_curve_ratio = -1.0;
}

        
  
  
CubitStatus GeomMeasureTool::measure_face_loops( RefFace *face,
                                                 double &min_distance_between_loops,
                                                 double &min_distance_in_one_loop,
                                                 double &min_angle,
                                                 double &max_angle,
                                                 double tolerance)
{
    //This is the most tricky of these functions.
    //To do this I'm going to facet the edges, then use an AbstractTree to
    //find the minimum distance between the loops and between a single
    //loop.
  PointLoopList boundary_point_loops;
  CubitStatus stat;
  stat = get_boundary_points(face, boundary_point_loops, tolerance);
  if ( stat != CUBIT_SUCCESS )
    return CUBIT_FAILURE;
  
  SegLoopList boundary_seg_loops;
  stat = convert_to_lines( boundary_point_loops,
                           boundary_seg_loops,
                           face);
  if ( stat != CUBIT_SUCCESS )
    return stat;

    //Now add the points to the AbstractTree.
  DLIList<AbstractTree<GeomSeg*>*> atree_list;
  AbstractTree<GeomSeg*> *curr_tree;
  SegList *seg_list;
  GeomSeg  *curr_seg, *next_seg;
  int ii,jj, kk;
  double angle;
  min_angle = CUBIT_DBL_MAX;
  max_angle = 0.0;
  double creation_time=0.0;
  CpuTimer creation_timer;
  const double angle_convert = 180.0/CUBIT_PI;
  boundary_seg_loops.reset();
  int num_segs = 0;
  for (ii = 0; ii < boundary_seg_loops.size(); ii++ )
  {
    seg_list = boundary_seg_loops.get_and_step();
    creation_timer.cpu_secs();
    curr_tree = new RTree<GeomSeg*>(GEOMETRY_RESABS);
    creation_time += creation_timer.cpu_secs();
    for ( jj = seg_list->size(); jj > 0; jj-- )
    {
        //build the r-tree.
      num_segs++;
      creation_timer.cpu_secs();
      curr_seg = seg_list->get_and_step();
      curr_tree->add(curr_seg);
      creation_time += creation_timer.cpu_secs();
        //calculate the interior angles.
      next_seg = seg_list->get();

      RefEntity* temp_ent = curr_seg->get_end()->owner();
        //only measure the angle at vertices.  This
        //should make things slightly faster.
      if ( CAST_TO(temp_ent, RefVertex) )
      {
        stat = interior_angle(curr_seg, next_seg,
                              face, angle);
        if ( stat != CUBIT_SUCCESS )
        {
          min_angle = 0.0;
          max_angle = 360.0;
        }
        else if ( angle < min_angle )
        {
          min_angle = angle;
        }
        else if ( angle > max_angle )
        {
          max_angle = angle;
        }
      }
    }
    atree_list.append(curr_tree);
  }
  min_angle *= angle_convert;
  max_angle *= angle_convert;

  if ( atree_list.size() <= 1 )
    min_distance_between_loops = -1.0;
  else
  {
      //determine the minimum distance between the loops.
    CpuTimer atree_time;
    atree_time.cpu_secs();
    PointList *curr_points;
    GeomPoint *curr_point;
    DLIList <GeomSeg*> nearest_neighbors;
    
    CubitVector curr_loc;
    double closest_dist;
    min_distance_between_loops = CUBIT_DBL_MAX;
    atree_list.reset();
    for ( ii = 0; ii < atree_list.size(); ii++ )
    {
      curr_points = boundary_point_loops.get_and_step();
      for ( jj = ii+1; jj < atree_list.size(); jj++ )
      {
        curr_tree = atree_list.next(jj);
          //now for every point in curr_points, find it's closest_point
          //in the curr_tree.
        for ( kk = 0; kk < curr_points->size(); kk++ )
        {
          curr_point = curr_points->get_and_step();
          curr_loc.set(curr_point->coordinates());
          nearest_neighbors.clean_out();
          curr_tree->k_nearest_neighbor(curr_loc,
                                        1, closest_dist, nearest_neighbors,
                                        dist_sq_point_data);
          if ( nearest_neighbors.size() == 0)
          {
              //hmm, not sure what is wrong here.
            PRINT_ERROR("Can't find closest point between loops.\n");
            return CUBIT_FAILURE;
          }
          if (closest_dist < min_distance_between_loops)
            min_distance_between_loops = closest_dist;
        }
      }
    }
    double time =  atree_time.cpu_secs();
    PRINT_DEBUG_139("Time to create atree: %f\n", creation_time);
    PRINT_DEBUG_139("Time for atree nn search: %f\n", time);
  }
  if ( min_distance_between_loops != -1.0 )
  min_distance_between_loops = sqrt(min_distance_between_loops);
    //Now find the min distance inside a loop.  This is tricky
    //in that we want to find distances that are across some area
    //and not just between adjacent curves.
  atree_list.reset();
  GeomSeg *tmp_seg;
  double dist;
  min_distance_in_one_loop = CUBIT_DBL_MAX;
  int k=5;
  CubitVector curr_loc;
  DLIList <GeomSeg*> nearest_neighbors;
  RefEntity *ent_1, *ent_2;
  TopologyEntity *top_1, *top_2;
  
  
  for ( ii = atree_list.size(); ii > 0; ii-- )
  {
    curr_tree = atree_list.get_and_step();
    seg_list = boundary_seg_loops.get_and_step();
    for ( jj = seg_list->size(); jj > 0; jj-- )
    {
      nearest_neighbors.clean_out();
      curr_seg = seg_list->get_and_step();
        //get the k closest segments.
      curr_loc.set(curr_seg->get_start()->coordinates());
      curr_tree->k_nearest_neighbor(curr_loc,
                                    k, dist, nearest_neighbors,
                                    dist_sq_point_data);
        //go through these nearest_neighbors and through out
        //the segments that are attached to curr_seg.
      for ( kk = 0; kk < nearest_neighbors.size(); kk++ )
      {
        tmp_seg = nearest_neighbors.get();
        if ( tmp_seg == curr_seg ||
             tmp_seg == curr_seg->get_prev() ||
             tmp_seg == curr_seg->get_next() )
          nearest_neighbors.remove();
      }
      if ( nearest_neighbors.size() == 0 )
        continue;
        //The min distance is the distance between the curr_loc
        //and the top nearest_neighbor.
      nearest_neighbors.reset();
      tmp_seg = nearest_neighbors.get();
      ent_1 = tmp_seg->owner();
      ent_2 = curr_seg->get_start()->owner();
      top_1 = CAST_TO(ent_1, TopologyEntity);
      top_2 = CAST_TO(ent_2, TopologyEntity);
      
      if ( top_1->is_directly_related(top_2) )
        continue;
      
        //we'll need to recompute it...
      dist = dist_sq_point_data(curr_loc, tmp_seg);
      if ( dist < min_distance_in_one_loop )
        min_distance_in_one_loop = dist;
    }
  }
  if ( min_distance_in_one_loop == CUBIT_DBL_MAX )
  {
    ent_1 = (RefEntity*)face;
    DLIList<RefEntity*> entities;
    entities.append(ent_1);
    double smallest, largest, ave, sum;
    RefEdge *small_e, *large_e;
    measure_curve_length(entities, smallest, small_e,
                         largest, large_e, ave, sum);
    min_distance_in_one_loop = smallest;
  }
  else
    min_distance_in_one_loop = sqrt(min_distance_in_one_loop);
    //clean up the memory.
  int ll,mm;
  atree_list.reset();
  boundary_seg_loops.reset();
  for ( ll = boundary_seg_loops.size(); ll > 0; ll-- )
  {
    delete atree_list.pop();
    seg_list = boundary_seg_loops.pop();
    for ( mm = seg_list->size(); mm > 0; mm-- )
      delete seg_list->pop();
    delete seg_list;
  }
		
  PointList *point_list;
  for ( ll = boundary_point_loops.size(); ll > 0; ll-- )
  {
    point_list = boundary_point_loops.pop();
    for ( mm = point_list->size(); mm > 0; mm-- )
    {
      GeomPoint *tmp_point = point_list->pop();
        //delete the CubitPointData
      delete tmp_point;
    }
    delete point_list;
  }
  return CUBIT_SUCCESS;
}
//-------------------------------------------------------------------
//Function: dist_point_data
//Computes the distance between the line segment and the vector position.
//This function is used to determine the nearest neigbhor search and
//gets passed to the AbstractTree.
//-------------------------------------------------------------------
double GeomMeasureTool::dist_sq_point_data( CubitVector &curr_point,
                                            GeomSeg *&curr_seg )
{
  double node[3];
  double point_0[3], point_1[3];
  node[0] = curr_point.x();
  node[1] = curr_point.y();
  node[2] = curr_point.z();
  CubitVector start_v = curr_seg->get_start()->coordinates();
  CubitVector end_v = curr_seg->get_end()->coordinates();
  point_0[0] = start_v.x();
  point_0[1] = start_v.y();
  point_0[2] = start_v.z();
  point_1[0] = end_v.x();
  point_1[1] = end_v.y();
  point_1[2] = end_v.z();

  IntersectionTool new_tool(GEOMETRY_RESABS);
  double t;
  double dist = new_tool.distance_point_line(node, point_0,
                                             point_1, t);
  if ( dist < 0.0 )
  {
      //This happens if the point is beyond the line segment, in which
      //case we just want the closer end point.
    if ( t < 0.0 )
    {
      dist = (curr_point -start_v).length_squared();
    }
    else
    {
      dist = (curr_point - end_v).length_squared();
    }
  }
  else
  {
      //I hate to do this but everything else is in
      //terms of distance squared.
    dist = dist*dist;
  }
  return dist;
}

CubitStatus GeomMeasureTool::convert_to_lines( PointLoopList &boundary_point_loops,
                                               SegLoopList &boundary_line_loops,
                                               RefFace *ref_face )
{
    //This does more than just converts the boundary points to line segments.  It also
    //Sets up a doubily linked list through the ImprintLineSegment data structure.
    //Calling codes need to make sure all this data is deleted.
    //This function also sets the PointType for each of the boundary points.  This
    //will depend also on the boundary_1 flag.  If we are doing boundary_1 then,
    //the flag will be true, otherwise it will be false.
  int ii, jj;
  PointList *point_list;
  SegList *segment_list;
  GeomSeg *new_line, *prev = NULL;
  SegList allocated_segs;
  RefEntity *entity_start, *entity_end, *seg_owner;
  boundary_point_loops.reset();
  for ( ii = boundary_point_loops.size(); ii > 0; ii-- )
  {
    point_list = boundary_point_loops.get_and_step();
    segment_list = new SegList;
    prev = NULL;
    new_line = NULL;
    seg_owner = NULL;
    for ( jj = point_list->size(); jj > 0; jj-- )
    {
      entity_start = point_list->get()->owner();
      entity_end = point_list->next()->owner();
      seg_owner =  CAST_TO(entity_start, RefEdge);
      if ( seg_owner == NULL )
      {
        seg_owner = CAST_TO(entity_end, RefEdge);
        if ( seg_owner == NULL )
        {
          RefVertex *ref_vert_end = CAST_TO(entity_end, RefVertex);
          RefVertex *ref_vert_start = CAST_TO(entity_start, RefVertex);
          DLIList <RefEdge*> common_edges;
          ref_vert_start->common_ref_edges(ref_vert_end,
                                           common_edges,
                                           ref_face);
          if ( common_edges.size() == 0  )
          {
            PRINT_ERROR("GeomMeasureTool::convert_to_lines having problem finding owner \n" 
                        "       of boundary.  Could be indicative of corrupt geometry.\n");
            int ll;
            for ( ll = boundary_line_loops.size(); ll > 0; ll-- )
              delete boundary_line_loops.pop();
            for ( ll = allocated_segs.size(); ll > 0; ll-- )
              delete allocated_segs.pop();
            delete segment_list;
            return CUBIT_FAILURE;
          }
          if ( common_edges.size() == 1 )
            seg_owner = common_edges.get();
          else
          {
              //Now we have to decide which of these edges is the one.
              //Lets take a mid point on this segment and check the distances.
              //First find the mid_point of the line segment.
            CubitVector start = point_list->get()->coordinates();
            CubitVector end = point_list->next()->coordinates();
            CubitVector mid = (start+end)/2.0;
            CubitVector closest_point;
            
              //Now test the edges to see which is closest.
            RefEdge *closest_edge = NULL, *curr_ref_edge;
            double min_dist = CUBIT_DBL_MAX, dist;
            int kk;
            for ( kk = common_edges.size(); kk > 0; kk-- )
            {
              curr_ref_edge = common_edges.get_and_step();
              curr_ref_edge->closest_point(mid, closest_point);
              dist = (mid - closest_point).length_squared();
              if ( dist < min_dist )
              {
                min_dist = dist;
                closest_edge = curr_ref_edge;
              }
            }
            if ( closest_edge == NULL )
            {
              PRINT_ERROR("Problems determining segment ownwership.\n"
                          "Internal problem with virtual imprinting.\n");
              int ll;
              for ( ll = boundary_line_loops.size(); ll > 0; ll-- )
                delete boundary_line_loops.pop();
              for ( ll = allocated_segs.size(); ll > 0; ll-- )
                delete allocated_segs.pop();
              delete segment_list;
              return CUBIT_FAILURE;
            }
            seg_owner = closest_edge;
          }
        }
      }
      new_line = new GeomSeg( point_list->get(), point_list->next(), seg_owner);
      allocated_segs.append(new_line);
      point_list->step();
      segment_list->append(new_line);
      if ( prev != NULL )
      {
        prev->set_next(new_line);
        new_line->set_prev(prev);
      }
      prev = new_line;
    }
    if ( prev != NULL )
    {
      segment_list->reset();
      assert(prev == segment_list->prev());
      segment_list->prev()->set_next(segment_list->get());
      segment_list->get()->set_prev(segment_list->prev());
      assert(segment_list->get()->get_next() != NULL );
    }
    if ( segment_list->size() )
    {
      boundary_line_loops.append(segment_list);
    }
    else
      delete segment_list;
  }
  return CUBIT_SUCCESS;
}

CubitStatus GeomMeasureTool::get_boundary_points( RefFace *ref_face,
                                                  PointLoopList &boundary_point_loops,
                                                  double seg_length_tol)
{
  DLIList<DLIList<CoEdge*> > co_edge_loops;
  ref_face->co_edge_loops(co_edge_loops);
  int ii, jj;
  PointList *new_point_loop_ptr, tmp_point_list;
  RefEdge *ref_edge_ptr;
  CoEdge *co_edge_ptr;
  CubitStatus stat;
  CubitSense sense;
  
  for ( ii = co_edge_loops.size(); ii > 0; ii-- )
  {
    DLIList <CoEdge*> &co_edge_list_ptr = co_edge_loops.get_and_step();
    new_point_loop_ptr = new PointList;
    for ( jj = co_edge_list_ptr.size(); jj > 0; jj-- )
    {
      co_edge_ptr = co_edge_list_ptr.get_and_step();
      ref_edge_ptr = co_edge_ptr->get_ref_edge_ptr();
      tmp_point_list.clean_out();
      stat = get_curve_facets( ref_edge_ptr, tmp_point_list,
                               seg_length_tol );
      if ( stat != CUBIT_SUCCESS )
        return CUBIT_FAILURE;
      if ( tmp_point_list.size() == 0 )
        continue;
      tmp_point_list.reset();
        //the points are in order from start vertex to end vertex.
        //append them now according to the loop.
      sense = co_edge_ptr->get_sense();
      if ( CUBIT_FORWARD != sense )
        tmp_point_list.reverse();
        //Now take off the last point as it is a duplicate with the
        //other list...
      tmp_point_list.reset();
      delete tmp_point_list.pop();
      (*new_point_loop_ptr) += tmp_point_list;
    }
    boundary_point_loops.append(new_point_loop_ptr);
  }
      
  return CUBIT_SUCCESS;
}

CubitStatus GeomMeasureTool::get_curve_facets( RefEdge* curve,
                                               PointList &segments,
                                               double seg_length_tol ) 
{
//  const double COS_ANGLE_TOL =  0.965925826289068312213715; // cos(15)
//  const double COS_ANGLE_TOL =  0.984807753012208020315654; // cos(10)
  //const double COS_ANGLE_TOL =  0.996194698091745545198705; // cos(5)
  GMem curve_graphics;
    //make this tol bigger than seg_length_tol, to
    //make sure the segments are larger than the tolerance.
  const double dist_tol = seg_length_tol;// + .05*seg_length_tol;
  //const double dist_tol_sqr = dist_tol*dist_tol;
  if ( curve->get_curve_ptr() == NULL )
  {
    PRINT_ERROR("Curve %d had no geometry, this is a bug!\n", curve->id());
    return CUBIT_FAILURE;
  }
  else if ( curve->get_curve_ptr()->get_geometry_query_engine() == NULL )
  {
    PRINT_ERROR("Curve %d had no geometry engine, this is a bug!\n",
                curve->id());
    return CUBIT_FAILURE;
  }
  curve->get_graphics( curve_graphics );
  
  GPoint* gp = curve_graphics.point_list();
  if ( gp == NULL )
  {
    if ( curve->measure() < GEOMETRY_RESABS ) 
    {
      if( curve->geometry_type() != POINT_CURVE_TYPE )
      {
        PRINT_INFO("Curve %d has a zero length, and won't be used for loop measurements.\n",
                    curve->id());
      }
    }
    else
      PRINT_ERROR("Curve %d had not faceting.  Ignoring this curve.\n",
                  curve->id());
    return CUBIT_SUCCESS;
  }
  GeomPoint *last = new GeomPoint( gp[0].x, gp[0].y, gp[0].z, curve );
  CubitVector lastv = last->coordinates();
  int num_points = curve_graphics.pointListCount;
  segments.append( last );
  int ii;
  CubitBoolean remove_second_to_end = CUBIT_FALSE;
  for ( ii = 1; ii < num_points; ii++ )
  {
    CubitVector pos(  gp[ii].x, gp[ii].y, gp[ii].z );
    CubitVector step1 = (pos - lastv);
    double len1 = step1.length();
    if( len1 < dist_tol && ii != num_points - 1) 
      continue;
    else if ( len1 < dist_tol && ii == num_points-1 )
    {
      remove_second_to_end = CUBIT_TRUE;
    }
    last = new GeomPoint(pos,curve);
    segments.append( last );
    lastv = last->coordinates();
  }
    // Now check if the segment list is reversed wrt the curve direction.
  segments.reset();
  if ( remove_second_to_end )
  {
    if ( segments.size() == 2 )
    {
        //just leave it in, let it be small...
    }
    else
    {
        //Remove the second to last one.  To do
        //this efficiently (don't do remove), pop
        //the last one, then save that and
        //re-add it after poping the second one.
      GeomPoint *temp = segments.pop();
      segments.pop();
      segments.append(temp);
    }
  }
  segments.reset();
  if( curve->start_vertex() != curve->end_vertex() )
  {
    CubitVector start_vec, end_vec;
    start_vec = curve->start_vertex()->coordinates();
    end_vec = curve->end_vertex()->coordinates();
    CubitVector start_seg = segments.get()->coordinates();
    double dist_1 = (start_seg - start_vec).length_squared();
    double dist_2 = (start_seg - end_vec).length_squared();
    if ( dist_1 > dist_2 )
      segments.reverse();
  }
  else
  {
    double u1, u2;
    u1 = curve->u_from_position( (segments.next(1)->coordinates()) );
    u2 = curve->u_from_position( (segments.next(2)->coordinates()) );    
    if( (u2 < u1) && (curve->start_param() <= curve->end_param()) )
      segments.reverse();
  }
    //clean up the periodic curve case (last seg may be too small.)
  if ( curve->start_vertex() == curve->end_vertex() )
  {
    segments.reset();
    CubitVector start_v = segments.get()->coordinates();
    CubitVector last_v = segments.prev()->coordinates();
    double dist = (start_v - last_v).length();
    if ( dist < dist_tol )
    {
        //remove the last one.
      segments.pop();
    }
  }
  if ( segments.size() < 2 )
  {
    PRINT_ERROR("Problem faceting boundary.\n");
    return CUBIT_FAILURE;
  }
  segments.get()->owner(curve->start_vertex());
  segments.prev()->owner(curve->end_vertex());
  return CUBIT_SUCCESS;
}
CubitStatus GeomMeasureTool::interior_angle(GeomSeg *first_seg,
                                            GeomSeg *next_seg,
                                            RefFace *face,
                                            double &angle )
{
  if ( first_seg->get_end() != next_seg->get_start() )
  {
    PRINT_ERROR("GeomMeasureTool::interior_angle expects segments\n"
                "to be connected (Users: this is a bug, please submit.\n");
    return CUBIT_FAILURE;
  }
  CubitVector point = first_seg->get_end()->coordinates();
  CubitVector to_prev = first_seg->get_start()->coordinates();
  to_prev -= point;
  CubitVector to_next = next_seg->get_end()->coordinates();
  to_next -= point;
  CubitVector surf_norm = face->normal_at(point);
  if ( surf_norm.length_squared() < CUBIT_RESABS )
  {
      //Try getting it at one of the other nodes...
    surf_norm = face->normal_at(first_seg->get_start()->coordinates() );
    if (surf_norm.length_squared() < CUBIT_RESABS )
    {
        //Try getting it at one of the other nodes...
      surf_norm = face->normal_at(next_seg->get_end()->coordinates() );
      if (surf_norm.length_squared() < CUBIT_RESABS )
      {
        PRINT_ERROR("Trying to get normal at surf %d.\n"
                    "       Normal length being returned equals zero.\n",
                    face->id() );
        angle = CUBIT_DBL_MAX;
        return CUBIT_FAILURE;
      }
    }
  }
  angle = surf_norm.vector_angle ( to_next, to_prev );
  return CUBIT_SUCCESS;
}

void GeomMeasureTool::measure_face_area (DLIList <RefEntity*> &entity_list,
                                         double &smallest,
                                         RefFace *&smallest_face,
                                         double &largest,
                                         RefFace *&largest_face,
                                         double &average,
                                         double &sum)
{
  DLIList<RefFace*> ref_faces;
  get_faces_from_list(entity_list, ref_faces);
  measure_face_area(ref_faces, smallest, smallest_face,
                    largest, largest_face, average, sum);
  
}

void GeomMeasureTool::measure_face_area (DLIList <RefFace*> &ref_faces,
                                         double &smallest,
                                         RefFace *&smallest_face,
                                         double &largest,
                                         RefFace *&largest_face,
                                         double &average,
                                         double &sum)
{
  int ii;
  double curr_area, total_area = 0.0;
  smallest = CUBIT_DBL_MAX;
  largest = 0.0;
  RefFace *curr_face;
  for( ii = ref_faces.size(); ii > 0; ii-- )
  {
    curr_face = ref_faces.get_and_step();
    curr_area = measure_area(curr_face);
    if ( curr_area < smallest )
    {
      smallest = curr_area;
      smallest_face = curr_face;
    }
    if( curr_area > largest )
    {
      largest = curr_area;
      largest_face = curr_face;
    }
    total_area += curr_area;
  }
  if (ref_faces.size() != 0 )
    average = total_area/ref_faces.size();
  sum = total_area;
  
}

void GeomMeasureTool::measure_volume_volume (DLIList <RefEntity*> &entity_list,
                                             double &smallest,
                                             RefVolume *&smallest_volume,
                                             double &largest,
                                             RefVolume *&largest_volume,
                                             double &average,
                                             double &sum)
{
  DLIList<RefVolume*> ref_volumes;
  get_volumes_from_list(entity_list, ref_volumes);
  measure_volume_volume(ref_volumes, smallest, smallest_volume,
                        largest, largest_volume, average, sum);
  
}

void GeomMeasureTool::measure_volume_volume (DLIList <RefVolume*> &ref_volumes,
                                             double &smallest,
                                             RefVolume *&smallest_volume,
                                             double &largest,
                                             RefVolume *&largest_volume,
                                             double &average,
                                             double &sum)
{
  int ii;
  double curr_volume, total_volume = 0.0;
  smallest = CUBIT_DBL_MAX;
  largest = 0.0;
  RefVolume *curr_solid;
  for ( ii = ref_volumes.size(); ii > 0; ii-- )
  {
    curr_solid = ref_volumes.get_and_step();
    curr_volume = curr_solid->measure();

    if ( curr_volume < smallest )
    {
      smallest = curr_volume;
      smallest_volume = curr_solid;
    }
    if ( curr_volume > largest )
    {
      largest = curr_volume;
      largest_volume = curr_solid;
    }
    total_volume += curr_volume;
  }
  if (ref_volumes.size() != 0 )
    average = total_volume/ref_volumes.size();
  sum = total_volume;
  
}

void GeomMeasureTool::measure_face_area_and_hydraulic_radius(RefFace *curr_face,
                                                             double &face_area,
                                                             double &face_hydraulic_radius)
{
  int ii;
  double total_edge_length = 0.0, curr_edge_length;
  RefEdge *curr_edge;
  DLIList <CoEdge*> my_co_edges;

  face_area = measure_area(curr_face);
  curr_face->co_edges(my_co_edges);
  for( ii = my_co_edges.size(); ii > 0; ii-- )
  {
    curr_edge = my_co_edges.get_and_step()->get_ref_edge_ptr();
    curr_edge_length = curr_edge->measure();
    total_edge_length += curr_edge_length;
  }
  if (total_edge_length != 0)
    face_hydraulic_radius = 4.0*(face_area / total_edge_length);
}

void GeomMeasureTool::measure_volume_volume_and_hydraulic_radius(RefVolume *curr_volume,
                                                                 double &volume_volume,
                                                                 double &volume_hydraulic_radius)
{
  int ii;
  double curr_face_area, total_surface_area = 0.0;
  DLIList <RefFace*> ref_face_list;
  RefFace *curr_face;

  volume_volume = curr_volume->measure();
  curr_volume->ref_faces(ref_face_list);
  
  for ( ii = ref_face_list.size(); ii > 0; ii-- )
  {
    curr_face = ref_face_list.get_and_step();
    curr_face_area = measure_area(curr_face);
    total_surface_area += curr_face_area;
  }
  if (total_surface_area != 0)
    volume_hydraulic_radius = 6.0*(volume_volume / total_surface_area);
 
}

void GeomMeasureTool::angles_between_volume_surfaces(RefVolume *curr_volume,
                                                     double &min_angle,
                                                     double &max_angle,
                                                     RefFace *&face_min_1,
                                                     RefFace *&face_min_2,
                                                     RefFace *&face_max_1,
                                                     RefFace *&face_max_2)
{
  int ii, jj, shared_angles = 0;
  double common_angle, sum_of_angles = 0.0;
  double smallest = CUBIT_DBL_MAX, largest = 0.0;
  DLIList <RefFace*> face_list_1, face_list_2;
  RefFace *curr_face_1, *curr_face_2;
  RefEdge *common_edge;

  curr_volume->ref_faces(face_list_1);
  face_list_2 = face_list_1;

  for (ii = face_list_1.size(); ii > 0; ii--)
  {
    curr_face_1 = face_list_1.get_and_step();
    for(jj = face_list_2.size(); jj > 0; jj--)
    {
      curr_face_2 = face_list_2.get_and_step();

      if(curr_face_1 == curr_face_2)
        continue;
      else
      {
        common_edge = curr_face_1->common_ref_edge(curr_face_2);
        if( common_edge == NULL )
          continue;
        else
        {
          shared_angles++;
          
          if ( common_edge->get_curve_ptr()->geometry_type() != STRAIGHT_CURVE_TYPE )
          {
            int kk;
            double frac_pos = 0.00;
            for ( kk = 0; kk < 4; kk++ )
            {
              common_angle =  GeometryQueryTool::instance()->surface_angle(curr_face_1, curr_face_2,
                                                                           common_edge, curr_volume,
                                                                           frac_pos);
              frac_pos += .25;
              if( common_angle < smallest )
              {
                min_angle = common_angle * (180 / CUBIT_PI);
                smallest = common_angle;
                face_min_1 = curr_face_1;
                face_min_2 = curr_face_2;
              }
              if( common_angle > largest )
              {
                max_angle = common_angle * (180 / CUBIT_PI);
                largest = common_angle;
                face_max_1 = curr_face_1;
                face_max_2 = curr_face_2;
              }
            }
          }
          else
          {
            
            common_angle =  GeometryQueryTool::instance()->surface_angle(curr_face_1, curr_face_2,
                                                                         common_edge, curr_volume );
            if( common_angle < smallest )
            {
              min_angle = common_angle * (180 / CUBIT_PI);
              smallest = common_angle;
              face_min_1 = curr_face_1;
              face_min_2 = curr_face_2;
            }
            if( common_angle > largest )
            {
              max_angle = common_angle * (180 / CUBIT_PI);
              largest = common_angle;
              face_max_1 = curr_face_1;
              face_max_2 = curr_face_2;
            }
          }
          
          sum_of_angles += common_angle;
        }
      }
    }
  }
}

void GeomMeasureTool::merged_unmerged_surface_ratio(DLIList <RefVolume*> &ref_volumes,
                                                    int &merged, int &unmerged,
                                                    double &ratio)
  
{
  int ii, jj;
  RefVolume *curr_volume_1;
  DLIList <RefFace*> surface_list_1, surface_list_2;
  RefFace *curr_face_1;
  int merged_surfaces = 0, unmerged_surfaces = 0;
  
  for( ii = ref_volumes.size(); ii > 0; ii-- )
  {
    curr_volume_1 = ref_volumes.get_and_step();
    surface_list_1.clean_out();
    
    curr_volume_1->ref_faces(surface_list_1);
    for( jj = surface_list_1.size(); jj > 0; jj-- )
    {
      curr_face_1 = surface_list_1.get_and_step();
      if ( curr_face_1->marked() )
        continue;
      else
      {
        curr_face_1->marked(1);
        surface_list_2.append(curr_face_1);
      }
      if ( curr_face_1->num_ref_volumes() == 2 )
        merged_surfaces++;
      else
        unmerged_surfaces++;
    }
  }
  for ( ii = surface_list_2.size(); ii > 0; ii-- )
  {
    surface_list_2.get_and_step()->marked(0);
  }
  merged = merged_surfaces;
  unmerged = unmerged_surfaces;
  if ( unmerged_surfaces == 0 )
  {
    ratio = 100.0;
  }
  else
    ratio = (double) ((double)merged_surfaces / (double)unmerged_surfaces);
}

void GeomMeasureTool::report_intersected_volumes(DLIList <RefVolume*> &ref_vols,
                                                 DLIList <RefVolume*> &intersection_list)
{
  DLIList <RefVolume*> results;
  RefVolume *curr_vol;
  int i, j;
  ProgressTool *progress_ptr = NULL;
  int total_volumes = ref_vols.size();
  if (total_volumes > 5)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Overlapping Volumes Progress", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.

  for( i = 0; i < ref_vols.size(); i++ )
  {
    curr_vol = ref_vols.next(i);

    //is this body a multi-volume-body?
    Body *single_volume_body = curr_vol->body();
    DLIList<RefVolume*> body_vols;
    single_volume_body->ref_volumes( body_vols );
    if( body_vols.size() > 1 )
      single_volume_body = NULL;
      
    for( j = (i + 1); j < ref_vols.size(); j++ )
    {
      RefVolume *curr_vol2 = ref_vols.next(j);
      if ( AppUtil::instance()->interrupt() )
      {
          //interrpt.  We need to exit.
        if ( progress_ptr != NULL )
        {
          progress_ptr->end();
        }
          //just leave what has been calculated...
        return;
      }

      Body *single_volume_body2 = curr_vol2->body();
      DLIList<RefVolume*> body_vols2;
      single_volume_body2->ref_volumes( body_vols2 );
      if( body_vols2.size() > 1 )
        single_volume_body2 = NULL;

      //update the progress..
      if ( progress_ptr != NULL )
      {
        curr_percent = ((double)(i+1))/((double)(total_volumes));
        progress_ptr->percent(curr_percent);
      }
    
      //if both are single-volume-bodies
      if( single_volume_body && single_volume_body2 )
      {
        if( GeometryQueryTool::instance()->bodies_overlap( single_volume_body,
                                                       single_volume_body2 ) )
        {
          intersection_list.append( curr_vol );
          intersection_list.append( curr_vol2 );
        }
      }
      else if( GeometryQueryTool::instance()->volumes_overlap( curr_vol, curr_vol2 ) )
      {
       intersection_list.append( curr_vol );
       intersection_list.append( curr_vol2 );
      }
    }
  }
  
  if ( progress_ptr != NULL )
  {
    progress_ptr->end();
  }
}

void GeomMeasureTool::report_intersected_bodies(DLIList <Body*> &ref_bodies,
                                                DLIList <Body*> &intersection_list)
{
  Body *curr_body, *curr_body_2;
  int ii, jj;
  ProgressTool *progress_ptr = NULL;
  int total_bodies = ref_bodies.size();
  if (total_bodies > 5)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Overlapping Volumes Progress", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.
 
  
  for( ii = 0; ii < ref_bodies.size(); ii++ )
  {
    curr_body = ref_bodies.next(ii);

    for( jj = (ii + 1); jj < ref_bodies.size(); jj++ )
    {
      curr_body_2 = ref_bodies.next(jj);
      if ( AppUtil::instance()->interrupt() )
      {
          //interrpt.  We need to exit.
        if ( progress_ptr != NULL )
        {
          progress_ptr->end();
        }
          //just leave what has been calculated...
        return;
      }

      if (GeometryQueryTool::instance()->bodies_overlap(curr_body,
                                                        curr_body_2) )
      {
        intersection_list.append(curr_body);
        intersection_list.append(curr_body_2);
      }
    }
       //update the progress..
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(ii+1))/((double)(total_bodies));
      progress_ptr->percent(curr_percent);
    }
  }
  if ( progress_ptr != NULL )
  {
    progress_ptr->end();
  }
}

void GeomMeasureTool::face_list_from_volume_list( DLIList <RefVolume*> &input_vols,
                                                  DLIList <RefFace*> &all_faces )
{
  DLIList <RefFace*> curr_faces;
  RefVolume *curr_vol;
  RefFace *curr_face;
  int ii, jj;

    //get the all RefFaces from input_vols.
    //put them into all_faces list.
  
  for ( ii = 0; ii < input_vols.size(); ii++ )
  {
    curr_vol = input_vols.get_and_step();
    curr_faces.clean_out();
    curr_vol->ref_faces(curr_faces);
    
    for( jj = 0; jj < curr_faces.size(); jj++ )
    {
      curr_face = curr_faces.get_and_step();
      if( curr_face->marked() )
        continue;
      else
      {
        curr_face->marked(1);
        all_faces.append(curr_face);
      }
    }
  }
  for ( ii = all_faces.size();ii >0; ii-- )
    all_faces.get_and_step()->marked(0);
}

RefFace* GeomMeasureTool::valid_start( DLIList <RefFace*> &all_faces )
{
  int ii;
  RefFace *start_face;

  for( ii = 0; ii < all_faces.size(); ii++ )
  {
    start_face = all_faces.get_and_step();

    if( !start_face->marked() && start_face->num_ref_volumes()== 1 )
      return start_face;
  }

  return NULL;
  
}

void GeomMeasureTool::find_shells( DLIList <RefVolume*> &input_vols,
                                   RefGroup *&owner_groups,
                                   int &number_of_shells)
{
  DLIList <DLIList<RefEntity*>*> list_of_lists;
  DLIList <RefEntity*> *new_list;
  DLIList <RefFace*> all_faces, stack, related_faces;
  DLIList <RefEdge*> curr_edges;
  RefFace *start_face, *curr_face, *related_face;
  RefEdge *curr_edge;
  int ii, jj;
  RefEntity *tmp_ent;
  number_of_shells = 0;
  DLIList <RefFace*> marked_faces;
  
  face_list_from_volume_list( input_vols, all_faces );
  
    // all_faces all marked - need unmarked
  for (ii = 0; ii < all_faces.size(); ii++ )
  {
    curr_face = all_faces.get_and_step();
    curr_face->marked(0);
  }
  while ( (start_face = valid_start(all_faces) ) != NULL )
  {
      //create a list.
    stack.clean_out();
    new_list = new DLIList<RefEntity*>;
    stack.append(start_face);
    start_face->marked(1);
    marked_faces.append(start_face);

    while (stack.size() > 0 )
    {
      curr_edges.clean_out();

      curr_face = stack.pop(); // get last face added
      tmp_ent = CAST_TO(curr_face, RefEntity);
      new_list->append(tmp_ent); // append that face to the newest list
      curr_face->ref_edges(curr_edges); // gets list of face's edges

      for( ii = 0; ii < curr_edges.size(); ii++ )  // loops edges
      {
        related_faces.clean_out();

        curr_edge = curr_edges.get_and_step(); // chooses next edge
        curr_edge->ref_faces(related_faces); // finds edge's related faces
        for( jj = 0; jj < related_faces.size(); jj++ ) // loops faces
        {
          related_face = related_faces.get_and_step(); // gets face
          if(!related_face->marked() && related_face->num_ref_volumes() == 1)
          {
            stack.append(related_face);
            related_face->marked(1);
            marked_faces.append(related_face);
          }
        }
      }
    }
    list_of_lists.append(new_list);
    number_of_shells++;
  }

  for (ii = 0; ii < marked_faces.size(); ii++ )
  {
    curr_face = marked_faces.get_and_step();
    curr_face->marked(0);
  }
  
  RefGroup *curr_group;
  owner_groups = RefEntityFactory::instance()->construct_RefGroup("volume_shells");
  for( ii = list_of_lists.size(); ii > 0; ii-- )
  {
    new_list = list_of_lists.pop();
    curr_group = RefEntityFactory::instance()->construct_RefGroup(*new_list);
    tmp_ent = CAST_TO(curr_group, RefEntity);
    owner_groups->add_ref_entity(tmp_ent);
    delete new_list;
  } 
}

void GeomMeasureTool::print_surface_measure_summary( DLIList <RefFace*> &ref_faces )
                                                  
{
  
  double min_dist_loops=CUBIT_DBL_MAX, min_dist_loop=CUBIT_DBL_MAX;
  double curr_min_dist_loops=CUBIT_DBL_MAX, curr_min_dist_loop=CUBIT_DBL_MAX;
  double min_angle=CUBIT_DBL_MAX, max_angle=-CUBIT_DBL_MAX;
  double curr_min_angle=CUBIT_DBL_MAX, curr_max_angle=-CUBIT_DBL_MAX;
  double curr_face_area, curr_face_hydraulic_radius;
  double min_face_area=CUBIT_DBL_MAX, min_face_hydraulic_radius=CUBIT_DBL_MAX;
  double max_face_area=-CUBIT_DBL_MAX;
  int counter = 0;
  double total_area= 0.0;
  double min_curve_length = CUBIT_DBL_MAX;
  double max_curve_ratio = -CUBIT_DBL_MAX;
  double curve_length, curve_ratio;
  int ii;
       
  RefFace *curr_face;
  RefFace *face_min_dist_loops=NULL, *face_min_dist_loop=NULL;
  RefFace *face_min_angle=NULL, *face_max_angle=NULL;
  RefFace *face_min_area=NULL, *face_min_hydraulic_radius=NULL;
  RefFace *face_max_area=NULL;
  RefFace *face_min_curve_length=NULL, *face_max_curve_ratio=NULL;
  RefEdge *smallest_edge=NULL, *curr_small_edge=NULL;
       
       
  for( ii = 0; ii < ref_faces.size(); ii++ )
  {
    curr_face = ref_faces.get_and_step();
    if ( curr_face->num_ref_edges() > 0 )
    {
      GeomMeasureTool::measure_face_loops(curr_face,
                                          curr_min_dist_loops,
                                          curr_min_dist_loop,
                                          curr_min_angle,
                                          curr_max_angle,
                                          GEOMETRY_RESABS*500);
      GeomMeasureTool::measure_face_area_and_hydraulic_radius(curr_face,
                                                              curr_face_area,
                                                              curr_face_hydraulic_radius);
      GeomMeasureTool::measure_face_curves(curr_face, curve_length,
                                           curve_ratio, curr_small_edge);
    }
    else
      curr_face_area = curr_face->measure();
         
    total_area += curr_face_area;
    counter++;
    if ( curr_face->num_ref_edges() > 0 )
    {
      if ( curve_length < min_curve_length )
      {
        min_curve_length = curve_length;
        smallest_edge = curr_small_edge;
        face_min_curve_length = curr_face;
      }
      if ( curve_ratio != -1.0 && curve_ratio > max_curve_ratio )
      {
        max_curve_ratio = curve_ratio;
        face_max_curve_ratio = curr_face;
      }
      if ( curr_min_dist_loops != -1.0 && curr_min_dist_loops < min_dist_loops )
      {
        face_min_dist_loops = curr_face;
        min_dist_loops = curr_min_dist_loops;
      }
      if ( curr_min_dist_loop < min_dist_loop )
      {
        face_min_dist_loop = curr_face;
        min_dist_loop = curr_min_dist_loop;
      }
      if ( curr_min_angle < min_angle )
      {
        face_min_angle = curr_face;
        min_angle = curr_min_angle;
      }
      if ( curr_max_angle > max_angle )
      {
        face_max_angle = curr_face;
        max_angle = curr_max_angle;
      }

      if ( curr_face_hydraulic_radius < min_face_hydraulic_radius )
      {
        min_face_hydraulic_radius = curr_face_hydraulic_radius;
        face_min_hydraulic_radius = curr_face;
      }

    }
    if ( curr_face_area < min_face_area )
    {
      min_face_area = curr_face_area;
      face_min_area = curr_face;
    } 
    if ( curr_face_area > max_face_area )
    {
      max_face_area = curr_face_area;
      face_max_area = curr_face;
    }
  }
  if ( counter > 0 )
  {
    PRINT_INFO("\n******Summary of Surface Measure Info*******\n\n");
    PRINT_INFO("Measured %d surfaces: Average Area: %f \n\n", counter, total_area/counter );
    PRINT_INFO("Minimum Surface Area is: %f on surface %d\n\n",
               min_face_area, face_min_area->id());
    PRINT_INFO("Maximum Surface Area is: %f on surface %d\n\n",
               max_face_area, face_max_area->id());
    if ( face_min_hydraulic_radius )
      PRINT_INFO("Minimum Hydraulic Radius (Area/Perimeter) is: %f on surface %d\n\n",
                 min_face_hydraulic_radius, face_min_hydraulic_radius->id());
    if ( smallest_edge )
      PRINT_INFO("Minimum Curve Length is: %f for curve %d on surface %d\n\n",
                 min_curve_length, smallest_edge->id(), face_min_curve_length->id());
    if ( face_max_curve_ratio )
      PRINT_INFO("Maximum Ratio of Adjacent Curve Lengths is: %f on surface %d\n\n",
                 max_curve_ratio, face_max_curve_ratio->id());
    if ( face_min_dist_loops && face_min_dist_loops->num_loops() > 1 )
    {
      PRINT_INFO("Minimum Distance Between Loops is: %f on surface %d\n\n",
                 min_dist_loops, face_min_dist_loops->id());
    }
    if ( face_min_dist_loop )
      PRINT_INFO("Minimum Distance Between a Single Loop is: %f on surface %d\n\n",
                 min_dist_loop, face_min_dist_loop->id());
    if ( face_min_angle )
      PRINT_INFO("Minimum Interior Angle Between Curves is: %f Degrees on surface %d\n\n",
                 min_angle, face_min_angle->id());
    if ( face_max_angle )
      PRINT_INFO("Maximum Interior Angle Between Curves is: %f Degrees on surface %d\n\n",
                 max_angle, face_max_angle->id());
	center(ref_faces);
    PRINT_INFO("************End of Summary***********\n\n");
  }

}
void GeomMeasureTool::find_adjacent_face_ratios(RefVolume *curr_volume, double &max_face_ratio,
                                                RefFace *&big_face,
                                                RefFace *&small_face)
{
    //Loop over all the faces in the model.  Find the maximum face area ratio.
    //(larger face area / smaller face area)
  
  DLIList <Shell*> shells;
  curr_volume->shells(shells);
  DLIList <RefFace*> ref_face_stack, adj_faces;
  Shell* curr_shell;
  RefFace *curr_ref_face, *adj_face;
    //AreaHashTuple is defined in the .hpp file.
  DLIList <AreaHashTuple*> *hash_table;
  max_face_ratio = -CUBIT_DBL_MAX;
  
  
  int ii, jj, table_size;
  int hash_val;
  double tmp_area;
  double tmp_ratio;
  
  AreaHashTuple* curr_tuple, *adj_tuple;
  
  for ( ii = shells.size(); ii > 0; ii-- )
  {
    curr_shell = shells.get_and_step();
    ref_face_stack.clean_out();
    curr_shell->ref_faces(ref_face_stack);
    table_size = ref_face_stack.size();
      //build a hash table with the most simple form
      //of hashing based on the ref face id.
      //To get this more reliable, make table size a prime
      //number...
      //Do this so that we don't have to calculate the
      //areas more than once...
    hash_table = new DLIList<AreaHashTuple*> [table_size];
    for ( jj = 0; jj < ref_face_stack.size(); jj++ )
    {
      curr_ref_face = ref_face_stack.get_and_step();
      tmp_area = curr_ref_face->measure();
      curr_tuple = new AreaHashTuple();
      curr_tuple->myFace = curr_ref_face;
      curr_tuple->myArea = tmp_area;
      hash_val = curr_ref_face->id() % table_size;
      hash_table[hash_val].append(curr_tuple);
    }
    
    while ( ref_face_stack.size() )
    {
      curr_ref_face = ref_face_stack.pop();
        //Now get the adjacent faces.
      get_adjacent_faces(curr_ref_face, adj_faces);
      find_index(hash_table, table_size, curr_ref_face, curr_tuple);
      assert(curr_tuple != NULL);
      if ( curr_tuple == NULL )
        return;
      CubitBoolean curr_is_big = CUBIT_TRUE;
      for ( jj = 0; jj < adj_faces.size(); jj++ )
      {
        adj_face = adj_faces.get_and_step();
        find_index(hash_table, table_size, adj_face, adj_tuple);
        if ( adj_tuple == NULL )
          continue;
        curr_is_big = CUBIT_TRUE;
        if ( curr_tuple->myArea < adj_tuple->myArea )
        {
          tmp_ratio = adj_tuple->myArea / curr_tuple->myArea;
          curr_is_big = CUBIT_FALSE;
        }
        else
          tmp_ratio = curr_tuple->myArea / adj_tuple->myArea;
        if ( tmp_ratio > max_face_ratio )
        {
          max_face_ratio = tmp_ratio;
          if ( curr_is_big )
          {
            big_face = curr_ref_face;
            small_face = adj_face;
          }
          else
          {
            big_face = adj_face;
            small_face = curr_ref_face;
          }
        }
      }
    }
    for ( jj = 0; jj < table_size; jj++ )
    {
        //delete the area tuples.
      while ( hash_table[jj].size() > 0 )
        delete hash_table[jj].pop();
    }
      //delete the hash_table.
    delete [] hash_table;
  }
}
void GeomMeasureTool::find_index(DLIList<AreaHashTuple*> *hash_table,
                                 int table_size,
                                 RefFace *ref_face,
                                 AreaHashTuple *&curr_tuple )
{
  int ii;
  int hash_val = ref_face->id() % table_size;
  if ( hash_table[hash_val].size() == 0 )
  {
      //this ref face isn't store here.
    curr_tuple = NULL;
    return;
  }
  int curr_size = hash_table[hash_val].size();
    //resolve collisions by separate chaining method...
  for ( ii = 0;ii < curr_size; ii++ )
  {
    if ( hash_table[hash_val].next(ii)->myFace == ref_face )
    {
      curr_tuple = hash_table[hash_val].next(ii);
      return;
    }
  }
  curr_tuple = NULL;
  return;
}
  
void GeomMeasureTool::get_adjacent_faces(RefFace* curr_face,
                                         DLIList<RefFace*> &adjacent_faces)
{
  DLIList <RefEdge*> ref_edges;
  DLIList <RefFace*> tmp_ref_faces;
  curr_face->ref_edges(ref_edges);
  RefEdge *ref_edge;
  RefFace *tmp_ref_face;
  int ii, jj;
  for ( ii = 0; ii < ref_edges.size(); ii++ )
  {
    ref_edge = ref_edges.get_and_step();
    tmp_ref_faces.clean_out();
    ref_edge->ref_faces(tmp_ref_faces);
    for ( jj = 0; jj < tmp_ref_faces.size(); jj++ )
    {
      tmp_ref_face = tmp_ref_faces.get_and_step();
      if ( !tmp_ref_face->marked() && tmp_ref_face != curr_face )
      {
        tmp_ref_face->marked(1);
        adjacent_faces.append(tmp_ref_face);
      }
      else
        continue;
    }
  }
  for ( ii = adjacent_faces.size(); ii > 0; ii-- )
    adjacent_faces.get_and_step()->marked(0);
}
void GeomMeasureTool::print_volume_measure_summary(DLIList <RefVolume*> &ref_volumes)
{
  int ii;
  RefVolume *curr_volume;
  RefFace *curr_face;
  DLIList <RefFace*> ref_faces, tmp_faces;
    //First get a list of all the ref_faces.
    //Get them uniquely.
  for ( ii = 0; ii< ref_volumes.size(); ii++ )
  {
    curr_volume = ref_volumes.get_and_step();
    tmp_faces.clean_out();
    curr_volume->ref_faces(tmp_faces);
    int jj;
    for ( jj = 0; jj < tmp_faces.size(); jj++ )
    {
      curr_face = tmp_faces.get_and_step();
      if ( curr_face->marked() )
        continue;
      else
      {
        curr_face->marked(1);
        ref_faces.append(curr_face);
      }
    }
  }
    //reset the mark.
  for ( ii = 0; ii< ref_faces.size(); ii++ )
    ref_faces.get_and_step()->marked(0);

  double volume_volume, volume_hydraulic_radius;
  double min_angle=CUBIT_DBL_MAX, max_angle=-CUBIT_DBL_MAX;
  double total_volume = 0.0, min_volume_hydraulic_radius = CUBIT_DBL_MAX;
  double min_volume=CUBIT_DBL_MAX; //, max_volume=-CUBIT_DBL_MAX;
  double curr_min_angle = 361.0, curr_max_angle =361.;
  double max_face_ratio = -CUBIT_DBL_MAX, curr_face_ratio=CUBIT_DBL_MAX;
         

  RefVolume *volume_min_volume = NULL, *volume_min_hydraulic_radius=NULL;
  RefVolume *volume_min_angle=NULL, *volume_max_angle=NULL;
  RefVolume *volume_face_ratio= NULL;
  RefFace *max_small_face=NULL, *max_big_face=NULL;
  RefFace *small_face=NULL, *big_face=NULL;
  RefFace *face_min_1=NULL, *face_min_2=NULL;
  RefFace *face_max_1=NULL, *face_max_2=NULL;
  RefFace *face_min_angle_1=NULL, *face_min_angle_2=NULL;
  RefFace *face_max_angle_1=NULL, *face_max_angle_2=NULL;
  
  
  int counter = 0;
       
  for( ii = 0; ii< ref_volumes.size(); ii++ )
  {
    curr_volume = ref_volumes.get_and_step();
    GeomMeasureTool::measure_volume_volume_and_hydraulic_radius(curr_volume,
                                                                volume_volume,
                                                                volume_hydraulic_radius);
    if ( curr_volume->num_ref_faces() > 1 )
    {
      GeomMeasureTool::angles_between_volume_surfaces(curr_volume,
                                                      curr_min_angle,
                                                      curr_max_angle,
                                                      face_min_1, face_min_2,
                                                      face_max_1, face_max_2);
      GeomMeasureTool::find_adjacent_face_ratios(curr_volume,
                                                 curr_face_ratio,
                                                 big_face, small_face);
    }
    total_volume += volume_volume;
    counter++;
    if ( volume_volume < min_volume )
    {
      volume_min_volume = curr_volume;
      min_volume = volume_volume;
    }
    if ( volume_hydraulic_radius < min_volume_hydraulic_radius )
    {
      volume_min_hydraulic_radius = curr_volume;
      min_volume_hydraulic_radius = volume_hydraulic_radius;
    }
    if ( curr_volume->num_ref_faces() > 1 &&  curr_min_angle < min_angle )
    {
      volume_min_angle = curr_volume;
      face_min_angle_1 = face_min_1;
      face_min_angle_2 = face_min_2;
      min_angle = curr_min_angle;
    }
    if ( curr_volume->num_ref_faces() > 1 && curr_max_angle > max_angle )
    {
      volume_max_angle = curr_volume;
      max_angle = curr_max_angle;
      face_max_angle_1 = face_max_1;
      face_max_angle_2 = face_max_2;
    }
    if ( curr_volume->num_ref_faces() > 1 && curr_face_ratio > max_face_ratio )
    {
      volume_face_ratio = curr_volume;
      max_face_ratio = curr_face_ratio;
      max_small_face = small_face;
      max_big_face = big_face;
    }
  }
  if ( counter > 0 )
  {
    PRINT_INFO("*************Volume Summary************\n\n");
    PRINT_INFO("Measured %d Volumes: Average Volume was %f.\n\n", 
               counter, total_volume/counter);
    PRINT_INFO("Minimum Volume was %f in volume %d\n\n",
               min_volume, volume_min_volume->id());
    PRINT_INFO("Minimum Volume Hydraulic Radius (volume/surface area) was %f in volume %d\n\n",
               min_volume_hydraulic_radius, volume_min_hydraulic_radius->id());
    if ( volume_min_angle )
      PRINT_INFO("Minimum Interior Angle Between Two Surfaces was %f,\n"
                 "\tsurfaces %d and %d in volume %d\n\n",
                 min_angle, face_min_angle_1->id(), face_min_angle_2->id(), volume_min_angle->id());
    if ( volume_max_angle )
      PRINT_INFO("Maximum Interior Angle Between Two Surfaces was %f,\n"
                 "\tsurfaces %d and %d in volume %d\n\n",
                 max_angle, face_max_angle_1->id(), face_max_angle_2->id(), volume_max_angle->id());
    if ( volume_face_ratio )
      PRINT_INFO("Maximum Area Ratios Between Adjacent Faces was %f,\n"
                 "\tsurfaces %d and %d in volume %d.\n\n",
                 max_face_ratio, max_big_face->id(), max_small_face->id(), volume_face_ratio->id());
    int merged, unmerged;
    double ratio;     
    GeomMeasureTool::merged_unmerged_surface_ratio(ref_volumes, merged, unmerged, ratio);
    PRINT_INFO("Total number of merged surfaces:\t%d\n"
               "Total number of unmerged surfaces:\t%d\n"
               "Ratio of merged to unmerged surfaces:\t%f\n\n", merged, unmerged, ratio);
    RefGroup *group_of_shells;
    int number_shells;
    GeomMeasureTool::find_shells(ref_volumes, group_of_shells, number_shells);
    if ( number_shells > 0 )
    {
      if ( number_shells == 1 )
      {
        PRINT_INFO("Found %d shell of connected surfaces.\n", number_shells);
        PRINT_INFO("These shell is contained in the group %s\n",
                   group_of_shells->entity_name().c_str());
      }
      else
      {
        PRINT_INFO("Found %d shells of connected surfaces.\n", number_shells);
        PRINT_INFO("These shells are contained in the group %s\n",
                   group_of_shells->entity_name().c_str());
      }
    }
    PRINT_INFO("\n\n");
    PRINT_INFO("Now Printing Summaries of Surfaces contained by these volumes.\n");
    GeomMeasureTool::print_surface_measure_summary(ref_faces);
  }
  return;

}

// Find all of the surfaces in the given volumes that have narrow regions.
void GeomMeasureTool::find_surfs_with_narrow_regions(DLIList <RefVolume*> &ref_vols,
                                          double tol,
                                          DLIList <RefFace*> &surfs_with_narrow_regions)
{
  int j;

  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    DLIList<RefFace*> faces;
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(faces);
    for ( jj = faces.size(); jj > 0; jj-- )
    {
      curr_face = faces.get_and_step();
      curr_face->marked(0);
      temp_faces.append(curr_face);
    }
  }

  //uniquely add the faces.
  for ( jj = temp_faces.size(); jj > 0; jj-- )
  {
    curr_face = temp_faces.get_and_step();
    if ( curr_face->marked()== 0 )
    {
      curr_face->marked(1);
      ref_faces.append(curr_face);
    }
  }

  int num_faces = ref_faces.size();

  ProgressTool *progress_ptr = NULL;
  if (num_faces > 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Surfaces with Narrow Regions", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }

  int total_faces = 0;
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.

  for(j=0; j<num_faces; j++)
  {
    DLIList<CubitVector> split_pos1_list;
    DLIList<CubitVector> split_pos2_list;
    RefFace *cur_face = ref_faces.get_and_step();
    total_faces++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_faces))/((double)(num_faces));
      progress_ptr->percent(curr_percent);
    }

    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }
    find_split_points_for_narrow_regions(cur_face,
      tol, split_pos1_list, split_pos2_list); 
    if(split_pos1_list.size() > 0)
      surfs_with_narrow_regions.append_unique(cur_face);
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();
}

void GeomMeasureTool::get_narrow_regions(DLIList <RefVolume*> &ref_vols,
                                          double tol,
                                          DLIList <RefFace*> &surfs_with_narrow_regions)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    DLIList<RefFace*> faces;
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(faces);
    for ( jj = faces.size(); jj > 0; jj-- )
    {
      curr_face = faces.get_and_step();
      curr_face->marked(0);
      temp_faces.append(curr_face);
    }
  }

  //uniquely add the faces.
  for ( jj = temp_faces.size(); jj > 0; jj-- )
  {
    curr_face = temp_faces.get_and_step();
    if ( curr_face->marked()== 0 )
    {
      curr_face->marked(1);
      ref_faces.append(curr_face);
    }
  }

  int num_faces = ref_faces.size();

  ProgressTool *progress_ptr = NULL;
  if (num_faces > 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Surfaces with Narrow Regions", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }

  int total_faces = 0;
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.

  for(jj=0; jj<num_faces; jj++)
  {
    RefFace *cur_face = ref_faces.get_and_step();
    total_faces++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_faces))/((double)(num_faces));
      progress_ptr->percent(curr_percent);
    }

    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }
    if(cur_face->num_loops() == 1)
    {
      DLIList<CubitVector> split_pos1_list;
      DLIList<CubitVector> split_pos2_list;
      find_split_points_for_narrow_regions(cur_face,
        tol, split_pos1_list, split_pos2_list); 
      if(split_pos1_list.size() > 0)
        surfs_with_narrow_regions.append(cur_face);
    }
    else if(cur_face->num_loops() > 1)
    {
      DLIList <RefEdge*> tmp_close_edges;
      DLIList <double> tmp_small_lengths;
      find_close_loops( cur_face, tmp_close_edges,
                        tmp_small_lengths, tol);
      if ( tmp_close_edges.size() > 0 )
      {
        surfs_with_narrow_regions.append(cur_face);
      }
    }
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();
}

bool GeomMeasureTool::is_surface_narrow(RefFace *face, double small_curve_size)
{
  bool ret = true;
  DLIList<RefEdge*> edges;
  face->ref_edges(edges);
  RefVolume *vol = face->ref_volume();
  int i, j;
  double dist_sq = small_curve_size*small_curve_size;
  double proj_dist = 1.2 * small_curve_size;
  for(i=edges.size(); i>0 && ret == true; i--)
  {
    RefEdge *cur_edge = edges.get_and_step();
    double edge_length = cur_edge->measure();
    if(edge_length > small_curve_size)
    {
      int num_incs = (int)(edge_length/small_curve_size) + 1;
      double start, end;
      cur_edge->get_param_range(start, end);
      double t = start;
      bool one_bad = false;
      for(j=0; j<num_incs && ret == true; j++)
      {
        CubitVector pos1, tangent;
        cur_edge->position_from_u(t, pos1);
        cur_edge->tangent(pos1, tangent, face);
        CubitVector norm = face->normal_at(pos1, vol);
        CubitVector indir = norm * tangent;
        indir.normalize();
        CubitVector new_pos = pos1 + proj_dist * indir;
        CubitVector pt_on_surf;
        face->get_surface_ptr()->closest_point_trimmed(new_pos, pt_on_surf);
        if((pt_on_surf-pos1).length_squared() < dist_sq)
        {
          one_bad = false;
        }
        else // we found one out of small_curve range
        {
          if(one_bad)  // if we had already found one out of range this makes two in a row
            ret = false;
          else  // this is the first one out of range
            one_bad = true;
        }
      }
    }
  }

  return ret;
}

void GeomMeasureTool::find_split_points_for_narrow_regions(RefFace *face,
                                                          double size, 
                                                          DLIList<CubitVector> &split_pos1_list,
                                                          DLIList<CubitVector> &split_pos2_list)
{
  int k, i, j;
  double size_sq = size*size;
  DLIList<RefEdge*> edges;
  face->ref_edges(edges);
  while(edges.size() > 1)
  {
    RefEdge *cur_edge = edges.extract();
    for(k=edges.size(); k--;)
    {
      RefEdge *other_edge = edges.get_and_step();
      DLIList<CubitVector*> e1_pos_list, e2_pos_list;
      DLIList<RefVertex*> e1_vert_list, e2_vert_list;
      if(narrow_region_exists(cur_edge, other_edge, face, size,
        e1_pos_list, e2_pos_list, e1_vert_list, e2_vert_list))
      {
        e1_pos_list.reset();
        e2_pos_list.reset();
        e1_vert_list.reset();
        e2_vert_list.reset();

        // Loop through each pair of positions defining a split.
        for(i=e1_pos_list.size(); i--;)
        {
          RefVertex *e1_vert = e1_vert_list.get_and_step();
          RefVertex *e2_vert = e2_vert_list.get_and_step();
          CubitVector *e1_pos = e1_pos_list.get_and_step();
          CubitVector *e2_pos = e2_pos_list.get_and_step();

          // Snap to existing vertices if we are not already at at vertex.
          if(!e1_vert)
          {
            if((cur_edge->start_vertex()->coordinates() - *e1_pos).length_squared() < size_sq)
              e1_vert = cur_edge->start_vertex();
            else if((cur_edge->end_vertex()->coordinates() - *e1_pos).length_squared() < size_sq)
              e1_vert = cur_edge->end_vertex();
          }
          if(!e2_vert)
          {
            if((other_edge->start_vertex()->coordinates() - *e2_pos).length_squared() < size_sq)
              e2_vert = other_edge->start_vertex();
            else if((other_edge->end_vertex()->coordinates() - *e2_pos).length_squared() < size_sq)
              e2_vert = other_edge->end_vertex();
          }

          // We may have multiple edges separating these two vertices but the accumulated
          // length of them may still be within our narrow size so check this.  If this
          // is the case we will not want to consider this as a place to split and 
          // will as a result discard these positions.
          if(e1_vert && e2_vert)
          {
            double dist = size*sqrt(2.0);
            RefVertex *cur_vert = e1_vert;
            RefEdge *edge = cur_edge;
            double length = 0.0;
            while(edge && cur_vert != e2_vert && length <= dist)
            {
              edge = edge->get_other_curve(cur_vert, face);
              if(edge)
              {
                length += edge->get_arc_length();
                cur_vert = edge->other_vertex(cur_vert);
              }
            }
            if(length > dist)
            {
              // We want to keep this split.
              split_pos1_list.append(e1_vert->coordinates());
              split_pos2_list.append(e2_vert->coordinates());
            }
          }
          else
          {
            // We want to keep this split.
            split_pos1_list.append(*e1_pos);
            split_pos2_list.append(*e2_pos);
          }
        }
      }
      while(e1_pos_list.size())
        delete e1_pos_list.pop();
      while(e2_pos_list.size())
        delete e2_pos_list.pop();
    }
  }

  // Make splits unique
  DLIList<CubitVector> unique_list1, unique_list2;
  split_pos1_list.reset();
  split_pos2_list.reset();
  for(i=split_pos1_list.size(); i--;)
  {
    CubitVector p1 = split_pos1_list.get_and_step();
    CubitVector p2 = split_pos2_list.get_and_step();
    int unique = 1;
    for(j=unique_list1.size(); j>0 && unique; j--)
    {
      CubitVector u1 = unique_list1.get_and_step();
      CubitVector u2 = unique_list2.get_and_step();
      if((p1.about_equal(u1) && p2.about_equal(u2)) ||
        (p1.about_equal(u2) && p2.about_equal(u1)))
      {
          unique = 0;
      }
    }
    if(unique)
    {
      unique_list1.append(p1);
      unique_list2.append(p2);
    }
  }
  split_pos1_list = unique_list1;
  split_pos2_list = unique_list2;
}

// Checks to see if at the given position the two edges are close together and 
// have the same tangent.
int GeomMeasureTool::is_narrow_region_at_point(RefEdge *e1,
                                               RefFace *face,
                                               const CubitVector &pt_on_e1,
                                               RefEdge *e2,
                                               const double &tol_sq,
                                               CubitVector &closest)
{
  int ret = 0;

  CubitVector tan_1, tan_2;
  e2->closest_point_trimmed(pt_on_e1, closest);
  
  double dist_sq = CUBIT_DBL_MAX;

  // If the surface is not a plane lets project
  // the midpoint to the surface and fit a circular
  // arc through the 3 points and get the arc length
  // to approximate the acutal distance on the surface
  // between the 2 original points.
  if(PLANE_SURFACE_TYPE != face->geometry_type())
  {
    CubitVector closest_mid;
    CubitVector vec1 = closest-pt_on_e1;
    double straight_length_sq = vec1.length_squared();
    CubitVector mid = pt_on_e1 + 0.5 * vec1;
    face->get_surface_ptr()->closest_point(mid, &closest_mid);
    CubitVector mid_vec = mid-closest_mid;
    double mid_length_sq = mid_vec.length_squared();
    // If we are dealing with very small distances or
    // it doesn't look like there is much curvature just use
    // the straight line distance.
    if(straight_length_sq < 1e-10 || mid_length_sq < 0.01*straight_length_sq)
    {
      dist_sq = straight_length_sq;
    }
    else
    {
      double mid_length = sqrt(mid_length_sq);
      double half_length = sqrt(straight_length_sq)/2.0;
      // theta is the angle between the vector from the orig point to its projection
      // on the other edge and the vector from the orig point to the projected mid point.
      double theta = atan(mid_length/half_length); 
      // beta is the angle between the vector going from the circle center to
      // the orig point and the vector going from the circle center to the
      // projected mid point.
      double beta = 2.0*theta;
      // Some trig to get the radius of the circle that goes through all 3 points
      double radius = half_length/sin(beta);
      dist_sq = 2.0*radius*beta;
      dist_sq *= dist_sq;
    }
  }
  else
  {
    dist_sq = (pt_on_e1-closest).length_squared(); 
  }

  if(dist_sq < tol_sq)
  { 
    DLIList<CoEdge*> coes;
    e1->tangent(pt_on_e1, tan_1);
    e2->tangent(closest, tan_2);
    e1->get_co_edges(coes, face);
    if(coes.size() == 1)
    {
      if(coes.get()->get_sense() == CUBIT_REVERSED)
        tan_1 *= -1.0;
      coes.clean_out();
      e2->get_co_edges(coes, face);
      if(coes.size() == 1)
      {
        if(coes.get()->get_sense() == CUBIT_REVERSED)
          tan_2 *= -1.0;
        tan_1.normalize();
        tan_2.normalize();
        if(tan_1 % tan_2 < -0.9)
          ret = 1;
      }
    }
  }
  return ret;
}

int GeomMeasureTool::narrow_region_exists(RefFace *face,
                                          const double &tol)
{
  int k, ret = 0;
  DLIList<RefEdge*> edges;
  face->ref_edges(edges);
  int num_curves = edges.size();

  // if the surface has no bounding edges then we won't consider
  // it narrow.  This can happen with complete sphere surfaces.
  if (0 == num_curves)
    return 0;

  int num_small_curves = 0;
  while(edges.size() > 1 && !ret)
  {
    // Remove the current edge each time so that we aren't
    // doing redundant comparisons.
    RefEdge *cur_edge = edges.extract();
    if(cur_edge->get_arc_length() < tol)
      num_small_curves++;

    // Compare this edge with the remaining edges on the face.
    for(k=edges.size(); k && !ret; k--)
    {
      RefEdge *other_edge = edges.get_and_step();

      DLIList<CubitVector*> e1_pos_list, e2_pos_list;
      DLIList<RefVertex*> e1_vert_list, e2_vert_list;
      ret = narrow_region_exists(cur_edge, other_edge, face, tol,
        e1_pos_list, e2_pos_list, e1_vert_list, e2_vert_list);
      while(e1_pos_list.size())
        delete e1_pos_list.pop();
      while(e2_pos_list.size())
        delete e2_pos_list.pop();
    }
  }
  if(!ret)
  {
    if(edges.size() == 1 && edges.get()->get_arc_length() < tol)
      num_small_curves++;
  }

  ret = ret || (num_small_curves == num_curves);

  return ret;
}

int GeomMeasureTool::narrow_region_exists(RefFace *face,
                                          RefEdge *edge,
                                          const double &tol)
{
  int k, ret = 0;
  DLIList<RefEdge*> edges;
  face->ref_edges(edges);
  if(edges.move_to(edge))
  {
    edges.extract();

    // Compare this edge with the remaining edges on the face.
    for(k=edges.size(); k && !ret; k--)
    {
      RefEdge *other_edge = edges.get_and_step();

      DLIList<CubitVector*> e1_pos_list, e2_pos_list;
      DLIList<RefVertex*> e1_vert_list, e2_vert_list;
      ret = narrow_region_exists(edge, other_edge, face, tol,
        e1_pos_list, e2_pos_list, e1_vert_list, e2_vert_list);
      while(e1_pos_list.size())
        delete e1_pos_list.pop();
      while(e2_pos_list.size())
        delete e2_pos_list.pop();
    }
  }
  return ret;
}

// Finds the start and stop locations between two "close" curves
// that meet the narrow criteria.  The lists that are returned
// mark the beginning and ends of the narrow regions.
int GeomMeasureTool::narrow_region_exists(
                                            RefEdge *e1,
                                            RefEdge *e2,
                                            RefFace *face,
                                            const double &tol,
                                            DLIList<CubitVector*> &e1_pos_list,
                                            DLIList<CubitVector*> &e2_pos_list,
                                            DLIList<RefVertex*> &e1_vert_list,
                                            DLIList<RefVertex*> &e2_vert_list)
{
  int ret = 0;
  double tol_sq = tol*tol;
  double max_dist_sq = 0.0;
  RefVertex *e1_start_vert = e1->start_vertex();
  RefVertex *e1_end_vert = e1->end_vertex();

  CubitVector closest;
  DLIList<RefVertex*> e1_verts, e2_verts;

  e1->ref_vertices(e1_verts);
  e2->ref_vertices(e2_verts);
  e1_verts.intersect_unordered(e2_verts);
  int num_shared_verts = e1_verts.size();
  RefVertex *shared_vert = NULL;
  RefEdge *edge1 = NULL;
  RefEdge *edge2 = NULL;
  if(num_shared_verts == 1)
  {
    shared_vert = e1_verts.get();
    DLIList<CoEdge*> coes;
    e1->get_co_edges(coes, face);
    if(coes.size() == 1)
    {
      RefVolume *vol = face->ref_volume();
      CubitSense facevol_sense = face->sense(vol);
      if((coes.get()->start_vertex() == shared_vert) ==
        (facevol_sense == CUBIT_FORWARD))
      {
        edge1 = e1;
        edge2 = e2;
      }
      else
      {
        edge1 = e2;
        edge2 = e1;
      }
    }
  }

  // Project cur endpoints onto other.

  // First check the endpoints of e1 agaisnt e2
  int do_narrow_region_check = 1;
  if(num_shared_verts == 1 && shared_vert == e1_start_vert)
  {
    // Edges are next to each other.  Check the angle between them
    // before doing anything else.
    double interior_angle = edge1->angle_between(edge2, face);
    if(interior_angle > CUBIT_PI/4.0)
      do_narrow_region_check = 0;
  }
  if(do_narrow_region_check &&
     is_narrow_region_at_point(e1, face, e1_start_vert->coordinates(), e2, tol_sq, closest))
  {
    max_dist_sq = (closest - e1_start_vert->coordinates()).length_squared();
    e1_pos_list.append(new CubitVector(e1_start_vert->coordinates()));
    e2_pos_list.append(new CubitVector(closest));
    e1_vert_list.append(e1_start_vert);
    e2_vert_list.append(NULL);
  }
  do_narrow_region_check = 1;
  if(num_shared_verts == 1 && shared_vert == e1_end_vert)
  {
    // Edges are next to each other.  Check the angle between them
    // before doing anything else.
    double interior_angle = edge1->angle_between(edge2, face);
    if(interior_angle > CUBIT_PI/4.0)
      do_narrow_region_check = 0;
  }
  if(do_narrow_region_check &&
     is_narrow_region_at_point(e1, face, e1_end_vert->coordinates(), e2, tol_sq, closest))
  {
    double cur_dist_sq = (closest - e1_end_vert->coordinates()).length_squared();
    max_dist_sq = max_dist_sq > cur_dist_sq ? max_dist_sq : cur_dist_sq;
    e1_pos_list.append(new CubitVector(e1_end_vert->coordinates()));
    e2_pos_list.append(new CubitVector(closest));
    e1_vert_list.append(e1_end_vert);
    e2_vert_list.append(NULL);
  }

  if(e1_pos_list.size() < 2)
  {
    RefVertex *e2_start_vert = e2->start_vertex();
    RefVertex *e2_end_vert = e2->end_vertex();
    do_narrow_region_check = 1;
	// Now check the end points of e2 against e1
    if(num_shared_verts == 1 && shared_vert == e2_start_vert)
    {
      // Edges are next to each other.  Check the angle between them
      // before doing anything else.
      double interior_angle = edge1->angle_between(edge2, face);
      if(interior_angle > CUBIT_PI/4.0)
        do_narrow_region_check = 0;
    }
    if(do_narrow_region_check &&
       is_narrow_region_at_point(e2, face, e2_start_vert->coordinates(), e1, tol_sq, closest))
    {
      if(e1_pos_list.size() == 0 || !closest.about_equal(*e1_pos_list.get()))
      {
        double cur_dist_sq = (closest - e2_start_vert->coordinates()).length_squared();
        max_dist_sq = max_dist_sq > cur_dist_sq ? max_dist_sq : cur_dist_sq;
        e2_pos_list.append(new CubitVector(e2_start_vert->coordinates()));
        e1_pos_list.append(new CubitVector(closest));
        e2_vert_list.append(e2_start_vert);
        e1_vert_list.append(NULL);
      }
    }
    if(e1_pos_list.size() < 2)
    {
      do_narrow_region_check = 1;
      if(num_shared_verts == 1 && shared_vert == e2_end_vert)
      {
        // Edges are next to each other.  Check the angle between them
        // before doing anything else.
        double interior_angle = edge1->angle_between(edge2, face);
        if(interior_angle > CUBIT_PI/4.0)
          do_narrow_region_check = 0;
      }
      if(do_narrow_region_check &&
         is_narrow_region_at_point(e2, face, e2_end_vert->coordinates(), e1, tol_sq, closest))
      {
        if(e1_pos_list.size() == 0 || !closest.about_equal(*e1_pos_list.get()))
        {
          double cur_dist_sq = (closest - e2_end_vert->coordinates()).length_squared();
          max_dist_sq = max_dist_sq > cur_dist_sq ? max_dist_sq : cur_dist_sq;<--- Variable 'max_dist_sq' is assigned a value that is never used.
          e2_pos_list.append(new CubitVector(e2_end_vert->coordinates()));
          e1_pos_list.append(new CubitVector(closest));
          e2_vert_list.append(e2_end_vert);
          e1_vert_list.append(NULL);
        }
      }
    }
  }

  // At this point we have projected each curves endpoints onto the other curve 
  // and if the projection was "close" we have recorded these as in the lists 
  // as close positions.

  // If we have two sets of narrow points check to see if the region
  // between them is all narrow.
  if(e1_pos_list.size() == 2)
  {
    int w;
    int all_good = 1;
 //   double dist_tol = sqrt(max_dist_sq)*5.0;
    e1_pos_list.reset();
    e2_pos_list.reset();
    CubitVector *cur1 = e1_pos_list.get_and_step();
    CubitVector *cur2 = e1_pos_list.get();
    CubitVector *other1 = e2_pos_list.get_and_step();
    CubitVector *other2 = e2_pos_list.get();

    double len1 = e1->get_arc_length(*cur1, *cur2);
    //We have to check for periodic edges because the narrow points
    //will be the same when one or both of the edges is periodic.
    if(len1 > tol || (e1->is_periodic()|| e2->is_periodic()))
    {
      double len2 = e2->get_arc_length(*other1, *other2);
      if(len2 > tol || (e1->is_periodic()|| e2->is_periodic()))
      {
        double cur_param1 = e1->u_from_position(*cur1);
        double cur_param2 = e1->u_from_position(*cur2);
        int num_divisions = 2;
        CubitVector cur_pos;
        double param_step = (cur_param2-cur_param1)/num_divisions;
        double cur_param = cur_param1 + param_step;
        for(w=1; w<num_divisions && all_good; w++)
        {
          e1->position_from_u(cur_param, cur_pos);
          cur_param += param_step;
          if(is_narrow_region_at_point(e1, face, cur_pos, e2, tol_sq, closest))
          {
            // Sanity check to make sure we aren't splitting off negative space.
            CubitVector mid = (cur_pos + closest)/2.0;
            CubitVector tmp_pt;
            face->get_surface_ptr()->closest_point_trimmed(mid, tmp_pt);
			// If it didn't move we are fine but if it did we need to check
			// a little more.
            if(!mid.about_equal(tmp_pt))
            {
			  // It is possible that the midpoint between the two curves does not lie on the
			  // surface.  This would be the case in a blend surface.  Check to see that
		      // the projected point is just an offset along the normal of the surface
			  // rather than a laterl movement because the space between the two 
			  // curves was actually negative space and not part of the surface.
              CubitVector norm = face->normal_at(tmp_pt);
              CubitVector dir(tmp_pt - mid);
              dir.normalize();
              if(fabs(norm % dir) < .9)
                all_good = 0;
            }
          }
        }
      }
      else
        all_good = 0;
    }
    else 
      all_good = 0;

    if(all_good)
      ret = 1;
    else
    {
      e1_pos_list.remove(cur1);
      e1_pos_list.remove(cur2);
      e2_pos_list.remove(other1);
      e2_pos_list.remove(other2);
      delete cur1;
      delete cur2;
      delete other1;
      delete other2;
    }
  }

  // If we didn't find a continous region that was narrow we may curves
  // that start narrow at one or more of the ends and then move away from each other.
  // We need to find if there is a significant region that is narrow and identify
  // where the two curves start diverging.  Add additional points to the lists
  // at the points where things start diverging.
  if(!ret && e1_pos_list.size() > 0)
  {
    int i;
    e1_pos_list.reset();
    e2_pos_list.reset();
    e1_vert_list.reset();
    e2_vert_list.reset();
    for(i=e1_pos_list.size(); i--;)
    {
      RefVertex *e1_vert = e1_vert_list.get_and_step();
      RefVertex *e2_vert = e2_vert_list.get_and_step();

      RefVertex *cur_vert = NULL;
      RefEdge *cur_edge = NULL, *other_edge = NULL;
      if(e1_vert)
      {
        cur_edge = e1;
        other_edge = e2;
        cur_vert = e1_vert;
      }
      else if(e2_vert)
      {
        cur_edge = e2;
        other_edge = e1;
        cur_vert = e2_vert;
      }
      if(cur_vert)
      {
        CubitVector next_pos;
        CubitVector prev_pos = cur_vert->coordinates();
        int num_incs = 20;
        double step = cur_edge->get_arc_length()/(double)num_incs;
        int still_good = 1;
        int cntr = 0;
        double direction = (cur_vert == cur_edge->start_vertex() ? 1.0 : -1.0);
        // Do coarse traversal along curve to see where we start deviating from
        // narrow.
        while(still_good)
        {
          cur_edge->point_from_arc_length(prev_pos, step*direction, next_pos);
          if(is_narrow_region_at_point(cur_edge, face, next_pos, other_edge, tol_sq, closest) &&
                      cntr < num_incs)
          {
            prev_pos = next_pos;
            cntr++;
          }
          else
            still_good = 0;
        }
        if(cntr < num_incs)
        {
          cntr = 0;
          double cur_arc_length = cur_edge->get_arc_length(prev_pos, next_pos);
          // Do bisection on remaining interval to zero in on point where
          // we go from narrow to non-narrow.
          CubitVector mid_pos;
          double close_enough = tol/20.0;
          while(cur_arc_length > close_enough && cntr < 100)
          {
            cntr++;  // prevent infinite looping
            cur_edge->point_from_arc_length(prev_pos, cur_arc_length*direction/2.0, mid_pos);
            if(is_narrow_region_at_point(cur_edge, face, mid_pos, other_edge, tol_sq, closest))
              prev_pos = mid_pos;
            else
              next_pos = mid_pos;
            cur_arc_length = cur_edge->get_arc_length(prev_pos, next_pos);
          }
          if(cur_edge->get_arc_length(cur_vert->coordinates(), prev_pos) > tol)
          {
            // end up with the position that guarantees a new split curve that
            // is smaller than the small curve size.
            other_edge->closest_point_trimmed(prev_pos, closest);
            if(cur_edge == e1)
            {
              e1_pos_list.append(new CubitVector(prev_pos));
              e2_pos_list.append(new CubitVector(closest));
            }
            else
            {
              e2_pos_list.append(new CubitVector(prev_pos));
              e1_pos_list.append(new CubitVector(closest));
            }
            e1_vert_list.append(NULL);
            e2_vert_list.append(NULL);
            ret = 1;
          }
        }
      }
    }
  }

  // Now remove and regions of close curves between which 
  // there is negative space (not actually part of the surface).
  if(ret)
  {
    int i, j;
    e1_pos_list.reset();
    e2_pos_list.reset();
    e1_vert_list.reset();
    e2_vert_list.reset();
    for(i=e1_pos_list.size(); i--;)
    {
      CubitVector *e1_pos = e1_pos_list.get();
      CubitVector *e2_pos = e2_pos_list.get();
      int num_divisions = 6;
      CubitVector step = (*e2_pos - *e1_pos)/num_divisions;
      CubitVector cur_pos = *e1_pos + step;
      int removed = 0;
      for(j=1; j<num_divisions; j++)
      {
        CubitVector tmp_pt;
        face->get_surface_ptr()->closest_point_trimmed(cur_pos, tmp_pt);
        if(!cur_pos.about_equal(tmp_pt))
        {
          CubitVector norm = face->normal_at(tmp_pt);
          CubitVector dir(tmp_pt - cur_pos);
          dir.normalize();
          if(fabs(norm % dir) < .9)
          {
            removed = 1;
            j=num_divisions;
            delete e1_pos_list.remove();
            delete e2_pos_list.remove();
            e1_vert_list.remove();
            e2_vert_list.remove();
          }
        }
        else
          cur_pos += step;
      }
      if(!removed)
      {
        e1_pos_list.step();
        e2_pos_list.step();
        e1_vert_list.step();
        e2_vert_list.step();
      }
    }

    if(e1_pos_list.size() == 0)
      ret = 0;
  }

  return ret;
}

void GeomMeasureTool::find_small_curves( DLIList <RefVolume*> &ref_vols,
                                         double tol,
                                         DLIList <RefEdge*> &small_curves,
                                         DLIList <double> &small_lengths)
{
  int ii, jj;
  DLIList <RefEdge*> ref_edges, temp_edges;
  RefVolume *ref_vol;
  RefEdge *curr_edge;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_edges(temp_edges);
      //uniquely add the edges.
    for ( jj = temp_edges.size(); jj > 0; jj-- )
    {
      curr_edge = temp_edges.pop();
      if ( curr_edge->marked()== 0 )
      {
        curr_edge->marked(1);
        ref_edges.append(curr_edge);
      }
    }
  }

  int num_curves = ref_edges.size();
  ProgressTool *progress_ptr = NULL;
  if (num_curves> 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Small Curves", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }

  int total_curves = 0;
  double curr_percent = 0.0;
  double length;
    //find the small curves and reset the marked flag.
  for ( ii = ref_edges.size(); ii > 0; ii-- )
  {
    total_curves++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_curves))/((double)(num_curves));
      progress_ptr->percent(curr_percent);
    }
    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }

    curr_edge = ref_edges.get_and_step();
    curr_edge->marked(0);

    //skip point curves
    if( curr_edge->geometry_type() == POINT_CURVE_TYPE )
      continue;

    length = curr_edge->measure();
    if ( length <= tol )
    {
      small_curves.append(curr_edge);
      small_lengths.append(length);
    }
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();

  return;
}

RefEdge* GeomMeasureTool::find_first_small_curve(RefVolume* vol,
                                         double tol)
{
  RefEdge *ret = NULL;
  int j;
  DLIList <RefEdge*> ref_edges;
  vol->ref_edges(ref_edges);
  for(j=ref_edges.size(); j > 0 && !ret; j--)
  {
    RefEdge *curr_edge = ref_edges.get_and_step();
    if(curr_edge->measure() <= tol)
      ret = curr_edge;
  }
  return ret;
}

void GeomMeasureTool::find_narrow_faces(DLIList<RefVolume*> &ref_vols,
                                        double small_curve_size,
                                        DLIList<RefFace*> &narrow_faces,
                                        DLIList<RefFace*> &surfs_to_ignore)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;

  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    DLIList<RefFace*> faces;
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(faces);
    for ( jj = faces.size(); jj > 0; jj-- )
    {
      curr_face = faces.get_and_step();
      curr_face->marked(0);
      temp_faces.append(curr_face);
    }
  }

  //uniquely add the faces.
  for ( jj = temp_faces.size(); jj > 0; jj-- )
  {
    curr_face = temp_faces.get_and_step();
    if ( curr_face->marked()== 0 )
    {
      curr_face->marked(1);
      ref_faces.append(curr_face);
    }
  }

  int num_faces = ref_faces.size();
  ProgressTool *progress_ptr = NULL;
  if (num_faces > 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Narrow Surfaces", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }

  int total_faces = 0;
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.

  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    total_faces++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_faces))/((double)(num_faces));
      progress_ptr->percent(curr_percent);
    }

    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }

    curr_face = ref_faces.get_and_step();
    if(!surfs_to_ignore.is_in_list(curr_face))
    {
      if(narrow_region_exists(curr_face, small_curve_size))
      {
        DLIList<CubitVector> split_pos1_list;
        DLIList<CubitVector> split_pos2_list;
        find_split_points_for_narrow_regions(curr_face,
          small_curve_size, split_pos1_list, split_pos2_list); 
        if(split_pos1_list.size() == 0)
          narrow_faces.append_unique(curr_face);
      }
    }
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();

  return;
}

void GeomMeasureTool::find_small_faces( DLIList <RefVolume*> &ref_vols,
                                        double tol,
                                        DLIList <RefFace*> &small_faces)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    DLIList<RefFace*> faces;
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(faces);
    for ( jj = faces.size(); jj > 0; jj-- )
    {
      curr_face = faces.get_and_step();
      curr_face->marked(0);
      temp_faces.append(curr_face);
    }
  }

  //uniquely add the faces.
  for ( jj = temp_faces.size(); jj > 0; jj-- )
  {
    curr_face = temp_faces.get_and_step();
    if ( curr_face->marked()== 0 )
    {
      curr_face->marked(1);
      ref_faces.append(curr_face);
    }
  }

  int num_faces = ref_faces.size();
  ProgressTool *progress_ptr = NULL;
  if (num_faces > 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Small Surfaces", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }

  int total_faces = 0;
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.
  double area;
    //find the small curves and reset the marked flag.
  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    total_faces++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_faces))/((double)(num_faces));
      progress_ptr->percent(curr_percent);
    }

    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }

    curr_face = ref_faces.get_and_step();
    area = measure_area(curr_face);
    if ( area <= tol )
      small_faces.append(curr_face);
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();

  return;
}

void GeomMeasureTool::find_closed_narrow_faces( DLIList <RefVolume*> &ref_vols,
                                        double tol,
                                        DLIList <RefFace*> &face_list)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    DLIList<RefFace*> faces;
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(faces);
    for ( jj = faces.size(); jj > 0; jj-- )
    {
      curr_face = faces.get_and_step();
      curr_face->marked(0);
      temp_faces.append(curr_face);
    }
  }

  //uniquely add the faces.
  for ( jj = temp_faces.size(); jj > 0; jj-- )
  {
    curr_face = temp_faces.get_and_step();
    if ( curr_face->marked()== 0 )
    {
      curr_face->marked(1);
      ref_faces.append(curr_face);
    }
  }

  int num_faces = ref_faces.size();
  ProgressTool *progress_ptr = NULL;
  if (num_faces > 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Small Surfaces", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }

  int total_faces = 0;
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.
  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    total_faces++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_faces))/((double)(num_faces));
      progress_ptr->percent(curr_percent);
    }

    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }

    curr_face = ref_faces.get_and_step();
    if(curr_face->get_surface_ptr()->is_closed_in_U() || curr_face->get_surface_ptr()->is_closed_in_V())
    {
      if(curr_face->num_loops() == 2)
      {
        DLIList<RefEdge*> ref_edge_list;
        curr_face->ref_edges(ref_edge_list);
        if(ref_edge_list.size() == 2)
        {
          int num_pts = 5;
          double start, end;
          RefEdge *e1 = ref_edge_list.get_and_step();
          RefEdge *e2 = ref_edge_list.get();
          e1->get_param_range(start, end);
          double dt = (end-start)/(double)(num_pts-1);
          double t = start;
          bool one_bad = false;
          double dist_sq = tol*tol;
          for(jj=0; jj<num_pts && one_bad == false; jj++)
          {
            CubitVector pos1, pos2;
            e1->position_from_u(t, pos1);
            e2->closest_point_trimmed(pos1, pos2);
            if((pos1-pos2).length_squared() > dist_sq)
              one_bad = true;
            t += dt;
          }
          if(one_bad == false)
          {
            face_list.append(curr_face);
          }
        }
      }
    }
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();

  return;
}

void GeomMeasureTool::find_small_faces_hydraulic_radius( DLIList <RefVolume*> &ref_vols,
                                                         double tol,
                                                         DLIList <RefFace*> &small_faces,
                                                         DLIList <double> &small_hyd_rad,
                                                         double &percent_planar,
                                                         double &percent_pl_co)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(temp_faces);
      //uniquely add the faces.
    for ( jj = temp_faces.size(); jj > 0; jj-- )
    {
      curr_face = temp_faces.pop();
      if ( curr_face->marked()== 0 )
      {
        curr_face->marked(1);
        ref_faces.append(curr_face);
      }
    }
  }
  double area;
  double length;
  int total_faces = 0;
  int total_plane = 0;
  int total_cone = 0;
  double area_plane = 0.0;
  double total_area = 0.0;
    //find the small curves and reset the marked flag.
  DLIList <CoEdge*> co_edges;
  int num_faces = ref_faces.size();
  ProgressTool *progress_ptr = NULL;
  if (num_faces > 20)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Small Surface Progress", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }
  double curr_percent = 0.0;
  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    curr_face = ref_faces.get_and_step();
    curr_face->marked(0);
    area = measure_area(curr_face);
    total_area += area;
    total_faces++;
      //update the progress..
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(total_faces))/((double)(num_faces));
      progress_ptr->percent(curr_percent);
    }
    if ( AppUtil::instance()->interrupt() )
    {
        //interrpt.  We need to exit.
      if ( progress_ptr != NULL )
        progress_ptr->end();
        //just leave what has been calculated...
      return;
    }
    if ( curr_face->geometry_type() == PLANE_SURFACE_TYPE )
    {
      total_plane++;
      area_plane += area;
    }
    else if ( curr_face->geometry_type() == CONE_SURFACE_TYPE )
      total_cone++;
    co_edges.clean_out();
    curr_face->co_edges(co_edges);
    length = 0.0;
    for ( jj = co_edges.size(); jj > 0; jj-- )
    {
      length += co_edges.get_and_step()->get_ref_edge_ptr()->measure();
    }
      //reset the mark.
    curr_face->marked(0);
      //compute the hydraulic radius 4*(A/P).
    if ( length <= CUBIT_RESABS )
    {
      PRINT_INFO("Total Perimeter Length of Surface %d is less than tolerance.\n",
                 curr_face->id());
      
      continue;
    }
    area = 4.0*(area/length);
    if ( area <= tol )
    {
      small_faces.append(curr_face);
      small_hyd_rad.append(area);
    }
  }
  if ( total_faces > 0 )
  {
    percent_planar = 100.0*area_plane/total_area;
    percent_pl_co = 100.0*((double)total_plane+(double)total_cone)/(double)total_faces;
  }
  else
  {
    percent_planar = 0.;
    percent_pl_co = 0.;
  }
  if ( progress_ptr != NULL )
  {
    progress_ptr->end();
  }
  
  return;
}
void GeomMeasureTool::find_small_volumes( DLIList <RefVolume*> &ref_vols,
                                          double tol,
                                          DLIList <RefVolume*> &small_volumes)
{
  int ii;
  RefVolume *ref_vol;
  double volume;
  
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    ref_vol = ref_vols.get_and_step();
    volume = ref_vol->measure();
    if ( volume <= tol )
      small_volumes.append(ref_vol);
  }
  return;
}
void GeomMeasureTool::find_small_volumes_hydraulic_radius( DLIList <RefVolume*> &ref_vols,
                                                           double tol,
                                                           DLIList <RefVolume*> &small_volumes,
                                                           DLIList <double> &small_hyd_rad)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  double area;
  double hyd_volume;
  
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(temp_faces);
    area = 0.0;
    for ( jj = temp_faces.size(); jj > 0; jj-- )
    {
      curr_face = temp_faces.pop();
      area += measure_area(curr_face);
    }
    if ( area <= CUBIT_RESABS )
      continue;
    hyd_volume = 6.0*(ref_vol->measure() / area);
    if ( hyd_volume <= tol )
    {
      small_hyd_rad.append(hyd_volume);
      small_volumes.append(ref_vol);
    }
  }
  return;
}
///
/// Find the tangential meetings in the volume.
/// This specifically looks for surfaces that meet tangentially
/// that would be a problem.  Usually these are surfaces that
/// come into a side 180 degrees and on top there is a
/// sharpe angle.  Usually if there isn't a sharpe curve
/// angle these meetings are not bad.
/// Note that this function assumes that you are passing
/// in sets of curve edges that have small interior angles between them.
/// It also assumes that the edge pairs are ordered as they would
/// be found in the surfaces (first edge then next edge).
/// Basically, call the funciton, find_interior_curve_angles
/// before calling this function, and pass this function those results.
///
void GeomMeasureTool::find_sharp_tangential_meets( RefVolume *ref_volume,
                                                   DLIList <RefEdge*> &small_angle_edge_pairs,
                                                   DLIList <RefFace*> &tangential_surface_pairs )
{
    //Okay, given the small angle edge pairs.  See if there are surfaces that meet tangentially.
    //I'm really looking for something spefic.
  RefEdge *ref_edge_1, *ref_edge_2;
  int ii, jj;
  DLIList <Loop*> loops_1, loops_2;
  Loop *common_loop;
  const double rad_to_deg = 180.0/CUBIT_PI;
  RefFace *ref_face;
  
    //loop over for each edge pair.
  for ( ii = 0; ii < small_angle_edge_pairs.size(); ii += 2 )
  {
    ref_edge_1 = small_angle_edge_pairs.get_and_step();
    ref_edge_2 = small_angle_edge_pairs.get_and_step();
      //find the loop that these edges are both on.
    loops_1.clean_out();
    loops_2.clean_out();
    ref_edge_1->loops(loops_1);
    ref_edge_2->loops(loops_2);
    common_loop = NULL;
    for ( jj = loops_1.size(); jj > 0; jj-- )
    {
      if ( !loops_2.move_to(loops_1.get()) )
        loops_1.remove();
      else
        loops_1.step();
    }
    if ( loops_1.size() == 1 )
      common_loop = loops_1.get();
    else
    {
        //find the loop that has a co_edge from ref_edge_1
        //then a co_edge from ref_edge_2.
      DLIList <CoEdge*> co_edges;
      Loop *tmp_loop;
      int kk;
      CoEdge *co_edge;
      for ( jj = loops_1.size(); jj > 0; jj-- )
      {
        tmp_loop = loops_1.get_and_step();
        co_edges.clean_out();
        tmp_loop->ordered_co_edges(co_edges);
        for ( kk = co_edges.size(); kk > 0; kk-- )
        {
          co_edge = co_edges.get_and_step();
          if ( co_edge->get_ref_edge_ptr() == ref_edge_1 )
          {
            if ( co_edges.get()->get_ref_edge_ptr() == ref_edge_2 )
            {
              common_loop = tmp_loop;
              break;
            }
            else
                //not this loop..
              break;
          }
        }
        if ( common_loop != NULL )
          break;
      }
    }
    assert ( common_loop != NULL );
      //now find surface.
      //Then see if there are surfaces that meet at 180 degrees that
      //are 90* rotated from here.
    ref_face = common_loop->get_ref_face_ptr();
      //Okay, refface is the surface on which we have the bad angle.
      //One of these curves will have a concave angle and the other a
      //convex angle.  The surface on the other side of the convex angle
      //we need to check to see if it comes tangentially into another
      //surface (dihedral angle of 180.)
    RefFace *ref_face_1 = ref_edge_1->other_face(ref_face, ref_volume);
    if( ref_face_1 == NULL )
      continue;

    double surf_angle_1, surf_angle_2;
    RefFace *tangential_face = NULL;
    RefEdge *edge_next_to = NULL;
    surf_angle_1 =  GeometryQueryTool::instance()->surface_angle(ref_face,
                                                                 ref_face_1,
                                                                 ref_edge_1,
                                                                 ref_volume);
    surf_angle_1 *= rad_to_deg;
    RefFace *ref_face_2;
    ref_face_2 = ref_edge_2->other_face(ref_face, ref_volume);
    if( ref_face_2 == NULL )
      continue;
    surf_angle_2 = GeometryQueryTool::instance()->surface_angle(ref_face,
                                                                ref_face_2,
                                                                ref_edge_2,
                                                                ref_volume);
    surf_angle_2 *= rad_to_deg;
    if ( surf_angle_1 < 180.0 && surf_angle_2 > 180.0 )
    {
      tangential_face = ref_face_1;
      edge_next_to = ref_edge_1;
    }
    else if ( surf_angle_2 < 180.0 && surf_angle_1 > 180.0 )
    {
      tangential_face = ref_face_2;
      edge_next_to = ref_edge_2;
    }
    if ( tangential_face == NULL ) // This isn't the config we seek.
      continue;

      //Now, on this face, there is an edge, next to edge_next_to,
      //that will have an interior angle of 180.0.  If that is the
      //case then this is what we seek.
    RefVertex* ref_vert = ref_edge_1->common_ref_vertex(ref_edge_2,
                                                        ref_face);
    DLIList <RefEdge*> tmp_ref_edges;
    ref_vert->ref_edges(tmp_ref_edges);
      //Find the edge that is on tangential face and is not edge_next_to.
    RefEdge *tangential_edge=NULL, *tmp_edge;
    for ( jj = tmp_ref_edges.size(); jj > 0; jj-- )
    {
      tmp_edge = tmp_ref_edges.get_and_step();
      if ( tmp_edge != edge_next_to &&
           tmp_edge->is_directly_related(tangential_face) )
      {
        tangential_edge = tmp_edge;
        break;
      }
    }
    if ( tangential_edge == NULL )
        //not what we are looking for.
      continue;
      //Now get the other face and measure the dihedral angle.
    RefFace *other_face = tangential_edge->other_face(tangential_face,
                                                      ref_volume);
    double tan_angle;
    tan_angle = GeometryQueryTool::instance()->surface_angle(tangential_face,
                                                             other_face,
                                                             tangential_edge,
                                                             ref_volume);
    tan_angle *= rad_to_deg;
      //If they are within tolerance, then we have it...
    if ( tan_angle > 165.0 && tan_angle < 195.0 )
    {
      tangential_surface_pairs.append(tangential_face);
      tangential_surface_pairs.append(other_face);
    }
  }
}
    
      
    
void GeomMeasureTool::find_interior_curve_angles( RefVolume *ref_volume,
                                                  double upper_bound,
                                                  double lower_bound,
                                                  DLIList <RefEdge*> &large_edge_angles,
                                                  DLIList <RefEdge*> &small_edge_angles,
                                                  DLIList <double> &large_angles,
                                                  DLIList <double> &small_angles,
                                                  int &total_interior,
                                                  int &total_fuzzy)
{
  int ii, jj, kk;
  DLIList <RefFace*> ref_faces;
  RefFace *curr_face;
  total_interior = 0;
  total_fuzzy = 0;
  ref_volume->ref_faces(ref_faces);
  
    //Okay now loop over the edge loops of the reffaces
    //and find the interior angles.
  DLIList<Loop*> loops;
  Loop *curr_loop;
  DLIList <CoEdge*> co_edges;
  CoEdge *curr_edge, *next_edge;
  double angle = 0.0;
  const double RAD_TO_DEG = 180.0/CUBIT_PI;
  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    curr_face = ref_faces.get_and_step(); 
    curr_face->loops(loops);
      //Now loop over these loops.
    for ( jj = loops.size(); jj > 0; jj-- )
    {
      curr_loop = loops.pop();
        //now loop over the edges in this
      co_edges.clean_out();
      curr_loop->ordered_co_edges(co_edges);
      for ( kk = co_edges.size(); kk > 0; kk-- )
      {
        curr_edge = co_edges.get_and_step();
        next_edge = co_edges.get();
	   
        if ( curr_edge == next_edge )
        {
            //This is a hardpoint
          if ( curr_edge->get_ref_edge_ptr()->geometry_type() == POINT_CURVE_TYPE )
          {
            angle = 360.0;
              //for now ignore this..
            continue;
          }
          else
          {
            RefEdge *the_edge = curr_edge->get_ref_edge_ptr();
              //Get points 1% from the ends in both directions.
            CubitVector point_start, point_end;
            CubitStatus stat = the_edge->position_from_fraction(0.01,
                                                                point_start);
            if ( stat != CUBIT_SUCCESS )
              angle = 180.0;
            else
            {
              stat = the_edge->position_from_fraction(0.99,
                                                      point_end);
              if ( stat != CUBIT_SUCCESS )
                angle = 180.0;
              else
              {
                CubitVector normal = curr_face->normal_at(point_start, NULL);
                CubitVector tangent_1, tangent_2;
                stat = the_edge->tangent( point_start, tangent_1, curr_face);
                CubitStatus stat2 = the_edge->tangent( point_end, tangent_2, curr_face);
                if ( stat != CUBIT_SUCCESS || stat2 != CUBIT_SUCCESS )
                  angle = 180.0;
                else
                {
                    //one of the tangents will be pointing the wrong
                    //direction, so reverse it based on the sense.
                  if ( curr_edge->get_sense() == CUBIT_REVERSED )
                    tangent_1 = -tangent_1;
                  else
                    tangent_2 = -tangent_2;
                  angle = normal.vector_angle(tangent_2, tangent_1);
                  angle *= RAD_TO_DEG;
                }
              }
            }
          }
        }
        else if ( curr_edge->get_ref_edge_ptr() == next_edge->get_ref_edge_ptr() )
        {
            //This is the end of a hard line. Set this angle because
            //it is tricky to measure it.
          angle = 360.0;
        }
        else if ( curr_edge != next_edge )<--- Condition 'curr_edge!=next_edge' is always true
        {
          angle =
            GeometryQueryTool::instance()->geometric_angle(curr_edge,
                                                           next_edge);
          angle *= RAD_TO_DEG;
        }
        total_interior++;
          //count the number of angles that are fuzzy.
        if ( angle <= GEOM_END_LOWER )
          total_fuzzy++;
        else if ( angle >= GEOM_END_UPPER &&
                  angle <= GEOM_SIDE_LOWER )
          total_fuzzy++;
        else if ( angle >= GEOM_SIDE_UPPER &&
                  angle <= GEOM_CORNER_LOWER )
          total_fuzzy++;
        else if ( angle >= GEOM_CORNER_UPPER )
          total_fuzzy++;
        if ( angle <= lower_bound )
        {
          small_edge_angles.append(curr_edge->get_ref_edge_ptr());
          small_edge_angles.append(next_edge->get_ref_edge_ptr());
          small_angles.append(angle);
        }
        if ( angle >= upper_bound )
        {
          large_edge_angles.append(curr_edge->get_ref_edge_ptr());
          large_edge_angles.append(next_edge->get_ref_edge_ptr());
          large_angles.append(angle);
        }
      }
    }
  }
  return;
}

void GeomMeasureTool::find_dihedral_angles( DLIList <RefVolume*> &ref_vols,
                                            double upper_bound,
                                            double lower_bound,
                                            DLIList <RefFace*> &large_face_angles,
                                            DLIList <RefFace*> &small_face_angles,
                                            DLIList <double> &large_angles,
                                            DLIList <double> &small_angles,
                                            int &total_interior,
                                            int &total_fuzzy,
                                            int &total_not_flat)
{
  int ii;
  RefVolume *ref_vol;
  total_interior = 0;
  total_fuzzy = 0;
  total_not_flat = 0;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    ref_vol = ref_vols.get_and_step();
    find_dihedral_angles(ref_vol, upper_bound,
                         lower_bound,
                         large_face_angles,
                         small_face_angles,
                         large_angles, small_angles,
                         total_interior, total_fuzzy, total_not_flat);
  }
  return;
}
void GeomMeasureTool::find_dihedral_angles(RefVolume *curr_volume,
                                           double upper_bound,
                                           double lower_bound,
                                           DLIList <RefFace*> &large_face_angles,
                                           DLIList <RefFace*> &small_face_angles,
                                           DLIList <double> &large_angles,
                                           DLIList <double> &small_angles,
                                           int &total_interior,
                                           int &total_fuzzy, int &total_not_flat)
{
  int ii, jj, shared_angles = 0;
  double common_angle;
  DLIList <RefFace*> face_list;
  RefFace *curr_face_1, *curr_face_2;
  RefFace *tmp_face;
  RefEdge *common_edge;
  DLIList <RefVolume*> ref_vols;
  DLIList<RefEdge*> vol_edges;
  curr_volume->ref_edges(vol_edges);

  //maybe the body is a sheet-body...don't print errors if so
  bool is_sheet_body = false;
  if( curr_volume->measure() == 0 )
    is_sheet_body = true;

  for ( ii = vol_edges.size(); ii > 0; ii-- )
  {
    common_edge = vol_edges.get_and_step();

    //skip point curves
    if( common_edge->geometry_type() == POINT_CURVE_TYPE )
      continue;

    curr_face_1 = NULL;
    curr_face_2 = NULL;
    face_list.clean_out();
    common_edge->ref_faces(face_list);
      //find the two faces that are part of this volume.
    for ( jj = face_list.size(); jj > 0; jj-- )
    {
      tmp_face = face_list.get_and_step();
      ref_vols.clean_out();
      tmp_face->ref_volumes(ref_vols);
      if ( ref_vols.move_to(curr_volume) )
      {
        if ( curr_face_1 == NULL )
          curr_face_1 = tmp_face;
        else if ( curr_face_2 == NULL )
        {
          curr_face_2 = tmp_face;
          break;
        }
      }
    }
    if ( curr_face_1 == NULL ||
         curr_face_2 == NULL )
    {
      if( is_sheet_body == false )
        PRINT_ERROR("Problems finding connected surfaces to a curve\n"
                   "\tfor measuring dihedral angles.\n");
      continue;
    }
    shared_angles++;
    common_angle =  GeometryQueryTool::instance()->surface_angle(curr_face_1,
                                                                 curr_face_2,
                                                                 common_edge,
                                                                 curr_volume);
    common_angle *= 180.0/CUBIT_PI;
    total_interior++;
      //use hard coded angles because we want these a little looser.
    if ( common_angle >= 200.0 ||
         common_angle <= 160.0 )
      total_not_flat++;
    
    if ( curr_face_1->geometry_type() != PLANE_SURFACE_TYPE ||
         curr_face_2->geometry_type() != PLANE_SURFACE_TYPE )
      total_fuzzy++;
    else if ( common_angle <= GEOM_END_LOWER )
      total_fuzzy++;
    else if ( common_angle >= GEOM_END_UPPER &&
              common_angle <= GEOM_SIDE_LOWER )
      total_fuzzy++;
    else if ( common_angle >= GEOM_SIDE_UPPER &&
              common_angle <= GEOM_CORNER_LOWER )
      total_fuzzy++;
    else if ( common_angle >= GEOM_CORNER_UPPER )
      total_fuzzy++;
    
    if( common_angle <= lower_bound )
    {
      small_face_angles.append(curr_face_1);
      small_face_angles.append(curr_face_2);
      small_angles.append(common_angle);
    }
    if( common_angle >= upper_bound )
    {
      large_face_angles.append(curr_face_1);
      large_face_angles.append(curr_face_2);
      large_angles.append(common_angle);
    }
  }
    //clean up the edge marks.
  for ( ii = vol_edges.size(); ii > 0; ii-- )
    vol_edges.get_and_step()->marked(0);
}
void GeomMeasureTool::find_close_loops(DLIList <RefVolume*> &ref_vols,
                                       DLIList <RefEdge*> &close_edges,
                                       DLIList <RefFace*> &close_loop_faces,
                                       DLIList <double> &small_lengths,
                                       double tol)
{
  int ii, jj;
  DLIList <RefFace*> ref_faces, temp_faces;
  RefVolume *ref_vol;
  RefFace *curr_face;
  for ( ii = 0; ii < ref_vols.size(); ii++ )
  {
    ref_vol = ref_vols.get_and_step();
    ref_vol->ref_faces(temp_faces);
      //uniquely add the faces.
    for ( jj = temp_faces.size(); jj > 0; jj-- )
    {
      curr_face = temp_faces.pop();
      ref_faces.append(curr_face);
    }
  }
  DLIList <RefEdge*> tmp_close_edges;
  DLIList <double> tmp_small_lengths;
  
  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    curr_face = ref_faces.get_and_step();
    curr_face->marked(0);
    if ( curr_face->num_loops() < 2 )
      continue;
    tmp_close_edges.clean_out();
    tmp_small_lengths.clean_out();
    find_close_loops( curr_face, tmp_close_edges,
                      tmp_small_lengths, tol);
    if ( tmp_close_edges.size() > 0 )
    {
      assert(tmp_close_edges.size()%2 == 0 );
      tmp_close_edges.reset();
      tmp_small_lengths.reset();
      for ( jj = tmp_close_edges.size()/2; jj > 0; jj-- )
      {
        close_edges.append(tmp_close_edges.get_and_step());
        close_edges.append(tmp_close_edges.get_and_step());
        small_lengths.append(tmp_small_lengths.get_and_step());
        close_loop_faces.append(curr_face);
      }
    }
  }
  return;
}

  
void GeomMeasureTool::find_close_loops(RefFace *face,
                                       DLIList <RefEdge*> &close_edges,
                                       DLIList <double> &small_lengths,
                                       double tol)
{
    //only do faces with multiple loops since we are measuring the
    //distances between these loops.
  if ( face->num_loops() < 2 )
    return;
    //This is the most tricky of these functions.
    //To do this I'm going to facet the edges, then use an AbstractTree to
    //find the minimum distance between the loops and between a single
    //loop.
  PointLoopList boundary_point_loops;
  CubitStatus stat;
  stat = get_boundary_points(face, boundary_point_loops, GEOMETRY_RESABS*100);
  if ( stat != CUBIT_SUCCESS )
    return;
  
  SegLoopList boundary_seg_loops;
  stat = convert_to_lines( boundary_point_loops,
                           boundary_seg_loops,
                           face);
  if ( stat != CUBIT_SUCCESS )
    return;

    //Now add the points to the AbstractTree.
  DLIList<AbstractTree<GeomSeg*>*> atree_list;
  AbstractTree<GeomSeg*> *curr_tree;
  SegList *seg_list;
  GeomSeg  *curr_seg;
  int ii,jj, kk;
  //const double angle_convert = 180.0/CUBIT_PI;
  boundary_seg_loops.reset();
  int num_segs = 0;
  for (ii = 0; ii < boundary_seg_loops.size(); ii++ )
  {
    seg_list = boundary_seg_loops.get_and_step();
    curr_tree = new RTree<GeomSeg*>(GEOMETRY_RESABS);
    for ( jj = seg_list->size(); jj > 0; jj-- )
    {
        //build the r-tree.
      num_segs++;
      curr_seg = seg_list->get_and_step();
      curr_tree->add(curr_seg);
        //calculate the interior angles.
    }
    atree_list.append(curr_tree);
  }
    //determine the minimum distance between the loops.
  PointList *curr_points;
  GeomPoint *curr_point;
  DLIList <GeomSeg*> nearest_neighbors;
  RefEdge *closest_edge_1, *closest_edge_2;
  CubitVector curr_loc;
  double closest_dist;
  double min_for_loop_squared;
  atree_list.reset();
  // This searching was orignally coded to not be an n-squared
  // search but I changed it to be n-squared because the
  // way the loops are compared comparing loop 1 against loop 2 may
  // give a different result than comparing loop 2 against loop 1.
  for ( ii = 0; ii < atree_list.size(); ii++ )
  {
    curr_points = boundary_point_loops.get_and_step();
    for ( jj = 1; jj < atree_list.size(); jj++ )
    {
      min_for_loop_squared = CUBIT_DBL_MAX;
      closest_edge_1 = NULL;
      closest_edge_2 = NULL;
      curr_tree = atree_list.next(ii+jj);
        //now for every point in curr_points, find it's closest_point
        //in the curr_tree.
      for ( kk = 0; kk < curr_points->size(); kk++ )
      {
        curr_point = curr_points->get_and_step();
        curr_loc.set(curr_point->coordinates());
        nearest_neighbors.clean_out();
        curr_tree->k_nearest_neighbor(curr_loc,
                                      1, closest_dist, nearest_neighbors,
                                      dist_sq_point_data);
        if ( nearest_neighbors.size() == 0)
        {
            //hmm, not sure what is wrong here.
          PRINT_ERROR("Can't find closest point between loops.\n");
        }
        if (closest_dist <= tol*tol && closest_dist < min_for_loop_squared )
        {
            //just store the smaller ones.  Don't store
          RefEntity *ref_ent = curr_point->owner();
          GeomSeg *near_seg = nearest_neighbors.get();
          RefEdge *ref_edge_1 = CAST_TO(ref_ent, RefEdge);
          if ( ref_edge_1 == NULL )
          {
              //assume this is a vertex.
            RefVertex *ref_vert = CAST_TO(ref_ent, RefVertex);
            if ( ref_vert == NULL )
            {
              PRINT_ERROR("problem with point ownership in closest loops.\n");
              continue;
            }
            DLIList <RefEdge*> ref_edges;
            ref_vert->ref_edges(ref_edges);
            int ll;
            RefEdge *the_edge = NULL;
            for ( ll = ref_edges.size(); ll > 0; ll-- )
            {
              if ( ref_edges.get()->is_directly_related(face) )
              {
                the_edge = ref_edges.get();
                break;
              }
              ref_edges.step();
            }
            if ( the_edge == NULL )
            {
              PRINT_ERROR("problem with point ownership in closest loops.\n");
              continue;
            }
            ref_edge_1 = the_edge;
          }
          ref_ent = near_seg->owner();
          RefEdge *ref_edge_2 = CAST_TO(ref_ent, RefEdge);
          if ( ref_edge_2 == NULL )
          {
            PRINT_ERROR("problem with point ownership in closest loops.\n");
            continue;
          }
          
          min_for_loop_squared = closest_dist;
          closest_edge_1 = ref_edge_1;
          closest_edge_2 = ref_edge_2;
        }
      }
      if ( closest_edge_1 != NULL )
      {
        close_edges.append(closest_edge_1);
        close_edges.append(closest_edge_2);
        small_lengths.append(sqrt(min_for_loop_squared));
      }
    }
  }
    //clean up the memory.
  int ll,mm;
  atree_list.reset();
  boundary_seg_loops.reset();
  for ( ll = boundary_seg_loops.size(); ll > 0; ll-- )
  {
    delete atree_list.pop();
    seg_list = boundary_seg_loops.pop();
    for ( mm = seg_list->size(); mm > 0; mm-- )
      delete seg_list->pop();
    delete seg_list;
  }
		
  PointList *point_list;
  for ( ll = boundary_point_loops.size(); ll > 0; ll-- )
  {
    point_list = boundary_point_loops.pop();
    for ( mm = point_list->size(); mm > 0; mm-- )
    {
      GeomPoint *tmp_point = point_list->pop();
        //delete the CubitPointData
      delete tmp_point;
    }
    delete point_list;
  }
  return;
}
double GeomMeasureTool::measure_area(RefFace* curr_face)
{
  double area;
  GeomDataObserver *g_d_o = GeomDataObserver::get(curr_face);
  if ( g_d_o == NULL )
    g_d_o = GeomDataObserver::create(curr_face);

  if ( g_d_o == NULL )
  {
    assert(g_d_o != NULL );
    return curr_face->measure();
  }
  if ( !g_d_o->is_measure_set() )
  {
    area  = (curr_face->get_surface_ptr())->measure();
    g_d_o->set_measure(area);
  }
  area = g_d_o->get_measure();
  return area;
}

///
///  This function simply gets the bad entities of the volume.  This
///  assumes the volume is from some solid modelar where this function
///  is defined. If not, it will just be empty...
///
void GeomMeasureTool::find_bad_geometry(RefVolume *volume,
                                        DLIList <RefEntity*> &bad_ents)
{
  Lump *lump_ptr = volume->get_lump_ptr();
  DLIList <TopologyEntity*> bad_top_ents;
  lump_ptr->validate(volume->entity_name(), bad_top_ents);
    //Now go through and get a RefEnttiy for each entity.
  RefEntity *ref_ent;
  TopologyEntity *topo_ent;
  GroupingEntity *gr_ent;
  SenseEntity *se_ent;
  
  int ii;
  for (ii = bad_top_ents.size(); ii > 0; ii-- )
  {
    topo_ent = bad_top_ents.pop();
    ref_ent = CAST_TO(topo_ent, RefEntity);
    if ( ref_ent != NULL )
    {
      bad_ents.append(ref_ent);
      continue;
    }
    gr_ent = CAST_TO(topo_ent, GroupingEntity);
    if ( gr_ent != NULL )
    {
      Chain *ch = CAST_TO(gr_ent, Chain);
      if ( ch != NULL )
      {
        RefVertex *start_v = ch->start_vertex();
        RefVertex *end_v = ch->end_vertex();
        RefEdge *ref_edge = start_v->common_ref_edge(end_v);
        bad_ents.append(ref_edge);
        continue;
      }
      Loop *lo = CAST_TO(gr_ent, Loop);
      if ( lo != NULL )
      {
        RefFace *rf = lo->get_ref_face_ptr();
        bad_ents.append(rf);
        continue;
      }
      Shell *sh = CAST_TO(gr_ent, Shell);
      if (sh != NULL )
      {
        RefVolume *rv = sh->get_ref_volume_ptr();
        bad_ents.append(rv);
        continue;
      }
      else
      {
          //this is incase there is some other entity we don't
          //know about, just get the body...
        Body *body = topo_ent->body();
        bad_ents.append(body);
        continue;
      }
    }
    se_ent = CAST_TO(topo_ent, SenseEntity);
    if ( se_ent != NULL )
    {
      BasicTopologyEntity *be = se_ent->get_basic_topology_entity_ptr();
      bad_ents.append(be);
      continue;
    }
      //If we are here we didn't get the right entity.  Just get
      //the body associated with this topology entity.
    Body *b = topo_ent->body();
    bad_ents.append(b);
  }
  return;
}

///
///  Find the irregular vertex valences.
///  Find things like vertices with valences greater than 4.
///  Assume for now that the volumes are not merged..
///
void GeomMeasureTool::find_irregular_valence( DLIList <RefVolume*> &ref_volumes,
                                              DLIList <RefVertex*> &irregular_vertices)
{
  RefVolume *ref_vol;
  int ii;
  for ( ii = 0; ii < ref_volumes.size(); ii++ )
  {
    ref_vol = ref_volumes.get_and_step();
    find_irregular_valence(ref_vol, irregular_vertices);
  }
}
///
///  Find the irregular vertex valences.
///  Find things like vertices with valences greater than 4.
///  Assume for now that the volumes are not merged..
///
void GeomMeasureTool::find_irregular_valence( RefVolume *ref_volume,
                                              DLIList <RefVertex*> &irregular_vertices)
{
  RefVertex *ref_vert;
  DLIList <RefVertex*> ref_vertices;
  ref_volume->ref_vertices(ref_vertices);
  int ii;
  for ( ii = ref_vertices.size(); ii > 0; ii-- )
  {
    ref_vert = ref_vertices.get_and_step();
    if ( ref_vert->num_ref_edges() > 4 )
      irregular_vertices.append(ref_vert);
  }
}

///
///  Find fillets and rounds.
///
#define FACE_BLEND      1
#define VERTEX_BLEND    2
#define BLEND_PROCESSED 3

void GeomMeasureTool::find_blends( RefVolume *ref_volume,
                                   DLIList <RefFace*> &blend_faces,
                                   DLIList <DLIList<RefFace*>*> &blend_groups )
{
    //Assume for now that blends have cartesian angles between
    //all the attached faces and itself.
    //Also a blend cannot be planar.  And directly accross from
    //one blend edge, there is usually another blended edge but
    //the angle between the two normals must be orthogonal. In other
    //words their must be some sort of transition.
  
    //mark all the faces as 0.  The mark is used here to find the
    // blend groups.  The function is_vertex_blend() marks faces
    // as being a vertex blend.  The group of faces is then traversed
    // until the adjacent surface (across the cross curve) is either
    // a vertex blend or not a blend.
  DLIList <RefFace*> ref_faces;
  DLIList <RefFace*> vertex_blends, face_blends;
  ref_volume->ref_faces(ref_faces);
  int ii, jj;
  for ( ii =0; ii < ref_faces.size(); ii++ )
    ref_faces.get_and_step()->marked(0);

    //First go through each face and each edge on each face.
  DLIList <RefEdge*> ref_edges;
  RefFace *ref_face;
  RefEdge *ref_edge, *other_edge;
  int num_vertex_blends = 0;
  int num_face_blends = 0;
  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
    ref_face = ref_faces.get_and_step();
      //Test the face to see if it is a face or vertex blend.
    other_edge = NULL;
    ref_edge = NULL;
    if ( is_face_blend(ref_face, ref_volume,
                       ref_edge, other_edge ) )
    {
      face_blends.append(ref_face);
      ref_face->marked(1);
      num_face_blends++;
    }
    else if ( is_vertex_blend(ref_face, ref_volume) )
    {
      vertex_blends.append(ref_face);
      ref_face->marked(2);
      num_vertex_blends++;  // keep track of the number of vertex blends
    }
  }
  if ( face_blends.size() + vertex_blends.size() == 0 )
  {
    return;
  }
    //Find out how many different groups of surfaces there
    //are that share curves.
  DLIList <RefFace*> *blend_group = NULL;
  DLIList <RefFace*> stack;
  RefFace *start_face = NULL, *other_face;
  
  // continue while there are blends to process
  while ( vertex_blends.size() + face_blends.size() > 0 || 
          start_face != NULL)
  {
    // if we don't have a start_face, get a new one.
    if (!start_face) 
    {
      // this is the start of a new group
      blend_group = new DLIList <RefFace*>;
      blend_groups.append(blend_group);

      // always prefer to start at a vertex blend if one exists
      if (vertex_blends.size() > 0 )
      {
        start_face = vertex_blends.pop();
      }
      else 
      {
        start_face = face_blends.pop();
      }
    }

    blend_group->append(start_face);  // add ref_face to group
    blend_faces.append(start_face);   // add the ref_face to the returned list
    start_face->marked(BLEND_PROCESSED);

    ref_edges.clean_out();
    start_face->ref_edges(ref_edges);
    for ( jj = 0; jj < ref_edges.size(); jj++ )
    {
      ref_edge = ref_edges.get_and_step();
      other_face = ref_edge->other_face(start_face, ref_volume);
      if (other_face == NULL)
      {
        start_face = NULL;
        break;
      }

      // if this blend has been processed and isn't in the current group
      if (other_face->marked() == BLEND_PROCESSED && 
          !blend_group->is_in_list(other_face) )
      {
        start_face = NULL;
        break;  // reached an end of this loop
      }
      else if (other_face->marked() == VERTEX_BLEND)
      {
        blend_group->append(other_face);  // add the ref_face to the group
        blend_faces.append(other_face);   // add the ref_face to the returned list
        vertex_blends.remove(other_face);
        other_face->marked(BLEND_PROCESSED);
        start_face = NULL;
        break;  // a vertex blend is also end of the line
      }
      else if (other_face->marked() == FACE_BLEND)
      {
        start_face = other_face;
        face_blends.remove(other_face);
        break;  // continue using this face as the new start face
      }
    }
    // if we traversed through all the edges of this blend without
    // finding another blend attached, this is the end of the chain 
    // and we need to find another starting place.
    if ( jj >= ref_edges.size() ) 
    {
      start_face = NULL;
    }
    
  }

    //cleanup marks.
  for ( ii =0; ii < ref_faces.size(); ii++ )
    ref_faces.get_and_step()->marked(0);
  for ( ii =0; ii < blend_faces.size(); ii++ )
    blend_faces.get_and_step()->marked(0);
    //Okay we should have it now.

    //REMEMBER TO DELETE the dllists in blend_faces.
  return;
}

// struct type object private to the next function
class NextBlend
{
  public:
  RefEdge* ref_edge;
  RefFace* ref_face;
};

void GeomMeasureTool::find_blends_from_edge( RefVolume* ref_volume, 
                                                    RefFace *start_face, 
                                                    RefEdge* start_edge,
                                                    std::vector <RefFace*> &blend_faces)
{
  // initialize the mark on all surfaces in the volume
  DLIList <RefFace*> ref_faces;
  RefEdge *next_edge, *other_edge;
  ref_volume->ref_faces(ref_faces);
  int ii;
  for ( ii =0; ii < ref_faces.size(); ii++ )
    ref_faces.get_and_step()->marked(0);

  std::stack<NextBlend> blend_stack;

  NextBlend blend;
  blend.ref_edge = start_edge;
  blend.ref_face = start_face;
  blend_faces.push_back(start_face);

  blend_stack.push(blend);

  while (blend_stack.size() > 0)
  {
    // this is an oddity with std::stack.  Get the top of the stack
    // and then you have to remove it from the stack with pop
    NextBlend next_blend = blend_stack.top();
    blend_stack.pop();
    RefEdge* ref_edge = next_blend.ref_edge;
    RefFace* ref_face = next_blend.ref_face;

    RefFace* next_face;
    next_face = ref_edge->other_face(ref_face, ref_volume);

    // the the next face exists and it's a face blend, save the current blend,
    // create a new blend object and add it to the stack
    if ( next_face && is_face_blend(next_face, ref_volume, next_edge, other_edge ) )
    {
      // if the next face is already processed, the chain loops back on itself.
      if (next_face->marked() == BLEND_PROCESSED)
      {
        for ( ii =0; ii < ref_faces.size(); ii++ )
          ref_faces.get_and_step()->marked(0);
        return;
      }

      blend_faces.push_back(next_face);
      next_face->marked(BLEND_PROCESSED);

      // the is_face_blend function assumes (poorly) rectangular surfaces
      // without any holes.  It returns the spring curves and we want
      // the cross curves. So get all four edges and remove the two
      // spring curves returned by is_face_blend
      DLIList <RefEdge*> ref_edges;
      next_face->ref_edges(ref_edges);
      ref_edges.remove(next_edge);
      ref_edges.remove(other_edge);

      if (ref_edges.get() != ref_edge)
      {
        next_blend.ref_edge = ref_edges.get();
      }
      else
      {
        next_blend.ref_edge = ref_edges.next(1);
      }
      next_blend.ref_face = next_face;
      blend_stack.push(next_blend);
    }
    else if ( next_face && is_vertex_blend(next_face, ref_volume) )
    {
      // we will stop the chain at a vertex blend
      blend_faces.push_back(next_face);
      next_face->marked(BLEND_PROCESSED);
    }
  }

  // clean up the marks
  std::vector<RefFace*>::iterator iter;
  for ( iter = blend_faces.begin(); iter != blend_faces.end(); iter++)
    (*iter)->marked(0);
}

// given a starting blend surface find a chain of blends from
// that surface.  
//
// Note that this function intentionally does _not_
// clear the blend_face list so that additional chains can be added.
CubitStatus GeomMeasureTool::find_blend_chains( RefFace *start_face,
                                std::vector<std::vector< RefFace*> > &blend_chains)
{

  if (start_face == NULL)
  {
    return CUBIT_FAILURE;
  }

  std::vector <RefFace*> blend_faces;

  // get the owning volume of this blend
  DLIList<RefEntity*> entity_list;
  RefVolume* ref_volume;
  int ii;

  start_face->get_parent_ref_entities(entity_list);

  // this indicates merged enitites and potential problems
  if (entity_list.size() > 1)
  {
    return CUBIT_FAILURE;
  }

  // make sure we're at the beginning of the list and get the first
  // and only item on the list and cast it to a RefVolume
  entity_list.reset();
  ref_volume = CAST_TO(entity_list.get(), RefVolume);
 
  if (!ref_volume)
  {
    return CUBIT_FAILURE;
  }

  // initialize the mark on all surfaces in the volume
  DLIList <RefFace*> ref_faces;
  ref_volume->ref_faces(ref_faces);

  RefEdge *spring_curve1, *spring_curve2;
  if ( is_face_blend(start_face, ref_volume, spring_curve1, spring_curve2 ) )
  {
    // the is_face_blend function assumes (poorly) rectangular surfaces
    // without any holes.  It returns the spring curves and we want
    // the cross curves. So get all four edges and remove the two
    // spring curves returned by is_face_blend
    DLIList <RefEdge*> ref_edges;
    start_face->ref_edges(ref_edges);
    ref_edges.remove(spring_curve1);
    ref_edges.remove(spring_curve2);

    // there is a special case where the blend is a periodic surface
    // meaning that there _are_ no cross curves
    if ( ref_edges.size() == 0 )
    {
      blend_faces.push_back(start_face);
    }
    else
    {
      // now find additional blends extending from either side of the blend
      blend_faces.clear();
      find_blends_from_edge( ref_volume, start_face, ref_edges.get(), blend_faces);
      find_blends_from_edge( ref_volume, start_face, ref_edges.next(1), blend_faces);

      // make sure that we have a unique list (the start surface is probably here twice)
      std::vector<RefFace*>::iterator new_end;
      std::sort( blend_faces.begin(), blend_faces.end() );
      new_end = std::unique( blend_faces.begin(), blend_faces.end() );
      blend_faces.erase(new_end, blend_faces.end());
    }

    blend_chains.push_back(blend_faces);
  }
  else if ( is_vertex_blend(start_face, ref_volume) )
  {
    DLIList<RefEdge*> ref_edges;
    start_face->ref_edges(ref_edges);

    for (ii = 0; ii < ref_edges.size(); ii++)
    {
      RefEdge* start_edge = ref_edges.get_and_step();
      blend_faces.clear();
      find_blends_from_edge( ref_volume, start_face, start_edge, blend_faces);

      blend_chains.push_back(blend_faces);
    }
  }
  else
  {
    // the given face is not a blend
    return CUBIT_FAILURE;
  }

  return CUBIT_SUCCESS;
}

//--------------------------------------------------------------------
//Function: Public, is_face_blend
//Description: Determines if a face is a blend surface, returns the
//   ref_edge on one side of the blend and other_edge on the opposite side.
//   For this type of blend, only ref_edge must be tangentially meeting
//   with another surface.  Other_edge must be oriented orthogonally to
//   it and may or may not blend with another surface.  This assumes
//   a rectangular blend surface, without holes.
//---------------------------------------------------------------------
CubitBoolean GeomMeasureTool::is_face_blend(RefFace *ref_face,
                                            RefVolume *ref_volume,
                                            RefEdge *&ref_edge,
                                            RefEdge *&other_edge)
{
    //first we know that blend surfaces are not planar.
    //so remove these first.
    //Also, don't look at faces with more than 2 loops.
  if ( ref_face->geometry_type() == PLANE_SURFACE_TYPE ||
       ref_face->num_loops() > 2 )
    return CUBIT_FALSE;
  
  CubitBoolean is_cartesian;
  DLIList<RefEdge*> ref_edges;
  ref_edge = NULL;
  other_edge = NULL;
  RefFace *other_face;
  int jj;
  double angle;
  ref_face->ref_edges(ref_edges);
  for ( jj = ref_edges.size(); jj > 0; jj-- )
  {
    ref_edge = ref_edges.get_and_step();

    //Weed-out case where one edge is shared between more 
    //than 2 surfaces of the same volume
    DLIList<RefFace*> tmp_faces;
    ref_edge->ref_faces( tmp_faces );

    if( tmp_faces.size() > 2 )
    {
      int kk;
      for(kk=tmp_faces.size(); kk--;)
      {
        if( !tmp_faces.get()->is_child( ref_volume ) )
          tmp_faces.change_to(NULL);
        tmp_faces.step();
      }
      tmp_faces.remove_all_with_value( NULL );
      if( tmp_faces.size() > 2 )
        //this isn't the type of surface we are looking for...
        continue;
    }

    other_face = ref_edge->other_face(ref_face, ref_volume);
    if ( other_face == NULL )
    {
        //this isn't the type of surface we are looking for...
      break;
    }
    angle = GeometryQueryTool::instance()->surface_angle(ref_face,
                                                         other_face,
                                                         ref_edge,
                                                         ref_volume);
    angle *= 180.0/CUBIT_PI;
    is_cartesian = CUBIT_TRUE;
    if ( angle <= GEOM_SIDE_LOWER ||
         angle >= GEOM_SIDE_UPPER )
      is_cartesian = CUBIT_FALSE;
      //Okay, we have one major criteria achieved, this edge is a cartesian meet.

    if ( !is_cartesian )
      continue;
      //Now we need to check the radius of curvatures between these
      //two surfaces. I'm not totally sure about this but I think we
      //don't want the same radius of curvature.
    double k1_s1, k2_s1, k1_s2, k2_s2;
    CubitVector mid_point;
    ref_edge->mid_point(mid_point);
    ref_face->get_principal_curvatures( mid_point, k1_s1, k2_s1, ref_volume);
    other_face->get_principal_curvatures( mid_point, k1_s2, k2_s2, ref_volume);
    if (( is_equal(k1_s1, k1_s2) || is_equal(k1_s1, k2_s2) ) &&
        ( is_equal(k2_s1, k1_s2) || is_equal(k2_s1, k2_s2) ) )
        //try a different edge.
      continue;

      //Okay, this looks pretty good.
      //Now try and find an edge that is on the "opposite" side of
      // this surface. Sort of assume this is like a mapable surface.
      //check the oposite side.
    other_edge = NULL;
    CubitVector opp_point;
    if ( !find_opposite_edge( ref_edge, ref_face, other_edge, opp_point) ||
         other_edge == NULL )
        //if we can't find an opposite edge, we need to not
        //continue checking this surface.
      return CUBIT_FALSE;
    
      //Okay, we have the point opposite.  If this is a blend surface
      //the normal of this point should be pointing 90 degrees from
      //us.
    CubitVector normal_1 = ref_face->normal_at(mid_point, ref_volume);
    CubitVector normal_2 = ref_face->normal_at(opp_point, ref_volume);
    normal_1.normalize();
    normal_2.normalize();
    double dot_product = normal_1 % normal_2;
    if ( dot_product >= .5 || dot_product <= -.5  )
        //try a different edge.
      continue;
    CubitVector tangent_1, tangent_2;
    ref_edge->tangent(mid_point, tangent_1, ref_face);
    other_edge->tangent(opp_point, tangent_2, ref_face);
    tangent_1.normalize();      
    tangent_2.normalize();      
    dot_product = tangent_1 % tangent_2;
      //basically these edges should be somewhat parallel at
      //this point.
    if ( dot_product < .5 && dot_product > -.5 )
      continue;
//This was called but never checked, not sure why.
//      //check the curvature opposite.
//    double k1_s3, k2_s3;
//    ref_face->get_principal_curvatures( opp_point, k1_s3, k2_s3, ref_volume);
      //we are done with this face. It is a blend face.
    return CUBIT_TRUE;
  }
  return CUBIT_FALSE;
}

//--------------------------------------------------------------------
//Function: Public, is_vertex_blend
//Description: Determines if a face is a vertex blend surface.
//   For this type of blend, all ref_edges must be meet tangentially
//   with another surface.  This assumes blend surface with no holes. 
//---------------------------------------------------------------------
CubitBoolean GeomMeasureTool::is_vertex_blend(RefFace *ref_face,
                                              RefVolume* ref_volume)
{
    //first we know that blend surfaces are not planar.
    //so remove these first.
    //Also, don't look at faces with more than 2 loops.
  if ( ref_face->geometry_type() == PLANE_SURFACE_TYPE ||
       ref_face->num_loops() > 2 )
    return CUBIT_FALSE;
  
  CubitBoolean is_cartesian;
  DLIList<RefEdge*> ref_edges;
  RefFace *other_face;
  RefEdge *ref_edge = NULL;
  int jj;
  double angle;
  ref_face->ref_edges(ref_edges);
  for ( jj = ref_edges.size(); jj > 0; jj-- )
  {
    ref_edge = ref_edges.get_and_step();

    //Weed-out case where one edge is shared between more 
    //than 2 surfaces of the same volume
    DLIList<RefFace*> tmp_faces;
    ref_edge->ref_faces( tmp_faces );

    if( tmp_faces.size() > 2 )
    {
      int kk;
      for(kk=tmp_faces.size(); kk--;)
      {
        if( !tmp_faces.get()->is_child( ref_volume ) )
          tmp_faces.change_to(NULL);
        tmp_faces.step();
      }
      tmp_faces.remove_all_with_value( NULL );
      if( tmp_faces.size() > 2 )
        //this isn't the type of surface we are looking for...
        continue;
    }

    other_face = ref_edge->other_face(ref_face, ref_volume);
    if ( other_face == NULL )
    {
        //this isn't the type of surface we are looking for...
      break;
    }
    angle = GeometryQueryTool::instance()->surface_angle(ref_face,
                                                         other_face,
                                                         ref_edge,
                                                         ref_volume);
    angle *= 180.0/CUBIT_PI;
    is_cartesian = CUBIT_TRUE;
    if ( angle <= GEOM_SIDE_LOWER ||
         angle >= GEOM_SIDE_UPPER )
      is_cartesian = CUBIT_FALSE;
      //Okay, we have one major criteria achieved, this edge is a cartesian meet.

    if ( !is_cartesian )
      return CUBIT_FALSE;
      //Now we need to check the radius of curvatures between these
      //two surfaces. I'm not totally sure about this but I think we
      // want the same radius of curvature.
    double k1_s1, k2_s1, k1_s2, k2_s2;
    CubitVector mid_point;
    ref_edge->mid_point(mid_point);
    ref_face->get_principal_curvatures( mid_point, k1_s1, k2_s1, ref_volume);
    other_face->get_principal_curvatures( mid_point, k1_s2, k2_s2, ref_volume);
    if (( is_equal(k1_s1, k1_s2) || is_equal(k1_s1, k2_s2) ) &&
        ( is_equal(k2_s1, k1_s2) || is_equal(k2_s1, k2_s2) ) )
        //try a different edge.
        continue;
    else
      return CUBIT_FALSE;
  }

  // if all edges are tangent and share curvatures then it must be a 
  // vertex blend.
  return CUBIT_TRUE;
}

CubitBoolean GeomMeasureTool::find_opposite_edge( RefEdge* ref_edge,
                                                  RefFace* ref_face,
                                                  RefEdge *&other_edge,
                                                  CubitVector &closest_point)
{
    //Here we want to find the edge that is "opposite" to ref_edge.
    //Opposite is defined by assuming the surface is a square or cylinder and
    //finding the edge on the other side, so that there is a point closest
    //to the mid point on the ref_edge.
  //double need_to_turn = CUBIT_PI;
  int ii;
  DLIList <Loop*> loops;
  DLIList <CoEdge*> co_edges;
  Loop *curr_loop;
  CoEdge *co_edge, *this_co_edge;
  ref_edge->get_co_edges(co_edges, ref_face);
  if ( co_edges.size() != 1 )
    return CUBIT_FALSE;
  this_co_edge = co_edges.get();
  co_edges.clean_out();
  ref_face->loops(loops);
  if ( loops.size() > 2 )
    return CUBIT_FALSE;
  CubitBoolean found_loop = CUBIT_FALSE;
    //find the loop where this_co_edge is on.
  for(ii = loops.size(); ii > 0; ii-- )
  {
    curr_loop = loops.get_and_step();
    co_edges.clean_out();
    curr_loop->ordered_co_edges(co_edges);
    if ( co_edges.move_to(this_co_edge) )
    {
      found_loop = CUBIT_TRUE;
      break;
    }
  }
  if ( !found_loop )
    return CUBIT_FALSE;
  CoEdge *opp_co_edge = NULL;
  if ( loops.size() == 1 )
  {
      //This is the normal case, I hope.
      //this co_edge, go around the interior until
      //we turn twice.  If there are angles greater than CUBIT_PI, this isn't
      //a traditional blend.  Everything should be convex.
    if ( co_edges.size() == 4 )
    {
        //Just get the one opposite...
      opp_co_edge = co_edges.next(2);
    }
    else
    {
      //double total_angle = 0.0;
      int turn_counter = 0;
      CoEdge *next_co_edge = NULL;
      for ( ii = co_edges.size(); ii > 0; ii-- )
      {
        co_edge = co_edges.get_and_step();
        next_co_edge = co_edges.get();
        double angle = GeometryQueryTool::instance()->geometric_angle(co_edge, next_co_edge);
        angle *= 180.0/CUBIT_PI;
          //we don't deal with small or concave angles.
        if ( angle < 10.0 )
          return CUBIT_FALSE;
        else if ( angle > 185.0 )
          return CUBIT_FALSE;
        else if ( angle < 140.0 )
          turn_counter++;
        if ( turn_counter == 2 )
        {
          opp_co_edge = next_co_edge;
          break;
        }
      }
    }
  }
  else
  {
      //Just get the other loop and get one coedge in it.
    co_edges.clean_out();
    curr_loop = loops.get();
    curr_loop->ordered_co_edges(co_edges);
    opp_co_edge = co_edges.get();
  }
  if ( opp_co_edge == NULL )
    return CUBIT_FALSE;
    //This is just here to test correctness to this point, it can be taken out
    //if need be.
  if ( !co_edges.move_to(opp_co_edge) )
  {
    PRINT_ERROR("logic messed-up in find_opposite edge\n");
    assert(0);
    return CUBIT_FALSE;
  }
    //okay, we now have the co_edges in the correct position and
    //we have a canidate co_edge.
    //Search from the co_edge with the point that has closest point
    //to the mid point of this_co_edge.
  other_edge = NULL;
  CubitVector mid_point;
  ref_edge->mid_point(mid_point);
  CubitVector tmp_closest_point;
  double min_dist_sq = CUBIT_DBL_MAX;
  double dist_sq;
  RefEdge *test_ref_edge;
  CoEdge *prev_co_edge;
  CubitBoolean first = CUBIT_TRUE;
  for ( ii = co_edges.size(); ii > 0; ii-- )
  {
    co_edge = co_edges.get_and_step();
    prev_co_edge = co_edges.prev(2);
    test_ref_edge = co_edge->get_ref_edge_ptr();
      //We don't want edges that are connected to ref edge.
      //If we have them, we have gone to far around the surface.
    if ( test_ref_edge->common_ref_vertex(ref_edge) )
      break;
    if ( !first )
    {
        //break out of the for loop if we turn again. We just want the closest
        //point opposite.
      double angle = GeometryQueryTool::instance()->
        geometric_angle(prev_co_edge, co_edge);
      angle *= 180.0/CUBIT_PI;
      if ( angle > 195.0 || angle < 145.0 )
        break;
    }
    else
      first = CUBIT_FALSE;
      //now find the closest point to the mid point of ref_edge.
    test_ref_edge->closest_point_trimmed( mid_point,
                                          tmp_closest_point);
    dist_sq = (mid_point - closest_point).length_squared();
      //find and keep the closest one.
    if ( dist_sq < min_dist_sq )
    {
      other_edge = test_ref_edge;
      min_dist_sq = dist_sq;
      closest_point = tmp_closest_point;
    }
  }
  if ( other_edge == NULL )
    return CUBIT_FALSE;

  return CUBIT_TRUE;
}
CubitBoolean GeomMeasureTool::is_equal(double v1, double v2)
{
  double smallv = v1-v2;
  if ( smallv < 0.0 )
  {
    if ( smallv >= -CUBIT_RESABS )
      return CUBIT_TRUE;
    else
      return CUBIT_FALSE;
  }
  else
  {
    if ( smallv <= CUBIT_RESABS )
      return CUBIT_TRUE;
    else
      return CUBIT_FALSE;
  }
}
CubitStatus GeomMeasureTool::get_centroid( RefFace *ref_face, CubitVector &centroid, double &tot_area )
{
  GMem g_mem;
  unsigned short norm_tol = 5;
  double dist_tol = -1.0;

  ref_face->get_geometry_query_engine()->
      get_graphics(ref_face->get_surface_ptr(), &g_mem, norm_tol, dist_tol );

  if(g_mem.fListCount < 1)
  {
      // Decrease tolerance and try again (we can get this for small features)
      norm_tol /= 2;
      ref_face->get_geometry_query_engine()->
          get_graphics(ref_face->get_surface_ptr(), &g_mem, norm_tol, dist_tol );
  }

  if(g_mem.fListCount < 1)
  {
      // Lets give up 
      PRINT_ERROR( "Unable to find the center of a surface\n" );
      return CUBIT_FAILURE;
  }

  // Initialize
  tot_area = 0.0;
  centroid.set( 0.0, 0.0, 0.0 );

  // Loop through the triangles
  double tri_area;
  GPoint p[3];
  GPoint* plist = g_mem.point_list();
  int* facet_list = g_mem.facet_list();
  int c = 0;
  for( ; c < g_mem.fListCount; )
  {
      p[0] = plist[facet_list[++c]];
      p[2] = plist[facet_list[++c]];
      p[1] = plist[facet_list[++c]]; 
      c++;

      // Get centroid
      CubitVector p1( p[0].x, p[0].y, p[0].z );
      CubitVector p2( p[2].x, p[2].y, p[2].z );
      CubitVector p3( p[1].x, p[1].y, p[1].z );

      CubitVector center = (p1 + p2 + p3)/3.0;

      //Calculate area
      CubitVector vec1 = (p2 - p1);
	  CubitVector vec2 = (p3 - p1);

	  CubitVector cross = vec1 * vec2;
		
	  tri_area = 0.5* sqrt(cross.x() * cross.x() + cross.y() * cross.y() + cross.z() * cross.z());
		
		
	  centroid += (tri_area * center);

	  tot_area += tri_area;
        
    }
  if( tot_area == 0 )
    return CUBIT_FAILURE;

  centroid /= tot_area;
  return CUBIT_SUCCESS;
}    

CubitStatus
GeomMeasureTool::center( DLIList<RefFace*> ref_faces )
{
  int ii,id;
  double surf_area;
  double tot_area = 0.0;
  CubitVector surf_centroid;
  CubitVector centroid;
  centroid.set( 0.0, 0.0, 0.0 );

  for ( ii = ref_faces.size(); ii > 0; ii-- )
  {
	RefFace *ref_face = ref_faces.get_and_step();
	if( GeomMeasureTool::get_centroid( ref_face, surf_centroid, surf_area) != CUBIT_SUCCESS )
	  return CUBIT_FAILURE;
    centroid += (surf_area * surf_centroid);
	tot_area += surf_area;
	id = ref_face->id();
  }
  
  centroid /= tot_area;

  if( ref_faces.size() > 1 )
	PRINT_INFO("Centroid of Composite surface located at: ( %f , %f , %f )\n\n",
                 centroid.x(), centroid.y(), centroid.z());
  else
	PRINT_INFO("Centroid of Surface %i located at: ( %f , %f , %f )\n\n",
                  id, centroid.x(), centroid.y(), centroid.z());
  return CUBIT_SUCCESS;
}

CubitStatus GeomMeasureTool::find_near_coincident_vertices( 
                            DLIList<RefVolume*> &ref_volumes,
                            DLIList<RefVertex*> &ref_vertices_out,
                            DLIList<double> &distances,
                            double low_tol,
                            double high_tol,
                            bool filter_same_volume_cases)
{
  DLIList<RefVertex*> tmp_vert_list;
  DLIList<RefVertex*> ref_verts; 
  int i,j;
  for( i=ref_volumes.size(); i--; )
  {
    RefVolume *tmp_vol = ref_volumes.get_and_step();
    tmp_vert_list.clean_out();
    tmp_vol->ref_vertices( tmp_vert_list );
    ref_verts += tmp_vert_list;
  }

  //put all the vertices in a tree 
  AbstractTree <RefVertex*> *a_tree = new RTree<RefVertex*>( high_tol ); 
  for (i=ref_verts.size(); i--;)
    a_tree->add(ref_verts.get_and_step());

  std::multimap<double, dist_vert_struct> distance_vertex_map; 

  //for each vertex
  for (i=ref_verts.size(); i--;)
  {
    RefVertex *tmp_vert = ref_verts.get_and_step();
    RefVolume *v1 = tmp_vert->ref_volume();
    CubitVector vert_xyz = tmp_vert->coordinates();

    //get all close vertices
    DLIList<RefVertex*> close_verts;
    a_tree->find(tmp_vert->bounding_box(), close_verts);

    //if any vertex is between low_tol and high_tol
    //add it to the list
    DLIList<RefVertex*> near_coincident_verts;
    for( j=close_verts.size(); j--; )
    {
      RefVertex *close_vert = close_verts.get_and_step();
      if( close_vert == tmp_vert ) 
        continue;

      RefVolume *v2 = close_vert->ref_volume();
      bool check_distance = true;
      if(filter_same_volume_cases && v1 && v2 && v1 == v2)
        check_distance = false;
      if(check_distance)
      {
        double distance = vert_xyz.distance_between( close_vert->coordinates() );
        if( distance >= low_tol && distance <= high_tol )
        {
          dist_vert_struct tmp_struct;
          tmp_struct.dist = distance;
          tmp_struct.v1 = tmp_vert;
          tmp_struct.v2 = close_vert;
          distance_vertex_map.insert( std::multimap<double, dist_vert_struct>::
                                      value_type( distance, tmp_struct ));
        }
      }
    }

    a_tree->remove( tmp_vert );
  }

  std::multimap<double, dist_vert_struct>::reverse_iterator iter;
  
  iter = distance_vertex_map.rbegin();
  for(; iter!=distance_vertex_map.rend(); iter++ )
  {
    distances.append( (*iter).second.dist );
    ref_vertices_out.append( (*iter).second.v1 );
    ref_vertices_out.append( (*iter).second.v2 );
  }

  delete a_tree;

  return CUBIT_SUCCESS;
}

// This function is similar to find_near_coincident_vertices except for the
// fact that it will only find the closest vertex in a given volume for
// a vertex in another volume to be close to.  This tries to exclude the case where
// you would attempt to merge one vertex from one volume to two different
// vertices in another volume.
CubitStatus GeomMeasureTool::find_near_coincident_vertices_unique( 
                            DLIList<RefVolume*> &ref_volumes,
                            double high_tol,
                            std::map <RefVertex*, DLIList<dist_vert_struct*>*> &vert_dist_map)
{
  DLIList<RefVertex*> tmp_vert_list;
  DLIList<RefVertex*> ref_verts; 
  int i,j;
  for( i=ref_volumes.size(); i--; )
  {
    RefVolume *tmp_vol = ref_volumes.get_and_step();
    tmp_vert_list.clean_out();
    tmp_vol->ref_vertices( tmp_vert_list );
    ref_verts += tmp_vert_list;
  }

  //put all the vertices in a tree 
  AbstractTree <RefVertex*> *a_tree = new RTree<RefVertex*>( high_tol ); 
  for (i=ref_verts.size(); i--;)
    a_tree->add(ref_verts.get_and_step());

  //for each vertex
  for (i=ref_verts.size(); i--;)
  {
    RefVertex *tmp_vert = ref_verts.get_and_step();
    RefVolume *vol1 = tmp_vert->ref_volume();
    CubitVector vert_xyz = tmp_vert->coordinates();

    //get all close vertices
    DLIList<RefVertex*> close_verts;
    a_tree->find(tmp_vert->bounding_box(), close_verts);

    //if any vertex is between low_tol and high_tol
    //add it to the list
    DLIList<dist_vert_struct*> *near_coincident_verts = NULL;
    for( j=close_verts.size(); j--; )
    {
      RefVertex *close_vert = close_verts.get_and_step();
      if( close_vert == tmp_vert ) 
        continue;

      RefVolume *vol2 = close_vert->ref_volume();
      if(vol1 && vol2 && vol1 != vol2)
      {
        if(!near_coincident_verts)
        {
          near_coincident_verts = new DLIList<dist_vert_struct*>;
          vert_dist_map[tmp_vert] = near_coincident_verts;
        }
        double distance = vert_xyz.distance_between( close_vert->coordinates() );
        int h;
        bool found_entry_with_same_vol = false;
        for(h=near_coincident_verts->size(); h>0 && !found_entry_with_same_vol; h--)
        {
          dist_vert_struct* vds = near_coincident_verts->get_and_step();
          if(vds->vol2 == vol2)
          {
            found_entry_with_same_vol = true;
            if(distance < vds->dist)
            {
              vds->dist = distance;
              vds->vol2 = vol2;
              vds->v2 = close_vert;
            }
          }
        }
        if(!found_entry_with_same_vol)
        {
          dist_vert_struct *new_vds = new dist_vert_struct;
          new_vds->dist = distance;
          new_vds->v2 = close_vert;
          new_vds->vol2 = vol2;
          near_coincident_verts->append(new_vds);
        }
      }
    }
    a_tree->remove( tmp_vert );
  }

  delete a_tree;

  return CUBIT_SUCCESS;
}

struct dist_vert_vert_struct
{
  double dist;
  RefVertex *vert1;
  RefVertex *vert2;
};

struct dist_vert_curve_struct
{
  double dist;
  RefVertex *vert;
  RefEdge *edge;
//  bool operator<( const dist_vert_curve_struct& b ) const
//  {
//    return this->dist < b.dist;
 // }
};

struct vert_curve_dist_sort
{
  bool operator()( const dist_vert_curve_struct& a, const dist_vert_curve_struct& b ) const
  {
    return a.dist < b.dist;
  }
};

struct vert_curve_dist_sort_ptr
{
  bool operator()( dist_vert_curve_struct *a, dist_vert_curve_struct *b ) const
  {
    return a->dist < b->dist;
  }
};

struct vert_vert_dist_sort_ptr
{
  bool operator()( dist_vert_vert_struct *a, dist_vert_vert_struct *b ) const
  {
    return a->dist < b->dist;
  }
};

CubitStatus GeomMeasureTool::find_closest_vertex_curve_pairs(
                                  DLIList<RefVolume*> &vol_list,
                                  int &num_to_return,
                                  DLIList<RefVertex*> &vert_list,
                                  DLIList<RefEdge*> &curve_list,
                                  DLIList<double> &distances)
{
  DLIList<RefFace*> surfs;

  int i, total_num_entries = 0;
  for( i=vol_list.size(); i--; )
  {
    RefVolume *tmp_vol = vol_list.get_and_step();
    tmp_vol->ref_faces( surfs );
  }

  std::set<dist_vert_curve_struct*,vert_curve_dist_sort_ptr> distance_vertex_curve_set; 

  for(i=surfs.size(); i>0; i--)
  {
    RefFace *surf = surfs.get_and_step();
    DLIList<RefVertex*> surf_verts;
    surf->ref_vertices(surf_verts);
    DLIList<RefEdge*> surf_curves;
    surf->ref_edges(surf_curves);

    int j;
    for(j=surf_verts.size(); j>0; j--)
    {
      RefVertex *tmp_vert = surf_verts.get_and_step();
      CubitVector vert_xyz = tmp_vert->coordinates();
      CubitVector closest_pt;
      int k;
      for(k=surf_curves.size(); k>0; k--)
      {
        RefEdge *cur_edge = surf_curves.get_and_step();
        if(cur_edge->start_vertex() != tmp_vert &&
          cur_edge->end_vertex() != tmp_vert)
        {
          cur_edge->closest_point_trimmed(vert_xyz, closest_pt);
          if(!closest_pt.about_equal(cur_edge->start_coordinates()) &&
             !closest_pt.about_equal(cur_edge->end_coordinates()))
          {
            double dist_sq = vert_xyz.distance_between_squared(closest_pt);
            dist_vert_curve_struct *tmp_struct = new dist_vert_curve_struct;
            tmp_struct->dist = dist_sq;
            tmp_struct->vert = tmp_vert;
            tmp_struct->edge = cur_edge; 
            distance_vertex_curve_set.insert( tmp_struct );
            total_num_entries++;
          }
        }
      }
    }
  }

  std::set<dist_vert_curve_struct*, vert_curve_dist_sort_ptr>::iterator iter, upper_iter; <--- Unused variable: upper_iter
  
  int local_num_to_return = num_to_return;
  if(num_to_return == -1)
  {
    local_num_to_return = total_num_entries;
  }
  int cntr = 0;
  iter = distance_vertex_curve_set.begin();
  for(; iter!=distance_vertex_curve_set.end() && cntr < local_num_to_return; iter++ )
  {
    distances.append( sqrt((*iter)->dist) );
    vert_list.append( (*iter)->vert );
    curve_list.append( (*iter)->edge );
    cntr++;
  }

  iter = distance_vertex_curve_set.begin();
  for(; iter!=distance_vertex_curve_set.end(); iter++ )
  {
    delete *iter;
  }

  return CUBIT_SUCCESS;
}

CubitStatus GeomMeasureTool::find_closest_vertex_vertex_pairs(
                                  DLIList<RefVolume*> &vol_list,
                                  int &num_to_return,
                                  DLIList<RefVertex*> &vert_list1,
                                  DLIList<RefVertex*> &vert_list2,
                                  DLIList<double> &distances)
{
  std::set<dist_vert_vert_struct*,vert_vert_dist_sort_ptr> distance_vertex_vertex_set; 

  int i, total_num_entries = 0;
  for( i=vol_list.size(); i--; )
  {
    RefVolume *tmp_vol = vol_list.get_and_step();
    DLIList<RefVertex*> vol_verts;
    tmp_vol->ref_vertices(vol_verts);
    while(vol_verts.size() > 1)
    {
      RefVertex *vert1 = vol_verts.pop();
      CubitVector vert1_xyz = vert1->coordinates();
      int j;
      for(j=vol_verts.size(); j>0; j--)
      {
        RefVertex *vert2 = vol_verts.get_and_step();
        double dist_sq = vert2->coordinates().distance_between_squared(vert1_xyz);
        dist_vert_vert_struct *tmp_struct = new dist_vert_vert_struct;
        tmp_struct->dist = dist_sq;
        tmp_struct->vert1 = vert1;
        tmp_struct->vert2 = vert2; 
        distance_vertex_vertex_set.insert( tmp_struct );
        total_num_entries++;
      }
    }
  }

  std::set<dist_vert_vert_struct*, vert_vert_dist_sort_ptr>::iterator iter, upper_iter; <--- Unused variable: upper_iter
  
  int local_num_to_return = num_to_return;
  if(num_to_return == -1)
    local_num_to_return = total_num_entries;
  int cntr = 0;
  iter = distance_vertex_vertex_set.begin();
  for(; iter!=distance_vertex_vertex_set.end() && cntr < local_num_to_return; iter++ )
  {
    distances.append( sqrt((*iter)->dist) );
    vert_list1.append( (*iter)->vert1 );
    vert_list2.append( (*iter)->vert2 );
    cntr++;
  }

  iter = distance_vertex_vertex_set.begin();
  for(; iter!=distance_vertex_vertex_set.end(); iter++ )
  {
    delete *iter;
  }

  return CUBIT_SUCCESS;
}

CubitStatus GeomMeasureTool::find_near_coincident_vertex_curve_pairs( 
                                DLIList<RefVolume*> &ref_vols,
                                DLIList<RefEdge*> &ref_edges,
                                DLIList<RefVertex*> &ref_verts,
                                DLIList<double> &distances,
                                double low_tol,
                                double high_tol,
                                bool filter_same_volume_cases)
{
  //get all the curves and vertices of volumes in list
  DLIList<RefVertex*> verts;
  DLIList<RefEdge*> curves;

  RTree<RefEdge*> a_tree(high_tol);

  int i,j;
  for( i=ref_vols.size(); i--; )
  {
    RefVolume *tmp_vol = ref_vols.get_and_step();
    tmp_vol->ref_vertices( verts );
    
    curves.clean_out();
    tmp_vol->ref_edges( curves );
    for( j=curves.size(); j--; )
    {
      RefEdge *tmp_edge = curves.get_and_step();
      a_tree.add( tmp_edge );
    }
  }

  ProgressTool *progress_ptr = NULL;
  int total_verts = verts.size();
  if (total_verts > 5)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Near Coincident Vertex-Curve Pairs", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }
    
  double curr_percent = 0.0;<--- Variable 'curr_percent' is assigned a value that is never used.
  int processed_verts = 0;
  std::multimap<double, dist_vert_curve_struct> distance_vertex_curve_map; 

  //for each vertex
  for( i=verts.size(); i--; )
  {
    processed_verts++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(processed_verts))/((double)(total_verts));
      progress_ptr->percent(curr_percent);
    }

    RefVertex *tmp_vert = verts.get_and_step();
    RefVolume *v1 = tmp_vert->ref_volume();
    CubitBox vertex_box ( tmp_vert->coordinates(), tmp_vert->coordinates() );
    DLIList<RefEdge*> close_curves;
    a_tree.find( vertex_box, close_curves );

    CubitVector vertex_xyz = tmp_vert->coordinates(); 

    for( j=close_curves.size(); j--; )
    {
      RefEdge *tmp_edge = close_curves.get_and_step();
      RefVolume *v2 = tmp_edge->ref_volume();

      bool check_distance = true;
      if(filter_same_volume_cases && v1 && v2 && v1 == v2)
        check_distance = false;
      if(check_distance)
      {
        CubitVector closest_location;
        tmp_edge->closest_point_trimmed( vertex_xyz, closest_location );
        double distance = closest_location.distance_between( vertex_xyz );

        if( distance >= low_tol && distance <= high_tol )
        {
          dist_vert_curve_struct tmp_struct;
          tmp_struct.dist = distance;
          tmp_struct.vert = tmp_vert;
          tmp_struct.edge = tmp_edge; 

          distance_vertex_curve_map.insert( std::multimap<double, dist_vert_curve_struct>::
                                            value_type( distance, tmp_struct ));
        }
      }
    }
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();

  //std::set<dist_vert_curve_struct, vert_curve_dist_sort>::iterator iter, upper_iter; 
  std::multimap<double, dist_vert_curve_struct>::reverse_iterator iter;
  
  iter = distance_vertex_curve_map.rbegin();
  for(; iter!=distance_vertex_curve_map.rend(); iter++ )
  {
    distances.append( (*iter).second.dist );
    ref_verts.append( (*iter).second.vert );
    ref_edges.append( (*iter).second.edge );
  }

  return CUBIT_SUCCESS;
}


struct dist_vert_surf_struct
{
  double dist;
  RefVertex *vert;
  RefFace *face;
};

struct vert_surf_dist_sort
{
  bool operator()( dist_vert_surf_struct a, dist_vert_surf_struct b ) const
  {
    if( a.dist < b.dist )
      return true;
    else if( a.dist > b.dist )
      return false;
    else 
    {
      if( a.vert < b.vert )
        return true;
      else if( a.vert > b.vert )
        return false;
      else if( a.face < b.face )
        return true;
      else if( a.face > b.face )
        return false;
    }
    return false;
  }
};


CubitStatus GeomMeasureTool::find_near_coincident_vertex_surface_pairs( 
                                DLIList<RefVolume*> &ref_vols,
                                DLIList<RefFace*> &ref_faces,
                                DLIList<RefVertex*> &ref_verts,
                                DLIList<double> &distances,
                                double low_tol,
                                double high_tol,
                                bool filter_same_volume_cases)
{
  //get all the curves and vertices of volumes in list
  DLIList<RefVertex*> verts;
  DLIList<RefFace*> faces;

  AbstractTree<RefFace*> *a_tree = new RTree<RefFace*>( high_tol );

  int i,j;
  for( i=ref_vols.size(); i--; )
  {
    RefVolume *tmp_vol = ref_vols.get_and_step();
    tmp_vol->ref_vertices( verts );
    
    faces.clean_out();
    tmp_vol->ref_faces( faces );
    // Populate the Surface AbstractTree
    for( j=faces.size(); j--; )
    {
      RefFace *tmp_face = faces.get_and_step();
      a_tree->add( tmp_face );
    }
  }

  ProgressTool *progress_ptr = NULL;
  int total_verts = verts.size();
  if (total_verts > 50)
  {
    progress_ptr = AppUtil::instance()->progress_tool();
    assert(progress_ptr != NULL);
    progress_ptr->start(0, 100, "Finding Near Coincident Vertex-Surface Pairs", 
      NULL, CUBIT_TRUE, CUBIT_TRUE);
  }
  double curr_percent = 0.0;
  int processed_verts = 0;

  std::multimap<double, dist_vert_surf_struct> distance_vertex_surface_map; 

  //for each vertex
  for( i=verts.size(); i--; )
  {
    processed_verts++;
    if ( progress_ptr != NULL )
    {
      curr_percent = ((double)(processed_verts))/((double)(total_verts));
      progress_ptr->percent(curr_percent);
    }

    RefVertex *tmp_vert = verts.get_and_step();
    RefVolume *v1 = tmp_vert->ref_volume();
    CubitBox vertex_box ( tmp_vert->coordinates(), tmp_vert->coordinates() );
    DLIList<RefFace*> close_faces;
    a_tree->find( vertex_box, close_faces);

    CubitVector vertex_xyz = tmp_vert->coordinates(); 

    for( j=close_faces.size(); j--; )
    {
      RefFace *tmp_face = close_faces.get_and_step();
      RefVolume *v2 = tmp_face->ref_volume();

      bool check = true;
      if(filter_same_volume_cases && v1 && v2 && v1 == v2)
        check = false;

      if(check)
      {
        DLIList<RefVertex*> tmp_verts;
        tmp_face->ref_vertices( tmp_verts );
        if( tmp_verts.is_in_list( tmp_vert ) ) 
          continue;

        CubitVector closest_location;
        tmp_face->find_closest_point_trimmed( vertex_xyz, closest_location );
        double distance = closest_location.distance_between( vertex_xyz );

        if( distance > low_tol && distance < high_tol )
        {
          dist_vert_surf_struct tmp_struct;
          tmp_struct.dist = distance;
          tmp_struct.vert = tmp_vert;
          tmp_struct.face = tmp_face; 
          distance_vertex_surface_map.insert( std::multimap<double, dist_vert_surf_struct>::
                                      value_type( distance, tmp_struct ));
        }
      }
    }
  }

  if ( progress_ptr != NULL )
    progress_ptr->end();

  std::multimap<double, dist_vert_surf_struct>::reverse_iterator iter;

  iter = distance_vertex_surface_map.rbegin();
  for(; iter!=distance_vertex_surface_map.rend(); iter++ )
  {
    distances.append( (*iter).second.dist );
    ref_verts.append( (*iter).second.vert );
    ref_faces.append( (*iter).second.face);
  }

  delete a_tree;

  return CUBIT_SUCCESS;
}