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
//- Class:       FacetDataUtil
//- Description: static library of general functions for querying and/or
//-              modifying facet entities
//- Owner:       Steve Owen
//- Checked by:
//- Version:

#include "FacetDataUtil.hpp"
#include "FacetEvalTool.hpp"
#include "CubitFacet.hpp"
#include "CubitFacetEdge.hpp"
#include "CubitPoint.hpp"
#include "CubitPointData.hpp"
#include "CubitFacetData.hpp"
#include "CubitFacetEdgeData.hpp"
#include "CubitQuadFacet.hpp"
#include "GeometryDefines.h"
#include "ChollaDebug.hpp"
#include "GfxDebug.hpp"
#include "KDDTree.hpp"

//===========================================================================
//Function Name: edges_by_count
//Description:   find edges that are shared by "count" number of facets
//Author: bwhanks
//Date: 2003
//===========================================================================
void FacetDataUtil::edges_by_count(DLIList<CubitFacet*> &facets,
                                   unsigned int count,
                                   DLIList<CubitFacetEdge*> &edges)
{
  int i;
  int j;

  // get a list of all edges from the facets
  DLIList<CubitFacetEdge*> edge_list;
  facets.reset();
  for (i=facets.size(); i>0; i--)
  {
    CubitFacet* p_facet = facets.get_and_step();
    for (j=0; j<3; j++)
    {
      edge_list.append(p_facet->edge(j));
    }
  }

  // reset edge marks
  edge_list.reset();
  for (i=edge_list.size(); i>0; i--)
    edge_list.get_and_step()->marked(0);

  // mark with the hit count
  edge_list.reset();
  for (i=edge_list.size(); i>0; i--)
  {
    CubitFacetEdge* p_edge = edge_list.get_and_step();
    p_edge->marked(p_edge->marked() + 1);
  }

  // create the output list of edges hit the number of times passed in
  edge_list.reset();
  for (i=edge_list.size(); i>0; i--)
  {
    CubitFacetEdge* p_edge = edge_list.get_and_step();
    if (static_cast<unsigned>(p_edge->marked()) == count)
      edges.append(p_edge);
  }

  // reset edge marks
  edge_list.reset();
  for (i=edge_list.size(); i>0; i--)
    edge_list.get_and_step()->marked(0);
}

//===========================================================================
//Function Name: ordered_point_edge_bdry
//Description:   get an ordered list of points and edges around the boundary
//               of a set of facets. The output "chain" consists of point -
//               edge - point - edge - ... around the boundary of the input
//               facets.  Having the ordered points as well as the edges
//               helps provides sense information for the bounding edges.
//Author: bwhanks
//Date: 2003
//===========================================================================
void FacetDataUtil::ordered_point_edge_bdry(DLIList<CubitFacet*> &facets,
                                            DLIList<FacetEntity*> &point_edge_chain)
{
  assert(point_edge_chain.size() == 0);
  DLIList<CubitFacetEdge*> unordered_edges;

  int use_count = 1; // get boundary edges (used only once by facets in the facet list)
  FacetDataUtil::edges_by_count(facets, use_count, unordered_edges);

//  Adds the start_edge to the list of boundary_edges and then attempts to find the
//  next edge by getting all of the edges belonging to the second point on this
//  edge and searching for an unmarked (-1) boundary edge.  Does this until it can't
//  find another edge.


  // mark all edges connected to points of boundary edges with -2
  // NOTE: using negative marks, since as boundary edges are added to the ordered list they
  // get marked with the order in which they are found

  // TODO - this seems very inefficient
  int i;
  int j;
  int k;
  DLIList<CubitFacetEdge*> pt_edge_list;
  CubitFacetEdge* cur_edge;
  unordered_edges.reset();
  for (i=unordered_edges.size(); i>0; i--)
  {
    cur_edge = unordered_edges.get_and_step();
    for (j=0; j<2; j++)
    {
      pt_edge_list.clean_out();
      cur_edge->point(j)->edges(pt_edge_list);
      pt_edge_list.reset();
      for (k=pt_edge_list.size(); k>0; k--)
        pt_edge_list.get_and_step()->marked(-2);
    }
  }

  // mark all boundary edges with -1
  unordered_edges.reset();
  for (i=unordered_edges.size(); i>0; i--)
    unordered_edges.get_and_step()->marked(-1);




  CubitPoint *edge_pt1, *edge_pt2, *pt2, *originalpt1;
  CubitFacetEdge *start_edge;
  CubitFacetEdge *this_edge;
  CubitBoolean keepgoing;

  int i_found_some = 0;
  unordered_edges.reset();
  start_edge = unordered_edges.get();

  // find the orientation of the first edge with respect to one of the facets
  // then order the edges accordingly
  facets.reset();
  int e_index;
  int start_edge_sense = 0;
  for (i=facets.size(); i>0; i--)
  {
    CubitFacet* p_facet = facets.get_and_step();
    if ( (e_index = p_facet->edge_index(start_edge)) >= 0 )
    {
      start_edge_sense = p_facet->edge_use(e_index);
      break;
    }
  }


  start_edge->marked(i_found_some);
  i_found_some += 1;
  if (1 == start_edge_sense)
  {
    originalpt1 = start_edge->point(0);
    point_edge_chain.append(originalpt1);
    point_edge_chain.append(start_edge);
    pt2 = start_edge->point(1);
  }
  else
  {
    assert(-1 == start_edge_sense);
    originalpt1 = start_edge->point(1);
    point_edge_chain.append(originalpt1);
    point_edge_chain.append(start_edge);
    pt2 = start_edge->point(0);
  }


//  Look for an edge having pt2 as a point and also being on the boundary.
  pt_edge_list.clean_out();
  pt2->edges(pt_edge_list);

  keepgoing = CUBIT_TRUE;
  pt_edge_list.reset();
  while ( keepgoing == CUBIT_TRUE ) {
    keepgoing = CUBIT_FALSE;
    for ( i = pt_edge_list.size(); i > 0; i-- ) {
      this_edge = pt_edge_list.get_and_step();
      if ( this_edge->marked() == -1 ) {
        i_found_some += 1;
        this_edge->marked(i_found_some);
        point_edge_chain.append(pt2);
        point_edge_chain.append(this_edge);

        edge_pt1 = this_edge->point(0);
        edge_pt2 = this_edge->point(1);
        if (pt2 == edge_pt1)
        {
          pt2 = edge_pt2;
        }
        else
        {
          assert(pt2 == edge_pt2);
          pt2 = edge_pt1;
        }


	      keepgoing = CUBIT_TRUE;
	      pt_edge_list.clean_out();
        pt2->edges(pt_edge_list);
	      break;
      }
    }
  }
  
  // clear marks
  for (i=unordered_edges.size(); i>0; i--)
  {
    cur_edge = unordered_edges.get_and_step();
    for (j=0; j<2; j++)
    {
      pt_edge_list.clean_out();
      cur_edge->point(j)->edges(pt_edge_list);
      pt_edge_list.reset();
      for (k=pt_edge_list.size(); k>0; k--)
        pt_edge_list.get_and_step()->marked(0);
    }
  }

  assert(pt2 == originalpt1);
}

//===========================================================================
//Function Name: partial_chain
//Description:   given an ordered point - edge boundary and a start and end
//               point on the boundary, return the point - edge chain between
//               the two points, inclusive.
//Author: bwhanks
//Date: 2003
//===========================================================================
CubitStatus FacetDataUtil::partial_chain(DLIList<FacetEntity*> &point_edge_chain,
                                         FacetEntity* point1,
                                         FacetEntity* point2,
                                         DLIList<FacetEntity*> &chain_between)
{
  // make sure the edges_between list is empty
  assert(chain_between.size() == 0);
  assert(point1 != point2);

  // find the start point in the list
  int pt1_index = point_edge_chain.where_is_item(point1);

  if (-1 == pt1_index)
    return CUBIT_FAILURE;

  point_edge_chain.reset();
  point_edge_chain.step(pt1_index);

  CubitBoolean b_done = CUBIT_FALSE;
  while (!b_done)
  {
    if (point_edge_chain.get() == point2)
      b_done = CUBIT_TRUE;

    chain_between.append(point_edge_chain.get_and_step());
  }

  return CUBIT_SUCCESS;
}

//===========================================================================
//Function Name: get_facet_points
//Description:   return a unique list of the points from a given set of facets
//Author: bwhanks
//Date: 2003
//===========================================================================
void FacetDataUtil::get_facet_points(DLIList<CubitFacet*> &cubit_facets,
                                     DLIList<CubitPoint*> &facet_points)
{
  int i;
  DLIList<CubitPoint*> cubit_points(cubit_facets.size() * 3);
  for( i = cubit_facets.size(); i--; )
  {
    CubitFacet* facet = cubit_facets.step_and_get();
    for( int j = 0; j < 3; j++ )
    {
      CubitPoint* pt = dynamic_cast<CubitPoint*>(facet->point(j));
      assert(!!pt);
      pt->marked(0);
      cubit_points.append(pt);
    }
  }
  for( i = cubit_points.size(); i--; )
  {
    CubitPoint* pt = cubit_points.step_and_get();
    pt->marked( pt->marked() + 1);
  }
  for( i = cubit_points.size(); i--; )
  {
    CubitPoint* pt = cubit_points.step_and_get();
    pt->marked( pt->marked() - 1 );
    if( pt->marked() > 0 )
      cubit_points.change_to(0);
  }
  cubit_points.remove_all_with_value(0);

  facet_points = cubit_points;
}

//===========================================================================
//Function Name: get_boundary_points
//Description:   return the boundary points from a list of facets (unordered)
//               assumes edges exist on the facets
//Author: bwhanks
//Date: 2003
//===========================================================================
void FacetDataUtil::get_boundary_points(DLIList<CubitFacet*> &facet_list,
                                        DLIList<CubitPoint*> &point_list)
{
  int ii, jj;
  CubitPoint *pt;
  CubitFacetEdge *edge;
  CubitFacet *facet;
  DLIList<CubitFacetEdge *>edge_list;
  for(ii=0; ii<facet_list.size(); ii++)
  {
    facet = facet_list.get_and_step();
    for(jj=0; jj<3; jj++)
    {
      edge = facet->edge( jj );
      if (1 == edge->num_adj_facets())
      {
        edge_list.append(edge);
        edge->point( 0 )->marked( 0 );
        edge->point( 1 )->marked( 0 );
      }
    }
  }
  for(ii=0; ii<edge_list.size(); ii++)
  {
    edge = edge_list.get_and_step();
    pt = edge->point( 0 );
    if (pt->marked() == 0)
    {
      pt->marked( 1 );
      point_list.append( pt );
    }
    pt = edge->point( 1 );
    if (pt->marked() == 0)
    {
      pt->marked( 1 );
      point_list.append( pt );
    }
  }
  for(ii=0; ii<point_list.size(); ii++)
    point_list.get_and_step()->marked(0);

}

