nurbspy.nurbs_surface module#

class nurbspy.nurbs_surface.NurbsSurface(control_points=None, weights=None, u_degree=None, v_degree=None, u_knots=None, v_knots=None)[source]#

Bases: object

Create a NURBS (Non-Uniform Rational Basis Spline) surface object

Parameters:
control_pointsndarray with shape (ndim, n+1, m+1)

Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (any number of dimensions) The second dimension of P spans the u-direction control points (0, 1, …, n) The third dimension of P spans the v-direction control points (0, 1, …, m)

weightsndarray with shape (n+1, m+1)

Array containing the weight of the control points The first dimension of W spans the u-direction control points weights (0, 1, …, n) The second dimension of W spans the v-direction control points weights (0, 1, …, m)

u_degreeint

Degree of the u-basis polynomials

v_degreeint

Degree of the v-basis polynomials

u_knotsndarray with shape (r+1=n+p+2,)

Knot vector in the u-direction Set the multiplicity of the first and last entries equal to p+1 to obtain a clamped spline

v_knotsndarray with shape (s+1=m+q+2,)

Knot vector in the v-direction Set the multiplicity of the first and last entries equal to q+1 to obtain a clamped spline

Methods

attach_nurbs_udir(new_nurbs)

Attatch a new NURBS curve to the end of the instance NURBS curve and return the merged NURBS curve

attach_nurbs_vdir(new_nurbs)

Attatch a new NURBS curve to the end of the instance NURBS curve and return the merged NURBS curve

compute_bspline_coordinates(P, p, q, U, V, u, v)

Evaluate the coordinates of the B-Spline surface corresponding to the (u,v) parametrization

compute_bspline_derivatives(P, p, q, U, V, ...)

Compute the derivatives of a B-Spline (or NURBS surface in homogeneous space) up to orders derivative_order_u and derivative_order_v

compute_nurbs_coordinates(P, W, p, q, U, V, u, v)

Evaluate the coordinates of the NURBS surface corresponding to the (u,v) parametrization

compute_nurbs_derivatives(P, W, p, q, U, V, ...)

Compute the derivatives of a NURBS surface in ordinary space up to to the desired orders

get_curvature(u, v)

Evaluate the mean and gaussian curvatures of the surface the input (u,v) parametrization

get_derivative(u, v, order_u, order_v)

Evaluate the derivative of the surface for the input u-parametrization

get_isocurve_u(u0)

Create a NURBS curve object that contains the surface isoparametric curve S(u0,v)

get_isocurve_v(v0)

Create a NURBS curve object that contains the surface isoparametric curve S(u,v0)

get_normals(u, v)

Evaluate the unitary vectors normal to the surface the input (u,v) parametrization

get_value(u, v)

Evaluate the coordinates of the surface corresponding to the (u,v) parametrization

plot([fig, ax, surface, surface_color, ...])

plot_boundary(fig, ax[, color, linewidth, ...])

Plot the isoparametric curves at the boundary

plot_control_points(fig, ax[, color, ...])

Plot the control points

plot_isocurve_u(fig, ax, u_values[, color, ...])

Plot isoparametric curves in the u-direction

plot_isocurve_v(fig, ax, v_values[, color, ...])

Plot isoparametric curves in the v-direction

plot_normals(fig, ax[, number_u, number_v, ...])

Plot the normal vectors

project_point_to_surface(P)

Solve the point projection problem for the prescribed point P

rescale_plot(fig, ax)

Adjust the aspect ratio of the figure

PointToSurfaceProjectionProblem

plot_curvature

plot_surface

Notes

This class includes methods to compute:

  • Surface coordinates for any number of dimensions

  • Analytic surface partial derivatives of any order and number of dimensions

  • Analytic mean and gaussian curvatures

  • Isoparametric curves in the u- and v-directions

The class can be used to represent polynomial and rational Bézier, B-Spline and NURBS surfaces The type of surface depends on the initialization arguments

  • Polymnomial Bézier: Provide the array of control points

  • Rational Bézier: Provide the arrays of control points and weights

  • B-Spline: Provide the array of control points, (u,v) degrees and (u,v) knot vectors

  • NURBS: Provide the arrays of control points and weights, (u,v) degrees and (u,v) knot vectors

