forked from orbingol/NURBS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNURBS.py
565 lines (437 loc) · 19.6 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
"""
.. module:: NURBS
:platform: Unix, Windows
:synopsis: Provides data storage and evaluation functionality for rational spline geometries
.. moduleauthor:: Onur Rauf Bingol <orbingol@gmail.com>
"""
from . import BSpline, compatibility, evaluators
from ._utilities import export
@export
class Curve(BSpline.Curve):
""" Data storage and evaluation class for n-variate NURBS (rational) curves.
The rational shapes have some minor differences between the non-rational ones. This class is designed to operate
with weighted control points (Pw) as described in *The NURBS Book* by Piegl and Tiller. Therefore, it provides
a different set of properties (i.e. getters and setters):
* ``ctrlptsw``: 1-dimensional array of weighted control points
* ``ctrlpts``: 1-dimensional array of control points
* ``weights``: 1-dimensional array of weights
You may also use ``set_ctrlpts()`` function which is designed to work with all types of control points.
This class provides the following properties:
* :py:attr:`order`
* :py:attr:`degree`
* :py:attr:`knotvector`
* :py:attr:`ctrlptsw`
* :py:attr:`ctrlpts`
* :py:attr:`weights`
* :py:attr:`delta`
* :py:attr:`sample_size`
* :py:attr:`bbox`
* :py:attr:`vis`
* :py:attr:`name`
* :py:attr:`dimension`
* :py:attr:`evaluator`
* :py:attr:`rational`
The following code segment illustrates the usage of Curve class:
.. code-block:: python
from geomdl import NURBS
# Create a 3-dimensional B-spline Curve
curve = NURBS.Curve()
# Set degree
curve.degree = 3
# Set control points (weights vector will be 1 by default)
# Use curve.ctrlptsw is if you are using homogeneous points as Pw
curve.ctrlpts = [[10, 5, 10], [10, 20, -30], [40, 10, 25], [-10, 5, 0]]
# Set knot vector
curve.knotvector = [0, 0, 0, 0, 1, 1, 1, 1]
# Set evaluation delta (controls the number of curve points)
curve.delta = 0.05
# Get curve points (the curve will be automatically evaluated)
curve_points = curve.evalpts
**Keyword Arguments:**
* ``precision``: number of decimal places to round to. *Default: 18*
* ``normalize_kv``: activates knot vector normalization. *Default: True*
* ``find_span_func``: sets knot span search implementation. *Default:* :func:`.helpers.find_span_linear`
* ``insert_knot_func``: sets knot insertion implementation. *Default:* :func:`.operations.insert_knot`
* ``remove_knot_func``: sets knot removal implementation. *Default:* :func:`.operations.remove_knot`
Please refer to the :py:class:`.abstract.Curve()` documentation for more details.
"""
def __init__(self, **kwargs):
super(Curve, self).__init__(**kwargs)
self._rational = True
self._evaluator = evaluators.CurveEvaluatorRational(find_span_func=self._span_func)
# Variables for caching
self.init_cache()
def __deepcopy__(self, memo):
# Call parent method
result = super(Curve, self).__deepcopy__(memo)
result.init_cache()
return result
def init_cache(self):
self._cache['ctrlpts'] = self._init_array()
self._cache['weights'] = self._init_array()
@property
def ctrlptsw(self):
""" Weighted control points (Pw).
Weighted control points are in (x*w, y*w, z*w, w) format; where x,y,z are the coordinates and w is the weight.
Please refer to the `wiki <https://github.com/orbingol/NURBS-Python/wiki/Using-Python-Properties>`_ for details
on using this class member.
:getter: Gets the weighted control points
:setter: Sets the weighted control points
"""
return self._control_points
@ctrlptsw.setter
def ctrlptsw(self, value):
self.set_ctrlpts(value)
@property
def ctrlpts(self):
""" Control points (P).
Please refer to the `wiki <https://github.com/orbingol/NURBS-Python/wiki/Using-Python-Properties>`_ for details
on using this class member.
:getter: Gets unweighted control points. Use :py:attr:`~weights` to get weights vector.
:setter: Sets unweighted control points
:type: list
"""
# Populate the cache, if necessary
if not self._cache['ctrlpts']:
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
self._cache['ctrlpts'] = [crd for crd in c]
self._cache['weights'] = w
return self._cache['ctrlpts']
@ctrlpts.setter
def ctrlpts(self, value):
# Check if we can retrieve the existing weights. If not, generate a weights vector of 1.0s.
if not self.weights:
weights = [1.0 for _ in range(len(value))]
else:
weights = self.weights
# Generate weighted control points using the new control points
ctrlptsw = compatibility.combine_ctrlpts_weights(value, weights)
# Set new weighted control points
self.set_ctrlpts(ctrlptsw)
@property
def weights(self):
""" Weights vector.
Please refer to the `wiki <https://github.com/orbingol/NURBS-Python/wiki/Using-Python-Properties>`_ for details
on using this class member.
:getter: Gets the weights vector
:setter: Sets the weights vector
:type: list
"""
# Populate the cache, if necessary
if not self._cache['weights']:
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
self._cache['ctrlpts'] = [crd for crd in c]
self._cache['weights'] = w
return self._cache['weights']
@weights.setter
def weights(self, value):
if not self.ctrlpts:
raise ValueError("Set control points first")
# Generate weighted control points using the new weights
ctrlptsw = compatibility.combine_ctrlpts_weights(self.ctrlpts, value)
# Set new weighted control points
self.set_ctrlpts(ctrlptsw)
def reset(self, **kwargs):
""" Resets control points and/or evaluated points.
Keyword Arguments:
* ``evalpts``: if True, then resets evaluated points
* ``ctrlpts`` if True, then resets control points
"""
reset_ctrlpts = kwargs.get('ctrlpts', False)
reset_evalpts = kwargs.get('evalpts', False)
# Call parent function
super(Curve, self).reset(ctrlpts=reset_ctrlpts, evalpts=reset_evalpts)
if reset_ctrlpts:
# Delete the caches
self._cache['ctrlpts'] = self._init_array()
self._cache['weights'][:] = self._init_array()
@export
class Surface(BSpline.Surface):
""" Data storage and evaluation class for NURBS (rational) surfaces.
The rational shapes have some minor differences between the non-rational ones. This class is designed to operate
with weighted control points (Pw) as described in *The NURBS Book* by Piegl and Tiller. Therefore, it provides
a different set of properties (i.e. getters and setters):
* ``ctrlptsw``: 1-dimensional array of weighted control points
* ``ctrlpts2d``: 2-dimensional array of weighted control points
* ``ctrlpts``: 1-dimensional array of control points
* ``weights``: 1-dimensional array of weights
You may also use ``set_ctrlpts()`` function which is designed to work with all types of control points.
This class provides the following properties:
* :py:attr:`order_u`
* :py:attr:`order_v`
* :py:attr:`degree_u`
* :py:attr:`degree_v`
* :py:attr:`knotvector_u`
* :py:attr:`knotvector_v`
* :py:attr:`ctrlptsw`
* :py:attr:`ctrlpts`
* :py:attr:`weights`
* :py:attr:`ctrlpts_size_u`
* :py:attr:`ctrlpts_size_v`
* :py:attr:`ctrlpts2d`
* :py:attr:`delta`
* :py:attr:`delta_u`
* :py:attr:`delta_v`
* :py:attr:`sample_size`
* :py:attr:`sample_size_u`
* :py:attr:`sample_size_v`
* :py:attr:`bbox`
* :py:attr:`name`
* :py:attr:`dimension`
* :py:attr:`vis`
* :py:attr:`evaluator`
* :py:attr:`tessellator`
* :py:attr:`rational`
* :py:attr:`trims`
The following code segment illustrates the usage of Surface class:
.. code-block:: python
:linenos:
from geomdl import NURBS
# Create a NURBS surface instance
surf = NURBS.Surface()
# Set degrees
surf.degree_u = 3
surf.degree_v = 2
# Set control points (weights vector will be 1 by default)
# Use curve.ctrlptsw is if you are using homogeneous points as Pw
control_points = [[0, 0, 0], [0, 4, 0], [0, 8, -3],
[2, 0, 6], [2, 4, 0], [2, 8, 0],
[4, 0, 0], [4, 4, 0], [4, 8, 3],
[6, 0, 0], [6, 4, -3], [6, 8, 0]]
surf.set_ctrlpts(control_points, 4, 3)
# Set knot vectors
surf.knotvector_u = [0, 0, 0, 0, 1, 1, 1, 1]
surf.knotvector_v = [0, 0, 0, 1, 1, 1]
# Set evaluation delta (control the number of surface points)
surf.delta = 0.05
# Get surface points (the surface will be automatically evaluated)
surface_points = surf.evalpts
**Keyword Arguments:**
* ``precision``: number of decimal places to round to. *Default: 18*
* ``normalize_kv``: activates knot vector normalization. *Default: True*
* ``find_span_func``: sets knot span search implementation. *Default:* :func:`.helpers.find_span_linear`
* ``insert_knot_func``: sets knot insertion implementation. *Default:* :func:`.operations.insert_knot`
* ``remove_knot_func``: sets knot removal implementation. *Default:* :func:`.operations.remove_knot`
Please refer to the :py:class:`.abstract.Surface()` documentation for more details.
"""
def __init__(self, **kwargs):
super(Surface, self).__init__(**kwargs)
self._rational = True
self._evaluator = evaluators.SurfaceEvaluatorRational(find_span_func=self._span_func)
# Variables for caching
self.init_cache()
def __deepcopy__(self, memo):
# Call parent method
result = super(Surface, self).__deepcopy__(memo)
result.init_cache()
return result
def init_cache(self):
self._cache['ctrlpts'] = self._init_array()
self._cache['weights'] = self._init_array()
@property
def ctrlptsw(self):
""" 1-dimensional array of weighted control points (Pw).
Weighted control points are in (x*w, y*w, z*w, w) format; where x,y,z are the coordinates and w is the weight.
This property sets and gets the control points in 1-D.
:getter: Gets weighted control points
:setter: Sets weighted control points
"""
return self._control_points
@ctrlptsw.setter
def ctrlptsw(self, value):
if self.ctrlpts_size_u <= 0 or self.ctrlpts_size_v <= 0:
raise ValueError("Please set the number of control points on the u- and v-directions")
self.set_ctrlpts(value, self.ctrlpts_size_u, self.ctrlpts_size_v)
@property
def ctrlpts(self):
""" 1-dimensional array of control points (P).
This property sets and gets the control points in 1-D.
:getter: Gets unweighted control points. Use :py:attr:`~weights` to get weights vector.
:setter: Sets unweighted control points.
:type: list
"""
if not self._cache['ctrlpts']:
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
self._cache['ctrlpts'] = [crd for crd in c]
self._cache['weights'] = w
return self._cache['ctrlpts']
@ctrlpts.setter
def ctrlpts(self, value):
if self.ctrlpts_size_u <= 0 or self.ctrlpts_size_v <= 0:
raise ValueError("Please set the number of control points on the u- and v-directions")
# Check if we can retrieve the existing weights. If not, generate a weights vector of 1.0s.
if not self.weights:
weights = [1.0 for _ in range(len(value))]
else:
weights = self.weights
# Generate weighted control points using the new control points
ctrlptsw = compatibility.combine_ctrlpts_weights(value, weights)
# Set weighted control points
self.set_ctrlpts(ctrlptsw, self.ctrlpts_size_u, self.ctrlpts_size_v)
@property
def weights(self):
""" Weights vector.
:getter: Gets the weights vector
:setter: Sets the weights vector
:type: list
"""
if not self._cache['weights']:
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
self._cache['ctrlpts'] = [crd for crd in c]
self._cache['weights'] = w
return self._cache['weights']
@weights.setter
def weights(self, value):
if not self.ctrlpts:
raise ValueError("Set control points first")
# Generate weighted control points using the new weights
ctrlptsw = compatibility.combine_ctrlpts_weights(self.ctrlpts, value)
# Set weighted control points
self.set_ctrlpts(ctrlptsw, self.ctrlpts_size_u, self.ctrlpts_size_v)
def reset(self, **kwargs):
""" Resets control points and/or evaluated points.
Keyword Arguments:
* ``evalpts``: if True, then resets evaluated points
* ``ctrlpts`` if True, then resets control points
"""
reset_ctrlpts = kwargs.get('ctrlpts', False)
reset_evalpts = kwargs.get('evalpts', False)
# Call parent function
super(Surface, self).reset(ctrlpts=reset_ctrlpts, evalpts=reset_evalpts)
if reset_ctrlpts:
# Re-initialize the caches
self.init_cache()
@export
class Volume(BSpline.Volume):
""" Data storage and evaluation class for NURBS (rational) volumes.
The rational shapes have some minor differences between the non-rational ones. This class is designed to operate
with weighted control points (Pw) as described in *The NURBS Book* by Piegl and Tiller. Therefore, it provides
a different set of properties (i.e. getters and setters):
* ``ctrlptsw``: 1-dimensional array of weighted control points
* ``ctrlpts``: 1-dimensional array of control points
* ``weights``: 1-dimensional array of weights
This class provides the following properties:
* :py:attr:`order_u`
* :py:attr:`order_v`
* :py:attr:`order_w`
* :py:attr:`degree_u`
* :py:attr:`degree_v`
* :py:attr:`degree_w`
* :py:attr:`knotvector_u`
* :py:attr:`knotvector_v`
* :py:attr:`knotvector_w`
* :py:attr:`ctrlptsw`
* :py:attr:`ctrlpts`
* :py:attr:`weights`
* :py:attr:`ctrlpts_size_u`
* :py:attr:`ctrlpts_size_v`
* :py:attr:`ctrlpts_size_w`
* :py:attr:`delta`
* :py:attr:`delta_u`
* :py:attr:`delta_v`
* :py:attr:`delta_w`
* :py:attr:`sample_size`
* :py:attr:`sample_size_u`
* :py:attr:`sample_size_v`
* :py:attr:`sample_size_w`
* :py:attr:`bbox`
* :py:attr:`name`
* :py:attr:`dimension`
* :py:attr:`vis`
* :py:attr:`evaluator`
* :py:attr:`rational`
**Keyword Arguments:**
* ``precision``: number of decimal places to round to. *Default: 18*
* ``normalize_kv``: activates knot vector normalization. *Default: True*
* ``find_span_func``: sets knot span search implementation. *Default:* :func:`.helpers.find_span_linear`
* ``insert_knot_func``: sets knot insertion implementation. *Default:* :func:`.operations.insert_knot`
* ``remove_knot_func``: sets knot removal implementation. *Default:* :func:`.operations.remove_knot`
Please refer to the :py:class:`.abstract.Volume()` documentation for more details.
"""
def __init__(self, **kwargs):
super(Volume, self).__init__(**kwargs)
self._rational = True
self._evaluator = evaluators.VolumeEvaluatorRational(find_span_func=self._span_func)
# Variables for caching
self.init_cache()
def __deepcopy__(self, memo):
# Call parent method
result = super(Volume, self).__deepcopy__(memo)
result.init_cache()
return result
def init_cache(self):
self._cache['ctrlpts'] = self._init_array()
self._cache['weights'] = self._init_array()
def reset(self, **kwargs):
""" Resets control points and/or evaluated points.
Keyword Arguments:
* ``evalpts``: if True, then resets the evaluated points
* ``ctrlpts`` if True, then resets the control points
"""
reset_ctrlpts = kwargs.get('ctrlpts', False)
reset_evalpts = kwargs.get('evalpts', False)
# Call parent function
super(Volume, self).reset(ctrlpts=reset_ctrlpts, evalpts=reset_evalpts)
if reset_ctrlpts:
# Re-initialize the caches
self.init_cache()
@property
def ctrlptsw(self):
""" 1-dimensional array of weighted control points (Pw).
Weighted control points are in (x*w, y*w, z*w, w) format; where x,y,z are the coordinates and w is the weight.
This property sets and gets the control points in 1-D.
:getter: Gets weighted control points
:setter: Sets weighted control points
"""
return self._control_points
@ctrlptsw.setter
def ctrlptsw(self, value):
if self.ctrlpts_size_u <= 0 or self.ctrlpts_size_v <= 0 or self.ctrlpts_size_w <= 0:
raise ValueError("Please set the number of control points for all u-, v- and w-directions")
self.set_ctrlpts(value, self.ctrlpts_size_u, self.ctrlpts_size_v, self.ctrlpts_size_w)
@property
def ctrlpts(self):
""" 1-dimensional array of control points (P).
This property sets and gets the control points in 1-D.
:getter: Gets unweighted control points. Use :py:attr:`~weights` to get weights vector.
:setter: Sets unweighted control points.
:type: list
"""
if not self._cache['ctrlpts']:
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
self._cache['ctrlpts'] = [crd for crd in c]
self._cache['weights'] = w
return self._cache['ctrlpts']
@ctrlpts.setter
def ctrlpts(self, value):
if self.ctrlpts_size_u <= 0 or self.ctrlpts_size_v <= 0 or self.ctrlpts_size_w <= 0:
raise ValueError("Please set the number of control points for all u-, v- and w-directions")
# Check if we can retrieve the existing weights. If not, generate a weights vector of 1.0s.
if not self.weights:
weights = [1.0 for _ in range(len(value))]
else:
weights = self.weights
# Generate weighted control points using the new control points
ctrlptsw = compatibility.combine_ctrlpts_weights(value, weights)
# Set weighted control points
self.set_ctrlpts(ctrlptsw, self.ctrlpts_size_u, self.ctrlpts_size_v, self.ctrlpts_size_w)
@property
def weights(self):
""" Weights vector.
:getter: Gets the weights vector
:setter: Sets the weights vector
:type: list
"""
if not self._cache['weights']:
c, w = compatibility.separate_ctrlpts_weights(self._control_points)
self._cache['ctrlpts'] = [crd for crd in c]
self._cache['weights'] = w
return self._cache['weights']
@weights.setter
def weights(self, value):
if not self.ctrlpts:
raise ValueError("Set control points first")
# Generate weighted control points using the new weights
ctrlptsw = compatibility.combine_ctrlpts_weights(self.ctrlpts, value)
# Set weighted control points
self.set_ctrlpts(ctrlptsw, self.ctrlpts_size_u, self.ctrlpts_size_v, self.ctrlpts_size_w)