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
//-------------------------------------------------------------------------
// Filename      : PartitionCurve-new.cpp
//
// Purpose       : 
//
// Special Notes : 
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 04/10/02
//-------------------------------------------------------------------------

#include "PartitionCurve.hpp"
#include "PartitionPoint.hpp"
#include "PartitionCoEdge.hpp"
#include "PartitionSurface.hpp"
#include "VirtualQueryEngine.hpp"
#include "PartSurfFacetTool.hpp"

#include "TDVGFacetOwner.hpp"
#include "CubitFacetEdgeData.hpp"
#include "CubitPointData.hpp"
#include "CubitFacetData.hpp"
#include "CubitTransformMatrix.hpp"
#include "Surface.hpp"

#include "GfxDebug.hpp"
#include "GMem.hpp"

/*
void print_point_list( PartitionPoint* point )
{
  PRINT_INFO("  Point %p: (%d curves)\n", point, point ? point->num_curves() : 0 );
  if(!point) { PRINT_INFO("\n\n"); return; }
  
  PartitionCurve* curve = 0;
  while( curve = point->next_curve(curve) )
    PRINT_INFO("%10p",curve);
  PRINT_INFO("\n");
  curve = 0;

  while( curve = point->next_curve(curve) )
    PRINT_INFO("%10s",
      curve->start_point() == point && curve->end_point() == point ? "(both)" :
      curve->start_point() == point ? "(start)" :
      curve->end_point() == point ? "(end)" : "(none)" );
  PRINT_INFO("\n");
}
*/

PartitionCurve::PartitionCurve( )
  : firstCoEdge(0),
    startPoint(0), 
    endPoint(0), 
    startNext(0), 
    endNext(0)
{ 
}

PartitionCurve::~PartitionCurve()
{
  start_point(0);
  end_point(0);
  
  remove_all_coedges();
  remove_facet_data();
}

CubitStatus PartitionCurve::add( PartitionCoEdge* coedge )
{
  if( coedge->myCurve )
    return CUBIT_FAILURE;
  
  coedge->curveNext = firstCoEdge;
  firstCoEdge = coedge;
  coedge->myCurve = this;
  return CUBIT_SUCCESS;
}

CubitStatus PartitionCurve::remove( PartitionCoEdge* coedge )
{
  if( coedge->myCurve != this )
    return CUBIT_FAILURE;
  
  if( firstCoEdge == coedge )
  {
    firstCoEdge = coedge->curveNext;
  }
  else
  {
    PartitionCoEdge* prev = firstCoEdge;
    while( prev && prev->curveNext != coedge )
      prev = prev->curveNext;
    
    if( !prev )
    {
      assert(0);
      return CUBIT_FAILURE;
    }
    
    prev->curveNext = coedge->curveNext;
  }
  
  coedge->curveNext = 0;
  coedge->myCurve = 0;
  return CUBIT_SUCCESS;
}

int PartitionCurve::num_coedges() const
{
  int count = 0;
  for( PartitionCoEdge* coe = firstCoEdge; coe; coe = coe->curveNext )
    count++;
  return count;
}

void PartitionCurve::remove_all_coedges( )
{
  while( firstCoEdge && remove(firstCoEdge) ) {;}
  assert( !firstCoEdge );
}

CubitStatus PartitionCurve::start_point( PartitionPoint* point )
{
  if( point == startPoint )
    return CUBIT_SUCCESS;
  
  if( !remove_start_point() )
    { assert(0); return CUBIT_FAILURE; }

  if( !point )
    return CUBIT_SUCCESS;
  
  startPoint = point;
  if( point == endPoint )
  {
    startNext = endNext;
    endNext = 0;
  }
  else
  {
    startNext = point->firstCurve;
    point->firstCurve = this;
    point->curveCount++;
  }

  return CUBIT_SUCCESS;
}

CubitStatus PartitionCurve::end_point( PartitionPoint* point )
{
  if( point == endPoint )
    return CUBIT_SUCCESS;
  
  if( !remove_end_point() )
    { assert(0); return CUBIT_FAILURE; }
  
  if( !point )
    return CUBIT_SUCCESS;

  endPoint = point;
  if( point != startPoint )
  {
    endNext = point->firstCurve;
    point->firstCurve = this;
    point->curveCount++;
  }

  return CUBIT_SUCCESS;
}

