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
//-----------------------------------------------------------
// Class: MedialTool2D
// Description:  Creates medials for 2D surfaces.  Uses the CMU
//               code for creating the medial.  This class
//               serves as the interface to that.
// Creator: David white
// Date: 6/21/2002
//-----------------------------------------------------------
#include "MedialTool2D.hpp"
#include "CubitMessage.hpp"
#ifdef USING_MEDIAL
#include "GfxDebug.hpp"
#include "CubitVector.hpp"
#include "RefEdge.hpp"
#include "RefFace.hpp"
#include "CoEdge.hpp"
#include "Curve.hpp"
#include "RefVertex.hpp"
#include "GeometryQueryEngine.hpp"
#include "VirtualGeometryEngine.hpp"
//----------medial includes---------------------
#include "medial/medial_util/MedialDefines.hpp"
#include "medial/medial2D/Medial2DAPI.hpp"
#include "medial/medial_util/Vec3D.hpp"
#include "medial/medial2D/MedialVertex.hpp"
#include "medial/medial2D/MedialSegment.hpp"
//----------medial includes---------------------
#endif //USING_MEDIAL


MedialTool2D::MedialTool2D(RefFace *ref_face)
{
  myRefFace = ref_face;
}
MedialTool2D::~MedialTool2D()
{
    //do nothing for now.
}
#ifndef USING_MEDIAL
CubitStatus MedialTool2D::create_medial_axis(DLIList <RefEdge*>&, double, double)
{
  PRINT_ERROR("Medial Axis Code is not included with this version of CGM.\n");
  return CUBIT_FAILURE;
}
#endif //USING_MEDIAL
//---------------------------------------------------------------------------
//Functions that get compiled only when compiling in the medial stuff
//---------------------------------------------------------------------------
#ifdef USING_MEDIAL
//////////////////////////////////////////////////////////////////////
///Function Name : create_medial_axis
///Member Type   : Public
///Description   : create 2D medial axis for myRefFace. Does the following
///                1. get_boundary_points() as  DLIList <PointList*>
///                2. convert_to_segments() to get
///                      vector <vector<MedialSegment*>*>
///                3. create_medial_axis() and get results as
///                    vector<MedialVertex*> and vector<MedialSegment*>
///                4. 
///
///Date          : 09/11/2002
//////////////////////////////////////////////////////////////////////
CubitStatus MedialTool2D::create_medial_axis( DLIList <RefEdge*> &medial_axis,
                                              double cell_size,
                                              double angle_tol)
{
    //first get the loop of edges.
  PointLoopList boundary_point_loops;
  CubitStatus stat = get_boundary_points(myRefFace,<--- stat is initialized
                                         boundary_point_loops);

    //Now convert these to segments.
  MedialSegmentPLoop boundary_segments;
  stat = convert_to_segments(boundary_point_loops, boundary_segments);<--- stat is overwritten<--- Variable 'stat' is assigned a value that is never used.
  
  MedialSegmentPArray output_segments;
  MedialVertexPArray output_points;
  int results = Medial2DAPI::create_medial(boundary_segments, output_points,
                                           output_segments, cell_size,
                                           angle_tol);
  if ( results != 1 )
  {
    PRINT_ERROR("Medial Extraction Failed.\n");
    return CUBIT_FAILURE;
  }

    // create geometry from the medial results
//  create_medial_geometry( output_points, output_segments, medial_axis );
  draw_results(output_points, output_segments);

  free_medial_memory(output_points, output_segments);

  return CUBIT_SUCCESS;
}

