Basics
In order to generate a spline shape with NURBS-Python, you need 3 components:
degree
knot vector
control points
The number of components depend on the parametric dimensionality of the shape regardless of the spatial dimensionality.
curve is parametrically 1-dimensional (or 1-manifold)
surface is parametrically 2-dimensional (or 2-manifold)
volume is parametrically 3-dimensional (or 3-manifold)
Parametric dimensions are defined by u
, v
, w
and spatial dimensions are defined by x
, y
, z
.
Working with the curves
In this section, we will cover the basics of spline curve generation using NURBS-Python. The following code snippet is an example to a 3-dimensional curve.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
As described in the introduction text, we set the 3 required components to generate a 3-dimensional spline curve.
Evaluating the curve points
The code snippet is updated to retrieve evaluated curve points.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
14
15# Get curve points
16points = crv.evalpts
17
18# Do something with the evaluated points
19for pt in points:
20 print(pt)
evalpts
property will automatically call evaluate()
function.
Getting the curve point at a specific parameter
evaluate_single
method will return the point evaluated as the specified parameter.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
14
15# Get curve point at u = 0.5
16point = crv.evaluate_single(0.5)
Setting the evaluation delta
Evaluation delta is used to change the number of evaluated points. Increasing the number of points will result in a
bigger evaluated points array, as described with evalpts
property and decreasing will reduce the size of the
evalpts
array. Therefore, evaluation delta can also be used to change smoothness of the plots generated using
the visualization modules.
delta
property will set the evaluation delta. It is also possible to use sample_size
property to set the number
of evaluated points.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
14
15# Set evaluation delta
16crv.delta = 0.005
17
18# Get evaluated points
19points_a = crv.evalpts
20
21# Update delta
22crv.delta = 0.1
23
24# The curve will be automatically re-evaluated
25points_b = crv.evalpts
Inserting a knot
insert_knot
method is recommended for this purpose.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
14
15# Insert knot
16crv.insert_knot(0.5)
Plotting
To plot the curve, a visualization module should be imported and curve should be updated to use the visualization module.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
14
15# Import Matplotlib visualization module
16from geomdl.visualization import VisMPL
17
18# Set the visualization component of the curve
19crv.vis = VisMPL.VisCurve3D()
20
21# Plot the curve
22crv.render()
Convert non-rational to rational curve
The following code snippet generates a B-Spline (non-rational) curve and converts it into a NURBS (rational) curve.
1from geomdl import BSpline
2
3# Create the curve instance
4crv = BSpline.Curve()
5
6# Set degree
7crv.degree = 2
8
9# Set control points
10crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
11
12# Set knot vector
13crv.knotvector = [0, 0, 0, 1, 1, 1]
14
15# Import convert module
16from geomdl import convert
17
18# BSpline to NURBS
19crv_rat = convert.bspline_to_nurbs(crv)
Using knot vector generator
Knot vector generator is located in the knotvector module.
1from geomdl import BSpline
2from geomdl import knotvector
3
4# Create the curve instance
5crv = BSpline.Curve()
6
7# Set degree
8crv.degree = 2
9
10# Set control points
11crv.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
12
13# Generate a uniform knot vector
14crv.knotvector = knotvector.generate(crv.degree, crv.ctrlpts_size)
Plotting multiple curves
multi module can be used to plot multiple curves on the same figure.
1from geomdl import BSpline
2from geomdl import multi
3from geomdl import knotvector
4
5# Create the curve instance #1
6crv1 = BSpline.Curve()
7
8# Set degree
9crv1.degree = 2
10
11# Set control points
12crv1.ctrlpts = [[1, 0, 0], [1, 1, 0], [0, 1, 0]]
13
14# Generate a uniform knot vector
15crv1.knotvector = knotvector.generate(crv1.degree, crv1.ctrlpts_size)
16
17# Create the curve instance #2
18crv2 = BSpline.Curve()
19
20# Set degree
21crv2.degree = 3
22
23# Set control points
24crv2.ctrlpts = [[1, 0, 0], [1, 1, 0], [2, 1, 0], [1, 1, 0]]
25
26# Generate a uniform knot vector
27crv2.knotvector = knotvector.generate(crv2.degree, crv2.ctrlpts_size)
28
29# Create a curve container
30mcrv = multi.CurveContainer(crv1, crv2)
31
32# Import Matplotlib visualization module
33from geomdl.visualization import VisMPL
34
35# Set the visualization component of the curve container
36mcrv.vis = VisMPL.VisCurve3D()
37
38# Plot the curves in the curve container
39mcrv.render()
Please refer to the Examples Repository for more curve examples.
Working with the surfaces
The majority of the surface API is very similar to the curve API. Since a surface is defined on a 2-dimensional
parametric space, the getters/setters have a suffix of _u
and _v
; such as knotvector_u
and
knotvector_v
.
For setting up the control points, please refer to the control points manager documentation.
Please refer to the Examples Repository for surface examples.
Working with the volumes
Volumes are defined on a 3-dimensional parametric space. Working with the volumes are very similar to working
with the surfaces. The only difference is the 3rd parametric dimension, w
. For instance, to access the
knot vectors, the properties you will use are knotvector_u
, knotvector_v
and knotvector_w
.
For setting up the control points, please refer to the control points manager documentation.
Please refer to the Examples Repository for volume examples.