CubitStatus PartitionCurve::remove_start_point()
{
  if( startPoint == endPoint )
    endNext = startNext;
  else if( startPoint && !remove_from_point( startPoint, startNext ) )
    return CUBIT_FAILURE;

  startPoint = 0;
  startNext = 0;

  return CUBIT_SUCCESS;
}

CubitStatus PartitionCurve::remove_end_point()
{
  assert( startPoint != endPoint || !endNext );
    // endNext should be null if points are the same

  if( endPoint && endPoint != startPoint &&
      !remove_from_point( endPoint, endNext ) )
    return CUBIT_FAILURE;
  
  endPoint = 0;
  endNext = 0;

  return CUBIT_SUCCESS;
}

CubitStatus PartitionCurve::remove_from_point( PartitionPoint* point, 
                                               PartitionCurve* next )
{
  point->curveCount--;
  if( point->firstCurve == this )
  {
    point->firstCurve = next;
  }
  else
  {
    PartitionCurve* prev = point->firstCurve; 
    while( prev )
    {
      PartitionCurve* temp = prev->next_curve(point);
      if( temp == this )
        break;
      prev = temp;
    }
    
    if( !prev )
    {
      assert(0);
      return CUBIT_FAILURE;
    }
    
    if( prev->startPoint == point )
      prev->startNext = next;
    else if( prev->endPoint == point )
      prev->endNext = next;
    else
      assert(0);
  }
  
  return CUBIT_SUCCESS;
}
    
    
    

bool PartitionCurve::is_nonmanifold( const PartitionSurface* surf ) const
{
  int count = 0;
  PartitionCoEdge* coedge = 0;
  while ( (coedge = next_coedge(coedge)) )
    if ( coedge->get_loop() && coedge->get_loop()->get_surface() == surf )
      count++;
  return count > 1;
}

bool PartitionCurve::is_in_surface( const PartitionSurface* surf,
                                    bool manifold_only ) const
{
  int count = 0;
  PartitionCoEdge* coedge = 0;
  while ( (coedge = next_coedge(coedge)) )
    if ( coedge->get_loop() && coedge->get_loop()->get_surface() == surf )
      count++;
  
  if (manifold_only)
    return count == 1;
  else 
    return count > 0;
}
  

CubitStatus PartitionCurve::move_to_geometry( CubitVector& position )
{
  const CubitVector copy(position);
  return closest_point( copy, position );
}

void PartitionCurve::get_parents_virt( DLIList<TopologyBridge*>& result )
{
  for( PartitionCoEdge* coe = firstCoEdge; coe; coe = coe->curveNext )
    result.append( coe );
}

void PartitionCurve::get_children_virt( DLIList<TopologyBridge*>& result )
{
  result.append( startPoint );
  if( startPoint != endPoint )
    result.append( endPoint );
}

GeometryQueryEngine* PartitionCurve::get_geometry_query_engine() const
{
  return VirtualQueryEngine::instance();
}

void PartitionCurve::print_debug_info( const char* prefix,
                                       bool entset ) const
{
  const bool print_children = true;
  
  if( prefix == 0 ) prefix = "";
  char* new_prefix = new char[strlen(prefix)+3];
  strcpy( new_prefix, prefix );
  strcat( new_prefix, "  ");
  PRINT_INFO("%sPartitionCurve %p\n", prefix, (void*)this );
  
  if(entset)
    sub_entity_set().print_debug_info( new_prefix );
  else
    print_partitioned_entity();
  
  PartitionCoEdge* coedge = 0;
  DLIList<Surface*> surface_list(1);
  while( (coedge = next_coedge(coedge)) )
  {
    surface_list.clean_out();
    coedge->surfaces(surface_list);
    Surface* surf_ptr = surface_list.size() == 1 ? surface_list.get() : 0;
    PRINT_INFO("%s  %s on Surface %p\n", prefix, 
      coedge->sense() == CUBIT_FORWARD ? "FORWARD" :
      coedge->sense() == CUBIT_REVERSED ? "REVERSE" : "UNKNOWN",
      (void*)surf_ptr );
  }
  
  PRINT_INFO("%s  start point: %p\n", prefix, (void*)start_point() );
  if( start_point() && print_children )
    start_point()->print_debug_info( new_prefix, false );
  
  if( end_point() && end_point() == start_point() )
    PRINT_INFO("%s  end point SAME as start point\n", prefix ); 
  else
    PRINT_INFO("%s  end point: %p\n", prefix, (void*)end_point() );
  if( end_point() && end_point() != start_point() && print_children )
    end_point()->print_debug_info( new_prefix, false );
  
  delete [] new_prefix;
}