//===========================================================================
//Function Name: get_boundary_edges
//Description: return an unordered list of edges at the boundary of a
//             set of facets
//Author: sjowen
//Date: 1/19/2004
//===========================================================================
void FacetDataUtil::get_boundary_edges(DLIList<CubitFacet*> &facet_list,
                                       DLIList<CubitFacetEdge*> &edge_list)
{
  int ii, jj;
  CubitFacetEdge *edge;
  CubitFacet *facet;
  for(ii=0; ii<facet_list.size(); ii++)
  {
    facet = facet_list.get_and_step();
    for(jj=0; jj<3; jj++)
    {
      edge = facet->edge( jj );
      if (1 == edge->num_adj_facets())
      {
        edge_list.append(edge);
      }
    }
  }
}

//===========================================================================
//Function Name: get_points
//Description:   get a unique set of points from facets
//Author: sjowen
//Date: 9/11/03
//===========================================================================
void FacetDataUtil::get_points(DLIList<CubitFacet*> &facet_list,
                               DLIList<CubitPoint*> &point_list)
{
  CubitFacet *facet;
  CubitPoint *pt;
  int ii, jj;
  for(ii=0; ii<facet_list.size(); ii++)
  {
    facet = facet_list.get_and_step();
    facet->point( 0 )->marked( 0 );
    facet->point( 1 )->marked( 0 );
    facet->point( 2 )->marked( 0 );
  }

  for (ii=0; ii<facet_list.size(); ii++)
  {
    facet = facet_list.get_and_step();
    for(jj=0; jj<3; jj++)
    {
      pt = facet->point(jj);
      if (pt->marked() == 0)
      {
        pt->marked(1);
        point_list.append(pt);
      }
    }
  }

  for(ii=0; ii<facet_list.size(); ii++)
  {
    facet = facet_list.get_and_step();
    facet->point( 0 )->marked( 0 );
    facet->point( 1 )->marked( 0 );
    facet->point( 2 )->marked( 0 );
  }
}

//===========================================================================
//Function Name: copy_facets
//Description:  make a complete copy of facets, points and edges
//Note:  copies edges and points with their "feature" flag
//Author: sjowen
//Date: 9/11/03
//===========================================================================
void FacetDataUtil::copy_facets(DLIList<CubitFacet*> &old_facet_list,
                                DLIList<CubitFacet*> &new_facet_list,
                                DLIList<CubitPoint*> &new_point_list,
                                DLIList<CubitFacetEdge*> &new_edge_list)
{
  // get a unique set of points from the facets

  DLIList<CubitPoint *> old_point_list;
  get_points(old_facet_list, old_point_list);
  CubitPoint **point_array = new CubitPoint* [old_point_list.size()];

  //- copy the points

  int ii;
  old_point_list.reset();
  CubitPoint *new_point, *the_point;
  for(ii=0; ii<old_point_list.size(); ii++)
  {
    the_point = old_point_list.get_and_step();
    new_point = new CubitPointData( the_point->coordinates() );
    the_point->marked( ii );
    new_point_list.append( new_point );
    point_array[ii] = new_point;
    if (the_point->is_feature())
      new_point->set_as_feature();
  }

  //- copy the facets

  int jj, idx;
  CubitFacet *new_facet, *the_facet;
  CubitPoint *points[3];

  old_facet_list.reset();
  for (ii=0; ii<old_facet_list.size(); ii++)
  {
    the_facet = old_facet_list.get_and_step();
    for (jj=0; jj<3; jj++)
    {
      idx = the_facet->point(jj)->marked();
      points[jj] = point_array[idx];
    }
    new_facet = new CubitFacetData( points[0], points[1], points[2] );
    new_facet_list.append( new_facet );
  }

  //- copy the edges

  int idx0, idx1;
  CubitFacetEdge *new_edge;
  CubitFacetEdge *old_edge;
  DLIList<CubitFacetEdge *>old_edge_list;
  get_edges(old_facet_list, old_edge_list);
  for(ii=0; ii<old_edge_list.size(); ii++)
  {
    old_edge = old_edge_list.get_and_step();
    idx0 = old_edge->point(0)->marked();
    idx1 = old_edge->point(1)->marked();
    new_edge = new CubitFacetEdgeData( point_array[idx0], point_array[idx1] );
    if (old_edge->is_feature())
      new_edge->set_as_feature();
    new_edge_list.append( new_edge );
  }

  delete [] point_array;

}

//===========================================================================
//Function Name: get_edges
//Description:  Populates the edge list from the list of facets - creates the
//              edges if necessary
//Author: sjowen
//Date: 9/11/03
//===========================================================================
void FacetDataUtil::get_edges(
  DLIList<CubitFacet *> &facet_list,
  DLIList<CubitFacetEdge *> &edge_list )
{
  int i, j;
  CubitPoint *p0, *p1;
  CubitFacet *facet_ptr;
  CubitFacetEdge *edge_ptr;
  DLIList<CubitFacet *> adj_facet_list;

  // mark the edges and create any that are missing
  facet_list.reset();  //have to reset this list to that the CubitFacetEdgeData's
                         //get constructed correctly
  for ( i = 0; i < facet_list.size(); i++)
  {
    facet_ptr = facet_list.get_and_step();
    for (j=0; j<3; j++) {
      edge_ptr = facet_ptr->edge(j);
      if (!(edge_ptr))
      {
        facet_ptr->get_edge_pts(j, p0, p1);
        edge_ptr = (CubitFacetEdge *) new CubitFacetEdgeData( p0, p1 );
      }
      edge_ptr->set_flag( 0 );
    }
  }

  // create a unique list of edges

  for ( i = 0; i < facet_list.size(); i++)
  {
    facet_ptr = facet_list.get_and_step();
    for (j=0; j<3; j++)
    {
      edge_ptr = facet_ptr->edge(j);
      if(edge_ptr && 0 == edge_ptr->get_flag())
      {
        edge_ptr->set_flag( 1 );
        edge_list.append( edge_ptr );
      }
    }
  }

  // reset the flags on the edges

  for ( i = 0; i < facet_list.size(); i++)
  {
    facet_ptr = facet_list.get_and_step();
    for (j=0; j<3; j++)
    {
      edge_ptr = facet_ptr->edge(j);
      if( edge_ptr )
        edge_ptr->set_flag( 0 );
    }
  }
}

//============================================================================
// Function: quality
// Author: sjowen
// Description: this is the S.H. Lo metric for triangles that also takes into
//               account a surface normal.
// Date: 2/2003
//============================================================================
double FacetDataUtil::quality(CubitVector &c1, CubitVector &c2, CubitVector &c3,
                              CubitVector &surf_normal)
{
#define TWO_ROOT_THREE  3.46410161514
   double area2, alpha;
   double length1, length2, length3;
   CubitVector edge1, edge2, edge3;

   // create edges from the vertices

   edge1 = c3 - c2;
   edge2 = c3 - c1;
   edge3 = c2 - c1;

   // compute twice the area

   CubitVector normal = edge3 * edge2;
   area2 = normal.length();

   length1 = edge1.length_squared();
   length2 = edge2.length_squared();
   length3 = edge3.length_squared();

   alpha = TWO_ROOT_THREE * area2 / (length1 + length2 + length3);

   // modify the alpha metric by the dot product of the triangle normal
   // with the surface normal.

   if (fabs(area2) < CUBIT_RESABS)
     alpha = 0.0;
   else
   {
     normal /= area2;
     double dot = normal % surf_normal;
     double penalty = pow(dot, 5);
     alpha *= penalty;
   }

   return alpha;
}

//================================================================================
// Description: compares length of two edges.
// Notes:       used for DLIList sort
// Author     : Steve Owen
// Date       : 9/27/2003
//================================================================================
int FacetDataUtil::edge_compare(CubitFacetEdge *&ea, CubitFacetEdge *&eb)
{
  double la = ea->length();
  double lb = eb->length();
  if (la < lb)
    return -1;
  else if (lb < la)
    return 1;
  return 0;
}

