Skip to main content

A lightweight library for NURBS curves and surfaces

Project description

nurbspy

Description

nurbspy is a Python package for Non-Uniform Rational Basis Spline (NURBS) curves and surfaces. The classes and methods were inspired by the algorithms presented in The NURBS Book and the code was implemented using vectorized Numpy functions and Numba's just-in-time compilation decorators to achieve C-like speed.

nurbspy aims to be a simple NURBS library, not a fully fledged CAD kernel. If you need a powerful, open source CAD kernel we recommend you to check out the C++ OpenCascade library. If you feel that OpenCascade is too complex or you are not sure how to start using it, this repository might be useful for you!

     

Capabilities

nurbspy has the following features for NURBS curves:

  • Constructors for rational and non-rational Bézier and B-Spline curves
  • Methods to evaluate curve coordinates
  • Methods to evaluate arbitrary-order derivatives analytically
  • Methods to evaluate the tangent, normal, and binormal unitary vectors (Frenet-Serret frame of reference)
  • Methods to compute the curvature and torsion
  • Methods to compute the arc-length of the curve by numerical quadrature
  • Methods to for point projection / point inversion
  • Methods to visualize the curve using the Matplotlib library

In addition, nurbspy provides the following capabilities for NURBS surfaces:

  • Constructors for rational and non-rational Bézier and B-Spline surfaces
  • Additional constructors for some common special surfaces:
    • Bilinear surfaces
    • Ruled surfaces
    • Extruded surfaces
    • Revolution surfaces
    • Coons surfaces
  • Methods to evaluate surface coordinates
  • Methods to evaluate arbitrary-order derivatives analytically
  • Methods to evaluate the unitary normal vectors
  • Methods to evaluate the mean and Gaussian curvatures
  • Methods to compute u- and v-isoparametic curves
  • Methods to for point projection / point inversion
  • Methods to visualize the surface using the Matplotlib library

In addition, nurbspy can work with real and complex data types natively. This allows to compute accurate (down to machine epsilon!) shape derivatives using the complex step method and avoid the numerical error incurred by finite-difference derivative approximations. This shape sensitivity information is necessary to solve shape optimization problems with many design variables using gradient based-optimization algorithms. To our knowledge, nurbspy is the only Python package that provides the flexibility to work with complex numbers right away.

Installation

nurbspy has the following mandatory runtime dependencies:

  • numpy (multidimensional array library)
  • scipy (scientific computing library)
  • numba (just-in-time Python compiler)
  • pygmo (optimization library)
  • matplotlib (visualization library)

In addition, nurbspy uses pytest for local tests.

nurbspy is available on Linux via the pip package manager. The installation with pip is straightfoward:

pip install nurbspy

nurbspy is also available on Linux, Windows, and macOS via the conda installer. In order to install nurbspy via conda you need to add conda-forge and roberagro to the list of available channels:

conda install nurbspy --channel conda-forge --channel roberagro

You can verify that nurbspy was installed successfully with this minimal Python script:

# Nurbspy minimal working example
import nurbspy
nurbspy.minimal_example.run()

or by typing this one-liner on your terminal:

python3 -c "import nurbspy; nurbspy.minimal_example.run()"

Minimal working examples

NURBS curves

nurbspy can be used to create Bézier, B-Spline and NURBS curves. The type of curve depends on the arguments used to initialize the curve class. As an example, the following piece of code can be used to generate a degree four Bézier curve in two dimensions:

# Import packages
import numpy as np
import nurbspy as nrb
import matplotlib.pyplot as plt

# Define the array of control points
P = np.zeros((2,5))
P[:, 0] = [0.20, 0.50]
P[:, 1] = [0.40, 0.70]
P[:, 2] = [0.80, 0.60]
P[:, 3] = [0.80, 0.40]
P[:, 4] = [0.40, 0.20]

# Create and plot the Bezier curve
bezierCurve = nrb.NurbsCurve(control_points=P)
bezierCurve.plot()
plt.show()

If the installation was succesful, you should be able to see the Bézier curve when you execute the previous code snippet.

Check out the curve demos directory to see more examples showing the capabilities of the library and how to use them.

NURBS surfaces

Similarly, nurbspy can be used to create Bézier, B-Spline and NURBS surfaces. The type of surface depends on the arguments used to initialize the surface class. As an example, the following code snippet can be used to generate a simple Bézier surface of degree 3 in the u-direction and degree 2 in the v-direction:

# Import packages
import numpy as np
import nurbspy as nrb
import matplotlib.pyplot as plt

# Define the array of control points
n_dim, n, m = 3, 4, 3
P = np.zeros((n_dim, n, m))

# First row
P[:, 0, 0] = [0.00, 0.00, 0.00]
P[:, 1, 0] = [1.00, 0.00, 1.00]
P[:, 2, 0] = [2.00, 0.00, 1.00]
P[:, 3, 0] = [3.00, 0.00, 0.00]

# Second row
P[:, 0, 1] = [0.00, 1.00, 1.00]
P[:, 1, 1] = [1.00, 1.00, 2.00]
P[:, 2, 1] = [2.00, 1.00, 2.00]
P[:, 3, 1] = [3.00, 1.00, 1.00]

# Third row
P[:, 0, 2] = [0.00, 2.00, 0.00]
P[:, 1, 2] = [1.00, 2.00, 1.00]
P[:, 2, 2] = [2.00, 2.00, 1.00]
P[:, 3, 2] = [3.00, 2.00, 0.00]

# Create and plot the Bezier surface
bezierSurface = nrb.NurbsSurface(control_points=P)
bezierSurface.plot(control_points=True, isocurves_u=6, isocurves_v=6)
plt.show()

If the installation was successful, you should be able to see the Bézier surface when you execute the previous script.

Check out the surface demos directory to see more examples showing the capabilities of the library and how to use them.

Mathematical background

Check out the Bézier, B-Spline, and NURBS notes if you want to learn more about the definition and mathematical properties of these curves and surfaces

Contact information

nurbspy was developed by Roberto Agromayor under the supervision of Associate Professor Lars O. Nord at the Norwegian University of Science and Technology (NTNU) as part of his PhD on turbomachinery shape optimization. Please, drop us an email to roberto.agromayor@ntnu.no if you have questions about the code or you have a bug to report!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nurbspy-1.1.0.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

nurbspy-1.1.0-py3-none-any.whl (35.2 kB view details)

Uploaded Python 3

File details

Details for the file nurbspy-1.1.0.tar.gz.

File metadata

  • Download URL: nurbspy-1.1.0.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for nurbspy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 32d0f41ced4adc30bd1f9976700eaa0f4ca2578dba858105dc7a1c15fb18f4f0
MD5 4ddc5c492e3a089d3b8cf2b59e586584
BLAKE2b-256 f536f67951ca20ff7171d6c7664f2c5ddc13409ef2d493a54eec5f25bf37435d

See more details on using hashes here.

File details

Details for the file nurbspy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: nurbspy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 35.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for nurbspy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2fa966529a2567a186068899ad2425653102966dadaccaacc787e753c228b227
MD5 8db3cf43d9f35f60c3f05efaf70ebdc6
BLAKE2b-256 94dba7e9e01995d5b4c0645b1a66e01f0dd4f4b73d8ad08d14eadbb43a935dc3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page