In addition, this class supports operations with real and complex numbers The data type used for the computations is detected from the data type of the arguments Using complex numbers can be useful to compute the derivative of the shape using the complex step method

References

The NURBS Book. See references to equations and algorithms throughout the code L. Piegl and W. Tiller Springer, second edition

Curves and Surfaces for CADGD. See references to equations the source code G. Farin Morgan Kaufmann Publishers, fifth edition

All references correspond to The NURBS book unless it is explicitly stated that they come from Farin’s book

class PointToSurfaceProjectionProblem(S, dS, P)[source]#

Bases: object

Methods

fitness(x)

Evaluate the deviation between the prescribed point and the parametrized point

get_bounds()

Set the bounds for the optimization problem

gradient(x)

Compute the gradient of the fitness function analytically

fitness(x)[source]#

Evaluate the deviation between the prescribed point and the parametrized point

static get_bounds()[source]#

Set the bounds for the optimization problem

gradient(x)[source]#

Compute the gradient of the fitness function analytically

attach_nurbs_udir(new_nurbs)[source]#

Attatch a new NURBS curve to the end of the instance NURBS curve and return the merged NURBS curve

attach_nurbs_vdir(new_nurbs)[source]#

Attatch a new NURBS curve to the end of the instance NURBS curve and return the merged NURBS curve

static compute_bspline_coordinates(P, p, q, U, V, u, v)[source]#

Evaluate the coordinates of the B-Spline surface corresponding to the (u,v) parametrization

This function computes the coordinates of a B-Spline surface as given by equation 3.11. See algorithm A3.5

Parameters:
Pndarray with shape (ndim, n+1, m+1)

Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (any number of dimensions) The second dimension of P spans the u-direction control points (0, 1, …, n) The third dimension of P spans the v-direction control points (0, 1, …, m)

pint

Degree of the u-basis polynomials

qint

Degree of the v-basis polynomials

Undarray with shape (r+1=n+p+2,)

Knot vector in the u-direction Set the multiplicity of the first and last entries equal to p+1 to obtain a clamped spline

Vndarray with shape (s+1=m+q+2,)

Knot vector in the v-direction Set the multiplicity of the first and last entries equal to q+1 to obtain a clamped spline

uscalar or ndarray with shape (N,)

u-parameter used to evaluate the surface

vscalar or ndarray with shape (N,)

v-parameter used to evaluate the surface

Returns:
Sndarray with shape (ndim, N)

Array containing the NURBS surface coordinates The first dimension of S spans the (x,y,z) coordinates The second dimension of S spans the (u,v) parametrization sample points

static compute_bspline_derivatives(P, p, q, U, V, u, v, up_to_order_u, up_to_order_v)[source]#

Compute the derivatives of a B-Spline (or NURBS surface in homogeneous space) up to orders derivative_order_u and derivative_order_v

This function computes the analytic derivatives of a B-Spline surface using equation 3.17. See algorithm A3.6

Parameters:
Pndarray with shape (ndim, n+1, m+1)

Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (any number of dimensions) The second dimension of P spans the u-direction control points (0, 1, …, n) The third dimension of P spans the v-direction control points (0, 1, …, m)

pint

Degree of the u-basis polynomials

qint

Degree of the v-basis polynomials

Undarray with shape (r+1=n+p+2,)

Knot vector in the u-direction Set the multiplicity of the first and last entries equal to p+1 to obtain a clamped spline

Vndarray with shape (s+1=m+q+2,)

Knot vector in the v-direction Set the multiplicity of the first and last entries equal to q+1 to obtain a clamped spline

uscalar or ndarray with shape (N,)

u-parameter used to evaluate the surface

vscalar or ndarray with shape (N,)

v-parameter used to evaluate the surface

up_to_order_uinteger

Order of the highest derivative in the u-direction

up_to_order_vinteger

Order of the highest derivative in the v-direction

Returns:
bspline_derivatives: ndarray of shape (up_to_order_u+1, up_to_order_v+1, ndim, Nu)