void PartitionCurve::append_simple_attribute_virt(const CubitSimpleAttrib& csa)
  { sub_entity_set().add_attribute( this, csa ); }
void PartitionCurve::remove_simple_attribute_virt(const CubitSimpleAttrib& csa)
  { sub_entity_set().rem_attribute( this, csa ); }
void PartitionCurve::remove_all_simple_attribute_virt()
  { sub_entity_set().rem_all_attrib( this ); }
CubitStatus PartitionCurve::get_simple_attribute(DLIList<CubitSimpleAttrib>& list)
{ 
  sub_entity_set().get_attributes( this, list ); 
  return CUBIT_SUCCESS;
}
CubitStatus PartitionCurve::get_simple_attribute(const CubitString& name,
                                       DLIList<CubitSimpleAttrib>& list)
{ 
  sub_entity_set().get_attributes( this, name.c_str(), list ); 
  return CUBIT_SUCCESS;
}

void PartitionCurve::reverse_point_order()
{
  if( startPoint != endPoint )
  {
    PartitionPoint* tmp_pt = startPoint;
    startPoint = endPoint;
    endPoint = tmp_pt;
    
    PartitionCurve* tmp_cv = startNext;
    startNext = endNext;
    endNext = tmp_cv;
  }
}

void PartitionCurve::get_facet_data( DLIList<CubitFacetEdgeData*>& result_list ) const
  { result_list = facetEdges; }
  
void PartitionCurve::set_facet_data( const DLIList<CubitFacetEdgeData*>& new_list )
{
  remove_facet_data();
  facetEdges = new_list;
  for( int i = facetEdges.size(); i--; )
    TDVGFacetOwner::set( facetEdges.step_and_get(), this );
}

void PartitionCurve::remove_facet_data( )
{
  while( facetEdges.size() )
    TDVGFacetOwner::remove( facetEdges.pop() );
}  


void PartitionCurve::notify_split( FacetEntity* old_entity, FacetEntity* new_entity )
{
  CubitFacetEdgeData* old_ptr = dynamic_cast<CubitFacetEdgeData*>(old_entity);
  CubitFacetEdgeData* new_ptr = dynamic_cast<CubitFacetEdgeData*>(new_entity);
  assert(TDVGFacetOwner::get(old_entity) == this);
  assert(TDVGFacetOwner::get(new_entity) == 0);
  assert(old_ptr && new_ptr);
  
  facetEdges.reset();
  if (facetEdges.get() == old_ptr)
  {
    if (new_ptr->contains(start_point()->facet_point()))
      facetEdges.insert_first(new_ptr);
    else
      facetEdges.insert(new_ptr);
  }
  else if (facetEdges.move_to(old_ptr))
  {
    CubitFacetEdgeData* prev = facetEdges.prev();
    if (prev->shared_point(new_ptr))
      facetEdges.back();
    facetEdges.insert(new_ptr);
  }
  else
  {
    assert( 0 /*old_entity not in list*/);
  }

  facetEdges.reset();
  TDVGFacetOwner::set( new_ptr, this );
}


