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
//-------------------------------------------------------------------------
// Filename      : SegmentedCurve.cc
//
// Purpose       : 
//
// Special Notes : The parameterization of this is defined such that the
//                 parameter range between two adjacent nodes is one.
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 06/10/98
//-------------------------------------------------------------------------

#include "SegmentedCurve.hpp"

#include "PartitionPoint.hpp"
#include "PartitionCoEdge.hpp"
#include "PartitionSurface.hpp"
#include "PartitionLump.hpp"
#include "GMem.hpp"
#include "PartitionEngine.hpp"
#include "CubitTransformMatrix.hpp"
#include "CubitMessage.hpp"

int SegmentedCurve::segment_from_u( double& param )
{
  if( is_periodic() ) 
    normalize_periodic_parameter( param );
  
  int num_segments = point_count() - 1;
  if( param < -CUBIT_RESABS ||
      param > (num_segments + CUBIT_RESABS) )
    return -1;
  
    // Could call trunc() here, but why?  Need to cast to an
    // int anyway.  Also, the cast to int should be save 
    // because the magnitude of param is tested above.
  int segment_index = (int)param;
  if( segment_index == num_segments )
    --segment_index;
  
  param -= segment_index;
  return segment_index;
}

PartitionCurve* SegmentedCurve::split( double param )
{
  int seg = segment_from_u(param);
  if( seg < 0 )
    return 0;  
  
  CubitVector beg = position(seg);
  CubitVector end = position(seg+1);
  CubitVector pos = beg + param * (end - beg);
  
  if( (pos - end).length_squared() < GEOMETRY_RESABS*GEOMETRY_RESABS )
  {
    seg++;
    param = 0;
    beg = pos = end;<--- Variable 'beg' is assigned a value that is never used.
    
    if( seg == point_count() - 1 )
      return 0;
  }
  else if( (pos - beg).length_squared() < GEOMETRY_RESABS*GEOMETRY_RESABS )
  {
    if( seg == 0 )
      return 0;
      
    pos = beg;
    param = 0;
  }
  
  SegmentedCurve* new_curve = new SegmentedCurve( this, point_count() - seg );
  
  new_curve->point_list[0] = pos;
  for( int i = 1; i < new_curve->point_list.size(); i++ )
    new_curve->point_list[i] = point_list[seg + i];
  if( param == 0 )
  {
    point_list.size( seg + 1 );
  }
  else
  {
    point_list.size( seg + 2 );
    point_list[seg+1] = pos;
  }
    
  return new_curve;
}

CubitStatus SegmentedCurve::combine( PartitionCurve* dead_curve )
{
  SegmentedCurve* dead = dynamic_cast<SegmentedCurve*>(dead_curve);
  if( !dead || dead->partitioned_entity() != this->partitioned_entity() )
    return CUBIT_FAILURE;
  
  CubitVector this_start = this->position(0);
  CubitVector dead_start = dead->position(0);
  CubitVector this_end   = this->position( this->point_count() - 1 );
  CubitVector dead_end   = dead->position( dead->point_count() - 1 );
  
  int i;
  bool append, reverse;
  const double tol_sqr = GEOMETRY_RESABS * GEOMETRY_RESABS;
  if( (this_end - dead_start).length_squared() < tol_sqr )
  {
    append = true;
    reverse = false;
  }
  else if( (this_end - dead_end).length_squared() < tol_sqr )
  {
    append = true;
    reverse = true;
  }
  else if( (this_start - dead_end).length_squared()  < tol_sqr )
  {
    append = false;
    reverse = false;
  }
  else if( (this_start - dead_start).length_squared() < tol_sqr )
  {
    append = false;
    reverse = true;
  }
  else
  {
    assert(0);
    return CUBIT_FAILURE;
  }
  
  if( reverse )
    dead->point_list.reverse();
  
  int this_point_count = this->point_list.size();
  int dead_point_count = dead->point_list.size();
  int new_point_count = this_point_count + dead_point_count - 1;
  this->point_list.size(new_point_count);
  
  if( append )
  {
    for( i = 1; i < dead_point_count; i++ )
      point_list[this_point_count + i - 1] = dead->point_list[i];
  }
  else
  {
    for( i = 1; i <= this_point_count; i++ )
      point_list[new_point_count-i] = point_list[this_point_count-1];
    
    for( i = 0; i < dead_point_count; i++ )
      this->point_list[i] = dead->point_list[i];
  }
    
  return CUBIT_SUCCESS;
}  
      
  