The first dimension spans the order of the u-derivatives (0, 1, 2, …) The second dimension spans the order of the v-derivatives (0, 1, 2, …) The third dimension spans the coordinates (x,y,z,…) The fourth dimension spans (u,v) parametrization sample points

static compute_nurbs_coordinates(P, W, p, q, U, V, u, v)[source]#

Evaluate the coordinates of the NURBS surface corresponding to the (u,v) parametrization

This function computes the coordinates of the NURBS surface in homogeneous space using equation 4.15 and then maps the coordinates to ordinary space using the perspective map given by equation 1.16. See algorithm A4.3

Parameters:
Pndarray with shape (ndim, n+1, m+1)

Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (any number of dimensions) The second dimension of P spans the u-direction control points (0, 1, …, n) The third dimension of P spans the v-direction control points (0, 1, …, m)

Wndarray with shape (n+1, m+1)

Array containing the weight of the control points The first dimension of W spans the u-direction control points weights (0, 1, …, n) The second dimension of W spans the v-direction control points weights (0, 1, …, m)

pint

Degree of the u-basis polynomials

qint

Degree of the v-basis polynomials

Undarray with shape (r+1=n+p+2,)

Knot vector in the u-direction Set the multiplicity of the first and last entries equal to p+1 to obtain a clamped spline

Vndarray with shape (s+1=m+q+2,)

Knot vector in the v-direction Set the multiplicity of the first and last entries equal to q+1 to obtain a clamped spline

uscalar or ndarray with shape (N,)

u-parameter used to evaluate the surface

vscalar or ndarray with shape (N,)

v-parameter used to evaluate the surface

Returns:
Sndarray with shape (ndim, N)

Array containing the NURBS surface coordinates The first dimension of S spans the (x,y,z) coordinates The second dimension of S spans the (u,v) parametrization sample points

compute_nurbs_derivatives(P, W, p, q, U, V, u, v, up_to_order_u, up_to_order_v)[source]#

Compute the derivatives of a NURBS surface in ordinary space up to to the desired orders

This function computes the analytic derivatives of the NURBS surface in ordinary space using equation 4.20 and the derivatives of the NURBS surface in homogeneous space obtained from compute_bspline_derivatives()

The derivatives are computed recursively in a fashion similar to algorithm A4.4

Parameters:
Pndarray with shape (ndim, n+1, m+1)

Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (any number of dimensions) The second dimension of P spans the u-direction control points (0, 1, …, n) The third dimension of P spans the v-direction control points (0, 1, …, m)

Wndarray with shape (n+1, m+1)

Array containing the weight of the control points The first dimension of W spans the u-direction control points weights (0, 1, …, n) The second dimension of W spans the v-direction control points weights (0, 1, …, m)

pint

Degree of the u-basis polynomials

qint

Degree of the v-basis polynomials

Undarray with shape (r+1=n+p+2,)

Knot vector in the u-direction Set the multiplicity of the first and last entries equal to p+1 to obtain a clamped spline

Vndarray with shape (s+1=m+q+2,)

Knot vector in the v-direction Set the multiplicity of the first and last entries equal to q+1 to obtain a clamped spline

uscalar or ndarray with shape (N,)

u-parameter used to evaluate the surface

vscalar or ndarray with shape (N,)

v-parameter used to evaluate the surface

up_to_order_uinteger

Order of the highest derivative in the u-direction

up_to_order_vinteger

Order of the highest derivative in the v-direction

Returns:
nurbs_derivatives: ndarray of shape (up_to_order_u+1, up_to_order_v+1, ndim, Nu)

The first dimension spans the order of the u-derivatives (0, 1, 2, …) The second dimension spans the order of the v-derivatives (0, 1, 2, …) The third dimension spans the coordinates (x,y,z,…) The fourth dimension spans (u,v) parametrization sample points

get_curvature(u, v)[source]#

Evaluate the mean and gaussian curvatures of the surface the input (u,v) parametrization

The definition of the gaussian and mean curvatures are given by equations 19.11 and 19.12 (Farin’s textbook)

Parameters:
uscalar or ndarray with shape (N,)

Scalar or array containing the u-parameter used to evaluate the curvatures