CubitStatus PartitionCurve::fix_facet_data( PartitionCurve* new_curve )
{
  if ( !facetEdges.size() )
    return CUBIT_SUCCESS;
    
    
  DLIList<CubitFacetEdgeData*> my_edges(facetEdges);
  assert( end_point() == new_curve->start_point() );
  CubitVector pos( end_point()->coordinates() );
  
  double min_edge_len_sqr = CUBIT_DBL_MAX;
  double min_edge_dst_sqr = CUBIT_DBL_MAX;
  CubitFacetEdgeData* closest_edge = 0;
  CubitVector point, vect, edge_pos;
  int i;
  
  for ( i = my_edges.size(); i--; ) {
    CubitFacetEdgeData* edge = my_edges.step_and_get();
    point = edge->point(0)->coordinates();
    vect = edge->point(1)->coordinates() - point;
    
    double len_sqr = vect.length_squared();
    if ( len_sqr < min_edge_len_sqr )
      min_edge_len_sqr = len_sqr;
    
    point -= pos;  
    double t = (vect % -point) / len_sqr;
    if ( t < 0.0 ) t = 0.0;
    else if( t > 1.0 ) t = 1.0;
    double dst_sqr = (point + t * vect).length_squared();

    if ( dst_sqr < min_edge_dst_sqr ) {
      min_edge_dst_sqr = dst_sqr;
      closest_edge = edge;
      edge_pos = edge->point(0)->coordinates() + t * vect;
    }
  }
  
  my_edges.reset();
  DLIList<CubitFacetEdgeData*> new_curve_edges;
  my_edges.last();
  while( my_edges.get() != closest_edge ) {
    new_curve_edges.append( my_edges.pop() );
    my_edges.last();
  }
  
  min_edge_len_sqr *= 0.2;
  double dst_tol_sqr = CUBIT_MAX( min_edge_len_sqr, GEOMETRY_RESABS*GEOMETRY_RESABS );
  
  bool edge_reversed = false;
  if ( my_edges.size() > 1 )
  {
    my_edges.last();
    CubitFacetEdgeData* other = my_edges.prev();
    edge_reversed = other->contains( closest_edge->point(1) );
  }
  else if( new_curve_edges.size() > 1 )
  {
    new_curve_edges.last();
    CubitFacetEdgeData* other = new_curve_edges.get();
    edge_reversed = other->contains( closest_edge->point(0) );
  }
  else
  {
    edge_reversed = (start_point()->facet_point() == closest_edge->point(1));
    assert(edge_reversed || start_point()->facet_point() == closest_edge->point(0));
  }
  
  
  CubitPoint* new_point = 0;
  double d1 = (pos - closest_edge->point(0)->coordinates()).length_squared();
  double d2 = (pos - closest_edge->point(1)->coordinates()).length_squared();
  if ( (!TDVGFacetOwner::get(closest_edge->point(0)) && d1 < dst_tol_sqr) ||
       (d1 < GEOMETRY_RESABS*GEOMETRY_RESABS) )
  {
    if ( !edge_reversed )
      new_curve_edges.append( my_edges.pop() );
    new_point = closest_edge->point(0);
  }
  else if( (!TDVGFacetOwner::get(closest_edge->point(1)) && d2 < dst_tol_sqr) ||
      (d2 < GEOMETRY_RESABS*GEOMETRY_RESABS) )
  {
    if( edge_reversed )
      new_curve_edges.append( my_edges.pop() );
    new_point = closest_edge->point(1);
  }
  else
  {
    CubitFacetEdge* new_edge;
    CubitFacet* new_facet;
    TDVGFacetOwner::remove(closest_edge);
    PartSurfFacetTool::split_edge(closest_edge, edge_pos, 0, new_point, new_edge, new_facet );
    TDVGFacetOwner::set(closest_edge, this);

    if ( edge_reversed ) {
      new_curve_edges.append( my_edges.pop() );
      my_edges.append( dynamic_cast<CubitFacetEdgeData*>(new_edge) );
    } else {
      new_curve_edges.append( dynamic_cast<CubitFacetEdgeData*>(new_edge) );
      //TDVGFacetOwner::remove(new_edge);
    }
  }

  CubitPointData* new_point_d = dynamic_cast<CubitPointData*>(new_point);
  new_point_d->set(end_point()->coordinates());
  end_point()->facet_point( new_point_d );
  set_facet_data(my_edges);
  new_curve_edges.reverse();
  new_curve->set_facet_data(new_curve_edges);
  
  PartitionCoEdge* coedge = 0;
  while ((coedge = next_coedge(coedge)) != NULL )
  {
    PartitionLoop* loop = coedge->get_loop();
    if (!loop) continue;
    
    PartitionSurface* surf = loop->get_surface();
    if (!surf->notify_moving_point( new_point, pos ))
      return CUBIT_FAILURE;
  }

  new_point->set(pos);
  return CUBIT_SUCCESS;
}

void PartitionCurve::remove_dead_facet( CubitFacetEdgeData* edge )
{
  facetEdges.remove(edge);
}
  
CubitStatus PartitionCurve::get_save_topology( DLIList<int>& end_points )
{
  for( int i = 0; i < 2; i++ )
  {
    PartitionPoint* point = i ? end_point() : start_point();
    int set_id, pt_id;
    if( &(point->sub_entity_set()) == &sub_entity_set() )
      set_id = 0;
    else 
      set_id = point->sub_entity_set().get_unique_id();
    pt_id = point->sub_entity_set().get_id(point);
    end_points.append(set_id);
    end_points.append(pt_id);
  }
  return CUBIT_SUCCESS;
}



