forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_nurbssurface.cpp
3782 lines (3391 loc) · 97.5 KB
/
opennurbs_nurbssurface.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_NurbsSurface,ON_Surface,"4ED7D4DE-E947-11d3-BFE5-0010830122F0");
ON_NurbsSurface* ON_NurbsSurface::New()
{
// static function replaces new ON_NurbsSurface();
return new ON_NurbsSurface();
}
ON_NurbsSurface* ON_NurbsSurface::New(
const ON_NurbsSurface& nurbs_surface
)
{
// static function replaces new ON_NurbsSurface(const ON_NurbsSurface& nurbs_surface);
return new ON_NurbsSurface(nurbs_surface);
}
ON_NurbsSurface* ON_NurbsSurface::New(
const ON_BezierSurface& bezier_surface
)
{
// static function replaces new ON_NurbsSurface(const ON_BezierSurface& bezier_surface);
return new ON_NurbsSurface(bezier_surface);
}
ON_NurbsSurface* ON_NurbsSurface::New(
int dimension,
bool bIsRational,
int order0,
int order1,
int cv_count0,
int cv_count1
)
{
// static function replaces new ON_NurbsSurface(dim, is_rat, order0, ..., cv_count1 );
return new ON_NurbsSurface(dimension,bIsRational,order0,order1,cv_count0,cv_count1);
}
ON_NurbsSurface::ON_NurbsSurface()
{
ON__SET__THIS__PTR(m_s_ON_NurbsSurface_ptr);
Initialize();
}
ON_NurbsSurface::ON_NurbsSurface( const ON_NurbsSurface& src )
{
ON__SET__THIS__PTR(m_s_ON_NurbsSurface_ptr);
Initialize();
*this = src;
}
ON_NurbsSurface::ON_NurbsSurface( const ON_BezierSurface& bezier_surface )
{
ON__SET__THIS__PTR(m_s_ON_NurbsSurface_ptr);
Initialize();
*this = bezier_surface;
}
ON_NurbsSurface::ON_NurbsSurface(
int dim, // dimension (>= 1)
bool is_rat, // true to make a rational NURBS
int order0, // order (>= 2)
int order1, // order (>= 2)
int cv_count0, // cv count0 (>= order0)
int cv_count1 // cv count1 (>= order1)
)
{
ON__SET__THIS__PTR(m_s_ON_NurbsSurface_ptr);
Initialize();
Create( dim, is_rat, order0, order1, cv_count0, cv_count1 );
}
ON_NurbsSurface::~ON_NurbsSurface()
{
Destroy();
}
unsigned int ON_NurbsSurface::SizeOf() const
{
unsigned int sz = ON_Surface::SizeOf();
sz += (sizeof(*this) - sizeof(ON_Surface));
sz += m_knot_capacity[0]*sizeof(*m_knot[0]);
sz += m_knot_capacity[1]*sizeof(*m_knot[1]);
sz += m_cv_capacity*sizeof(*m_cv);
return sz;
}
ON__UINT32 ON_NurbsSurface::DataCRC(ON__UINT32 current_remainder) const
{
current_remainder = ON_CRC32(current_remainder,sizeof(m_dim),&m_dim);
current_remainder = ON_CRC32(current_remainder,sizeof(m_is_rat),&m_is_rat);
current_remainder = ON_CRC32(current_remainder,2*sizeof(m_order[0]),&m_order[0]);
current_remainder = ON_CRC32(current_remainder,2*sizeof(m_cv_count[0]),&m_cv_count[0]);
if ( m_cv_count[0] > 0 && m_cv_count[1] > 0
&& m_cv_stride[0] > 0 && m_cv_stride[1] > 0
&& m_cv )
{
size_t sizeof_cv = CVSize()*sizeof(m_cv[0]);
const double* cv = m_cv;
int i, j;
for ( i = 0; i < m_cv_count[0]; i++ )
{
cv = CV(i,0);
for ( j = 0; j < m_cv_count[1]; j++ )
{
current_remainder = ON_CRC32(current_remainder,sizeof_cv,cv);
cv += m_cv_stride[1];
}
}
}
current_remainder = ON_CRC32(current_remainder,KnotCount(0)*sizeof(m_knot[0][0]),m_knot[0]);
current_remainder = ON_CRC32(current_remainder,KnotCount(1)*sizeof(m_knot[1][0]),m_knot[1]);
return current_remainder;
}
bool ON_NurbsSurface::SetDomain(
int dir, // 0 sets first parameter's domain, 1 gets second parameter's domain
double t0,
double t1
)
{
bool rc = false;
if ( m_order[dir] >= 2 && m_cv_count[dir] >= m_order[dir] && nullptr != m_knot[dir] && t0 < t1 ) {
const double k0 = m_knot[dir][m_order[dir]-2];
const double k1 = m_knot[dir][m_cv_count[dir]-1];
if ( k0 == t0 && k1 == t1 )
rc = true;
else if ( k0 < k1 ) {
const double d = (t1-t0)/(k1-k0);
const double km = 0.5*(k0+k1);
const int knot_count = KnotCount(dir);
int i;
for ( i = 0; i < knot_count; i++ ) {
if ( m_knot[dir][i] <= km ) {
m_knot[dir][i] = (m_knot[dir][i] - k0)*d + t0;
}
else {
m_knot[dir][i] = (m_knot[dir][i] - k1)*d + t1;
}
}
rc = true;
DestroySurfaceTree();
}
}
return rc;
}
int ON_NurbsSurface::Dimension() const
{
return m_dim;
}
bool ON_NurbsSurface::IsRational() const
{
return m_is_rat?true:false;
}
int ON_NurbsSurface::CVSize() const
{
return (m_is_rat) ? m_dim+1 : m_dim;
}
int ON_NurbsSurface::Order( int dir ) const
{
return m_order[dir?1:0];
}
int ON_NurbsSurface::Degree( int dir ) const
{
return (m_order[dir?1:0]>=2) ? m_order[dir?1:0]-1 : 0;
}
int ON_NurbsSurface::CVCount( int dir ) const
{
return m_cv_count[dir?1:0];
}
int ON_NurbsSurface::CVCount( void ) const
{
return m_cv_count[0]*m_cv_count[1];
}
int ON_NurbsSurface::KnotCount( int dir ) const
{
return ON_KnotCount( m_order[dir?1:0], m_cv_count[dir?1:0] );
}
double* ON_NurbsSurface::CV( int i, int j ) const
{
const int offset = (i * m_cv_stride[0] + j * m_cv_stride[1]);
return (m_cv && offset >= 0) ? (m_cv + offset) : nullptr;
}
double* ON_NurbsSurface::CV(
ON_2dex cvdex
) const
{
return (cvdex.i >= 0 && cvdex.j >= 0) ? CV(cvdex.i, cvdex.j) : nullptr;
}
double* ON_NurbsSurface::CV(
ON_2udex cvdex
) const
{
return (cvdex.i < 0x7FFFFFFFU && cvdex.j < 0x7FFFFFFFU) ? CV(cvdex.i, cvdex.j) : nullptr;
}
const ON_4dPoint ON_NurbsSurface::ControlPoint(
int i,
int j
) const
{
ON_4dPoint cv;
if (!GetCV(i, j, cv))
cv = ON_4dPoint::Nan;
return cv;
}
ON::point_style ON_NurbsSurface::CVStyle() const
{
return m_is_rat ? ON::homogeneous_rational : ON::not_rational;
}
double ON_NurbsSurface::Weight( int i, int j ) const
{
return (m_cv && m_is_rat) ? m_cv[i*m_cv_stride[0] + j*m_cv_stride[1] + m_dim] : 1.0;
}
double ON_NurbsSurface::Knot( int dir, int i ) const
{
return (m_knot[dir?1:0]) ? m_knot[dir?1:0][i] : 0.0;
}
int ON_NurbsSurface::KnotMultiplicity( int dir, int i ) const
{
dir = dir?1:0;
return ON_KnotMultiplicity( m_order[dir], m_cv_count[dir], m_knot[dir], i );
}
const double* ON_NurbsSurface::Knot( int dir ) const
{
return m_knot[dir?1:0];
}
bool ON_NurbsSurface::MakeClampedUniformKnotVector(
int dir,
double delta
)
{
if ( dir < 0 || dir > 1 )
return false;
DestroySurfaceTree();
ReserveKnotCapacity( dir, ON_KnotCount( m_order[dir], m_cv_count[dir] ) );
return ON_MakeClampedUniformKnotVector( m_order[dir], m_cv_count[dir], m_knot[dir], delta );
}
bool ON_NurbsSurface::MakePeriodicUniformKnotVector(
int dir,
double delta
)
{
if ( dir < 0 || dir > 1 )
return false;
DestroySurfaceTree();
ReserveKnotCapacity( dir, ON_KnotCount( m_order[dir], m_cv_count[dir] ) );
return ON_MakePeriodicUniformKnotVector( m_order[dir], m_cv_count[dir], m_knot[dir], delta );
}
double ON_NurbsSurface::SuperfluousKnot( int dir, int end ) const
{ return(m_knot[dir?1:0]) ? ON_SuperfluousKnot(m_order[dir?1:0],m_cv_count[dir?1:0],m_knot[dir?1:0],end) : 0.0;}
bool ON_NurbsSurface::Create(
int dim, // dimension (>= 1)
bool is_rat, // true to make a rational NURBS
int order0, // order (>= 2)
int order1, // order (>= 2)
int cv_count0, // cv count0 (>= order0)
int cv_count1 // cv count1 (>= order1)
)
{
DestroySurfaceTree();
if ( dim < 1 )
return false;
if ( order0 < 2 )
return false;
if ( order1 < 2 )
return false;
if ( cv_count0 < order0 )
return false;
if ( cv_count1 < order1 )
return false;
m_dim = dim;
m_is_rat = (is_rat) ? true : false;
m_order[0] = order0;
m_order[1] = order1;
m_cv_count[0] = cv_count0;
m_cv_count[1] = cv_count1;
m_cv_stride[1] = (m_is_rat) ? m_dim+1 : m_dim;
m_cv_stride[0] = m_cv_stride[1]*m_cv_count[1];
bool rc = ReserveKnotCapacity( 0, KnotCount(0) );
if ( !ReserveKnotCapacity( 1, KnotCount(1) ) )
rc = false;
if ( !ReserveCVCapacity( m_cv_count[0]*m_cv_count[1]*m_cv_stride[1] ) )
rc = false;
return rc;
}
void ON_NurbsSurface::Destroy()
{
double* cv = ( m_cv && m_cv_capacity ) ? m_cv : nullptr;
double* knot0 = ( m_knot[0] && m_knot_capacity[0] ) ? m_knot[0] : nullptr;
double* knot1 = ( m_knot[1] && m_knot_capacity[1] ) ? m_knot[1] : nullptr;
Initialize();
if ( cv )
onfree(cv);
if ( knot0 )
onfree(knot0);
if ( knot1 )
onfree(knot1);
}
void ON_NurbsSurface::EmergencyDestroy()
{
Initialize();
}
void ON_NurbsSurface::Initialize()
{
m_dim = 0;
m_is_rat = 0;
m_order[0] = 0;
m_order[1] = 0;
m_cv_count[0] = 0;
m_cv_count[1] = 0;
m_knot_capacity[0] = 0;
m_knot_capacity[1] = 0;
m_knot[0] = 0;
m_knot[1] = 0;
m_cv_stride[0] = 0;
m_cv_stride[1] = 0;
m_cv_capacity = 0;
m_cv = 0;
}
bool ON_NurbsSurface::ReserveKnotCapacity( int dir, int capacity )
{
if (dir)
dir = 1;
if ( m_knot_capacity[dir] < capacity ) {
if ( m_knot[dir] ) {
if ( m_knot_capacity[dir] ) {
m_knot[dir] = (double*)onrealloc( m_knot[dir], capacity*sizeof(*m_knot[dir]) );
m_knot_capacity[dir] = (m_knot[dir]) ? capacity : 0;
}
// else user supplied m_knot[] array
}
else {
m_knot[dir] = (double*)onmalloc( capacity*sizeof(*m_knot[dir]) );
m_knot_capacity[dir] = (m_knot[dir]) ? capacity : 0;
}
}
return ( m_knot[dir] ) ? true : false;
}
bool ON_NurbsSurface::ReserveCVCapacity( int capacity )
{
if ( m_cv_capacity < capacity ) {
if ( m_cv ) {
if ( m_cv_capacity ) {
m_cv = (double*)onrealloc( m_cv, capacity*sizeof(*m_cv) );
m_cv_capacity = (m_cv) ? capacity : 0;
}
// else user supplied m_cv[] array
}
else {
m_cv = (double*)onmalloc( capacity*sizeof(*m_cv) );
m_cv_capacity = (m_cv) ? capacity : 0;
}
}
return ( m_cv ) ? true : false;
}
static void ON_NurbsSurfaceCopyHelper( const ON_NurbsSurface& src, ON_NurbsSurface& dest )
{
dest.m_dim = src.m_dim;
dest.m_is_rat = src.m_is_rat;
dest.m_order[0] = src.m_order[0];
dest.m_order[1] = src.m_order[1];
dest.m_cv_count[0] = src.m_cv_count[0];
dest.m_cv_count[1] = src.m_cv_count[1];
dest.m_cv_stride[1] = dest.m_is_rat ? dest.m_dim+1 : dest.m_dim;
dest.m_cv_stride[0] = dest.m_cv_count[1]*dest.m_cv_stride[1];
if ( src.m_knot[0] )
{
// copy knot array
dest.ReserveKnotCapacity( 0, dest.KnotCount(0) );
memcpy( dest.m_knot[0], src.m_knot[0], dest.KnotCount(0)*sizeof(*dest.m_knot[0]) );
}
if ( src.m_knot[1] )
{
// copy knot array
dest.ReserveKnotCapacity( 1, dest.KnotCount(1) );
memcpy( dest.m_knot[1], src.m_knot[1], dest.KnotCount(1)*sizeof(*dest.m_knot[1]) );
}
if ( src.m_cv )
{
// copy cv array
dest.ReserveCVCapacity( dest.m_cv_count[0]*dest.m_cv_count[1]*dest.m_cv_stride[1] );
const int dst_cv_size = dest.CVSize()*sizeof(*dest.m_cv);
const int src_stride[2] = {src.m_cv_stride[0],src.m_cv_stride[1]};
if ( src_stride[0] == dest.m_cv_stride[0] && src_stride[1] == dest.m_cv_stride[1] )
{
memcpy( dest.m_cv, src.m_cv, dest.m_cv_count[0]*dest.m_cv_count[1]*dest.m_cv_stride[1]*sizeof(*dest.m_cv) );
}
else
{
const double *src_cv;
double *dst_cv = dest.m_cv;
int i, j;
for ( i = 0; i < dest.m_cv_count[0]; i++ )
{
src_cv = src.CV(i,0);
for ( j = 0; j < dest.m_cv_count[1]; j++ )
{
memcpy( dst_cv, src_cv, dst_cv_size );
dst_cv += dest.m_cv_stride[1];
src_cv += src_stride[1];
}
}
}
}
}
ON_NurbsSurface& ON_NurbsSurface::operator=( const ON_NurbsSurface& src )
{
if ( this != &src )
{
ON_Surface::operator=(src);
ON_NurbsSurfaceCopyHelper(src,*this);
}
return *this;
}
ON_NurbsSurface& ON_NurbsSurface::operator=( const ON_BezierSurface& bezier_surface )
{
int i, j;
DestroySurfaceTree();
m_dim = bezier_surface.m_dim;
m_is_rat = bezier_surface.m_is_rat;
m_order[0] = bezier_surface.m_order[0];
m_order[1] = bezier_surface.m_order[1];
m_cv_count[0] = m_order[0];
m_cv_count[1] = m_order[1];
m_cv_stride[1] = m_is_rat ? (m_dim+1) : m_dim;
m_cv_stride[0] = m_cv_stride[1]*m_cv_count[1];
// copy control points
if ( bezier_surface.m_cv )
{
ReserveCVCapacity(m_cv_count[0]*m_cv_count[1]*m_cv_stride[1]);
const int sizeof_cv = m_cv_stride[1]*sizeof(*m_cv);
const double* src_cv;
double* dst_cv;
for ( i = 0; i < m_order[0]; i++ ) for( j = 0; j < m_order[1]; j++ )
{
src_cv = bezier_surface.CV(i,j);
dst_cv = CV(i,j);
memcpy( dst_cv, src_cv, sizeof_cv );
}
}
// set clamped knots for domain [0,1]
for ( j = 0; j < 2; j++ )
{
const int knot_count = KnotCount(j);
ReserveKnotCapacity( j, knot_count );
for ( i = 0; i <= m_order[j]-2; i++ )
m_knot[j][i] = 0.0;
for ( i = m_order[j]-1; i < knot_count; i++ )
m_knot[j][i] = 1.0;
}
return *this;
}
void ON_NurbsSurface::Dump( ON_TextLog& dump ) const
{
dump.Print( "ON_NurbsSurface dim = %d is_rat = %d\n"
" order = %d X %d cv_count = %d X %d\n",
m_dim, m_is_rat, m_order[0], m_order[1], m_cv_count[0], m_cv_count[1] );
int dir;
for ( dir = 0; dir < 2; dir++ )
{
dump.Print( "Knot Vector %d ( %d knots )\n", dir, KnotCount(dir) );
dump.PrintKnotVector( m_order[dir], m_cv_count[dir], m_knot[dir] );
}
dump.Print( "Control Points %d %s points\n"
" index value\n",
m_cv_count[0]*m_cv_count[1],
(m_is_rat) ? "rational" : "non-rational" );
if ( !m_cv )
{
dump.Print(" nullptr cv array\n");
}
else
{
int i;
char sPreamble[128] = { 0 };
const size_t sPremable_capacity = sizeof(sPreamble) / sizeof(sPreamble[0]);
for ( i = 0; i < m_cv_count[0]; i++ )
{
if ( i > 0 )
dump.Print("\n");
sPreamble[0] = 0;
ON_String::FormatIntoBuffer(sPreamble, sPremable_capacity," CV[%2d]", i);
dump.PrintPointList( m_dim, m_is_rat,
m_cv_count[1], m_cv_stride[1],
CV(i,0),
sPreamble );
}
}
}
bool ON_NurbsSurface::IsValid( ON_TextLog* text_log ) const
{
bool rc = false;
if ( m_dim <= 0 )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_dim = %d (should be > 0).\n",m_dim);
}
}
else if ( m_cv == nullptr )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_cv is nullptr.\n",m_dim);
}
}
else
{
rc = true;
for ( int i = 0; i < 2 && rc; i++ )
{
rc = false;
if (m_order[i] < 2 )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_order[i] = %d (should be >= 2).\n",i,m_order[i]);
}
}
else if (m_cv_count[i] < m_order[i] )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_cv_count[%d] = %d (should be >= m_order[%d]=%d).\n",i,m_cv_count[i],i,m_order[i]);
}
}
else if (m_knot[i] == nullptr)
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_knot[i] is nullptr.\n");
}
}
else if ( !ON_IsValidKnotVector( m_order[i], m_cv_count[i], m_knot[i], text_log ) )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_knot[%d] is not a valid knot vector.\n",i);
}
}
else if ( m_cv_stride[i] < CVSize() )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_cv_stride[%d]=%d is too small (should be >= %d).\n",i,m_cv_stride[i],CVSize());
}
}
else
rc = true;
}
if ( rc )
{
int a0 = CVSize();
int a1 = m_cv_count[0]*a0;
int b1 = CVSize();
int b0 = m_cv_count[1]*b1;
if ( m_cv_stride[0] < a0 || m_cv_stride[1] < a1 )
{
if ( m_cv_stride[0] < b0 || m_cv_stride[1] < b1 )
{
if ( text_log )
{
text_log->Print("ON_NurbsSurface.m_cv_stride[] = {%d,%d} is not valid.\n",m_cv_stride[0],m_cv_stride[1]);
}
rc = false;
}
}
}
if (rc)
{
const int cvdim = CVSize();
for (int i = 0; m_cv_count[0] > i; ++i)
{
for (int j = 0; m_cv_count[1] > j; ++j)
{
const double* cv = CV(i, j);
for (int k = 0; cvdim > k; ++k)
{
if (false == ON_CV_COORDINATE_IS_VALID(cv[k]))
return false;
}
}
}
}
}
return rc;
}
bool ON_NurbsSurface::GetBBox( // returns true if successful
double* boxmin, // minimum
double* boxmax, // maximum
bool bGrowBox // true means grow box
) const
{
return ON_GetPointGridBoundingBox( m_dim, m_is_rat,
m_cv_count[0], m_cv_count[1],
m_cv_stride[0], m_cv_stride[1],
m_cv,
boxmin, boxmax, bGrowBox?true:false );
}
bool ON_NurbsSurface::Transform( const ON_Xform& xform )
{
DestroySurfaceTree();
TransformUserData(xform);
if ( 0 == m_is_rat )
{
if ( xform.m_xform[3][0] != 0.0 || xform.m_xform[3][1] != 0.0 || xform.m_xform[3][2] != 0.0 )
{
MakeRational();
}
}
return ON_TransformPointGrid( m_dim, m_is_rat?true:false, m_cv_count[0], m_cv_count[1], m_cv_stride[0], m_cv_stride[1], m_cv, xform );
}
bool ON_NurbsSurface::IsDeformable() const
{
return true;
}
bool ON_NurbsSurface::MakeDeformable()
{
return true;
}
bool ON_NurbsSurface::Write(
ON_BinaryArchive& file // open binary file
) const
{
// NOTE - check legacy I/O code if changed
bool rc = file.Write3dmChunkVersion(1,0);
if (rc) {
if (rc) rc = file.WriteInt( m_dim );
if (rc) rc = file.WriteInt( m_is_rat );
if (rc) rc = file.WriteInt( m_order[0] );
if (rc) rc = file.WriteInt( m_order[1] );
if (rc) rc = file.WriteInt( m_cv_count[0] );
if (rc) rc = file.WriteInt( m_cv_count[1] );
if (rc) rc = file.WriteInt(0); // reserved1
if (rc) rc = file.WriteInt(0); // reserved2
if (rc) {
ON_BoundingBox bbox; // write invalid bounding box - may be used in future
rc = file.WriteBoundingBox(bbox);
}
int count = m_knot[0] ? KnotCount(0) : 0;
if (rc) rc = file.WriteInt(count);
if (rc) rc = file.WriteDouble( count, m_knot[0] );
count = m_knot[1] ? KnotCount(1) : 0;
if (rc) rc = file.WriteInt(count);
if (rc) rc = file.WriteDouble( count, m_knot[1] );
const int cv_size = CVSize();
count = ( m_cv && cv_size > 0
&& m_cv_count[0] > 0 && m_cv_count[1] > 0
&& m_cv_stride[0] >= cv_size && m_cv_stride[1] >= cv_size)
? m_cv_count[0]*m_cv_count[1]
: 0;
if (rc) rc = file.WriteInt(count);
if (rc && count > 0 ) {
int i, j;
for ( i = 0; i < m_cv_count[0] && rc; i++ ) {
for ( j = 0; j < m_cv_count[1] && rc; j++ ) {
rc = file.WriteDouble( cv_size, CV(i,j) );
}
}
}
}
return rc;
}
bool ON_NurbsSurface::Read(
ON_BinaryArchive& file // open binary file
)
{
DestroySurfaceTree();
// NOTE - check legacy I/O code if changed
int major_version = 0;
int minor_version = 0;
bool rc = file.Read3dmChunkVersion(&major_version,&minor_version);
if (rc && major_version==1) {
// common to all 1.x versions
int dim = 0, is_rat = 0, order0 = 0, order1 = 0, cv_count0 = 0, cv_count1 = 0;
int reserved1 = 0, reserved2 = 0;
if (rc) rc = file.ReadInt( &dim );
if (rc) rc = file.ReadInt( &is_rat );
if (rc) rc = file.ReadInt( &order0 );
if (rc) rc = file.ReadInt( &order1 );
if (rc) rc = file.ReadInt( &cv_count0 );
if (rc) rc = file.ReadInt( &cv_count1 );
if (rc) rc = file.ReadInt(&reserved1);
if (rc) rc = file.ReadInt(&reserved2);
if (rc) {
ON_BoundingBox bbox; // read bounding box - may be used in future
rc = file.ReadBoundingBox(bbox);
}
Create( dim, is_rat, order0, order1, cv_count0, cv_count1 );
int count = 0;
if (rc) rc = file.ReadInt(&count);
if (rc && count < 0)
rc = false;
if (rc ) rc = ReserveKnotCapacity(0,count);
if (rc) rc = file.ReadDouble( count, m_knot[0] );
count = 0;
if (rc) rc = file.ReadInt(&count);
if (rc && count < 0)
rc = false;
if (rc ) rc = ReserveKnotCapacity(1,count);
if (rc) rc = file.ReadDouble( count, m_knot[1] );
count = 0;
if (rc) rc = file.ReadInt(&count);
if (rc && count < 0)
rc = false;
const int cv_size = CVSize();
if (rc) rc = ReserveCVCapacity( count*cv_size );
if (count > 0 && cv_size > 0 && rc ) {
int i, j;
for ( i = 0; i < m_cv_count[0] && rc; i++ ) {
for ( j = 0; j < m_cv_count[1] && rc; j++ ) {
rc = file.ReadDouble( cv_size, CV(i,j) );
}
}
}
}
if ( !rc )
Destroy();
return rc;
}
ON_Interval ON_NurbsSurface::Domain( int dir ) const
{
ON_Interval d;
if (dir) dir = 1;
ON_GetKnotVectorDomain( m_order[dir], m_cv_count[dir], m_knot[dir], &d.m_t[0], &d.m_t[1] );
return d;
}
double ON_NurbsSurface::ControlPolygonLength( int dir ) const
{
double max_length = 0.0;
if ( dir >= 0 && dir <= 1 && m_cv_count[0] >= 2 && m_cv_count[1] >= 2 && m_cv != nullptr )
{
double length;
const double* p;
int i;
for( i = 0; i < m_cv_count[1-dir]; i++ )
{
length = 0.0;
p = (dir) ? CV(i,0) : CV(0,i);
ON_GetPolylineLength( m_dim, m_is_rat, m_cv_count[dir], m_cv_stride[dir], p, &length );
if ( length > max_length )
max_length = length;
}
}
return max_length;
}
bool ON_NurbsSurface::GetSurfaceSize(
double* width,
double* height
) const
{
// TODO - get lengths of polygon
bool rc = true;
if ( width )
{
*width = ControlPolygonLength( 0 );
}
if ( height )
{
*height = ControlPolygonLength( 1 );
}
return rc;
}
int ON_NurbsSurface::SpanCount( int dir ) const
{
if (dir) dir = 1;
return ON_KnotVectorSpanCount( m_order[dir], m_cv_count[dir], m_knot[dir] );
}
bool ON_NurbsSurface::GetSpanVector( int dir, double* s ) const
{
if (dir) dir = 1;
return ON_GetKnotVectorSpanVector( m_order[dir], m_cv_count[dir], m_knot[dir], s );
}
bool ON_NurbsSurface::GetParameterTolerance( // returns tminus < tplus: parameters tminus <= s <= tplus
int dir,
double t, // t = parameter in domain
double* tminus, // tminus
double* tplus // tplus
) const
{
bool rc = false;
ON_Interval d = Domain(dir);
double t0 = d.Min();
double t1 = d.Max();
if ( t0 <= t1 ) {
const double* knot = Knot(dir);
const int order = Order(dir);
const int cv_count = CVCount(dir);
if ( t < knot[order-1] )
t1 = knot[order-1];
else if ( t > knot[cv_count-2] )
t0 = knot[cv_count-2];
rc = ON_GetParameterTolerance( t0, t1, t, tminus, tplus );
}
return rc;
}
bool
ON_NurbsSurface::Evaluate( // returns false if unable to evaluate
double s, 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, // optional - determines which side to evaluate from
// 0 = default
// 1 = from NE quadrant
// 2 = from NW quadrant
// 3 = from SW quadrant
// 4 = from SE quadrant
int hint[2] // optional - evaluation hint (int) used to speed
// repeated evaluations
) const
{
bool rc = false;
int span_index[2];
span_index[0] = ON_NurbsSpanIndex(m_order[0],m_cv_count[0],m_knot[0],s,(side==2||side==3)?-1:1,(hint)?hint[0]:0);
span_index[1] = ON_NurbsSpanIndex(m_order[1],m_cv_count[1],m_knot[1],t,(side==3||side==4)?-1:1,(hint)?hint[1]:0);
rc = ON_EvaluateNurbsSurfaceSpan(
m_dim, m_is_rat,
m_order[0], m_order[1],
m_knot[0] + span_index[0],
m_knot[1] + span_index[1],
m_cv_stride[0], m_cv_stride[1],
m_cv + (span_index[0]*m_cv_stride[0] + span_index[1]*m_cv_stride[1]),
der_count,
s, t,
v_stride, v
);
if ( hint ) {
hint[0] = span_index[0];
hint[1] = span_index[1];
}
return rc;
}
ON_Curve* ON_NurbsSurface::IsoCurve(
int dir, // 0 first parameter varies and second parameter is constant
// e.g., point on IsoCurve(0,c) at t is srf(t,c)
// 1 first parameter is constant and second parameter varies
// e.g., point on IsoCurve(1,c) at t is srf(c,t)
double c // value of constant parameter
) const
{
ON_Curve* crv = 0;
int i,j,k,Scvsize,span_index;
double* Ncv;
const double* Scv;
if ( (dir == 0 || dir == 1) && IsValid() )
{
Scvsize = CVSize();
ON_NurbsCurve* nurbscrv = new ON_NurbsCurve( m_dim, m_is_rat, m_order[dir], m_cv_count[dir] );
memcpy( nurbscrv->m_knot, m_knot[dir], nurbscrv->KnotCount()*sizeof(*nurbscrv->m_knot) );
span_index = ON_NurbsSpanIndex(m_order[1-dir],m_cv_count[1-dir],m_knot[1-dir],c,1,0);
if ( span_index < 0 )
span_index = 0;
else if ( span_index > m_cv_count[1-dir]-m_order[1-dir] )
span_index = m_cv_count[1-dir]-m_order[1-dir];
ON_NurbsCurve N( Scvsize*nurbscrv->CVCount(), 0, m_order[1-dir], m_order[1-dir] );
memcpy( N.m_knot, m_knot[1-dir]+span_index, N.KnotCount()*sizeof(*N.m_knot) );
for ( i = 0; i < N.m_cv_count; i++ ) {
Ncv = N.CV(i);
for ( j = 0; j < m_cv_count[dir]; j++ ) {
Scv = (dir) ? CV(i+span_index,j) : CV(j,i+span_index);
for ( k = 0; k < Scvsize; k++ )
*Ncv++ = *Scv++;
}
}
N.Evaluate( c, 0, N.Dimension(), nurbscrv->m_cv );
crv = nurbscrv;
}
return crv;
}
// Converts a surface to a high degree NURBS curve.
// Use FromCurve to convert back to a surface.
static ON_NurbsCurve* ToCurve( const ON_NurbsSurface& srf, int dir,
ON_NurbsCurve* crv )
{
double* tmp_cv = nullptr;
if ( dir < 0 || dir > 1 )
return nullptr;
if ( !srf.m_cv )
return nullptr;
if ( !crv )
crv = new ON_NurbsCurve();
int srf_cv_size = srf.CVSize();
if ( !crv->Create(
srf_cv_size*srf.m_cv_count[1-dir], // dim
false, // is_rat
srf.m_order[dir],
srf.m_cv_count[dir]
) )
return nullptr;
if ( crv->m_cv == srf.m_cv )
{
tmp_cv = (double*)onmalloc( crv->m_dim*crv->m_cv_stride*sizeof(tmp_cv[0]) );
crv->m_cv = tmp_cv;