forked from orbingol/NURBS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
1800 lines (1422 loc) · 68.7 KB
/
operations.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
"""
.. module:: operations
:platform: Unix, Windows
:synopsis: Provides geometric operations for spline geometry classes
.. moduleauthor:: Onur Rauf Bingol <orbingol@gmail.com>
"""
import math
import copy
import warnings
from . import abstract, helpers, linalg, compatibility
from . import _operations as ops
from .exceptions import GeomdlException
from ._utilities import export
@export
def insert_knot(obj, param, num, **kwargs):
""" Inserts knots n-times to a spline geometry.
The following code snippet illustrates the usage of this function:
.. code-block:: python
# Insert knot u=0.5 to a curve 2 times
operations.insert_knot(curve, [0.5], [2])
# Insert knot v=0.25 to a surface 1 time
operations.insert_knot(surface, [None, 0.25], [0, 1])
# Insert knots u=0.75, v=0.25 to a surface 2 and 1 times, respectively
operations.insert_knot(surface, [0.75, 0.25], [2, 1])
# Insert knot w=0.5 to a volume 1 time
operations.insert_knot(volume, [None, None, 0.5], [0, 0, 1])
Please note that input spline geometry object will always be updated if the knot insertion operation is successful.
Keyword Arguments:
* ``check_num``: enables/disables operation validity checks. *Default: True*
:param obj: spline geometry
:type obj: abstract.SplineGeometry
:param param: knot(s) to be inserted in [u, v, w] format
:type param: list, tuple
:param num: number of knot insertions in [num_u, num_v, num_w] format
:type num: list, tuple
:return: updated spline geometry
"""
# Get keyword arguments
check_num = kwargs.get('check_num', True) # can be set to False when the caller checks number of insertions
if check_num:
# Check the validity of number of insertions
if not isinstance(num, (list, tuple)):
raise GeomdlException("The number of insertions must be a list or a tuple",
data=dict(num=num))
if len(num) != obj.pdimension:
raise GeomdlException("The length of the num array must be equal to the number of parametric dimensions",
data=dict(pdim=obj.pdimension, num_len=len(num)))
for idx, val in enumerate(num):
if val < 0:
raise GeomdlException('Number of insertions must be a positive integer value',
data=dict(idx=idx, num=val))
# Start curve knot insertion
if isinstance(obj, abstract.Curve):
if param[0] is not None and num[0] > 0:
# Find knot multiplicity
s = helpers.find_multiplicity(param[0], obj.knotvector)
# Check if it is possible add that many number of knots
if check_num and num[0] > obj.degree - s:
raise GeomdlException("Knot " + str(param[0]) + " cannot be inserted " + str(num[0]) + " times",
data=dict(knot=param[0], num=num[0], multiplicity=s))
# Find knot span
span = helpers.find_span_linear(obj.degree, obj.knotvector, obj.ctrlpts_size, param[0])
# Compute new knot vector
kv_new = helpers.knot_insertion_kv(obj.knotvector, param[0], span, num[0])
# Compute new control points
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
cpts_tmp = helpers.knot_insertion(obj.degree, obj.knotvector, cpts, param[0],
num=num[0], s=s, span=span)
# Update curve
obj.set_ctrlpts(cpts_tmp)
obj.knotvector = kv_new
# Start surface knot insertion
if isinstance(obj, abstract.Surface):
# u-direction
if param[0] is not None and num[0] > 0:
# Find knot multiplicity
s_u = helpers.find_multiplicity(param[0], obj.knotvector_u)
# Check if it is possible add that many number of knots
if check_num and num[0] > obj.degree_u - s_u:
raise GeomdlException("Knot " + str(param[0]) + " cannot be inserted " + str(num[0]) + " times (u-dir)",
data=dict(knot=param[0], num=num[0], multiplicity=s_u))
# Find knot span
span_u = helpers.find_span_linear(obj.degree_u, obj.knotvector_u, obj.ctrlpts_size_u, param[0])
# Compute new knot vector
kv_u = helpers.knot_insertion_kv(obj.knotvector_u, param[0], span_u, num[0])
# Get curves
cpts_tmp = []
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
for v in range(obj.ctrlpts_size_v):
ccu = [cpts[v + (obj.ctrlpts_size_v * u)] for u in range(obj.ctrlpts_size_u)]
ctrlpts_tmp = helpers.knot_insertion(obj.degree_u, obj.knotvector_u, ccu, param[0],
num=num[0], s=s_u, span=span_u)
cpts_tmp += ctrlpts_tmp
# Update the surface after knot insertion
obj.set_ctrlpts(compatibility.flip_ctrlpts_u(cpts_tmp, obj.ctrlpts_size_u + num[0], obj.ctrlpts_size_v),
obj.ctrlpts_size_u + num[0], obj.ctrlpts_size_v)
obj.knotvector_u = kv_u
# v-direction
if param[1] is not None and num[1] > 0:
# Find knot multiplicity
s_v = helpers.find_multiplicity(param[1], obj.knotvector_v)
# Check if it is possible add that many number of knots
if check_num and num[1] > obj.degree_v - s_v:
raise GeomdlException("Knot " + str(param[1]) + " cannot be inserted " + str(num[1]) + " times (v-dir)",
data=dict(knot=param[1], num=num[1], multiplicity=s_v))
# Find knot span
span_v = helpers.find_span_linear(obj.degree_v, obj.knotvector_v, obj.ctrlpts_size_v, param[1])
# Compute new knot vector
kv_v = helpers.knot_insertion_kv(obj.knotvector_v, param[1], span_v, num[1])
# Get curves
cpts_tmp = []
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
for u in range(obj.ctrlpts_size_u):
ccv = [cpts[v + (obj.ctrlpts_size_v * u)] for v in range(obj.ctrlpts_size_v)]
ctrlpts_tmp = helpers.knot_insertion(obj.degree_v, obj.knotvector_v, ccv, param[1],
num=num[1], s=s_v, span=span_v)
cpts_tmp += ctrlpts_tmp
# Update the surface after knot insertion
obj.set_ctrlpts(cpts_tmp, obj.ctrlpts_size_u, obj.ctrlpts_size_v + num[1])
obj.knotvector_v = kv_v
# Start volume knot insertion
if isinstance(obj, abstract.Volume):
# u-direction
if param[0] is not None and num[0] > 0:
# Find knot multiplicity
s_u = helpers.find_multiplicity(param[0], obj.knotvector_u)
# Check if it is possible add that many number of knots
if check_num and num[0] > obj.degree_u - s_u:
raise GeomdlException("Knot " + str(param[0]) + " cannot be inserted " + str(num[0]) + " times (u-dir)",
data=dict(knot=param[0], num=num[0], multiplicity=s_u))
# Find knot span
span_u = helpers.find_span_linear(obj.degree_u, obj.knotvector_u, obj.ctrlpts_size_u, param[0])
# Compute new knot vector
kv_u = helpers.knot_insertion_kv(obj.knotvector_u, param[0], span_u, num[0])
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for u in range(obj.ctrlpts_size_u):
temp_surf = []
for w in range(obj.ctrlpts_size_w):
for v in range(obj.ctrlpts_size_v):
temp_pt = cpts[v + (u * obj.ctrlpts_size_v) + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
temp_surf.append(temp_pt)
cpt2d.append(temp_surf)
# Compute new control points
ctrlpts_tmp = helpers.knot_insertion(obj.degree_u, obj.knotvector_u, cpt2d, param[0],
num=num[0], s=s_u, span=span_u)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u + num[0]):
for v in range(obj.ctrlpts_size_v):
temp_pt = ctrlpts_tmp[u][v + (w * obj.ctrlpts_size_v)]
ctrlpts_new.append(temp_pt)
# Update the volume after knot insertion
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u + num[0], obj.ctrlpts_size_v, obj.ctrlpts_size_w)
obj.knotvector_u = kv_u
# v-direction
if param[1] is not None and num[1] > 0:
# Find knot multiplicity
s_v = helpers.find_multiplicity(param[1], obj.knotvector_v)
# Check if it is possible add that many number of knots
if check_num and num[1] > obj.degree_v - s_v:
raise GeomdlException("Knot " + str(param[1]) + " cannot be inserted " + str(num[1]) + " times (v-dir)",
data=dict(knot=param[1], num=num[1], multiplicity=s_v))
# Find knot span
span_v = helpers.find_span_linear(obj.degree_v, obj.knotvector_v, obj.ctrlpts_size_v, param[1])
# Compute new knot vector
kv_v = helpers.knot_insertion_kv(obj.knotvector_v, param[1], span_v, num[1])
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for v in range(obj.ctrlpts_size_v):
temp_surf = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u):
temp_pt = cpts[v + (u * obj.ctrlpts_size_v) + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
temp_surf.append(temp_pt)
cpt2d.append(temp_surf)
# Compute new control points
ctrlpts_tmp = helpers.knot_insertion(obj.degree_v, obj.knotvector_v, cpt2d, param[1],
num=num[1], s=s_v, span=span_v)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u):
for v in range(obj.ctrlpts_size_v + num[1]):
temp_pt = ctrlpts_tmp[v][u + (w * obj.ctrlpts_size_u)]
ctrlpts_new.append(temp_pt)
# Update the volume after knot insertion
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, obj.ctrlpts_size_v + num[1], obj.ctrlpts_size_w)
obj.knotvector_v = kv_v
# w-direction
if param[2] is not None and num[2] > 0:
# Find knot multiplicity
s_w = helpers.find_multiplicity(param[2], obj.knotvector_w)
# Check if it is possible add that many number of knots
if check_num and num[2] > obj.degree_w - s_w:
raise GeomdlException("Knot " + str(param[2]) + " cannot be inserted " + str(num[2]) + " times (w-dir)",
data=dict(knot=param[2], num=num[2], multiplicity=s_w))
# Find knot span
span_w = helpers.find_span_linear(obj.degree_w, obj.knotvector_w, obj.ctrlpts_size_w, param[2])
# Compute new knot vector
kv_w = helpers.knot_insertion_kv(obj.knotvector_w, param[2], span_w, num[2])
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for w in range(obj.ctrlpts_size_w):
temp_surf = [cpts[uv + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)] for uv in
range(obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
cpt2d.append(temp_surf)
# Compute new control points
ctrlpts_tmp = helpers.knot_insertion(obj.degree_w, obj.knotvector_w, cpt2d, param[2],
num=num[2], s=s_w, span=span_w)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w + num[2]):
ctrlpts_new += ctrlpts_tmp[w]
# Update the volume after knot insertion
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, obj.ctrlpts_size_v, obj.ctrlpts_size_w + num[2])
obj.knotvector_w = kv_w
# Return updated spline geometry
return obj
@export
def remove_knot(obj, param, num, **kwargs):
""" Removes knots n-times from a spline geometry.
The following code snippet illustrates the usage of this function:
.. code-block:: python
# Remove knot u=0.5 from a curve 2 times
operations.remove_knot(curve, [0.5], [2])
# Remove knot v=0.25 from a surface 1 time
operations.remove_knot(surface, [None, 0.25], [0, 1])
# Remove knots u=0.75, v=0.25 from a surface 2 and 1 times, respectively
operations.remove_knot(surface, [0.75, 0.25], [2, 1])
# Remove knot w=0.5 from a volume 1 time
operations.remove_knot(volume, [None, None, 0.5], [0, 0, 1])
Please note that input spline geometry object will always be updated if the knot removal operation is successful.
Keyword Arguments:
* ``check_num``: enables/disables operation validity checks. *Default: True*
:param obj: spline geometry
:type obj: abstract.SplineGeometry
:param param: knot(s) to be removed in [u, v, w] format
:type param: list, tuple
:param num: number of knot removals in [num_u, num_v, num_w] format
:type num: list, tuple
:return: updated spline geometry
"""
# Get keyword arguments
check_num = kwargs.get('check_num', True) # can be set to False when the caller checks number of removals
if check_num:
# Check the validity of number of insertions
if not isinstance(num, (list, tuple)):
raise GeomdlException("The number of removals must be a list or a tuple",
data=dict(num=num))
if len(num) != obj.pdimension:
raise GeomdlException("The length of the num array must be equal to the number of parametric dimensions",
data=dict(pdim=obj.pdimension, num_len=len(num)))
for idx, val in enumerate(num):
if val < 0:
raise GeomdlException('Number of removals must be a positive integer value',
data=dict(idx=idx, num=val))
# Start curve knot removal
if isinstance(obj, abstract.Curve):
if param[0] is not None and num[0] > 0:
# Find knot multiplicity
s = helpers.find_multiplicity(param[0], obj.knotvector)
# It is impossible to remove knots if num > s
if check_num and num[0] > s:
raise GeomdlException("Knot " + str(param[0]) + " cannot be removed " + str(num[0]) + " times",
data=dict(knot=param[0], num=num[0], multiplicity=s))
# Find knot span
span = helpers.find_span_linear(obj.degree, obj.knotvector, obj.ctrlpts_size, param[0])
# Compute new control points
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
ctrlpts_new = helpers.knot_removal(obj.degree, obj.knotvector, cpts, param[0], num=num[0], s=s, span=span)
# Compute new knot vector
kv_new = helpers.knot_removal_kv(obj.knotvector, span, num[0])
# Update curve
obj.set_ctrlpts(ctrlpts_new)
obj.knotvector = kv_new
# Start surface knot removal
if isinstance(obj, abstract.Surface):
# u-direction
if param[0] is not None and num[0] > 0:
# Find knot multiplicity
s_u = helpers.find_multiplicity(param[0], obj.knotvector_u)
# Check if it is possible add that many number of knots
if check_num and num[0] > s_u:
raise GeomdlException("Knot " + str(param[0]) + " cannot be removed " + str(num[0]) + " times (u-dir)",
data=dict(knot=param[0], num=num[0], multiplicity=s_u))
# Find knot span
span_u = helpers.find_span_linear(obj.degree_u, obj.knotvector_u, obj.ctrlpts_size_u, param[0])
# Get curves
ctrlpts_new = []
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
for v in range(obj.ctrlpts_size_v):
ccu = [cpts[v + (obj.ctrlpts_size_v * u)] for u in range(obj.ctrlpts_size_u)]
ctrlpts_tmp = helpers.knot_removal(obj.degree_u, obj.knotvector_u, ccu, param[0],
num=num[0], s=s_u, span=span_u)
ctrlpts_new += ctrlpts_tmp
# Compute new knot vector
kv_u = helpers.knot_removal_kv(obj.knotvector_u, span_u, num[0])
# Update the surface after knot removal
obj.set_ctrlpts(compatibility.flip_ctrlpts_u(ctrlpts_new, obj.ctrlpts_size_u - num[0], obj.ctrlpts_size_v),
obj.ctrlpts_size_u - num[0], obj.ctrlpts_size_v)
obj.knotvector_u = kv_u
# v-direction
if param[1] is not None and num[1] > 0:
# Find knot multiplicity
s_v = helpers.find_multiplicity(param[1], obj.knotvector_v)
# Check if it is possible add that many number of knots
if check_num and num[1] > s_v:
raise GeomdlException("Knot " + str(param[1]) + " cannot be removed " + str(num[1]) + " times (v-dir)",
data=dict(knot=param[1], num=num[1], multiplicity=s_v))
# Find knot span
span_v = helpers.find_span_linear(obj.degree_v, obj.knotvector_v, obj.ctrlpts_size_v, param[1])
# Get curves
ctrlpts_new = []
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
for u in range(obj.ctrlpts_size_u):
ccv = [cpts[v + (obj.ctrlpts_size_v * u)] for v in range(obj.ctrlpts_size_v)]
ctrlpts_tmp = helpers.knot_removal(obj.degree_v, obj.knotvector_v, ccv, param[1],
num=num[1], s=s_v, span=span_v)
ctrlpts_new += ctrlpts_tmp
# Compute new knot vector
kv_v = helpers.knot_removal_kv(obj.knotvector_v, span_v, num[1])
# Update the surface after knot removal
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, obj.ctrlpts_size_v - num[1])
obj.knotvector_v = kv_v
# Start volume knot removal
if isinstance(obj, abstract.Volume):
# u-direction
if param[0] is not None and num[0] > 0:
# Find knot multiplicity
s_u = helpers.find_multiplicity(param[0], obj.knotvector_u)
# Check if it is possible add that many number of knots
if check_num and num[0] > s_u:
raise GeomdlException("Knot " + str(param[0]) + " cannot be removed " + str(num[0]) + " times (u-dir)",
data=dict(knot=param[0], num=num[0], multiplicity=s_u))
# Find knot span
span_u = helpers.find_span_linear(obj.degree_u, obj.knotvector_u, obj.ctrlpts_size_u, param[0])
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for u in range(obj.ctrlpts_size_u):
temp_surf = []
for w in range(obj.ctrlpts_size_w):
for v in range(obj.ctrlpts_size_v):
temp_pt = cpts[v + (u * obj.ctrlpts_size_v) + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
temp_surf.append(temp_pt)
cpt2d.append(temp_surf)
# Compute new control points
ctrlpts_tmp = helpers.knot_removal(obj.degree_u, obj.knotvector_u, cpt2d, param[0],
num=num[0], s=s_u, span=span_u)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u - num[0]):
for v in range(obj.ctrlpts_size_v):
temp_pt = ctrlpts_tmp[u][v + (w * obj.ctrlpts_size_v)]
ctrlpts_new.append(temp_pt)
# Compute new knot vector
kv_u = helpers.knot_removal_kv(obj.knotvector_u, span_u, num[0])
# Update the volume after knot removal
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u - num[0], obj.ctrlpts_size_v, obj.ctrlpts_size_w)
obj.knotvector_u = kv_u
# v-direction
if param[1] is not None and num[1] > 0:
# Find knot multiplicity
s_v = helpers.find_multiplicity(param[1], obj.knotvector_v)
# Check if it is possible add that many number of knots
if check_num and num[1] > s_v:
raise GeomdlException("Knot " + str(param[1]) + " cannot be removed " + str(num[1]) + " times (v-dir)",
data=dict(knot=param[1], num=num[1], multiplicity=s_v))
# Find knot span
span_v = helpers.find_span_linear(obj.degree_v, obj.knotvector_v, obj.ctrlpts_size_v, param[1])
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for v in range(obj.ctrlpts_size_v):
temp_surf = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u):
temp_pt = cpts[v + (u * obj.ctrlpts_size_v) + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
temp_surf.append(temp_pt)
cpt2d.append(temp_surf)
# Compute new control points
ctrlpts_tmp = helpers.knot_removal(obj.degree_v, obj.knotvector_v, cpt2d, param[1],
num=num[1], s=s_v, span=span_v)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u):
for v in range(obj.ctrlpts_size_v - num[1]):
temp_pt = ctrlpts_tmp[v][u + (w * obj.ctrlpts_size_u)]
ctrlpts_new.append(temp_pt)
# Compute new knot vector
kv_v = helpers.knot_removal_kv(obj.knotvector_v, span_v, num[1])
# Update the volume after knot removal
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, obj.ctrlpts_size_v - num[1], obj.ctrlpts_size_w)
obj.knotvector_v = kv_v
# w-direction
if param[2] is not None and num[2] > 0:
# Find knot multiplicity
s_w = helpers.find_multiplicity(param[2], obj.knotvector_w)
# Check if it is possible add that many number of knots
if check_num and num[2] > s_w:
raise GeomdlException("Knot " + str(param[2]) + " cannot be removed " + str(num[2]) + " times (w-dir)",
data=dict(knot=param[2], num=num[2], multiplicity=s_w))
# Find knot span
span_w = helpers.find_span_linear(obj.degree_w, obj.knotvector_w, obj.ctrlpts_size_w, param[2])
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for w in range(obj.ctrlpts_size_w):
temp_surf = [cpts[uv + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)] for uv in
range(obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
cpt2d.append(temp_surf)
# Compute new control points
ctrlpts_tmp = helpers.knot_removal(obj.degree_w, obj.knotvector_w, cpt2d, param[2],
num=num[2], s=s_w, span=span_w)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w - num[2]):
ctrlpts_new += ctrlpts_tmp[w]
# Compute new knot vector
kv_w = helpers.knot_removal_kv(obj.knotvector_w, span_w, num[2])
# Update the volume after knot removal
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, obj.ctrlpts_size_v, obj.ctrlpts_size_w - num[2])
obj.knotvector_w = kv_w
# Return updated spline geometry
return obj
@export
def refine_knotvector(obj, param, **kwargs):
""" Refines the knot vector(s) of a spline geometry.
The following code snippet illustrates the usage of this function:
.. code-block:: python
# Refines the knot vector of a curve
operations.refine_knotvector(curve, [1])
# Refines the knot vector on the v-direction of a surface
operations.refine_knotvector(surface, [0, 1])
# Refines the both knot vectors of a surface
operations.refine_knotvector(surface, [1, 1])
# Refines the knot vector on the w-direction of a volume
operations.refine_knotvector(volume, [0, 0, 1])
The values of ``param`` argument can be used to set the *knot refinement density*. If *density* is bigger than 1,
then the algorithm finds the middle knots in each internal knot span to increase the number of knots to be refined.
**Example**: Let the degree is 2 and the knot vector to be refined is ``[0, 2, 4]`` with the superfluous knots
from the start and end are removed. Knot vectors with the changing ``density (d)`` value will be:
* ``d = 1``, knot vector ``[0, 1, 1, 2, 2, 3, 3, 4]``
* ``d = 2``, knot vector ``[0, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 2, 2.5, 2.5, 3, 3, 3.5, 3.5, 4]``
The following code snippet illustrates the usage of knot refinement densities:
.. code-block:: python
# Refines the knot vector of a curve with density = 3
operations.refine_knotvector(curve, [3])
# Refines the knot vectors of a surface with density for
# u-dir = 2 and v-dir = 3
operations.refine_knotvector(surface, [2, 3])
# Refines only the knot vector on the v-direction of a surface with density = 1
operations.refine_knotvector(surface, [0, 1])
# Refines the knot vectors of a volume with density for
# u-dir = 1, v-dir = 3 and w-dir = 2
operations.refine_knotvector(volume, [1, 3, 2])
Please refer to :func:`.helpers.knot_refinement` function for more usage options.
Keyword Arguments:
* ``check_num``: enables/disables operation validity checks. *Default: True*
:param obj: spline geometry
:type obj: abstract.SplineGeometry
:param param: parametric dimensions to be refined in [u, v, w] format
:type param: list, tuple
:return: updated spline geometry
"""
# Get keyword arguments
check_num = kwargs.get('check_num', True) # enables/disables input validity checks
if check_num:
if not isinstance(param, (list, tuple)):
raise GeomdlException("Parametric dimensions argument (param) must be a list or a tuple")
if len(param) != obj.pdimension:
raise GeomdlException("The length of the param array must be equal to the number of parametric dimensions",
data=dict(pdim=obj.pdimension, param_len=len(param)))
# Start curve knot refinement
if isinstance(obj, abstract.Curve):
if param[0] > 0:
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
new_cpts, new_kv = helpers.knot_refinement(obj.degree, obj.knotvector, cpts, density=param[0])
obj.set_ctrlpts(new_cpts)
obj.knotvector = new_kv
# Start surface knot refinement
if isinstance(obj, abstract.Surface):
# u-direction
if param[0] > 0:
# Get curves
new_cpts = []
new_cpts_size = 0
new_kv = []
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
for v in range(obj.ctrlpts_size_v):
ccu = [cpts[v + (obj.ctrlpts_size_v * u)] for u in range(obj.ctrlpts_size_u)]
ptmp, new_kv = helpers.knot_refinement(obj.degree_u, obj.knotvector_u, ccu, density=param[0])
new_cpts_size = len(ptmp)
new_cpts += ptmp
# Update the surface after knot refinement
obj.set_ctrlpts(compatibility.flip_ctrlpts_u(new_cpts, new_cpts_size, obj.ctrlpts_size_v),
new_cpts_size, obj.ctrlpts_size_v)
obj.knotvector_u = new_kv
# v-direction
if param[1] > 0:
# Get curves
new_cpts = []
new_cpts_size = 0
new_kv = []
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
for u in range(obj.ctrlpts_size_u):
ccv = [cpts[v + (obj.ctrlpts_size_v * u)] for v in range(obj.ctrlpts_size_v)]
ptmp, new_kv = helpers.knot_refinement(obj.degree_v, obj.knotvector_v, ccv, density=param[1])
new_cpts_size = len(ptmp)
new_cpts += ptmp
# Update the surface after knot refinement
obj.set_ctrlpts(new_cpts, obj.ctrlpts_size_u, new_cpts_size)
obj.knotvector_v = new_kv
# Start volume knot refinement
if isinstance(obj, abstract.Volume):
# u-direction
if param[0] > 0:
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for u in range(obj.ctrlpts_size_u):
temp_surf = []
for w in range(obj.ctrlpts_size_w):
for v in range(obj.ctrlpts_size_v):
temp_pt = cpts[v + (u * obj.ctrlpts_size_v) + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
temp_surf.append(temp_pt)
cpt2d.append(temp_surf)
# Apply knot refinement
ctrlpts_tmp, kv_new = helpers.knot_refinement(obj.degree_u, obj.knotvector_u, cpt2d, density=param[0])
new_cpts_size = len(ctrlpts_tmp)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w):
for u in range(new_cpts_size):
for v in range(obj.ctrlpts_size_v):
temp_pt = ctrlpts_tmp[u][v + (w * obj.ctrlpts_size_v)]
ctrlpts_new.append(temp_pt)
# Update the volume after knot removal
obj.set_ctrlpts(ctrlpts_new, new_cpts_size, obj.ctrlpts_size_v, obj.ctrlpts_size_w)
obj.knotvector_u = kv_new
# v-direction
if param[1] > 0:
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for v in range(obj.ctrlpts_size_v):
temp_surf = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u):
temp_pt = cpts[v + (u * obj.ctrlpts_size_v) + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
temp_surf.append(temp_pt)
cpt2d.append(temp_surf)
# Apply knot refinement
ctrlpts_tmp, kv_new = helpers.knot_refinement(obj.degree_v, obj.knotvector_v, cpt2d, density=param[1])
new_cpts_size = len(ctrlpts_tmp)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(obj.ctrlpts_size_w):
for u in range(obj.ctrlpts_size_u):
for v in range(new_cpts_size):
temp_pt = ctrlpts_tmp[v][u + (w * obj.ctrlpts_size_u)]
ctrlpts_new.append(temp_pt)
# Update the volume after knot removal
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, new_cpts_size, obj.ctrlpts_size_w)
obj.knotvector_v = kv_new
# w-direction
if param[2] > 0:
# Use Pw if rational
cpts = obj.ctrlptsw if obj.rational else obj.ctrlpts
# Construct 2-dimensional structure
cpt2d = []
for w in range(obj.ctrlpts_size_w):
temp_surf = [cpts[uv + (w * obj.ctrlpts_size_u * obj.ctrlpts_size_v)] for uv in
range(obj.ctrlpts_size_u * obj.ctrlpts_size_v)]
cpt2d.append(temp_surf)
# Apply knot refinement
ctrlpts_tmp, kv_new = helpers.knot_refinement(obj.degree_w, obj.knotvector_w, cpt2d, density=param[2])
new_cpts_size = len(ctrlpts_tmp)
# Flatten to 1-dimensional structure
ctrlpts_new = []
for w in range(new_cpts_size):
ctrlpts_new += ctrlpts_tmp[w]
# Update the volume after knot removal
obj.set_ctrlpts(ctrlpts_new, obj.ctrlpts_size_u, obj.ctrlpts_size_v, new_cpts_size)
obj.knotvector_w = kv_new
# Return updated spline geometry
return obj
def degree_operations(obj, param, **kwargs):
""" Applies degree elevation and degree reduction algorithms to spline geometries.
:param obj: spline geometry
:type obj: abstract.SplineGeometry
:param param: operation definition
:type param: list, tuple
:return: updated spline geometry
"""
def validate_reduction(degree):
if degree < 2:
raise GeomdlException("Input spline geometry must have degree > 1")
# Start curve degree manipulation operations
if isinstance(obj, abstract.Curve):
if param[0] is not None and param[0] != 0:
# Find multiplicity of the internal knots
int_knots = set(obj.knotvector[obj.degree + 1:-(obj.degree + 1)])
mult_arr = []
for ik in int_knots:
s = helpers.find_multiplicity(ik, obj.knotvector)
mult_arr.append(s)
# Decompose the input by knot insertion
crv_list = decompose_curve(obj, **kwargs)
# If parameter is positive, apply degree elevation. Otherwise, apply degree reduction
if param[0] > 0:
# Loop through to apply degree elevation
for crv in crv_list:
cpts = crv.ctrlptsw if crv.rational else crv.ctrlpts
new_cpts = helpers.degree_elevation(crv.degree, cpts, num=param[0])
crv.degree += param[0]
crv.set_ctrlpts(new_cpts)
crv.knotvector = [crv.knotvector[0] for _ in range(param[0])] + list(crv.knotvector) + [crv.knotvector[-1] for _ in range(param[0])]
# Compute new degree
nd = obj.degree + param[0]
# Number of knot removals
num = obj.degree + 1
else:
# Validate degree reduction operation
validate_reduction(obj.degree)
# Loop through to apply degree reduction
for crv in crv_list:
cpts = crv.ctrlptsw if crv.rational else crv.ctrlpts
new_cpts = helpers.degree_reduction(crv.degree, cpts)
crv.degree -= 1
crv.set_ctrlpts(new_cpts)
crv.knotvector = list(crv.knotvector[1:-1])
# Compute new degree
nd = obj.degree - 1
# Number of knot removals
num = obj.degree - 1
# Link curves together (reverse of decomposition)
kv, cpts, ws, knots = ops.link_curves(*crv_list, validate=False)
# Organize control points (if necessary)
ctrlpts = compatibility.combine_ctrlpts_weights(cpts, ws) if obj.rational else cpts
# Apply knot removal
for k, s in zip(knots, mult_arr):
span = helpers.find_span_linear(nd, kv, len(ctrlpts), k)
ctrlpts = helpers.knot_removal(nd, kv, ctrlpts, k, num=num-s)
kv = helpers.knot_removal_kv(kv, span, num-s)
# Update input curve
obj.degree = nd
obj.set_ctrlpts(ctrlpts)
obj.knotvector = kv
# Start surface degree manipulation operations
if isinstance(obj, abstract.Surface):
# u-direction
if param[0] is not None and param[0] != 0:
# If parameter is positive, apply degree elevation. Else, apply degree reduction
if param[0] > 0:
pass
else:
# Apply degree reduction operation
validate_reduction(obj.degree_u)
# v-direction
if param[1] is not None and param[1] != 0:
# If parameter is positive, apply degree elevation. Otherwise, apply degree reduction
if param[1] > 0:
pass
else:
# Validate degree reduction operation
validate_reduction(obj.degree_v)
# Start surface degree manipulation operations
if isinstance(obj, abstract.Volume):
raise GeomdlException("Degree manipulation operations are not available for spline volumes")
# Return updated spline geometry
return obj
@export
def add_dimension(obj, **kwargs):
""" Elevates the spatial dimension of the spline geometry.
If you pass ``inplace=True`` keyword argument, the input will be updated. Otherwise, this function does not
change the input but returns a new instance with the updated data.
:param obj: spline geometry
:type obj: abstract.SplineGeometry
:return: updated spline geometry
:rtype: abstract.SplineGeometry
"""
if not isinstance(obj, abstract.SplineGeometry):
raise GeomdlException("Can only operate on spline geometry objects")
# Keyword arguments
inplace = kwargs.get('inplace', False)
array_init = kwargs.get('array_init', [[] for _ in range(len(obj.ctrlpts))])
offset_value = kwargs.get('offset', 0.0)
# Update control points
new_ctrlpts = array_init
for idx, point in enumerate(obj.ctrlpts):
temp = [float(p) for p in point[0:obj.dimension]]
temp.append(offset_value)
new_ctrlpts[idx] = temp
if inplace:
obj.ctrlpts = new_ctrlpts
return obj
else:
ret = copy.deepcopy(obj)
ret.ctrlpts = new_ctrlpts
return ret
@export
def split_curve(obj, param, **kwargs):
""" Splits the curve at the input parametric coordinate.
This method splits the curve into two pieces at the given parametric coordinate, generates two different
curve objects and returns them. It does not modify the input curve.
Keyword Arguments:
* ``find_span_func``: FindSpan implementation. *Default:* :func:`.helpers.find_span_linear`
* ``insert_knot_func``: knot insertion algorithm implementation. *Default:* :func:`.operations.insert_knot`
:param obj: Curve to be split
:type obj: abstract.Curve
:param param: parameter
:type param: float
:return: a list of curve segments
:rtype: list
"""
# Validate input
if not isinstance(obj, abstract.Curve):
raise GeomdlException("Input shape must be an instance of abstract.Curve class")
if param == obj.domain[0] or param == obj.domain[1]:
raise GeomdlException("Cannot split from the domain edge")
# Keyword arguments
span_func = kwargs.get('find_span_func', helpers.find_span_linear) # FindSpan implementation
insert_knot_func = kwargs.get('insert_knot_func', insert_knot) # Knot insertion algorithm
# Find multiplicity of the knot and define how many times we need to add the knot
ks = span_func(obj.degree, obj.knotvector, len(obj.ctrlpts), param) - obj.degree + 1
s = helpers.find_multiplicity(param, obj.knotvector)
r = obj.degree - s
# Create backups of the original curve
temp_obj = copy.deepcopy(obj)
# Insert knot
insert_knot_func(temp_obj, [param], num=[r], check_num=False)
# Knot vectors
knot_span = span_func(temp_obj.degree, temp_obj.knotvector, len(temp_obj.ctrlpts), param) + 1
curve1_kv = list(temp_obj.knotvector[0:knot_span])
curve1_kv.append(param)
curve2_kv = list(temp_obj.knotvector[knot_span:])
for _ in range(0, temp_obj.degree + 1):
curve2_kv.insert(0, param)
# Control points (use Pw if rational)
cpts = temp_obj.ctrlptsw if obj.rational else temp_obj.ctrlpts
curve1_ctrlpts = cpts[0:ks + r]
curve2_ctrlpts = cpts[ks + r - 1:]
# Create a new curve for the first half
curve1 = temp_obj.__class__()
curve1.degree = temp_obj.degree
curve1.set_ctrlpts(curve1_ctrlpts)
curve1.knotvector = curve1_kv
# Create another curve fot the second half
curve2 = temp_obj.__class__()
curve2.degree = temp_obj.degree
curve2.set_ctrlpts(curve2_ctrlpts)
curve2.knotvector = curve2_kv
# Return the split curves
ret_val = [curve1, curve2]
return ret_val
@export
def decompose_curve(obj, **kwargs):
""" Decomposes the curve into Bezier curve segments of the same degree.
This operation does not modify the input curve, instead it returns the split curve segments.
Keyword Arguments:
* ``find_span_func``: FindSpan implementation. *Default:* :func:`.helpers.find_span_linear`
* ``insert_knot_func``: knot insertion algorithm implementation. *Default:* :func:`.operations.insert_knot`
:param obj: Curve to be decomposed
:type obj: abstract.Curve
:return: a list of Bezier segments
:rtype: list
"""
if not isinstance(obj, abstract.Curve):
raise GeomdlException("Input shape must be an instance of abstract.Curve class")