/// draw the results to the graphics output screen
void MedialTool2D::draw_results( MedialVertexPArray &output_points,
                                 MedialSegmentPArray &output_segments )
{
  int ii;
  CubitVector vec, vec_2;
  Vec3D point, start_point, end_point;
  MedialVertex *vertex, *start, *end;
  MedialSegment *seg;
  for ( ii = 0; ii < output_points.size(); ii++ )
  {
    vertex = output_points[ii];
    point = vertex->coordinates();
    convert_from_vec3d(point,vec);
    GfxDebug::draw_point(&vec,CUBIT_RED_INDEX);
    GfxDebug::flush();
  }
  GfxDebug::flush();

  for ( ii = 0; ii < output_segments.size(); ii++ )
  {
    seg = output_segments[ii];
    start = seg->get_start();
    end = seg->get_end();
    start_point = start->coordinates();
    end_point = end->coordinates();
    GfxDebug::draw_line( start_point.x, start_point.y,
                         start_point.z, end_point.x,
                         end_point.y, end_point.z,
                         CUBIT_CYAN_INDEX);
    GfxDebug::flush();
  }
  GfxDebug::flush();
}

//////////////////////////////////////////////////////////////////////
///Function Name : free_medial_memory
///Member Type   : Private
///Description   : free the medial memory
///Author        : Ragunath Sankaranarayanan
///Date          : 11/23/2002
//////////////////////////////////////////////////////////////////////
void MedialTool2D::free_medial_memory(MedialVertexPArray &vertices,
                                      MedialSegmentPArray &segments)
{
  int ii;
  MedialSegment *currs;
  MedialVertex *currv;
  
  for(ii=0; ii < segments.size(); ii++)
  {
    currs = segments[ii];
    delete currs;
  }
  
  for(ii=0; ii < vertices.size(); ii++)
  {
    currv = vertices[ii];
    delete currv;
  }
}


                                 
CubitStatus MedialTool2D::convert_to_segments(
  PointLoopList &boundary_point_loops,
  MedialSegmentPLoop &boundary_segments)
{
  int ii;
  PointList *point_loop;
  int jj;
  MedialVertex *start_vert, *end_vert;
  MedialSegment *new_seg;
  MedialSegmentPArray *new_vector;
  for ( ii = 0; ii < boundary_point_loops.size(); ii++ )
  {
    point_loop = boundary_point_loops.get_and_step();
    new_vector = new MedialSegmentPArray;
    for ( jj = 0; jj < point_loop->size(); jj++ )
    {
      start_vert = point_loop->get_and_step();
      end_vert = point_loop->get();
      new_seg = new MedialSegment(start_vert, end_vert);
      new_vector->push_back(new_seg);
    }
    boundary_segments.push_back(new_vector);
  }
  return CUBIT_SUCCESS;
}
 
                                               
CubitStatus MedialTool2D::get_boundary_points( RefFace *ref_face,
                                               PointLoopList &boundary_point_loops )
{
  DLIList<DLIList<CoEdge*>*> co_edge_loops;
  ref_face->co_edge_loops(co_edge_loops);
  int ii, jj;
  DLIList <CoEdge*> *co_edge_list_ptr;
  PointList *new_point_loop_ptr, tmp_point_list;
  RefEdge *ref_edge_ptr;
  CoEdge *co_edge_ptr;
  CubitStatus stat;
  CubitSense sense;
  RefVertex *temp_vert;
  double tolerance = 500*GEOMETRY_RESABS;
  
  
  for ( ii = co_edge_loops.size(); ii > 0; ii-- )
  {
    co_edge_list_ptr = co_edge_loops.get_and_step();
    new_point_loop_ptr = new PointList;
    for ( jj = co_edge_list_ptr->size(); jj > 0; jj-- )
    {
      co_edge_ptr = co_edge_list_ptr->get_and_step();
      ref_edge_ptr = co_edge_ptr->get_ref_edge_ptr();
      tmp_point_list.clean_out();
      stat = get_curve_facets( ref_edge_ptr, tmp_point_list );
      PRINT_DEBUG_129("curve %d has %d points\n",
                      ref_edge_ptr->id(),
                      tmp_point_list.size());
      if ( stat != CUBIT_SUCCESS )
        return CUBIT_FAILURE;
      tmp_point_list.reset();
        //the points are in order from start vertex to end vertex.
        //append them now according to the loop.

        //Assign the points to be part owned by the vertex, rather than the
        //curve.
      if ( ref_edge_ptr->start_vertex() !=
           ref_edge_ptr->end_vertex() )
      {
        MedialVertex *temp_point = tmp_point_list.get();
        Vec3D temp_v1 = temp_point->coordinates();
        CubitVector v1;
        convert_from_vec3d(temp_v1, v1);
        CubitVector v2 = ref_edge_ptr->start_vertex()->coordinates();
        if ( !v1.within_tolerance(v2, tolerance) )
        {
          PRINT_ERROR("Problem with surface geometry\n"
                      "Check surface %d, especially curve %d\n",
                      myRefFace->id(), ref_edge_ptr->id());
          return CUBIT_FAILURE;
        }
        temp_vert = ref_edge_ptr->start_vertex();<--- temp_vert is assigned
        temp_point = tmp_point_list.prev();
        temp_v1 = temp_point->coordinates();
        convert_from_vec3d(temp_v1, v1);
        v2 = ref_edge_ptr->end_vertex()->coordinates();    
        if (!v1.within_tolerance(v2, tolerance))
        {
          PRINT_ERROR("Problem with surface geometry\n"
                      "Check surface %d and %d, especially curve %d\n",
                      myRefFace->id(), ref_edge_ptr->id());
          return CUBIT_FAILURE;
        }
        temp_vert = ref_edge_ptr->end_vertex();<--- temp_vert is overwritten<--- Variable 'temp_vert' is assigned a value that is never used.
      }
      else
      {

      }
      tmp_point_list.reset();
      sense = co_edge_ptr->get_sense();
      if ( CUBIT_FORWARD != sense )
        tmp_point_list.reverse();
        //Now take off the last point as it is a duplicate with the
        //other list...
      tmp_point_list.reset();
      if ( co_edge_list_ptr->size() != 1 )
        tmp_point_list.pop();
      (*new_point_loop_ptr) += tmp_point_list;
    }
    boundary_point_loops.append(new_point_loop_ptr);
  }
    //clean up the list memory.
  for(ii = co_edge_loops.size(); ii>0; ii-- )
    delete co_edge_loops.pop();
  co_edge_loops.clean_out();
      
  return CUBIT_SUCCESS;
}