//================================================================================
// Description: collapse edges that are smaller than 10% the average length
// Notes:       non_manifold_only=true indicates that only edges that are attached
//              to more than two edges will be candidates for collapse.
// Author     : Steve Owen
// Date       : 9/27/2003
//================================================================================
CubitStatus FacetDataUtil::collapse_short_edges(
  DLIList<CubitFacet*> &facet_list,
  CubitBoolean non_manifold_only)
{
#define COLLAPSE_TOLERANCE 0.3

  CubitStatus rv = CUBIT_SUCCESS;
  int ncollapse = 0;

  // get the edges

  DLIList <CubitFacetEdge *>edge_list;
  DLIList <CubitPoint *> point_list;
  DLIList <CubitFacet *> one_facet;
  DLIList <CubitFacetEdge *> facet_edges;
  FacetDataUtil::get_edges( facet_list, edge_list );
  FacetDataUtil::get_points( facet_list, point_list );

  // determine average length and get the collapse threshold

  int ii, jj, kk;
  double len;
  double tot_len = 0.0;
  for(ii=0; ii<point_list.size(); ii++)
    point_list.get_and_step()->marked(0);
  CubitFacetEdge *edge;
  for(ii=0; ii<edge_list.size(); ii++)
  {
    edge = edge_list.get_and_step();
    len = edge->length();
    tot_len += len;
    edge->marked(0);
  }
  len = tot_len / edge_list.size();
  double collapse_tol = COLLAPSE_TOLERANCE * len;
  //check(edge_list, facet_list);

  // get a list of the edges that qualify for collapse

  DLIList<CubitFacetEdge *>short_edges;
  for(ii=0; ii<edge_list.size(); ii++)
  {
    edge = edge_list.get_and_step();
    if (non_manifold_only && edge->num_adj_facets() == 2)
      continue;
    len = edge->length();
    if (len < collapse_tol)
    {
      short_edges.append(edge);
    }
  }
  //sort them

  short_edges.sort(FacetDataUtil::edge_compare);

  // main loop

  int nedges = short_edges.size();
  for(ii=0; ii<nedges; ii++)
  {
    edge = short_edges.get_and_step();
    if (edge->marked() == 1)
      continue;
    len = edge->length();

    bool collapse = true;
    CubitPoint *pt[2];
    CubitPoint *pt1, *pt2;
    pt[0] = edge->point(0);
    pt[1] = edge->point(1);

    // compute a new candidate location for the merged points

    CubitVector new_location;
    CubitVector cpt[2];
    cpt[0] = pt[0]->coordinates();
    cpt[1] = pt[1]->coordinates();
    new_location = (cpt[0] + cpt[1]) * 0.5;

    for (jj=0; jj<2 && collapse; jj++)
    {
      DLIList<CubitFacet *> adjfacets;
      pt[jj]->facets( adjfacets );

      // check all facets adjacent this point that don't contain the edge
      // to make sure the resulting facet will be valid (we aren't inverting anything)

      for(kk=0; kk<adjfacets.size() && collapse; kk++)
      {
        CubitFacet *facet = adjfacets.get_and_step();
        pt1 = facet->next_node( pt[jj] );
        pt2 = facet->next_node( pt1 );
        int eidx = facet->edge_index( edge );
        if (eidx < 0)
          // don't check facets that have the current edge - they'll be deleted anyway
        {

          CubitVector cpt1 = pt1->coordinates();
          CubitVector cpt2 = pt2->coordinates();
          CubitVector norm = facet->normal();
          double q0 = FacetDataUtil::quality(cpt[jj], cpt1, cpt2, norm);
          double q1 = FacetDataUtil::quality(new_location, cpt1, cpt2, norm);
          if (!(q1 > 0.0 || q1 > q0))
          {
            collapse = false;
          }
        }
      }
    }

    if (collapse)
    {
      DLIList<CubitFacet *> new_facets;
      CubitPoint *collpt = pt[0];
      CubitPoint *delpt = pt[1];
      DLIList<CubitFacetEdge *> adjedges;
      CubitFacetEdge *adjedge;
      delpt->edges( adjedges );
      CubitFacet *facet;


      int mydebug = 0;<--- Assignment 'mydebug=0', assigned value is 0
      if (mydebug)<--- Condition 'mydebug' is always false
      {
        dcolor(4);
        draw_edge( edge );
        dview();
      }

      collpt->set(new_location);

      // delete all facets adjacent to the delpt.

      DLIList<CubitFacet *> adjfacets;
      delpt->facets( adjfacets );

      for(jj=0; jj<adjfacets.size(); jj++)
      {
        facet = adjfacets.get_and_step();
        pt1 = facet->next_node(delpt);
        pt2 = facet->next_node(pt1);
        int eidx = facet->edge_index( edge );
        facet_list.move_to(facet);
        delete facet;
        facet = NULL;
        if (eidx >= 0)
        {
          //if this facet is adjecnt the edge, then just remove it from the list
          facet_list.extract();
        }
        else
        {

          // get or create edges as needed

          CubitFacetEdge *e[3];
          e[0] = collpt->get_edge( pt1 );
          e[1] = pt1->get_edge( pt2 );
          e[2] = pt2->get_edge( collpt );
          for (kk=0; kk<3; kk++)
          {
            if (!(e[kk]))
            {
              switch (kk)
              {
              case 0: e[kk] = (CubitFacetEdge *) new CubitFacetEdgeData( collpt, pt1 ); break;
              case 1: e[kk] = (CubitFacetEdge *) new CubitFacetEdgeData( pt1, pt2 ); break;
              case 2: e[kk] = (CubitFacetEdge *) new CubitFacetEdgeData( pt2, collpt ); break;
              }
              edge_list.append( e[kk] );
            }
          }

          // create a new facet with the points from the old facet and the collpt

          facet = new CubitFacetData( e[0], e[1], e[2] );
          new_facets.append(facet);

          // create edges on the facet

          facet_list.change_to( facet );
        }
      }

      for(jj=0; jj<adjedges.size(); jj++)
      {
        adjedge = adjedges.get_and_step();
        adjedge->marked(1);  // mark it for deletion later
        assert(adjedge->num_adj_facets() == 0);
      }
      assert(delpt->num_adj_facets() == 0);
      ncollapse++;

      //check(edge_list, facet_list);
    }

  }

  PRINT_INFO("Collapsed %d short edges in triangulation\n", ncollapse);

  // delete points and edges that aren't used
  if (ncollapse > 0)
  {
    for(ii=0; ii<edge_list.size(); ii++)
    {
      edge = edge_list.get_and_step();
      if (edge->marked())
      {
        assert(edge->num_adj_facets() == 0);
        delete edge;
      }
      else if(edge->num_adj_facets() < 2){
        PRINT_ERROR("Unexpected result while collapsing an edge.\n");
        return CUBIT_FAILURE;
          //assert(edge->num_adj_facets() >= 2);
      }
    }
    CubitPoint *point;
    for(ii=0; ii<point_list.size(); ii++)
    {
      point = point_list.get_and_step();
      if (point->num_adj_facets() == 0)
      {
        delete point;
      }
    }
  }

  return rv;
}

//===========================================================================
//  Function: check
//  Purpose:  debugging only
//  Date:     10/2003
//  Author:   sjowen
//===========================================================================
void FacetDataUtil::check(DLIList<CubitFacetEdge *> &edge_list,
                          DLIList<CubitFacet *> &facet_list)
{

  CubitBox box;
  CubitFacetEdge *edge;
  int ii;
  int nedges = 0;
  for(ii=0; ii<edge_list.size(); ii++)
  {
    edge = edge_list.get_and_step();
    if (edge->marked() == 1)
      continue;
    int nadj = edge->num_adj_facets();
    if (nadj <= 1)
    {
      CubitVector v0 = edge->point(0)->coordinates();
      CubitVector v1 = edge->point(1)->coordinates();
      CubitVector min(CUBIT_MIN(v0.x(), v1.x()), CUBIT_MIN(v0.y(), v1.y()), CUBIT_MIN(v0.z(), v1.z()));
      CubitVector max(CUBIT_MAX(v0.x(), v1.x()), CUBIT_MAX(v0.y(), v1.y()), CUBIT_MAX(v0.z(), v1.z()));
      CubitBox ebox(min, max);
      if (nedges == 0)
        box.reset(min, max);
      else
        box |= ebox;
//      dcolor(3);
//      dfldraw(facet_list);
      dcolor(4);
      dedraw(edge);
      dpoint(v0);
      dpoint(v1);
      nedges++;
    }
  }
  if (nedges > 0)
  {
    dzoom(box);
    dview();
  }
}

//===========================================================================
//  Function: draw_edge
//  Purpose:
//  Date:     10/2003
//  Author:   sjowen
//===========================================================================
void FacetDataUtil::draw_edge( CubitFacetEdge *edge )
{
  DLIList<CubitFacet *>adjfacets;
  edge->facets( adjfacets );
  int ii,jj;
  CubitFacet *facet;
  CubitBox box;
  for(jj=0; jj<2; jj++)
  {
    CubitPoint *p = edge->point(jj);
    adjfacets.clean_out();
    p->facets( adjfacets );
    for(ii=0; ii<adjfacets.size(); ii++)
    {
      facet = adjfacets.get_and_step();
      if (jj==0 && ii==0)
        box = facet->bounding_box();
      else
        box |= facet->bounding_box();
      dfdraw(facet);
    }
    dpoint(p->coordinates());
    dpoint(p->coordinates());
  }
  dzoom(box);
}

//=============================================================================
//Function:  is_point_in_polyhedron
//Description: point-in-polyhedron test; polyhedron must be water-tight,
//             manifold, triangle-tiled.  Casts a random ray in positive
//             x, y, and z.  Counts crossings.  Starts over with a recast
//             if a triangle is perpendicular to the ray or if the ray
//             hits a triangle edge or vertex.
//             Also tests for point on polyhedron.
//Author: John Fowler
//Date: 9/30/03
//=============================================================================
CubitStatus FacetDataUtil::is_point_in_polyhedron(
                                     DLIList<CubitFacet *> &tfacet_list,
                                     CubitVector &point_coords,
                                     CubitPointContainment &is_point_in)
{
unsigned int i;
CubitBox bbox;
CubitFacet *facet;
CubitVector bbox_min, bbox_max, ray;
CubitStatus status;
CubitPointContainment pt_status;
bool is_outside, recast, is_on_plane;
double xpt, ypt, zpt;
double rayx, rayy, rayz;
int number_of_recasts;

  point_coords.get_xyz(xpt, ypt, zpt);
  recast = true;
  number_of_recasts = 0;
  while ( (recast == true) && (number_of_recasts < 10) ) {
    recast = false;
    is_outside = true;
    random_positive_ray(ray); // a random positively-pointing ray
    ray.get_xyz(rayx,rayy,rayz);
    for ( i = tfacet_list.size(); i > 0; i-- ) {
      facet = tfacet_list.get_and_step();
      bbox = facet->bounding_box();
      bbox_min = bbox.minimum();
      bbox_max = bbox.maximum();
      //  Because the ray is positive in direction, discard bounding boxes
      //  that are entirely < the starting point.
      if ( (xpt-bbox_max.x()) > CUBIT_RESABS ) continue;
      if ( (ypt-bbox_max.y()) > CUBIT_RESABS ) continue;
      if ( (zpt-bbox_max.z()) > CUBIT_RESABS ) continue;
      if ( ray_intersects_boundingbox(point_coords,ray,bbox) == false )
        continue;
      CubitPlane plane = facet->plane();
      CubitVector normal = plane.normal();
      double xnorm, ynorm, znorm;
      xnorm = normal.x();
      ynorm = normal.y();
      znorm = normal.z();
      //  Intersect the ray with the facet plane.  If ray is perpendicular to
      //  facet plane and point is on it, recast the ray and try again.
      double denominator = rayx*xnorm + rayy*ynorm + rayz*znorm;
      double distanc = xnorm*xpt + ynorm*ypt + znorm*zpt + plane.coefficient();
      if ( fabs(denominator) < GEOMETRY_RESABS )
      {
        if ( fabs(distanc) < GEOMETRY_RESABS ) {
          recast = true;
	  break;
	} else continue;  // point is not on plane and ray is parallel to plane
      }
      double t, xintpt, yintpt, zintpt;
      t = -distanc;
      t /= denominator;
      if ( fabs(t) < GEOMETRY_RESABS ) is_on_plane = true;
      else if ( t < GEOMETRY_RESABS ) continue;
      else is_on_plane = false;
      xintpt = xpt + t*rayx;
      yintpt = ypt + t*rayy;
      zintpt = zpt + t*rayz;
      CubitPoint *pt;
      //  We need to project the triangle onto 2D.  Use the smaller components of
      //  the normal to detemine a good projection.
      double x0, y0, x1, y1, x2, y2;
      if ( (fabs(xnorm) >= fabs(ynorm)) && (fabs(xnorm) >= fabs(znorm)) ){  //  Use y,z
        pt = facet->point(0);
        x0 = pt->y(); y0 = pt->z();
        pt = facet->point(1);
        x1 = pt->y(); y1 = pt->z();
        pt = facet->point(2);
        x2 = pt->y(); y2 = pt->z();
        status = pt_in_tri_2d(yintpt,zintpt,x0,y0,x1,y1,x2,y2,pt_status);
      } else if (fabs(ynorm) >= fabs(znorm)) {  //  Use z,x
        pt = facet->point(0);
        x0 = pt->x(); y0 = pt->z();
        pt = facet->point(1);
        x1 = pt->x(); y1 = pt->z();
        pt = facet->point(2);
        x2 = pt->x(); y2 = pt->z();
        status = pt_in_tri_2d(xintpt,zintpt,x0,y0,x1,y1,x2,y2,pt_status);
      } else {  // Use x,y
        pt = facet->point(0);
        x0 = pt->x(); y0 = pt->y();
        pt = facet->point(1);
        x1 = pt->x(); y1 = pt->y();
        pt = facet->point(2);
        x2 = pt->x(); y2 = pt->y();
        status = pt_in_tri_2d(xintpt,yintpt,x0,y0,x1,y1,x2,y2,pt_status);
      }

      if ( status == CUBIT_FAILURE ) {
        recast = true;
	break;
      }
      if ( pt_status == CUBIT_PNT_OUTSIDE ) continue;
      //  Is the point on the triangle?
      if ( is_on_plane == true ) {
        is_point_in = CUBIT_PNT_BOUNDARY;
        return CUBIT_SUCCESS;
      }
      if ( pt_status == CUBIT_PNT_INSIDE ) is_outside = ! is_outside;
      else if ( pt_status == CUBIT_PNT_BOUNDARY ) {
        recast = true;
        break;
      }
    }
    if ( recast == true ) {
      tfacet_list.reset();
      number_of_recasts += 1;
    }
  }
  if ( recast == true ) {
    PRINT_ERROR("Number of recasts in point-in-polygon exceeded 10.\n");
    return CUBIT_FAILURE;
  }
  if ( is_outside == false ) is_point_in = CUBIT_PNT_INSIDE;
  else is_point_in = CUBIT_PNT_OUTSIDE;

  return CUBIT_SUCCESS;
}