CubitBoolean SegmentedCurve::is_linear()
{
  int i, count = point_count();
  if( count == 2 ) return CUBIT_TRUE;
  if( is_periodic() ) return CUBIT_FALSE;
  
  CubitVector mean(0.0,0.0,0.0);
  for( i = 1; i < count; i++ ) mean += (position(i)-position(i-1));
  mean.normalize();
  for( i = 1; i < count; i++ )
  {
    if( (1-(mean % ~(position(i)-position(i-1)))) > CUBIT_RESABS )
      return CUBIT_FALSE;
  }
  return CUBIT_TRUE;
}

CubitStatus SegmentedCurve::get_point_direction( 
  CubitVector& origin, CubitVector& direction )
{
  if( ! is_linear() ) return CUBIT_FAILURE;
  origin = position(0);
  direction = ~(position(point_count()-1) - origin);
  return CUBIT_SUCCESS;
}

double SegmentedCurve::length_from_u( double param1, double param2 )
{
  if( param1 > param2 )
  {
    double temp = param1;
    param1 = param2;
    param2 = temp;
  }
  
  if( is_periodic() )
  {
    normalize_periodic_parameter( param1 );
    normalize_periodic_parameter( param2 );
  }
  
  int first = (int)floor( param1 );
  int last = (int)floor( param2 );
  int seg_count = point_count() - 1;

  //check for errors, round a little
  if( first < 0 )
  {
    if( param1 > -CUBIT_RESABS ) first = 0;
    else return -1.0;
  }
  if( last < 0 )
  {
    if( param2 > -CUBIT_RESABS ) last = 0;
    else return -1.0;
  }
  if( last >= seg_count )
  {
    if( param2 < (CUBIT_RESABS + seg_count) )
      last--;
    else return -1.0;
  }
  if( first >= seg_count )
  {
    if( param1 < (CUBIT_RESABS + seg_count) )
      first--;
    else return -1.0;
  }
    
  //calculate the remaining length on the first segment
  
  double length_sum = segment_length( first ) * (1 - (param1 - first) );
  
  //add up the remaining segments upto and including last
  
  for( int i = first + 1; i <= last; i++ )
    length_sum += segment_length( i );
    
  // by including the last segment, we (may have) gone too far,
  // so subtract back off the extra on the last segment
  
  length_sum -= segment_length(last) * (1 - (param2 - last) );
  
  return length_sum;

}

CubitBoolean SegmentedCurve::get_param_range( double& lower, double& upper )
{
  lower = 0.0;
  upper = double(point_count() - 1);
  return CUBIT_TRUE;
}

