Nurbs Python
Nurbs Python
Release
1 Motivation 3
1.1 Algorithms Implemented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Citing NURBS-Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Installation 7
3.1 Using Pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Manual Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 File Formats 11
5.1 TXT Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.2 CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6 Visualization 15
6.1 Visualization Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.2 Advanced Visualization Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
7 Package Name 31
8 Included Modules 33
8.1 B-Spline Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.2 NURBS Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.3 Utilities Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.4 Control Points Generator Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.5 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
i
ii
NURBS-Python Documentation, Release
Welcome to the NURBS-Python Library documentation! NURBS-Python Library contains native Python implemen-
tations of several The NURBS Book algorithms. These algorithms are used for generating Non-Uniform Rational
B-Spline (NURBS) 2D/3D curves and surfaces.
NURBS-Python also provides a convenient and easy-to-use data structure for storing curve and surface descriptions.
All elements of the provided data structures are documented under Modules.
Note: I would be glad if you cite this repository using the DOI provided as a badge at the top. The link will open up
a new page where you can find all the bibliographic information.
Introduction 1
NURBS-Python Documentation, Release
2 Introduction
CHAPTER 1
Motivation
NURBS-Python is an object-oriented Python library containing implementations of NURBS 2D/3D curve and surface
generation and evaluation algorithms. It also provides a convenient and easy-to-use data structure for storing curve
and surface descriptions.
Some significant features of NURBS-Python are:
• Fully object-oriented API
• Data structures for storing surface and 2D/3D curve descriptions
• Helper functions, such as automatic uniform knot vector generator and many more
• Control points grid generator for surfaces
• Visualization component, plus a Matplotlib implementation
• CSV export functionality with customizable meshing options
• Python 2.x and 3.x compatibility
• No external C/C++ library dependencies
• No compilation steps necessary, everything is implemented with Python
• Easy to install via pip install NURBS-Python
NURBS-Python currently implements the following algorithms from The NURBS Book (2nd Edition) by Piegl &
Tiller:
• A2.1 FindSpan (page 68)
• A2.2 BasisFuns (page 70)
• A2.3 DersBasisFuns (pages 72,73)
• A3.1 CurvePoint (page 82)
3
NURBS-Python Documentation, Release
I would be glad if you cite this repository using the DOI provided. You can also find it as a badge on the main page of
this documentation.
1.3 Author
1.4 Acknowledgments
I would like to thank my PhD adviser, Dr. Adarsh Krishnamurthy, for his guidance and supervision throughout the
course of this project.
4 Chapter 1. Motivation
CHAPTER 2
NURBS is an acronym for Non-Uniform Rational Basis Spline and it represents a mathematical model for generation
of curves and surfaces in a flexible way. It is a well-accepted industry standard and used as a basis for nearly all of the
3D modeling and CAD/CAM software packages as well as modeling and visualization frameworks.
Please see the related Wikipedia article or The NURBS Book, a very nice and informative book written by Les A.
Piegl and Wayne Tiller.
The main purpose is implementing the well-known NURBS algorithms in native Python in an organized way and
without using any converters or wrappers, like SWIG or Boost.Python.
Although these wrappers are lifesavers by means of converting C++ code to Python when there are too many deadlines,
their support on the source language might be limited or you might need to learn the wrapper’s own language to get
the things done in your way. Personally speaking, I had to learn Python’s C API to understand how SWIG’s typemap
system works. It takes so much time when you are not well-acquainted with the low-level programming or not willing
to learn a programming language’s internals.
On the other hand, NURBS-Python is designed to get the things done in a fast way. I used object-oriented approach
as much as possible and tried to make the code look more Pythonic and optimized. Since all the code is implemented
in Python natively with no external dependencies, it is possible to use this library in every platform which core Python
programming language is supported or integrate into embedded systems/distributions. Using native implementation
approach also allows users to debug and extend this library very easily.
5
NURBS-Python Documentation, Release
NURBS-Python is tested on Python versions 2.7.13 and 3.5.3+. It doesn’t require any additional packages, such
as NumPy, so that you can run it even on a plain Python installation as well as on a scientific distribution, such as
Anaconda.
Note: Please use the issue tracker on GitHub to report bugs. If you have any questions and/or comments, please feel
free to email the author.
Installation
You can find the NURBS-Python library on Python Package Index and install using the following command:
pip install NURBS-Python
Included setup.py script will take care of the installation and automatically copy the required files to site-packages
directory. Please run the following from the command line:
python setup.py install
If you don’t want to put the files into your Python distribution’s site-packages directory for some reason, you can run
python setup.py develop
from the command line to generate a link to the package directory inside site-packages.
7
NURBS-Python Documentation, Release
8 Chapter 3. Installation
CHAPTER 4
Please check the Examples repository to find some example scripts on how to use this library.
Please note that I only provide some Python scripts in the Examples repository, nothing fancy. If you are an avid user
of Jupyter, I believe that it would be very easy for you to convert these scripts into Jupyter notebook format.
9
NURBS-Python Documentation, Release
File Formats
NURBS-Python library provides 2 functions in each class included for reading and saving the control points list.
These are:
• read_ctrlpts_from_txt: Reads control points list from a text file
• save_ctrlpts_to_txt: Saves control points list to a text file
The format of the text file depends on the type of the geometric element, i.e. curve or surface, that you are trying to
export. The following explains the file formats for .txt files which contain control points.
5.1.1 2D Curves
To generate a 2D B-Spline Curve, you need a list of (x, y) coordinates representing the control points (P), where
• x: value representing the x-coordinate
• y: value representing the y-coordinate
The format of the control points file for generating 2D B-Spline curves is as follows:
x y
x1 y1
x2 y2
x3 y3
The control points file format of the NURBS curves are very similiar to B-Spline ones with the difference of weights.
To generate a 2D NURBS curve, you need a list of (x*w, y*w, w) coordinates representing the weighted control points
(Pw ) where,
• x: value representing the x-coordinate
• y: value representing the y-coordinate
11
NURBS-Python Documentation, Release
x*w y*w w
x1 *w1 y1 *w1 w1
x2 *w2 y2 *w2 w2
x3 *w3 y3 *w3 w3
5.1.2 3D Curves
To generate a 3D B-Spline curve, you need a list of (x, y, z) coordinates representing the control points (P), where
• x: value representing the x-coordinate
• y: value representing the y-coordinate
• z: value representing the z-coordinate
The format of the control points file for generating 3D B-Spline curves is as follows:
x y z
x1 y1 z1
x2 y2 z2
x3 y3 z3
To generate a 3D NURBS curve, you need a list of (x*w, y*w, z*w, w) coordinates representing the weighted control
points (Pw ) where,
• x: value representing the x-coordinate
• y: value representing the y-coordinate
• z: value representing the z-coordinate
• w: value representing the weight
The format of the control points file for generating 3D NURBS curves is as follows:
5.1.3 Surfaces
Control points file for generating B-Spline and NURBS has 2 options:
First option is very similar to the curve control points files with one noticeable difference to process u and v indices.
In this list, the v index varies first. That is, a row of v control points for the first u value is found first. Then, the row of
v control points for the next u value.
The second option sets the rows as v and columns as u. To generate a B-Spline surface using this option, you need a
list of (x, y, z) coordinates representing the control points (P) where,
• x: value representing the x-coordinate
v0 v1 v2 v3 v4
u0 (x, y, z) (x, y, z) (x, y, z) (x, y, z) (x, y, z)
u1 (x, y, z) (x, y, z) (x, y, z) (x, y, z) (x, y, z)
u2 (x, y, z) (x, y, z) (x, y, z) (x, y, z) (x, y, z)
To generate a NURBS surface using the 2nd option, you need a list of (x*w, y*w, z*w, w) coordinates representing the
weighted control points (Pw ) where,
• x: value representing the x-coordinate
• y: value representing the y-coordinate
• z: value representing the z-coordinate
• w: value representing the weight
The format of the control points file for generating NURBS surfaces is as follows:
v0 v1 v2 v3
u0 (x*w, y*w, z*w, w) (x*w, y*w, z*w, w) (x*w, y*w, z*w, w) (x*w, y*w, z*w, w)
u1 (x*w, y*w, z*w, w) (x*w, y*w, z*w, w) (x*w, y*w, z*w, w) (x*w, y*w, z*w, w)
u2 (x*w, y*w, z*w, w) (x*w, y*w, z*w, w) (x*w, y*w, z*w, w) (x*w, y*w, z*w, w)
NURBS-Python library provides 2 functions in each class for exporting control points and evaluted points as CSV
files. These functions are:
For all classes:
• export_ctrlpts_to_csv: Saves control points list as a CSV file
For Curve and Curve2D classes:
• export_curvepts_to_csv: Saves evaluated curve points as a CSV file
For Surface class:
• export_surfpts_to_csv: Saves evaluted surface points as a CSV file
The control points and the evaluated curve points list are always linear and there are no customization options. On the
other hand, CSV exports from surface classes have some customization options.
The following modes are available via mode= parameter of the export_ctrlpts_to_csv method:
• linear: Default mode, saves the stored point array without any change
• zigzag: Generates a zig-zag shape
• wireframe: Generates a wireframe
The following modes are available via mode= parameter of the export_surfpts_to_csv method:
• linear: Default mode, saves the stored point array without any change
• zigzag: Generates a zig-zag shape
• wireframe: Generates a wireframe/quad mesh
• triangle: Generates a triangular mesh
Visualization
NURBS-Python v3.x series included an optional visualization module for plotting evaluated curves and surfaces.
Examples repository contains some examples on how to use the visualization component with surfaces and 2D/3D
curves.
visualization/ directory in the Examples repository contains customizable scripts for more advanced visualiza-
tion using Matplotlib.
6.3 Examples
The following examples illustrate the visualization component which comes with the NURBS-Python package and the
advanced visualization options using the scripts in the Examples repository.
6.3.1 Surfaces
The following figures are generated using Matplotlib v2.1.0 from the outputs of the examples shared in the Examples
repository. Please see File Formats section on details of CSV exporting capabilities. Visualization scripts can be found
in the Examples repository under visualization directory.
ex_surface01.py
15
NURBS-Python Documentation, Release
16 Chapter 6. Visualization
NURBS-Python Documentation, Release
ex_surface02.py
6.3. Examples 17
NURBS-Python Documentation, Release
ex_surface03.py
18 Chapter 6. Visualization
NURBS-Python Documentation, Release
mpl_trisurf_vectors.py
The following figure illustrates tangent and normal vectors on ex_surface02.py example. The example script
can be found in Examples repository under the visualization directory.
6.3. Examples 19
NURBS-Python Documentation, Release
6.3.2 Curves
The following examples use the visualization component which comes with the NURBS-Python package.
20 Chapter 6. Visualization
NURBS-Python Documentation, Release
ex_curve01.py
6.3. Examples 21
NURBS-Python Documentation, Release
ex_curve02.py
22 Chapter 6. Visualization
NURBS-Python Documentation, Release
ex_curve03.py
6.3. Examples 23
NURBS-Python Documentation, Release
ex_curve04.py
24 Chapter 6. Visualization
NURBS-Python Documentation, Release
ex_curve3d01.py
6.3. Examples 25
NURBS-Python Documentation, Release
ex_curve3d02.py
mpl_curve2d_tangents.py
This example illustrates a more advanced visualization option for plotting the 2D curve tangents alongside with the
control points grid and the evaluated curve. The example script can be found in Examples repository under the
visualization directory.
26 Chapter 6. Visualization
NURBS-Python Documentation, Release
mpl_curve3d_tangents.py
This example illustrates a more advanced visualization option for plotting the 3D curve tangents alongside with the
control points grid and the evaluated curve. The example script can be found in Examples repository under the
visualization directory.
6.3. Examples 27
NURBS-Python Documentation, Release
mpl_curve3d_vectors.py
This example illustrates a visualization option for plotting the 3D curve tangent, normal and binnormal vectors along-
side with the control points grid and the evaluated curve. The example script can be found in Examples repository
under the visualization directory.
Please note that binormal vector evaluation method for the curves is added on version 3.0.6.
28 Chapter 6. Visualization
NURBS-Python Documentation, Release
6.3. Examples 29
NURBS-Python Documentation, Release
30 Chapter 6. Visualization
CHAPTER 7
Package Name
The package name is geomdl which is the shortened version of geometric modeling.
Prior to v3.x series of the NURBS-Python library, the package name was nurbs. However, using a generic name like
that might seem to cause conflicts with other packages, and therefore in v3.x of the library, it is renamed to geomdl.
31
NURBS-Python Documentation, Release
Included Modules
The NURBS-Python library is more than a simple NURBS library. It includes 2D/3D curve and surface classes which
contains a convenient data structure implemented with Python properties and evaluation functionality. Everything is
self-contained, there is no need for loading external modules.
Following modules are included in this library:
BSpline module provides data storage and evaluation functions for B-Spline (NUBS) 2D/3D curves and surfaces.
class geomdl.BSpline.Curve
Bases: object
A data storage and evaluation class for 3D B-Spline (NUBS) curves.
Data Storage
The following properties are present in this class:
• order
• degree
• knotvector
• delta
• ctrlpts
• curvepts
33
NURBS-Python Documentation, Release
The function read_ctrlpts_from_txt() provides an easy way to read weighted control points from a
text file. Additional details on the file formats can be found in the documentation.
Evaluation
The evaluation methods are:
• evaluate()
• derivatives()
• tangent()
• insert_knot()
Please check the function reference for the details.
Note: If you update any of the data storage elements after the curve evaluation, the surface points stored in
curvepts property will be deleted automatically.
order
Curve order.
Defined as order = degree + 1
Getter Gets the curve order
Setter Sets the curve order
Type integer
degree
Curve degree.
Getter Gets the curve degree
Setter Sets the curve degree
Type integer
ctrlpts
Control points.
Getter Gets the control points
Setter Sets the control points
Type list
set_ctrlpts(ctrlpts)
Sets control points.
Parameters ctrlpts (list) – input control points as a list of coordinates
Returns None
knotvector
Knot vector.
Getter Gets the knot vector
Setter Sets the knot vector
Type list
delta
Curve evaluation delta.
curvepts
Evaluated curve points.
Getter (x, y) coordinates of the evaluated surface points
Type list
vis
Visualization component.
render(cpcolor=’blue’, curvecolor=’black’)
Renders the curve using the loaded visualization component
The visualization component must be set using vis property before calling this method.
Parameters
• cpcolor (str) – Color of the control points polygon
• curvecolor (str) – Color of the curve
Returns None
read_ctrlpts_from_txt(filename=”)
Loads control points from a text file.
Parameters filename (str) – input file name
Returns True if control points are loaded correctly, False otherwise
Return type bool
save_ctrlpts_to_txt(filename=”)
Saves control points to a text file.
Parameters filename (str) – output file name
Returns True if control points are saved correctly, False otherwise
Return type bool
save_ctrlpts_to_csv(filename=”, scalar=0)
Saves control points to a comma separated text file.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
Returns True if control points are saved correctly, False otherwise
Return type bool
save_curvepts_to_csv(filename=”, scalar=0)
Saves evaluated curve points to a comma separated text file.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
Returns True if curve points are saved correctly, False otherwise
Return type bool
curvept(u=-1, check_vars=True, get_ctrlpts=False)
Evaluates the B-Spline curve at the given u parameter.
Parameters
• u (float) – parameter
• check_vars (bool) – flag to disable variable checking (only for internal eval functions)
• get_ctrlpts – flag to add a list of control points associated with the curve evaluation
to return value
• get_ctrlpts – bool
Returns evaluated curve point at the given knot value
Return type list
evaluate()
Evaluates the B-Spline curve.
Returns None
derivatives2(u=-1, order=0)
Evaluates n-th order curve derivatives at the given u using Algorithm A3.2.
Parameters
• u (float) – knot value
• order (integer) – derivative order
Returns A list containing up to {order}-th derivative of the curve
Return type list
class geomdl.BSpline.Curve2D
Bases: geomdl.BSpline.Curve
A data storage and evaluation class for 2D B-Spline (NUBS) curves.
Data Storage
Evaluation
The evaluation methods are:
• evaluate()
• derivatives()
• tangent()
• insert_knot()
Please check the function reference for the details.
Note: If you update any of the data storage elements after the curve evaluation, the surface points stored in
curvepts property will be deleted automatically.
binormal(u=-1, normalize=True)
Evaluates the curve binormal at the given u parameter.
Curve binormal is the cross product of the normal and the tangent vectors. The output returns a list
containing the starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “binormal”
Return type list
binormals(u_list=(), normalize=False)
Evaluates the curve binormal at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
ctrlpts
Control points.
Getter Gets the control points
Setter Sets the control points
Type list
curvept(u=-1, check_vars=True, get_ctrlpts=False)
Evaluates the B-Spline curve at the given u parameter.
Parameters
• u (float) – parameter
• check_vars (bool) – flag to disable variable checking (only for internal eval functions)
• get_ctrlpts – flag to add a list of control points associated with the curve evaluation
to return value
• get_ctrlpts – bool
Returns evaluated curve point at the given knot value
Return type list
curvepts
Evaluated curve points.
Getter (x, y) coordinates of the evaluated surface points
Type list
degree
Curve degree.
Getter Gets the curve degree
Setter Sets the curve degree
Type integer
delta
Curve evaluation delta.
derivatives(u=-1, order=0)
Evaluates n-th order curve derivatives at the given u using Algorithm A3.4.
Parameters
• u (float) – knot value
• order (integer) – derivative order
Returns A list containing up to {order}-th derivative of the curve
Returns None
insert_knot(u, r=1)
Inserts the given knot and updates the control points array and the knot vector.
Parameters
• u (float) – Knot to be inserted
• r (int) – number of knot insertions
Returns None
knotvector
Knot vector.
Getter Gets the knot vector
Setter Sets the knot vector
Type list
normal(u=-1, normalize=True)
Evaluates the curve normal at the given u parameter.
Curve normal is basically the second derivative of the curve. The output returns a list containing the
starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “normal”
Return type list
normals(u_list=(), normalize=False)
Evaluates the curve normal at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
order
Curve order.
Defined as order = degree + 1
Getter Gets the curve order
Setter Sets the curve order
Type integer
read_ctrlpts_from_txt(filename=”)
Loads control points from a text file.
Parameters filename (str) – input file name
Returns True if control points are loaded correctly, False otherwise
Return type bool
render(cpcolor=’blue’, curvecolor=’black’)
Renders the curve using the loaded visualization component
The visualization component must be set using vis property before calling this method.
Parameters
• cpcolor (str) – Color of the control points polygon
• curvecolor (str) – Color of the curve
Returns None
save_ctrlpts_to_csv(filename=”, scalar=0)
Saves control points to a comma separated text file.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
Returns True if control points are saved correctly, False otherwise
Return type bool
save_ctrlpts_to_txt(filename=”)
Saves control points to a text file.
Parameters filename (str) – output file name
Returns True if control points are saved correctly, False otherwise
Return type bool
save_curvepts_to_csv(filename=”, scalar=0)
Saves evaluated curve points to a comma separated text file.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
Returns True if curve points are saved correctly, False otherwise
Return type bool
set_ctrlpts(ctrlpts)
Sets control points.
Parameters ctrlpts (list) – input control points as a list of coordinates
Returns None
tangent(u=-1, normalize=False)
Evaluates the curve tangent at the given u parameter.
The output returns a list containing the starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “tangent”
Return type list
tangents(u_list=(), normalize=False)
Evaluates the curve tangent at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
vis
Visualization component.
Type float
class geomdl.BSpline.Surface
Bases: object
A data storage and evaluation class for B-Spline (NUBS) surfaces.
Data Storage
The following properties are present in this class:
• order_u
• order_v
• degree_u
• degree_v
• knotvector_u
• knotvector_v
• delta
• ctrlpts
• ctrlpts2d
• surfpts
Details on Control Points
Control points are stored as a list of (x, y, z) coordinates. The function read_ctrlpts_from_txt()
provides an easy way to read control points from a text file. Additional details on the file formats can be found
on the documentation.
Evaluation
The evaluation methods are:
• evaluate()
• derivatives()
• tangent()
• normal()
• insert_knot()
Please check the function reference for the details.
Note: If you update any of the data storage elements after the surface evaluation, the surface points stored in
surfpts property will be deleted automatically.
order_u
Surface order for U direction.
Follows the following equality: order = degree + 1
Getter Gets the surface order for U direction
Note: The v index varies first. That is, a row of v control points for the first u value is found first. Then,
the row of v control points for the next u value.
ctrlpts2d
Control points.
The getter returns a tuple of 2D control points (weighted control points + weights if NURBS) in [u][v] for-
mat. The rows of the returned tuple correspond to V-direction and the columns correspond to U-direction.
The following example can be used to traverse 2D control points:
# Create a BSpline surface
surf_bs = BSpline.Surface()
When using NURBS.Surface class, the output of ctrlpts2d property could be confusing since,
ctrlpts always returns the unweighted control points, i.e. ctrlpts property returns 3D control
points all divided by the weights and you can use weights property to access the weights vector, but
ctrlpts2d returns the weighted ones plus weights as the last element. This difference is intentionally
added for compatibility and interoperability purposes.
To explain this situation in a simple way;
• If you need the weighted control points directly, use ctrlpts2d
• If you need the control points and the weights separately, use ctrlpts and weights
Note: Please note that the setter doesn’t check for inconsistencies and using the setter is not recommended.
Instead of the setter property, please use set_ctrlpts() function.
ctrlpts_size_u
Size of the control points array in U-direction.
Getter Gets number of control points in U-direction
Setter Sets number of control points in U-direction
Type int
ctrlpts_size_v
Size of the control points array in V-direction.
Getter Gets number of control points in V-direction
Setter Sets number of control points in V-direction
Type int
set_ctrlpts(ctrlpts, size_u, size_v)
Sets 1D control points.
This function expects a list coordinates which is also a list. For instance, if you are working in 3D space,
then your coordinates will be a list of 3 elements representing (x, y, z) coordinates.
This function also generates 2D control points in [u][v] format which can be accessed via ctrlpts2d
property.
Note: The v index varies first. That is, a row of v control points for the first u value is found first. Then,
the row of v control points for the next u value.
Parameters
• ctrlpts (list) – input control points as a list of coordinates
• size_u (int) – size of the control points grid in U-direction
• size_v (int) – size of the control points grid in V-direction
Returns None
knotvector_u
Knot vector for U direction.
Getter Gets the knot vector for U direction
Setter Sets the knot vector for U direction
Type list
knotvector_v
Knot vector for V direction.
Getter Gets the knot vector for V direction
Setter Sets the knot vector for V direction
Type list
delta
Surface evaluation delta.
surfpts
Evaluated surface points.
Getter (x, y, z) coordinates of the evaluated surface points
Type list
vis
Visualization component.
Type float
render(cpcolor=’blue’, surfcolor=’green’)
Renders the surface using the loaded visualization component
The visualization component must be set using vis property before calling this method.
Parameters
• cpcolor (str) – Color of the control points grid
• surfcolor (str) – Color of the surface
Returns None
read_ctrlpts_from_txt(filename=”, two_dimensional=True, size_u=0, size_v=0)
Loads control points from a text file.
The control points loaded from the file should follow the right-hand rule. In case two_dimensional = False,
then the following rules apply.
• The v index varies first. That is, a row of v control points for the first u value is found first.
• Then, the row of v control points for the next u value.
Parameters
• filename (str) – input file name
• two_dimensional (bool) – flag to control point list. one dimensional or two dimen-
sional
• size_u (int) – length of the control points array in U-direction
• size_v (int) – length of the control points array in V-direction
Returns True if control points are loaded correctly, False otherwise
Return type bool
save_ctrlpts_to_txt(filename=”, two_dimensional=True)
Saves control points to a text file.
The control points saved to the file follow the right-hand rule. In case two_dimensional = False, then the
following rules apply.
• The v index varies first. That is, a row of v control points for the first u value is found first.
• Then, the row of v control points for the next u value.
Parameters
• filename (str) – output file name
• two_dimensional (bool) – flag to control point list
Returns True if control points are saved correctly, False otherwise
Return type bool
• linear: Default mode, saves the stored point array without any change
• zigzag: Generates a zig-zag shape
• wireframe: Generates a wireframe
Please note that mode parameter does not modify the stored points in the object instance.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
• mode – sets the arrangement of the points, default is linear
Type str
Returns True if control points are saved correctly, False otherwise
Return type bool
save_surfpts_to_csv(filename=”, scalar=0, mode=’linear’)
Saves evaluated surface points to a comma separated text file.
Available Modes
The following modes are available via mode= parameter:
• linear: Default mode, saves the stored point array without any change
• zigzag: Generates a zig-zag shape
• wireframe: Generates a wireframe/quad mesh
• triangle: Generates a triangular mesh
Please note that mode parameter does not modify the stored points in the object instance.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
• mode – sets the arrangement of the points, default is linear
Type str
Returns True if surface points are saved correctly, False otherwise
Return type bool
transpose()
Transposes the surface by swapping U and V directions.
Returns None
surfpt(u=-1, v=-1, check_vars=True, get_ctrlpts=False)
Evaluates the B-Spline surface at the given (u,v) parameters.
Parameters
• u (float) – parameter in the U direction
• v (float) – parameter in the V direction
• check_vars (bool) – flag to disable variable checking (only for internal eval functions)
• get_ctrlpts – flag to add a list of control points associated with the surface evaluation
to return value
• get_ctrlpts – bool
Returns evaluated surface point at the given knot values
Return type list
evaluate()
Evaluates the surface.
Returns None
Parameters
• u (float) – parameter in the U direction
• v (float) – parameter in the V direction
• order (integer) – derivative order
Returns A list SKL, where SKL[k][l] is the derivative of the surface S(u,v) w.r.t. u k times and
v l times
Return type list
NURBS module provides data storage and evaluation functions for NURBS 2D/3D curves and surfaces.
class geomdl.NURBS.Curve
Bases: geomdl.BSpline.Curve
A data storage and evaluation class for 3D NURBS curves.
Data Storage
Note: Control points are stored as a list of (x*w, y*w, z*w, w) coordinates
Evaluation
The evaluation methods are:
• evaluate()
• derivatives()
• tangent()
• insert_knot()
Please check the function reference for the details.
Note: If you update any of the data storage elements after the curve evaluation, the surface points stored in
curvepts property will be deleted automatically.
ctrlpts
Control points.
Getter Gets un-weighted control points. Use weights to get weights vector.
Setter Sets weighted control points
Type list
weights
Weights vector.
Getter Extracts the weights vector from weighted control points array
Type list
curvept(u=-1, check_vars=True, get_ctrlpts=False)
Evaluates the NURBS curve at the given u parameter.
Parameters
• u (float) – parameter
• check_vars (bool) – flag to disable variable checking (only for internal eval functions)
• get_ctrlpts – flag to add a list of control points associated with the curve evaluation
to return value
• get_ctrlpts – bool
Returns evaluated curve point at the given knot value
Return type list
derivatives(u=-1, order=0)
Evaluates n-th order curve derivatives at the given u from the rational curve.
Parameters
• u (float) – knot value
• order (integer) – derivative order
Returns A list containing up to {order}-th derivative of the curve
Return type list
derivatives2(u=-1, order=0)
Evaluates n-th order curve derivatives at the given u from the rational curve.
Parameters
• u (float) – knot value
• order (integer) – derivative order
Returns A list containing up to {order}-th derivative of the curve
Return type list
binormal(u=-1, normalize=True)
Evaluates the curve binormal at the given u parameter.
Curve binormal is the cross product of the normal and the tangent vectors. The output returns a list
containing the starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “binormal”
Return type list
binormals(u_list=(), normalize=False)
Evaluates the curve binormal at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
curvepts
Evaluated curve points.
Getter (x, y) coordinates of the evaluated surface points
Type list
degree
Curve degree.
Getter Gets the curve degree
Setter Sets the curve degree
Type integer
delta
Curve evaluation delta.
Returns None
insert_knot(u, r=1)
Inserts the given knot and updates the control points array and the knot vector.
Parameters
• u (float) – Knot to be inserted
• r (int) – number of knot insertions
Returns None
knotvector
Knot vector.
save_ctrlpts_to_csv(filename=”, scalar=0)
Saves control points to a comma separated text file.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
Returns True if control points are saved correctly, False otherwise
Return type bool
save_ctrlpts_to_txt(filename=”)
Saves control points to a text file.
Parameters filename (str) – output file name
Returns True if control points are saved correctly, False otherwise
Return type bool
save_curvepts_to_csv(filename=”, scalar=0)
Saves evaluated curve points to a comma separated text file.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
Returns True if curve points are saved correctly, False otherwise
Return type bool
set_ctrlpts(ctrlpts)
Sets control points.
Parameters ctrlpts (list) – input control points as a list of coordinates
Returns None
tangent(u=-1, normalize=False)
Evaluates the curve tangent at the given u parameter.
The output returns a list containing the starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “tangent”
Return type list
tangents(u_list=(), normalize=False)
Evaluates the curve tangent at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
vis
Visualization component.
class geomdl.NURBS.Curve2D
Bases: geomdl.NURBS.Curve
A data storage and evaluation class for 2D NURBS curves.
Data Storage
The following properties are present in this class:
• order
• degree
• knotvector
• delta
• ctrlpts
• weights
• curvepts
The function read_ctrlpts_from_txt() provides an easy way to read weighted control points from a
text file. Additional details on the file formats can be found in the documentation.
Evaluation
The evaluation methods are:
• evaluate()
• derivatives()
• tangent()
• insert_knot()
Please check the function reference for the details.
Note: If you update any of the data storage elements after the curve evaluation, the surface points stored in
curvepts property will be deleted automatically.
binormal(u=-1, normalize=True)
Evaluates the curve binormal at the given u parameter.
Curve binormal is the cross product of the normal and the tangent vectors. The output returns a list
containing the starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “binormal”
Return type list
binormals(u_list=(), normalize=False)
Evaluates the curve binormal at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
ctrlpts
Control points.
Getter Gets un-weighted control points. Use weights to get weights vector.
Setter Sets weighted control points
Type list
curvept(u=-1, check_vars=True, get_ctrlpts=False)
Evaluates the NURBS curve at the given u parameter.
Parameters
• u (float) – parameter
• check_vars (bool) – flag to disable variable checking (only for internal eval functions)
• get_ctrlpts – flag to add a list of control points associated with the curve evaluation
to return value
• get_ctrlpts – bool
Returns evaluated curve point at the given knot value
Return type list
curvepts
Evaluated curve points.
Getter (x, y) coordinates of the evaluated surface points
Type list
degree
Curve degree.
Getter Gets the curve degree
Setter Sets the curve degree
Type integer
delta
Curve evaluation delta.
derivatives(u=-1, order=0)
Evaluates n-th order curve derivatives at the given u from the rational curve.
Parameters
• u (float) – knot value
• order (integer) – derivative order
Returns A list containing up to {order}-th derivative of the curve
Return type list
derivatives2(u=-1, order=0)
Evaluates n-th order curve derivatives at the given u from the rational curve.
Parameters
• u (float) – knot value
• order (integer) – derivative order
Returns A list containing up to {order}-th derivative of the curve
Return type list
derivatives_ctrlpts(order=0, r1=0, r2=0)
Computes the control points of all derivative curves up to and including the {degree}-th derivative.
Implements Algorithm A3.3. Output is PK[k][i], i-th control point of the k-th derivative curve where 0 <=
k <= degree and r1 <= i <= r2-k
Parameters
• order (integer) – derivative order
• r1 (integer) – minimum span
• r2 (integer) – maximum span
Returns PK, a 2D list of control points
Return type list
evaluate()
Evaluates the B-Spline curve.
Returns None
insert_knot(u, r=1)
Inserts the given knot and updates the control points array and the knot vector.
Parameters
• u (float) – Knot to be inserted
• r (int) – number of knot insertions
Returns None
knotvector
Knot vector.
Getter Gets the knot vector
Setter Sets the knot vector
Type list
normal(u=-1, normalize=True)
Evaluates the curve normal at the given u parameter.
Curve normal is basically the second derivative of the curve. The output returns a list containing the
starting point (i.e. origin) of the vector and the vector itself.
Parameters
• u (float) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list in the order of “curve point” and “normal”
Return type list
normals(u_list=(), normalize=False)
Evaluates the curve normal at all u values in the input list.
Parameters
• u_list (tuple, list) – knot value
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
order
Curve order.
Defined as order = degree + 1
Getter Gets the curve order
Setter Sets the curve order
Type integer
read_ctrlpts_from_txt(filename=”)
Loads control points from a text file.
Parameters filename (str) – input file name
Returns True if control points are loaded correctly, False otherwise
weights
Weights vector.
Getter Extracts the weights vector from weighted control points array
Type list
class geomdl.NURBS.Surface
Bases: geomdl.BSpline.Surface
A data storage and evaluation class for NURBS surfaces.
Data Storage
The following properties are present in this class:
• order_u
• order_v
• degree_u
• degree_v
• knotvector_u
• knotvector_v
• delta
• ctrlpts
• ctrlpts2d
• weights
• surfpts
Details on Control Points
Control points are stored as a list of (x*w, y*w, z*w, w) coordinates, where w corresponds to the weight.
The function read_ctrlpts_from_txt() provides an easy way to read control points from a text file.
Additional details on the file formats can be found on the documentation.
Evaluation
The evaluation methods are:
• evaluate()
• derivatives()
• tangent()
• normal()
• insert_knot()
Please check the function reference for the details.
Note: If you update any of the data storage elements after the surface evaluation, the surface points stored in
surfpts property will be deleted automatically.
ctrlpts
Control points.
Getter Gets un-weighted control points. Use weights to get weights vector.
Setter Sets weighted control points.
Type list
weights
Weights vector.
Getter Extracts the weights vector from weighted control points array
Type list
ctrlpts2d
Control points.
The getter returns a tuple of 2D control points (weighted control points + weights if NURBS) in [u][v] for-
mat. The rows of the returned tuple correspond to V-direction and the columns correspond to U-direction.
The following example can be used to traverse 2D control points:
surf_nb = NURBS.Surface()
When using NURBS.Surface class, the output of ctrlpts2d property could be confusing since,
ctrlpts always returns the unweighted control points, i.e. ctrlpts property returns 3D control
points all divided by the weights and you can use weights property to access the weights vector, but
ctrlpts2d returns the weighted ones plus weights as the last element. This difference is intentionally
added for compatibility and interoperability purposes.
To explain this situation in a simple way;
• If you need the weighted control points directly, use ctrlpts2d
• If you need the control points and the weights separately, use ctrlpts and weights
Note: Please note that the setter doesn’t check for inconsistencies and using the setter is not recommended.
Instead of the setter property, please use set_ctrlpts() function.
ctrlpts_size_u
Size of the control points array in U-direction.
Getter Gets number of control points in U-direction
Setter Sets number of control points in U-direction
Type int
ctrlpts_size_v
Size of the control points array in V-direction.
Getter Gets number of control points in V-direction
Setter Sets number of control points in V-direction
Type int
degree_u
Surface degree for U direction.
Getter Gets the surface degree for U direction
Setter Sets the surface degree for U direction
Type integer
degree_v
Surface degree for V direction.
Getter Gets the surface degree V for V direction
evaluate()
Evaluates the surface.
Returns None
Parameters
• filename (str) – input file name
• two_dimensional (bool) – flag to control point list. one dimensional or two dimen-
sional
• size_u (int) – length of the control points array in U-direction
• size_v (int) – length of the control points array in V-direction
Returns True if control points are loaded correctly, False otherwise
Return type bool
render(cpcolor=’blue’, surfcolor=’green’)
Renders the surface using the loaded visualization component
The visualization component must be set using vis property before calling this method.
Parameters
• cpcolor (str) – Color of the control points grid
• surfcolor (str) – Color of the surface
Returns None
save_ctrlpts_to_csv(filename=”, scalar=0, mode=’linear’)
Saves control points to a comma separated text file.
Available Modes
The following modes are available via mode= parameter:
• linear: Default mode, saves the stored point array without any change
• zigzag: Generates a zig-zag shape
• wireframe: Generates a wireframe
Please note that mode parameter does not modify the stored points in the object instance.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
• mode – sets the arrangement of the points, default is linear
Type str
Returns True if control points are saved correctly, False otherwise
Return type bool
save_ctrlpts_to_txt(filename=”, two_dimensional=True)
Saves control points to a text file.
The control points saved to the file follow the right-hand rule. In case two_dimensional = False, then the
following rules apply.
• The v index varies first. That is, a row of v control points for the first u value is found first.
• Then, the row of v control points for the next u value.
Parameters
• filename (str) – output file name
• two_dimensional (bool) – flag to control point list
Returns True if control points are saved correctly, False otherwise
Return type bool
• linear: Default mode, saves the stored point array without any change
• zigzag: Generates a zig-zag shape
• wireframe: Generates a wireframe/quad mesh
• triangle: Generates a triangular mesh
Please note that mode parameter does not modify the stored points in the object instance.
Parameters
• filename (str) – output file name
• scalar (int) – value of the scalar column in the output, defaults to 0
• mode – sets the arrangement of the points, default is linear
Type str
Returns True if surface points are saved correctly, False otherwise
Return type bool
set_ctrlpts(ctrlpts, size_u, size_v)
Sets 1D control points.
This function expects a list coordinates which is also a list. For instance, if you are working in 3D space,
then your coordinates will be a list of 3 elements representing (x, y, z) coordinates.
This function also generates 2D control points in [u][v] format which can be accessed via ctrlpts2d
property.
Note: The v index varies first. That is, a row of v control points for the first u value is found first. Then,
the row of v control points for the next u value.
Parameters
• ctrlpts (list) – input control points as a list of coordinates
• size_u (int) – size of the control points grid in U-direction
• size_v (int) – size of the control points grid in V-direction
Returns None
surfpts
Evaluated surface points.
Getter (x, y, z) coordinates of the evaluated surface points
Type list
tangent(u=-1, v=-1, normalize=False)
Evaluates the surface tangent at the given (u, v) parameter.
The output returns a list containing the starting point (i.e. origin) of the vector and the vectors themselves.
Parameters
• u (float) – parameter in the U direction
• v (float) – parameter in the V direction
• normalize (bool) – if True, the returned tangent vector is converted to a unit vector
Returns A list in the order of “surface point”, “derivative w.r.t. u” and “derivative w.r.t. v”
Return type list
tangents(uv_list=(), normalize=False)
Evaluates the surface tangent at all (u, v) values in the input list.
The input list should be arranged as [[u1, v1], [u2, v2], . . . ]
Parameters
• uv_list (tuple, list) – knot values
• normalize (bool) – if True, the returned vector is converted to a unit vector
Returns a list of starting points and the vectors
Return type list
transpose()
Transposes the surface by swapping U and V directions.
Returns None
vis
Visualization component.
Parameters
utilities module contains common helper functions for B-spline and NURBS curve and surface evaluation. In
addition, it contains conversion operations related to control points, such as flipping arrays and adding weights.
geomdl.utilities.flip_ctrlpts(file_in=”, file_out=’ctrlpts_flip.txt’)
Flips u and v directions of a 2D control points file and saves flipped coordinates to a file.
Parameters
• file_in (str) – name of the input file (to be read)
• file_out (str) – name of the output file (to be saved)
Returns None
geomdl.utilities.generate_ctrlptsw(file_in=”, file_out=’ctrlptsw.txt’)
Generates weighted control points from unweighted ones.
This function takes in a 2D control points file whose coordinates are organized like (x, y, z, w), converts into
(x*w, y*w, z*w, w), and saves it to a file. Therefore, it could be a direct input of the NURBS.Surface class.
Parameters
• file_in (str) – name of the input file (to be read)
• file_out (str) – name of the output file (to be saved)
Returns None
geomdl.utilities.generate_ctrlpts_weights(file_in=”, file_out=’ctrlpts_weights.txt’)
Generates unweighted control points from weighted ones.
This function takes in a 2D control points file whose coordinates are organized like (x*w, y*w, z*w, w), converts
into (x, y, z, w), and saves it to a file.
Parameters
• file_in (str) – name of the input file (to be read)
• file_out (str) – name of the output file (to be saved)
Returns None
geomdl.utilities.make_zigzag(points, row_size)
Changes linearly ordered list of points into a zig-zag shape.
This function is designed to create input for the visualization software. It orders the points to draw a zig-zag
shape which enables generating properly connected lines without any scanlines. Please see the below sketch on
the functionality of the row_size parameter:
row size
<-=============->
------->>-------|
|------<<-------|
|------>>-------|
-------<<-------|
Please note that this function does not detect the ordering of the input points to detect the input points have
already been processed to generate a zig-zag shape.
Parameters
• points (list) – list of points to be ordered
• row_size – number of elements in a row which the zig-zag generated
• row_size – int
Returns re-ordered points
Return type list
geomdl.utilities.make_quad(points, row_size, col_size)
Generates a quad mesh from linearly ordered list of points.
Parameters
• points (list, tuple) – list of points to be ordered
• row_size – number of elements in a row
• row_size – int
• col_size – number of elements in a column
• col_size – int
Returns re-ordered points
Return type list
geomdl.utilities.make_triangle(points, row_size, col_size)
Generates a triangular mesh from linearly ordered list of points.
Parameters
• points (list, tuple) – list of points to be ordered
• row_size – number of elements in a row
• row_size – int
• col_size – number of elements in a column
• col_size – int
Returns re-ordered points
Return type list
geomdl.utilities.frange(start, stop, step=1.0, decimals=7)
Implementation of Python’s range() function which works with floats.
Reference to this implementation: https://stackoverflow.com/a/47877721
Parameters
• start (float) – start value
CPGen module allows users to generate control points grids as an input to BSpline.Surface and NURBS.
Surface classes. This module is designed to enable more testing cases in a very simple way and it doesn’t have
the capabilities of a fully-featured grid generator, but it should be enough to be used side by side with BSpline and
NURBS modules.
CPGen.Grid class provides an easy way to generate control point grids for use with BSpline.Surface class and
CPGen.GridWeighted does the same for NURBS.Surface class.
8.4.1 Grid
Note: Additional details on the file formats can be found in the documentation.
Parameters
• size_x (float) – width of the grid
• size_y (float) – height of the grid
grid()
Returns the generated grid.
Returns 2D list of points in [u][v] format
generate(num_u, num_v)
Generates grid using the input division parameters.
Parameters
• num_u (int) – number of divisions in x-direction
• num_v (int) – number of divisions in y-direction
Returns None
rotate_z(angle=0.0)
Rotates the grid about the z-axis.
Parameters angle (float) – angle of rotation about the z-axis
Returns None
rotate_y(angle=0.0)
Rotates the grid about the y-axis.
Parameters angle (float) – angle of rotation about the y-axis
Returns None
rotate_x(angle=0.0)
Rotates the grid about the x-axis.
Parameters angle (float) – angle of rotation about the x-axis
Returns None
translate(pt=(0.0, 0.0, 0.0))
Translates the grid origin to the input position.
Grid origin is (0, 0, 0) at instantiation and always represents the bottom left corner of the 2D grid.
Parameters pt (list) – new origin point
Returns None
save(filename=’grid.txt’)
Saves the generated grid to a text file.
Parameters filename (str) – File name to be saved
Returns True if grid is saved correctly, False otherwise
Return type bool
Parameters
• num_bumps (int) – Number of bumps (i.e. hills) to be generated on the 2D grid
• all_positive (bool) – Generate all bumps on the positive z direction
• bump_height (float) – z-value of the generated bumps on the grid
Returns None
Note: Additional details for the file formats can be found in the documentation.
Parameters
• size_x (float) – width of the grid
• size_y (float) – height of the grid
add_weight(w=1.0)
Adds a uniform weight to grid points.
All grid points are divided by the input weight and weight value is added as the last element of the coordi-
nate array. Grid points can be accessed via grid() function and can be saved as a text file via save()
function.
Parameters w (float) – weight value to be added
Returns None
modify_weight(w=-1.0)
Modifies weight value of the grid points.
Parameters w (float) – weight value to be added
Returns None
Parameters
• num_bumps (int) – Number of bumps (i.e. hills) to be generated on the 2D grid
• all_positive (bool) – Generate all bumps on the positive z direction
• bump_height (float) – z-value of the generated bumps on the grid
Returns None
generate(num_u, num_v)
Generates grid using the input division parameters.
Parameters
• num_u (int) – number of divisions in x-direction
• num_v (int) – number of divisions in y-direction
Returns None
grid()
Returns the generated grid.
Returns 2D list of points in [u][v] format
rotate_x(angle=0.0)
Rotates the grid about the x-axis.
Parameters angle (float) – angle of rotation about the x-axis
Returns None
rotate_y(angle=0.0)
Rotates the grid about the y-axis.
Parameters angle (float) – angle of rotation about the y-axis
Returns None
rotate_z(angle=0.0)
Rotates the grid about the z-axis.
Parameters angle (float) – angle of rotation about the z-axis
Returns None
save(filename=’grid.txt’)
Saves the generated grid to a text file.
8.5 Visualization
NURBS-Python provides an optional-to-use visualization module with a sample implementation that uses Matplotlib.
Please see the documentation of the following modules for details:
The visualization component in the NURBS-Python package provides an easy way to visualise the surfaces and the
2D/3D curves generated using the library. VisAbstract class provides the abstract base for all visualization com-
ponents.
class geomdl.visualization.VisBase.VisAbstract(plot_ctrlpts=True)
Bases: object
Visualization abstract class
Uses Python’s Abstract Base Class implementation to define a base for all common visualization options in
NURBS-Python package.
Parameters plot_ctrlpts (bool) – enables/disables display of control points plot on the final
figure
clear()
Clears the points, colors and names lists.
add(ptsarr=(), size=0, name=None, color=None)
Adds points sets to the visualization instance for plotting.
Parameters
• ptsarr (list, tuple) – control, curve or surface points
• size (int, list) – size in all directions
• name (str) – name of the point on the legend
• color (str) – color of the point on the legend
render()
Abstract method for rendering plots of the point sets.
This method must be implemented in all subclasses of VisAbstract class.
class geomdl.visualization.VisBase.VisAbstractSurf(plot_ctrlpts=True)
Bases: geomdl.visualization.VisBase.VisAbstract
Visualization abstract class for surfaces
8.5. Visualization 77
NURBS-Python Documentation, Release
Implements VisABstract class and also uses Python’s Abstract Base Class implementation to define a base
for surface visualization options in NURBS-Python package.
Parameters plot_ctrlpts (bool) – enables/disables display of control points plot on the final
figure
set_ctrlpts_offset(offset_value)
Sets an offset for the control points grid plot.
Parameters offset_value (float) – offset value
render()
Abstract method for rendering plots of the point sets.
This method must be implemented in all subclasses of VisAbstractSurf class.
add(ptsarr=(), size=0, name=None, color=None)
Adds points sets to the visualization instance for plotting.
Parameters
• ptsarr (list, tuple) – control, curve or surface points
• size (int, list) – size in all directions
• name (str) – name of the point on the legend
• color (str) – color of the point on the legend
clear()
Clears the points, colors and names lists.
render()
Plots the 3D curve and the control points polygon
add(ptsarr=(), size=0, name=None, color=None)
Adds points sets to the visualization instance for plotting.
Parameters
• ptsarr (list, tuple) – control, curve or surface points
• size (int, list) – size in all directions
• name (str) – name of the point on the legend
• color (str) – color of the point on the legend
clear()
Clears the points, colors and names lists.
class geomdl.visualization.VisMPL.VisSurface(plot_ctrlpts=True)
Bases: geomdl.visualization.VisBase.VisAbstractSurf
Visualization module for Surfaces
Triangular mesh plot for the surface and wireframe plot for the control points grid
render()
Plots the surface and the control points grid
add(ptsarr=(), size=0, name=None, color=None)
Adds points sets to the visualization instance for plotting.
Parameters
• ptsarr (list, tuple) – control, curve or surface points
• size (int, list) – size in all directions
• name (str) – name of the point on the legend
• color (str) – color of the point on the legend
clear()
Clears the points, colors and names lists.
set_ctrlpts_offset(offset_value)
Sets an offset for the control points grid plot.
Parameters offset_value (float) – offset value
class geomdl.visualization.VisMPL.VisSurfWireframe(plot_ctrlpts=True)
Bases: geomdl.visualization.VisBase.VisAbstractSurf
Visualization module for Surfaces
Scatter plot for the control points and wireframe for the surface points
render()
Plots the surface and the control points grid
add(ptsarr=(), size=0, name=None, color=None)
Adds points sets to the visualization instance for plotting.
Parameters
• ptsarr (list, tuple) – control, curve or surface points
• size (int, list) – size in all directions
8.5. Visualization 79
NURBS-Python Documentation, Release
set_ctrlpts_offset(offset_value)
Sets an offset for the control points grid plot.
Parameters offset_value (float) – offset value
Although there exists a visualization component, the users are completely free to use any visualization method or
software. For instance, CSV exporting facility of the curve and surface classes can be used to draw control points,
curves and surfaces using a software, such as Paraview.
The NURBS-Python Library is developed by Onur Rauf Bingol and all the code released under The MIT License.
8.5. Visualization 81
NURBS-Python Documentation, Release
g
geomdl.utilities, 70
geomdl.visualization.VisBase, 77
geomdl.visualization.VisMPL, 78
u
utilities (Unix, Windows), 70
v
VisBase (Unix, Windows), 77
VisMPL (Unix, Windows), 78
83
NURBS-Python Documentation, Release
A clear() (geomdl.visualization.VisMPL.VisCurve3D
add() (geomdl.visualization.VisBase.VisAbstract method), 79
method), 77 clear() (geomdl.visualization.VisMPL.VisSurface
add() (geomdl.visualization.VisBase.VisAbstractSurf method), 79
method), 78 clear() (geomdl.visualization.VisMPL.VisSurfScatter
add() (geomdl.visualization.VisMPL.VisCurve2D method), 80
method), 78 clear() (geomdl.visualization.VisMPL.VisSurfTriangle
add() (geomdl.visualization.VisMPL.VisCurve3D method), 80
method), 79 clear() (geomdl.visualization.VisMPL.VisSurfWireframe
add() (geomdl.visualization.VisMPL.VisSurface method), 80
method), 79 ctrlpts (geomdl.BSpline.Curve attribute), 34
add() (geomdl.visualization.VisMPL.VisSurfScatter ctrlpts (geomdl.BSpline.Curve2D attribute), 39
method), 80 ctrlpts (geomdl.BSpline.Surface attribute), 45
add() (geomdl.visualization.VisMPL.VisSurfTriangle ctrlpts (geomdl.NURBS.Curve attribute), 52
method), 80 ctrlpts (geomdl.NURBS.Curve2D attribute), 58
add() (geomdl.visualization.VisMPL.VisSurfWireframe ctrlpts (geomdl.NURBS.Surface attribute), 63
method), 79 ctrlpts2d (geomdl.BSpline.Surface attribute), 45
add_weight() (geomdl.CPGen.GridWeighted method), 75 ctrlpts2d (geomdl.NURBS.Surface attribute), 63
ctrlpts_size_u (geomdl.BSpline.Surface attribute), 46
B ctrlpts_size_u (geomdl.NURBS.Surface attribute), 64
ctrlpts_size_v (geomdl.BSpline.Surface attribute), 46
binomial_coefficient() (in module geomdl.utilities), 73
ctrlpts_size_v (geomdl.NURBS.Surface attribute), 64
binormal() (geomdl.BSpline.Curve method), 38
Curve (class in geomdl.BSpline), 33
binormal() (geomdl.BSpline.Curve2D method), 39
Curve (class in geomdl.NURBS), 51
binormal() (geomdl.NURBS.Curve method), 53
Curve2D (class in geomdl.BSpline), 38
binormal() (geomdl.NURBS.Curve2D method), 57
Curve2D (class in geomdl.NURBS), 57
binormals() (geomdl.BSpline.Curve method), 38
curvept() (geomdl.BSpline.Curve method), 36
binormals() (geomdl.BSpline.Curve2D method), 39
curvept() (geomdl.BSpline.Curve2D method), 40
binormals() (geomdl.NURBS.Curve method), 53
curvept() (geomdl.NURBS.Curve method), 52
binormals() (geomdl.NURBS.Curve2D method), 58
curvept() (geomdl.NURBS.Curve2D method), 58
bumps() (geomdl.CPGen.Grid method), 74
curvepts (geomdl.BSpline.Curve attribute), 35
bumps() (geomdl.CPGen.GridWeighted method), 75
curvepts (geomdl.BSpline.Curve2D attribute), 40
curvepts (geomdl.NURBS.Curve attribute), 53
C curvepts (geomdl.NURBS.Curve2D attribute), 58
clear() (geomdl.visualization.VisBase.VisAbstract
method), 77 D
clear() (geomdl.visualization.VisBase.VisAbstractSurf degree (geomdl.BSpline.Curve attribute), 34
method), 78 degree (geomdl.BSpline.Curve2D attribute), 40
clear() (geomdl.visualization.VisMPL.VisCurve2D degree (geomdl.NURBS.Curve attribute), 54
method), 78 degree (geomdl.NURBS.Curve2D attribute), 58
85
NURBS-Python Documentation, Release
86 Index
NURBS-Python Documentation, Release
R save_ctrlpts_to_csv() (geomdl.NURBS.Curve2D
read_ctrlpts_from_txt() (geomdl.BSpline.Curve method), method), 61
35 save_ctrlpts_to_csv() (geomdl.NURBS.Surface method),
read_ctrlpts_from_txt() (geomdl.BSpline.Curve2D 67
method), 42 save_ctrlpts_to_txt() (geomdl.BSpline.Curve method), 35
read_ctrlpts_from_txt() (geomdl.BSpline.Surface save_ctrlpts_to_txt() (geomdl.BSpline.Curve2D method),
method), 48 42
read_ctrlpts_from_txt() (geomdl.NURBS.Curve method), save_ctrlpts_to_txt() (geomdl.BSpline.Surface method),
55 48
read_ctrlpts_from_txt() (geomdl.NURBS.Curve2D save_ctrlpts_to_txt() (geomdl.NURBS.Curve method),
method), 60 56
read_ctrlpts_from_txt() (geomdl.NURBS.Surface save_ctrlpts_to_txt() (geomdl.NURBS.Curve2D
method), 66 method), 61
render() (geomdl.BSpline.Curve method), 35 save_ctrlpts_to_txt() (geomdl.NURBS.Surface method),
render() (geomdl.BSpline.Curve2D method), 42 67
render() (geomdl.BSpline.Surface method), 48 save_curvepts_to_csv() (geomdl.BSpline.Curve method),
render() (geomdl.NURBS.Curve method), 55 36
render() (geomdl.NURBS.Curve2D method), 61 save_curvepts_to_csv() (geomdl.BSpline.Curve2D
render() (geomdl.NURBS.Surface method), 67 method), 43
render() (geomdl.visualization.VisBase.VisAbstract save_curvepts_to_csv() (geomdl.NURBS.Curve method),
method), 77 56
render() (geomdl.visualization.VisBase.VisAbstractSurf save_curvepts_to_csv() (geomdl.NURBS.Curve2D
method), 78 method), 61
render() (geomdl.visualization.VisMPL.VisCurve2D save_surfpts_to_csv() (geomdl.BSpline.Surface method),
method), 78 49
render() (geomdl.visualization.VisMPL.VisCurve3D save_surfpts_to_csv() (geomdl.NURBS.Surface method),
method), 78 67
render() (geomdl.visualization.VisMPL.VisSurface set_ctrlpts() (geomdl.BSpline.Curve method), 34
method), 79 set_ctrlpts() (geomdl.BSpline.Curve2D method), 43
render() (geomdl.visualization.VisMPL.VisSurfScatter set_ctrlpts() (geomdl.BSpline.Surface method), 46
method), 80 set_ctrlpts() (geomdl.NURBS.Curve method), 56
render() (geomdl.visualization.VisMPL.VisSurfTriangle set_ctrlpts() (geomdl.NURBS.Curve2D method), 61
method), 80 set_ctrlpts() (geomdl.NURBS.Surface method), 68
render() (geomdl.visualization.VisMPL.VisSurfWireframe set_ctrlpts_offset() (ge-
method), 79 omdl.visualization.VisBase.VisAbstractSurf
rotate_x() (geomdl.CPGen.Grid method), 74 method), 78
rotate_x() (geomdl.CPGen.GridWeighted method), 76 set_ctrlpts_offset() (ge-
rotate_y() (geomdl.CPGen.Grid method), 74 omdl.visualization.VisMPL.VisSurface
rotate_y() (geomdl.CPGen.GridWeighted method), 76 method), 79
rotate_z() (geomdl.CPGen.Grid method), 74 set_ctrlpts_offset() (ge-
rotate_z() (geomdl.CPGen.GridWeighted method), 76 omdl.visualization.VisMPL.VisSurfScatter
method), 80
S set_ctrlpts_offset() (ge-
omdl.visualization.VisMPL.VisSurfTriangle
save() (geomdl.CPGen.Grid method), 74
method), 80
save() (geomdl.CPGen.GridWeighted method), 76
set_ctrlpts_offset() (ge-
save_ctrlpts_to_csv() (geomdl.BSpline.Curve method),
omdl.visualization.VisMPL.VisSurfWireframe
35
method), 80
save_ctrlpts_to_csv() (geomdl.BSpline.Curve2D
Surface (class in geomdl.BSpline), 44
method), 42
Surface (class in geomdl.NURBS), 62
save_ctrlpts_to_csv() (geomdl.BSpline.Surface method),
surfpt() (geomdl.BSpline.Surface method), 49
48
surfpt() (geomdl.NURBS.Surface method), 68
save_ctrlpts_to_csv() (geomdl.NURBS.Curve method),
surfpts (geomdl.BSpline.Surface attribute), 47
55
surfpts (geomdl.NURBS.Surface attribute), 68
Index 87
NURBS-Python Documentation, Release
T
tangent() (geomdl.BSpline.Curve method), 37
tangent() (geomdl.BSpline.Curve2D method), 43
tangent() (geomdl.BSpline.Surface method), 50
tangent() (geomdl.NURBS.Curve method), 56
tangent() (geomdl.NURBS.Curve2D method), 61
tangent() (geomdl.NURBS.Surface method), 69
tangents() (geomdl.BSpline.Curve method), 37
tangents() (geomdl.BSpline.Curve2D method), 43
tangents() (geomdl.BSpline.Surface method), 50
tangents() (geomdl.NURBS.Curve method), 56
tangents() (geomdl.NURBS.Curve2D method), 62
tangents() (geomdl.NURBS.Surface method), 69
translate() (geomdl.CPGen.Grid method), 74
translate() (geomdl.CPGen.GridWeighted method), 77
transpose() (geomdl.BSpline.Surface method), 49
transpose() (geomdl.NURBS.Surface method), 69
U
utilities (module), 70
V
vector_cross() (in module geomdl.utilities), 72
vector_dot() (in module geomdl.utilities), 72
vector_generate() (in module geomdl.utilities), 72
vector_normalize() (in module geomdl.utilities), 73
vis (geomdl.BSpline.Curve attribute), 35
vis (geomdl.BSpline.Curve2D attribute), 43
vis (geomdl.BSpline.Surface attribute), 47
vis (geomdl.NURBS.Curve attribute), 56
vis (geomdl.NURBS.Curve2D attribute), 62
vis (geomdl.NURBS.Surface attribute), 69
VisAbstract (class in geomdl.visualization.VisBase), 77
VisAbstractSurf (class in geomdl.visualization.VisBase),
77
VisBase (module), 77
VisCurve2D (class in geomdl.visualization.VisMPL), 78
VisCurve3D (class in geomdl.visualization.VisMPL), 78
VisMPL (module), 78
VisSurface (class in geomdl.visualization.VisMPL), 79
VisSurfScatter (class in geomdl.visualization.VisMPL),
80
VisSurfTriangle (class in geomdl.visualization.VisMPL),
80
VisSurfWireframe (class in ge-
omdl.visualization.VisMPL), 79
W
weights (geomdl.NURBS.Curve attribute), 52
weights (geomdl.NURBS.Curve2D attribute), 62
weights (geomdl.NURBS.Surface attribute), 63
88 Index