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
//-------------------------------------------------------------------------
// Filename      : FacetorTool.cpp
//
// Purpose       : 2D Delaunay mesher, template that can use both mesh and geom entities, not yet tested with geometric
//                 entities
//
// Creator       : Christopher Hynes
//
// Creation Date : 5/31/2002
//
// Owner         : Steve Owen
//-------------------------------------------------------------------------

#ifdef INLINE_TEMPLATES
#define MY_INLINE inline
#else
#define MY_INLINE
#endif

#include "FacetorTool.hpp"
#include "FacetorUtil.hpp"
#include "TDDelaunay.hpp"
#include "BoundaryConstrainTool.hpp"
#include "CubitPoint.hpp"
#include "CubitPointData.hpp"
#include "CubitFacet.hpp"
#include "CubitFacetData.hpp"
#include "FacetEvalTool.hpp"
#include "ParamTool.hpp"
#include "CubitMessage.hpp"

#define DETERM(p1,q1,p2,q2,p3,q3)\
     ((q3)*((p2)-(p1)) + (q2)*((p1)-(p3)) + (q1)*((p3)-(p2)))
#define SQR(x) ((x) * (x))
#define FT_INSIDE_TOL 1.0e-6
#define QUALITY_ANGLE 0.361283155162   /* 20.7 degrees */
#define ALPHA 0.70228615

//-------------------------------------------------------------------------
// Function:    FacetorTool
// Description: constructor
// Author:      chynes
// Date:        6/5/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE 
FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::
FacetorTool( SURF *ref_face_ptr, <--- Member variable 'FacetorTool::facetList' is not initialized in the constructor.
			DLIList<NODE *> &bounding_nodes, 
			NODE **boundary_edge_start_nodes,
			NODE **boundary_edge_end_nodes, 
			int num_boundary_edges, 
			SIZEFUNC *sizing_function,
			ParamTool *p_tool )
{
  //update private variables
  pTool = p_tool;
  refFacePtr = ref_face_ptr;
  boundingNodes = &bounding_nodes;
  boundaryEdgeStartNodes = boundary_edge_start_nodes;
  boundaryEdgeEndNodes = boundary_edge_end_nodes;
  numBoundaryEdges = num_boundary_edges;
  sizingFunction = sizing_function;

  curVisitFlag = INT_MIN+1;
  boxNodes[0] = boxNodes[1] = boxNodes[2] = boxNodes[3] = NULL;
}


//-------------------------------------------------------------------------
// Function:    ~FacetorTool
// Description: destructor
// Author:      chynes
// Date:        5/31/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::~FacetorTool()
{
  numBoundaryEdges = 0;
}


//-------------------------------------------------------------------------
// Function:    mesh_surf
// Description: mesh the surface
// Author:      chynes
// Date:        5/31/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::mesh_surf(DLIList<TRI *> &facet_list)<--- The function 'mesh_surf' is never used.
{
  CubitStatus  ret_value  = CUBIT_SUCCESS;
  
  // update private variables
  facetList = &facet_list;

  // create two initial bounding triangles

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = init_box();
  }

  // insert the boundary nodes

  if (ret_value == CUBIT_SUCCESS)
  {
      //PRINT_INFO("Inserting boundary nodes \n");
    ret_value = insert_nodes( boundingNodes );
  }

  // constrain the boundary

  if (ret_value == CUBIT_SUCCESS)
  {
      //PRINT_INFO("Constraining the mesh to the boundary\n");
    ret_value = constrain_boundary();
  }

  // delete the triangles on the outside of the boundary

  if (ret_value == CUBIT_SUCCESS)
  {
      //PRINT_INFO("Deleting the exterior entities\n");
    ret_value = delete_exterior();
  }

  // insert the hards

  // insert interior points

  if (ret_value == CUBIT_SUCCESS) {
      //PRINT_INFO("Refining the interior mesh\n");
    ret_value = refine_interior(*facetList);
  }

    // clean up
    //PRINT_INFO("Cleaning up some data\n");
  clean_up_data();
  

  return ret_value;
}