CubitStatus MedialTool2D::get_curve_facets( RefEdge* curve, PointList &segments ) 
{
  const double COS_ANGLE_TOL =  0.996194698091745545198705; // cos(5)
  GMem curve_graphics;
  double tolerance = 500*GEOMETRY_RESABS;

  const double dist_tol = tolerance;
  const double dist_tol_sqr = dist_tol*dist_tol;
  int n;
  curve->get_curve_ptr()->get_geometry_query_engine()->get_graphics_edges( 
    curve, n, &curve_graphics );
  
  GPoint* gp = curve_graphics.point_list();
  CubitVector lastv(gp[0].x, gp[0].y, gp[0].z);
  Vec3D last_v3d;
  convert_to_vec3d(lastv, last_v3d);
  MedialVertex* last = new MedialVertex(last_v3d);
  int num_points = curve_graphics.pointListCount;
  segments.append( last );
  int ii;
  CubitBoolean remove_second_to_end = CUBIT_FALSE;
  for ( ii = 1; ii < num_points; ii++ )
  {
    CubitVector pos(  gp[ii].x, gp[ii].y, gp[ii].z );
    CubitVector step1 = (pos - lastv);
    double len1 = step1.length();
    if( len1 < dist_tol && ii != num_points - 1) 
      continue;
    else if ( len1 < dist_tol && ii == num_points-1 )
    {
      remove_second_to_end = CUBIT_TRUE;
    }
    Vec3D point;
    convert_to_vec3d(pos,point);
    last = new MedialVertex(point);
    segments.append( last );
    lastv = pos;
  }
    // Now check if the segment list is reversed wrt the curve direction.
  segments.reset();
  if ( remove_second_to_end )
  {
    if ( segments.size() == 2 )
    {
    }
    else
    {
        //Remove the second to last one.  To do
        //this efficiently (don't do remove), pop
        //the last one, then save that and
        //re-add it after poping the second one.
      MedialVertex *temp = segments.pop();
      segments.pop();
      segments.append(temp);
    }
  }
  segments.reset();
  if( curve->start_vertex() != curve->end_vertex() )
  {
    CubitVector start_vec, end_vec;
    start_vec = curve->start_vertex()->coordinates();
    end_vec = curve->end_vertex()->coordinates();
    Vec3D temp_pos = segments.get()->coordinates();
    CubitVector start_seg;
    convert_from_vec3d(temp_pos, start_seg);
    double dist_1 = (start_seg - start_vec).length_squared();
    double dist_2 = (start_seg - end_vec).length_squared();
    if ( dist_1 > dist_2 )
      segments.reverse();
  }
  else
  {
    double u1, u2;
    CubitVector next_1, next_2;
    Vec3D temp_next_1, temp_next_2;
    temp_next_1 = segments.next(1)->coordinates();
    temp_next_2 = segments.next(2)->coordinates();
    convert_from_vec3d(temp_next_1, next_1);
    convert_from_vec3d(temp_next_2, next_2);
    u1 = curve->u_from_position( next_1 );
    u2 = curve->u_from_position( next_2 );    
    if( (u2 < u1) && (curve->start_param() <= curve->end_param()) )
      segments.reverse();
  }
    //clean up the periodic curve case (last seg may be too small.)
  if ( curve->start_vertex() == curve->end_vertex() )
  {
    segments.reset();
    CubitVector start_v, last_v;
    Vec3D temp_start_v, temp_last_v;
    temp_start_v = segments.get()->coordinates();
    temp_last_v = segments.prev()->coordinates();
    convert_from_vec3d(temp_start_v, start_v);
    convert_from_vec3d(temp_last_v, last_v);
    double dist = (start_v - last_v).length();
    if ( dist < dist_tol )
    {
        //remove the last one.
      segments.pop();
    }
  }
  if ( segments.size() < 2 )
  {
    PRINT_ERROR("Problem with getting boundary segments.\n");
    return CUBIT_FAILURE;
  }
  return CUBIT_SUCCESS;
}
void MedialTool2D::convert_from_vec3d(Vec3D &vec, CubitVector &new_vec)
{
  new_vec.set(vec.x, vec.y, vec.z);
}
void MedialTool2D::convert_to_vec3d(CubitVector& vec, Vec3D &new_vec)
{
  new_vec.x = vec.x();
  new_vec.y = vec.y();
  new_vec.z = vec.z();
}