CubitStatus SegmentedCurve::closest_point(
  CubitVector const& location,
  CubitVector& closest_location,
  CubitVector* tangent_ptr,
  CubitVector* curvature_ptr,
  double* param)
{
  double seg_param;
  int segment_no = closest_segment( location, &seg_param, &closest_location );
  
  if ( Surface* surf = dynamic_cast<Surface*>(partitioned_entity()) )
  {
    CubitVector seg_point(closest_location);
    surf->closest_point( seg_point, &closest_location );
  }
  
  if( tangent_ptr != NULL )
  {
    //First check if the closest point is at the join of
    //two segments.  If so, return the average of the two 
    //tangets.
    if( (seg_param < CUBIT_RESABS) && (segment_no > 0) )
    {
      CubitVector seg1_start = position( segment_no - 1 );
      CubitVector seg2_end   = position( segment_no + 1 );
      *tangent_ptr = seg2_end - seg1_start;
    }
    
    //ditto
    else if( ( (1.0 - seg_param) < CUBIT_RESABS ) && 
             ( segment_no < (point_count() - 2) ) )
    {
      CubitVector seg1_start = position( segment_no );
      CubitVector seg2_end   = position( segment_no + 2 );
      *tangent_ptr = seg2_end - seg1_start;
    }
    
    //The segment_no is the index of the point beginning the segment.
    //A segment_no equal to point_count() - 1, the last point is 
    //possible.  So, for any segment point but the last, return the
    //tangent of the segment beginning with the point at segment_no.
    else if( segment_no < point_count() - 1 )
    {
      *tangent_ptr = this->position( segment_no + 1 ) 
        - this->position( segment_no );
    }
    
    else if( segment_no == 0 )<--- Assuming that condition 'segment_no==0' is not redundant
    {
      *tangent_ptr = this->position( segment_no + 1 )<--- Argument 'segment_no+1' to function position is always 1
        - this->position( segment_no );
    }
    
    //If the point is the last one, return the direction of
    //the last segment (this point and the previous one.)
    else 
    {
      *tangent_ptr = this->position( segment_no ) 
        - this->position( segment_no - 1 );
    }
    
    tangent_ptr->normalize();
  }
  
  //There is never a valid curvature for a segmented curve.  
  //The curvature is either infinity in the interior of segments,
  //or zero at the points between segments.  Just return zero.
  if( curvature_ptr != NULL )
  {
    curvature_ptr->set( 0., 0., 0. );
  }

  if (param)
  {
    *param = double(segment_no) + seg_param;
  }
  
  return CUBIT_SUCCESS;
}

CubitStatus SegmentedCurve::closest_point_trimmed( 
  CubitVector const& from_pt, CubitVector& result_pt ) 
{
  return closest_point( from_pt, result_pt );
}

int SegmentedCurve::closest_segment( CubitVector const& location,
                                     double* seg_fraction,
                                     CubitVector* closest_pt )
{  
  int length_ = point_count();
  
  int segment_no = 0;
  CubitVector start = position( 0 );
  CubitVector end = position( 1 );
  CubitVector dir = end - start;
  double seg_param = closest_point_on_segment( start, dir, location );
  CubitVector point = start + seg_param * dir;
  if( closest_pt ) *closest_pt = point;
  double shortest = (point - location ).length_squared();
   
  for( int i = 2; i < length_; i++ )
  {
    start = end;
    end = position( i );
    dir = end - start;
    double param = closest_point_on_segment( start, dir, location );
    point = start + param * dir;
    double dist = (point - location).length_squared();
    if( dist < shortest )
    {
      shortest = dist;
      if( closest_pt ) *closest_pt = point;
      segment_no = i - 1;
      seg_param = param;
    }
  }
  
  if( seg_fraction ) *seg_fraction = seg_param;  
  return segment_no;
}

double SegmentedCurve::u_from_position( const CubitVector& position )
{
  double seg_param;
  int segment = closest_segment( position, &seg_param );
  return double(segment) + seg_param;
}

CubitStatus SegmentedCurve::position_from_u( double u, CubitVector& p)
{
  double fraction = u;
  int segment_no = segment_from_u(fraction);
  if( segment_no < 0 )
    return CUBIT_FAILURE;
    
  CubitVector s = position( segment_no );
  CubitVector e = position( segment_no+1 );
  p = s + fraction * ( e - s );
  
  if ( Surface* surf = dynamic_cast<Surface*>(partitioned_entity()) )
  {
    CubitVector seg_pos(p);
    surf->closest_point( seg_pos, &p );
  }

  return CUBIT_SUCCESS;
}