void PartitionCurve::transform(const CubitTransformMatrix& xform)
{
  int i;
  
  DLIList<CubitPoint*> points(facetEdges.size());
  for ( i = facetEdges.size(); i--; )
  {
    CubitFacetEdgeData* edge = facetEdges.step_and_get();
    edge->point(0)->marked(1);
    edge->point(1)->marked(1);
  }
  for ( i = facetEdges.size(); i--; )
  {
    CubitFacetEdgeData* edge = facetEdges.step_and_get();
    for ( int j = 0; j < 2; j++ )
    {
      CubitPoint* pt = edge->point(j);
      if( pt->marked() )
      {
        pt->marked(0);
      
        if( TDVGFacetOwner::get(pt) == 0 )
          points.append(pt);
      }
    }
  }
   
  for( i = points.size(); i--; )
  {
    CubitPoint* pt = points.get_and_step();
    pt->set( xform * pt->coordinates() );
  }   
}

void PartitionCurve::replace_facet(CubitFacetEdgeData* dead_facet,
                                   DLIList<CubitFacetEdgeData*> &new_facets)
{
  assert(new_facets.size() > 1); // doesn't make sense to replace 1 to 1

  DLIList<CubitFacetEdgeData*> new_copy = new_facets;

  new_facets.reset();
  facetEdges.reset();
  int dead_index = facetEdges.where_is_item(dead_facet);
  assert(-1 != dead_index);

  // special case for a single facet on the edge
  // -  use curve endpoints to determine order
  if (facetEdges.size() == 1)
  {
    facetEdges.clean_out();

    new_copy.last();
    if (new_copy.get()->contains(start_point()->facet_point()) )
      new_copy.reverse();

    new_copy.reset();
    assert( new_copy.get()->contains(start_point()->facet_point()) );
    new_copy.last();
    assert( new_copy.get()->contains(end_point()->facet_point()) );

    facetEdges = new_copy;
  }
  else
  {
    DLIList<CubitFacetEdgeData*> ordered_edges;
    int i;

    // copy the edges before the dead edge into a new list
    for (i=0; i< dead_index; i++)
      ordered_edges.append(facetEdges.next(i));

    // if there are edges before the new edges, find out whether the first or last
    // new edge is adjacent to the one before the dead edge
    CubitFacetEdgeData *prev = NULL;
    CubitFacetEdgeData *next = NULL;
    CubitPoint *new_start_pt = NULL;
    CubitPoint *new_end_pt = NULL;
    if (dead_index > 0)
    {
      prev = facetEdges.next(dead_index-1);
      new_start_pt = prev->shared_point(dead_facet);
      assert(new_start_pt != NULL);
    }

    if (dead_index < (facetEdges.size() - 1) )
    {
      next = facetEdges.next(dead_index+1);
      new_end_pt = next->shared_point(dead_facet);
      assert(new_end_pt != NULL);
    }

    if (prev)
    {
      new_copy.last();
      if (new_copy.get()->contains(new_start_pt))
        new_copy.reverse();
    }
    else
    {
      // use the edge after the dead edge to find out whether to reverse the new
      // edges or not
      assert(next != NULL);
      new_copy.reset();
      if (new_copy.get()->contains(new_end_pt))
        new_copy.reverse();
    }

    if (prev)
    {
      new_copy.reset();
      assert( prev->shared_point(new_copy.get()) != NULL);
    }
    if (next)
    {
      new_copy.last();
      assert( next->shared_point(new_copy.get()) != NULL);
    }

    // copy the new edges into place in the ordered list
    new_copy.reset();
    for (i=new_copy.size(); i>0; i--)
      ordered_edges.append(new_copy.get_and_step());

    // add the rest of the edges to the list
    facetEdges.reset();
    for (i=dead_index+1; i<facetEdges.size(); i++)
      ordered_edges.append(facetEdges.next(i));


    facetEdges.clean_out();
    facetEdges = ordered_edges;
  }

  TDVGFacetOwner::remove(dead_facet);
  for( int j = 0; j < new_facets.size(); j++ )
    TDVGFacetOwner::set(new_facets.step_and_get(),this);

  // TODO - memory management - where should the facets get deleted
}