//////////////////////////////////////////////////////////////////////
///Function Name : create_medial_geometry
///Member Type   : Private
///Description   : generates geometry from the medial output.
///Creator       : Shiraj Khan                
///Date          : 11/11/2002
//////////////////////////////////////////////////////////////////////
CubitStatus MedialTool2D::create_medial_geometry(MedialVertexPArray &output_points,
                                   MedialSegmentPArray &output_segments,
								   DLIList<RefEdge*> &medial_axis)
{
	int ii;
  int jj;
  int iii;
  int jjj;
  int r=0;
  MedialSegment *seg1, *seg2, *curr_seg;
  MedialVertex  *start1, *start2, *end1, *end2;
  Vec3D start_point1, start_point2, end_point1, end_point2;
  for( ii=0; ii < output_segments.size(); ii++)//finding a segment attached to only one segment
  {
	  seg1 = output_segments[ii];
	  start1 = seg1->get_start();
	  end1 = seg1->get_end();
	  start_point1 = start1->coordinates();
	  end_point1 = end1->coordinates();
	  for ( jj = 0; jj < output_segments.size(); jj++)
	  {
		  seg2 = output_segments[jj];
		  start2 = seg2->get_start();
		  end2 = seg2->get_end();
		  start_point2 = start2->coordinates();
		  end_point2 = end2->coordinates();
		  if ( start_point1 == start_point2 || start_point1 == end_point2 || end_point1 == start_point2 || end_point1 == end_point2)
			  r=r+1;
	  }
	  if( r == 2)
		  break;
	  else 
		  r=0;
  }
   
  curr_seg = output_segments[ii];
  DLIList<DLIList<MedialSegment*>*> segment_listoflist;
  DLIList<MedialSegment*> *seg_list;
  
  r = 0;// 
  int w = 0;
  int b = 0;
  int y = 0;
  int x = 0; //no. of points at which more than one medial are emerging
  int a[50];//tracks number of medials attached to a particular point ( used as a[x] )
  int v[50];//tracks segments attached to a particular segment in the output_segments list 
  int u[50];//no. of medials emerging from a point
  int z[50][50];//first subscript represents no. of diverging points ; second represents no.of medial attached to a particular diverging point
  u[0] = -1;
  int flag = 0;
  
   seg_list  = new DLIList<MedialSegment*>;
   seg_list->append(curr_seg);
   output_segments[ii] = NULL;
   seg1 = curr_seg;
              
  for( ii=0; ii < output_segments.size()-1; ii++)
  {
      
	  start1 = seg1->get_start();
	  end1 = seg1->get_end();
	  start_point1 = start1->coordinates();
	  end_point1 = end1->coordinates();
	  
	  for ( jj = 0; jj < output_segments.size(); jj++)
	  {
		  if( output_segments[jj] == NULL)
			  continue;
		  else
		  {
			  for ( jjj = 0; jjj <= b; jjj++)
			  {
				  if ( jj == u[jjj] )
				  {
					  flag = 1;
				      break;
				  }
			  }
			  if ( flag )
				  
			  {
				  flag = 0;
			      continue;
			  }
			  else
			  {
				  seg2 = output_segments[jj];
			      start2 = seg2->get_start();
			      end2 = seg2->get_end();
			      start_point2 = start2->coordinates();
			      end_point2 = end2->coordinates();
			  }
		  }
			  
			  
		       
		  if ( start_point1 == start_point2 || start_point1 == end_point2 || end_point1 == start_point2 || end_point1 == end_point2)
		  {
			  w = r;
			  v[w] = jj;
			  curr_seg = output_segments[jj];
			  r = r+1;
			  
		  }
	  }//endof for jj

	  if( r == 1)// only one segment attached to a given segment
	  {
		  seg_list->append(curr_seg);
		  seg1 = curr_seg;
		  output_segments[v[w]] = NULL;
		  curr_seg = NULL;
		  r = 0;
		  if ( ii == output_segments.size()-2 )
		  {
			  segment_listoflist.append(seg_list);
		  }

	  }// end of if ( r == 1 )
	  
	  else
	  {
		  if( r == 0)// no segment attached to a given segment
		  {
			  segment_listoflist.append(seg_list);
			  if ( x == 0 && y == 0 )//getting a segment with only one segment attached to it
			  {
				  for( ii=0; ii < output_segments.size(); ii++)
				  {
					  if ( output_segments [ii] == NULL)
							  continue;
					  else
					  {
						  seg1 = output_segments[ii];
	                      start1 = seg1->get_start();
	                      end1 = seg1->get_end();
	                      start_point1 = start1->coordinates();
	                      end_point1 = end1->coordinates();
					  }
					  for ( jj = 0; jj < output_segments.size(); jj++)
					  {
						  if ( output_segments[jj] == NULL )
							  continue;
						  else
						  {
							  seg2 = output_segments[jj];
		                      start2 = seg2->get_start();
		                      end2 = seg2->get_end();
		                      start_point2 = start2->coordinates();
		                      end_point2 = end2->coordinates();
						  }// end of if ( output_segments[jj] == NULL )
						  if ( start_point1 == start_point2 || start_point1 == end_point2 || end_point1 == start_point2 || end_point1 == end_point2)
								  r=r+1;
					  }// end of for ( jj = 0; jj < output_segments.size(); jj++)
					  if( r == 2)
						  break;
	                  else 
						  r=0;
				  }// end of for( ii=0; ii < output_segments.size(); ii++)
				  seg1 = output_segments[ii];
				  output_segments[ii] = NULL;
				  seg_list  = new DLIList<MedialSegment*>;
				  seg_list->append(seg1);
		          r = 0;
					  
			  }
			  else
			  {
				  while( output_segments[z[x][y]] == NULL)<--- Negative array index
				  {
					  y = y-1;
			          if( y < 0)
					  {
						  x = x-1;
					      if( x < 1 )
							  break;
					       else
							   y = a[x];
					  }// end of if ( y < 0 )
			        
				  } //end of while( output_segments[z[x][y]] == NULL)
				  if ( x <= 0 && y <= -1 ) <--- Assuming that condition 'y<=-1' is not redundant
				  {
					  for( ii=0; ii < output_segments.size(); ii++)
					  {
						  if ( output_segments [ii] == NULL)
							  continue;
						  else
						  {
							  seg1 = output_segments[ii];
	                          start1 = seg1->get_start();
	                          end1 = seg1->get_end();
	                          start_point1 = start1->coordinates();
	                          end_point1 = end1->coordinates();
						  }// end of if ( output_segments [ii] == NULL)
						  for ( jj = 0; jj < output_segments.size(); jj++)
						  {
							  if ( output_segments[jj] == NULL )
								  continue;
							  else
							  {
								  seg2 = output_segments[jj];
		                          start2 = seg2->get_start();
		                          end2 = seg2->get_end();
		                          start_point2 = start2->coordinates();
		                          end_point2 = end2->coordinates();
							  }// end of else to  if ( output_segments[jj] == NULL )
							  if ( start_point1 == start_point2 || start_point1 == end_point2 || end_point1 == start_point2 || end_point1 == end_point2)
								        r=r+1;
						  }// end of for ( jj = 0; jj < output_segments.size(); jj++)
						  if( r == 2)
							  break;
	                      else
							  r=0;
					  }// end of for( ii=0; ii < output_segments.size(); ii++)
					  seg1 = output_segments[ii];
					  output_segments[ii] = NULL;
					  seg_list  = new DLIList<MedialSegment*>;
				      seg_list->append(seg1);
		              r = 0;
					  x = 0;
					  y = 0;
				  }
				  else
				  {
					  seg1 = output_segments[z[x][y]];
		              output_segments[z[x][y]] = NULL;
		              seg_list  = new DLIList<MedialSegment*>;
				      seg_list->append(seg1);
		              r = 0;
				          
				  }
			  }
			  
		  }// end of if ( r == 0 )
	      
		  else
		  {
			  if ( r>1 )//more than one segments attached to a given segment
			  {
				  segment_listoflist.append(seg_list);
		          x = x+1;
		          a[x] = w;
		          y = w;
				  b = w;
		          for( iii = 0; iii < r; iii++)
				  {
					  z[x][iii] = v[iii];
					  u[iii] = v[iii];
				  }
				  seg1 = output_segments[z[x][y]];
		          output_segments[z[x][y]] = NULL;
		          seg_list  = new DLIList<MedialSegment*>;
			      seg_list->append(seg1);
		          r = 0;
			  }// end of if ( r > 1)
		  }// end of else to if ( r == 0 )
	  }// end of else to if ( r == 1)
  }// end of for( ii=0; ii < output_segments.size()-1; ii++)  
	  
  segment_listoflist.reset();
  MedialSegment *first_seg, *second_seg;
  MedialVertex  *start, *end, *intermediate_start, *intermediate_end, *secondseg_start, *secondseg_end ;
  Vec3D start_point, end_point, intermediate_prev_end_point, intermediate_curr_start_point, intermediate_curr_end_point, secondseg_startpoint, secondseg_endpoint;
  DLIList<CubitVector*> curve_vectors;
  CubitVector cubit_start_vertex, cubit_end_vertex, cubit_intermediate_vertex;
  
  for ( ii = 0; ii < segment_listoflist.size(); ii++)
  {
	  seg_list  = new DLIList<MedialSegment*>;<--- seg_list is assigned
	  seg_list = segment_listoflist.get_and_step();<--- seg_list is overwritten
	  curve_vectors.clean_out();
	  for ( jj = 0; jj < seg_list->size(); jj++)
	  {
		 first_seg = seg_list->next(jj);
		  
		  if ( seg_list->size() == 1 )//seg_list contains only one segment
		  {
			  start =first_seg->get_start();
			  end =first_seg->get_end();
			  start_point = start->coordinates();
			  end_point = end->coordinates();
			  convert_from_vec3d(start_point,cubit_start_vertex);
			  convert_from_vec3d(end_point,cubit_end_vertex);
		  }// end of if ( seg_list->size() == 1 )
		  else
		  {
			  if ( jj == 0)//for the first segment in the seg_list list<--- Assuming that condition 'jj==0' is not redundant
			  {
				  start =first_seg->get_start();
				  end =first_seg->get_end();
				  start_point = start->coordinates();
				  end_point = end->coordinates();
				  second_seg = seg_list->next(jj+1);<--- Argument 'jj+1' to function next is always 1
				  secondseg_start = second_seg->get_start();
				  secondseg_end = second_seg->get_end();
				  secondseg_startpoint = secondseg_start->coordinates();
				  secondseg_endpoint = secondseg_end->coordinates();
                  
				  if ( end_point == secondseg_startpoint || end_point == secondseg_endpoint )
				  {
					  convert_from_vec3d(start_point,cubit_start_vertex);
					  intermediate_prev_end_point = end_point;
				  }// end of if ( end_point == secondseg_startpoint || end_point == secondseg_endpoint )
				  else if ( start_point == secondseg_startpoint || start_point == secondseg_endpoint )
				  {
					  convert_from_vec3d(end_point,cubit_start_vertex);
					  intermediate_prev_end_point = start_point;
					  
				  }//end of if ( start_point == secondseg_startpoint || start_point == secondseg_endpoint )
				  
			  }// end of if ( jj== 0 )
			  else
			  {
				  intermediate_start =first_seg->get_start();
				  intermediate_end =first_seg->get_end();
				  intermediate_curr_start_point = intermediate_start->coordinates();
				  intermediate_curr_end_point = intermediate_end->coordinates();
				  if ( intermediate_curr_start_point == intermediate_prev_end_point )
				  {
					  convert_from_vec3d(intermediate_curr_start_point,cubit_intermediate_vertex);
					  intermediate_prev_end_point = intermediate_curr_end_point;
					  
				  }// end of if ( intermediate_curr_start_point == intermediate_prev_end_point )
				  else if ( intermediate_curr_end_point == intermediate_prev_end_point )
				  {
					  convert_from_vec3d(intermediate_curr_end_point,cubit_intermediate_vertex);
					  intermediate_prev_end_point = intermediate_curr_start_point;
					  
				  }// end of if ( intermediate_curr_end_point == intermediate_prev_end_point )
				  
				  curve_vectors.append(&cubit_intermediate_vertex);
				  
				  if ( jj == seg_list->size()-1 )//for the last segment in the seg_list
				  {
					  convert_from_vec3d(intermediate_prev_end_point,cubit_end_vertex);
					   
				  }// end of if ( jj == seg_list->size()-1 )

			  }//end of else to if ( jj == 0 )
		  }//end of else to if ( seg_list->size() == 1 ) 
	  }// end of for ( jj = 0; jj < seg_list->size(); jj++)
	  
	  RefVertex *first_ref = VirtualGeometryEngine::instance()->create_VirtualVertex(cubit_start_vertex);
	  RefVertex *last_ref = VirtualGeometryEngine::instance()->create_VirtualVertex(cubit_end_vertex);;
	  assert( first_ref && last_ref );
	  RefEdge *ref_edge = NULL;
	  if ( curve_vectors.size() == 0)//only one segment 
	  {
		  
		  ref_edge = VirtualGeometryEngine::instance()->create_VirtualEdge(first_ref, last_ref);
		  medial_axis.append( ref_edge );
		 
	  }//end of if ( curve_vectors.size() == 0)
	  else
	  {
		  
		  ref_edge = VirtualGeometryEngine::instance()->create_VirtualEdge(first_ref, last_ref, curve_vectors);
		  medial_axis.append( ref_edge );
		  
		  
	  }// end of if ( curve_vectors.size() == 0)
	  
	  seg_list->clean_out();
	  
  }//end of for ( ii = 0; ii < segment_listoflist.size(); ii++)


  return CUBIT_SUCCESS;
}

#endif //USING_MEDIAL