//===========================================================================
//  Function: pt_in_tri_2d
//  Purpose:
//  Date:     10/2003
//  Author:   John Fowler
//===========================================================================
CubitStatus FacetDataUtil::pt_in_tri_2d(double xpt, double ypt,
                       double x0, double y0,
		       double x1, double y1,
		       double x2, double y2,
		       CubitPointContainment &is_point_in)
{
//  From Schneider & Eberly, "Geometric Tools for COmputer Graphics",
//  Chap. 13.3.1.  If triangle is needle-thin, CUBIT_FAILURE might be
//  returned, in wich case is_point_in is undefined.

double c0, c1, c2;
double e0x, e1x, e2x, e0y, e1y, e2y;
double n0x, n1x, n2x, n0y, n1y, n2y;
double denom0, denom1, denom2;

  e0x = x1 - x0; e0y = y1 - y0;
  e1x = x2 - x1; e1y = y2 - y1;
  e2x = x0 - x2; e2y = y0 - y2;
  n0x = e0y; n0y = -e0x;
  n1x = e1y; n1y = -e1x;
  n2x = e2y; n2y = -e2x;
  denom0 = n1x*e0x + n1y*e0y;
  if ( fabs(denom0) < CUBIT_RESABS ) {
    PRINT_ERROR("Failure in pt_in_tri_2d; needle-thin triangle encountered.\n");
    return CUBIT_FAILURE;
  }
  denom1 = n2x*e1x + n2y*e1y;
  if ( fabs(denom1) < CUBIT_RESABS ) {
    PRINT_ERROR("Failure in pt_in_tri_2d; needle-thin triangle encountered.\n");
    return CUBIT_FAILURE;
  }
  denom2 = n0x*e2x + n0y*e2y;
  if ( fabs(denom2) < CUBIT_RESABS ) {
    PRINT_ERROR("Failure in pt_in_tri_2d; needle-thin triangle encountered.\n");
    return CUBIT_FAILURE;
  }

  c0 = -( n1x*(xpt-x1) + n1y*(ypt-y1) )/denom0;
  c1 = -( n2x*(xpt-x2) + n2y*(ypt-y2) )/denom1;
  c2 = -( n0x*(xpt-x0) + n0y*(ypt-y0) )/denom2;

  if ( (c0 > 0.0) && (c1 > 0.0) && (c2 > 0.0) ) is_point_in = CUBIT_PNT_INSIDE;
  else if ( (c0 < 0.0) || (c1 < 0.0) || (c2 < 0.0) ) is_point_in = CUBIT_PNT_OUTSIDE;
  else is_point_in = CUBIT_PNT_BOUNDARY;

  return CUBIT_SUCCESS;

}

//===========================================================================
//  Function: random_positive_ray
//  Purpose:
//  Date:     10/2003
//  Author:   John Fowler
//===========================================================================
void FacetDataUtil::random_positive_ray(CubitVector& ray)
{
  double temp;
  double rayx = 0.0, rayy = 0.0, rayz = 0.0;

  temp = 0.0;
  while ( temp < 1.e-6 ) {
    rayx = (double(rand())/(RAND_MAX+1.0));
    rayy = (double(rand())/(RAND_MAX+1.0));
    rayz = (double(rand())/(RAND_MAX+1.0));
    temp = sqrt(rayx*rayx + rayy*rayy + rayz*rayz);
  }
  rayx /= temp;
  rayy /= temp;
  rayz /= temp;

  ray.set(rayx,rayy,rayz);

}

//===========================================================================
//  Function: ray_intersects_boundingbox
//  Purpose:
//  Date:     10/2003
//  Author:   John Fowler
//===========================================================================
bool FacetDataUtil::ray_intersects_boundingbox(CubitVector& point, CubitVector& ray, const CubitBox& bbox)
{
double t, xtest, ytest, ztest;
double xdir, ydir, zdir, xpt, ypt, zpt, xmin, ymin, zmin, xmax, ymax, zmax;
CubitVector bbox_min, bbox_max;

  point.get_xyz(xpt,ypt,zpt);
  ray.get_xyz(xdir,ydir,zdir);
  bbox_min = bbox.minimum();
  bbox_max = bbox.maximum();
  bbox_min.get_xyz(xmin,ymin,zmin);
  bbox_max.get_xyz(xmax,ymax,zmax);
  xmin -= GEOMETRY_RESABS;  //  So we don't miss any.
  ymin -= GEOMETRY_RESABS;
  zmin -= GEOMETRY_RESABS;
  xmax += GEOMETRY_RESABS;
  ymax += GEOMETRY_RESABS;
  zmax += GEOMETRY_RESABS;

 //  Notice that we are only interested in bboxes on the non-negative side of t.
  if ( fabs(xdir) > CUBIT_RESABS ) {
    // test xmin plane
    t = (xmin - xpt)/xdir;
    if ( t >= 0.0 ) {
      ytest = ypt + t*ydir;
      ztest = zpt + t*zdir;
      if ( (ytest >= ymin) && (ytest <= ymax) && (ztest >= zmin) && (ztest <= zmax) )
        return true;
    }
    // test xmax plane
    t = (xmax - xpt)/xdir;
    if ( t > 0.0 ) {
      ytest = ypt + t*ydir;
      ztest = zpt + t*zdir;
      if ( (ytest >= ymin) && (ytest <= ymax) && (ztest >= zmin) && (ztest <= zmax) )
        return true;
    }
  }
  if ( fabs(ydir) > CUBIT_RESABS ) {
    // test ymin plane
    t = (ymin - ypt)/ydir;
    if ( t >= 0.0 ) {
      xtest = xpt + t*xdir;
      ztest = zpt + t*zdir;
      if ( (xtest >= xmin) && (xtest <= xmax) && (ztest >= zmin) && (ztest <= zmax) )
        return true;
    }
    // test ymax plane

    t = (ymax - ypt)/ydir;
    if ( t > 0.0 ) {
      xtest = xpt + t*xdir;
      ztest = zpt + t*zdir;
      if ( (xtest >= xmin) && (xtest <= xmax) && (ztest >= zmin) && (ztest <= zmax) )
        return true;
    }
  }
  if ( fabs(zdir) > CUBIT_RESABS ) {
    // test zmin plane
    t = (zmin - zpt)/zdir;
    if ( t > 0.0 ) {
      xtest = xpt + t*xdir;
      ytest = ypt + t*ydir;
      if ( (xtest >= xmin) && (xtest <= xmax) && (ytest >= ymin) && (ytest <= ymax) )
        return true;
    }
    // test zmax plane
    t = (zmax - zpt)/zdir;
    if ( t > 0.0 ) {
      xtest = xpt + t*xdir;
      ytest = ypt + t*ydir;
      if ( (xtest >= xmin) && (xtest <= xmax) && (ytest >= ymin) && (ytest <= ymax) )
        return true;
    }
  }

  return false;
}

//===========================================================================
//  Function: write_facets
//  Purpose:  write a list of facets to a cubit facet file
//  Date:     11/28/2002
//  Author:   sjowen
//===========================================================================
CubitStatus FacetDataUtil::write_facets( const char *file_name, DLIList<CubitFacet *> &facet_list)
{
  FILE *fp = fopen(file_name, "w");
  if (!fp)
  {
    PRINT_ERROR("Couldn't open file %s for writing a facet file.\n", file_name);
    return CUBIT_FAILURE;
  }

  DLIList<CubitPoint*> point_list;
  get_points(facet_list, point_list);

  fprintf(fp, "%d %d\n", point_list.size(), facet_list.size());
  CubitPoint *pt;
  int ii;
  for (ii=1; ii<=point_list.size(); ii++)
  {
    pt = point_list.get_and_step();
    pt->set_id(ii);
    fprintf(fp, "%d %f %f %f\n", ii, pt->x(), pt->y(), pt->z() );
  }

  CubitFacet *facet;
  for (ii=1; ii<=facet_list.size(); ii++)
  {
    facet = facet_list.get_and_step();
    fprintf(fp, "%d %d %d %d\n", ii, facet->point(0)->id(),
             facet->point(1)->id(), facet->point(2)->id());
  }

  fclose(fp);
  return CUBIT_SUCCESS;

}

//=============================================================================
//Function:  split_into_shells (PUBLIC)
//Description: split this set of facets into multiple surfaces where there are
//             may be disjoint regions.
//Author: sjowen
//Date: 8/27/2003
//=============================================================================
CubitStatus FacetDataUtil::split_into_shells(
  DLIList<CubitFacet *> &tfacet_list,
  DLIList<CubitQuadFacet *> &qfacet_list,
  DLIList<DLIList<CubitFacet *> *> &shell_list,
  CubitBoolean &is_water_tight)
{
  CubitStatus stat = CUBIT_SUCCESS;<--- Variable 'stat' is assigned a value that is never used.
    //need to init this variable otherwise the caller must have.
  is_water_tight = CUBIT_TRUE;

  // combine the quads and tri lists

  DLIList <CubitFacet *> facet_list = tfacet_list;
  int ii;
  CubitQuadFacet *qfacet_ptr;
  for (ii=0; ii<qfacet_list.size(); ii++)
  {
    qfacet_ptr = qfacet_list.get_and_step();
    facet_list.append(qfacet_ptr->get_tri_facet( 0 ));
    facet_list.append(qfacet_ptr->get_tri_facet( 1 ));
  }

  // mark all the facets to begin with

  CubitFacet *facet_ptr;
  for (ii=0; ii<facet_list.size(); ii++)
  {
    facet_ptr = facet_list.get_and_step();
    facet_ptr->marked( 1 );
  }

  // populate the facet edge lists

  DLIList<CubitFacetEdge *> edge_list;
  FacetDataUtil::get_edges( facet_list, edge_list );

  // some debug stuff to draw the facets

  int mydebug=0;<--- Assignment 'mydebug=0', assigned value is 0
  if (mydebug)<--- Condition 'mydebug' is always false
  {
    for (ii=0; ii<facet_list.size(); ii++)
    {
      facet_ptr = facet_list.get_and_step();
      GfxDebug::draw_facet(facet_ptr,CUBIT_YELLOW_INDEX);
    }
    GfxDebug::flush();
    GfxDebug::mouse_xforms();
  }

  // Go through the surfaceElemList and pull facets off one by one as we
  // determine which surface it belongs to.  Continue until we have depleted
  // the list

  int jj;
  int num_surfs_created = 0;
  int num_facets_remaining = facet_list.size();
  while( num_facets_remaining > 0)
  {
    // create a new shell to hold the face info

    num_surfs_created++;
    DLIList<CubitFacet *> *shell_ptr = new DLIList<CubitFacet *>;

    // start with the first facet and create a list of all elements
    // attached to the facet

    CubitBoolean shell_is_water_tight = CUBIT_TRUE;
    CubitFacet *start_facet_ptr = facet_list.get_and_step();
    stat = get_adj_facets_on_shell( start_facet_ptr, shell_ptr,
                                    shell_is_water_tight, mydebug );
    if (stat != CUBIT_SUCCESS)
      return stat;
    if (!shell_is_water_tight)
      is_water_tight = CUBIT_FALSE;

    shell_list.append( shell_ptr );

    // remove the facets in this shell from the facet list

    for( jj = facet_list.size(); jj > 0; jj-- )
    {
      facet_ptr = facet_list.get();
      if (facet_ptr->marked() == 0)
      {
        facet_list.change_to( NULL );
      }
      facet_list.step();
    }
    facet_list.remove_all_with_value( NULL );
    num_facets_remaining = facet_list.size();
  }

  return CUBIT_SUCCESS;
}