double SegmentedCurve::u_from_arc_length( double param, double length )
{
  if( fabs(length) < CUBIT_RESABS ) 
    return param;
  
  double fraction = param;  
  int segment = segment_from_u( fraction );
  if( segment < 0 )
    return param;
  
  //Find the length of the remaining portion of the first
  //segment (after the base param.)
  double seg_len = segment_length( segment );
// BWC  double len_sum = seg_len * ( 1.0 - param + segment );
  double len_sum;
  
  if(length < 0.0)
    len_sum = seg_len*(param-segment);
  else
    len_sum = seg_len * ( 1.0 - param + segment );
  
  //If the passed base param and length correspond to a
  //portion of the curve which lies entirely on a single
  //segment, return the passed base param plus the 
  //fraction of the segment (the parameter range for a 
  //segment is one.)
//BWC  if( len_sum >= length ) 
  if(length < 0.0)
  {
    if(-len_sum < length)
      return param + ((seg_len > CUBIT_DBL_MIN ) ? length / seg_len : 0.0);
  }
  else
  {
    if( len_sum >= length ) 
      return param + ((seg_len > CUBIT_DBL_MIN ) ? length / seg_len : 0.0);
  }
  
  //Increment the total length until we have passed up
  //the specified arc length.
  /*
  BWC
  while( length > len_sum)
  {
    segment++;
    if( segment >= point_count() - 1 ) return -1.0;
    seg_len = segment_length( segment );
    len_sum += seg_len;
    if(fabs(len_sum-length) < GEOMETRY_RESABS)
      len_sum = length;
 }
 */
  if(length < 0.0)
  {
    while( fabs(length) > len_sum )
    {
      segment--;
      if( segment < 0 ) return -1.0;
      seg_len = segment_length( segment );
      len_sum += seg_len;
      if(fabs(len_sum+length) < GEOMETRY_RESABS)
        len_sum = -length;
    }
  }
  else
  {
    while( length > len_sum )
    {
      segment++;
      if( segment >= point_count() - 1 ) return -1.0;
      seg_len = segment_length( segment );
      len_sum += seg_len;
      if(fabs(len_sum-length) < GEOMETRY_RESABS)
        len_sum = length;
    }
  }
  
  //Now subtract off the extra length on the last segment,
  //the amount passed the specified arc length.  The ratio
  //of this length to the total length of the last segment
  //is the about we overshot the desired parameter value.
  if(length < 0.0)
  {
    if( seg_len > CUBIT_DBL_MIN )
      return (double)(segment) + (len_sum + length)/seg_len;
    else 
      return (double)(segment);
  }
  else
  {
    if( seg_len > CUBIT_DBL_MIN )
      return (double)(segment + 1) - (len_sum - length)/seg_len;
    else 
      return (double)(segment + 1);
  }
}

CubitBoolean SegmentedCurve::is_position_on( const CubitVector& pos )
{
  //Get the segment to test on
  CubitVector v;
  closest_segment( pos, NULL, &v);
  
  return (pos - v).length() <= GEOMETRY_RESABS ? CUBIT_TRUE : CUBIT_FALSE;
}

CubitBox SegmentedCurve::bounding_box() const
{
  CubitBox b( position( 0 ) );
  for( int i = 1; i < point_count(); i++ )
    b |= position( i );

  return b;
}

double SegmentedCurve::measure()
{
  double len = 0;
  for( int i = 0; i < point_count() - 1; i++ )
  {
    len += segment_length( i );
  }
  return len;
}

CubitStatus SegmentedCurve::get_interior_extrema( 
                                DLIList<CubitVector*>& interior_points,
                                CubitSense& return_sense )
{
  return_sense = CUBIT_FORWARD;
  
  //For each point, check the segments to either side.  If any of deltaX, 
  //deltaY or deltaZ have opposite signs for the two segments, then the
  //point is an extrema in x, y, or z, respectively.
  int count = point_count() - 1; //stop with second-to-last point
  int prev_index = 0;

  for( int i = 1; i < count; i++ )
  {
    CubitVector curr = position(i+1) - position(i);
    CubitVector prev = position( i ) - position( prev_index );
    if( (prev.x()*curr.x() < 0.0) || (prev.y()*curr.y()<0.0) || (prev.z()*curr.z()<0.0) )
    {
      interior_points.append( new CubitVector( position(i) ) );
      prev_index = i;
    }
  }
  return CUBIT_SUCCESS;
}

