-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopennurbs_evaluate_nurbs.cpp
More file actions
1650 lines (1503 loc) · 46.8 KB
/
opennurbs_evaluate_nurbs.cpp
File metadata and controls
1650 lines (1503 loc) · 46.8 KB
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"
double ON_EvaluateBernsteinBasis(int degree, int i, double t)
/*****************************************************************************
Evaluate Bernstein basis polynomial
INPUT:
degree
If degree < 0, then 0.0 is returned.
i
If i < 0 or i > degree, then 0.0 is returned.
t
The formula for the Bernstein polynomial is valid
for all values of t.
OUTPUT:
TL_EvBernsteinBasis()
degree!
---------------- * (1-t)^(degree-i) * t^i, if 0 <= i <= degree
(degree-i)! * i!
0, otherwise.
(In this function, 0^0 is treated as 1.)
COMMENTS:
Below, B(d,i,t) is used to denote the i-th Bernstein basis polynomial of
degree d; i.e., B(d,i,t) = TL_EvBernsteinBasis(d,i,t).
When degree <= 4, TL_EvBernsteinBasis() computes the value directly.
When 4 < degree < 9, the value is computed recursively using the formula
B(d,i,t) = t*B(d-1,i-1,t) + (1-t)*B(d-1,i,t).
For 9 <= degree, the value is computed using the formula
B(d,i,t) = TL_EvBinomial(degree-i,i)
*((degree==i) ? 1.0 : pow(1.0-t,(double)(degree-i)))
*((i) ? pow(t,(double)i) : 1.0);
The value of a degree d Bezier at t with control vertices
{P_0, ..., P_d} is equal to B(d,0,t)*P_0 + ... + B(d,d,t)*P_d.
Numerically, this formula is inefficient and unstable. The
de Casteljau algorithm used in TL_EvdeCasteljau() is faster
and more stable.
EXAMPLE:
// Use TL_EvBernsteinBasis() to evaluate a 3 dimensional
// non-rational cubic Bezier at 1/4.
double cv[4][3], t, B[4], answer[3];
int i, j, degree;
degree = 3;
t = 0.25;
answer[0] = answer[1] = answer[2] = 0.0;
for (i = 0; i <= degree; i++)
cv[i] = something;
for (i = 0; i <=degree; i++) {
B[i] = TL_EvBernsteinBasis(degree,i,t);
for (j = 0; j < 3; j++)
answer[j] += B[i]*cv[i][j];
}
REFERENCE:
BOHM-01, Page 7.
RELATED FUNCTIONS:
TL_EvNurbBasis
TL_EvdeCasteljau()
TL_EvBezier()
TL_EvHorner()
*****************************************************************************/
{
double
s;
if (degree < 0 || i < 0 || i > degree)
return 0.0;
switch(degree) {
case 0: /* degree 0 */
return 1.0;
case 1: /* degree 1 */
return ((i) ? t : 1.0-t);
case 2: /* degree 2 */
switch(i) {
case 0:
t = 1.0-t;
return t*t;
case 1:
return 2.0*t*(1.0-t);
default: /* i == 2 */
return t*t;
}
case 3: /* degree 3 */
switch(i) {
case 0:
t = 1.0 - t;
return t*t*t;
case 1:
s = 1.0-t;
return 3.0*s*s*t;
case 2:
return 3.0*(1.0-t)*t*t;
default: /* i == 3 */
return t*t*t;
}
case 4: /* degree 4 */
switch(i) {
case 0:
t = 1.0-t;
t = t*t;
return t*t;
case 1: /* 4*(1-t)^3*t */
s = 1.0-t;
return 4.0*s*s*s*t;
case 2: /* 6*(1-t)^2*t */
s = 1.0-t;
return 6.0*s*s*t*t;
case 3: /* 4*(1-t)*t^3 */
return 4.0*(1.0-t)*t*t*t;
default: /* t^4 (i == 4) */
t = t*t;
return t*t;
}
default: /* degree >= 5 */
/* The "9" was determined to produce the fastest code when
* tested on a SUN Sparc (SUNOS 4.3, gcc -O)
*/
if (degree < 9)
return (t*ON_EvaluateBernsteinBasis(degree-1,i-1,t)
+ (1-t)*ON_EvaluateBernsteinBasis(degree-1,i,t));
else
return ON_BinomialCoefficient(degree-i,i)*((degree==i)?1.0:pow(1.0-t,(double)(degree-i)))*((i)?pow(t,(double)i):1.0);
}
}
void ON_EvaluatedeCasteljau(int dim, int order, int side, int cv_stride, double* cv, double t)
/*****************************************************************************
Evaluate a Bezier using the de Casteljau algorithm
INPUT:
dim ( >= 1)
order ( >= 2)
side <= 0 return left side of bezier in cv array
> 0 return right side of bezier in cv array
cv array of order*cv_stride doubles that specify the Bezier's control
vertices.
cv_stride ( >= dim) number of doubles between cv's (typically a multiple of dim).
t If side <= 0, then t must be > 0.0.
If side > 0, then t must be < 1.0.
OUTPUT:
cv
If side <= 0, the input cv's are replaced with the cv's for
the bezier trimmed/extended to [0,t]. In particular,
{cv[(order-1)*cv_stride], ..., cv[order*cv_stride - 1]} is the value of
the Bezier at t.
If side > 0, the input cv's are replaced with the cv's for
the Bezier trimmed/extended to [t,1]. In particular,
{cv[0], ..., cv[dim-1]} is the value of the Bezier at t.
COMMENTS:
Set C[i,j] = {cv[j*cv_stride], ..., cv[(j+1)*cv_stride-1]}, if i = 0
(1-t)*C[i-1,j-1] + t*C[i-1,j], if 0 < i <= d = degree
The collection of C[i,j]'s is typically drawn in a triangular array:
C[0,0]
C[1,1]
C[0,1] C[2,2]
C[1,2] ...
C[0,2]
... C[d,d]
...
C[0,d-1] C[2,d]
C[1,d]
C[0,d]
The value of the Bezier at t is equal to C[d,d].
When side < 0, the input cv's are replaced with
C[0,0], C[1,2], ..., C[d,d].
If the output cv's are used as control vertices for a Bezier,
then output_bezier(s) = input_bezier(t*s).
When side >= 0, the input cv's are replace with
C[d,d], C[d-1,d], ..., C[0,d].
If the output cv's are used as control vertices for a Bezier,
then output_bezier(s) = input_bezier((1-s)*t + s).
If a Bezier is going to be evaluated more than a few times, it is
faster to convert the Bezier to power basis and evaluate using
TL_EvHorner. However, Horner's algorithm is not a stable as
de Casteljau's.
EXAMPLE:
// Use TL_EvdeCasteljau() to trim/extend a Bezier
// to the interval [t0,t1], where t0 < t1
double cv[order][dim], t0, t1
cv = whatever;
if (1.0 - t0 > t1) {
// first trim at t0, then trim at t1
if (t0 != 0.0) TL_EvdeCasteljau(dim,order, 1,cv,dim,t0);
t1 = (t1-t0)/(1.0 - t0); // adjust t1 to new domain
if (t1 != 1.0) TL_EvdeCasteljau(dim,order,-1,cv,dim,t1);
}
else {
// first trim at t1, then trim at t0
if (t1 != 1.0) TL_EvdeCasteljau(dim,order,-1,cv,dim,t1);
t0 /= t1; // adjust t0 to new domain
if (t0 != 0.0) TL_EvdeCasteljau(dim,order, 1,cv,dim,t0);
}
REFERENCE:
BOHM-01, Page 8.
RELATED FUNCTIONS:
TL_EvBernsteinBasis
TL_EvBezier
TL_EvdeBoor
TL_EvHorner
TL_ConvertBezierToPolynomial
*****************************************************************************/
{
double
s, *P0, *P1;
int
j, d, off_minus_dim;
if (t == 0.0 || t == 1.0)
return;
s = 1.0 - t;
/* it's ugly and it's fast */
if (cv_stride > dim) {
off_minus_dim = cv_stride - dim;
if (side > 0) {
/* output cv's = bezier trimmed to [t,1] */
while (--order) {
P0 = cv; /* first cv */
P1 = P0 + cv_stride; /* next cv */
j = order;
while (j--) {
d = dim;
while (d--) {*P0 = (*P0 * s) + (*P1 * t); P0++; P1++;}
P0 += off_minus_dim; P1 += off_minus_dim;}}
}
else {
/* side <= 0, so output cv's = bezier trimmed to [0,t] */
cv += order*dim; /* now cv = last control vertex */
while (--order) {
P1 = cv; /* last cv */
P0 = P1 - cv_stride; /* next to last cv */
j = order;
while (j--) {
d = dim;
while (d--) {P0--; P1--; *P1 = (*P0 * s) + (*P1 * t);}
P0 -= off_minus_dim; P1 -= off_minus_dim;}}
}
}
else {
if (side > 0) {
/* output cv's = bezier trimmed to [t,1] */
while (--order) {
P0 = cv; /* first cv */
P1 = P0 + dim; /* next cv */
j = order;
while (j--) {
d = dim;
while (d--) {*P0 = (*P0 * s) + (*P1 * t); P0++; P1++;}}
}
}
else {
/* side <= 0, so output cv's = bezier trimmed to [0,t] */
cv += order*dim; /* now cv = last control vertex */
while (--order) {
P1 = cv; /* last cv */
P0 = P1 - dim; /* next to last cv */
j = order;
while (j--) {
d = dim;
while (d--) {P0--; P1--; *P1 = (*P0 * s) + (*P1 * t);}}
}
}
}
}
bool ON_IncreaseBezierDegree(
int dim,
ON_BOOL32 is_rat,
int order,
int cv_stride,
double* cv
)
/*****************************************************************************
Increase the degree of a Bezier
INPUT:
cvdim (dim + is_rat)
order ( >= 2 )
order of input bezier
cv
control vertices of bezier
newcv
array of cvdim*(order+1) doubles (The cv and newcv pointers may be equal.)
OUTPUT:
newcv Control vertices of an Bezier with order (order+1). The new Bezier
and the old Bezier evaluate to the same point.
COMMENTS:
If {B_0, ... B_d} are the control vertices of the input Bezier, then
{C_0, ..., C_{d+1}} are the control vertices of the returned Bezier,
where,
C_0 = B_0
C_k = k/(d+1) * B_{k-1} + (d+1-k)/(d+1) * B_{k}(1 < k <= d)
C_{d+1} = B_d
The computation is done in a way that permits the pointers cv and newcv
to be equal; i.e., if the cv array is long enough, the degree may be
raised with a call like
TL_IncreaseBezierDegree(cvdim,order,cv,cv);
EXAMPLE:
raise_degree(TL_BEZIER* bez)
{
// raise the degree of a TL_BEZIER
bez->cv = (double*) onrealloc ( bez->cv, (bez->order+1)*(bez->dim+bez->is_rat) );
TL_IncreaseBezierDegree ( bez->dim+bez->is_rat, bez->order,bez->cv,bez->cv );
bez->order++;
}
REFERENCE:
BOHM-01, Page 7.
RELATED FUNCTIONS:
TL_DecreaseBezierDegree
*****************************************************************************/
{
double a0, a1, d, c0, c1;
int j;
double* newcv = cv;
const int cvdim = (is_rat)?dim+1:dim;
const int dcv = cv_stride - cvdim;
j = cv_stride*order;
newcv += j;
memcpy( newcv, newcv-cv_stride, cvdim*sizeof(*newcv) );
newcv -= (dcv+1);
cv = newcv - cv_stride;
a0 = order;
a1 = 0.0;
d = 1.0/a0;
while (--order) {
a0 -= 1.0;
a1 += 1.0;
c0 = d*a0;
c1 = d*a1;
j = cvdim;
while(j--) {
*newcv = c0 * *cv + c1 * *newcv;
cv--;
newcv--;
}
cv -= dcv;
newcv -= dcv;
}
return true;
}
bool ON_RemoveBezierSingAt0(
int dim,
int order,
int cv_stride,
double* cv
)
{
const int cvdim = dim+1;
int j,k,ord0;
ord0 = order;
while(cv[dim] == 0.0) {
order--;
if (order < 2)
return false;
j = dim;
while(j--) {
if (cv[j] != 0.0)
return false;
}
for (j=0; j<order; j++) {
for (k=0; k<cvdim; k++)
cv[j*cv_stride+k] = (order*cv[(j+1)*cv_stride+k])/(j+1);
}
}
while (order < ord0)
ON_IncreaseBezierDegree(dim,true,order++,cv_stride,cv);
return true;
}
bool ON_RemoveBezierSingAt1(
int dim,
int order,
int cv_stride,
double* cv
)
{
const int cvdim = dim+1;
int i,k,ord0,CVlen;
ord0 = order;
CVlen=order*cvdim;
while (order > 1 && cv[CVlen-1] == 0.0) {
order--;
if (order < 2)
return false;
i = dim;
while(i--) {
if (cv[CVlen-1-i] != 0.0)
return false;
}
for (i=0; i<order; i++) {
for (k=0; k<cvdim; k++)
cv[i*cv_stride+k] = (order*cv[i*cv_stride+k])/(order-i);
}
CVlen -= cvdim;
}
while(order < ord0)
ON_IncreaseBezierDegree(dim,true,order++,cv_stride,cv);
return false;
}
bool ON_EvaluateBezier(
int dim, // dimension
ON_BOOL32 is_rat, // true if NURBS is rational
int order, // order
int cv_stride, // cv_stride >= (is_rat)?dim+1:dim
const double* cv, // cv[order*cv_stride] array
double t0, double t1, // domain
int der_count, // number of derivatives to compute
double t, // evaluation parameter
int v_stride, // v_stride (>=dimension)
double* v // v[(der_count+1)*v_stride] array
)
/*****************************************************************************
Evaluate a Bezier
INPUT:
dim
(>= 1) dimension of Bezier's range
is_rat
0: bezier is not rational
1: bezier is rational
order
(>= 2) (order = degree+1)
cv
array of (dim+is_rat)*order doubles that define the
Bezier's control vertices.
t0, t1 (t0 != t1)
Bezier's domain. Mathematically, Beziers have domain [0,1]. In practice
Beziers are frequently evaluated at (t-t0)/(t1-t0) and the chain
rule is used to evaluate the derivatives. This function is the most
efficient place to apply the chain rule.
t
Evaluation parameter
der_count
(>= 0) number of derivatives to evaluate
answer
answer[i] is NULL or points to an array of dim doubles.
OUTPUT:
ON_EvBezier()
0: successful
-1: failure - rational function had nonzero numerator and zero
denominator
answer
bez(t) = answer[0]
bez'(t) = answer[1]
...
(n)
bez (t) = answer[n]
COMMENTS:
Use de Casteljau's algorithm. Rational fuctions with removable singularities
(like x^2/x) are efficiently and correctly handled.
EXAMPLE:
// ...
REFERENCE:
AUTHOR page ?
RELATED FUNCTIONS:
ON_EvaluatedeCasteljau
ON_EvQuotientRule
ON_EvNurb
ON_EvPolynomialPoint
ON_onvertBezierToPolynomial
ON_onvertPolynomialToBezier
ON_onvertNurbToBezier
*****************************************************************************/
{
unsigned char stack_buffer[4*64*sizeof(double)];
double delta_t;
register double alpha0;
register double alpha1;
register double *cv0, *cv1;
register int i, j, k;
double* CV, *tmp;
void* free_me = 0;
const int degree = order-1;
const int cvdim = (is_rat)?dim+1:dim;
if ( cv_stride < cvdim )
cv_stride = cvdim;
memset( v, 0, v_stride*(der_count+1)*sizeof(*v) );
#if defined( ON_DEBUG)
if (t0==t1) {
return false;
}
#endif
i = order*cvdim;
j = 0;
if (der_count > degree) {
if (is_rat)
j = (der_count-degree)*cvdim;
else {
der_count = degree;
}
}
size_t sizeofCV = (i+j)*sizeof(*CV);
// 21 November 2007 Dale Lear RR 29005 - remove call to alloca()
CV = (double*)( (sizeofCV <= sizeof(stack_buffer)) ? stack_buffer : (free_me=onmalloc(sizeofCV)) );
if (j) {
memset( CV+i, 0, j*sizeof(*CV) );
}
cv0=CV;
if ( t0 == t
|| (t <= 0.5*(t0+t1) && t != t1)
)
{
for ( i = 0; i < order; i++ )
{
memcpy( cv0, cv, cvdim*sizeof(*cv0) );
cv0 += cvdim;
cv += cv_stride;
}
cv -= (cv_stride*order);
delta_t = 1.0/(t1 - t);
alpha1 = 1.0/(t1-t0);
alpha0 = (t1-t)*alpha1;
alpha1 *= t-t0;
}
else
{
cv += (cv_stride*order);
k=order;
while(k--)
{
cv -= cv_stride;
memcpy( cv0, cv, cvdim*sizeof(*cv0) );
cv0 += cvdim;
}
delta_t = 1.0/(t0 - t);
alpha0 = 1.0/(t1-t0);
alpha1 = (t1-t)*alpha0;
alpha0 *= t-t0;
}
/* deCasteljau (from the right) */
if (alpha1 != 0.0) {
j = order; while (--j) {
cv0 = CV;
cv1 = cv0 + cvdim;
i = j; while (i--) {
k = cvdim;
while (k--) {
*cv0 = *cv0 * alpha0 + *cv1 * alpha1;
cv0++;
cv1++;
}
}
}
}
/* check for removable singularity */
if (is_rat && CV[dim] == 0.0)
{
if ( !ON_RemoveBezierSingAt0(dim,order,cvdim,CV) )
{
if ( free_me )
onfree(free_me);
return false;
}
}
/* Lee (from the right) */
if (der_count) {
tmp=CV;
alpha0 = order;
j = (der_count>=order)?order:der_count+1;
CV += cvdim*j; while(--j) {
alpha0 -= 1.0; cv1 = CV; cv0 = cv1-cvdim;
i=j; while(i--) {
alpha1 = alpha0 * delta_t;
k=cvdim; while(k--) {
cv0--;
cv1--;
*cv1 = alpha1*(*cv1 - *cv0);
}
}
}
CV=tmp;
}
if ( 2 == order )
{
// 7 January 2004 Dale Lear
// Added to fix those cases when, numerically, t*a + (1.0-t)*a != a.
// Similar to fix for RR 9683.
j = cv_stride;
for ( i = 0; i < cvdim; i++, j++ )
{
if ( cv[i] == cv[j] )
{
CV[i] = cv[i];
}
}
}
if (is_rat) {
ON_EvaluateQuotientRule( dim, der_count, cvdim, CV );
}
for (i=0;i<=der_count;i++) {
memcpy( v, CV, dim*sizeof(*v) );
v += v_stride;
CV += cvdim;
}
if ( free_me )
onfree(free_me);
return true;
}
bool ON_EvaluateNurbsBasis( int order, const double* knot,
double t, double* N )
{
/*****************************************************************************
Evaluate B-spline basis functions
INPUT:
order >= 1
d = degree = order - 1
knot[]
array of length 2*d.
Generally, knot[0] <= ... <= knot[d-1] < knot[d] <= ... <= knot[2*d-1].
N[]
array of length order*order
OUTPUT:
If "N" were declared as double N[order][order], then
k
N[d-k][i] = N (t) = value of i-th degree k basis function.
i
where 0 <= k <= d and k <= i <= d.
In particular, N[0], ..., N[d] - values of degree d basis functions.
The "lower left" triangle is not initialized.
Actually, the above is true when knot[d-1] <= t < knot[d]. Otherwise, the
value returned is the value of the polynomial that agrees with N_i^k on the
half open domain [ knot[d-1], knot[d] )
COMMENTS:
If a degree d NURBS has n control points, then the TL knot vector has
length d+n-1. ( Most literature, including DeBoor and The NURBS Book,
duplicate the TL start and end knot and have knot vectors of length
d+n+1. )
Assume C is a B-spline of degree d (order=d+1) with n control vertices
(n>=d+1) and knot[] is its knot vector. Then
C(t) = Sum( 0 <= i < n, N_{i}(t) * C_{i} )
where N_{i} are the degree d b-spline basis functions and C_{i} are the control
vertices. The knot[] array length d+n-1 and satisfies
knot[0] <= ... <= knot[d-1] < knot[d]
knot[n-2] < knot[n-1] <= ... <= knot[n+d-2]
knot[i] < knot[d+i] for 0 <= i < n-1
knot[i] <= knot[i+1] for 0 <= i < n+d-2
The domain of C is [ knot[d-1], knot[n-1] ].
The support of N_{i} is [ knot[i-1], knot[i+d] ).
If d-1 <= k < n-1 and knot[k] <= t < knot[k+1], then
N_{i}(t) = 0 if i <= k-d
= 0 if i >= k+2
= B[i-k+d-1] if k-d+1 <= i <= k+1, where B[] is computed by the call
TL_EvNurbBasis( d+1, knot+k-d+1, t, B );
If 0 <= j < n-d, 0 <= m <= d, knot[j+d-1] <= t < knot[j+d], and B[] is
computed by the call
TL_EvNurbBasis( d+1, knot+j, t, B ),
then
N_{j+m}(t) = B[m].
EXAMPLE:
REFERENCE:
The NURBS book
RELATED FUNCTIONS:
TL_EvNurbBasis
TL_EvNurbBasisDer
*****************************************************************************/
register double a0, a1, x, y;
const double *k0;
double *t_k, *k_t, *N0;
const int d = order-1;
register int j, r;
t_k = (double*)alloca( d<<4 );
k_t = t_k + d;
if (knot[d-1] == knot[d]) {
/* value is defined to be zero on empty spans */
memset( N, 0, order*order*sizeof(*N) );
return true;
}
N += order*order-1;
N[0] = 1.0;
knot += d;
k0 = knot - 1;
for (j = 0; j < d; j++ ) {
N0 = N;
N -= order+1;
t_k[j] = t - *k0--;
k_t[j] = *knot++ - t;
x = 0.0;
for (r = 0; r <= j; r++) {
a0 = t_k[j-r];
a1 = k_t[r];
y = N0[r]/(a0 + a1);
N[r] = x + a1*y;
x = a0*y;
}
N[r] = x;
}
// 16 September 2003 Dale Lear (at Chuck's request)
// When t is at an end knot, do a check to
// get exact values of basis functions.
// The problem being that a0*y above can
// fail to be one by a bit or two when knot
// values are large.
x = 1.0-ON_SQRT_EPSILON;
if ( N[0] > x )
{
if ( N[0] != 1.0 && N[0] < 1.0 + ON_SQRT_EPSILON )
{
r = 1;
for ( j = 1; j <= d && r; j++ )
{
if ( N[j] != 0.0 )
r = 0;
}
if (r)
N[0] = 1.0;
}
}
else if ( N[d] > x )
{
if ( N[d] != 1.0 && N[d] < 1.0 + ON_SQRT_EPSILON )
{
r = 1;
for ( j = 0; j < d && r; j++ )
{
if ( N[j] != 0.0 )
r = 0;
}
if (r)
N[d] = 1.0;
}
}
return true;
}
bool ON_EvaluateNurbsBasisDerivatives( int order, const double* knot,
int der_count, double* N )
{
/* INPUT:
* Results of the call
* TL_EvNurbBasis( order, knot, t, N ); (initializes N[] )
* are sent to
* TL_EvNurbBasisDer( order, knot, der_count, N ),
* where 1 <= der_count < order
*
* OUTPUT:
* If "N" were declared as double N[order][order], then
*
* d
* N[d-k][i] = k-th derivative of N (t)
* i
*
* where 0 <= k <= d and 0 <= i <= d.
*
* In particular,
* N[0], ..., N[d] - values of degree d basis functions.
* N[order], ..., N[order_d] - values of first derivative.
*
* Actually, the above is true when knot[d-1] <= t < knot[d]. Otherwise, the
* values returned are the values of the polynomials that agree with N_i^k on the
* half open domain [ knot[d-1], knot[d] )
*
* Ref: The NURBS Book
*/
double dN, c;
const double *k0, *k1;
double *a0, *a1, *ptr, **dk;
int i, j, k, jmax;
const int d = order-1;
const int Nstride = -der_count*order;
/* workspaces for knot differences and coefficients
*
* a0[] and a1[] have order doubles
*
* dk[0] = array of d knot differences
* dk[1] = array of (d-1) knot differences
*
* dk[der_count-1] = 1.0/(knot[d] - knot[d-1])
* dk[der_count] = dummy pointer to make loop efficient
*/
dk = (double**)alloca( (der_count+1) << 3 ); /* << 3 in case pointers are 8 bytes long */
a0 = (double*)alloca( (order*(2 + ((d+1)>>1))) << 3 ); /* d for a0, d for a1, d*order/2 for dk[]'s and slop to avoid /2 */
a1 = a0 + order;
/* initialize reciprocal of knot differences */
dk[0] = a1 + order;
for (k = 0; k < der_count; k++) {
j = d-k;
k0 = knot++;
k1 = k0 + j;
for (i = 0; i < j; i++)
dk[k][i] = 1.0/(*k1++ - *k0++);
dk[k+1] = dk[k] + j;
}
dk--;
/* dk[1] = 1/{t[d]-t[0], t[d+1]-t[1], ..., t[2d-2] - t[d-2], t[2d-1] - t[d-1]}
* = diffs needed for 1rst derivative
* dk[2] = 1/{t[d]-t[1], t[d+1]-t[2], ..., t[2d-2] - t[d-1]}
* = diffs needed for 2nd derivative
* ...
* dk[d] = 1/{t[d] - t[d-1]}
* = diff needed for d-th derivative
*
* d[k][n] = 1.0/( t[d+n] - t[k-1+n] )
*/
N += order;
/* set N[0] ,..., N[d] = 1rst derivatives,
* N[order], ..., N[order+d] = 2nd, etc.
*/
for ( i=0; i<order; i++) {
a0[0] = 1.0;
for (k = 1; k <= der_count; k++) {
/* compute k-th derivative of N_i^d up to d!/(d-k)! scaling factor */
dN = 0.0;
j = k-i;
if (j <= 0) {
dN = (a1[0] = a0[0]*dk[k][i-k])*N[i];
j = 1;
}
jmax = d-i;
if (jmax < k) {
while (j <= jmax) {
dN += (a1[j] = (a0[j] - a0[j-1])*dk[k][i+j-k])*N[i+j];
j++;
}
}
else {
/* sum j all the way to j = k */
while (j < k) {
dN += (a1[j] = (a0[j] - a0[j-1])*dk[k][i+j-k])*N[i+j];
j++;
}
dN += (a1[k] = -a0[k-1]*dk[k][i])*N[i+k];
}
/* d!/(d-k)!*dN = value of k-th derivative */
N[i] = dN;
N += order;
/* a1[] s for next derivative = linear combination
* of a[]s used to compute this derivative.
*/
ptr = a0; a0 = a1; a1 = ptr;
}
N += Nstride;
}
/* apply d!/(d-k)! scaling factor */
dN = c = (double)d;
k = der_count;
while (k--) {
i = order;
while (i--)
*N++ *= c;
dN -= 1.0;
c *= dN;
}
return true;
}
static
bool ON_EvaluateNurbsNonRationalSpan(
int dim, // dimension
int order, // order
const double* knot, // knot[] array of (2*order-2) doubles
int cv_stride, // cv_stride >= (is_rat)?dim+1:dim
const double* cv, // cv[order*cv_stride] array
int der_count, // number of derivatives to compute
double t, // evaluation parameter
int v_stride, // v_stride (>=dimension)
double* v // v[(der_count+1)*v_stride] array
)
{
const int stride_minus_dim = cv_stride - dim;
const int cv_len = cv_stride*order;
int i, j, k;
double *N;
double a;
N = (double*)alloca( (order*order)<<3 );
if ( stride_minus_dim > 0)
{
i = (der_count+1);
while( i--)
{
memset(v,0,dim*sizeof(v[0]));
v += v_stride;
}
v -= ((der_count+1)*v_stride);
}
else
{
memset( v, 0, (der_count+1)*v_stride*sizeof(*v) );
}
if ( der_count >= order )
der_count = order-1;
// evaluate basis functions
ON_EvaluateNurbsBasis( order, knot, t, N );
if ( der_count )
ON_EvaluateNurbsBasisDerivatives( order, knot, der_count, N );
// convert cv's into answers
for (i = 0; i <= der_count; i++, v += v_stride, N += order) {
for ( j = 0; j < order; j++ ) {
a = N[j];
for ( k = 0; k < dim; k++ ) {
*v++ += a* *cv++;
}
v -= dim;
cv += stride_minus_dim;
}
cv -= cv_len;
}
if ( 2 == order )
{
// 7 January 2004 Dale Lear