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
//-------------------------------------------------------------------------
//- Filename:       CollapseCurveTool
//- Purpose:  To collapse small curves for preparing for mesh
//-      "collapse curve <id> vertex <id>\n",
//-
//- Creator:       Brett Clark
//- Creation date: 01/10/2006
//-------------------------------------------------------------------------

// ********** BEGIN STANDARD INCLUDES         **********
// ********** END STANDARD INCLUDES           **********
                                                                                
// ********** BEGIN MOTIF INCLUDES            **********
// ********** END MOTIF INCLUDES              **********

// ********** BEGIN CUBIT INCLUDES            **********
#include "GMem.hpp"                                                           
#include "RefVertex.hpp"
#include "RefEdge.hpp"
#include "DLIList.hpp"
#include "CoEdge.hpp"
#include "CoFace.hpp"
#include "Shell.hpp"
#include "Point.hpp"
#include "Loop.hpp"
#include "RefFace.hpp"
#include "Body.hpp"
#include "PartitionTool.hpp"
#include "CompositeTool.hpp"
#include "CollapseCurveTool.hpp"
#include "GeometryModifyTool.hpp"
#include "SplitCompositeSurfaceTool.hpp"
#include "CubitUndo.hpp"
#include "CubitMessage.hpp"
//#include "InteropTool.hpp"

/*
*/
// ********** END CUBIT INCLUDES              **********

CollapseCurveTool *CollapseCurveTool::instance_ = NULL;

// ********** BEGIN PUBLIC FUNCTIONS          **********
CollapseCurveTool *CollapseCurveTool::instance()
{
  if (instance_ == NULL) 
    instance_ = new CollapseCurveTool();
                                                                                
  return instance_;
}