//=============================================================================
//Function:  get_adj_facets_on_shell (PRIVATE)
//Description: non recursive function that creates a list of all facets connected
//             the passed in face that are on the same surface
//Author: sjowen
//Date: 12/22/00
//=============================================================================
CubitStatus FacetDataUtil::get_adj_facets_on_shell(
  CubitFacet *start_facet_ptr,
  DLIList<CubitFacet *> *shell_ptr,
  CubitBoolean &is_water_tight,
  int mydebug)
{
  int found = 0;<--- Variable 'found' is assigned a value that is never used.
  int ii, jj;
  CubitStatus stat = CUBIT_SUCCESS;
  DLIList<CubitFacet*> temp_list;
  CubitFacet *facet_ptr = NULL;
  CubitFacet *adj_facet_ptr = NULL;
  DLIList<CubitFacetEdge *>edge_list;
  CubitFacetEdge *edge_ptr = NULL;
  DLIList<CubitFacet *>adj_facet_list;

  // add this facet to the list

  temp_list.append( start_facet_ptr );
  start_facet_ptr->marked( 0 );

  while (temp_list.size())
  {
    facet_ptr = temp_list.pop();
    if (facet_ptr->marked() == 0)
    {
      shell_ptr->append( facet_ptr );
      if (mydebug)
      {
        GfxDebug::draw_facet(facet_ptr, CUBIT_RED_INDEX);
        GfxDebug::flush();
      }
      edge_list.clean_out();
      facet_ptr->edges( edge_list );
      for (ii=0; ii<edge_list.size(); ii++)
      {
        edge_ptr = edge_list.get_and_step();
        adj_facet_list.clean_out();
        edge_ptr->facets( adj_facet_list );
        found = 0;

        for (jj=0; jj<adj_facet_list.size() && !found; jj++)
        {
          adj_facet_ptr = adj_facet_list.get_and_step();
          if (adj_facet_ptr != facet_ptr)
          {

            // go to its neighbor if it is part of the surface

            if (adj_facet_ptr->marked() == 1)
            {
              temp_list.append( adj_facet_ptr );
              adj_facet_ptr->marked( 0 );
              found = 1;
            }
          }

        }
        if (is_water_tight && adj_facet_list.size() == 1)
        {
          is_water_tight = CUBIT_FALSE;
        }
      }
    }
  }

  return stat;
}

//=============================================================================
//Function:  stitch_facets (PRIVATE)
//Description: attempt to merge facets to form a watertight model
//Author: sjowen
//Date: 9/9/03
//=============================================================================
CubitStatus FacetDataUtil::stitch_facets(
  DLIList<DLIList<CubitFacet *> *> &shell_list,
  double tol,
  CubitBoolean &is_water_tight,
  CubitBoolean write_result)
{

  CubitStatus rv = CUBIT_SUCCESS;
  int npmerge = 0;
  int nemerge = 0;
  DLIList<CubitPoint*> unmerged_points;
  is_water_tight = CUBIT_FALSE;

  rv = merge_coincident_vertices( shell_list, tol, npmerge, nemerge, unmerged_points );
  if (rv != CUBIT_SUCCESS)
    return rv;

  int nnomerge = unmerged_points.size();
  if (nnomerge == 0)
  {
    is_water_tight = CUBIT_TRUE;
  }
  else
  {
    rv = merge_colinear_vertices( shell_list, tol, unmerged_points,
                                  npmerge, nemerge, nnomerge );
    if (rv != CUBIT_SUCCESS)
      return rv;

    if (nnomerge == 0)
    {
      is_water_tight = CUBIT_TRUE;
      int mydebug = 0;<--- Assignment 'mydebug=0', assigned value is 0
      if (mydebug)  // make sure its really water-tight<--- Condition 'mydebug' is always false
      {
        DLIList<CubitFacet *> *shell_ptr;
        DLIList<CubitFacetEdge *>boundary_edges;
        for (int ii=0; ii<shell_list.size(); ii++)
        {
          shell_ptr = shell_list.get_and_step();
          FacetDataUtil::get_boundary_edges(*shell_ptr, boundary_edges);
        }
        if (boundary_edges.size() > 0)
        {
          PRINT_ERROR("Not Water-tight!\n");
        }
      }
    }
  }

  if (write_result)
  {
    if (npmerge > 0 || nemerge > 0)
    {
      PRINT_INFO("%d facet vertices and %d facet edges were successfully merged.\n",
                 npmerge, nemerge);
    }
    if (is_water_tight)
    {
      PRINT_INFO("Facets are water-tight.\n");
    }
    else
    {
      PRINT_INFO("%d facet vertices on model boundary detected that could not be merged.\n", nnomerge);
    }
  }

  return rv;
}