vscalar or ndarray with shape (N,)

Scalar or array containing the v-parameter used to evaluate the curvatures

Returns:
mean_curvaturendarray with shape (N, )

Scalar or array containing the mean curvature of the surface

gaussian_curvaturendarray with shape (N, )

Scalar or array containing the gaussian curvature of the surface

get_derivative(u, v, order_u, order_v)[source]#

Evaluate the derivative of the surface for the input u-parametrization

Parameters:
uscalar or ndarray with shape (N,)

u-parameter used to evaluate the surface

vscalar or ndarray with shape (N,)

v-parameter used to evaluate the surface

order_uint

Order of the partial derivative in the u-direction

order_vint

Order of the partial derivative in the v-direction

Returns:
dSndarray with shape (ndim, N)

Array containing the derivative of the desired order The first dimension of dC spans the (x,y,z) coordinates The second dimension of dC spans the u parametrization sample points

get_isocurve_u(u0)[source]#

Create a NURBS curve object that contains the surface isoparametric curve S(u0,v)

The isoparametric nurbs curve is defined by equations 4.16 and 4.18

Parameters:
u0scalar

Scalar defining the u-parameter of the isoparametric curve

Returns:
isocurve_uinstance of NurbsCurve class

Object defining the isoparametric curve

get_isocurve_v(v0)[source]#

Create a NURBS curve object that contains the surface isoparametric curve S(u,v0)

The isoparametric nurbs curve is defined by equations 4.17 and 4.18

Parameters:
v0scalar

Scalar defining the v-parameter of the isoparametric curve

Returns:
isocurve_vinstance of NurbsCurve class

Object defining the isoparametric curve

get_normals(u, v)[source]#

Evaluate the unitary vectors normal to the surface the input (u,v) parametrization

The definition of the unitary normal vector is given in section 19.2 (Farin’s textbook)

Parameters:
uscalar or ndarray with shape (N,)

Scalar or array containing the u-parameter used to evaluate the normals

vscalar or ndarray with shape (N,)

Scalar or array containing the v-parameter used to evaluate the normals

Returns:
normalsndarray with shape (ndim, N)

Array containing the unitary vectors normal to the surface

get_value(u, v)[source]#

Evaluate the coordinates of the surface corresponding to the (u,v) parametrization

Parameters:
uscalar or ndarray with shape (N,)

u-parameter used to evaluate the surface

vscalar or ndarray with shape (N,)

v-parameter used to evaluate the surface

Returns:
Sndarray with shape (ndim, N)

Array containing the coordinates of the surface The first dimension of S spans the (x,y,z) coordinates The second dimension of S spans the (u,v) parametrization sample points

plot(fig=None, ax=None, surface=True, surface_color='blue', colorbar=False, boundary=True, control_points=False, normals=False, axis_off=False, ticks_off=False, Nu=50, Nv=50, isocurves_u=None, isocurves_v=None)[source]#
plot_boundary(fig, ax, color='black', linewidth=1.0, linestyle='-', south=True, north=True, east=True, west=True)[source]#

Plot the isoparametric curves at the boundary

plot_control_points(fig, ax, color='red', linewidth=1.0, linestyle='-', markersize=5, markerstyle='o')[source]#

Plot the control points

plot_curvature(fig=None, ax=None, curvature_type='mean')[source]#
plot_isocurve_u(fig, ax, u_values, color='black', linewidth=1.0, linestyle='-')[source]#

Plot isoparametric curves in the u-direction

plot_isocurve_v(fig, ax, v_values, color='black', linewidth=1.0, linestyle='-')[source]#

Plot isoparametric curves in the v-direction

plot_normals(fig, ax, number_u=10, number_v=10, scale=0.075)[source]#

Plot the normal vectors

plot_surface(fig, ax, color='blue', alpha=0.3, colorbar=False, Nu=50, Nv=50)[source]#
project_point_to_surface(P)[source]#

Solve the point projection problem for the prescribed point P

rescale_plot(fig, ax)[source]#

Adjust the aspect ratio of the figure

nurbspy.nurbs_surface.make_offset_surface(surface, offset)[source]#