CubitStatus CollapseCurveTool::collapse_curve_only_virtual(DLIList <RefEdge*> ref_edge_list, 
                                              DLIList<RefVertex*> ref_vertex_list,
                                              int ignore_surfaces,
                                              double *small_curve_size)
{
  CubitStatus result = CUBIT_SUCCESS;

  RefEdge *edge_to_collapse = ref_edge_list.get();
  RefVertex *keep_vertex = NULL;
  RefVertex *discard_vertex = NULL;
  CoEdge *exit_coedge = NULL;
  CoEdge *enter_coedge = NULL;
  DLIList<RefEdge*> curves_to_partition;
  DLIList<RefFace*> surfaces_to_partition;
  DLIList<CubitVector> partition_positions;
  DLIList<CubitVector> keep_vecs;
  DLIList<CubitVector> partition_curve_vecs;
  DLIList<RefFace*> adjacent_surfaces;
  DLIList<double> angles;
  DLIList<double> arc_lengths;
  DLIList<RefEdge*> ref_edges_on_discard_vertex;
  DLIList<RefEdge*> ref_edges_on_keep_vertex;
  DLIList<RefVertex*> new_partition_vertices;
  DLIList<RefEdge*> new_partition_edges;
  RefEdge *close_edge = NULL;
  RefEdge *far_edge = NULL;
  DLIList<RefEdge*> edges_to_composite;
  int keep_vertex_specified = 0;
  int discard_valency = 0;<--- Variable 'discard_valency' is assigned a value that is never used.
  int keep_valency = 0;<--- Variable 'keep_valency' is assigned a value that is never used.
  int finished = 0;
  double arc_length = 0;<--- Variable 'arc_length' is assigned a value that is never used.
  double u = 0, v = 0;
  CubitVector left_vec(0,0,0), right_vec(0,0,0);<--- Shadowed declaration<--- Shadowed declaration
  CubitVector position_on_left_curve(0,0,0), position_on_right_curve(0,0,0);
  CubitVector discard_pos(0,0,0), keep_pos(0,0,0);

  bool undo_state = CubitUndo::get_undo_enabled();
  if( undo_state )
      CubitUndo::save_state_with_cubit_file( ref_edge_list );
  CubitUndo::set_undo_enabled(false);

  if(edge_to_collapse == NULL)
  {
    PRINT_ERROR("No curve was found to collapse.\n");
    finished = 1;
    result = CUBIT_FAILURE;
  }

  if(!finished)
  {
    // Make sure we have a vertex to keep.
    if(ref_vertex_list.size() > 0)
    {
      keep_vertex_specified = 1;
      keep_vertex = ref_vertex_list.get();
    }
    // We need to choose a vertex to keep.
    else
    {
      // Arbitrarily choose start vertex.
      keep_vertex = edge_to_collapse->start_vertex();
    }

    // Get the vertex that will go away.
    if(keep_vertex == edge_to_collapse->start_vertex())
    {
      discard_vertex = edge_to_collapse->end_vertex();
    }
    else if(keep_vertex == edge_to_collapse->end_vertex())
    {
      discard_vertex = edge_to_collapse->start_vertex();
    }
    else
    {
      PRINT_ERROR("Vertex to keep was not found on the curve being collapsed.\n");
      result = CUBIT_FAILURE;
      finished = 1;
    }
  }
  
  if(!finished)
  {
    // Get the valency of the keep and discard vertices.
    discard_vertex->ref_edges(ref_edges_on_discard_vertex);
    keep_vertex->ref_edges(ref_edges_on_keep_vertex);
    discard_valency = ref_edges_on_discard_vertex.size();
    keep_valency = ref_edges_on_keep_vertex.size();

    // Now do some error checking and also some logic to try to 
    // pick the best vertex to keep/discard.

    // If one of the valencies is 2 just composite the vertex out.
    if(discard_valency == 2)
    {
      CompositeTool::instance()->composite(ref_edges_on_discard_vertex);
      finished = 1;
    }
    else if (keep_valency == 2)
    {
      CompositeTool::instance()->composite(ref_edges_on_keep_vertex);
      finished = 1;
    }
    else
    {
      // Make sure that at least one of the vertices is qualified to
      // be the discard vertex (valency of 3 or 4).  The keep vertex
      // really doesn't have any restrictions.
      if((discard_valency > 4 || discard_valency < 3) &&
        (keep_valency > 4 || keep_valency < 3))
      {
        PRINT_ERROR("Cannot currently collapse curves where one of the vertices does not have a valency equal to 3 or 4.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
      else
      {
        if(keep_vertex_specified)
        {
          if(discard_valency < 3 || discard_valency > 4)
          {
            PRINT_ERROR("Cannot currently collapse curves where the discard vertex valency is not 3 or 4.\n"
                  "Try specifying the keep vertex so that the discard vertex is 3 or 4.\n");
            result = CUBIT_FAILURE;
            finished = 1;
          }
        }
        else
        {
          // The user didn't specify a keep vertex so we can try to choose the best one.
          
          int swap_vertices = 0;
          if(discard_valency < 5 && discard_valency > 2 &&
            keep_valency < 5 && keep_valency > 2)
          {
            // Either vertex can be the discard/keep vertex so choose the one with the
            // lower valency as the discard vertex because it will require fewer operations.
            if(discard_valency > keep_valency)
            {
              swap_vertices = 1;
            }
          }
          else
          {
            // Only one of the vertices can be the discard vertex so pick it.
            if(discard_valency > 4 || discard_valency < 3)
            {
              swap_vertices = 1;
            }
          }

          if(swap_vertices)
          {
            // Swap the vertices.
            RefVertex *tmp = discard_vertex;
            discard_vertex = keep_vertex;
            keep_vertex = tmp;

            // Make sure to refresh the discard vertex edge list because
            // it is used below.
            ref_edges_on_discard_vertex.clean_out();
            discard_vertex->ref_edges(ref_edges_on_discard_vertex);
          }
        }
      }
    }
  }

  if(!finished && small_curve_size)
  {
    // Check if the edges attached to the discard vertex are longer 
    // than the small curve size.
    DLIList<RefEdge*> tmp_edges;
    discard_vertex->ref_edges(tmp_edges);
    if(tmp_edges.move_to(edge_to_collapse))
      tmp_edges.extract();
    int y;
    bool all_edges_long_enough = true;
    for(y=tmp_edges.size(); y && all_edges_long_enough; y--)
    {
      RefEdge *cur_edge = tmp_edges.get_and_step();
      if(cur_edge->get_arc_length() <= *small_curve_size)
        all_edges_long_enough = false;
    }
    if(!all_edges_long_enough)
    {
      PRINT_ERROR("Cannot collapse curve %d to vertex %d--not all of the curves attached to\n"
            "vertex %d are longer than the small_curve_size.  Try collapsing to vertex %d instead.\n",
            edge_to_collapse->id(), keep_vertex->id(), discard_vertex->id(), discard_vertex->id());
      result = CUBIT_FAILURE;
      finished = 1;
    }
  }

  // Look at all of the coedges on the curve being collapsed and
  // throw out any that are on nonmanifold surfaces.  The collapse
  // curve may be on a boundary of a merged surface.  This is ok
  // but we want to make sure we don't involve this surface
  // in the partitions and composites so we will remove from the
  // list any coedges that are hooked to merged surfaces.
  if(!finished)
  {
    DLIList<CoEdge*> collapse_curve_coedges;
    edge_to_collapse->get_co_edges(collapse_curve_coedges);

    int initial_size = collapse_curve_coedges.size();
    while(collapse_curve_coedges.size() > 2 && initial_size > 0)
    {
      for(int g=collapse_curve_coedges.size(); g--;)
      {
        CoEdge *cur_coedge = collapse_curve_coedges.get_and_step();
        Loop *loop_ptr = cur_coedge->get_loop_ptr();
        if(loop_ptr)
        {
          RefFace *ref_face_ptr = loop_ptr->get_ref_face_ptr();
          if(ref_face_ptr)
          {
            DLIList<CoFace*> coface_list;
            ref_face_ptr->co_faces(coface_list);
            if(coface_list.size() > 1)
            {
              collapse_curve_coedges.remove(cur_coedge);
              g = 0;
            }
          }
        }
      }

      // Keep from looping infinitely.
      initial_size--;
    }

    // If we get to this point and have more than 2 coedges left
    // in the list we are not sure what to do.
    if(collapse_curve_coedges.size() != 2)
    {
      PRINT_ERROR("Currently can only collapse curves with manifold topology.\n");
      result = CUBIT_FAILURE;
      finished = 1;
    }
    else
    {
      // Get the collapse curve coedges entering and leaving the discard vertex.
      exit_coedge = collapse_curve_coedges.get_and_step();
      enter_coedge = collapse_curve_coedges.get();
      if(enter_coedge->end_vertex() != discard_vertex)
      {
        CoEdge *tmp = exit_coedge;
        exit_coedge = enter_coedge;
        enter_coedge = tmp;
      }
    }
  }

  // Next we need to explore the topology around the discard vertex
  // so that we can get the edges and surfaces that will be involved
  // with the partitioning and compositing.  We will identify a "left"
  // and "right" edge coming out of the discard vertex and adjacent
  // surfacs to these edges.
  if(!finished)
  {
    // Get these values for use later on.
    discard_pos = discard_vertex->get_point_ptr()->coordinates();
    keep_pos = keep_vertex->get_point_ptr()->coordinates();
    CubitVector discard_to_keep = keep_pos - discard_pos;
    arc_length = edge_to_collapse->get_arc_length();

    // Depending on whether the discard vertex has valency of 3 or 4 we will
    // either partition 1 or 2 of the curves coming into the discard vertex
    // respectively.  Set up lists so that below we can just loop through the
    // partitioning and compositing for either the 3 or 4 valency case.

    // "Left" and "right" will be defined as if you were standing on the
    // keep vertex looking at the discard vertex.
    Loop *left_loop = enter_coedge->get_loop_ptr();
    Loop *right_loop = exit_coedge->get_loop_ptr();
    DLIList<SenseEntity*> left_sense_entities, right_sense_entities;
    left_loop->get_sense_entity_list(left_sense_entities);
    right_loop->get_sense_entity_list(right_sense_entities);

    // We need to get these two coedges because the chains defined by the "next" and 
    // "previous" pointers in the CoEdge objects are not circular (you can have a NULL
    // "previous" or "next" pointer).  We will manage the circularity manually.
    CoEdge *left_start = dynamic_cast<CoEdge*>(left_loop->get_first_sense_entity_ptr()); 
    CoEdge *right_end = dynamic_cast<CoEdge*>(right_loop->get_last_sense_entity_ptr()); 
    
    CoEdge *left_coedge = dynamic_cast<CoEdge*>(enter_coedge->next());
    if(left_coedge == NULL)
    {
      left_coedge = left_start;
    }
    CoEdge *right_coedge = dynamic_cast<CoEdge*>(exit_coedge->previous());
    if(right_coedge == NULL)
    {
      right_coedge = right_end;
    }

    RefEdge *left_edge = left_coedge->get_ref_edge_ptr();
    RefEdge *right_edge = right_coedge->get_ref_edge_ptr();
    RefFace *left_common_face = left_edge->common_ref_face(edge_to_collapse);
    RefFace *right_common_face = right_edge->common_ref_face(edge_to_collapse);

    double left_arc_length = left_edge->get_arc_length();
    double right_arc_length = right_edge->get_arc_length();
    int left_ok = 1;
    int right_ok = 1;

    DLIList<RefEdge*> left_face_edges;
    DLIList<RefEdge*> right_face_edges;
    left_common_face->ref_edges(left_face_edges);
    right_common_face->ref_edges(right_face_edges);
    if(left_face_edges.size() < 3 || right_face_edges.size() < 3 ||
      left_sense_entities.size() < 3 || right_sense_entities.size() < 3)
    {
      PRINT_ERROR("Cannot collapse a curve that bounds a face or loop with only two edges.\n");
      finished = 1;
    }
    else
    {
      // We use the length of the curve being collapsed as the distance
      // to partition the adjacent curves connected to it.  If the
      // adjacent curves are shorter than the curve being collapsed we
      // will bail because the user should probably be getting rid
      // of the adjacent edges instead or at least first.

      // Get the length of the adjacent curves.
      
      // If the adjacent curve is within resabs of the length of 
      // the curve being collapsed we will just snap to the end
      // of the adjacent curve for our partition position.
      if(arc_length > left_arc_length + GEOMETRY_RESABS)
      {
        left_ok = 0;
      }
      if(arc_length > right_arc_length + GEOMETRY_RESABS)
      {
        right_ok = 0;
      }

      // If neither curve is ok we need to bail.
      if(!left_ok && !right_ok)
      {
        PRINT_ERROR("Curve being collapsed is too long compared to adjacent curves.\n");
        finished = 1;
      }
    }

    // If it looks like the lengths of the adjacent curves are
    // ok we will go ahead and try to find a partition position
    // on them.
    if(!finished)
    {
      if(left_ok)
      {
         // First see if we can just use the end point of the
         // adjacent curve as the partition position.
         if(fabs(left_arc_length-arc_length) < GEOMETRY_RESABS)
         {
           if(left_edge->start_vertex() == discard_vertex)
             position_on_left_curve = left_edge->end_vertex()->coordinates();
           else
             position_on_left_curve = left_edge->start_vertex()->coordinates();
         }
         else
         {
           result = this->position_from_length(left_edge, discard_vertex,
                arc_length, position_on_left_curve);
         }
      }
      if(result == CUBIT_SUCCESS && right_ok)
      {
         // First see if we can just use the end point of the
         // adjacent curve as the partition position.
         if(fabs(right_arc_length-arc_length) < GEOMETRY_RESABS)
         {
           if(right_edge->start_vertex() == discard_vertex)
             position_on_right_curve = right_edge->end_vertex()->coordinates();
           else
             position_on_right_curve = right_edge->start_vertex()->coordinates();
         }
         else
         {
            result = this->position_from_length(right_edge, discard_vertex,
                  arc_length, position_on_right_curve);
         }
      }
      if(result == CUBIT_FAILURE)
      {
        PRINT_ERROR("Was unable to locate appropriate partition points on curves adjacent to curve being collapased.\n");
        finished = 1;
      }
    }
      
    if(!finished)
    {
      // Get the vectors from the discard vertex to the potential partition locations.
      CubitVector left_vec = position_on_left_curve - discard_pos;<--- Shadow variable
      CubitVector right_vec = position_on_right_curve - discard_pos;<--- Shadow variable

      // Calculate the angles between the left/right edge and the
      // edge being collapsed.  I am doing it this way rather than just
      // calling RefEdge::angle_between() because I want the angle
      // calculation done out at the potential partition location.
      // This will step over any small kinks in the curve near the
      // discard vertex that might otherwise give misleading angles
      // that don't represent what is happening out by the potential
      // partition position.
      left_common_face->u_v_from_position(discard_pos, u, v);
      CubitVector left_normal = left_common_face->normal_at(discard_pos, NULL, &u, &v);
      double left_angle = left_normal.vector_angle(left_vec, discard_to_keep);

      right_common_face->u_v_from_position(discard_pos, u, v);
      CubitVector right_normal = right_common_face->normal_at(discard_pos, NULL, &u, &v);
      double right_angle = right_normal.vector_angle(discard_to_keep, right_vec);

      // 3 valency case: 
      // We only need to partition one of the curves (left or right) and
      // the corresponding surface.
      if(ref_edges_on_discard_vertex.size() == 3)
      {
        int use_left = 0;
        // We can use either adjacent curve.
        if(left_ok && right_ok)
        {
          // Choose the side with the smaller angle as this will in general
          // give better angles for doing the partitioning on the surface.
          if(left_angle < right_angle)
          {
            use_left = 1;
          }
        }
        else if(left_ok)
        {
          use_left = 1;
        }
        if(use_left)
        {
          curves_to_partition.append(left_edge);
          surfaces_to_partition.append(left_common_face);
          angles.append(left_angle);
          partition_positions.append(position_on_left_curve);
          arc_lengths.append(arc_length);
 
          // These vectors will be used in calculating a bisector direction
          // below if necessary.
          keep_vecs.append(-discard_to_keep);
          partition_curve_vecs.append(left_vec);
        }
        else
        {
          curves_to_partition.append(right_edge);
          surfaces_to_partition.append(right_common_face);
          angles.append(right_angle);
          partition_positions.append(position_on_right_curve);
          arc_lengths.append(arc_length);
 
          // These vectors will be used in calculating a bisector direction
          // below if necessary.
          keep_vecs.append(discard_to_keep);
          partition_curve_vecs.append(-right_vec);
        }
      }
      else if(ref_edges_on_discard_vertex.size() == 4)
      {
        // We have to partition both left and right curves so make
        // sure we can.
        if(!left_ok || !right_ok)
        {
          PRINT_ERROR("One of the curves adjacent to the collapse curve is not long enough for the collapse operation.\n");
          finished = 1;
        }
        else
        {
          // Both curves (and surfaces) adjacent to the collapse curve (left and right)
          // will need to be partitioned so add them to the lists.

          curves_to_partition.append(left_edge);
          curves_to_partition.append(right_edge);
          surfaces_to_partition.append(left_common_face);
          surfaces_to_partition.append(right_common_face);
          angles.append(left_angle);
          angles.append(right_angle);
          keep_vecs.append(-discard_to_keep);
          keep_vecs.append(discard_to_keep);
          partition_curve_vecs.append(left_vec);
          partition_curve_vecs.append(-right_vec);
          partition_positions.append(position_on_left_curve);
          partition_positions.append(position_on_right_curve);
          arc_lengths.append(arc_length);
          arc_lengths.append(arc_length);
        }
      }
      else
      {
        PRINT_ERROR("Currently can only collapse curves with 3 or 4 valency vertices.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
    }
  }

  if(!finished)
  {
    int num_reps = curves_to_partition.size();
    curves_to_partition.reset();
    surfaces_to_partition.reset();
    for(int n=0; n<num_reps && !finished; ++n)
    {
      RefEdge *side_edge = curves_to_partition.get_and_step();
      RefFace *side_face = surfaces_to_partition.get_and_step();

      // Now we need to find the face on the other side of the edge.
      // Because there may be edges on the boundaries of merged surfaces
      // we want to find the
      // face on the left/right edge that isn't the face shared by
      // the collapse edge and isn't nonmanifold.
      DLIList<CoEdge*> side_edge_coedges;
      side_edge->co_edges(side_edge_coedges);
      DLIList<RefFace*> possible_adj_faces;

      // First loop through and get all of the potential faces.
      for(int r=side_edge_coedges.size(); r--;)
      {
        CoEdge *cur_coedge = side_edge_coedges.get_and_step();
        if(cur_coedge &&
          cur_coedge->get_loop_ptr() &&
          cur_coedge->get_loop_ptr()->get_ref_face_ptr())
        {
          RefFace *cur_ref_face = cur_coedge->get_loop_ptr()->get_ref_face_ptr();
          if(cur_ref_face != side_face)
          {
            DLIList<CoFace*> coface_list;
            cur_ref_face->co_faces(coface_list);

            // Along with checking for whether the face is manifold we need
            // to check if it belongs to the same volume as the side face.
            // We have to check this because we can't composite faces
            // from different volumes and these faces will be involved in
            // a composite below.
            if(coface_list.size() == 1 &&
              side_face->ref_volume() == cur_ref_face->ref_volume())
            {
              possible_adj_faces.append(cur_ref_face);
            }
          }
        }
      }

      // If we ended up with more than one face in the list it isn't clear
      // what we should do so bail out.
      if(possible_adj_faces.size() != 1)
      {
        PRINT_ERROR("Couldn't figure out how to perform the collapse curve with the current topology.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
      else
      {
        adjacent_surfaces.append(possible_adj_faces.get());
      }
    }
  }

  if(!finished)
  {
    // At this point we should know which curves and surfaces we need
    // to partition.

    int num_reps = curves_to_partition.size();
    curves_to_partition.reset();
    surfaces_to_partition.reset();
    adjacent_surfaces.reset();
    angles.reset();
    keep_vecs.reset();
    partition_curve_vecs.reset();
    partition_positions.reset();
    arc_lengths.reset();

    for(int i=0; i<num_reps && !finished; ++i)
    {
      RefEdge *curve_to_partition = curves_to_partition.get_and_step();

      // As we process each curve remove it from the list so that
      // at the end we can take the last one in the list and composite
      // it with the curve being collapsed.
      ref_edges_on_discard_vertex.remove(curve_to_partition);

      CubitVector position_on_curve = partition_positions.get_and_step();

      // Partition the curve adjacent to the collapse curve.
      RefEdge *edge1, *edge2;
      RefVertex *new_vertex = PartitionTool::instance()->
        partition( curve_to_partition, position_on_curve, edge1, edge2 );

      // Keep track of these in case we need to undo the partitioning.
      if(new_vertex)
      {
        new_partition_vertices.append(new_vertex);
      }

      // Get the two new curves and classify them as close and far.
      if(edge1 && edge2)
      {
        close_edge = edge1;
        far_edge = edge2;
        if(close_edge->start_vertex() != discard_vertex &&
          close_edge->end_vertex() != discard_vertex)
        {
          RefEdge *tmp = close_edge;
          close_edge = far_edge;
          far_edge = tmp;
        }
      }
      else
      {
        // If the partition didn't create a new vertex and two new curves
        // (the case when the partition position lands on an existing
        // vertex) just classify the curve as "close" and set the 
        // "far" curve to NULL. 
        close_edge = curve_to_partition;
        far_edge = NULL;
      }

      RefFace *surface_to_partition = surfaces_to_partition.get_and_step();

      DLIList<CubitVector*> positions;

      // Add the point on the curve we just partitioned.  The other
      // point we normally need to add is the keep_vertex.  However,
      // if the angle between the collapse curve and the curve we 
      // just partitioned dictates that we need to introduce more points 
      // (this would be cases where the angle is large and just partitioning the
      // surface with one curve--two points--would result in a very skinny
      // surface) we need to add them before adding the keep_vertex.  Therefore, check
      // that now and add the extra points if needed.
      positions.append(new CubitVector(position_on_curve));

      double cur_angle = angles.get_and_step();
      arc_length = arc_lengths.get_and_step();

      // These vectors are only used in the block below if we need to
      // add extra points but we need to always retrieve them from the lists
      // so that the pointer in the list stays in sync with the "for" loop.
      CubitVector keep_vec = keep_vecs.get_and_step();
      CubitVector partition_vec = partition_curve_vecs.get_and_step();

      // Greater than 3*PI/2--add 3 interior points.  Two of these
      // points will be generated by projecting from the discard vertex
      // into the surface normal to the vector from the discard vertex
      // to the keep point and new partition point respectively.  The third
      // point will be obtained by projecting from the discard point in the
      // direction of the bisector angle of the two previous projections.
      if(cur_angle > 4.71)
      {
        // Get the u,v position of the discard vertex on this surface.
        surface_to_partition->u_v_from_position(discard_pos, u, v);
        
        // Get the normal at the discard vertex.
        CubitVector normal = surface_to_partition->normal_at(discard_pos, NULL, &u, &v);
        normal.normalize();
        
        // We need to calculate a bisector direction between the
        // collape edge and the edge we just partitioned.  We will do 
        // this using the face normal at the discard vertex and the vectors 
        // we calculated previously with the partition points.  Cross
        // the face normal into the the direction vectors at the 
        // discard and partition points to get two vectors pointing
        // into the face and then average those two to get the
        // bisector direction.  This is all approximate but should 
        // be sufficient for locating another point for partitioning 
        // the surface.

        // I am not normalizing the result here because they should
        // be roughly the same length and it shouldn't affect 
        // the average too much.
        CubitVector vec1 = normal * partition_vec;
        CubitVector vec2 = normal * keep_vec;

        // Get the bisector direction.
        CubitVector bisector_dir = vec1 + vec2;
        bisector_dir.normalize();

        // Now normalise these because they will be used to
        // project two of the new interior points.
        vec1.normalize();
        vec2.normalize();

        CubitVector new_pos1 = discard_pos + (arc_length*vec1);
        CubitVector mid_pos = discard_pos + (arc_length * bisector_dir);
        CubitVector new_pos2 = discard_pos + (arc_length*vec2);

        // Use the u,v from the discard vertex because it is fairly
        // close to the new position and will at least provide a
        // meaningful starting point.
        double save_u = u, save_v = v;
        surface_to_partition->move_to_surface(new_pos1, &u, &v);
        u = save_u; v = save_v;
        surface_to_partition->move_to_surface(mid_pos, &u, &v);
        u = save_u; v = save_v;
        surface_to_partition->move_to_surface(new_pos2, &u, &v);

        // Add the new position to the list of partition points.
        positions.append(new CubitVector(new_pos1));
        positions.append(new CubitVector(mid_pos));
        positions.append(new CubitVector(new_pos2));
      }
      // Greater than 3*PI/4 and less than 3*PI/2--add one interior point
      else if(cur_angle > 2.4)
      {
        CubitVector third_pt;
        
        // Get the u,v position of the discard vertex on this surface.
        surface_to_partition->u_v_from_position(discard_pos, u, v);
        
        // Get the normal at the discard vertex.
        CubitVector normal = surface_to_partition->normal_at(discard_pos, NULL, &u, &v);
        normal.normalize();
        
        // We need to calculate a bisector direction between the
        // collape edge and the edge we just partitioned.  We will do 
        // this using the face normal at the discard vertex and the vectors 
        // we calculated previously with the partition points.  Cross
        // the face normal into the the direction vectors at the 
        // discard and partition points to get two vectors pointing
        // into the face and then average those two to get the
        // bisector direction.  This is all approximate but should 
        // be sufficient for locating another point for partitioning 
        // the surface.

        // I am not normalizing the result here because they should
        // be roughly the same length and it shouldn't affect 
        // the average too much.
        CubitVector vec1 = normal * keep_vec;
        CubitVector vec2 = normal * partition_vec;

        // Get the bisector direction.
        CubitVector bisector_dir = vec1 + vec2;
        bisector_dir.normalize();

        // Project from the discard vertex in the direction of the
        // bisector direction to get a new point for partitioning
        // the surface.
        CubitVector new_pos = discard_pos + (arc_length * bisector_dir);

        // Use the u,v from the discard vertex because it is fairly
        // close to the new position and will at least provide a
        // meaningful starting point.
        surface_to_partition->move_to_surface(new_pos, &u, &v);

        // Add the new position to the list of partition points.
        positions.append(new CubitVector(new_pos));
      }

      // Finally, add the keep_vertex to the list.
      positions.append(new CubitVector(keep_vertex->get_point_ptr()->coordinates()));
                                                                                    
      DLIList<RefEdge*> new_edges;
      PartitionTool::instance()->
          insert_edge( surface_to_partition, positions, CUBIT_FALSE, new_edges,
                        0, &arc_length);

      // Keep for later in case we need to clean up.
      if(new_edges.size() > 0)
      {
        new_partition_edges += new_edges;<--- Variable 'new_partition_edges' is assigned a value that is never used.
      }

      delete positions.get_and_step();
      delete positions.get_and_step();

      if(cur_angle > 4.71)
      {
        delete positions.get_and_step();
        delete positions.get_and_step();
        delete positions.get_and_step();
      }
      else if(cur_angle > 2.4)
      {
        delete positions.get();
      }

      RefFace *new_small_face = edge_to_collapse->common_ref_face(close_edge);

      if(!new_small_face)
      {
        PRINT_ERROR("Failed to do the partition surface operation of the collapse curve.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
      else
      {
        DLIList<RefFace*> result_faces;
        DLIList<RefFace*> faces_to_composite;

        // Used below if "ignore" keyword was specified.
        int new_small_face_id = new_small_face->id();

        faces_to_composite.append(new_small_face);
        faces_to_composite.append(adjacent_surfaces.get_and_step());

        RefFace *new_comp_face = CompositeTool::instance()->composite(faces_to_composite);

        CompositeSurface* csurf = NULL;
        
        if(new_comp_face)
        {
          csurf = dynamic_cast<CompositeSurface*>(new_comp_face->get_surface_ptr());
        }

        if(!new_comp_face || !csurf)
        {
          PRINT_ERROR("Failed to do the composite surface operation of the collapse curve.\n");
          result = CUBIT_FAILURE;
          finished = 1;
        }
        else
        {
          if(ignore_surfaces)
          {
            csurf->ignore_surface(new_small_face_id);
          }

          if(far_edge)
          {
            for(int k=new_edges.size(); k--;)
            {
              edges_to_composite.append(new_edges.get_and_step());
            }
            edges_to_composite.append(far_edge);

            if(edges_to_composite.size() > 1)
            {
              CompositeTool::instance()->composite(edges_to_composite);
            }
          }

          edges_to_composite.clean_out();
        }
      }
    }

    if(!finished)
    {
      ref_edges_on_discard_vertex.remove(edge_to_collapse);

      // Now there should only be one edge in the ref_edges_on_discard_vertex
      // list.  It should be the edge that hasn't had any modifications
      // done to it.  We finally want to composite it with the edge
      // being collapsed.
      if(ref_edges_on_discard_vertex.size() != 1)
      {
        PRINT_ERROR("Wasn't able to complete collapse operation.\n");
        result = CUBIT_FAILURE;
        finished = 1;<--- Variable 'finished' is assigned a value that is never used.
      }
      else
      {
        edges_to_composite.append(ref_edges_on_discard_vertex.get());
        edges_to_composite.append(edge_to_collapse);
        CompositeTool::instance()->composite(edges_to_composite);
      }
    }
  }

  CubitUndo::set_undo_enabled(undo_state);

  return result;
}

CubitStatus CollapseCurveTool::collapse_curve(DLIList <RefEdge*> ref_edge_list, 
                                              DLIList<RefVertex*> ref_vertex_list,
                                              int ignore_surfaces,
                                              double *small_curve_size)
{
  CubitStatus result = CUBIT_SUCCESS;

  RefEdge *edge_to_collapse = ref_edge_list.get();
  RefVertex *keep_vertex = NULL;
  RefVertex *discard_vertex = NULL;
  CoEdge *exit_coedge = NULL;
  CoEdge *enter_coedge = NULL;
  DLIList<RefEdge*> curves_to_partition;
  DLIList<RefFace*> surfaces_to_partition;
  DLIList<CubitVector> partition_positions;
  DLIList<CubitVector> keep_vecs;
  DLIList<CubitVector> partition_curve_vecs;
  DLIList<RefFace*> adjacent_surfaces;
  DLIList<double> angles;
  DLIList<double> arc_lengths;
  DLIList<RefEdge*> ref_edges_on_discard_vertex;
  DLIList<RefEdge*> ref_edges_on_keep_vertex;
  DLIList<RefVertex*> new_partition_vertices;
  DLIList<RefEdge*> new_partition_edges;
  RefEdge *close_edge = NULL;
  RefEdge *far_edge = NULL;
  DLIList<RefEdge*> edges_to_composite;
  int keep_vertex_specified = 0;
  int discard_valency = 0;<--- Variable 'discard_valency' is assigned a value that is never used.
  int keep_valency = 0;<--- Variable 'keep_valency' is assigned a value that is never used.
  int finished = 0;
  double arc_length = 0;<--- Variable 'arc_length' is assigned a value that is never used.
  double u = 0, v = 0;
  CubitVector left_vec(0,0,0), right_vec(0,0,0);<--- Shadowed declaration<--- Shadowed declaration
  CubitVector position_on_left_curve(0,0,0), position_on_right_curve(0,0,0);
  CubitVector discard_pos(0,0,0), keep_pos(0,0,0);
  Body *the_body;

  bool undo_state = CubitUndo::get_undo_enabled();
  if( undo_state )
      CubitUndo::save_state_with_cubit_file( ref_edge_list );
  CubitUndo::set_undo_enabled(false);

  if(edge_to_collapse == NULL)<--- Assuming that condition 'edge_to_collapse==NULL' is not redundant<--- Assuming that condition 'edge_to_collapse==NULL' is not redundant
  {
    PRINT_ERROR("No curve was found to collapse.\n");
    finished = 1;
    result = CUBIT_FAILURE;
  }

  if(edge_to_collapse->is_merged())<--- Null pointer dereference
  {
    PRINT_ERROR("Cannot collapse a merged curve.\n");
    finished = 1;
    result = CUBIT_FAILURE;
  }

  if(edge_to_collapse->start_vertex()->is_merged() &&<--- Null pointer dereference
    edge_to_collapse->end_vertex()->is_merged())
  {
    PRINT_ERROR("Cannot collapse a curve where both vertices are merged.\n");
    finished = 1;
    result = CUBIT_FAILURE;
  }
  else if(edge_to_collapse->start_vertex()->is_merged())
  {
    if(ref_vertex_list.size() > 0)
    {
      if(ref_vertex_list.get() == edge_to_collapse->end_vertex())
      {
        PRINT_ERROR("Specified vertex to collapse to is merged--try using other vertex.\n");
        finished = 1;
        result = CUBIT_FAILURE;
      }
    }
    else
      ref_vertex_list.append(edge_to_collapse->start_vertex());
  }
  else if(edge_to_collapse->end_vertex()->is_merged())
  {
    if(ref_vertex_list.size() > 0)
    {
      if(ref_vertex_list.get() == edge_to_collapse->start_vertex())
      {
        PRINT_ERROR("Specified vertex to collapse to is merged--try using other vertex.\n");
        finished = 1;
        result = CUBIT_FAILURE;
      }
    }
    else
      ref_vertex_list.append(edge_to_collapse->end_vertex());
  }

  if(!finished)
  {
    the_body = edge_to_collapse->body();

    // Make sure we have a vertex to keep.
    if(ref_vertex_list.size() > 0)
    {
      keep_vertex_specified = 1;
      keep_vertex = ref_vertex_list.get();
    }
    // We need to choose a vertex to keep.
    else
    {
      // Arbitrarily choose start vertex.
      keep_vertex = edge_to_collapse->start_vertex();
    }

    // Get the vertex that will go away.
    if(keep_vertex == edge_to_collapse->start_vertex())
    {
      discard_vertex = edge_to_collapse->end_vertex();
    }
    else if(keep_vertex == edge_to_collapse->end_vertex())
    {
      discard_vertex = edge_to_collapse->start_vertex();
    }
    else
    {
      PRINT_ERROR("Vertex to keep was not found on the curve being collapsed.\n");
      result = CUBIT_FAILURE;
      finished = 1;
    }
  }

  if(!finished)
  {
    // Get the valency of the keep and discard vertices.
    discard_vertex->ref_edges(ref_edges_on_discard_vertex);
    keep_vertex->ref_edges(ref_edges_on_keep_vertex);
    discard_valency = ref_edges_on_discard_vertex.size();
    keep_valency = ref_edges_on_keep_vertex.size();

    // Now do some error checking and also some logic to try to 
    // pick the best vertex to keep/discard.

    // If one of the valencies is 2 just composite the vertex out.
    if(discard_valency == 2)
    {
      CompositeTool::instance()->composite(ref_edges_on_discard_vertex);
      finished = 1;
    }
    else if (keep_valency == 2)
    {
      CompositeTool::instance()->composite(ref_edges_on_keep_vertex);
      finished = 1;
    }
    else
    {
      // Make sure that at least one of the vertices is qualified to
      // be the discard vertex (valency of 3 or 4).  The keep vertex
      // really doesn't have any restrictions.
      if((discard_valency > 4 || discard_valency < 3) &&
        (keep_valency > 4 || keep_valency < 3))
      {
        PRINT_ERROR("Cannot currently collapse curves where one of the vertices does not have a valency equal to 3 or 4.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
      else
      {
        if(keep_vertex_specified)
        {
          if(discard_valency < 3 || discard_valency > 4)
          {
            PRINT_ERROR("Cannot currently collapse curves where the discard vertex valency is not 3 or 4.\n"
                  "Try specifying the keep vertex so that the discard vertex is 3 or 4.\n");
            result = CUBIT_FAILURE;
            finished = 1;
          }
        }
        else
        {
          // The user didn't specify a keep vertex so we can try to choose the best one.
          
          int swap_vertices = 0;
          if(discard_valency < 5 && discard_valency > 2 &&
            keep_valency < 5 && keep_valency > 2)
          {
            // Either vertex can be the discard/keep vertex so choose the one with the
            // lower valency as the discard vertex because it will require fewer operations.
            if(discard_valency > keep_valency)
            {
              swap_vertices = 1;
            }
          }
          else
          {
            // Only one of the vertices can be the discard vertex so pick it.
            if(discard_valency > 4 || discard_valency < 3)
            {
              swap_vertices = 1;
            }
          }

          if(swap_vertices)
          {
            // Swap the vertices.
            RefVertex *tmp = discard_vertex;
            discard_vertex = keep_vertex;
            keep_vertex = tmp;

            // Make sure to refresh the discard vertex edge list because
            // it is used below.
            ref_edges_on_discard_vertex.clean_out();
            discard_vertex->ref_edges(ref_edges_on_discard_vertex);
          }
        }
      }
    }
  }

  if(!finished && small_curve_size)
  {
    // Check if the edges attached to the discard vertex are longer 
    // than the small curve size.
    DLIList<RefEdge*> tmp_edges;
    discard_vertex->ref_edges(tmp_edges);
    if(tmp_edges.move_to(edge_to_collapse))
      tmp_edges.extract();
    int y;
    bool all_edges_long_enough = true;
    for(y=tmp_edges.size(); y && all_edges_long_enough; y--)
    {
      RefEdge *cur_edge = tmp_edges.get_and_step();
      if(cur_edge->get_arc_length() <= *small_curve_size)
        all_edges_long_enough = false;
    }
    if(!all_edges_long_enough)
    {
      PRINT_ERROR("Cannot collapse curve %d to vertex %d--not all of the curves attached to\n"
            "vertex %d are longer than the small_curve_size.  Try collapsing to vertex %d instead.\n",
            edge_to_collapse->id(), keep_vertex->id(), discard_vertex->id(), discard_vertex->id());
      result = CUBIT_FAILURE;
      finished = 1;
    }
  }

  // Look at all of the coedges on the curve being collapsed and
  // throw out any that are on nonmanifold surfaces.  The collapse
  // curve may be on a boundary of a merged surface.  This is ok
  // but we want to make sure we don't involve this surface
  // in the partitions and composites so we will remove from the
  // list any coedges that are hooked to merged surfaces.
  if(!finished)
  {
    DLIList<CoEdge*> collapse_curve_coedges;
    edge_to_collapse->get_co_edges(collapse_curve_coedges);

    int initial_size = collapse_curve_coedges.size();
    while(collapse_curve_coedges.size() > 2 && initial_size > 0)
    {
      for(int g=collapse_curve_coedges.size(); g--;)
      {
        CoEdge *cur_coedge = collapse_curve_coedges.get_and_step();
        Loop *loop_ptr = cur_coedge->get_loop_ptr();
        if(loop_ptr)
        {
          RefFace *ref_face_ptr = loop_ptr->get_ref_face_ptr();
          if(ref_face_ptr)
          {
            DLIList<CoFace*> coface_list;
            ref_face_ptr->co_faces(coface_list);
            if(coface_list.size() > 1)
            {
              collapse_curve_coedges.remove(cur_coedge);
              g = 0;
            }
          }
        }
      }

      // Keep from looping infinitely.
      initial_size--;
    }

    // If we get to this point and have more than 2 coedges left
    // in the list we are not sure what to do.
    if(collapse_curve_coedges.size() != 2)
    {
      PRINT_ERROR("Currently can only collapse curves with manifold topology.\n");
      result = CUBIT_FAILURE;
      finished = 1;
    }
    else
    {
      // Get the collapse curve coedges entering and leaving the discard vertex.
      exit_coedge = collapse_curve_coedges.get_and_step();
      enter_coedge = collapse_curve_coedges.get();
      if(enter_coedge->end_vertex() != discard_vertex)
      {
        CoEdge *tmp = exit_coedge;
        exit_coedge = enter_coedge;
        enter_coedge = tmp;
      }
    }
  }

  // Next we need to explore the topology around the discard vertex
  // so that we can get the edges and surfaces that will be involved
  // with the partitioning and compositing.  We will identify a "left"
  // and "right" edge coming out of the discard vertex and adjacent
  // surfacs to these edges.
  if(!finished)
  {
    // Get these values for use later on.
    discard_pos = discard_vertex->get_point_ptr()->coordinates();
    keep_pos = keep_vertex->get_point_ptr()->coordinates();
    CubitVector discard_to_keep = keep_pos - discard_pos;
    arc_length = edge_to_collapse->get_arc_length();

    // Depending on whether the discard vertex has valency of 3 or 4 we will
    // either partition 1 or 2 of the curves coming into the discard vertex
    // respectively.  Set up lists so that below we can just loop through the
    // partitioning and compositing for either the 3 or 4 valency case.

    // "Left" and "right" will be defined as if you were standing on the
    // keep vertex looking at the discard vertex.
    Loop *left_loop = enter_coedge->get_loop_ptr();
    Loop *right_loop = exit_coedge->get_loop_ptr();
    DLIList<SenseEntity*> left_sense_entities, right_sense_entities;
    left_loop->get_sense_entity_list(left_sense_entities);
    right_loop->get_sense_entity_list(right_sense_entities);

    // We need to get these two coedges because the chains defined by the "next" and 
    // "previous" pointers in the CoEdge objects are not circular (you can have a NULL
    // "previous" or "next" pointer).  We will manage the circularity manually.
    CoEdge *left_start = dynamic_cast<CoEdge*>(left_loop->get_first_sense_entity_ptr()); 
    CoEdge *right_end = dynamic_cast<CoEdge*>(right_loop->get_last_sense_entity_ptr()); 
    
    CoEdge *left_coedge = dynamic_cast<CoEdge*>(enter_coedge->next());
    if(left_coedge == NULL)
    {
      left_coedge = left_start;
    }
    CoEdge *right_coedge = dynamic_cast<CoEdge*>(exit_coedge->previous());
    if(right_coedge == NULL)
    {
      right_coedge = right_end;
    }

    RefEdge *left_edge = left_coedge->get_ref_edge_ptr();
    RefEdge *right_edge = right_coedge->get_ref_edge_ptr();
    RefFace *left_common_face = left_edge->common_ref_face(edge_to_collapse);
    RefFace *right_common_face = right_edge->common_ref_face(edge_to_collapse);

    double left_arc_length = left_edge->get_arc_length();
    double right_arc_length = right_edge->get_arc_length();
    int left_ok = 1;
    int right_ok = 1;

    DLIList<RefEdge*> left_face_edges;
    DLIList<RefEdge*> right_face_edges;
    left_common_face->ref_edges(left_face_edges);
    right_common_face->ref_edges(right_face_edges);
    if(left_face_edges.size() < 3 || right_face_edges.size() < 3 ||
      left_sense_entities.size() < 3 || right_sense_entities.size() < 3)
    {
      PRINT_ERROR("Cannot collapse a curve that bounds a face or loop with only two edges.\n");
      result = CUBIT_FAILURE;
      finished = 1;
    }
    else
    {
      // We use the length of the curve being collapsed as the distance
      // to partition the adjacent curves connected to it.  If the
      // adjacent curves are shorter than the curve being collapsed we
      // will bail because the user should probably be getting rid
      // of the adjacent edges instead or at least first.

      // Get the length of the adjacent curves.
      
      // If the adjacent curve is within resabs of the length of 
      // the curve being collapsed we will just snap to the end
      // of the adjacent curve for our partition position.
      if(arc_length > left_arc_length + GEOMETRY_RESABS)
      {
        left_ok = 0;
      }
      if(arc_length > right_arc_length + GEOMETRY_RESABS)
      {
        right_ok = 0;
      }

      // If neither curve is ok we need to bail.
      if(!left_ok && !right_ok)
      {
        PRINT_ERROR("Curve being collapsed is too long compared to adjacent curves.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
    }

    // If it looks like the lengths of the adjacent curves are
    // ok we will go ahead and try to find a partition position
    // on them.
    if(!finished)
    {
      if(left_ok)
      {
         // First see if we can just use the end point of the
         // adjacent curve as the partition position.
         if(fabs(left_arc_length-arc_length) < GEOMETRY_RESABS)
         {
           if(left_edge->start_vertex() == discard_vertex)
             position_on_left_curve = left_edge->end_vertex()->coordinates();
           else
             position_on_left_curve = left_edge->start_vertex()->coordinates();
         }
         else
         {
           result = this->position_from_length(left_edge, discard_vertex,
                arc_length, position_on_left_curve);
         }
      }
      if(result == CUBIT_SUCCESS && right_ok)
      {
         // First see if we can just use the end point of the
         // adjacent curve as the partition position.
         if(fabs(right_arc_length-arc_length) < GEOMETRY_RESABS)
         {
           if(right_edge->start_vertex() == discard_vertex)
             position_on_right_curve = right_edge->end_vertex()->coordinates();
           else
             position_on_right_curve = right_edge->start_vertex()->coordinates();
         }
         else
         {
            result = this->position_from_length(right_edge, discard_vertex,
                  arc_length, position_on_right_curve);
         }
      }
      if(result == CUBIT_FAILURE)
      {
        PRINT_ERROR("Was unable to locate appropriate partition points on curves adjacent to curve being collapased.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
    }
      
    if(!finished)
    {
      // Get the vectors from the discard vertex to the potential partition locations.
      CubitVector left_vec = position_on_left_curve - discard_pos;<--- Shadow variable
      CubitVector right_vec = position_on_right_curve - discard_pos;<--- Shadow variable

      // Calculate the angles between the left/right edge and the
      // edge being collapsed.  I am doing it this way rather than just
      // calling RefEdge::angle_between() because I want the angle
      // calculation done out at the potential partition location.
      // This will step over any small kinks in the curve near the
      // discard vertex that might otherwise give misleading angles
      // that don't represent what is happening out by the potential
      // partition position.
      left_common_face->u_v_from_position(discard_pos, u, v);
      CubitVector left_normal = left_common_face->normal_at(discard_pos, NULL, &u, &v);
      double left_angle = left_normal.vector_angle(left_vec, discard_to_keep);

      right_common_face->u_v_from_position(discard_pos, u, v);
      CubitVector right_normal = right_common_face->normal_at(discard_pos, NULL, &u, &v);
      double right_angle = right_normal.vector_angle(discard_to_keep, right_vec);

      // 3 valency case: 
      // We only need to partition one of the curves (left or right) and
      // the corresponding surface.
      if(ref_edges_on_discard_vertex.size() == 3)
      {
        int use_left = 0;
        // We can use either adjacent curve.
        if(left_ok && right_ok)
        {
          // Choose the side with the smaller angle as this will in general
          // give better angles for doing the partitioning on the surface.
          CompositeSurface *cs_left = dynamic_cast<CompositeSurface*>(left_common_face->get_surface_ptr());
          CompositeSurface *cs_right = dynamic_cast<CompositeSurface*>(right_common_face->get_surface_ptr());
          if(!cs_left && cs_right)
            use_left = 1;
          else if(cs_left && !cs_right)
            use_left = 0;
          else
          {
            if(cs_left && cs_right)
            {
              int edge_on_ignored_left=0, edge_on_ignored_right=0;
              DLIList<Surface*> srfs;
              cs_left->get_ignored_surfs(srfs);
              int g;
              for(g=srfs.size(); g>0 && !edge_on_ignored_left; g--)
              {
                Surface *srf = srfs.get_and_step();
                DLIList<Curve*> crvs;
                srf->curves(crvs);
                if(crvs.is_in_list(edge_to_collapse->get_curve_ptr()))
                  edge_on_ignored_left = 1;
              }
              srfs.clean_out();
              cs_right->get_ignored_surfs(srfs);
              for(g=srfs.size(); g>0 && !edge_on_ignored_right; g--)
              {
                Surface *srf = srfs.get_and_step();
                DLIList<Curve*> crvs;
                srf->curves(crvs);
                if(crvs.is_in_list(edge_to_collapse->get_curve_ptr()))
                  edge_on_ignored_right = 1;
              }
              if(edge_on_ignored_left && !edge_on_ignored_right)
                use_left = 0;
              else if(!edge_on_ignored_left && edge_on_ignored_right)
                use_left = 1;
              else
              {
                if(left_angle < right_angle)
                  use_left = 1;
              }
            }
            else
            {
              if(left_angle < right_angle)
                use_left = 1;
            }
          }
        }
        else if(left_ok)
          use_left = 1;

        if(use_left)
        {
          curves_to_partition.append(left_edge);
          surfaces_to_partition.append(left_common_face);
          angles.append(left_angle);
          partition_positions.append(position_on_left_curve);
          arc_lengths.append(arc_length);
 
          // These vectors will be used in calculating a bisector direction
          // below if necessary.
          keep_vecs.append(-discard_to_keep);
          partition_curve_vecs.append(left_vec);
        }
        else
        {
          curves_to_partition.append(right_edge);
          surfaces_to_partition.append(right_common_face);
          angles.append(right_angle);
          partition_positions.append(position_on_right_curve);
          arc_lengths.append(arc_length);
 
          // These vectors will be used in calculating a bisector direction
          // below if necessary.
          keep_vecs.append(discard_to_keep);
          partition_curve_vecs.append(-right_vec);
        }
      }
      else if(ref_edges_on_discard_vertex.size() == 4)
      {
        // We have to partition both left and right curves so make
        // sure we can.
        if(!left_ok || !right_ok)
        {
          PRINT_ERROR("One of the curves adjacent to the collapse curve is not long enough for the collapse operation.\n");
          result = CUBIT_FAILURE;
          finished = 1;
        }
        else
        {
          // Both curves (and surfaces) adjacent to the collapse curve (left and right)
          // will need to be partitioned so add them to the lists.

          curves_to_partition.append(left_edge);
          curves_to_partition.append(right_edge);
          surfaces_to_partition.append(left_common_face);
          surfaces_to_partition.append(right_common_face);
          angles.append(left_angle);
          angles.append(right_angle);
          keep_vecs.append(-discard_to_keep);
          keep_vecs.append(discard_to_keep);
          partition_curve_vecs.append(left_vec);
          partition_curve_vecs.append(-right_vec);
          partition_positions.append(position_on_left_curve);
          partition_positions.append(position_on_right_curve);
          arc_lengths.append(arc_length);
          arc_lengths.append(arc_length);
        }
      }
      else
      {
        PRINT_ERROR("Currently can only collapse curves with 3 or 4 valency vertices.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
    }
  }

  if(!finished)
  {
    int num_reps = curves_to_partition.size();
    curves_to_partition.reset();
    surfaces_to_partition.reset();
    for(int n=0; n<num_reps && !finished; ++n)
    {
      RefEdge *side_edge = curves_to_partition.get_and_step();
      RefFace *side_face = surfaces_to_partition.get_and_step();

      // Now we need to find the face on the other side of the edge.
      // Because there may be edges on the boundaries of merged surfaces
      // we want to find the
      // face on the left/right edge that isn't the face shared by
      // the collapse edge and isn't nonmanifold.
      DLIList<CoEdge*> side_edge_coedges;
      side_edge->co_edges(side_edge_coedges);
      DLIList<RefFace*> possible_adj_faces;

      // First loop through and get all of the potential faces.
      for(int r=side_edge_coedges.size(); r--;)
      {
        CoEdge *cur_coedge = side_edge_coedges.get_and_step();
        if(cur_coedge &&
          cur_coedge->get_loop_ptr() &&
          cur_coedge->get_loop_ptr()->get_ref_face_ptr())
        {
          RefFace *cur_ref_face = cur_coedge->get_loop_ptr()->get_ref_face_ptr();
          if(cur_ref_face != side_face)
          {
            DLIList<CoFace*> coface_list;
            cur_ref_face->co_faces(coface_list);

            // Along with checking for whether the face is manifold we need
            // to check if it belongs to the same volume as the side face.
            // We have to check this because we can't composite faces
            // from different volumes and these faces will be involved in
            // a composite below.
            if(coface_list.size() == 1 &&
              side_face->ref_volume() == cur_ref_face->ref_volume())
            {
              possible_adj_faces.append(cur_ref_face);
            }
          }
        }
      }

      // If we ended up with more than one face in the list it isn't clear
      // what we should do so bail out.
      if(possible_adj_faces.size() != 1)
      {
        PRINT_ERROR("Couldn't figure out how to perform the collapse curve with the current topology.\n");
        result = CUBIT_FAILURE;
        finished = 1;
      }
      else
      {
        adjacent_surfaces.append(possible_adj_faces.get());
      }
    }
  }

  if(!finished)
  {
    // At this point we should know which curves and surfaces we need
    // to partition.

    int num_reps = curves_to_partition.size();
    curves_to_partition.reset();
    surfaces_to_partition.reset();
    adjacent_surfaces.reset();
    angles.reset();
    keep_vecs.reset();
    partition_curve_vecs.reset();
    partition_positions.reset();
    arc_lengths.reset();

    for(int i=0; i<num_reps && !finished; ++i)
    {
      RefEdge *curve_to_partition = curves_to_partition.get_and_step();
      RefFace *surface_to_partition = surfaces_to_partition.get_and_step();

      // Sanity check to make sure a previous operation has not 
      // destroyed the current face.
      DLIList<RefFace*> body_faces;
      the_body->ref_faces(body_faces);
      if(body_faces.is_in_list(surface_to_partition))
      {
        // As we process each curve remove it from the list so that
        // at the end we can take the last one in the list and composite
        // it with the curve being collapsed.
        ref_edges_on_discard_vertex.remove(curve_to_partition);

        CubitVector position_on_curve = partition_positions.get_and_step();
        DLIList<CubitVector*> positions;

        // Add the point on the curve we just partitioned.  The other
        // point we normally need to add is the keep_vertex.  However,
        // if the angle between the collapse curve and the curve we 
        // just partitioned dictates that we need to introduce more points 
        // (this would be cases where the angle is large and just partitioning the
        // surface with one curve--two points--would result in a very skinny
        // surface) we need to add them before adding the keep_vertex.  Therefore, check
        // that now and add the extra points if needed.
        positions.append(new CubitVector(position_on_curve));

        double cur_angle = angles.get_and_step();
        arc_length = arc_lengths.get_and_step();

        // These vectors are only used in the block below if we need to
        // add extra points but we need to always retrieve them from the lists
        // so that the pointer in the list stays in sync with the "for" loop.
        CubitVector keep_vec = keep_vecs.get_and_step();
        CubitVector partition_vec = partition_curve_vecs.get_and_step();

        // Greater than 3*PI/2--add 3 interior points.  Two of these
        // points will be generated by projecting from the discard vertex
        // into the surface normal to the vector from the discard vertex
        // to the keep point and new partition point respectively.  The third
        // point will be obtained by projecting from the discard point in the
        // direction of the bisector angle of the two previous projections.
        if(cur_angle > 4.71)
        {
          // Get the u,v position of the discard vertex on this surface.
          surface_to_partition->u_v_from_position(discard_pos, u, v);
          
          // Get the normal at the discard vertex.
          CubitVector normal = surface_to_partition->normal_at(discard_pos, NULL, &u, &v);
          normal.normalize();
          
          // We need to calculate a bisector direction between the
          // collape edge and the edge we just partitioned.  We will do 
          // this using the face normal at the discard vertex and the vectors 
          // we calculated previously with the partition points.  Cross
          // the face normal into the the direction vectors at the 
          // discard and partition points to get two vectors pointing
          // into the face and then average those two to get the
          // bisector direction.  This is all approximate but should 
          // be sufficient for locating another point for partitioning 
          // the surface.

          // I am not normalizing the result here because they should
          // be roughly the same length and it shouldn't affect 
          // the average too much.
          CubitVector vec1 = normal * partition_vec;
          CubitVector vec2 = normal * keep_vec;

          // Get the bisector direction.
          CubitVector bisector_dir = vec1 + vec2;
          bisector_dir.normalize();

          // Now normalise these because they will be used to
          // project two of the new interior points.
          vec1.normalize();
          vec2.normalize();

          CubitVector new_pos1 = discard_pos + (arc_length*vec1);
          CubitVector mid_pos = discard_pos + (arc_length * bisector_dir);
          CubitVector new_pos2 = discard_pos + (arc_length*vec2);

          // Use the u,v from the discard vertex because it is fairly
          // close to the new position and will at least provide a
          // meaningful starting point.
          double save_u = u, save_v = v;
          surface_to_partition->move_to_surface(new_pos1, &u, &v);
          u = save_u; v = save_v;
          surface_to_partition->move_to_surface(mid_pos, &u, &v);
          u = save_u; v = save_v;
          surface_to_partition->move_to_surface(new_pos2, &u, &v);

          // Add the new position to the list of partition points.
          positions.append(new CubitVector(new_pos1));
          positions.append(new CubitVector(mid_pos));
          positions.append(new CubitVector(new_pos2));
        }
        // Greater than 3*PI/4 and less than 3*PI/2--add one interior point
        else if(cur_angle > 2.4)
        {
          CubitVector third_pt;
          
          // Get the u,v position of the discard vertex on this surface.
          surface_to_partition->u_v_from_position(discard_pos, u, v);
          
          // Get the normal at the discard vertex.
          CubitVector normal = surface_to_partition->normal_at(discard_pos, NULL, &u, &v);
          normal.normalize();
          
          // We need to calculate a bisector direction between the
          // collape edge and the edge we just partitioned.  We will do 
          // this using the face normal at the discard vertex and the vectors 
          // we calculated previously with the partition points.  Cross
          // the face normal into the the direction vectors at the 
          // discard and partition points to get two vectors pointing
          // into the face and then average those two to get the
          // bisector direction.  This is all approximate but should 
          // be sufficient for locating another point for partitioning 
          // the surface.

          // I am not normalizing the result here because they should
          // be roughly the same length and it shouldn't affect 
          // the average too much.
          CubitVector vec1 = normal * keep_vec;
          CubitVector vec2 = normal * partition_vec;

          // Get the bisector direction.
          CubitVector bisector_dir = vec1 + vec2;
          bisector_dir.normalize();

          // Project from the discard vertex in the direction of the
          // bisector direction to get a new point for partitioning
          // the surface.
          CubitVector new_pos = discard_pos + (arc_length * bisector_dir);

          // Use the u,v from the discard vertex because it is fairly
          // close to the new position and will at least provide a
          // meaningful starting point.
          surface_to_partition->move_to_surface(new_pos, &u, &v);

          // Add the new position to the list of partition points.
          positions.append(new CubitVector(new_pos));
        }

        // Finally, add the keep_vertex to the list.
        positions.append(new CubitVector(keep_vertex->get_point_ptr()->coordinates()));
                
        DLIList<RefEdge*> new_edges;
        DLIList<DLIList<CubitVector*>*> vec_lists;
        DLIList<CubitVector*> *vec_list = new DLIList<CubitVector*>;<--- Shadowed declaration
        positions.reset();
        for(int m=positions.size(); m--;)
          vec_list->append( new CubitVector( *positions.get_and_step() ) );
        vec_lists.append( vec_list );

        if(!SplitCompositeSurfaceTool::instance()->split_surface(surface_to_partition,
          positions, vec_lists ))
        {
          finished = 1;
          result = CUBIT_FAILURE;
        }

        while( vec_lists.size() )
        {
          DLIList<CubitVector*> *vec_list = vec_lists.pop();<--- Shadow variable
          while( vec_list->size() ) delete vec_list->pop();
          delete vec_list;
        }

        delete positions.get_and_step();
        delete positions.get_and_step();

        if(cur_angle > 4.71)
        {
          delete positions.get_and_step();
          delete positions.get_and_step();
          delete positions.get_and_step();
        }
        else if(cur_angle > 2.4)
        {
          delete positions.get();
        }

        if(!finished)
        {
          RefFace *new_small_face = NULL;
          DLIList<RefEdge*> keep_edges, discard_edges;
          DLIList<RefFace*> faces_on_collapse_edge;
          keep_vertex->ref_edges(keep_edges);
          discard_vertex->ref_edges(discard_edges);
          keep_edges.intersect_unordered(discard_edges);
          if(keep_edges.size() != 1)
          {
            finished = 1;
            result = CUBIT_FAILURE;
          }
          else
          {
            edge_to_collapse = keep_edges.get();
            edge_to_collapse->ref_faces(faces_on_collapse_edge);
            for(int y=faces_on_collapse_edge.size(); y && !new_small_face; y--)
            {
              RefFace *cur_face = faces_on_collapse_edge.get_and_step();
              DLIList<RefVertex*> verts_in_face;
              cur_face->ref_vertices(verts_in_face);
              for(int p=verts_in_face.size(); p && !new_small_face; p--)
              {
                RefVertex *cur_vert = verts_in_face.get_and_step();
                if(position_on_curve.about_equal(cur_vert->coordinates()))
                  new_small_face = cur_face;
              }
            }
          }

          if(!new_small_face || new_small_face == surface_to_partition)
          {
            PRINT_ERROR("Failed to do the split surface operation of the collapse curve.\n");
            result = CUBIT_FAILURE;
            finished = 1;
          }
          else
          {
            RefVertex *vert_at_split_pos;
            DLIList<RefEdge*> new_face_edges;
            new_small_face->ref_edges(new_face_edges);
            if(new_face_edges.is_in_list(curve_to_partition))
            {
              // The split went right through one of the vertices of the
              // curve_to_partition so just set the close edge to be
              // this edge and the far edge to be NULL.
              close_edge = curve_to_partition;
              far_edge = NULL;
              vert_at_split_pos = close_edge->other_vertex(discard_vertex);
            }
            else
            {
              close_edge = edge_to_collapse->get_other_curve(discard_vertex, new_small_face);
              RefVertex *tmp_vert = close_edge->other_vertex(discard_vertex);
              if(tmp_vert)
              {
                RefEdge *tmp_edge = close_edge->get_other_curve(tmp_vert, new_small_face);
                if(tmp_edge)
                {
                  RefFace *other_face = tmp_edge->other_face(new_small_face);
                  if(other_face)
                  {
                    far_edge = tmp_edge->get_other_curve(tmp_vert, other_face);
                    vert_at_split_pos = tmp_vert;
                    // If close_edge and far_edge are the same it may mean
                    // that the split really didn't do much other than maybe 
                    // imprint one point on an edge so bail out.
                    if(far_edge == close_edge)
                    {
                      PRINT_ERROR("Failed to do the split surface operation of the collapse curve.\n");
                      result = CUBIT_FAILURE;
                      finished = 1;
                    }
                  }
                  else
                  {
                    result = CUBIT_FAILURE;
                    finished = 1;
                  }
                }
                else
                {
                  result = CUBIT_FAILURE;
                  finished = 1;
                }
              }
              else
              {
                finished = 1;
                result = CUBIT_FAILURE;
              }
            }

            if(!finished)
            {
              RefEdge *cur_edge = close_edge;
              RefVertex *cur_vert = vert_at_split_pos;
              while(cur_vert != keep_vertex)
              {
                cur_edge = cur_edge->get_other_curve(cur_vert, new_small_face);
                cur_vert = cur_edge->other_vertex(cur_vert);
                new_edges.append(cur_edge);
              }

              DLIList<RefFace*> result_faces;
              DLIList<RefFace*> faces_to_composite;

              // Used below if "ignore" keyword was specified.
              int new_small_face_id = new_small_face->id();

              faces_to_composite.append(new_small_face);
              faces_to_composite.append(close_edge->other_face(new_small_face));

              RefFace *new_comp_face = CompositeTool::instance()->composite(faces_to_composite);

              CompositeSurface* csurf = NULL;
              
              if(new_comp_face)
              {
                csurf = dynamic_cast<CompositeSurface*>(new_comp_face->get_surface_ptr());
              }

              if(!new_comp_face || !csurf)
              {
                PRINT_ERROR("Failed to do the composite surface operation of the collapse curve.\n");
                result = CUBIT_FAILURE;
                finished = 1;
              }
              else
              {
                if(ignore_surfaces)
                {
                  csurf->ignore_surface(new_small_face_id);
                }

                if(far_edge)
                {
                  for(int k=new_edges.size(); k--;)
                  {
                    edges_to_composite.append(new_edges.get_and_step());
                  }
                  edges_to_composite.append(far_edge);

                  if(edges_to_composite.size() > 1)
                  {
                    CompositeTool::instance()->composite(edges_to_composite);
                  }
                }

                edges_to_composite.clean_out();
              }
            }
          }
        }
      }
    }

    if(!finished)
    {
      ref_edges_on_discard_vertex.clean_out();
      discard_vertex->ref_edges(ref_edges_on_discard_vertex);
      ref_edges_on_discard_vertex.remove(edge_to_collapse);

      // Now there should only be one edge in the ref_edges_on_discard_vertex
      // list.  It should be the edge that hasn't had any modifications
      // done to it.  We finally want to composite it with the edge
      // being collapsed.
      if(ref_edges_on_discard_vertex.size() != 1)
      {
        PRINT_ERROR("Wasn't able to complete collapse operation.\n");
        result = CUBIT_FAILURE;
        finished = 1;<--- Variable 'finished' is assigned a value that is never used.
      }
      else
      {
        edges_to_composite.append(ref_edges_on_discard_vertex.get());
        edges_to_composite.append(edge_to_collapse);
        CompositeTool::instance()->composite(edges_to_composite);
      }
    }
  }

  CubitUndo::set_undo_enabled(undo_state);

  return result;
}

CubitStatus CollapseCurveTool::position_from_length(RefEdge *edge,
                                                  RefVertex *root_vertex,
                                                  double  arc_length,
                                                  CubitVector& v_new)
{
  CubitStatus result;
  double sense = 1.0;
  if (root_vertex != edge->start_vertex())
    sense = -1.0;
  CubitVector v_root = root_vertex->get_point_ptr()->coordinates();
  result = edge->point_from_arc_length(v_root, arc_length*sense, v_new);
  return result;
}


// ********** END PUBLIC FUNCTIONS            **********
                                                                                
// ********** BEGIN PROTECTED FUNCTIONS       **********
// ********** END PROTECTED FUNCTIONS         **********
                                                                                
// ********** BEGIN PRIVATE FUNCTIONS         **********
CollapseCurveTool::CollapseCurveTool()
{
}

CollapseCurveTool::~CollapseCurveTool()
{
}