CubitStatus SegmentedCurve::get_center_radius( CubitVector&, double& )
  { return CUBIT_FAILURE; }
  

double SegmentedCurve::closest_point_on_segment( 
                                              const CubitVector& base,
                                              const CubitVector& dir,
                                              const CubitVector& location )
{
  if( dir.length_squared() < CUBIT_DBL_MIN ) return 0.0;
  
  //Find closest location to infinite line
  double param = dir % ( location - base ) / dir.length_squared();
  
  //Trim to segment
  if( param < 0.0 ) param = 0.0;
  else if( param > 1.0 ) param = 1.0;
  
  return param;
}

//-------------------------------------------------------------------------
// Purpose       : Check for G1 discontinuities
//
// Special Notes : 
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 05/03/00
//-------------------------------------------------------------------------
CubitBoolean SegmentedCurve::G1_discontinuous( double param,
                                            CubitVector* minus_tangent,
                                            CubitVector* plus_tangent )
{
  if( is_periodic() ) normalize_periodic_parameter( param );
  
  double lower = floor( param );
  double upper = ceil( param );
  int first_point = -1;
  int last = point_count() - 1;
  if( last < 2 ) return CUBIT_FALSE;

  //If the parameter value corresponds to the start of a segment,
  //and that segment is not the first one
  if( ((param - lower) < CUBIT_RESABS) && (int(lower) != 0) )
    first_point = int(lower) - 1;
  //If the parameter value corresponds to the end of a segment,
  //and that segment is not the last one
  else if( ((upper - param) < CUBIT_RESABS) && (int(upper) < last ) )
    first_point = int(upper);
  //If the parameter value corresponds to the start or end of the
  //curve, and the curve is a closed, periodic curve.
  else if( is_periodic() && (fabs(start_param() - end_param()) < CUBIT_RESABS) &&
           ((((param - lower) < CUBIT_RESABS) && (int(lower) == 0)) ||
            (((upper - param) < CUBIT_RESABS) && (int(upper) == last)) ) )
    first_point = last - 1;
  //Otherwise the point is in the interior of a segment
  else return CUBIT_FALSE;
  
  int third_point = first_point + 2;
  if( third_point > last )
  {
    if( is_periodic() )
    {
      third_point = (third_point + 1) % point_count();
    }
    else
    {
      return CUBIT_FALSE;
    }
  }

  CubitVector tan1, tan2;
  tan1 = position( first_point + 1 ) - position( first_point );
  tan2 = position( third_point ) - position( first_point + 1 );
  if( (tan1 * tan2).length_squared() < CUBIT_RESABS )
    return CUBIT_FALSE;
  
  if( minus_tangent ) *minus_tangent = tan1;
  if( plus_tangent )  *plus_tangent = tan2;
  return CUBIT_TRUE;
}


//-------------------------------------------------------------------------
// Purpose       : If the curve is periodic, adjust the param value to
//                 be in the base range.
//
// Special Notes : 
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 05/15/00
//-------------------------------------------------------------------------
void SegmentedCurve::normalize_periodic_parameter( double& u ) 
{
  //The parameter range for a segmented curve is always (by my implementation)
  //from zero to some positive n.  And the period is equal to that n.  This 
  //simplifies the following a lot. -jk
  double period;
  if( is_periodic( period ) )
  {
    if( u > period )
      u = fmod( u, period );
    else if( u < -period ) 
      u = fmod( u, period ) + period;
    else if( u < 0.0 )
      u += period;  //fmod() seems to be expensive, so avoid it here.
  }
}

