-
Notifications
You must be signed in to change notification settings - Fork 0
/
nurbs.py
1380 lines (1258 loc) · 60.4 KB
/
nurbs.py
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
import numpy as np
import scipy.special as spe
import scipy.sparse as sps
def KnotInsertionCoefficients(U,u,p):
""" Returns the knot insertion coefficients """
k = findKnotSpan(u, U, p)
l = len(U)-p-1
alpha = np.zeros(l+1)
alpha[:k-p+1] = 1
alpha[k-p+1:k+1] = (u-U[k-p+1:k+1])/(U[k+1:k+p+1]-U[k-p+1:k+1])
alpha[k+1:] = 0
return alpha, k
def KnotInsertionOperatorOneKnot(U,u,p):
""" Consttucts the uni-variate knot refinement operator for one knot insertion """
alpha, k = KnotInsertionCoefficients(U,u,p)
nbf = len(alpha)-1
indexI = np.kron(np.arange(nbf),np.ones(2))
indexJ = np.c_[np.arange(nbf),np.arange(1,nbf+1)].ravel()
nnz_values = np.c_[alpha[:-1],1-alpha[1:]].ravel()
M = sps.csc_matrix(( nnz_values, (indexI,indexJ)), shape = (nbf,nbf+1))
return M,k
def KnotInsertionOperatorMultipleKnots(U,um,p):
""" Constructs the uni-variate knot refinement operator for multiple knot insertion"""
if len(um)==0:
raise ValueError('Must insert at least one knot')
Uc = np.copy(U)
M,k = KnotInsertionOperatorOneKnot(Uc,um[0],p)
Uc = np.insert(Uc,k+1,um[0])
if len(um>1):
for i in range(1,len(um)):
Mo,k = KnotInsertionOperatorOneKnot(Uc,um[i],p)
M = M.dot(Mo)
Uc = np.insert(Uc,k+1,um[i])
return M
def KnotInsertionOperator2d(p,U,um,V,vm):
"""
returns the 2d refinement opertor by the tensor product operation
Coarse knot vectors in Xi and Eta directions: cXi,cEta
Fine knot vectors in Xi and Eta directions: rXi,rEta
"""
Cu = KnotInsertionOperatorMultipleKnots(U,um,p)
Cv = KnotInsertionOperatorMultipleKnots(V,vm,p)
C = sps.kron(Cv,Cu)
return C
def bezier_extraction_nurbs_1d(U,m,p):
C = []
a = p+1
b = a+1
nb = 1
C.append(np.identity(p+1))
while b < m:
C.append(np.identity(p+1)) # Initialize the next extraction operator.
i = b
# Count multiplicity of the knot at location b.
while b < m and U[b] == U[b-1]:
b = b+1
mult = b-i+1
if mult < p:
#Use (10) to compute the alphas.
numer = U[b-1]-U[a-1]
alphas = [0 for x in range(p)]
for j in range(p,mult,-1):
alphas[j-mult-1] = numer / (U[a+j-1]-U[a-1])
r = p-mult
#Update the matrix coefficients for r new knots
for j in range(1,r+1):
save = r-j+1
s = mult+j
for k in range(p+1,s,-1):
alpha = alphas[k-s-1]
#The following line corresponds to (9).
C[-2][:,k-1] = alpha*C[-2][:,k-1] + (1.0-alpha)*C[-2][:,k-2]
if b < m:
#Update overlapping coefficients of the next operator.
C[-1][save-1:j+save,save-1] = C[-2][p-j:p+1,p]
nb = nb + 1 # Finished with the current operator.
if b < m:
#Update indices for the next operator.
a = b
b = b+1
C.pop()
return C, nb
# def Oslo1(p, coarsekn, finekn, rf ):
# """ Knot insertion coefficients
# using Oslo algorithm
# Taken from Article Multi level Bezier extraction
# for hierarchical local refinement of IGA """
# # rf in range(m-p-1)
# cf = findKnotSpan(finekn[rf],coarsekn,p)
# b = 1
# for k in range(1,p+1):
# t1 = coarsekn[cf-k+1:cf+1]
# t2 = coarsekn[cf+1:cf+k+1]
# x = finekn[rf+k]
# w = (x-t1)/(t2-t1)
# b = np.r_[(1-w)*b,0] + np.r_[0,w*b]
# return b
# def KnotInsertionOperator(p,coarsekn,finekn):
# m = len(finekn)
# rf = 0
# C = Oslo1(p,coarsekn,finekn,rf)
# for rf in range(1,m-p-1):
# b = Oslo1(p,coarsekn,finekn,rf)
# C = np.c_[C,b]
# return C
# def KnotInsertionOperator2d(p,cXi,cEta,fXi,fEta):
# """
# returns the 2d refinement opertor
# Coarse knot vectors in Xi and Eta directions: cXi,cEta
# Fine knot vectors in Xi and Eta directions: rXi,rEta
# """
# Cxi = KnotInsertionOperator(p,cXi,fXi)
# Ceta = KnotInsertionOperator(p,cEta,fEta)
# C = np.kron(Ceta,Cxi)
# return C
#def MultipleKnotInsertionsOperator(p,coarsekn,knots):
# coarseTemp = coarsekn.copy()
# k = knots[0]
# cf = findKnotSpan(k,coarseTemp,p)
# finekn = np.insert(coarseTemp, cf+1, k)
# C = OneKnotInsertionOperator(p,coarseTemp,finekn)
# coarseTemp = finekn
# print(C)
# for i in range(len(knots)-1):
# k = knots[i+1]
# cf = findKnotSpan(k,coarseTemp,p)
# finekn = np.insert(coarseTemp, cf+1, k)
# print(k,cf,finekn, coarseTemp)
# print(OneKnotInsertionOperator(p,coarseTemp,finekn))
# C = C @ OneKnotInsertionOperator(p,coarseTemp,finekn)
# coarseTemp = finekn
# return C
def BernsteinRef(u,p):
""" Evaluates Bernsein basis functions defined on [-1,1]"""
i = np.arange(p+1)
ur = np.repeat(u,p+1).reshape((len(u),p+1))
N = ((1-ur)**(p-i)*(1+ur)**i)*spe.comb(p,i)/(2**p)
N1 = ((1-ur)**(p-1-(i-1))*(1+ur)**(i-1))*spe.comb(p-1,i-1)/(2**(p-1))
N2 = ((1-ur)**(p-1-(i))*(1+ur)**(i))*spe.comb(p-1,i)/(2**(p-1))
dN = 0.5*p*(N1-N2)
return N,dN
def nubsconnect(p,n):
"""
It returns the connectivity table INE for NURBS
nel : element index in the total connectivity
nen : number of functions per support
p : degree of functions
n : number of functions = number of control points
fonction qui donne la table de connectivite INE pour un NURBS
Numerotation en arriere pour trouver directement NURBS coordinate
nel nombre d'element total
nen nombre de fonctions par support
p degre des fnc
n nombre de fonctions (nbre de points de controle) """
nel = n-p
nen = p+1
IEN = np.zeros((nen,nel),dtype='int64')
for i in range(nen):
for j in range(nel):
IEN[i,j]=p+j-i
return IEN
def connect_2D(n_xi,n_eta,p,q):
"""
Connectivity between 2D elements and control points
Each parametric element is supported by (p+1)*(q+1) basis function
"""
nbf_elem = (p+1)*(q+1)
n_elems = n_xi*n_eta
IEN = np.zeros((n_elems,nbf_elem))
k=0
for j in range(n_eta):
for i in range(n_xi):
t=0
# Local functions numbers support (p+1)*(q+1)
for s in range(q+1):
for r in range(p+1):
IEN[k,t] = (n_xi+p)*(j+s)+i+r
t+=1
k+=1
return IEN
#%%
def GaussLegendre(n):
# [nodes,weigths]=GaussLegendre(n)
#
# Generates the abscissa and weights for a Gauss-Legendre quadrature.
# Reference: Numerical Recipes in Fortran 77, Cornell press.
xg = np.zeros(n) # Preallocations.
wg = np.zeros(n)
m = (n+1)/2
#import pdb; pdb.set_trace()
for ii in range(int(m)): # for ii=1:m
z = np.cos(np.pi*(ii+1-0.25)/(n+0.5)) # Initial estimate.
z1 = z+1
while np.abs(z-z1)>np.finfo(np.float64).eps:
p1 = 1
p2 = 0
for jj in range(n): #for jj = 1:n
p3 = p2
p2 = p1
p1 = ((2*jj+1)*z*p2-(jj)*p3)/(jj+1) # The Legendre polynomial.
pp = n*(z*p1-p2)/(z**2-1) # The L.P. derivative.
z1 = z
z = z1-p1/pp
xg[ii] = -z # Build up the abscissas.
xg[-1-ii] = z
wg[ii] = 2/((1-z**2)*(pp**2)) # Build up the weights.
wg[-1-ii] = wg[ii]
return xg,wg
def GaussTetrahedron(n):
"""
Gauss integration on a tetrahedron using barycentric coordinates
returns the barycentric coordinates of the integration points
and the weight coefficient:
Attention: we must multiply the returned weight by the volume of the integrated tetrahedron
Taken from Alexander Ern and Jean-Luc Guermond book on finite elements
return c: the barycentric coordinates
wc: weight coefficient that must be multiplied by the tetrahedon volume
"""
if n==1:
# Deg = 1: 1 point
c = np.array([[1/4,1/4,1/4,1/4]])
wc = np.array([1])
elif n==2:
# Deg =2: 4 points
a = (5-np.sqrt(5))/20
c = np.array([[1-3*a,a,a,a],
[a,1-3*a,a,a],
[a,a,1-3*a,a],
[a,a,a,1-3*a]])
wc = np.array([1/4,1/4,1/4,1/4])
elif n==3:
# Deg 3: 5 points
c = np.array([[1/4,1/4,1/4,1/4],
[1/2,1/6,1/6,1/6],
[1/6,1/2,1/6,1/6],
[1/6,1/6,1/2,1/6],
[1/6,1/6,1/6,1/2]])
wc = np.array([-4/5, 9/20, 9/20, 9/20,9/20])
elif n == 4 or n== 5 :
# Deg 5: 15 points
a1 = (7-np.sqrt(15))/34
a2 = (7+np.sqrt(15))/34
a = (10-2*np.sqrt(5))/40
w1 = (2665+14*np.sqrt(15))/37800
w2 = (2665-14*np.sqrt(15))/37800
w = 10/189
c = np.array([[1/4,1/4,1/4,1/4],
[1-2*a1,a1,a1,a1],
[a1,1-2*a1,a1,a1],
[a1,a1,1-2*a1,a1],
[a1,a1,a1,1-2*a1],
[1-2*a2,a2,a2,a2],
[a2,1-2*a2,a2,a2],
[a2,a2,1-2*a2,a2],
[a2,a2,a2,1-2*a2],
[1/2-a,1/2-a,a,a],
[1/2-a,a,1/2-a,a],
[1/2-a,a,a,1/2-a],
[a,1/2-a,1/2-a,a],
[a,a,1/2-a,1/2-a],
[a,1/2-a,1/2-a,a]])
wc = np.array([16/135, w1,w1,w1,w1, w2,w2,w2,w2, w,w,w,w,w,w])
else :
raise ValueError('WARNING: Maximum number of Gauss points for tetrahedon reached' )
return c,wc
def GaussTriangle(n):
""" Gauss integration on the
reference triangle """
if n== 1 :
# Deg = 1 : 1 point
x = 1/3*np.array([[1,1]])
w = np.array([1/2])
elif n == 2 :
# Deg = 2 : 3 points
x = 1/6*np.array([[1,1],
[4,1],
[1,4]])
w = 1/6*np.array([ 1,1,1 ])
elif n == 3 :
# Deg =3 : 4 points
x = np.array([ np.array([1/3 ,1/3]),
1/5*np.array([1, 1]),
1/5*np.array([3, 1]),
1/5*np.array([1, 3]) ] )
w=1/96*np.array([-27 , 25 , 25 , 25]);
elif n == 4 :
# Deg = 4 : 6 points
a=0.445948490915965;
b=0.091576213509771;
x = np.array([ [a, a],
[1-2*a, a],
[a, 1-2*a],
[b, b],
[1-2*b, b],
[b, 1-2*b] ])
w = np.r_[ 0.111690794839005*np.array([1 , 1 , 1]), 0.054975871827661*np.array([1 , 1 , 1]) ]
elif n== 5 :
# Deg = 5 : 7 points
a=(6+np.sqrt(15))/21;
b=4/7-a;
x= np.array([ [1/3, 1/3 ],
[a , a],
[1-2*a , a],
[a , 1-2*a],
[b , b],
[1-2*b , b],
[b , 1-2*b] ] )
w = np.r_[ np.array([9/80]), (155+np.sqrt(15))/2400*np.array([1 , 1 , 1]), (31/240-(155+np.sqrt(15))/2400)*np.array([1 , 1 , 1]) ]
elif n == 6 :
# Deg = 6 : 12 points
w = np.array( [0.05839314 ,
0.05839314 ,
0.05839314 ,
0.02542245 ,
0.02542245 ,
0.02542245 ,
0.04142554 ,
0.04142554 ,
0.04142554 ,
0.04142554 ,
0.04142554 ,
0.04142554] )
x = np.array( [ [0.5014265 , 0.2492867],
[0.2492867 , 0.5014265] ,
[0.2492867 , 0.2492867] ,
[0.8738220 , 0.06308901] ,
[0.06308901 , 0.8738220] ,
[0.06308901 , 0.06308901] ,
[0.6365025 , 0.05314505] ,
[0.6365025 , 0.3103525] ,
[0.05314505 , 0.6365025] ,
[0.05314505 , 0.3103525] ,
[0.3103525 , 0.6365025] ,
[0.3103525 , 0.05314505] ] )
elif n == 7 :
# Deg 7 : 13 points
w= np.array( [-0.07478502 ,
0.08780763 ,
0.08780763 ,
0.08780763 ,
0.02667362 ,
0.02667362 ,
0.02667362 ,
0.03855688 ,
0.03855688 ,
0.03855688 ,
0.03855688 ,
0.03855688 ,
0.03855688 ] )
x= np.array([ [0.3333333, 0.3333333 ] ,
[0.4793081 , 0.2603460],
[0.2603460 , 0.4793081],
[0.2603460 , 0.2603460],
[0.8697398 , 0.06513010],
[0.06513010 , 0.8697398],
[0.06513010 , 0.06513010],
[0.6384442 , 0.04869032],
[0.6384442 , 0.3128655],
[0.04869032 , 0.6384442],
[0.04869032 , 0.3128655],
[0.3128655 , 0.6384442],
[0.3128655 , 0.04869032] ] )
elif n== 8 :
# Deg 8 : 16 points
w= np.array( [0.07215780 ,
0.04754582,
0.04754582,
0.04754582,
0.01622925,
0.01622925,
0.01622925,
0.05160869,
0.05160869,
0.05160869,
0.01361516,
0.01361516,
0.01361516,
0.01361516,
0.01361516,
0.01361516] )
x= np.array([ [0.3333333, 0.3333333],
[0.08141482 , 0.4592926 ] ,
[0.4592926 , 0.08141482 ] ,
[0.4592926 , 0.4592926 ] ,
[0.8989055 , 0.05054723 ] ,
[0.05054723 , 0.8989055 ] ,
[0.05054723 , 0.05054723 ] ,
[0.6588614 , 0.1705693 ] ,
[0.1705693 , 0.6588614 ] ,
[0.1705693 , 0.1705693 ] ,
[0.008394777 , 0.7284924 ] ,
[0.008394777 , 0.2631128 ] ,
[0.7284924 , 0.008394777 ] ,
[0.7284924 , 0.2631128 ] ,
[0.2631128 , 0.008394777 ] ,
[0.2631128 , 0.7284924] ] )
elif n == 9 :
# Deg 9 : 19 points
w= np.array( [0.04856790 ,
0.01566735 ,
0.01566735 ,
0.01566735,
0.03891377,
0.03891377,
0.03891377,
0.03982387,
0.03982387,
0.03982387,
0.01278884,
0.01278884,
0.01278884,
0.02164177,
0.02164177,
0.02164177,
0.02164177,
0.02164177,
0.02164177] )
x= np.array([ [0.3333333 , 0.3333333 ],
[0.02063496, 0.4896825 ],
[0.4896825 , 0.02063496 ],
[0.4896825 , 0.4896825 ],
[0.1258208 , 0.4370896 ],
[0.4370896 , 0.1258208 ],
[0.4370896 , 0.4370896 ],
[0.6235929 , 0.1882035 ],
[0.1882035 , 0.6235929 ],
[0.1882035 , 0.1882035 ],
[0.9105410 , 0.04472951 ],
[0.04472951 , 0.9105410 ],
[0.04472951 , 0.04472951 ],
[0.03683841 , 0.7411986 ],
[0.03683841 , 0.2219630 ],
[0.7411986 , 0.03683841 ],
[0.7411986 , 0.2219630 ],
[0.2219630 , 0.03683841 ],
[0.2219630 , 0.7411986 ] ] )
elif n == 11 :
# Deg 11 : 27 points
w= np.array( [0.006829866 ,
0.006829866 ,
0.006829866 ,
0.01809227 ,
0.01809227 ,
0.01809227 ,
0.0004635032 ,
0.0004635032 ,
0.0004635032 ,
0.02966149 ,
0.02966149 ,
0.02966149 ,
0.03857477 ,
0.03857477 ,
0.03857477 ,
0.02616856 ,
0.02616856 ,
0.02616856 ,
0.02616856 ,
0.02616856 ,
0.02616856 ,
0.01035383 ,
0.01035383 ,
0.01035383 ,
0.01035383 ,
0.01035383 ,
0.01035383] )
x= np.array([ [0.9352701 , 0.03236495 ],
[0.03236495 , 0.9352701 ]
[0.03236495 , 0.03236495 ]
[0.7612982 , 0.1193509 ]
[0.1193509 , 0.7612982 ]
[0.1193509 , 0.1193509 ]
[0.06922210 , 0.5346110 ]
[0.5346110 , 0.06922210 ]
[0.5346110 , 0.5346110 ]
[0.5933802 , 0.2033099 ]
[0.2033099 , 0.5933802 ]
[0.2033099 , 0.2033099 ]
[0.2020614 , 0.3989693 ]
[0.3989693 , 0.2020614 ]
[0.3989693 , 0.3989693 ]
[0.05017814 , 0.5932012 ]
[0.05017814 , 0.3566206 ]
[0.5932012 , 0.05017814 ]
[0.5932012 , 0.3566206 ]
[0.3566206 , 0.05017814 ]
[0.3566206 , 0.5932012 ]
[0.02102202 , 0.8074890 ]
[0.02102202 , 0.1714890 ]
[0.8074890 , 0.02102202 ]
[0.8074890 , 0.1714890 ]
[0.1714890 , 0.02102202 ]
[0.1714890 , 0.8074890] ] )
else :
raise ValueError('WARNING: Maximum number of Gauss points for triangle reached (11)' )
return x,w
# def derbasisfuncVectorInput(p,U,u,nb_u_values,span,nders):
# ders_matrix = np.zeros(((nders+1)*(nb_u_values),p+1))
# for i in range(nb_u_values):
# ders = derbasisfuns(span,p,U,nders,u[i])
# ders_matrix[2*i:2*(i+1),:] = ders
# return ders_matrix
# def derbasisfunsPython(p,U,u,nb_u_values,span,nders):
# ders = derbasisfuncVectorInput(p,U,u,nb_u_values,span,nders)
# N = ders[::2,:]
# dN = ders[1::2,:]
# return N,dN
def Lagrange1D(n, xi):
"""1D Lagrange basis functions
# defined on the reference element [-1,1]"""
if n == 2 :
N = np.array([0.5*(1-xi),0.5*(xi+1)])
dN = np.array([-0.5,0.5])
else :
raise ValueError('Only n=2 implemented')
return [N,dN]
def Lagrange2D(n,type_e, xi, eta):
"""2D Lagrange basis functions
defined on reference elements ([-1,1] square or [0,1] triangle)
n: number of basis functions
type_e: element type quad or triangle
"""
if type_e !='quad' and type_e!='triangle':
raise ValueError('Element type not considered')
if type_e =='triangle':
if n == 3:
N = np.array([1-xi-eta,eta,xi])
dNdxi = np.array([-1,0,1])
dNdeta = np.array([-1,1,0])
else :
raise ValueError('Only T3 implemented')
elif type_e =='quad':
if n== 4:
N = np.array([0.25*(1-xi)*(1-eta),
0.25*(1-xi)*(1+eta),
0.25*(1+xi)*(1+eta),
0.25*(1+xi)*(1-eta)])
dNdxi = np.array([-0.25*(1-eta),
-0.25*(1+eta),
0.25*(1+eta),
0.25*(1-eta)])
dNdeta = np.array([-0.25*(1-xi),
0.25*(1-xi),
0.25*(1+xi),
-0.25*(1+xi)])
else :
raise ValueError('Only Q4 implemented')
return [N,dNdxi,dNdeta]
#%%
def findKnotSpan(u,U,p):
"""
Finds the knots space of a given knot parameter u in
the knot vector U corresponding to the degree p
"""
m = np.size(U)
if u==U[m-p-1]:
k=m-p-2
else :
k=np.max(np.where(u>=U))
return k
def findspan(n,p,u,U):
return findKnotSpan(u,U,p)
def findspanUniformKnotVector(U,deg,l,u):
if u==U[len(U)-deg-1]:
return len(U)-deg-2
return int(np.floor( (u-U[0])/l))+deg
#%% Semble Ok. Bien penser à mettre les knots à ajouter sous forme d'un vecteur np.array (même s'il n'y en a qu'un)
def bspkntins(d,c,k,u):
''' Function Name:
#
# bspkntins - Insert knots into a univariate B-Spline.
#
# Calling Sequence:
#
# [ic,ik] = bspkntins(d,c,k,u)
#
# Parameters:
#
# d : Degree of the B-Spline.
#
# c : Control points, matrix of size (dim,nc).
#
# k : Knot sequence, row vector of size nk.
#
# u : Row vector of knots to be inserted, size nu
#
# ic : Control points of the new B-Spline, of size (dim,nc+nu)
#
# ik : Knot vector of the new B-Spline, of size (nk+nu)
#
# Description:
#
# Insert knots into a univariate B-Spline. This function provides an
# interface to a toolbox 'C' routine. '''
mc,nc = c.shape
nu = len(u)
nk = len(k)
#
# int bspkntins(int d, double *c, int mc, int nc, double *k, int nk,
# double *u, int nu, double *ic, double *ik)
# {
# int ierr = 0;
# int a, b, r, l, i, j, m, n, s, q, ind;
# double alfa;
#
# double **ctrl = vec2mat(c, mc, nc);
ic = np.zeros((mc,nc+nu)) # double **ictrl = vec2mat(ic, mc, nc+nu);
ik = np.zeros(nk+nu)
#
n = c.shape[1] - 1 # n = nc - 1;
r = len(u) - 1 # r = nu - 1;
#
m = n + d + 1 # m = n + d + 1;
a = findspan(n, d, u[0], k) # a = findspan(n, d, u[0], k);
b = findspan(n, d, u[r], k) # b = findspan(n, d, u[r], k);
b+=1 # ++b;
#
for q in range(mc): # for (q = 0; q < mc; q++) {
for j in range(a-d+1):
ic[q,j] = c[q,j] # for (j = 0; j <= a-d; j++) ictrl[j][q] = ctrl[j][q];
for j in range(b-1,n+1):
ic[q,j+r+1] = c[q,j] # for (j = b-1; j <= n; j++) ictrl[j+r+1][q] = ctrl[j][q];
# }
for j in range(a+1):
ik[j] = k[j] # for (j = 0; j <= a; j++) ik[j] = k[j];
for j in range(b+d,m+1):
ik[j+r+1] = k[j] # for (j = b+d; j <= m; j++) ik[j+r+1] = k[j];
#
i = b + d - 1 # i = b + d - 1;
s = b + d + r # s = b + d + r;
for j in range(r,-1,-1): # for (j = r; j >= 0; j--) {
while (u[j] <= k[i] and i > a): # while (u[j] <= k[i] && i > a) {
for q in range(mc): # for (q = 0; q < mc; q++)
ic[q,s-d-1] = c[q,i-d-1] # ictrl[s-d-1][q] = ctrl[i-d-1][q];
ik[s] = k[i] # ik[s] = k[i];
s -= 1 # --s;
i -= 1 # --i;
# }
for q in range(mc): # for (q = 0; q < mc; q++)
ic[q,s-d-1] = ic[q,s-d] # ictrl[s-d-1][q] = ictrl[s-d][q];
for l in range(1,d+1): # for (l = 1; l <= d; l++) {
ind = s - d + l # ind = s - d + l;
alfa = ik[s+l] - u[j] # alfa = ik[s+l] - u[j];
if abs(alfa) == 0: # if (fabs(alfa) == 0.0)
for q in range(mc): # for (q = 0; q < mc; q++)
ic[q,ind-1] = ic[q,ind] # ictrl[ind-1][q] = ictrl[ind][q];
else: # else {
alfa = alfa/(ik[s+l] - k[i-d+l]) # alfa /= (ik[s+l] - k[i-d+l]);
for q in range(mc): # for (q = 0; q < mc; q++)
tmp = (1.-alfa)*ic[q,ind]
ic[q,ind-1] = alfa*ic[q,ind-1] + tmp # ictrl[ind-1][q] = alfa*ictrl[ind-1][q]+(1.0-alfa)*ictrl[ind][q];
# }
# }
#
ik[s] = u[j] # ik[s] = u[j];
s -= 1 # --s;
# }
#
# freevec2mat(ctrl);
# freevec2mat(ictrl);
#
# return ierr;
# }
return ic,ik
#%%
def bincoeff(n,k):
# Computes the binomial coefficient.
#
# ( n ) n!
# ( ) = --------
# ( k ) k!(n-k)!
#
# b = bincoeff(n,k)
#
# Algorithm from 'Numerical Recipes in C, 2nd Edition' pg215.
# double bincoeff(int n, int k)
# {
b = np.floor(0.5+np.exp(factln(n)-factln(k)-factln(n-k))); # return floor(0.5+exp(factln(n)-factln(k)-factln(n-k)));
return b
def factln(n):
# computes ln(n!)
if n <= 1:
f = 0
return f
f = spe.gammaln(n+1) #log(factorial(n));</pre>
return f
#%%
def bspdegelev(d,c,k,t):
'''
# Function Name:
# bspdegevel - Degree elevate a univariate B-Spline.
# Calling Sequence:
# [ic,ik] = bspdegelev(d,c,k,t)
# Parameters:
# d : Degree of the B-Spline.
# c : Control points, matrix of size (dim,nc).
# k : Knot sequence, row vector of size nk.
# t : Raise the B-Spline degree t times.
# ic : Control points of the new B-Spline.
# ik : Knot vector of the new B-Spline.
# Description:
# Degree elevate a univariate B-Spline. This function provides an
# interface to a toolbox 'C' routine.
'''
mc,nc = c.shape
#
# int bspdegelev(int d, double *c, int mc, int nc, double *k, int nk,
# int t, int *nh, double *ic, double *ik)
# {
# int row,col
#
# int ierr = 0;
# int i, j, q, s, m, ph, ph2, mpi, mh, r, a, b, cind, oldr, mul;
# int n, lbz, rbz, save, tr, kj, first, kind, last, bet, ii;
# double inv, ua, ub, numer, den, alf, gam;
# double **bezalfs, **bpts, **ebpts, **Nextbpts, *alfs;
#
#init ic # double **ctrl = vec2mat(c, mc, nc);
ic = np.zeros((mc,nc*(t+1))) # double **ictrl = vec2mat(ic, mc, nc*(t+1));
ik = np.zeros((t+1)*k.shape[0])
#
n = nc - 1 # n = nc - 1;
#
bezalfs = np.zeros((d+1,d+t+1)) # bezalfs = matrix(d+1,d+t+1);
bpts = np.zeros((mc,d+1)) # bpts = matrix(mc,d+1);
ebpts = np.zeros((mc,d+t+1)) # ebpts = matrix(mc,d+t+1);
Nextbpts = np.zeros((mc,d+1)) # Nextbpts = matrix(mc,d+1);
alfs = np.zeros((d,1)) # alfs = (double *) mxMalloc(d*sizeof(double));
#
m = n + d + 1 # m = n + d + 1;
ph = d + t # ph = d + t;
ph2 = int(ph/2) # ph2 = ph / 2;
#
# // compute bezier degree elevation coefficeients
bezalfs[0,0] = 1. # bezalfs[0][0] = bezalfs[ph][d] = 1.0;
bezalfs[d,ph] = 1. #
for i in np.arange(1,ph2+1): #1:ph2 # for (i = 1; i <= ph2; i++) {
inv = 1/bincoeff(ph,i) # inv = 1.0 / bincoeff(ph,i);
mpi = min(d,i) # mpi = min(d,i);
#
for j in np.arange(max(0,i-t),mpi+1): #max(0,i-t):mpi # for (j = max(0,i-t); j <= mpi; j++)
bezalfs[j,i] = inv*bincoeff(d,j)*bincoeff(t,i-j) # bezalfs[i][j] = inv * bincoeff(d,j) * bincoeff(t,i-j);
#
for i in np.arange(ph2+1,ph): #ph2+1:ph-1 # for (i = ph2+1; i <= ph-1; i++) {
mpi = min(d,i) # mpi = min(d, i);
for j in np.arange(max(0,i-t),mpi+1): #max(0,i-t):mpi # for (j = max(0,i-t); j <= mpi; j++)
bezalfs[j,i] = bezalfs[d-j,ph-i] # bezalfs[i][j] = bezalfs[ph-i][d-j];
#
mh = ph # mh = ph;
kind = ph+1 # kind = ph+1;
r = -1 # r = -1;
a = d # a = d;
b = d+1 # b = d+1;
cind = 1 # cind = 1;
ua = k[0] # ua = k[0];
#
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
ic[ii,0] = c[ii,0] # ictrl[0][ii] = ctrl[0][ii];
for i in range(ph+1): #0:ph # for (i = 0; i <= ph; i++)
ik[i] = ua # ik[i] = ua;
# // initialise first bezier seg
for i in range(d+1): #0:d # for (i = 0; i <= d; i++)
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
bpts[ii,i] = c[ii,i] # bpts[i][ii] = ctrl[i][ii];
# // big loop thru knot vector
while b < m : # while (b < m) {
i = b # i = b;
while b < m and k[b] == k[b+1] : # while (b < m && k[b] == k[b+1])
b = b + 1 # b++;
mul = b - i + 1 # mul = b - i + 1;
mh += mul + t # mh += mul + t;
ub = k[b] # ub = k[b];
oldr = r # oldr = r;
r = d - mul # r = d - mul;
#
# // insert knot u(b) r times
if oldr > 0: # if (oldr > 0)
lbz = np.floor((oldr+2)/2) # lbz = (oldr+2) / 2;
else : # else
lbz = 1 # lbz = 1;
if r > 0 : # if (r > 0)
rbz = ph - np.floor((r+1)/2) # rbz = ph - (r+1)/2;
else : # else
rbz = ph # rbz = ph;
if r > 0 : # if (r > 0) {
# // insert knot to get bezier segment
numer = ub - ua # numer = ub - ua;
for q in np.arange(d,mul,-1): #d:-1:mul+1 # for (q = d; q > mul; q--)
alfs[q-mul-1] = numer / (k[a+q]-ua) # alfs[q-mul-1] = numer / (k[a+q]-ua);
for j in np.arange(1,r+1): #1:r # for (j = 1; j <= r; j++) {
save = r - j # save = r - j;
s = mul + j # s = mul + j;
#
for q in np.arange(d,s-1,-1): #d:-1:s # for (q = d; q >= s; q--)
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
tmp1 = alfs[q-s]*bpts[ii,q]
tmp2 = (1-alfs(q-s))*bpts(ii,q-1)
bpts[ii,q] = tmp1 + tmp2 # bpts[q][ii] = alfs[q-s]*bpts[q][ii]+(1.0-alfs[q-s])*bpts[q-1][ii];
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
Nextbpts[ii,save] = bpts[ii,d] # Nextbpts[save][ii] = bpts[d][ii];
# // end of insert knot
#
# // degree elevate bezier
for i in np.arange(lbz,ph+1): #lbz:ph # for (i = lbz; i <= ph; i++) {
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
ebpts[ii,i] = 0 # ebpts[i][ii] = 0.0;
mpi = min(d, i) # mpi = min(d, i);
for j in np.arange(max(0,i-t),mpi+1): #max(0,i-t):mpi # for (j = max(0,i-t); j <= mpi; j++)
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
tmp1 = ebpts[ii,i]
tmp2 = bezalfs[j,i]*bpts[ii,j]
ebpts[ii,i] = tmp1 + tmp2 # ebpts[i][ii] = ebpts[i][ii] + bezalfs[i][j]*bpts[j][ii];
# // end of degree elevating bezier
#
if oldr > 1 : # if (oldr > 1) {
# // must remove knot u=k[a] oldr times
first = kind - 2 # first = kind - 2;
last = kind # last = kind;
den = ub - ua # den = ub - ua;
bet = np.floor((ub-ik[kind-1]) / den) # bet = (ub-ik[kind-1]) / den;
#
# // knot removal loop
for tr in np.arange(1,oldr): #1:oldr-1 # for (tr = 1; tr < oldr; tr++) {
i = first # i = first;
j = last # j = last;
kj = j - kind + 1 # kj = j - kind + 1;
while j-i > tr : # while (j - i > tr) {
# // loop and compute the new control points
# // for one removal step
if i < cind : # if (i < cind) {
alf = (ub-ik[i])/(ua-ik[i]) # alf = (ub-ik[i])/(ua-ik[i]);
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
tmp1 = alf*ic[ii,i]
tmp2 = (1-alf)*ic[ii,i-1]
ic[ii,i] = tmp1 + tmp2 # ictrl[i][ii] = alf * ictrl[i][ii] + (1.0-alf) * ictrl[i-1][ii];
if j >= lbz : # if (j >= lbz) {
if j-tr <= kind-ph+oldr : # if (j-tr <= kind-ph+oldr) {
gam = (ub-ik[j-tr]) / den # gam = (ub-ik[j-tr]) / den;
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
tmp1 = gam*ebpts[ii,kj]
tmp2 = (1-gam)*ebpts[ii,kj+1]
ebpts[ii,kj] = tmp1 + tmp2 # ebpts[kj][ii] = gam*ebpts[kj][ii] + (1.0-gam)*ebpts[kj+1][ii];
else : # else {
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
tmp1 = bet*ebpts[ii,kj]
tmp2 = (1-bet)*ebpts[ii,kj+1]
ebpts[ii,kj] = tmp1 + tmp2 # ebpts[kj][ii] = bet*ebpts[kj][ii] + (1.0-bet)*ebpts[kj+1][ii];
i += 1 # i++;
j -= 1 # j--;
kj -= 1 # kj--;
#
first -= 1 # first--;
last += 1 # last++;
# // end of removing knot n=k[a]
#
# // load the knot ua
if a != d : # if (a != d)
for i in range(ph-oldr): #0:ph-oldr-1 # for (i = 0; i < ph-oldr; i++) {
ik[kind] = ua # ik[kind] = ua;
kind += 1 # kind++;
#
# // load ctrl pts into ic
for j in np.arange(lbz,rbz+1): #lbz:rbz # for (j = lbz; j <= rbz; j++) {
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
ic[ii,cind] = ebpts[ii,j] # ictrl[cind][ii] = ebpts[j][ii];
cind += 1 # cind++;
#
if b < m : # if (b < m) {
# // setup for next pass thru loop
for j in range(r): #0:r-1 # for (j = 0; j < r; j++)
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
bpts[ii,j] = Nextbpts[ii,j] # bpts[j][ii] = Nextbpts[j][ii];
for j in np.arange(r,d+1): #r:d # for (j = r; j <= d; j++)
for ii in range(mc): #0:mc-1 # for (ii = 0; ii < mc; ii++)
bpts[ii,j] = c[ii,b-d+j] # bpts[j][ii] = ctrl[b-d+j][ii];
a = b # a = b;
b += 1 # b++;
ua = ub # ua = ub;
# }
else: # else
# // end knot
for i in range(ph+1): #0:ph # for (i = 0; i <= ph; i++)
ik[kind+i] = ub # ik[kind+i] = ub;
# End big while loop # // end while loop
#
# *nh = mh - ph - 1;
#
# freevec2mat(ctrl);
# freevec2mat(ictrl);
# freematrix(bezalfs);
# freematrix(bpts);
# freematrix(ebpts);
# freematrix(Nextbpts);
# mxFree(alfs);
#
# return(ierr);
# }
# ajout dû au fait qu'on a initialisé trop grand (car difficile d'estimer la taille de ic et ik avant, dépend entre autres de la multiplicité des knots)
# on enleve les 0 à la fin du knot vector ik
ik = np.trim_zeros(ik,'b')
# on tronque la matrice des points de contrôle où il faut (revient à enlever les 0, mais si la courbe finit avec un point en (0,0), on n'enlève pas celui-là)
n = len(ik)-(d+t)-1
ic = ic[:,0:n]
return ic,ik
def derbasisfuns(i,pl,U,nders,u):
"""
i : knot span of u
p : degree
u : parameter on which we want to evaluate the function
nders: number of derivatives
U : knot vector
# i span de u
# pl = degrés de la nurbs
# u = endroit ou l'on veut la fonction
# nders = numéro de la dérivée désirée
# U = vecteur de noeud de la fonction """
# import pdb; pdb.set_trace()
u_knotl=U.copy()
left = np.zeros((pl+1))
right = np.zeros((pl+1))
ndu = np.zeros((pl+1,pl+1))
ders = np.zeros((nders+1,pl+1))
ndu[0,0] = 1
for j in range(pl): #1:pl
left[j+1] = u - u_knotl[i-j] ### rq Ali : i-j au lieu de i-j-1
right[j+1] = u_knotl[i+j+1] - u ### rq : i+j+1 au lieu de i+j
saved = 0
for r in range(j+1): #0:j-1