forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_polycurve.cpp
3845 lines (3498 loc) · 103 KB
/
opennurbs_polycurve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
ON_OBJECT_IMPLEMENT(ON_PolyCurve,ON_Curve,"4ED7D4E0-E947-11d3-BFE5-0010830122F0");
ON_PolyCurve::ON_PolyCurve() ON_NOEXCEPT
{
}
ON_PolyCurve::~ON_PolyCurve()
{
Destroy();
}
ON_PolyCurve::ON_PolyCurve( const ON_PolyCurve& src )
: ON_Curve(src)
, m_t(src.m_t)
{
// need to copy the curves (not just the pointer values)
src.m_segment.Duplicate(m_segment);
}
ON_PolyCurve& ON_PolyCurve::operator=( const ON_PolyCurve& src )
{
if ( this != &src )
{
ON_Curve::operator=(src);
src.m_segment.Duplicate(m_segment);
m_t = src.m_t;
}
return *this;
}
#if defined(ON_HAS_RVALUEREF)
ON_PolyCurve::ON_PolyCurve( ON_PolyCurve&& src) ON_NOEXCEPT
: ON_Curve(std::move(src)) // moves userdata
, m_segment(std::move(src.m_segment))
, m_t(std::move(src.m_t))
{}
ON_PolyCurve& ON_PolyCurve::operator=( ON_PolyCurve&& src)
{
if ( this != &src )
{
this->Destroy();
ON_Curve::operator=(src); // moves userdata
m_segment = std::move(src.m_segment);
m_t = std::move(src.m_t);
}
return *this;
}
#endif
ON_PolyCurve::ON_PolyCurve( int capacity )
: m_segment(capacity), m_t(capacity+1)
{
m_segment.Zero();
}
void ON_PolyCurve::Destroy()
{
// release memory
m_segment.Destroy();
m_t.Destroy();
}
void ON_PolyCurve::EmergencyDestroy()
{
m_segment.EmergencyDestroy();
m_t.EmergencyDestroy();
}
void ON_PolyCurve::DestroyRuntimeCache( bool bDelete )
{
ON_Curve::DestroyRuntimeCache(bDelete);
int i, count = m_segment.Count();
for ( i = 0; i < count; i++ )
{
ON_Curve* segment_curve = m_segment[i];
if ( 0 != segment_curve && this != segment_curve )
segment_curve->DestroyRuntimeCache(bDelete);
}
}
unsigned int ON_PolyCurve::SizeOf() const
{
unsigned int sz = ON_Curve::SizeOf();
sz += (sizeof(*this) - sizeof(ON_Curve));
sz += m_t.SizeOfArray();
sz += m_segment.SizeOfArray();
int i, count = m_segment.Count();
for ( i = 0; i < count; i++ )
{
const ON_Curve* crv = m_segment[i];
if ( crv )
sz += crv->SizeOf();
}
return sz;
}
ON__UINT32 ON_PolyCurve::DataCRC(ON__UINT32 current_remainder) const
{
int i, count = m_segment.Count();
for ( i = 0; i < count; i++ )
{
const ON_Curve* crv = m_segment[i];
if ( crv )
current_remainder = crv->DataCRC(current_remainder);
}
current_remainder = m_t.DataCRC(current_remainder);
return current_remainder;
}
int ON_PolyCurve::Dimension() const
{
const ON_Curve* p = SegmentCurve(0);
return (p) ? p->Dimension() : 0;
}
bool ON_PolyCurve::GetBBox( // returns true if successful
double* boxmin, // minimum
double* boxmax, // maximum
bool bGrowBox
) const
{
const int count = Count();
int segment_index;
bool rc = (count>0) ? true : false;
for ( segment_index = 0; segment_index < count && rc; segment_index++ ) {
rc = m_segment[segment_index]->GetBBox( boxmin, boxmax, bGrowBox );
bGrowBox = true;
}
return rc;
}
bool
ON_PolyCurve::Transform( const ON_Xform& xform )
{
TransformUserData(xform);
DestroyRuntimeCache();
const int count = Count();
int segment_index;
bool rc = (count>0) ? true : false;
for ( segment_index = 0; segment_index < count && rc; segment_index++ ) {
rc = m_segment[segment_index]->Transform( xform );
}
return rc;
}
bool ON_PolyCurve::IsDeformable() const
{
bool rc = true;
const int count = Count();
int segment_index;
for ( segment_index = 0; segment_index < count ; segment_index++ )
{
const ON_Curve* seg = m_segment[segment_index];
if ( seg && !seg->IsDeformable() )
{
rc = false;
break;
}
}
return rc;
}
bool ON_PolyCurve::MakeDeformable()
{
bool rc = true;
bool bDestroyRuntimeCache = false;
const int count = Count();
int segment_index;
for ( segment_index = 0; segment_index < count ; segment_index++ )
{
ON_Curve* seg = m_segment[segment_index];
if ( seg && !seg->IsDeformable() )
{
bDestroyRuntimeCache = true;
if ( !seg->MakeDeformable() )
{
ON_NurbsCurve* nurbs_curve = seg->NurbsCurve();
if ( nurbs_curve )
{
delete seg;
m_segment[segment_index] = nurbs_curve;
}
else
rc = false;
}
}
}
if ( bDestroyRuntimeCache )
DestroyRuntimeCache();
return rc;
}
bool
ON_PolyCurve::SwapCoordinates( int i, int j )
{
const int count = Count();
int segment_index;
bool rc = (count>0) ? true : false;
for ( segment_index = 0; segment_index < count && rc; segment_index++ ) {
rc = m_segment[segment_index]->SwapCoordinates( i, j );
}
DestroyCurveTree();
return rc;
}
void ON_PolyCurve::SanitizeDomain()
{
// remove "fuzz" in m_t[] domain array that is in some older files.
int segment_count = Count();
if (m_t.Count() != segment_count + 1)
return;
double s, t, d0, d1, fuzz;
ON_Interval in0, in1;
in1 = SegmentCurve(0)->Domain();
d1 = in1.Length();
for (int segment_index = 1; segment_index < segment_count; segment_index++ )
{
t = m_t[segment_index];
in0 = in1;
d0 = d1;
in1 = SegmentCurve(segment_index)->Domain();
d1 = in1.Length();
s = in0[1];
if ( s != t && s == in1[0] && t > in0[0] && t < in1[1] )
{
fuzz = ((d0<=d1)?d0:d1)*ON_SQRT_EPSILON;
if ( fabs(t-s) <= fuzz )
m_t[segment_index] = s;
}
}
fuzz = d1*ON_SQRT_EPSILON;
t = m_t[segment_count];
s = in1[1];
if ( s != t && t > in1[0] && fabs(s-t) <= fuzz )
m_t[segment_count] = s;
}
bool ON_PolyCurve::IsValid( ON_TextLog* text_log ) const
{
return IsValid(false,text_log);
}
bool ON_PolyCurve::IsValid( bool bAllowGaps, ON_TextLog* text_log ) const
{
const int count = Count();
const int dim = Dimension();
ON_3dPoint p0, p1;
int segment_index;
if ( count <= 0 || dim <= 0 )
{
if ( text_log )
text_log->Print("Polycurve segment count = %d and dim = %d\n",count,dim);
return ON_IsNotValid();
}
if ( m_t.Count() != count+1 )
{
if ( text_log )
text_log->Print("Polycurve segment count = %d and m_t.Count()=%d (should be segment count+1)\n",
count,m_t.Count());
return ON_IsNotValid();
}
for ( segment_index = 0; segment_index < count; segment_index++ )
{
if ( 0 == m_segment[segment_index] )
{
if ( text_log )
{
text_log->Print("Polycurve segment[%d] is null.\n",segment_index);
}
return ON_IsNotValid();
}
if ( !m_segment[segment_index]->IsValid( text_log ) )
{
if ( text_log )
{
text_log->Print("Polycurve segment[%d] is not valid.\n",segment_index);
}
return ON_IsNotValid();
}
int seg_dim = m_segment[segment_index]->Dimension();
if ( seg_dim != dim )
{
if ( text_log )
text_log->Print("Polycurve segment[%d]->Dimension()=%d (should be %d).\n",segment_index,seg_dim,dim);
return ON_IsNotValid(); // all segments must have same dimension
}
if ( m_t[segment_index] >= m_t[segment_index+1] )
{
if ( text_log )
text_log->Print("Polycurve m_t[%d]=%g and m_t[%d]=%g (should be increasing)\n",
segment_index, m_t[segment_index],
segment_index+1, m_t[segment_index+1]);
return ON_IsNotValid(); // segment domain must be non-empty
}
if ( count > 1 && !bAllowGaps && m_segment[segment_index]->IsClosed() )
{
if ( text_log )
text_log->Print("Polycurve segment[%d] is closed (%d segments).\n",segment_index,count);
return ON_IsNotValid(); // closed segments not permitted in multi segment curve
}
}
if ( !bAllowGaps )
{
// check for gaps
int gap_index = FindNextGap(0);
if ( gap_index > 0 )
{
p0 = m_segment[gap_index-1]->PointAtEnd();
p1 = m_segment[gap_index]->PointAtStart();
double d = p0.DistanceTo(p1);
if ( text_log )
text_log->Print("Polycurve end of segment[%d] != start of segment[%d] (distance=%g)\n",
gap_index-1, gap_index, d );
return ON_IsNotValid(); // not contiguous
}
}
return true;
}
void ON_PolyCurve::Dump( ON_TextLog& dump ) const
{
const int count = Count();
int i;
ON_3dPoint segment_start = ON_3dPoint::UnsetPoint;
ON_3dPoint segment_end = ON_3dPoint::UnsetPoint;
double gap;
dump.Print( "ON_PolyCurve segment count = %d\n", count );
dump.PushIndent();
for ( i = 0; i < count; i++ )
{
if ( 0 != m_segment[i] )
segment_start = m_segment[i]->PointAtStart();
else
segment_start = ON_3dPoint::UnsetPoint;
gap = (segment_start.IsValid() && segment_end.IsValid())
? segment_start.DistanceTo(segment_end)
: ON_UNSET_VALUE;
dump.Print( "Segment %d: (%g,%g)", i+1, m_t[i], m_t[i+1] );
if ( i > 0 )
{
if ( ON_IsValid(gap) )
dump.Print(" gap = %.17g",gap);
else if ( !segment_start.IsValid() )
dump.Print(" invalid segment curve");
else if ( !segment_end.IsValid() )
dump.Print(" invalid previous segment curve");
}
dump.Print("\n");
dump.PushIndent();
if ( 0 == m_segment[i] )
{
dump.Print("null curve pointer\n");
segment_end = ON_3dPoint::UnsetPoint;
}
else
{
m_segment[i]->Dump(dump);
segment_end = m_segment[i]->PointAtEnd();
}
dump.PopIndent();
}
dump.PopIndent();
}
bool ON_PolyCurve::Write(
ON_BinaryArchive& file // open binary file
) const
{
// NOTE - if changed, check legacy I/O code
bool rc = file.Write3dmChunkVersion(1,0);
if (rc) {
int reserved1 = 0;
int reserved2 = 0;
const int count = Count();
int segment_index;
rc = file.WriteInt( count );
if (rc) file.WriteInt(reserved1);
if (rc) file.WriteInt(reserved2);
if (rc) {
// may be set in future
ON_BoundingBox bbox;
rc = file.WriteBoundingBox(bbox);
}
if (rc) rc = file.WriteArray( m_t );
for ( segment_index = 0; segment_index < count && rc; segment_index++ ) {
rc = file.WriteObject( *SegmentCurve(segment_index) );
}
}
return rc;
}
bool ON_PolyCurve::Read(
ON_BinaryArchive& file // open binary file
)
{
// NOTE - if changed, check legacy I/O code
Destroy();
int major_version = 0;
int minor_version = 0;
bool rc = file.Read3dmChunkVersion(&major_version,&minor_version);
if (rc)
{
ON_Object* obj;
ON_Curve* crv;
int segment_index;
int segment_count = 0;
int reserved1 = 0;
int reserved2 = 0;
rc = file.ReadInt(&segment_count);
if (rc) rc = file.ReadInt(&reserved1);
if (rc) rc = file.ReadInt(&reserved2);
if (rc) {
// may be set in future
ON_BoundingBox bbox;
rc = file.ReadBoundingBox(bbox);
}
if (rc) rc = file.ReadArray( m_t );
for ( segment_index = 0; segment_index < segment_count && rc; segment_index++ ) {
obj = 0;
crv = 0;
rc = file.ReadObject( &obj );
if (rc) {
crv = ON_Curve::Cast(obj);
if (crv) {
//Append(crv); - this one adds to m_t[]
m_segment.Append(crv);
}
else {
ON_ERROR("ON_PolyCurve::Read() - non ON_Curve object in segment list\n");
delete obj;
rc = false;
}
}
}
if ( rc && m_segment.Count()>0 &&
m_segment.Count()==segment_count && m_t.Count()==segment_count+1)
{
// remove "fuzz" in m_t[] domain array that is in some older files.
SanitizeDomain();
/*
double s, t, d0, d1, fuzz;
ON_Interval in0, in1;
in1 = SegmentCurve(0)->Domain();
d1 = in1.Length();
for ( segment_index = 1; segment_index < segment_count; segment_index++ )
{
t = m_t[segment_index];
in0 = in1;
d0 = d1;
in1 = SegmentCurve(segment_index)->Domain();
d1 = in1.Length();
s = in0[1];
if ( s != t && s == in1[0] && t > in0[0] && t < in1[1] )
{
fuzz = ((d0<=d1)?d0:d1)*ON_SQRT_EPSILON;
if ( fabs(t-s) <= fuzz )
m_t[segment_index] = s;
}
}
fuzz = d1*ON_SQRT_EPSILON;
t = m_t[segment_count];
s = in1[1];
if ( s != t && t > in1[0] && fabs(s-t) <= fuzz )
m_t[segment_count] = s;
*/
}
}
if (rc && file.ArchiveOpenNURBSVersion() < 200304080 )
{
// 8 April 2003 Dale Lear:
// Some archives written by earlier versions
// of Rhino had nested polycurves and polycurves with
// zero length segments. This code cleans up
// those problems. See RR 8932.
RemoveNesting();
//RemoveShortSegments(ON_ZERO_TOLERANCE);
}
return rc;
}
ON_Curve* ON_PolyCurve::DuplicateCurve() const
{
// Call DuplicateCurve on each segment to construct duplicate curve.
int cnt = Count();
ON_PolyCurve* dup_crv = new ON_PolyCurve( cnt );
dup_crv->CopyUserData(*this);
for( int i=0; i<cnt; i++){
const ON_Curve* seg = SegmentCurve(i);
if(seg)
dup_crv->Append( seg->DuplicateCurve() );
}
if( cnt == dup_crv->Count() )
dup_crv->SetParameterization( m_t);
return dup_crv;
}
ON_Interval ON_PolyCurve::Domain() const
{
ON_Interval d;
const int count = Count();
if ( count > 0 && count+1 == m_t.Count() && m_t[0] < m_t[count] ) {
d.Set(m_t[0],m_t[count]);
}
return d;
}
bool ON_PolyCurve::SetDomain( double t0, double t1 )
{
ON_Interval d0 = Domain();
ON_Interval d1(t0,t1);
bool rc = d1.IsIncreasing();
if ( rc && d0 != d1 )
{
int i, count = m_t.Count();
double s;
for ( i = 0; i < count; i++ )
{
s = d0.NormalizedParameterAt( m_t[i] );
m_t[i] = d1.ParameterAt( s );
}
DestroyRuntimeCache();
}
return rc;
}
bool ON_PolyCurve::ChangeDimension( int desired_dimension )
{
int i, count = m_segment.Count();
bool rc = (count>0);
for ( i = 0; i < count; i++ )
{
ON_Curve* curve = m_segment[i];
if ( 0 != curve )
{
if ( !curve->ChangeDimension(desired_dimension) )
rc = false;
}
else
rc = false;
}
return rc;
}
bool ON_PolyCurve::SetParameterization( const double* t )
{
bool rc = false;
int i, count = m_segment.Count()+1;
if ( count >= 2 && 0 != t && ON_UNSET_VALUE != t[0] )
{
for ( i = 1; i < count; i++ )
{
if ( t[i] == ON_UNSET_VALUE )
break;
if ( t[i-1] >= t[i] )
break;
}
if ( i == count )
{
m_t.Reserve(count);
m_t.SetCount(0);
m_t.Append( count, t );
rc = true;
}
}
return rc;
}
bool ON_PolyCurve::ChangeClosedCurveSeam( double t )
{
bool rc = IsClosed();
if ( rc )
{
DestroyRuntimeCache();
rc = false;
const int old_count = Count();
const ON_Interval old_dom = Domain();
ON_Curve* scrv = 0;
if ( old_count == 1 )
{
scrv = SegmentCurve(0);
if ( scrv )
{
ON_Interval sdom = scrv->Domain();
double s = ( old_dom == sdom )
? t
: sdom.ParameterAt( old_dom.NormalizedParameterAt(t) );
rc = scrv->ChangeClosedCurveSeam(s);
if ( rc )
SetDomain( t, t + old_dom.Length() );
}
}
else
{
double k = t;
if ( !old_dom.Includes(t) )
{
double s = old_dom.NormalizedParameterAt(t);
s = fmod(s,1.0);
if ( s < 0.0 )
s += 1.0;
k = old_dom.ParameterAt(s);
}
if ( old_dom.Includes(k,true) )
{
int segment_index = ON_NurbsSpanIndex(2,old_count+1,m_t.Array(),k,0,0);
scrv = m_segment[segment_index];
if ( k < m_t[segment_index] )
return false;
if ( k >= m_t[segment_index+1] )
return false;
int new_count = (k==m_t[segment_index]) ? old_count : old_count+1;
ON_Curve* sleft = 0;
ON_Curve* sright = 0;
if ( new_count > old_count )
{
ON_Interval subdom(m_t[segment_index], m_t[segment_index+1]);
double nt = subdom.NormalizedParameterAt(k);
ON_Interval Segdom = scrv->Domain();
double segt = Segdom.ParameterAt(nt);
rc = scrv->Split( segt, sleft, sright );
// Greg Arden 6 May 2003. Fixes TRR#10332. If split fails we break the
// curve between segments and adjust the parameterization
if(!rc){
if(nt>.5){
segment_index++;
if(segment_index<old_count)
scrv = m_segment[segment_index];
else
scrv = nullptr;
}
new_count--;
}
}
if(new_count==old_count)
{
sright = scrv;
scrv = 0;
rc = true;
}
if ( rc && segment_index<old_count)
{
m_segment[segment_index] = 0;//todo
ON_SimpleArray<ON_Curve*> new_c(new_count);
ON_SimpleArray<double> new_t(new_count+1);
new_c.Append(sright);
new_t.Append(k);
new_c.Append( old_count-segment_index-1, m_segment.Array()+segment_index+1);
new_t.Append( old_count-segment_index-1, m_t.Array()+segment_index+1);
int j = new_t.Count();
new_c.Append( segment_index, m_segment.Array() );
new_t.Append( segment_index, m_t.Array() );
if ( sleft )
{
new_c.Append(sleft);
new_t.Append(m_t[segment_index]);
}
new_t.Append(k);
double d = old_dom.Length();
while (j < new_t.Count() )
{
new_t[j] += d;
j++;
}
// take care when moving new_c pointers to m_segment
// so that we don't delete any curves.
memset( m_segment.Array(), 0, m_segment.Capacity()*sizeof( *m_segment.Array() ) );
m_segment.SetCount(0);
m_segment.Append( new_c.Count(), new_c.Array() );
m_t = new_t;
if ( scrv )
{
delete scrv;
scrv = 0;
}
}
}
else
{
// k is already the start or end of the existing curve
rc = true;
}
if ( rc )
SetDomain( t, t + old_dom.Length() );
}
}
return rc;
}
int ON_PolyCurve::SpanCount() const
{
int span_count = 0;
const int segment_count = Count();
int i, j;
for ( i = 0; i < segment_count; i++ ) {
if ( !m_segment[i] )
return false;
j = m_segment[i]->SpanCount();
if ( j == 0 )
return 0;
span_count += j;
}
return span_count;
}
bool ON_PolyCurve::GetSpanVector( // span "knots"
double* s // array of length SpanCount() + 1
) const
{
ON_Interval sp;
double t;
const int segment_count = Count();
int i, j, k;
for ( i = 0; i < segment_count; i++ ) {
if ( !m_segment[i] )
return false;
j = m_segment[i]->SpanCount();
if ( j == 0 )
return 0;
if ( !m_segment[i]->GetSpanVector( s ) )
return false;
sp.Set( m_t[i], m_t[i+1] );
ON_Interval segloc(s[0],s[j]);
if ( sp.Min() != s[0] || sp.Max() != s[j] ) {
for ( k = 0; k <= j; k++ ) {
t = segloc.NormalizedParameterAt(s[k]);
s[k] = sp.ParameterAt(t);
}
}
s += j;
}
return true;
}
int ON_PolyCurve::Degree() const
{
const int segment_count = Count();
int span_degree = 0;
int segment_index, d;
for ( segment_index = 0; segment_index < segment_count; segment_index++ ) {
if ( !m_segment[segment_index] )
return false;
d = m_segment[segment_index]->Degree();
if ( d <= 0 )
return 0;
if ( d > span_degree )
span_degree = d;
}
return span_degree;
}
bool
ON_PolyCurve::IsLinear( // true if curve locus is a line segment
double tolerance // tolerance to use when checking linearity
) const
{
bool rc = false;
int i, count = Count();
if ( count==1)
return m_segment[0]->IsLinear(tolerance);
else if ( count > 1 ) {
rc = true;
for ( i = 0; rc && i < count; i++ ) {
if ( !m_segment[i] )
rc = false;
else
rc = m_segment[i]->IsLinear(tolerance);
}
if (rc)
rc = ON_Curve::IsLinear(tolerance);
}
return rc;
}
int ON_PolyCurve::IsPolyline(
ON_SimpleArray<ON_3dPoint>* pline_points,
ON_SimpleArray<double>* pline_t
) const
{
int i, seg_i, seg_rc;
ON_Interval sdom, cdom;
int rc = 0;
if ( pline_points )
pline_points->SetCount(0);
if ( pline_t )
pline_t->SetCount(0);
const int seg_count = Count();
if ( seg_count == 1 )
{
if ( m_segment[0] )
rc = m_segment[0]->IsPolyline(pline_points,pline_t);
if (pline_t && rc > 0)
{
sdom.Set(m_t[0],m_t[1]);
cdom = m_segment[0]->Domain();
if ( sdom != cdom )
{
for ( i = 0; i < pline_t->Count(); i++ )
(*pline_t)[i] = sdom.ParameterAt(cdom.NormalizedParameterAt((*pline_t)[i]));
}
}
}
else if (seg_count > 1 )
{
ON_SimpleArray<ON_3dPoint> seg_points;
ON_SimpleArray<double> seg_t;
for ( seg_i = 0; seg_i < seg_count; seg_i++ )
{
seg_points.SetCount(0);
seg_t.SetCount(0);
seg_rc = m_segment[seg_i]->IsPolyline(pline_points?&seg_points:0,pline_t?&seg_t:0);
if ( seg_rc < 2 )
{
if ( pline_points )
pline_points->SetCount(0);
if ( pline_t )
pline_t->SetCount(0);
rc = 0;
break;
}
rc += seg_rc;
if ( seg_i )
rc--;
if ( pline_t )
{
sdom.Set( m_t[seg_i], m_t[seg_i+1] );
cdom = m_segment[seg_i]->Domain();
if ( sdom != cdom )
{
for ( i = 0; i < seg_t.Count(); i++ )
seg_t[i] = sdom.ParameterAt(cdom.NormalizedParameterAt(seg_t[i]));
}
if ( pline_t->Count() > 0 )
pline_t->Remove();
pline_t->Append( seg_t.Count(), seg_t.Array() );
}
if ( pline_points )
{
if ( pline_points->Count() > 0 )
pline_points->Remove();
pline_points->Append( seg_points.Count(), seg_points.Array() );
}
}
if(IsClosed() && pline_points && pline_points->Count() > 3)
{
// GBA 2/26/03. Make closed polylines spot on closed
*pline_points->Last() = *pline_points->First();
}
}
return rc;
}
bool
ON_PolyCurve::IsArc( // true if curve locus in an arc or circle
const ON_Plane* plane, // if not nullptr, test is performed in this plane
ON_Arc* arc, // if not nullptr and true is returned, then arc
// arc parameters are filled in
double tolerance // tolerance to use when checking linearity
) const
{
bool rc = false;
if ( 1 == m_segment.Count() && 0 != m_segment[0] )
{
rc = m_segment[0]->IsArc( plane, arc, tolerance )?true:false;
}
return rc;
}
static bool GetTestPlane( const ON_Curve& curve, ON_Plane& plane )
{
int i, j;
ON_3dPoint P, Q, R;
ON_3dVector X;
ON_Interval d = curve.Domain();
if ( !curve.Ev1Der( d[0], P, X ) )
{
return false;
}
if ( !X.Unitize() )
{
return false;
}
Q = P+X;
double best_dot = 1.0;
ON_3dPoint best_Y = ON_3dPoint::Origin;
for ( i = 2; i <= 16; i += 2 )
{
for ( j = 1; j < i; j += 2 )
{
R = curve.PointAt( d.ParameterAt( ((double)j)/((double)i) ) );
ON_3dVector Y = R - P;
if (!Y.Unitize())
continue;
if (! X.IsParallelTo (Y)){
if ( plane.CreateFromFrame( P, X, Y ) )
return true;
}
else {
double dot = fabs(X*Y);
if (dot < best_dot){
best_Y = Y;
best_dot = dot;
}
}
}
}
if (best_dot < 1.0){
if ( plane.CreateFromFrame( P, X, best_Y ) )
return true;
}
return false;
}
bool
ON_PolyCurve::IsPlanar(
ON_Plane* plane, // if not nullptr and true is returned, then plane parameters
// are filled in
double tolerance // tolerance to use when checking linearity
) const
{
if ( Dimension() == 2 )
{
return ON_Curve::IsPlanar(plane,tolerance);
}
bool rc = false;
ON_Plane test_plane;
const int count = Count();
const ON_Curve* crv = FirstSegmentCurve();
if ( count == 1 && crv )
rc = crv->IsPlanar( plane, tolerance );
else if ( count > 1 )