forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_revsurface.cpp
2415 lines (2206 loc) · 58.3 KB
/
opennurbs_revsurface.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_RevSurface, ON_Surface, "A16220D3-163B-11d4-8000-0010830122F0");
void ON_RevSurface::DestroyRuntimeCache( bool bDelete )
{
ON_Surface::DestroyRuntimeCache(bDelete);
if ( 0 != m_curve )
m_curve->DestroyRuntimeCache(bDelete);
// 15 August 2003 Dale Lear
// Added the call to destroy m_bbox.
m_bbox.Destroy();
}
ON_RevSurface* ON_RevSurface::New()
{
return new ON_RevSurface();
}
ON_RevSurface* ON_RevSurface::New( const ON_RevSurface& rev_surface )
{
return new ON_RevSurface(rev_surface);
}
ON_RevSurface::ON_RevSurface() : m_curve(0),
m_axis( ON_3dPoint::Origin, ON_3dVector::ZAxis ),
m_angle( 0.0, 2.0*ON_PI ),
m_t( 0.0, 2.0*ON_PI ),
m_bTransposed(0)
{
ON__SET__THIS__PTR(m_s_ON_RevSurface_ptr);
}
ON_RevSurface::~ON_RevSurface()
{
Destroy();
}
void ON_RevSurface::Destroy()
{
DestroySurfaceTree();
if ( m_curve)
{
delete m_curve;
m_curve = 0;
}
m_axis.Create( ON_3dPoint::Origin, ON_3dVector::ZAxis );
m_angle.Set(0.0,2.0*ON_PI);
m_t = m_angle;
m_bTransposed = false;
m_bbox.Destroy();
}
ON_RevSurface::ON_RevSurface( const ON_RevSurface& src ) : ON_Surface(src)
{
ON__SET__THIS__PTR(m_s_ON_RevSurface_ptr);
m_curve = src.m_curve ? src.m_curve->Duplicate() : nullptr;
m_axis = src.m_axis;
m_angle = src.m_angle;
m_t = src.m_t;
m_bTransposed = src.m_bTransposed;
m_bbox = src.m_bbox;
}
unsigned int ON_RevSurface::SizeOf() const
{
unsigned int sz = ON_Surface::SizeOf();
if ( m_curve )
sz += m_curve->SizeOf();
return sz;
}
ON__UINT32 ON_RevSurface::DataCRC(ON__UINT32 current_remainder) const
{
if ( m_curve )
current_remainder = m_curve->DataCRC(current_remainder);
current_remainder = ON_CRC32(current_remainder,sizeof(m_axis),&m_axis);
current_remainder = ON_CRC32(current_remainder,sizeof(m_angle),&m_angle);
current_remainder = ON_CRC32(current_remainder,sizeof(m_t),&m_t);
current_remainder = ON_CRC32(current_remainder,sizeof(m_bTransposed),&m_bTransposed);
return current_remainder;
}
ON_RevSurface& ON_RevSurface::operator=( const ON_RevSurface& src )
{
if ( this != &src )
{
Destroy();
ON_Surface::operator=(src);
if ( src.m_curve )
m_curve = src.m_curve->Duplicate();
m_axis = src.m_axis;
m_angle = src.m_angle;
m_t = src.m_t;
m_bTransposed = src.m_bTransposed;
m_bbox = src.m_bbox;
}
return *this;
}
bool ON_RevSurface::SetAngleRadians(
double start_angle_radians,
double end_angle_radians
)
{
bool rc = false;
double d = end_angle_radians-start_angle_radians;
if ( d >= 0.0 )
{
if ( d <= ON_ZERO_TOLERANCE || d > 2.0*ON_PI )
{
end_angle_radians = start_angle_radians + 2.0*ON_PI;
}
m_angle.Set( start_angle_radians, end_angle_radians );
rc = true;
DestroySurfaceTree();
}
return rc;
}
bool ON_RevSurface::SetAngleDegrees (
double start_angle_degrees,
double end_angle_degrees
)
{
return SetAngleRadians(
start_angle_degrees*ON_PI/180.0,
end_angle_degrees*ON_PI/180.0
);
}
bool ON_RevSurface::IsValid( ON_TextLog* text_log ) const
{
if ( !m_curve )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_curve is nullptr.\n");
return false;
}
if ( !m_curve->IsValid(text_log) )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_curve is not valid.\n");
return false;
}
int dim = m_curve->Dimension();
if ( dim != 3 )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_curve->Dimension()=%d (should be 3).\n",dim);
return false;
}
if ( !m_axis.IsValid() )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_axis is not valid.\n");
return false;
}
if ( !m_angle.IsIncreasing() )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_angle = (%g,%g) (should be an increasing interval)\n",
m_angle[0],m_angle[1]);
return false;
}
double length = m_angle.Length();
if ( length > 2.0*ON_PI + ON_ZERO_TOLERANCE )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_angle.Length() = %g (should be <= 2*pi radians).\n",length);
return false;
}
if ( m_angle.Length() <= ON_ZERO_TOLERANCE )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_angle.Length() = %g (should be > ON_ZERO_TOLERANCE).\n",length);
return false;
}
if ( !m_t.IsIncreasing() )
{
if ( text_log )
text_log->Print( "ON_RevSurface.m_t = (%g,%g) (should be an increasing interval)\n",
m_t[0],m_t[1]);
return false;
}
return true;
}
void ON_RevSurface::Dump( ON_TextLog& dump ) const
{
ON_Object::Dump(dump); // print class id
dump.PushIndent();
if ( m_bTransposed )
dump.Print("Paramerization: (curve,angle)\n");
else
dump.Print("Paramerization: (angle,curve)\n");
dump.Print("Axis: ");
dump.Print(m_axis.from);
dump.Print(" to ");
dump.Print(m_axis.to);
dump.Print("\n");
dump.Print("Rotation angle: %g to %g radians.\n",m_angle[0],m_angle[1]);
dump.Print("Angle evaluation parameter interval: [%g,%g].\n",m_t[0],m_t[1]);
if ( m_curve ) {
dump.Print("Revolute: \n");
dump.PushIndent();
m_curve->Dump(dump);
dump.PopIndent();
}
dump.PopIndent();
}
bool ON_RevSurface::Write( ON_BinaryArchive& file ) const
{
bool rc = file.Write3dmChunkVersion(2,0);
if (rc)
{
rc = file.WriteLine( m_axis );
rc = file.WriteInterval( m_angle );
rc = file.WriteInterval( m_t );
rc = file.WriteBoundingBox( m_bbox );
rc = file.WriteInt( m_bTransposed?1:0 );
if ( m_curve )
{
rc = file.WriteChar((char)1);
if (rc) rc = file.WriteObject(*m_curve);
}
else
{
rc = file.WriteChar((char)0);
}
}
return rc;
}
bool ON_RevSurface::Read( ON_BinaryArchive& file )
{
int major_version = 0;
int minor_version = 0;
char bHaveCurve = 0;
bool rc = file.Read3dmChunkVersion(&major_version,&minor_version);
if (rc && major_version == 1)
{
rc = file.ReadLine( m_axis );
rc = file.ReadInterval( m_angle );
rc = file.ReadBoundingBox( m_bbox );
int bTransposedAsInt = m_bTransposed ? 1 : 0;
rc = file.ReadInt( &bTransposedAsInt );
if (rc)
m_bTransposed = bTransposedAsInt ? true : false;
rc = file.ReadChar( &bHaveCurve );
if ( bHaveCurve )
{
ON_Object* obj = 0;
rc = file.ReadObject(&obj);
if ( obj )
{
m_curve = ON_Curve::Cast(obj);
if ( !m_curve )
delete obj;
}
}
m_t[0] = m_angle.Min();
m_t[1] = m_angle.Max();
}
else if (rc && major_version == 2)
{
rc = file.ReadLine( m_axis );
rc = file.ReadInterval( m_angle );
rc = file.ReadInterval( m_t );
rc = file.ReadBoundingBox( m_bbox );
int bTransposedAsInt = m_bTransposed ? 1 : 0;
rc = file.ReadInt( &bTransposedAsInt );
if (rc)
m_bTransposed = bTransposedAsInt ? true : false;
rc = file.ReadChar( &bHaveCurve );
if ( bHaveCurve )
{
ON_Object* obj = 0;
rc = file.ReadObject(&obj);
if ( obj )
{
m_curve = ON_Curve::Cast(obj);
if ( !m_curve )
delete obj;
}
}
}
return rc;
}
int ON_RevSurface::Dimension() const
{
return 3;
}
bool ON_RevSurface::Transform( const ON_Xform& xform )
{
DestroyRuntimeCache();
TransformUserData(xform);
bool rc = (m_curve) ? m_curve->Transform(xform) : false;
ON_3dVector X, Y, Z;
Z = m_axis.Tangent();
X.PerpendicularTo( Z );
X.Unitize();
Y = ON_CrossProduct( Z, X );
if ( !m_axis.Transform(xform) )
rc = false;
ON_3dVector transZ = m_axis.Tangent();
if ( transZ.Length() == 0.0 )
{
// transformation collapsed axis
m_axis.to = m_axis.from + Z;
}
else
{
// see if axis needs to be reversed.
// (Happens with transformations that
// have negative determinant - like mirroring.)
ON_3dVector transX = xform*X;
ON_3dVector transY = xform*Y;
ON_3dVector transXxY = ON_CrossProduct( transX, transY );
double d = transXxY*transZ;
if ( d < 0.0 )
m_axis.to = m_axis.from - m_axis.Direction();
}
m_bbox.Destroy();
m_bbox = BoundingBox();
return rc;
}
bool ON_RevSurface::Evaluate( // returns false if unable to evaluate
double s, // angle
double t, // curve_parameter
// evaluation parameters
int der_count, // number of derivatives (>=0)
int v_stride, // array stride (>=Dimension())
double* v, // array of length stride*(ndir+1)*(ndir+2)/2
int side, // optional - determines which quadrant to evaluate from
// 0 = default
// 1 from NE quadrant
// 2 from NW quadrant
// 3 from SW quadrant
// 4 from SE quadrant
int* hint // optional - evaluation hint (int[2]) used to speed
// repeated evaluations
) const
{
bool rc = false;
double ds = 1.0;
double x,y,z;
int i, j, k, src_i, dst_i;
ON_3dPoint pt;
if ( m_bTransposed )
{
x = s; s = t; t = x;
if ( side == 2 ) side = 4; else if ( side == 4 ) side = 2;
}
if ( m_t != m_angle )
{
if ( m_t.m_t[1] != m_t.m_t[0] )
{
ds = (m_angle.m_t[1] - m_angle.m_t[0])/(m_t.m_t[1] - m_t.m_t[0]);
x = m_t.NormalizedParameterAt(s);
y = m_angle.ParameterAt(x);
s = y;
}
}
double a = cos(s);
double b = sin(s);
const double ca[4] = {a, -b, -a, b}; // cosine derivatives
const double sa[4] = {b, a, -b, -a}; // sine derivatives
const int curve_dim = m_curve ? m_curve->Dimension() : 0;
if ( curve_dim == 2 || curve_dim == 3 )
{
int curve_side = 0;
switch(side)
{
case 1:
case 2:
curve_side = 1;
break;
case 3:
case 4:
curve_side = -1;
break;
}
rc = m_curve->Evaluate( t, der_count, v_stride, v, curve_side, hint )?true:false;
if ( rc )
{
ON_3dVector zaxis = m_axis.Tangent();
ON_3dVector xaxis, yaxis;
xaxis.PerpendicularTo(zaxis);
xaxis.Unitize();
yaxis = ON_CrossProduct(zaxis,xaxis);
// move curve derivatives to pure t partial spaces in v[]
if ( curve_dim == 2 )
{
for ( i = der_count; i >= 1; i-- )
{
// move curve derivative to proper spots
src_i = v_stride*i;
dst_i = v_stride*((i+1)*(i+2)/2 - 1);
v[dst_i++] = v[src_i++];
v[dst_i++] = 0.0;
v[dst_i] = v[src_i];
}
}
else
{
for ( i = der_count; i >= 1; i-- )
{
// move curve derivative to proper spots
src_i = v_stride*i;
dst_i = v_stride*((i+1)*(i+2)/2 - 1);
v[dst_i++] = v[src_i++];
v[dst_i++] = v[src_i++];
v[dst_i] = v[src_i];
}
}
// convert location coordinates to local frame with origin at m_axis.from
{
pt = ON_3dPoint(v)-m_axis.from;
v[0] = pt*xaxis;
v[1] = pt*yaxis;
v[2] = pt*zaxis;
}
// convert curve derivative coordinates to local frame
for ( i = 1; i <= der_count; i++ )
{
dst_i = v_stride*((i+1)*(i+2)/2 - 1);
pt = ON_3dPoint(v+dst_i); // pt = curve derivative in world coords
v[dst_i++] = pt*xaxis;
v[dst_i++] = pt*yaxis;
v[dst_i] = pt*zaxis;
}
for ( i = der_count; i >= 0; i-- )
{
// i = total order of derivative
double f = 1.0; // f = chain rule scale factor
for ( j = i; j >= 0; j-- )
{
// j = number of partials w.r.t curve parameter
// i-j = number of partials w.r.t angular parameter
dst_i = v_stride*(i*(i+1)/2 + j); //
src_i = v_stride*((j+1)*(j+2)/2 - 1); // curve derivative
k=(i-j)%4;
a = f*ca[k];
b = f*sa[k];
f *= ds;
// calculate derivative in local frame
x = a*v[src_i] - b*v[src_i+1];
y = b*v[src_i] + a*v[src_i+1];
z = (j<i) ? 0.0 : v[src_i+2];
// store answer in world coordinates
pt = x*xaxis + y*yaxis + z*zaxis;
v[dst_i++] = pt.x;
v[dst_i++] = pt.y;
v[dst_i] = pt.z;
}
}
// translate location
v[0] += m_axis.from.x;
v[1] += m_axis.from.y;
v[2] += m_axis.from.z;
if ( m_bTransposed )
{
for ( i = 1; i <= der_count; i++ )
{
for ( j = 0, k = i; j < k; j++, k-- )
{
dst_i = i*(i+1)/2;
src_i = dst_i + k;
dst_i += j;
src_i *= v_stride;
dst_i *= v_stride;
x = v[src_i]; v[src_i++] = v[dst_i]; v[dst_i++] = x;
x = v[src_i]; v[src_i++] = v[dst_i]; v[dst_i++] = x;
x = v[src_i]; v[src_i] = v[dst_i]; v[dst_i] = x;
}
}
}
}
}
return rc;
}
class ON_RevolutionTensor : public ON_TensorProduct
{
public:
ON_3dPoint O;
ON_3dVector X;
ON_3dVector Y;
ON_3dVector Z;
int DimensionA() const;
int DimensionB() const;
int DimensionC() const;
bool Evaluate( double, // a
const double*, // A
double, // b
const double*, // B
double* // C
);
};
int ON_RevolutionTensor::DimensionA() const
{
return 2;
}
int ON_RevolutionTensor::DimensionB() const
{
return 3;
}
int ON_RevolutionTensor::DimensionC() const
{
return 3;
}
bool ON_RevolutionTensor::Evaluate( double a, const double* ArcPoint, double b, const double* ShapePoint, double* SrfPoint )
{
double x, y, z, c, s, rx, ry, A[2], B[3];
if (a != 1.0) {
A[0] = a*ArcPoint[0];
A[1] = a*ArcPoint[1];
ArcPoint = A;
}
if (b != 1.0) {
B[0] = b*ShapePoint[0];
B[1] = b*ShapePoint[1];
B[2] = b*ShapePoint[2];
ShapePoint = B;
}
x = (ShapePoint[0] - O.x)*X.x + (ShapePoint[1] - O.y)*X.y + (ShapePoint[2] - O.z)*X.z;
y = (ShapePoint[0] - O.x)*Y.x + (ShapePoint[1] - O.y)*Y.y + (ShapePoint[2] - O.z)*Y.z;
z = (ShapePoint[0] - O.x)*Z.x + (ShapePoint[1] - O.y)*Z.y + (ShapePoint[2] - O.z)*Z.z;
c = ArcPoint[0];
s = ArcPoint[1];
rx = c*x - s*y;
ry = s*x + c*y;
SrfPoint[0] = O.x + rx*X.x + ry*Y.x + z*Z.x;
SrfPoint[1] = O.y + rx*X.y + ry*Y.y + z*Z.y;
SrfPoint[2] = O.z + rx*X.z + ry*Y.z + z*Z.z;
return true;
}
int ON_RevSurface::GetNurbForm(class ON_NurbsSurface& srf , double tolerance ) const
{
int rc = 0;
if ( 0 != m_curve )
{
ON_NurbsCurve a, c;
ON_Arc arc;
arc.plane.CreateFromNormal( ON_3dPoint::Origin, ON_3dVector::ZAxis );
arc.radius = 1.0;
arc.SetAngleRadians(m_angle[1]-m_angle[0]);
if ( arc.GetNurbForm(a) )
{
if ( m_t.IsIncreasing() )
a.SetDomain( m_t[0], m_t[1] );
rc = m_curve->GetNurbForm(c,tolerance);
if (rc)
{
if ( 2 == c.m_dim )
{
// Increasing the dimension of a 2d curve to 3d fixes
// was added to make the Scale1D operation in
// bug # 103845 work.
ON_WARNING("ON_RevSurface.m_curve is 2-dimensional.");
c.ChangeDimension(3);
}
if ( 3 != c.m_dim )
{
ON_ERROR("ON_RevSurface.m_curve is not valid.");
return 0;
}
if ( m_angle[0] != 0.0 )
{
c.Rotate( m_angle[0], m_axis.Direction(), m_axis.from );
}
ON_RevolutionTensor rho;
rho.O = m_axis.from;
rho.Z = m_axis.Direction();
rho.Z.Unitize();
rho.X.PerpendicularTo(rho.Z);
rho.X.Unitize();
rho.Y = ON_CrossProduct(rho.Z,rho.X);
rho.Y.Unitize();
if ( !srf.TensorProduct( a, c, rho ) )
{
// Testing for false here prevents crashes
// when and was added as part of investigating
// bug # 103845. A change a few lines up
// made it so that particular bug no longer
// fails to create a nurbs surface.
return 0;
}
// make singular points "spot on"
ON_3dPoint C0 = c.PointAtStart();
ON_3dPoint C1 = c.PointAtEnd();
ON_3dPoint A0, A1;
ON_4dPoint CV;
double t0 = ON_UNSET_VALUE;
double t1 = ON_UNSET_VALUE;
if (m_axis.ClosestPointTo(C0,&t0) && ON_IsValid(t0) )
{
A0 = m_axis.PointAt(t0);
if ( C0.DistanceTo(A0) <= ON_ZERO_TOLERANCE )
{
// SouthPole
int j = 0;
for ( int i = 0; i < srf.m_cv_count[0]; i++ )
{
CV.w = srf.Weight(i,j);
CV.x = CV.w*A0.x;
CV.y = CV.w*A0.y;
CV.z = CV.w*A0.z;
srf.SetCV(i,j,CV);
}
}
}
if (m_axis.ClosestPointTo(C1,&t1) && ON_IsValid(t1) )
{
A1 = m_axis.PointAt(t1);
if ( C1.DistanceTo(A1) <= ON_ZERO_TOLERANCE )
{
// NorthPole
int j = srf.m_cv_count[1]-1;
for ( int i = 0; i < srf.m_cv_count[0]; i++ )
{
CV.w = srf.Weight(i,j);
CV.x = CV.w*A1.x;
CV.y = CV.w*A1.y;
CV.z = CV.w*A1.z;
srf.SetCV(i,j,CV);
}
}
}
if ( m_bTransposed )
srf.Transpose();
}
}
}
return (rc > 0) ? 2 : 0;
}
int ON_RevSurface::HasNurbForm() const
{
if (!IsValid())
return 0;
return 2;
}
bool ON_RevSurface::GetSurfaceParameterFromNurbFormParameter(
double nurbs_s, double nurbs_t,
double* surface_s, double* surface_t
) const
{
// NOTE: overrides ON_Surface virtual function
bool rc = (0 != m_curve);
if ( m_bTransposed )
{
double* pTemp = surface_s; surface_s = surface_t; surface_t = pTemp;
double temp = nurbs_s; nurbs_s = nurbs_t; nurbs_t = temp;
}
*surface_s = nurbs_s;
*surface_t = nurbs_t;
ON_Circle circle( ON_xy_plane, 1.0 );
ON_Arc arc( circle, m_angle );
ON_ArcCurve arc_curve(arc, m_t[0], m_t[1]);
if ( !arc_curve.GetCurveParameterFromNurbFormParameter( nurbs_s, surface_s ) )
rc = false;
if ( m_curve )
{
if (!m_curve->GetCurveParameterFromNurbFormParameter( nurbs_t, surface_t ))
rc = false;
}
return rc;
}
bool ON_RevSurface::GetNurbFormParameterFromSurfaceParameter(
double surface_s, double surface_t,
double* nurbs_s, double* nurbs_t
) const
{
// NOTE: overrides ON_Surface virtual function
bool rc = (0 != m_curve);
if ( m_bTransposed )
{
double temp = surface_s; surface_s = surface_t; surface_t = temp;
double* pTemp = nurbs_s; nurbs_s = nurbs_t; nurbs_t = pTemp;
}
*nurbs_s = surface_s;
*nurbs_t = surface_t;
ON_Circle circle( ON_xy_plane, 1.0 );
ON_Arc arc( circle, m_angle );
ON_ArcCurve arc_curve(arc, m_t[0], m_t[1]);
if ( !arc_curve.GetNurbFormParameterFromCurveParameter( surface_s, nurbs_s ) )
rc = false;
if ( m_curve )
{
if (!m_curve->GetNurbFormParameterFromCurveParameter( surface_t, nurbs_t ))
rc = false;
}
return rc;
}
ON_Arc ON_RevSurface::IsoArc(
double curve_parameter
) const
{
for (;;)
{
if (nullptr == m_curve)
break;
// 8 December 2003 Chuck - fix iso extraction bug
// when m_angle[0] != 0.
ON_Circle circle;
ON_3dPoint P = m_curve->PointAt(curve_parameter);
if (false == P.IsValid())
break;
circle.plane.origin = m_axis.ClosestPointTo(P);
circle.plane.zaxis = m_axis.Tangent();
circle.plane.xaxis = P - circle.plane.origin;
circle.radius = circle.plane.xaxis.Length();
if (!circle.plane.xaxis.Unitize())
{
// 8 December 2003 Dale Lear - get valid zero radius
// arc/circle when revolute hits the axis.
// First: try middle of revolute for x-axis
P = m_curve->PointAt(m_curve->Domain().ParameterAt(0.5));
ON_3dPoint Q = m_axis.ClosestPointTo(P);
circle.plane.xaxis = P - Q;
if (!circle.plane.xaxis.Unitize())
{
// Then: just use a vector perp to zaxis
circle.plane.xaxis.PerpendicularTo(circle.plane.zaxis);
}
}
circle.plane.yaxis = ON_CrossProduct(circle.plane.zaxis, circle.plane.xaxis);
circle.plane.yaxis.Unitize();
circle.plane.UpdateEquation();
ON_Arc arc(circle, m_angle);
return arc;
}
ON_Arc arc;
arc.plane = ON_Plane::NanPlane;
arc.radius = ON_DBL_QNAN;
return arc;
}
ON_Curve* ON_RevSurface::IsoCurve( int dir, double c ) const
{
if ( dir < 0 || dir > 1 || !m_curve )
return nullptr;
ON_Curve* crv = 0;
if ( m_bTransposed )
dir = 1-dir;
if ( dir == 0 )
{
// 8 December 2003 Chuck - fix iso extraction bug
// when m_angle[0] != 0.
ON_Circle circle;
ON_3dPoint P = m_curve->PointAt(c);
circle.plane.origin = m_axis.ClosestPointTo(P);
circle.plane.zaxis = m_axis.Tangent();
circle.plane.xaxis = P - circle.plane.origin;
circle.radius = circle.plane.xaxis.Length();
if ( !circle.plane.xaxis.Unitize() )
{
// 8 December 2003 Dale Lear - get valid zero radius
// arc/circle when revolute hits the axis.
// First: try middle of revolute for x-axis
P = m_curve->PointAt(m_curve->Domain().ParameterAt(0.5));
ON_3dPoint Q = m_axis.ClosestPointTo(P);
circle.plane.xaxis = P-Q;
if ( !circle.plane.xaxis.Unitize() )
{
// Then: just use a vector perp to zaxis
circle.plane.xaxis.PerpendicularTo(circle.plane.zaxis);
}
}
circle.plane.yaxis = ON_CrossProduct( circle.plane.zaxis, circle.plane.xaxis );
circle.plane.yaxis.Unitize();
circle.plane.UpdateEquation();
ON_Arc arc( circle, m_angle );
crv = new ON_ArcCurve(arc, m_t[0], m_t[1]);
}
else if ( dir == 1 && m_curve )
{
crv = m_curve->DuplicateCurve();
if ( crv )
{
double a = c;
if ( m_t != m_angle )
{
double t = m_t.NormalizedParameterAt(c);
a = m_angle.ParameterAt(t);
}
if ( a != 0.0 )
{
crv->Rotate( a, m_axis.Direction(), m_axis.from );
}
}
}
return crv;
}
bool ON_RevSurface::Trim( int dir, const ON_Interval& domain )
{
bool rc = false;
if ( dir != 0 && dir != 1 )
return false;
if ( !domain.IsIncreasing() )
return false;
if ( m_bTransposed )
dir = 1-dir;
if ( dir == 0 )
{
ON_Interval dom;
dom.Intersection(domain,m_t);
if ( !dom.IsIncreasing() || !m_t.IsIncreasing() || !m_angle.IsIncreasing() )
return false;
double t0 = m_t.NormalizedParameterAt(dom[0]);
double t1 = m_t.NormalizedParameterAt(dom[1]);
ON_Interval a;
a[0] = m_angle.ParameterAt(t0);
a[1] = m_angle.ParameterAt(t1);
double d = a.Length();
if ( fabs(d) > ON_ZERO_TOLERANCE && fabs(d) <= 2.0*ON_PI+ON_ZERO_TOLERANCE )
{
m_angle = a;
m_t = domain;
rc = true;
}
}
else if ( dir == 1 && m_curve )
{
rc = m_curve->Trim( domain );
}
if ( rc )
{
// update bounding box
ON_BoundingBox bbox0 = m_bbox;
m_bbox.Destroy();
BoundingBox();
if ( m_bbox.IsValid() && bbox0.IsValid() )
m_bbox.Intersection(bbox0);
}
return rc;
}
bool ON_RevSurface::Extend(
int dir,
const ON_Interval& domain
)
{
if ( dir != 0 && dir != 1 )
return false;
if (IsClosed(dir))
return false;
bool do_it = false;
ON_Interval dom = Domain(dir);
if (domain[0] < dom[0])
{
dom[0] = domain[0];
do_it = true;
}
if (domain[1] > dom[1])
{
dom[1] = domain[1];
do_it = true;
}
if (!do_it)
return false;
if ( m_bTransposed )
dir = 1-dir;
bool rc = false;
if ( dir == 0 )
{
double t0 = m_t.NormalizedParameterAt(dom[0]);
double t1 = m_t.NormalizedParameterAt(dom[1]);
ON_Interval a;
a[0] = m_angle.ParameterAt(t0);
a[1] = m_angle.ParameterAt(t1);
if (a.Length() > 2.0*ON_PI+ON_ZERO_TOLERANCE) a[1] = a[0]+2.0*ON_PI;
m_angle = a;
m_t = dom;
rc = true;
}
else if ( dir == 1 && m_curve )
{
rc = m_curve->Extend(dom);
}
if ( rc )
{
DestroySurfaceTree();
// update bounding box
//ON_BoundingBox bbox0 = m_bbox;
m_bbox.Destroy();
BoundingBox();
}
return rc;
}
bool ON_RevSurface::Split(
int dir,
double c,
ON_Surface*& west_or_south_side,
ON_Surface*& east_or_north_side
) const
{
bool rc = false;
ON_RevSurface* srf_ws=ON_RevSurface::Cast(west_or_south_side);
ON_RevSurface* srf_en=ON_RevSurface::Cast(east_or_north_side);
if ( srf_ws && srf_ws == srf_en )
return false;
if ( west_or_south_side && !srf_ws )