//-------------------------------------------------------------------------
// Purpose       : Constructor
//
// Special Notes : 
//
// Creator       : Jason Kraftcheck
//
// Creation Date : 05/15/00
//-------------------------------------------------------------------------
SegmentedCurve::SegmentedCurve( PartitionSurface* surf, 
                                DLIList<CubitVector*>& points )
  : point_list( points.size() )
{
  points.reset();
  for( int i = 0; i < points.size(); i++ )
    point_list[i] = *points.get_and_step();
  
  surf->sub_entity_set().add_lower_order( this );
}
SegmentedCurve::SegmentedCurve( PartitionLump* vol, 
                                DLIList<CubitVector*>& points )
  : point_list( points.size() )
{
  points.reset();
  for( int i = 0; i < points.size(); i++ )
    point_list[i] = *points.get_and_step();
  
  vol->sub_entity_set().add_lower_order( this );
}


SegmentedCurve::SegmentedCurve( SegmentedCurve* curve, int ptcount )
  : point_list(ptcount)
{
  curve->sub_entity_set().add_lower_order(this);
}
 

SegmentedCurve::~SegmentedCurve()
{
}

int SegmentedCurve::point_count() const
{
  return point_list.size();
}

double SegmentedCurve::segment_length( int segment_no ) const
{
  return (point_list[segment_no+1] - point_list[segment_no]).length();
}

CubitVector SegmentedCurve::position( int index ) const
{
  return point_list[index];
}

  
CubitStatus SegmentedCurve::get_graphics( GMem& result, 
                                    double /*angle_tolerance*/,
                                    double /*distance_tolerance*/,
                                    double /*max_edge_length*/)
{
  result.allocate_polylines( point_count()-1 );
  for( int i = 0; i < point_count(); i++ )
  {
    CubitVector v = position(i);
    result.point_list()[i].x = (float)(v.x());
    result.point_list()[i].y = (float)(v.y());
    result.point_list()[i].z = (float)(v.z());
  }
  result.pointListCount = point_count();
  return CUBIT_SUCCESS;
}

CubitBoolean SegmentedCurve::is_periodic( double& period )
{
  if( is_periodic() )
  {
    period = end_param() - start_param();
    return CUBIT_TRUE;
  }
  return CUBIT_FALSE;
}

CubitBoolean SegmentedCurve::is_periodic() const
{
  return start_point() == end_point();
}

CubitPointContainment SegmentedCurve::point_containment( const CubitVector& pos )
{
  const double tol_sqr = GEOMETRY_RESABS * GEOMETRY_RESABS;
  if( (start_point()->coordinates() - pos).length_squared() < tol_sqr )
    return CUBIT_PNT_BOUNDARY;
  else if( (end_point()->coordinates() - pos).length_squared() < tol_sqr )
    return CUBIT_PNT_BOUNDARY;
  else if( is_position_on( pos ) )
    return CUBIT_PNT_ON;
  else 
    return CUBIT_PNT_OFF;
}


CubitStatus SegmentedCurve::get_segments( DLIList<CubitVector*>& list )
{
  for( int i = 0; i < point_count(); i++ )
    list.append( new CubitVector( position(i) ) );
  return CUBIT_SUCCESS;
}

void SegmentedCurve::reverse_sense()
{
  point_list.reverse();
  reverse_point_order();
  if( owner() )
    owner()->notify_reversed(this);
}

CubitStatus SegmentedCurve::save( CubitSimpleAttrib& attrib )
{
  DLIList<CubitVector*> segments;
  get_segments(segments);
  
  DLIList<int> topo;
  get_save_topology( topo );
  
  int id = sub_entity_set().get_id(this);
  
  return sub_entity_set().save_geometry( id, 1, &segments, 0, &topo, 0, attrib );
}