void PartitionCurve::draw_facets( int color)
{
  if( !facetEdges.size() )
  {
    PRINT_ERROR("No facet data.\n");
    return ;
  }
  
  int i;
  GPoint* pts = new GPoint[facetEdges.size() + 1];
  
  facetEdges.reset();
  CubitPoint* pt = 0;
  if( facetEdges.size() == 1 )
  {
    pt = facetEdges.get()->point(0);
  }
  else
  {
    pt = facetEdges.get()->shared_point(facetEdges.next());
    pt = facetEdges.get()->other_point(pt);
  }
  
  CubitVector v = pt->coordinates();<--- Shadowed declaration
  pts[0].x = (float)v.x();
  pts[0].y = (float)v.y();
  pts[0].z = (float)v.z();

  for( i = 1; i <= facetEdges.size(); i++ )
  {
    pt = facetEdges.get_and_step()->other_point(pt);
    CubitVector v = pt->coordinates();<--- Shadow variable
    pts[i].x = (float)v.x();
    pts[i].y = (float)v.y();
    pts[i].z = (float)v.z();
  }
  
  GfxDebug::draw_polyline( pts, facetEdges.size() + 1, color );
  for ( i = 0; i <= facetEdges.size(); i++ )
  {
    GfxDebug::draw_point( pts[i].x, pts[i].y, pts[i].z, color );
  }
  
  facetEdges.reset();
  for ( i = 0; i < facetEdges.size(); i++ )
  {
    CubitFacetEdge* edge = facetEdges.get_and_step();
    CubitVector mid = 0.5*(edge->point(0)->coordinates()+edge->point(1)->coordinates());
    GfxDebug::draw_label( i, (float)mid.x(), (float)mid.y(), (float)mid.z(), color );
  }
  GfxDebug::flush();
  
  delete [] pts;
}

void PartitionCurve::do_facet_cleanup()
{
  int i;
  const double tol = 10.0 * GEOMETRY_RESABS;
  const double tol_sqr = tol * tol;
  
  DLIList<CubitFacetEdgeData*> edge_list(facetEdges);
  CubitFacetEdgeData* prev_edge = 0;
  edge_list.reset();
  
  for ( i = edge_list.size(); i--; )
  {
    CubitFacetEdgeData* edge = edge_list.get_and_step();
    PartitionEntity* start_pt = TDVGFacetOwner::get(edge->point(0));
    PartitionEntity* end_pt = TDVGFacetOwner::get(edge->point(1));
    if ( start_pt && end_pt )
    {
      prev_edge = edge;
      continue;
    }
    
    CubitVector edge_vect(edge->point(1)->coordinates());
    edge_vect -= edge->point(0)->coordinates();
    if ( edge_vect.length_squared() > tol_sqr )
    {
      prev_edge = edge;
      continue;
    }
    
    CubitPoint *keep, *dead;
    if ( start_pt ) 
    {
      keep = edge->point(0);
      dead = edge->point(1);
    }
    else if ( end_pt )
    {
      keep = edge->point(1);
      dead = edge->point(0);
    }
    else
    {
      double prev = (prev_edge->point(0)->coordinates() -
        prev_edge->point(1)->coordinates()).length_squared();
      double next = (edge_list.get()->point(0)->coordinates() -
        edge_list.get()->point(1)->coordinates()).length_squared();
      if ( prev < next )
      {
        dead = prev_edge->shared_point(edge);
        keep = edge_list.get()->shared_point(edge);
      }
      else
      {
        dead = edge_list.get()->shared_point(edge);
        keep = prev_edge->shared_point(edge);
      }
      assert( dead && keep );
    }
    
    if ( PartSurfFacetTool::collapse_edge( keep, dead ) ) 
    {
      ;
    }
    else
    {
      prev_edge = edge;
    }
  }
}


CubitStatus PartitionCurve::move_to_geometry( CubitPoint* point )
{
  CubitVector new_pos;
  if (!closest_point( point->coordinates(), new_pos ))
    return CUBIT_FAILURE;
    
  point->set(new_pos);
  
  DLIList<PartitionSurface*> surfaces;
  PartitionCoEdge* coedge = 0;
  while ((coedge = next_coedge(coedge)) != NULL )
  {
    PartitionLoop* loop = coedge->get_loop();
    if (!loop)
      continue;
    
    PartitionSurface* surf = loop->get_surface();
    surfaces.append_unique(surf);
    
    coedge = coedge->next();
  }
  
  int i;
  surfaces.reset();
  for ( i = 0; i < surfaces.size(); i++ )
    if (!surfaces.get_and_step()->notify_moving_point( point, new_pos ))
      break;
      
  if (i < surfaces.size()) 
    return CUBIT_FAILURE;
  
  point->set(new_pos);
  return CUBIT_SUCCESS;
}