//-------------------------------------------------------------------------
// Function:    mesh_surfwoIP
// Description: mesh the surface w/o interior point refinement
// Author:      chynes
// Date:        6/20/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::mesh_surfwoIP(DLIList<TRI *> &facet_list)
{
  CubitStatus  ret_value  = CUBIT_SUCCESS; 

  //update private variables
  facetList = &facet_list;

  // create two initial bounding triangles

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = init_box();
  }

  // insert the boundary nodes

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = insert_nodes( boundingNodes );
  }

  // constrain the boundary

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = constrain_boundary();
  }

  // delete the triangles on the outside of the boundary

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = delete_exterior();
  }

  // insert the hard points

  // clean up

  clean_up_data();

  return ret_value;
}

//-------------------------------------------------------------------------
// Function:    mesh_surfwoIPBC
// Description: mesh the surface w/o interior point refinement
// Author:      chynes
// Date:        6/20/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::mesh_surfwoIPBC(DLIList<TRI *> &facet_list)<--- The function 'mesh_surfwoIPBC' is never used.
{
  CubitStatus  ret_value  = CUBIT_SUCCESS;

  // update private variables

  facetList = &facet_list;

  // create two initial bounding triangles

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = init_box();
  }

  // insert the boundary nodes

  if (ret_value == CUBIT_SUCCESS)
  {
    ret_value = insert_nodes( boundingNodes );
  }

  // delete the triangles on the outside of the boundary

  if (ret_value == CUBIT_SUCCESS){
    ret_value = delete_exterior();
  }

  // insert the hard points

  // clean up

  clean_up_data();

  return ret_value;
}

//-------------------------------------------------------------------------
// Function:    init_box
// Description: create two initial bounding triangles
// Author:      chynes
// Date:        5/31/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::init_box(void)
{
  // find the bounding box

  int ii;
  CubitVector min_box, max_box;
  min_box.x( CUBIT_DBL_MAX );min_box.y( CUBIT_DBL_MAX );
  max_box.x( -CUBIT_DBL_MAX );max_box.y( -CUBIT_DBL_MAX );

  NODE *node_ptr;
  CubitVector node_loc;
  for (ii=0; ii<boundingNodes->size(); ii++)
  {
    node_ptr = boundingNodes->get_and_step();
    node_loc = node_ptr->coordinates();
    if (node_loc.x() < min_box.x()) min_box.x( node_loc.x() );    
    if (node_loc.y() < min_box.y()) min_box.y( node_loc.y() );
    if (node_loc.x() > max_box.x()) max_box.x( node_loc.x() );    
    if (node_loc.y() > max_box.y()) max_box.y( node_loc.y() );
  }

  // expand the box by 10%

  double dx = max_box.x() - min_box.x();
  double dy = max_box.y() - min_box.y();
  double expand;
  if (dx > dy)
    expand = 0.1 * dx;
  else 
    expand = 0.1 * dy;

  min_box.x( min_box.x() - expand );
  min_box.y( min_box.y() - expand );
  max_box.x( max_box.x() + expand );
  max_box.y( max_box.y() + expand );
 
  
   // create four new nodes

  boxNodes[0] = (NODE *) new NODECHILD(min_box.x(), min_box.y(), 0.0, refFacePtr);
  boxNodes[1] = (NODE *) new NODECHILD(max_box.x(), min_box.y(), 0.0, refFacePtr);
  boxNodes[2] = (NODE *) new NODECHILD(max_box.x(), max_box.y(), 0.0, refFacePtr);
  boxNodes[3] = (NODE *) new NODECHILD(min_box.x(), max_box.y(), 0.0, refFacePtr);

  
  // create the two triangles

  TRI *new_facet1 = (TRI *) new TRICHILD( boxNodes[0], boxNodes[1], 
                                     boxNodes[3], refFacePtr );
  TRI *new_facet2 = (TRI *) new TRICHILD( boxNodes[1], boxNodes[2], 
                                     boxNodes[3], refFacePtr );

  facetList->append(new_facet1);
  facetList->append(new_facet2);

  return CUBIT_SUCCESS;
}