SegmentedCurve* SegmentedCurve::construct( const CubitSimpleAttrib& attrib,
                                           PartitionEntity* parent )
{
  PartitionSurface* owning_surf = dynamic_cast<PartitionSurface*>(parent);
  PartitionLump* owning_lump = dynamic_cast<PartitionLump*>(parent);
  if( !owning_surf && !owning_lump )
    return 0;
  
  DLIList<int> vertex_conn;
  SegmentedCurve* result = new SegmentedCurve( parent, attrib, vertex_conn );
  
  if( vertex_conn.size() != 4 )
  {
    delete result;
    return 0;
  }
  
  PartitionPoint *start = 0, *end = 0;
  PartitionEntity* ent;
  vertex_conn.reset();
  int set_id = vertex_conn.get_and_step();
  int ent_id = vertex_conn.get_and_step();
  ent = PartitionEngine::instance().entity_from_id(set_id,ent_id,parent->sub_entity_set());
  start = dynamic_cast<PartitionPoint*>(ent);
  set_id = vertex_conn.get_and_step();
  ent_id = vertex_conn.get_and_step();
  ent = PartitionEngine::instance().entity_from_id(set_id,ent_id,parent->sub_entity_set());
  end = dynamic_cast<PartitionPoint*>(ent);
  if( !start || !end )
  {
    delete result;
    return 0;
  }
  
  result->start_point(start);
  result->end_point(end);
  return result;
}
  
  
SegmentedCurve::SegmentedCurve( PartitionEntity* vol, 
                                const CubitSimpleAttrib& attrib,
                                DLIList<int>& vertex_conn )
{
  DLIList<CubitVector*> points;
  DLIList<int> junk;
  vol->sub_entity_set().add_lower_order( this, attrib, 1, points, junk, vertex_conn, junk );
  
  points.reset();
  point_list.size(points.size());
  for( int i = 0; i < points.size(); i++ )
  {
    CubitVector* pt = points.get_and_step();
    point_list[i] = *pt;
    delete pt;
  }
}

void SegmentedCurve::transform( const CubitTransformMatrix& xform )
{
  PartitionCurve::transform(xform);
  for( int i = 0; i < point_list.size(); i++ )
    point_list[i] = xform * point_list[i];
}

void SegmentedCurve::print_debug_info( const char* prefix, bool pss ) const
{
  if( prefix == 0 ) prefix = "";
  PartitionCurve::print_debug_info( prefix, pss );
  PRINT_INFO("%sSegmentedCurve %p\n", prefix, (void*)this);
  PRINT_INFO("%s%d segment points:\n", prefix, point_list.size());
  int i;
  for( i = 0; i  < point_list.size() - 1; i+= 2 )
  {
    PRINT_INFO("%s  (%f, %f, %f), (%f, %f, %f),\n",
      prefix, point_list[i  ].x(), point_list[i  ].y(), point_list[i  ].z(),
              point_list[i+1].x(), point_list[i+1].y(), point_list[i+1].z());
  }
  if( i == point_list.size() - 1 )
  {
    PRINT_INFO("%s  (%f, %f, %f)\n", prefix, point_list[i].x(), 
       point_list[i].y(), point_list[i].z());
  }
}

CubitStatus SegmentedCurve::get_spline_params
(
  bool &rational,    // return true/false
  int &degree,       // the degree of this spline
  DLIList<CubitVector> &cntrl_pts,  // xyz position of controlpoints
  DLIList<double> &cntrl_pt_weights, // if rational, a weight for each cntrl point.
  DLIList<double> &knots,   // There should be order+cntrl_pts.size()-2 knots
  bool &spline_is_reversed
) const
{
  PRINT_ERROR("Currently, Cubit is unable to determine spline parameters for SegmentedCurves.\n");
  return CUBIT_FAILURE;
}

CubitStatus SegmentedCurve::get_ellipse_params
(
  CubitVector &center_vec,
  CubitVector &normal,
  CubitVector &major_axis,
  double &radius_ratio
) const
{
  PRINT_ERROR("Currently, Cubit is unable to determine ellipse parameters for SegmentedCurves.\n");
  return CUBIT_FAILURE;
}