//=============================================================================
//Function:  merge_colinear_vertices (PRIVATE)
//Description: check if any vertices fall on a free facet edge.  If so - split
//             the  adjacent facet to merge
//Author: sjowen
//Date: 1/19/2004
//=============================================================================
CubitStatus FacetDataUtil::merge_colinear_vertices(
  DLIList<DLIList<CubitFacet *> *> &shell_list,
  double tol,
  DLIList<CubitPoint *> &merge_point_list,  // points to attempt to merge
  int &npmerge,    // return number of vertices merged
  int &nemerge,    // return number of edges merged
  int &nnomerge)   // return number of vertices in list NOT merged
{
  nnomerge = 0;
  int mydebug = 0;

  int ii, jj, kk, ll, mm, pt_shell_id, edge_shell_id;
  RTree <CubitFacetEdge*> r_tree(tol);
  shell_list.reset();
  DLIList<CubitFacet *> *shell_ptr = NULL;
  CubitPoint *pt;
  CubitFacetEdge *edge;
  DLIList<CubitFacetEdge *>boundary_edges;
  for (ii=0; ii<shell_list.size(); ii++)
  {
    shell_ptr = shell_list.get_and_step();

    // get the boundary edges from the shell - these are candidates for merging.
    // note that this won't merge internal facets

    DLIList<CubitFacetEdge *>shell_boundary_edges;
    FacetDataUtil::get_boundary_edges(*shell_ptr, shell_boundary_edges);    

    // mark each of the edges with a shell id so we know when to merge shells
    // and add them to the kdtree for fast spatial searching
    edge_shell_id = ii+1;
    for(jj=0; jj<shell_boundary_edges.size(); jj++)
    {
      edge = shell_boundary_edges.get_and_step();
      edge->point(0)->marked(edge_shell_id);
      edge->point(1)->marked(edge_shell_id);
      edge->marked(edge_shell_id);
      r_tree.add(edge);
      boundary_edges.append(edge);
    }
    for(jj=0; jj<shell_ptr->size(); jj++)
      shell_ptr->get_and_step()->marked( edge_shell_id );
  }  

  // find points in merge_point_list that are colinear with edges in boundary_edges

  CubitBox ptbox;
  CubitVector ptmin, ptmax, coord;
  DLIList<CubitFacetEdge *>close_edges;
  CubitFacetEdge *close_edge;
  CubitFacet *facet;
  CubitPoint *p0, *p1;
  DLIList<CubitPoint*>adj_pt_list;
  DLIList<CubitPoint*>del_points;
  for(ii=0; ii<merge_point_list.size(); ii++)
  {
    pt = merge_point_list.get_and_step();
    if (pt->marked() < 0)  // has already been merged
      continue;   

    // find the closest edges in the rtree

    coord = pt->coordinates();
    ptmin.set( coord.x() - tol, coord.y() - tol, coord.z() - tol );
    ptmax.set( coord.x() + tol, coord.y() + tol, coord.z() + tol );
    ptbox.reset( ptmin, ptmax );
    close_edges.clean_out();
    r_tree.find(ptbox, close_edges);


    //remove any close edges that already share the merge point
    DLIList<CubitFacetEdge*> adj_edges;<--- Shadowed declaration
    for (jj=close_edges.size(); jj--; )
    {
      close_edge = close_edges.get();

      if (close_edge->point(0) == pt || close_edge->point(1) == pt)
      {
        close_edges.change_to( NULL );
        adj_edges.append( close_edge );
      }

      close_edges.step();
    }
    close_edges.remove_all_with_value( NULL );

    if( close_edges.size() == 0 && adj_edges.size() > 0 )
    {
      //use a coareser tolerance to get some edges
      //one tenth the average length of attached edges
      double temp_tol = 0;
      for( int i=adj_edges.size(); i--; )
        temp_tol += adj_edges.get_and_step()->length();

      temp_tol /= adj_edges.size();
      temp_tol *= 0.1;      

      //bump up tolerance to get more edges
      ptmin.set( coord.x() - temp_tol, coord.y() - temp_tol, coord.z() - temp_tol );
      ptmax.set( coord.x() + temp_tol, coord.y() + temp_tol, coord.z() + temp_tol );
      ptbox.reset( ptmin, ptmax );
      close_edges.clean_out();
      r_tree.find(ptbox, close_edges);
    }
        

    // We did find something - go try to merge

    CubitBoolean was_merged = CUBIT_FALSE;
    for (jj=0; jj<close_edges.size() && !was_merged; jj++)
    {
      // test the next edge on the list

      close_edge = close_edges.get_and_step();

      // make sure the edge does not contain the merge point

      if (close_edge->point(0) == pt || close_edge->point(1) == pt)
        continue;
    
      // check to see if the edge is within tolerance of the merge point

      CubitVector loc_on_edge;
      CubitBoolean is_outside_edge;
      double dist = close_edge->dist_to_edge(pt->coordinates(),
        loc_on_edge, is_outside_edge);

      // allow for some surface curvature. permit the point to be close to
      // the edge but not on.  May want to modify this factor if it not closing
      // the facets correctly.  If it's too big, it may get edges that aren't
      // really adjacent.
      double edge_tol = 0.1 * close_edge->length();
      if (is_outside_edge || dist > edge_tol)
        continue;

      // go merge the point with the edge
      was_merged = CUBIT_TRUE;

      if (mydebug)
      {
        DLIList<CubitFacet *> fl;
        pt->facets( fl );
        dcolor(CUBIT_YELLOW_INDEX);
        dfldraw(fl);
        dcolor(CUBIT_BLUE_INDEX);
        dpoint(pt->coordinates());
        CubitFacet *f = close_edge->adj_facet(0);
        dcolor(CUBIT_RED_INDEX);
        dfdraw(f);
        dview();
      }

      // remove close_edge from the rtree
    
      r_tree.remove( close_edge );
      edge_shell_id = close_edge->marked();
      close_edge->marked(0);

      // split the edge

      assert(1 == close_edge->num_adj_facets());  // assumes we are splitting a boundary facet
      facet = close_edge->adj_facet(0);
      CubitFacetData *dfacet = dynamic_cast<CubitFacetData *>(facet);
      CubitPoint *new_pt = dfacet->split_edge(close_edge->point(0),
                                              close_edge->point(1),
                                              pt->coordinates());
      int facet_tool_id = facet->tool_id();
      new_pt->marked(edge_shell_id);

      // add any new facets to the shell and create missing edges

      for (ll=0; ll<edge_shell_id; ll++)
        shell_ptr = shell_list.get_and_step();
      DLIList<CubitFacet *>adj_facets;
      new_pt->facets( adj_facets );
      for (ll=0; ll<adj_facets.size(); ll++)
      {
        facet = adj_facets.get_and_step();
        if (!facet->marked())
        {
          facet->marked(edge_shell_id);
          shell_ptr->append(facet);
          for (mm=0; mm<3; mm++) {
            if (!(edge = facet->edge(mm)))
            {
              facet->get_edge_pts(mm, p0, p1);
              edge = (CubitFacetEdge *) new CubitFacetEdgeData( p0, p1 );
              edge->marked( 0 );
            }
          }
          facet->set_tool_id(facet_tool_id);
        }
      }

      // merge the points,
                                     
      merge_points( pt, new_pt, nemerge, &r_tree );
      npmerge++;

      // add any new edges to the rtree

      DLIList<CubitFacetEdge *> adj_edges;<--- Shadow variable
      pt->edges( adj_edges );
      for (kk=0; kk<adj_edges.size(); kk++)
      {
        CubitFacetEdge *adj_edge = adj_edges.get_and_step();
        if (!adj_edge->marked() && adj_edge->num_adj_facets() == 1)
        {
          adj_edge->marked(pt->marked());
          r_tree.add(adj_edge);
        }
      }

      // see if shells need to merge and then merge

      pt_shell_id = pt->marked();
      if (pt_shell_id != edge_shell_id)
      {

        // get the shell containing the close point.  Nullify the
        // pointer in the shell list and move all of its facets
        // to the other shell.

        int delete_shell = edge_shell_id;
        DLIList<CubitFacet *> *delete_shell_ptr = NULL;
        shell_list.reset();
        for (ll=0; ll<delete_shell; ll++)
          delete_shell_ptr = shell_list.get_and_step();
        shell_list.back();
        shell_list.change_to(NULL);

        if(!delete_shell_ptr)
          return CUBIT_FAILURE;

        // mark all the points on the delete_shell as now being part of
        // the other shell.

        for(ll=0; ll<delete_shell_ptr->size(); ll++)
        {
          facet = delete_shell_ptr->get_and_step();
          facet->marked( pt_shell_id );
          facet->point( 0 )->marked( pt_shell_id );
          facet->point( 1 )->marked( pt_shell_id );
          facet->point( 2 )->marked( pt_shell_id );
          facet->edge( 0 )->marked( pt_shell_id );
          facet->edge( 1 )->marked( pt_shell_id );
          facet->edge( 2 )->marked( pt_shell_id );
        }

        // append the two lists together

        shell_list.reset();
        for (ll=0; ll<pt_shell_id; ll++)
          shell_ptr = shell_list.get_and_step();
        *shell_ptr += (*delete_shell_ptr);
        delete delete_shell_ptr;
      }

      // set the marked flag to negative to indicate that it has been
      // merged and it needs to be deleted.

      del_points.append(new_pt);
      new_pt->marked( -new_pt->marked() );
      pt->marked( -pt->marked() );
    } // close_edges
   
    if (!was_merged)
    {
      nnomerge++;
      if (mydebug)
      {
        dcolor(CUBIT_BLUE_INDEX);
        dpdraw(pt);
        dcolor(CUBIT_RED_INDEX);
        CubitVector mmin(-1e10, -1e10, -1e10);
        CubitVector mmax(1e10, 1e10, 1e10);
        ptbox.reset(mmin, mmax);
        r_tree.find(ptbox, close_edges);
        for(int zz=0; zz<close_edges.size(); zz++)
        {
          edge = close_edges.get_and_step();
          dedraw(edge);
          CubitVector loc_on_edge;
          CubitBoolean is_on_edge;
          double dist = edge->dist_to_edge(pt->coordinates(), loc_on_edge, is_on_edge);
          PRINT_INFO("edge %d dist = %f is_on_edge = %d\n", edge->id(), dist, is_on_edge);
        }
        dview();
      }
    }
  } // merge_point_list

  // compress the shell list

  shell_list.remove_all_with_value(NULL);

  // delete points that were merged

  for (ii=0; ii<del_points.size(); ii++)
  {
    pt = del_points.get_and_step();
    if (pt->marked() < 0)
    {
      assert( pt->num_adj_facets() == 0 );
      delete pt;
    }
  }

  return CUBIT_SUCCESS;
}

//=============================================================================
//Function:  merge_points (PRIVATE)
//Description: merge two cubit points where it has been determined that they
//             are coincident.  Also handle merging their adjacent edges where
//             appropriate
//Author: sjowen
//Date: 1/19/2004
//=============================================================================
CubitStatus FacetDataUtil::merge_points(
  CubitPoint *pt0, CubitPoint *pt1,
  int &nemerge, RTree <CubitFacetEdge*> *r_tree)  // number of edges we had to merge top do this
{
  int mydebug = 0;<--- Assignment 'mydebug=0', assigned value is 0

  // merge the points
  pt0->merge_points( pt1, CUBIT_TRUE );
  //pt0->set_as_feature();

  if (mydebug)<--- Condition 'mydebug' is always false
  {
    DLIList<CubitFacet *>adj_facets;
    pt0->facets( adj_facets );
    dcolor(CUBIT_RED_INDEX);
    dfldraw(adj_facets);
  }

  // merge edges

  int ll, mm;
  bool edge_merged;
  do {
    edge_merged = false;
    CubitFacetEdge *edge, *other_edge;

    DLIList<CubitFacetEdge *> adj_edges;
    pt0->edges( adj_edges );
    for(ll=0; ll<adj_edges.size() && !edge_merged; ll++)
    {
      edge = adj_edges.get_and_step();
      for(mm=0; mm<adj_edges.size() && !edge_merged; mm++)
      {
        other_edge = adj_edges.get_and_step();
        if (other_edge != edge &&
            edge->other_point(pt0) == other_edge->other_point(pt0))
        {
          CubitFacetEdgeData *dedge = dynamic_cast<CubitFacetEdgeData*>(edge);
          CubitFacetEdgeData *dother_edge = dynamic_cast<CubitFacetEdgeData*>(other_edge);
          // mbrewer (11/16/2005 for Bug 5049)
          if(r_tree){
//            r_tree->remove(dother_edge);
             CubitBoolean temp_bool = r_tree->remove(dother_edge);
             if(!temp_bool){
               PRINT_DEBUG_139("FacetDataUtil:  D_OTHER_EDGE did not exist in RTREE.\n");
             }
          }
          if (dedge->merge_edges( dother_edge ) == CUBIT_SUCCESS)
          {            
            nemerge++;
            edge_merged = true;
            dedge->set_as_feature();
          }
        }
      }
    }
  } while (edge_merged);
  return CUBIT_SUCCESS;
}


CubitStatus FacetDataUtil::merge_coincident_vertices( DLIList<CubitPoint*> &points )
{
  points.reset();
  CubitPoint *surviving_point = points.pop();
  while( points.size() )
  {    
    int nemerge = 0;
    merge_points( surviving_point, points.pop(), nemerge );   
  }

  return CUBIT_SUCCESS;
}