//-------------------------------------------------------------------------
// Function:    insert_nodes
// Description: insert nodes into Delaunay mesh
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::
insert_nodes(DLIList<NODE *> *&bounding_nodes)
{
  CubitStatus rv = CUBIT_SUCCESS;
  int ii;
  NODE *point_ptr;
  TRI* start_tri = NULL;
  for (ii=0; ii<bounding_nodes->size() && rv == CUBIT_SUCCESS; ii++){
    point_ptr = bounding_nodes->get_and_step();
    rv = FacetorUtil<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::insert_node(
                                                                point_ptr, *facetList,
                                                                refFacePtr, curVisitFlag,
                                                                start_tri);
  }

  
  return rv;
}


//-------------------------------------------------------------------------
// Function:    constrain_boundary
// Description: recover the boundary edges from the mesh
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::constrain_boundary(void)
{
  CubitStatus rv = CUBIT_SUCCESS;

  BoundaryConstrainTool<SURF,TRI,EDGE,NODE,TRICHILD> bctool( refFacePtr );
  EDGE *edge_ptr = NULL;
  int ii;
  for (ii=0; ii<numBoundaryEdges && rv == CUBIT_SUCCESS; ii++)
  {
    rv = bctool.recover_edge( boundaryEdgeStartNodes[ii],
			      boundaryEdgeEndNodes[ii],
			      edge_ptr,
			      facetList);
    //assert(edge_ptr != NULL);
    if (edge_ptr == NULL)
    {
      PRINT_ERROR("Could not recover a boundary in Delaunay facetor.\n"
                  "Indicates potential problems with input loop geometry\n");
      rv = CUBIT_FAILURE;
    }
  }
  return rv;
}


//-------------------------------------------------------------------------
// Function:    delete_exterior
// Description: delete the triangles on the outside of the boundary and on
//              the interior of holes
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::delete_exterior(void)
{
  CubitStatus rv = CUBIT_SUCCESS;

  // mark edges at the boundary

  EDGE *edge_ptr;
  int ii;
  for (ii=0; ii<numBoundaryEdges; ii++)
  {
    edge_ptr = boundaryEdgeStartNodes[ii]->shared_edge( boundaryEdgeEndNodes[ii] );
    if(!edge_ptr){
      PRINT_ERROR("Boundary edges were not successfully recovered.\n");
      return CUBIT_FAILURE;
    }
    edge_ptr->marked( CUBIT_TRUE );
  }

  // get a tri adjacent to a bounding box node

  DLIList<TRI*> adjtris;
  boxNodes[0]->tris( adjtris );
  TRI *tri_ptr = adjtris.get();

  // mark the tris on the outside starting from this triangle

  int num_edges_found=0;
  rv = mark_tris( tri_ptr, num_edges_found );
  if (rv != CUBIT_SUCCESS)
    return rv;

  // delete the exterior triangles

  int marked_flag = 1;
  rv = delete_tris( *facetList, marked_flag );

  // check to see if we've found all the edges
  //PRINT_INFO("num_edges_found = %i, numBoundaryEdges = %i\n",num_edges_found,numBoundaryEdges);
  
  if (num_edges_found != numBoundaryEdges)
  {

    // if not, then there are interior loops(holes) that need to have their
    // triangles removed

    // find a triangle at the boundary

    TRI *adjtri;
    int jj;
    CubitBoolean found = CUBIT_FALSE;
    if(!facetList->size()){
      PRINT_ERROR("Problem marking exterior facets.\n");
      return CUBIT_FAILURE;
    }
    
    for (ii=0; ii<facetList->size() && !found; ii++)
    {
      tri_ptr = facetList->get_and_step();
      for (jj=0; jj<3 && !found; jj++)
      {
	int kk = jj;
	adjtri = tri_ptr->adjacent( kk, refFacePtr );
        if (adjtri == NULL)
	  {
          found = CUBIT_TRUE;
        }
      }
    }

    // mark all the tris we want to keep
    if(!found){
      PRINT_WARNING("Possible problem.  No boundary edge found.\n");
    }
    
    rv = mark_tris( tri_ptr, num_edges_found );<--- rv is assigned

    // delete all the rest

    marked_flag = 0;
    rv = delete_tris( *facetList, marked_flag );<--- rv is overwritten
  }

  return rv;
}


