forked from orbingol/NURBS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
70 lines (55 loc) · 2.15 KB
/
convert.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
"""
.. module:: convert
:platform: Unix, Windows
:synopsis: Helper module for converting rational and non-rational geometries to each other
.. moduleauthor:: Onur Rauf Bingol <orbingol@gmail.com>
"""
from . import BSpline, NURBS
from . import _convert as cvt
def bspline_to_nurbs(obj, **kwargs):
""" Converts non-rational splines to rational ones.
:param obj: non-rational spline geometry
:type obj: BSpline.Curve, BSpline.Surface or BSpline.Volume
:return: rational spline geometry
:rtype: NURBS.Curve, NURBS.Surface or NURBS.Volume
:raises: TypeError
"""
# B-Spline -> NURBS
if isinstance(obj, BSpline.Curve):
ret = cvt.convert_curve(obj, NURBS)
elif isinstance(obj, BSpline.Surface):
ret = cvt.convert_surface(obj, NURBS)
elif isinstance(obj, BSpline.Volume):
ret = cvt.convert_volume(obj, NURBS)
else:
raise TypeError("Input must be an instance of B-Spline curve, surface or volume")
return ret
def nurbs_to_bspline(obj, **kwargs):
""" Converts rational splines to non-rational ones (if possible).
The possibility of converting a rational spline geometry to
a non-rational one depends on the weights vector.
:param obj: rational spline geometry
:type obj: NURBS.Curve, NURBS.Surface or NURBS.Volume
:return: non-rational spline geometry
:rtype: BSpline.Curve, BSpline.Surface or BSpline.Volume
:raises: TypeError
"""
if not obj.rational:
raise TypeError("The input must be a rational geometry")
# Get keyword arguments
tol = kwargs.get('tol', 10e-8)
# Test for non-rational component extraction
for w in obj.weights:
if abs(w - 1.0) > tol:
print("Cannot extract non-rational components")
return obj
# NURBS -> B-Spline
if isinstance(obj, NURBS.Curve):
ret = cvt.convert_curve(obj, BSpline)
elif isinstance(obj, NURBS.Surface):
ret = cvt.convert_surface(obj, BSpline)
elif isinstance(obj, NURBS.Volume):
ret = cvt.convert_volume(obj, BSpline)
else:
raise TypeError("Input must be an instance of NURBS curve, surface or volume")
return ret