forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opennurbs_arccurve.cpp
1245 lines (1090 loc) · 30.2 KB
/
opennurbs_arccurve.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
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2012 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_ArcCurve,ON_Curve,"CF33BE2A-09B4-11d4-BFFB-0010830122F0");
ON_ArcCurve::ON_ArcCurve() ON_NOEXCEPT
{}
ON_ArcCurve::~ON_ArcCurve()
{}
ON_ArcCurve::ON_ArcCurve( const ON_ArcCurve& src )
: ON_Curve(src)
, m_arc(src.m_arc)
, m_t(src.m_t)
, m_dim(src.m_dim)
{}
ON_ArcCurve& ON_ArcCurve::operator=( const ON_ArcCurve& src )
{
if ( this != &src )
{
ON_Curve::operator=(src);
m_arc = src.m_arc;
m_t = src.m_t;
m_dim = src.m_dim;
}
return *this;
}
#if defined(ON_HAS_RVALUEREF)
ON_ArcCurve::ON_ArcCurve( ON_ArcCurve&& src) ON_NOEXCEPT
: ON_Curve(std::move(src))
, m_arc(std::move(src.m_arc))
, m_t(src.m_t)
, m_dim(src.m_dim)
{
}
ON_ArcCurve& ON_ArcCurve::operator=( ON_ArcCurve&& src)
{
if ( this != &src )
{
ON_Curve::operator=(std::move(src));
m_arc = std::move(src.m_arc);
m_t = src.m_t;
m_dim = src.m_dim;
}
return *this;
}
#endif
ON_ArcCurve::ON_ArcCurve( const ON_Arc& A )
{
m_arc = A;
m_t.m_t[0] = 0.0;
m_t.m_t[1] = m_arc.Length();
if ( m_t.m_t[1] <= 0.0 )
m_t.m_t[1] = 1.0;
m_dim = 3;
}
ON_ArcCurve::ON_ArcCurve( const ON_Circle& circle )
{
ON_ArcCurve::operator=(circle);
}
ON_ArcCurve::ON_ArcCurve( const ON_Arc& A, double t0, double t1 )
{
m_arc = A;
m_t.m_t[0] = t0;
m_t.m_t[1] = t1;
m_dim = 3;
}
ON_ArcCurve::ON_ArcCurve( const ON_Circle& circle, double t0, double t1 )
{
m_arc = circle;
m_t.m_t[0] = t0;
m_t.m_t[1] = t1;
m_dim = 3;
}
unsigned int ON_ArcCurve::SizeOf() const
{
unsigned int sz = ON_Curve::SizeOf();
sz += sizeof(*this) - sizeof(ON_Curve);
return sz;
}
ON__UINT32 ON_ArcCurve::DataCRC(ON__UINT32 current_remainder) const
{
current_remainder = ON_CRC32(current_remainder,sizeof(m_arc),&m_arc);
current_remainder = ON_CRC32(current_remainder,sizeof(m_t),&m_t);
current_remainder = ON_CRC32(current_remainder,sizeof(m_dim),&m_dim);
return current_remainder;
}
ON_ArcCurve& ON_ArcCurve::operator=( const ON_Arc& A )
{
m_arc = A;
m_t.m_t[0] = 0.0;
m_t.m_t[1] = A.Length();
if ( m_t.m_t[1] == 0.0 )
m_t.m_t[1] = 1.0;
m_dim = 3;
return *this;
}
ON_ArcCurve& ON_ArcCurve::operator=(const ON_Circle& circle)
{
m_arc = circle;
m_t.m_t[0] = 0.0;
m_t.m_t[1] = m_arc.Length();
if ( m_t.m_t[1] <= 0.0 )
m_t.m_t[1] = 1.0;
m_dim = 3;
return *this;
}
int ON_ArcCurve::Dimension() const
{
return m_dim;
}
bool ON_ArcCurve::GetBBox( // returns true if successful
double* boxmin, // minimum
double* boxmax, // maximum
bool bGrowBox
) const
{
bool rc = m_arc.IsValid();
if (rc) {
ON_BoundingBox bbox = m_arc.BoundingBox();
if ( bGrowBox ) {
if ( boxmin[0] > bbox.m_min.x ) boxmin[0] = bbox.m_min.x;
if ( boxmin[1] > bbox.m_min.y ) boxmin[1] = bbox.m_min.y;
if ( boxmax[0] < bbox.m_max.x ) boxmax[0] = bbox.m_max.x;
if ( boxmax[1] < bbox.m_max.y ) boxmax[1] = bbox.m_max.y;
if ( m_dim > 2 ) {
if ( boxmin[2] > bbox.m_min.z ) boxmin[2] = bbox.m_min.z;
if ( boxmax[2] < bbox.m_max.z ) boxmax[2] = bbox.m_max.z;
}
}
else {
boxmin[0] = bbox.m_min.x;
boxmin[1] = bbox.m_min.y;
boxmax[0] = bbox.m_max.x;
boxmax[1] = bbox.m_max.y;
if ( m_dim > 2 ) {
boxmin[2] = bbox.m_min.z;
boxmax[2] = bbox.m_max.z;
}
}
}
return rc;
}
bool
ON_ArcCurve::Transform( const ON_Xform& xform )
{
TransformUserData(xform);
DestroyCurveTree();
return m_arc.Transform( xform );
}
bool ON_ArcCurve::IsValid( ON_TextLog* text_log ) const
{
if ( !m_t.IsIncreasing() )
{
if ( 0 != text_log )
text_log->Print("ON_ArcCurve - m_t=(%g,%g) - it should be an increasing interval.\n",m_t[0],m_t[1]);
return false;
}
if ( !m_arc.IsValid() )
{
if ( 0 != text_log )
text_log->Print("ON_ArcCurve m_arc is not valid\n");
return false;
}
return true;
}
void ON_ArcCurve::Dump( ON_TextLog& dump ) const
{
dump.Print( "ON_ArcCurve: domain = [%g,%g]\n",m_t[0],m_t[1]);
dump.PushIndent();
dump.Print( "center = ");
dump.Print( m_arc.plane.origin );
dump.Print( "\nradius = %g\n",m_arc.radius);
dump.Print( "length = %g\n",m_arc.Length());
ON_3dPoint start = PointAtStart();
ON_3dPoint end = PointAtEnd();
dump.Print( "start = "); dump.Print(start);
dump.Print( "\nend = "); dump.Print(end); dump.Print("\n");
dump.PopIndent();
}
bool ON_ArcCurve::Write(
ON_BinaryArchive& file // open binary file
) const
{
bool rc = file.Write3dmChunkVersion(1,0);
if (rc)
{
rc = file.WriteArc( m_arc );
if (rc) rc = file.WriteInterval( m_t );
if (rc) rc = file.WriteInt(m_dim);
}
return rc;
}
bool ON_ArcCurve::Read(
ON_BinaryArchive& file // open binary file
)
{
int major_version = 0;
int minor_version = 0;
bool rc = file.Read3dmChunkVersion(&major_version,&minor_version);
if (rc)
{
if (major_version==1)
{
// common to all 1.x versions
rc = file.ReadArc( m_arc );
if (rc)
rc = file.ReadInterval( m_t );
if (rc)
rc = file.ReadInt(&m_dim);
if ( m_dim != 2 && m_dim != 3 )
m_dim = 3;
}
else
rc = 0;
}
return rc;
}
bool ON_ArcCurve::SetDomain( double t0, double t1 )
{
bool rc = false;
if ( t0 < t1 )
{
m_t.Set(t0,t1);
rc = true;
}
DestroyCurveTree();
return rc;
}
bool ON_ArcCurve::ChangeDimension( int desired_dimension )
{
bool rc = (desired_dimension>=2 && desired_dimension<=3);
if ( rc && m_dim != desired_dimension )
{
DestroyCurveTree();
if ( desired_dimension == 2 )
m_dim = 2;
else
m_dim = 3;
}
return rc;
}
ON_Interval ON_ArcCurve::Domain() const
{
return m_t;
}
bool ON_ArcCurve::ChangeClosedCurveSeam(
double t ){
bool rc = false;
if( IsCircle() ){
double angle_delta = m_t.NormalizedParameterAt(t);
angle_delta*= 2*ON_PI;
m_arc.Rotate(angle_delta, m_arc.plane.Normal());
m_t = ON_Interval( t, m_t[1] + t - m_t[0]);
rc = true;
}
return rc;
}
int ON_ArcCurve::SpanCount() const
{
return 1;
}
bool ON_ArcCurve::GetSpanVector( double* s ) const
{
s[0] = m_t[0];
s[1] = m_t[1];
return m_t.IsIncreasing();
}
int ON_ArcCurve::Degree() const
{
return 2;
}
bool
ON_ArcCurve::IsLinear( // true if curve locus is a line segment
double // tolerance - formal parameter intentionally ignored in this virtual function
) const
{
return false;
}
bool
ON_ArcCurve::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 = (plane) ? IsInPlane(*plane,tolerance) : true;
if (arc)
*arc = m_arc;
if (rc)
rc = IsValid();
return rc;
}
bool
ON_ArcCurve::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 ( m_dim == 2 )
{
return ON_Curve::IsPlanar(plane,tolerance);
}
if ( plane )
*plane = m_arc.plane;
return true;
}
bool
ON_ArcCurve::IsInPlane(
const ON_Plane& plane, // plane to test
double tolerance // tolerance to use when checking linearity
) const
{
return m_arc.IsInPlane( plane, tolerance );
}
bool
ON_ArcCurve::IsClosed() const
{
return m_arc.IsCircle();
}
bool
ON_ArcCurve::IsPeriodic() const
{
return m_arc.IsCircle();
}
bool
ON_ArcCurve::Reverse()
{
bool rc = m_arc.Reverse();
if (rc)
{
m_t.Reverse();
DestroyCurveTree();
}
return true;
}
bool ON_ArcCurve::SetStartPoint(ON_3dPoint start_point)
{
if (ON_Curve::SetStartPoint(start_point))
return true;
if (IsCircle())
return false;
bool rc = false;
if ( m_dim == 3 || start_point.z == 0.0 )
{
ON_3dPoint P;
ON_3dVector T;
double t = Domain()[1];
Ev1Der( t, P, T );
T = -T;
ON_Arc a;
rc = a.Create( P, T, start_point );
if ( rc )
{
a.Reverse();
m_arc = a;
}
else {
ON_3dPoint end_point = PointAt(Domain()[1]);
if (end_point.DistanceTo(start_point) < ON_ZERO_TOLERANCE*m_arc.Radius()){
//make arc into circle
m_arc.plane.xaxis = end_point - m_arc.Center();
m_arc.plane.xaxis.Unitize();
m_arc.plane.yaxis = ON_CrossProduct(m_arc.Normal(), m_arc.plane.xaxis);
m_arc.plane.yaxis.Unitize();
m_arc.SetAngleRadians(2.0*ON_PI);
rc = true;
}
}
}
DestroyCurveTree();
return rc;
}
bool ON_ArcCurve::SetEndPoint(ON_3dPoint end_point)
{
if (ON_Curve::SetEndPoint(end_point))
return true;
if (IsCircle())
return false;
bool rc = false;
if ( m_dim == 3 || end_point.z == 0.0 )
{
ON_3dPoint P;
ON_3dVector T;
double t = Domain()[0];
Ev1Der( t, P, T );
ON_Arc a;
rc = a.Create( P, T, end_point );
if ( rc )
{
m_arc = a;
}
else {
ON_3dPoint start_point = PointAt(Domain()[0]);
if (end_point.DistanceTo(start_point) < ON_ZERO_TOLERANCE*m_arc.Radius()){
//make arc into circle
m_arc.plane.xaxis = start_point - m_arc.Center();
m_arc.plane.xaxis.Unitize();
m_arc.plane.yaxis = ON_CrossProduct(m_arc.Normal(), m_arc.plane.xaxis);
m_arc.plane.yaxis.Unitize();
m_arc.SetAngleRadians(2.0*ON_PI);
rc = true;
}
}
}
DestroyCurveTree();
return rc;
}
bool ON_ArcCurve::Evaluate( // returns false if unable to evaluate
double t, // evaluation parameter
int der_count, // number of derivatives (>=0)
int v_stride, // v[] array stride (>=Dimension())
double* v, // v[] array of length stride*(ndir+1)
int, // side - formal parameter intentionally ignored in this virtual function
int* // hint - formal parameter intentionally ignored in this virtual function
) const
{
// The issue here is that ON_PI is a rational approximation of the "real" pi.
// Ideally sin(N*pi) would be zero and the bugs of July 2012 and RH-26341 that are
// discussed below would not exist. When N is large, N*ON_PI isn't close by any
// measure to a multiple of "real" pi. But, for smallish N that we are likely to
// encounter in practice, we want sin(N*ON_PI) to be zero in this evaluator.
// The multiple 4 is used because we felt is was reasonable for somebody to want
// this evaluator to apply the same special case handing to a and a + 2.0*ON_PI
// when fabs(a) <= 2.0*ON_PI.
static const double sin_of_pi = fabs(sin(4.0*ON_PI)) > fabs(sin(ON_PI))
? fabs(sin(4.0*ON_PI)) // the fabs(sin(4.0*ON_PI)) values is used in the tests we performed.
: fabs(sin(ON_PI));
static const double cos_of_pi_over_2 = fabs(cos(4.5*ON_PI)) > fabs(cos(0.5*ON_PI))
? fabs(cos(4.5*ON_PI)) // the fabs(cos(4.5*ON_PI)) values is used in the tests we performed.
: fabs(cos(0.5*ON_PI));
ON_3dVector d;
bool rc = false;
if ( m_t[0] < m_t[1] )
{
double rat = m_arc.DomainRadians().Length()/m_t.Length();
double scale = 1.0;
double a = m_arc.DomainRadians().ParameterAt( m_t.NormalizedParameterAt(t) );
// 12 July 2012 Dale Lear
// When evaluating circles centered at the origin
// a = ON_PI = 3.1415926535897931, c = -1.0 and s = 1.2246467991473532e-016.
// As a result the y coordinates that "should" be zero comes out as
// radius*1.2246467991473532e-016. When the radius is large (1.0e9 in the
// bug I was looking at), the y coordinate is big enough to cause other problems.
// When I added this comment I failed to insert the bug number, so I cannot
// provide more details as of May 6, 2014 and the changes for bug RH-26341.
double c = cos(a);
double s = sin(a);
// This test turned out to be too crude. The bug RH-26341
// is one example. The issue is that the trig function with the
// largest derivative has more pecise sensitivity to changes in
// angle and in orger to get as precise an evaluation as possible,
// it is inportant to allow non-zero values of one trig function
// even when the other is being rounded to +1 or -1.
//
////if ( fabs(c) < ON_EPSILON || fabs(s) > 1.0-ON_EPSILON )
////{
//// c = 0.0;
//// s = s < 0.0 ? -1.0 : 1.0;
////}
////else if ( fabs(s) < ON_EPSILON || fabs(c) > 1.0-ON_EPSILON )
////{
//// s = 0.0;
//// c = c < 0.0 ? -1.0 : 1.0;
////}
if (fabs(c) <= cos_of_pi_over_2)
{
c = 0.0;
s = s < 0.0 ? -1.0 : 1.0;
}
else if (fabs(s) <= sin_of_pi)
{
s = 0.0;
c = c < 0.0 ? -1.0 : 1.0;
}
c *= m_arc.radius;
s *= m_arc.radius;
ON_3dPoint p = m_arc.plane.origin + c*m_arc.plane.xaxis + s*m_arc.plane.yaxis;
v[0] = p.x;
v[1] = p.y;
if ( m_dim == 3 )
v[2] = p.z;
for ( int di = 1; di <= der_count; di++ ) {
scale*=rat;
a = c;
c = -s;
s = a;
d = c*m_arc.plane.xaxis + s*m_arc.plane.yaxis;
v += v_stride;
v[0] = d.x*scale;
v[1] = d.y*scale;
if ( m_dim == 3 )
v[2] = d.z*scale;
}
rc = true;
}
return rc;
}
bool ON_ArcCurve::Trim( const ON_Interval& in )
{
bool rc = in.IsIncreasing();
if (rc)
{
double t0 = m_t.NormalizedParameterAt(in.m_t[0]);
double t1 = m_t.NormalizedParameterAt(in.m_t[1]);
const ON_Interval arc_angle0 = m_arc.DomainRadians();
double a0 = arc_angle0.ParameterAt(t0);
double a1 = arc_angle0.ParameterAt(t1);
// Resulting ON_Arc must pass IsValid()
if ( a1 - a0 > ON_ZERO_TOLERANCE && m_arc.SetAngleIntervalRadians(ON_Interval(a0,a1)) )
{
m_t = in;
}
else
{
rc = false;
}
DestroyCurveTree();
}
return rc;
}
bool ON_ArcCurve::Extend(
const ON_Interval& domain
)
{
if (IsClosed()) return false;
double s0, s1;
bool changed = false;
GetDomain(&s0, &s1);
if (domain[0] < s0){
s0 = domain[0];
changed = true;
}
if (domain[1] > s1){
s1 = domain[1];
changed = true;
}
if (!changed) return false;
DestroyCurveTree();
double a0 = m_arc.Domain().ParameterAt(Domain().NormalizedParameterAt(s0));
double a1 = m_arc.Domain().ParameterAt(Domain().NormalizedParameterAt(s1));
if (a1 > a0+2.0*ON_PI) {
a1 = a0+2.0*ON_PI;
s1 = Domain().ParameterAt(m_arc.Domain().NormalizedParameterAt(a1));
}
m_arc.Trim(ON_Interval(a0, a1));
SetDomain(s0, s1);
return true;
}
bool ON_ArcCurve::Split(
double t,
ON_Curve*& left_side,
ON_Curve*& right_side
) const
{
// make sure t is strictly inside the arc's domain
ON_Interval arc_domain = Domain();
ON_Interval arc_angles = m_arc.DomainRadians();
if ( !arc_domain.Includes(t) )
return false;
double a = (arc_domain == arc_angles)
? t
: arc_angles.ParameterAt(arc_domain.NormalizedParameterAt(t));
if ( !arc_angles.Includes(a) )
return false;
// make sure input curves are ok.
ON_ArcCurve* left_arc = 0;
ON_ArcCurve* right_arc = 0;
if ( 0 != left_side )
{
if ( left_side == right_side )
return false;
left_arc = ON_ArcCurve::Cast(left_side);
if ( 0 == left_arc )
return false;
left_arc->DestroyCurveTree();
}
if ( 0 != right_side )
{
right_arc = ON_ArcCurve::Cast(right_side);
if ( 0 == right_arc )
return false;
right_arc->DestroyCurveTree();
}
if ( 0 == left_arc )
{
left_arc = new ON_ArcCurve( *this );
}
else if ( this != left_arc )
{
left_arc->operator=(*this);
}
if ( 0 == right_arc )
{
right_arc = new ON_ArcCurve( *this );
}
else if ( this != right_arc )
{
right_arc->operator=(*this);
}
bool rc = false;
if ( this != left_arc )
{
rc = left_arc->Trim( ON_Interval( arc_domain[0], t ) );
if (rc)
rc = right_arc->Trim( ON_Interval( t, arc_domain[1] ) );
}
else
{
rc = right_arc->Trim( ON_Interval( t, arc_domain[1] ) );
if (rc)
rc = left_arc->Trim( ON_Interval( arc_domain[0], t ) );
}
if ( rc )
{
if ( 0 == left_side )
left_side = left_arc;
if ( 0 == right_side )
right_side = right_arc;
}
else
{
if ( 0 == left_side && this != left_arc )
{
delete left_arc;
left_arc = 0;
}
if ( 0 == right_side && this != right_arc )
{
delete right_arc;
right_arc = 0;
}
}
return rc;
}
static double ArcDeFuzz( double d )
{
// 0.0078125 = 1.0/128.0 exactly
// Using 2^n scale factors insures no loss of precision
// but preserves fractional values that are multiples of 1/128.
//
// Fuzz tol should be scale * 2^m * ON_EPSILON for m >= 1
double f, i;
f = modf( d*128.0, &i );
if ( f != 0.0 && fabs(f) <= 1024.0*ON_EPSILON ) {
d = i*0.0078125;
}
return d;
}
static bool NurbsCurveArc ( const ON_Arc& arc, int dim, ON_NurbsCurve& nurb )
{
if ( !arc.IsValid() )
return false;
// makes a quadratic nurbs arc
const ON_3dPoint center = arc.Center();
double angle = arc.AngleRadians();
ON_Interval dom = arc.DomainRadians();
const double angle0 = dom[0];
const double angle1 = dom[1];
ON_3dPoint start_point = arc.StartPoint();
//ON_3dPoint mid_point = arc.PointAt(angle0 + 0.5*angle);
ON_3dPoint end_point = arc.IsCircle() ? start_point : arc.EndPoint();
ON_4dPoint CV[9];
double knot[10];
double a, b, c, w, winv;
double *cv;
int j, span_count, cv_count;
a = (0.5 + ON_SQRT_EPSILON)*ON_PI;
if (angle <= a)
span_count = 1;
else if (angle <= 2.0*a)
span_count = 2;
else if (angle <= 3.0*a)
span_count = 4; // TODO - make a 3 span case
else
span_count = 4;
cv_count = 2*span_count + 1;
switch(span_count) {
case 1:
CV[0] = start_point;
CV[1] = arc.PointAt(angle0 + 0.50*angle);
CV[2] = end_point;
break;
case 2:
CV[0] = start_point;
CV[1] = arc.PointAt(angle0 + 0.25*angle);
CV[2] = arc.PointAt(angle0 + 0.50*angle);
CV[3] = arc.PointAt(angle0 + 0.75*angle);
CV[4] = end_point;
angle *= 0.5;
break;
default: // 4 spans
CV[0] = start_point;
CV[1] = arc.PointAt(angle0 + 0.125*angle);
CV[2] = arc.PointAt(angle0 + 0.250*angle);
CV[3] = arc.PointAt(angle0 + 0.375*angle);
CV[4] = arc.PointAt(angle0 + 0.500*angle);
CV[5] = arc.PointAt(angle0 + 0.625*angle);
CV[6] = arc.PointAt(angle0 + 0.750*angle);
CV[7] = arc.PointAt(angle0 + 0.875*angle);
CV[8] = end_point;
angle *= 0.25;
break;
}
a = cos(0.5*angle);
b = a - 1.0;
//c = (radius > 0.0) ? radius*angle : angle;
c = angle;
span_count *= 2;
knot[0] = knot[1] = angle0; //0.0;
for (j = 1; j < span_count; j += 2) {
CV[j].x += b * center.x;
CV[j].y += b * center.y;
CV[j].z += b * center.z;
CV[j].w = a;
CV[j+1].w = 1.0;
knot[j+1] = knot[j+2] = knot[j-1] + c;
}
knot[cv_count-1] = knot[cv_count] = angle1;
for ( j = 1; j < span_count; j += 2 ) {
w = CV[j].w;
winv = 1.0/w;
a = CV[j].x*winv;
b = ArcDeFuzz(a);
if ( a != b ) {
CV[j].x = b*w;
}
a = CV[j].y*winv;
b = ArcDeFuzz(a);
if ( a != b ) {
CV[j].y = b*w;
}
a = CV[j].z*winv;
b = ArcDeFuzz(a);
if ( a != b ) {
CV[j].z = b*w;
}
}
nurb.m_dim = (dim==2) ? 2 : 3;
nurb.m_is_rat = 1;
nurb.m_order = 3;
nurb.m_cv_count = cv_count;
nurb.m_cv_stride = (dim==2 ? 3 : 4);
nurb.ReserveCVCapacity( nurb.m_cv_stride*cv_count );
nurb.ReserveKnotCapacity( cv_count+1 );
for ( j = 0; j < cv_count; j++ ) {
cv = nurb.CV(j);
cv[0] = CV[j].x;
cv[1] = CV[j].y;
if ( dim == 2 ) {
cv[2] = CV[j].w;
}
else {
cv[2] = CV[j].z;
cv[3] = CV[j].w;
}
nurb.m_knot[j] = knot[j];
}
nurb.m_knot[cv_count] = knot[cv_count];
return true;
}
int ON_Arc::GetNurbForm( ON_NurbsCurve& nurbscurve ) const
{
bool rc = NurbsCurveArc ( *this, 3, nurbscurve );
return (rc) ? 2 : 0;
}
bool ON_Arc::GetRadianFromNurbFormParameter(double NurbParameter, double* RadianParameter ) const
{
// TRR#53994.
// 16-Sept-09 Replaced this code so we dont use LocalClosestPoint.
// In addition to being slower than neccessary the old method suffered from getting the
// wrong answer at the seam of a full circle, This probably only happened with large
// coordinates where many digits of precision get lost.
ON_NurbsCurve crv;
if( !IsValid()|| RadianParameter==nullptr)
return false;
ON_Interval dom= Domain();
if( fabs(NurbParameter- dom[0])<=2.0*ON_EPSILON*fabs(dom[0]))
{
*RadianParameter=dom[0];
return true;
}
else if( fabs(NurbParameter- dom[1])<=2.0*ON_EPSILON*fabs(dom[1]))
{
*RadianParameter=dom[1];
return true;
}
if( !dom.Includes(NurbParameter) )
return false;
if( !GetNurbForm(crv) )
return false;
ON_3dPoint cp;
cp = crv.PointAt(NurbParameter);
cp -= Center();
double x = ON_DotProduct(Plane().Xaxis(), cp);
double y = ON_DotProduct(Plane().Yaxis(), cp);
double theta = atan2(y,x);
theta -= floor( (theta-dom[0])/(2*ON_PI)) * 2* ON_PI;
if( theta<dom[0] || theta>dom[1])
{
// 24-May-2010 GBA
// We got outside of the domain because of a numerical error somewhere.
// The only case that matters is because we are right near an endpoint.
// So we need to decide which endpoint to return. (Other possibilities
// are that the radius is way to small relative to the coordinates of the center.
// In this case the circle is just numerical noise around the center anyway.)
if( NurbParameter< (dom[0]+dom[1])/2.0)
theta = dom[0];
else
theta = dom[1];
}
// Carefully handle the potential discontinuity of this function
// when the domain is a full circle
if(dom.Length()>.99999*2.0*ON_PI)
{
double np_theta = dom.NormalizedParameterAt(theta);
double np_nurb = dom.NormalizedParameterAt(NurbParameter);
if( np_nurb<.01 && np_theta>.99)
theta = dom[0];
else if( np_nurb>.99 && np_theta<.01)
theta = dom[1];
}
*RadianParameter = theta;
return true;
}
bool ON_Arc::GetNurbFormParameterFromRadian(double RadianParameter, double* NurbParameter ) const
{
if(!IsValid() || NurbParameter==nullptr)
return false;
ON_Interval ADomain = DomainRadians();
double endtol = 10.0*ON_EPSILON*(fabs(ADomain[0]) + fabs(ADomain[1]));
double del = RadianParameter - ADomain[0];
if(del <= endtol && del >= -ON_SQRT_EPSILON)
{
*NurbParameter=ADomain[0];
return true;
}
else {
del = ADomain[1] - RadianParameter;
if(del <= endtol && del >= -ON_SQRT_EPSILON){
*NurbParameter=ADomain[1];
return true;
}
}
if( !ADomain.Includes(RadianParameter ) )
return false;
ON_NurbsCurve crv;
if( !GetNurbForm(crv))
return false;
//Isolate a bezier that contains the solution
int cnt = crv.SpanCount();
int si =0; //get span index
int ki=0; //knot index
double ang = ADomain[0];
ON_3dPoint cp;
cp = crv.PointAt( crv.Knot(0) ) - Center();
double x = ON_DotProduct(Plane().Xaxis(),cp);
double y = ON_DotProduct(Plane().Yaxis(),cp);