//=============================================================================
//Function:  merge_coincident_vertices (PRIVATE)
//Description: merge vertices (and connected facets and edges) if they are
//             within tolerance.
//Author: sjowen
//Date: 9/9/03
//=============================================================================
CubitStatus FacetDataUtil::merge_coincident_vertices(
  DLIList<DLIList<CubitFacet *> *> &shell_list,
  double tol,
  int &npmerge,    // return number of vertices merged
  int &nemerge,    // return number of edges merged
  DLIList<CubitPoint *> &unmerged_points)   // return the vertices on boundary NOT merged
{
  int mydebug = 0;<--- Assignment 'mydebug=0', assigned value is 0
  npmerge = 0;
  nemerge = 0;
  int ii, jj, kk, ll, shell_id;
  KDDTree <CubitPoint*> kd_tree(tol, false);
  CubitPoint::set_box_tol( tol );
  shell_list.reset();
  DLIList<CubitFacet *> *shell_ptr = NULL;
  CubitPoint *pt;
  DLIList<CubitPoint *>boundary_points;
  DLIList<CubitPoint *> del_points;
  for (ii=0; ii<shell_list.size(); ii++)
  {
    shell_ptr = shell_list.get_and_step();

    // get the boundary points from the shell - these are candidates for merging.
    // note that this won't merge internal facets

    DLIList<CubitPoint *>shell_boundary_points;
    FacetDataUtil::get_boundary_points(*shell_ptr, shell_boundary_points);

    // mark each of the points with a shell id so we know when to merge shells
    // and add them to the kdtree for fast spatial searching

    shell_id = ii+1;
    for(jj=0; jj<shell_boundary_points.size(); jj++)
    {
      pt = shell_boundary_points.get_and_step();
      pt->marked(shell_id);
      kd_tree.add(pt);
      boundary_points.append(pt);
    }
  }
  kd_tree.balance();

  // find points that are coincident

  CubitBox ptbox;
  CubitVector ptmin, ptmax, coord;
  DLIList<CubitPoint *>close_points;
  CubitPoint *close_pt = NULL;
  CubitFacet *facet;
  DLIList<CubitPoint*>adj_pt_list;

  for(ii=0; ii<boundary_points.size(); ii++)
  {
    pt = boundary_points.get_and_step();
    if (pt->marked() < 0)  // has already been merged
      continue;

    // find the closest points in the kdtree

    coord = pt->coordinates();
    ptmin.set( coord.x() - tol, coord.y() - tol, coord.z() - tol );
    ptmax.set( coord.x() + tol, coord.y() + tol, coord.z() + tol );
    ptbox.reset( ptmin, ptmax );
    close_points.clean_out();
    kd_tree.find(ptbox, close_points);

    // if it didn't find anything to merge with, then we aren't water-tight

    CubitBoolean was_merged = CUBIT_FALSE;

    // We did find something - go try to merge

    for (jj=0; jj<close_points.size(); jj++)
    {
      close_pt = close_points.get_and_step();
      if (close_pt == pt)
        continue;
      if (close_pt->marked() < 0)  // has already been merged
        continue;
      assert(close_points.size() >= 1);

      // make sure this point is not already one of its neighbors
      // so we don't collapse a triangle

      CubitBoolean is_adjacent = CUBIT_FALSE;
      adj_pt_list.clean_out();
      close_pt->adjacent_points( adj_pt_list );
      for (kk=0; kk<adj_pt_list.size() && !is_adjacent; kk++)
      {
        if (adj_pt_list.get_and_step() == pt)
          is_adjacent = CUBIT_TRUE;
      }
      if (!is_adjacent)
      {
        // merge the points

        merge_points( pt, close_pt, nemerge );
        npmerge++;
        was_merged = CUBIT_TRUE;

        // see if shells need to merge and then merge

        shell_id = pt->marked();
        if (shell_id != close_pt->marked())
        {

          // get the shell containing the close point.  Nullify the
          // pointer in the shell list and move all of its facets
          // to the other shell.

          int delete_shell = close_pt->marked();
          DLIList<CubitFacet *> *delete_shell_ptr = NULL;
          shell_list.reset();
          for (ll=0; ll<delete_shell; ll++)
            delete_shell_ptr = shell_list.get_and_step();
          shell_list.back();
          shell_list.change_to(NULL);

          // mark all the points on the delete_shell as now being part of
          // the other shell.

          for(ll=0; ll<delete_shell_ptr->size(); ll++)
          {
            facet = delete_shell_ptr->get_and_step();
            facet->point( 0 )->marked( shell_id );
            facet->point( 1 )->marked( shell_id );
            facet->point( 2 )->marked( shell_id );
          }

          // append the two lists together

          shell_list.reset();
          for (ll=0; ll<shell_id; ll++)
            shell_ptr = shell_list.get_and_step();
          *shell_ptr += (*delete_shell_ptr);
          delete delete_shell_ptr;
        }

        // set the marked flag to negative to indicate that it has been
        // merged and it need to be deleted.

        if (close_pt->marked() > 0)
          close_pt->marked( -close_pt->marked() );
        del_points.append( close_pt );
      }
    }
    if (was_merged)
    {
      if (pt->marked() > 0)
        pt->marked( -pt->marked() );
    }
    else
    {
      // check to see if it was already merged
      if (close_points.size() == 1 && close_pt == pt)
      {
        CubitFacetEdge *edge_ptr;
        DLIList<CubitFacetEdge *>adj_edges;
        pt->edges(adj_edges);
        CubitBoolean on_boundary = CUBIT_FALSE;
        for(kk=0; kk<adj_edges.size() && !on_boundary; kk++)
        {
          edge_ptr = adj_edges.get_and_step();
          if (edge_ptr->num_adj_facets() == 1)
            on_boundary = CUBIT_TRUE;
        }
        if (on_boundary)
        {
            //PRINT_INFO("Merging 'boundary' points.\n");
            //if (pt->marked() > 0) pt->marked( -pt->marked() );
          unmerged_points.append(pt);
        }
        else
        {
          if (pt->marked() > 0) pt->marked( -pt->marked() );
        }
      }
      else
      {
        // otherwise save it as an unmerged point to be handled later
        unmerged_points.append(pt);
      }
    }
  }

  // compress the shell list

  shell_list.remove_all_with_value(NULL);

  // delete points that were merged

  for (ii=0; ii<del_points.size(); ii++)
  {
    pt = del_points.get_and_step();
    if (pt->marked() < 0)
    {
      assert( pt->num_adj_facets() == 0 );
      delete pt;
    }
  }

  //double-check that these are still boundary points.
  //found case where two shells were tied together by a single facet point.
  //this point was initially deemed an unmerged boundary point but later
  //adjacent boundary edges got merged so it should not be a boundary 
  //point anymore.
  for( int k=0; k<unmerged_points.size(); k++ )
  {
    DLIList<CubitFacetEdge *>adj_edges;
    unmerged_points[k]->edges(adj_edges);
    CubitBoolean on_boundary = CUBIT_FALSE;
    for(kk=0; kk<adj_edges.size(); kk++)
    {
      CubitFacetEdge *edge_ptr = adj_edges.get_and_step();
      if (edge_ptr->num_adj_facets() == 1)
      {
        on_boundary = CUBIT_TRUE;
        break;
      }
    }
    if( CUBIT_FALSE == on_boundary )
      unmerged_points[k] = NULL;
  }
  unmerged_points.remove_all_with_value(NULL);
  
  if (mydebug)<--- Condition 'mydebug' is always false
  {
    CubitFacetEdge *edge;
    for (ii=0; ii<shell_list.size(); ii++)
    {
      shell_ptr = shell_list.get_and_step();
      dcolor(CUBIT_GREEN_INDEX);
      dfldraw(*shell_ptr);
      dcolor(CUBIT_RED_INDEX);
      for(jj=0; jj<shell_ptr->size(); jj++)
      {
        facet = shell_ptr->get_and_step();
        for(kk=0; kk<3; kk++)
        {
          edge = facet->edge(kk);
          DLIList<FacetEntity *> myfacet_list;
          edge->get_parents( myfacet_list );
          assert(myfacet_list.size() == edge->num_adj_facets());
          if (myfacet_list.size() != 2)
          {
            dedraw( edge );      
          }
        }
      }
    }
    dcolor(CUBIT_BLUE_INDEX);
    dpldraw(unmerged_points);
    dview();
  }

  return CUBIT_SUCCESS;

}

//=============================================================================
//Function:  delete_facets (PUBLIC)
//Description: delete the facets and all associated edges and points from
//             a list of lists of facets (shell_list)
//Author: sjowen
//Date: 1/21/2004
//=============================================================================
void FacetDataUtil::delete_facets(DLIList<DLIList<CubitFacet*>*> &shell_list)
{
  int ii;
  for (ii=0; ii<shell_list.size(); ii++)
  {
    DLIList<CubitFacet*> *facet_list_ptr = shell_list.get_and_step();
    delete_facets( *facet_list_ptr );
  }
}

//=============================================================================
//Function:  delete_facets (PUBLIC)
//Description: delete the facets and all associated edges and points from
//             a list of facets
//Author: sjowen
//Date: 1/21/2004
//=============================================================================
void FacetDataUtil::delete_facets(DLIList<CubitFacet*> &facet_list)
{
  int ii;
  CubitFacet *facet_ptr;
  for (ii=0; ii<facet_list.size(); ii++)
  {
    facet_ptr = facet_list.get_and_step();
    delete_facet( facet_ptr );
  }
}

//=============================================================================
//Function:  delete_facet (PUBLIC)
//Description: delete a single facet and its underlying edges and points if
//             they are no longer attached to anything
//Author: sjowen
//Date: 1/21/2004
//=============================================================================
void FacetDataUtil::delete_facet(CubitFacet *facet_ptr)
{
  DLIList<CubitPoint *>point_list;
  DLIList<CubitFacetEdge *>edge_list;
  facet_ptr->points(point_list);
  facet_ptr->edges(edge_list);

  delete facet_ptr;

  CubitFacetEdge *edge_ptr;
  CubitPoint *point_ptr;
  int ii;

  for (ii=0; ii<edge_list.size(); ii++)
  {
    edge_ptr = edge_list.get_and_step();
    if (edge_ptr->num_adj_facets() == 0)
      delete edge_ptr;
  }

  for (ii=0; ii<3; ii++)
  {
    point_ptr = point_list.get_and_step();
    if (point_ptr->num_adj_facets() == 0)
      delete point_ptr;
  }

}


void FacetDataUtil::destruct_facet_no_delete(CubitFacet *facet_ptr)
{
  CubitFacetData* facet_d_ptr = dynamic_cast<CubitFacetData*>(facet_ptr);
  if(!facet_d_ptr){
    PRINT_ERROR("Can't work with Facet pointer that isn't a facet data object.\n");
    return;
  }
  
  DLIList<CubitPoint *>point_list;
  DLIList<CubitFacetEdge *>edge_list;
  facet_ptr->points(point_list);
  facet_ptr->edges(edge_list);
  
  facet_d_ptr->destruct_facet_internals();

  CubitFacetEdge *edge_ptr;
  CubitPoint *point_ptr;
  int ii;

  for (ii=0; ii<edge_list.size(); ii++)
  {
    edge_ptr = edge_list.get_and_step();
    if (edge_ptr->num_adj_facets() == 0)
      delete edge_ptr;
  }

  for (ii=0; ii<3; ii++)
  {
    point_ptr = point_list.get_and_step();
    if (point_ptr->num_adj_facets() == 0)
      delete point_ptr;
  }

}

//=============================================================================
//Function:  intersect_facet (PUBLIC)
//Description: determine intersection of a segment with a facet
//             returns CUBIT_PNT_UNKNOWN: if segment is in plane of facet
//                     CUBIT_PNT_OUTSIDE: if segment does not intersect
//                     CUBIT_PNT_INSIDE: if segment intersects inside facet
//                     CUBIT_PNT_BOUNDARY: if segment intersects a vertex or edge
//Author: sjowen
//Date: 1/30/2004
//=============================================================================
CubitPointContainment FacetDataUtil::intersect_facet(
  CubitVector &start, CubitVector &end, // start and end points of vector
  CubitFacet *facet_ptr,      // the facet to intersect
  CubitVector &qq,            // return the intersection point
  CubitVector &ac,    // area coordinates of qq if is in or on facet
  CubitBoolean bound) // if true, only check for intersections between the end points.
{

  CubitPlane fplane = facet_ptr->plane();
  double dstart = fplane.distance(start);
  double dend = fplane.distance(end);

  // points are both in the plane of the facet - can't handle this case

  if (fabs(dstart) < GEOMETRY_RESABS &&
      fabs(dend) < GEOMETRY_RESABS)
  {
    return CUBIT_PNT_UNKNOWN;
  }

  // one point is on the plane

  if (fabs(dstart) < GEOMETRY_RESABS)
  {
    qq = start;
  }
  else if (fabs(dend) < GEOMETRY_RESABS)
  {
    qq = end;
  }

  // points are both on the same side of the plane
  else if(dstart*dend > 0.0 &&
          (bound || fabs(dstart-dend) < CUBIT_RESABS) )
  {
    return CUBIT_PNT_OUTSIDE;
  }
  // points are on opposite sides of plane: if bound == false then compute intersection with plane

  else
  {
    CubitVector dir = end-start;
    dir.normalize();
    qq = fplane.intersect(start, dir);
  }

  FacetEvalTool::facet_area_coordinate( facet_ptr, qq, ac );
   
//mod mbrewer ... the original code would call a point
    // on the boundary if any area coordinate was near
    // zero, regardless of whether another area coordinate
    // was negative... making it outside.
//   if (fabs(ac.x()) < GEOMETRY_RESABS ||
//       fabs(ac.y()) < GEOMETRY_RESABS ||
//       fabs(ac.z()) < GEOMETRY_RESABS)
//   {
//     return CUBIT_PNT_BOUNDARY;
//   }
  if ( (fabs(ac.x()) < GEOMETRY_RESABS && (ac.y() > -GEOMETRY_RESABS &&
                                           ac.z() > -GEOMETRY_RESABS) )||
       (fabs(ac.y()) < GEOMETRY_RESABS && (ac.x() > -GEOMETRY_RESABS &&
                                           ac.z() > -GEOMETRY_RESABS) )||
       (fabs(ac.z()) < GEOMETRY_RESABS && (ac.x() > -GEOMETRY_RESABS &&
                                           ac.y() > -GEOMETRY_RESABS) ) ){
    return CUBIT_PNT_BOUNDARY;
  }   
  else if (ac.x() < 0.0 || ac.y() < 0.0 || ac.z() < 0.0)
  {
    return CUBIT_PNT_OUTSIDE;
  }

  return CUBIT_PNT_INSIDE;
}