//-------------------------------------------------------------------------
// Function:    delete_tris
// Description: delete triangles in the list with the specified marked flag
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::delete_tris(DLIList<TRI *> &tri_list, int marked_flag)
{
  CubitStatus rv = CUBIT_SUCCESS;

  int ii, jj;
  EDGE *edge_ptr;
  TRI *tri_ptr;
  DLIList<EDGE *> edge_list;
  DLIList<TRI *> new_list;
  int ntri = tri_list.size();
  for(ii=0; ii<ntri; ii++)
  {
    tri_ptr = tri_list.pop();
    if (tri_ptr->marked() == marked_flag)
    {
      edge_list.clean_out();
      tri_ptr->edges( edge_list );
      delete tri_ptr;
      for(jj=0; jj<3; jj++)
      {
        edge_ptr = edge_list.get_and_step();
        if(edge_ptr->number_tris() == 0 && edge_ptr->number_faces() == 0)
          delete edge_ptr;
      }
    }
	else
	  new_list.append(tri_ptr);
  }
  tri_list+=new_list;
  return rv;
}


//-------------------------------------------------------------------------
// Function:    mark_tris
// Description: recursive function to mark all the triangles we want to keep
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::mark_tris(TRI *tri_ptr, 
                                       int &num_edges_found)
{
  CubitStatus rv = CUBIT_SUCCESS;
  tri_ptr->marked( CUBIT_TRUE );
  int ii;
  EDGE *edge_ptr;
  TRI *adjtri;
  for (ii=0; ii<3 && rv == CUBIT_SUCCESS; ii++)
  {
    int jj = ii;
    adjtri = tri_ptr->adjacent( jj, refFacePtr );
    edge_ptr = tri_ptr->edge( jj );
    if(edge_ptr->marked()) {
      num_edges_found++;
    }
    else {
      if (adjtri != NULL && !adjtri->marked())
        rv = mark_tris( adjtri, num_edges_found );
    }
  }
  return rv;
}


//-------------------------------------------------------------------------
// Function:    refine_interior
// Description: generate nodes on the interior of the tiangulation to
//              improve quality
// Notes:       this inserts nodes at the circumcenters of triangles roughly
//              based on the algorithm by Jonathon Shewchuk
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::refine_interior(DLIList<TRI *> &tri_list)
{
  CubitStatus rv = CUBIT_SUCCESS;

  // classify the triangles based on their minimum angle

  //rv =
  const int num_lists = 64;
  const double interval = QUALITY_ANGLE / double( num_lists - 1 );
  DLIList<TRI*>* tri_sort_array = new DLIList<TRI *> [num_lists];
  classify_triangles(tri_list, tri_sort_array, num_lists, interval );

  // process each of the triangles until done

  TRI *tri_ptr;
  TRI* start_tri = NULL;
  while ((tri_ptr = next_triangle(tri_sort_array, num_lists)) != NULL && rv == CUBIT_SUCCESS) 
  {
    rv = FacetorUtil<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::insert_at_circumcenter(tri_ptr,
                                                                                             *facetList,
                                                                                             start_tri,
                                                                                             curVisitFlag,
                                                                                             refFacePtr,
                                                                                             tri_sort_array,
                                                                                             num_lists,
                                                                                             interval,
                                                                                             QUALITY_ANGLE,
                                                                                             sizingFunction,
                                                                                             pTool);
  }

  delete [] tri_sort_array;
  tri_sort_array = NULL;

  return rv;
}

//-------------------------------------------------------------------------
// Function:    classify_triangles
// Description: order the triangles in the current mesh by their worst angle
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
CubitStatus FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::classify_triangles(DLIList<TRI *> &tri_list,
                                                                                                  DLIList<TRI*>* sorted_lists,
                                                                                                  const int num_lists,
                                                                                                  const double interval)
{
  CubitStatus rv = CUBIT_SUCCESS;

  // Create an array of lists that will hold triangles as they are
  // created (and deleted).  Each list will hold triangles whose angles 
  // fall within a specified interval.  Triangles will be processed
  // from smallest angle to best angle

  // classify each trriangle and place into sort lists

  TRI *tri_ptr;
  int ii;
  for(ii=0; ii<tri_list.size() && rv == CUBIT_SUCCESS; ii++)
  {
    tri_ptr = tri_list.get_and_step();
    //rv = 
    FacetorUtil<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::classify_tri_by_angle(
                                    tri_ptr, sorted_lists,  num_lists, interval, QUALITY_ANGLE);
  }
   
  return rv;
}


//-------------------------------------------------------------------------
// Function:    next_triangle
// Description: get the next triangle to process
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
TRI *FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::next_triangle(DLIList<TRI*>* sorted_lists,
                                                                                      const int num_lists)
{
  TRI *tri_ptr = NULL;
  int ii;
  for( ii = 1; ii < num_lists && tri_ptr == NULL; ii++)
  {
    if (sorted_lists[ii].size() > 0)
      tri_ptr = sorted_lists[ii].remove();
  }
  if (tri_ptr == NULL)
  {
    if (sorted_lists[0].size() > 0)
      tri_ptr = sorted_lists[0].remove();
  }
  return tri_ptr;
}

//-------------------------------------------------------------------------
// Function:    clean_up_data
// Description: clean up any data we've allocated
// Author:      chynes
// Date:        6/3/2002
//-------------------------------------------------------------------------
template<class SURF, class TRI, class EDGE, class NODE, class TRICHILD, class NODECHILD, class SIZEFUNC> MY_INLINE
void FacetorTool<SURF, TRI, EDGE, NODE, TRICHILD, NODECHILD, SIZEFUNC>::clean_up_data(void)
{

  EDGE *del_edge_ptr;
  DLIList<EDGE *> del_edge_list;
  TRI *tri_ptr;
  DLIList<TRI *> tri_list;
  
  
  int ii, jj, kk;

    //loop over facets and remove marks
  for(ii=0; ii<facetList->size(); ii++)
  {	
    tri_ptr = facetList->get_and_step();
    tri_ptr->marked(CUBIT_FALSE);
    ToolData *td = tri_ptr->remove_TD( TDDelaunay< TRI, NODE >::is_delaunay );
    if (td != NULL)
    {
      TDDelaunay< TRI, NODE > *td_del = dynamic_cast<TDDelaunay< TRI, NODE >*> (td);
      delete td_del;
    }
  }

    //loop over the box ndes
    //if meshing was successful, some of the below is unnecessary
  for(ii=0; ii<4; ii++)
  {
    if (boxNodes[ii] != NULL){
      tri_list.clean_out();
        //if there are tris attached, delete them.
      boxNodes[ii]->tris(tri_list);
      for (jj=0; jj<tri_list.size(); jj++)
      {
        tri_ptr = tri_list.get_and_step();
        del_edge_list.clean_out();
        tri_ptr->edges( del_edge_list );
        delete tri_ptr;
        
          // also deltet the attached, unused edges
        for (kk=0; kk<del_edge_list.size(); kk++)
        {
          del_edge_ptr = del_edge_list.get_and_step();
          if (del_edge_ptr->number_tris() == 0)
            delete del_edge_ptr;
        }
      }
    }
      //finally delete the nodes
    delete boxNodes[ii];
  }

  // clean off the edge marks

  EDGE *edge_ptr;
  for (ii=0; ii<numBoundaryEdges; ii++)
  {
    edge_ptr = boundaryEdgeStartNodes[ii]->shared_edge( boundaryEdgeEndNodes[ii] );
    if (edge_ptr)
      edge_ptr->marked( CUBIT_FALSE );
  }
}

// EOF