//=============================================================================
//Function:  get_bbox_of_points (PUBLIC)
//Description: Find the bounding box of a list of CubitPoints
//Author: jdfowle
//Date: 12/15/03
//=============================================================================
CubitStatus FacetDataUtil::get_bbox_of_points(DLIList<CubitPoint*>& point_list, CubitBox& bbox)
{
double x, y, z, min[3], max[3];
int i;
CubitPoint *point;
  min[0] = min[1] = min[2] = CUBIT_DBL_MAX;
  max[0] = max[1] = max[2] = -CUBIT_DBL_MAX + 1.;

  for ( i = 0; i < point_list.size(); i++ ) {
    point = point_list.get_and_step();
    x = point->x();
    if ( min[0] > x ) min[0] = x;
    if ( max[0] < x ) max[0] = x;
    y = point->y();
    if ( min[1] > y ) min[1] = y;
    if ( max[1] < y ) max[1] = y;
    z = point->z();
    if ( min[2] > z ) min[2] = z;
    if ( max[2] < z ) max[2] = z;
  }
  bbox.reset(min,max);

  return CUBIT_SUCCESS;
}

//=============================================================================
//Function:  squared_distance_to_segment (PUBLIC)
//Description: get square of distance from point to closest point on a line segment;
//  returns closest point.  Taken from "Geometric Tools for Computer Graphics",
// by Schneider & Eberly, sec. 6.1.
//Author: jdfowle
//Date: 03/08/03
//=============================================================================
CubitVector FacetDataUtil::squared_distance_to_segment(CubitVector &p, CubitVector &p0, 
                                                      CubitVector &p1, double &distance2)
{
CubitVector YmPO, D;
double t;

  D = p1 - p0;
  YmPO = p - p0;
  t = D.x()*YmPO.x() + D.y()*YmPO.y() + D.z()*YmPO.z();
  
  if ( t < 0.0 ) {
    distance2 = YmPO.x()*YmPO.x() + YmPO.y()*YmPO.y() + YmPO.z()*YmPO.z();
    return p0;
  }
  
  double DdD;
  DdD = D.x()*D.x() + D.y()*D.y() + D.z()*D.z();
  if ( t >= DdD ) {
    CubitVector YmP1;
    YmP1 = p - p1;
    distance2 = YmP1.x()*YmP1.x() + YmP1.y()*YmP1.y() + YmP1.z()*YmP1.z();
    return p1;
  }
  
  //  closest point is interior to segment
  distance2 = YmPO.x()*YmPO.x() + YmPO.y()*YmPO.y() + YmPO.z()*YmPO.z() - t*t/DdD;
  return p0 + (t/DdD)*(p1 - p0);
}

//=============================================================================
//Function:  get_bbox_intersections (PUBLIC)
//Description: Get the intersection of the line defined by point1 and point2 with
//bbox.  Returns 0,1 or 2 for the number of intersections.  A line
//in one of the planes of the box will return 0.  Returns -1 for failure.
//Author mborden
//Date: 04/05/07
//=============================================================================
int FacetDataUtil::get_bbox_intersections(CubitVector& point1,
                                          CubitVector& point2,
                                          const CubitBox& bbox,
                                          CubitVector& intersection_1,
                                          CubitVector& intersection_2)
{
  int debug = 0;<--- Assignment 'debug=0', assigned value is 0
  if( debug )<--- Condition 'debug' is always false
  {
    GfxDebug::draw_point( point1, CUBIT_RED_INDEX );
    GfxDebug::draw_point( point2, CUBIT_BLUE_INDEX );
    GfxDebug::flush();
  }
  
  double coords[6];
  coords[0] = bbox.min_x();
  coords[1] = bbox.max_x();
  coords[2] = bbox.min_y();
  coords[3] = bbox.max_y();
  coords[4] = bbox.min_z();
  coords[5] = bbox.max_z();

  DLIList<CubitVector*> intersections;
  
  int ii;
  for( ii = 0; ii < 3; ii++ )
  {
      //Define four points for each plane.
    double box_points[4][3];

      //ii = 0 -> x-planes
      //ii = 1 -> y_planes
      //ii = 2 -> z_planes

      //Only the coordinates for the plane we are in
      //change.  The other two are constant for the
      //jj loops below.
    box_points[0][(ii + 1) % 3] = coords[((ii*2)+2) % 6];
    box_points[1][(ii + 1) % 3] = coords[((ii*2)+3) % 6];
    box_points[2][(ii + 1) % 3] = coords[((ii*2)+3) % 6];
    box_points[3][(ii + 1) % 3] = coords[((ii*2)+2) % 6];

    box_points[0][(ii + 2) % 3] = coords[((ii*2)+4) % 6];
    box_points[1][(ii + 2) % 3] = coords[((ii*2)+4) % 6];
    box_points[2][(ii + 2) % 3] = coords[((ii*2)+5) % 6];
    box_points[3][(ii + 2) % 3] = coords[((ii*2)+5) % 6];
      
    int jj;
    for( jj = 0; jj < 2; jj++ )
    {
      CubitPoint* points[4];
      int kk;
      for( kk = 0; kk < 4; kk++ )
      {
        box_points[kk][ii] = coords[(ii*2)+jj];
        points[kk] = new CubitPointData( box_points[kk][0], box_points[kk][1], box_points[kk][2] );
      }
      
        //Create two facets for this plane to check for intersections.
      CubitFacet* facets[2];
      facets[0] = new CubitFacetData( points[0], points[1], points[3] );
      facets[1] = new CubitFacetData( points[1], points[2], points[3] );

      for( kk = 0; kk < 2; kk++ )
      {
        CubitVector intersection;
        CubitVector area_coord;

          //Make sure the points are not parrellel with the facet.
        CubitVector dir = point2 - point1;
        CubitVector normal = facets[kk]->normal();
        if( fabs(dir % normal) < CUBIT_RESABS )
            continue;
        
        CubitPointContainment contain = intersect_facet( point1, point2, facets[kk],
                                                         intersection, area_coord, CUBIT_FALSE );
        if( CUBIT_PNT_UNKNOWN == contain )
        {
            //The points are in a plane.  Return 0.
          delete facets[0];
          delete facets[1];
          int ll;
          for( ll = 0; ll < 4; ll++ )
              delete points[ll];
          for( ll = intersections.size(); ll > 0; ll-- )
              delete intersections.get_and_step();

          return 0;
        }
        if( CUBIT_PNT_BOUNDARY == contain ||
            CUBIT_PNT_INSIDE == contain )
        {
            //The point intersects the facet so it's inside the box's surface.
          CubitVector* new_intersection = new CubitVector;
          *new_intersection = intersection;
          intersections.append( new_intersection );

          if( debug )
          {
            GfxDebug::draw_point( *new_intersection, CUBIT_CYAN_INDEX );
            GfxDebug::flush();
          }
          
          break;          
        }
      }
      
      delete facets[0];
      delete facets[1];
      for( kk = 0; kk < 4; kk++ )
          delete points[kk];      
    }
  }

    //Check for duplicate intersections.
  intersections.reset();
  for( ii = 0; ii < intersections.size(); ii++ )
  {
    CubitVector* base_vec = intersections.next(ii);
    if( NULL == base_vec )
        continue;
    
    int jj;
    for( jj = ii+1; jj < intersections.size(); jj++ )
    {
      CubitVector* compare_vec = intersections.next(jj);
      if( NULL != compare_vec )
      {
        if( base_vec->distance_between_squared( *compare_vec ) < GEOMETRY_RESABS * GEOMETRY_RESABS )
        {
          intersections.step(jj);
          delete intersections.get();
          intersections.change_to( NULL );
          intersections.reset();
        }
      }
    }
  }
  intersections.remove_all_with_value( NULL );

  
  if( intersections.size() > 2 )
  {
    assert( intersections.size() <= 2 );
    return -1;
  }
  else if( intersections.size() > 0 )
  {
    intersection_1 = *intersections.get();
    if( intersections.size() > 1 )
        intersection_2 = *intersections.next();
  }
  
    //Delete memory.
  for( ii = intersections.size(); ii > 0; ii-- )
      delete intersections.get_and_step();
  
  return intersections.size();
}


//=============================================================================
//Function:  mark_facets (PUBLIC)
//Description: mark facets and their children.  assumes facets have points and edges
//Author sjowen
//Date: 09/18/09
//=============================================================================
void FacetDataUtil::mark_facets( DLIList<FacetEntity *> &facet_list, int mark_value )
{
  int ifacet;
  FacetEntity *facet_ptr;
  for (ifacet = 0; ifacet<facet_list.size(); ifacet++ )
  {
    facet_ptr = facet_list.get_and_step();
    CubitFacet *cfacet_ptr = dynamic_cast<CubitFacet *> (facet_ptr);
    if( cfacet_ptr )
    {
      cfacet_ptr->marked(mark_value);
      for (int ii=0; ii<3; ii++)
      {
        cfacet_ptr->point(ii)->marked(mark_value);
        cfacet_ptr->edge(ii)->marked(mark_value);
      }
    }
    else
    {
      CubitFacetEdge *edge_ptr = dynamic_cast<CubitFacetEdge*>(facet_ptr);
      if( edge_ptr )
      {
        edge_ptr->marked(mark_value);
        for (int ii=0; ii<2; ii++)        
          edge_ptr->point(ii)->marked(mark_value);                  
      }
    }
  }  
}

//==================================================================================
// Description: identifies all the facets that contain given two cubit points
//
//
// Notes:  
// Author: william roshan quadros
// Date: 3/30/2011
//===================================================================================
CubitStatus FacetDataUtil::find_facet( DLIList<CubitFacet *> temp_split_facets, CubitPoint *pnt0, CubitPoint *pnt1, DLIList<CubitFacet *>  &select_facets )
{
  int i;
  CubitFacet *ptr_facet;
  for( i = 0; i < temp_split_facets.size(); i++ )
  {
    ptr_facet = temp_split_facets.get_and_step();

    if( ptr_facet->contains( pnt0 ) && ptr_facet->contains( pnt1 ) )
    {
      select_facets.append( ptr_facet);
    }
  }

  if( select_facets.size() )
    return CUBIT_SUCCESS;
  else
    return CUBIT_FAILURE;
}