NURBS#

design3d.nurbs.core#

This module provides functions for working with Non-Uniform Rational B-Splines (NURBS) geometries. Some functions are based on the geomdl open-source library (MIT license), originally created by the geomdl authors: orbingol/NURBS-Python.

We also use as main reference Les Piegl and Wayne Tiller. The NURBS Book. Springer Science & Business Media, 1997

For more information on NURBs and their use in geometric modeling, please refer to the literature in the field. The authors of this module make no warranty or guarantee as to the accuracy or suitability of this code for any particular purpose.

design3d.nurbs.core.basis_function_all(degree, knot_vector, span, knot)#

Computes all non-zero basis functions of all degrees from 0 up to the input degree for a single parameter.

A slightly modified version of Algorithm A2.2 from The NURBS Book by Piegl & Tiller. Wrapper for helpers.basis_function() to compute multiple basis functions. Uses recurrence to compute the basis functions, also known as Cox - de Boor recursion formula.

For instance; if degree = 2, then this function will compute the basis function values of degrees 0, 1 and 2 for the knot value at the input knot span of the knot_vector.

Parameters:
  • degree (int) – degree, \(p\)

  • knot_vector (list, tuple) – knot vector, \(U\)

  • span (int) – knot span, \(i\)

  • knot (float) – knot or parameter, \(u\)

Returns:

basis functions

Return type:

list

design3d.nurbs.core.basis_function_ders_one(degree, knot_vector, span, knot, order)#

Computes the derivative of one basis functions for a single parameter.

Implementation of Algorithm A2.5 from The NURBS Book by Piegl & Tiller.

Parameters:
  • degree (int) – degree, \(p\)

  • knot_vector (list, tuple) – knot_vector, \(U\)

  • span (int) – knot span, \(i\)

  • knot (float) – knot or parameter, \(u\)

  • order (int) – order of the derivative

Returns:

basis function derivatives

Return type:

list

design3d.nurbs.core.basis_function_one(degree, knot_vector, span, knot)#

Computes the value of a basis function for a single parameter.

Implementation of Algorithm 2.4 from The NURBS Book by Piegl & Tiller.

Parameters:
  • degree (int) – degree, \(p\)

  • knot_vector (list, tuple) – knot vector

  • span (int) – knot span, \(i\)

  • knot (float) – knot or parameter, \(u\)

Returns:

basis function, \(N_{i,p}\)

Return type:

float

design3d.nurbs.core.basis_functions_ders(degree, knot_vector, spans, knots, order)#

Computes derivatives of the basis functions for a list of parameters.

Wrapper for helpers.basis_function_ders() to process multiple span and knot values.

Parameters:
  • degree (int) – degree, \(p\)

  • knot_vector (list, tuple) – knot vector, \(U\)

  • spans (list, tuple) – list of knot spans

  • knots (list, tuple) – list of knots or parameters

  • order (int) – order of the derivative

Returns:

derivatives of the basis functions

Return type:

list

design3d.nurbs.core.build_coeff_matrix(degree, knotvector, params, num_points)#

Builds the coefficient matrix for global interpolation.

This function only uses data points to build the coefficient matrix. Please refer to The NURBS Book (2nd Edition), pp364-370 for details.

Parameters:
  • degree (int) – degree

  • knotvector (list, tuple) – knot vector

  • params (list, tuple) – list of parameters

  • points (list, tuple) – data points

Returns:

coefficient matrix

Return type:

list

design3d.nurbs.core.derivatives_curve(datadict, parpos, deriv_order)#
design3d.nurbs.core.derivatives_surface(degree, knotvector, ctrlpts, size, rational, parpos, deriv_order)#
design3d.nurbs.core.evaluate_curve(datadict, start: float = 0.0, stop: float = 1.0)#
design3d.nurbs.core.evaluate_surface(datadict, **kwargs)#
design3d.nurbs.core.find_multiplicity(knot, knot_vector, **kwargs)#

Finds knot multiplicity over the knot vector.

Keyword Arguments:
  • tol: tolerance (delta) value for equality checking

Parameters:
  • knot (float) – knot or parameter, \(u\)

  • knot_vector (list, tuple) – knot vector, \(U\)

Returns:

knot multiplicity, \(s\)

Return type:

int

design3d.nurbs.core.find_span_binsearch(degree, knot_vector, num_ctrlpts, knot, **kwargs)#

Finds the span of the knot over the input knot vector using binary search.

Implementation of Algorithm A2.1 from The NURBS Book by Piegl & Tiller.

The NURBS Book states that the knot span index always starts from zero, i.e. for a knot vector [0, 0, 1, 1]; if FindSpan returns 1, then the knot is between the half-open interval [0, 1).

Parameters:
  • degree (int) – degree, \(p\)

  • knot_vector (list, tuple) – knot vector, \(U\)

  • num_ctrlpts (int) – number of control points, \(n + 1\)

  • knot (float) – knot or parameter, \(u\)

Returns:

knot span

Return type:

int

design3d.nurbs.core.find_span_linear(degree, knot_vector, num_ctrlpts, knot)#

Finds the span of a single knot over the knot vector using linear search.

Alternative implementation for the Algorithm A2.1 from The NURBS Book by Piegl & Tiller.

Parameters:
  • degree (int) – degree, \(p\)

  • knot_vector (list, tuple) – knot vector, \(U\)

  • num_ctrlpts (int) – number of control points, \(n + 1\)

  • knot (float) – knot or parameter, \(u\)

Returns:

knot span

Return type:

int

design3d.nurbs.core.fun(x, point3d, degree, knotvector, ctrlpts, size, rational)#
design3d.nurbs.core.point_inversion(point3d, x0, bounds, degree, knotvector, ctrlpts, size, rational)#

design3d.nurbs.helpers#

Helpers.

design3d.nurbs.helpers.backward_substitution(matrix_u, matrix_y)#

Backward substitution method for the solution of linear systems.

Solves the equation \(Ux = y\) using backward substitution method where \(U\) is a upper triangular matrix and \(y\) is a column matrix.

Parameters:
  • matrix_u (list, tuple) – U, upper triangular matrix

  • matrix_y (list, tuple) – y, column matrix

Returns:

x, column matrix

Return type:

list

design3d.nurbs.helpers.doolittle(matrix_a)#

Doolittle’s Method for LU-factorization.

Parameters:

matrix_a (list, tuple) – Input matrix (must be a square matrix)

Returns:

a tuple containing matrices (L,U)

Return type:

tuple

design3d.nurbs.helpers.forward_substitution(matrix_l, matrix_b)#

Forward substitution method for the solution of linear systems.

Solves the equation \(Ly = b\) using forward substitution method where \(L\) is a lower triangular matrix and \(b\) is a column matrix.

Parameters:
  • matrix_l (list, tuple) – L, lower triangular matrix

  • matrix_b (list, tuple) – b, column matrix

Returns:

y, column matrix

Return type:

list

design3d.nurbs.helpers.generate_knot_vector(degree, num_ctrlpts, **kwargs)#

Generates an equally spaced knot vector.

It uses the following equality to generate knot vector: \(m = n + p + 1\)

where;

  • \(p\), degree

  • \(n + 1\), number of control points

  • \(m + 1\), number of knots

Keyword Arguments:

  • clamped: Flag to choose from clamped or unclamped knot vector options. Default: True

Parameters:
  • degree (int) – degree

  • num_ctrlpts (int) – number of control points

Returns:

knot vector

Return type:

list

design3d.nurbs.helpers.insert_knots_and_mutiplicity(knots, knot_mutiplicities, knot_to_add, num)#

Compute knot-elements and multiplicities based on the global knot vector.

design3d.nurbs.helpers.lu_decomposition(matrix_a)#

LU-Factorization method using Doolittle’s Method for solution of linear systems.

Decomposes the matrix \(A\) such that \(A = LU\).

The input matrix is represented by a list or a tuple. The input matrix is 2-dimensional, i.e. list of lists of integers and/or floats.

Parameters:

matrix_a (list, tuple) – Input matrix (must be a square matrix)

Returns:

a tuple containing matrices L and U

Return type:

tuple

design3d.nurbs.helpers.round_c(num, digits=0)#
design3d.nurbs.helpers.standardize_knot_vector(knot_vector)#

Standardize a knot vector to range from 0 to 1.

design3d.nurbs.fitting#

Provides curve and surface fitting functions.

This module provides functions for working with Non-Uniform Rational B-Splines (NURBS) geometries. Some functions are based on the geomdl open-source library (MIT license), originally created by the geomdl authors: orbingol/NURBS-Python.

We also use as main reference Les Piegl and Wayne Tiller. The NURBS Book. Springer Science & Business Media, 1997

For more information on NURBs and their use in geometric modeling, please refer to the literature in the field. The authors of this module make no warranty or guarantee as to the accuracy or suitability of this code for any particular purpose.

design3d.nurbs.fitting.approximate_curve(points: np.ndarray, degree: cython.int, **kwargs)#

Curve approximation using least squares method with fixed number of control points.

Please refer to The NURBS Book (2nd Edition), pp.410-413 for details.

Keyword Arguments:
  • centripetal: activates centripetal parametrization method. Default: False

  • ctrlpts_size: number of control points. Default: len(points) - 1

Parameters:
  • points (list, tuple) – data points

  • degree (int) – degree of the output parametric curve

Returns:

approximated B-Spline curve

Return type:

BSpline.Curve

design3d.nurbs.fitting.approximate_surface(points: np.ndarray, size_u: cython.int, size_v: cython.int, degree_u: cython.int, degree_v: cython.int, **kwargs)#

Surface approximation using least squares method with fixed number of control points.

This algorithm interpolates the corner control points and approximates the remaining control points. Please refer to Algorithm A9.7 of The NURBS Book (2nd Edition), pp.422-423 for details.

Keyword Arguments:
  • centripetal: activates centripetal parametrization method. Default: False

  • ctrlpts_size_u: number of control points on the u-direction. Default: size_u - 1

  • ctrlpts_size_v: number of control points on the v-direction. Default: size_v - 1

Parameters:
  • points (list, tuple) – data points

  • size_u (int) – number of data points on the u-direction, \(r\)

  • size_v (int) – number of data points on the v-direction, \(s\)

  • degree_u (int) – degree of the output surface for the u-direction

  • degree_v (int) – degree of the output surface for the v-direction

Returns:

approximated B-Spline surface

Return type:

BSpline.Surface

design3d.nurbs.fitting.compute_params_curves(curves, centripetal: cython.bint = False)#

Computes ū_k for several curves having the same number of points, all at once.

Same computation as compute_params_curve, vectorized over the curves. The arithmetic is kept identical, so that the parameters are the same to the last bit:

  • a chord length is computed as sqrt(d.dot(d)), as np.linalg.norm does for a single vector. A batched np.matmul gives the same dot products, while np.linalg.norm(axis=...) or np.einsum can differ by one ulp;

  • the cumulated chord lengths are computed with np.sum on rows (pairwise summation), not with np.cumsum.

Parameters:
  • curves (np.ndarray) – data points of the curves, of shape (number of curves, number of points, dimension)

  • centripetal (bool) – activates centripetal parametrization method

Returns:

parameter arrays, of shape (number of curves, number of points)

Return type:

np.ndarray

design3d.nurbs.fitting.interpolate_curve(points: np.ndarray, degree: cython.int, centripetal: cython.bint)#

Curve interpolation through the data points.

Please refer to Algorithm A9.1 on The NURBS Book (2nd Edition), pp.369-370 for details.

Keyword Arguments:
  • centripetal: activates centripetal parametrization method. Default: False

Parameters:
  • points (list, tuple) – data points

  • degree (int) – degree of the output parametric curve

Returns:

interpolated B-Spline curve

Return type:

BSpline.Curve

design3d.nurbs.fitting.interpolate_surface(points: np.ndarray, size_u: cython.int, size_v: cython.int, degree_u: cython.int, degree_v: cython.int, **kwargs)#

Surface interpolation through the data points.

Please refer to the Algorithm A9.4 on The NURBS Book (2nd Edition), pp.380 for details.

Keyword Arguments:
  • centripetal: activates centripetal parametrization method. Default: False

Parameters:
  • points (list, tuple) – data points

  • size_u (int) – number of data points on the u-direction

  • size_v (int) – number of data points on the v-direction

  • degree_u (int) – degree of the output surface for the u-direction

  • degree_v (int) – degree of the output surface for the v-direction

Returns:

interpolated B-Spline surface

Return type:

BSpline.Surface

design3d.nurbs.operations#

Nurbs main operations algorithms.

design3d.nurbs.operations.construct_split_curve(obj, curve1_kv, curve2_kv, knot_span, insertion_count)#

Helper function to instantiate split curve.

design3d.nurbs.operations.construct_split_surfaces(obj, knotvectors, direction, knot_span, insertion_count)#

Helper function to construct split surfaces.

design3d.nurbs.operations.ctrlpts2d_to_ctrlpts(control_points_table)#

Transform the control_points m x n.

design3d.nurbs.operations.decompose_curve(obj, return_params: bool = False, number_max_patches: int = None, **kwargs)#

Generator: Decomposes the curve into Bézier curve segments of the same degree.

Parameters:
  • obj (BSplineCurve) – Curve to be decomposed

  • return_params (bool) – If True, returns the parameters from start and end of each Bézier patch with respect to the input curve.

  • number_max_patches – number max of patches, if limiting is needed.

Type:

int

Returns:

a generator element with a Bezier segment.

Return type:

Generator element.

design3d.nurbs.operations.decompose_surface(obj, return_params, **kwargs)#

Decomposes the surface into Bezier surface patches of the same degree.

Parameters:
  • obj (BSplineSurface3D) – surface

  • return_params (bool) – If True, returns the parameters from start and end of each Bézier patch with respect to the input curve.

Returns:

a list of Bezier patches

Return type:

list

design3d.nurbs.operations.extract_surface_curve_u(obj, param, curve_class, **kwargs)#

Extract an isocurve from the surface at the input parametric coordinate on the u-direction.

This method works by inserting knots to the surface so that from the new control points created we can extract the curve. Under the hood this method performs an incomplete process of splitting the surface.

Keyword Arguments:
  • find_span_func: FindSpan implementation. Default: helpers.find_span_linear()

Parameters:
Returns:

The bspline curve at the specified parameter

Return type:

design3d.surfaces.BSplineSurface3D

design3d.nurbs.operations.extract_surface_curve_v(obj, param, curve_class, **kwargs)#

Extract an isocurve from the surface at the input parametric coordinate on the v-direction.

This method works by inserting knots to the surface so that from the new control points created we can extract the curve. Under the hood this method performs an incomplete process of splitting the surface.

Keyword Arguments:
  • find_span_func: FindSpan implementation. Default: helpers.find_span_linear()

Parameters:
Returns:

The bspline curve at the specified parameter

Return type:

design3d.surfaces.BSplineSurface3D

design3d.nurbs.operations.flip_ctrlpts_u(ctrlpts, size_u, size_v)#

Flips a list of 1-dimensional control points from u-row order to v-row order.

u-row order: each row corresponds to a list of u values

v-row order: each row corresponds to a list of v values

Parameters:
  • ctrlpts (list, tuple) – control points in u-row order

  • size_u (int) – size in u-direction

  • size_v (int) – size in v-direction

Returns:

control points in v-row order

Return type:

list

design3d.nurbs.operations.get_knots_and_multiplicities(knotvector)#

Get knots and multiplicities from knotvector in u and v direction.

design3d.nurbs.operations.helper_decompose(srf, idx, split_func, return_params, other_direction_params=None, **kws)#

Helper function to decompose_surface.

design3d.nurbs.operations.helper_split_knot_vectors(degree, knotvector, num_ctrlpts, param, span_func)#

Computes knot vectors to split object into two pieces.

design3d.nurbs.operations.insert_control_points_surface_u(obj, param, num, **kwargs)#

Calculates the control points equivalent to inserts knot n-times to a spline geometry.

Keyword Arguments:
  • check_num: enables/disables operation validity checks. Default: True

Parameters:
  • obj – spline geometry

  • param (list, tuple) – knot to be inserted in u direction

  • num (list, tuple) – number of knot insertions

Returns:

a list with the control points

design3d.nurbs.operations.insert_control_points_surface_v(obj, param, num, **kwargs)#

Calculates the control points equivalent to inserts knot n-times to a spline geometry.

Keyword Arguments:
  • check_num: enables/disables operation validity checks. Default: True

Parameters:
  • obj – spline geometry

  • param (list, tuple) – knot to be inserted in v direction

  • num (list, tuple) – number of knot insertions

Returns:

a list with the control points

design3d.nurbs.operations.insert_knot_curve(obj, param, num, **kwargs)#

Inserts knots n-times to a spline geometry.

Keyword Arguments:
  • check_num: enables/disables operation validity checks. Default: True

Parameters:
  • obj – spline geometry

  • param (list, tuple) – knot(s) to be inserted in [u, v, w] format

  • num (list, tuple) – number of knot insertions in [num_u, num_v, num_w] format

Returns:

updated spline geometry

design3d.nurbs.operations.insert_knot_surface(obj, param, num, **kwargs)#

Inserts knots n-times to a spline geometry.

Keyword Arguments:
  • check_num: enables/disables operation validity checks. Default: True

Parameters:
  • obj – spline geometry

  • param (list, tuple) – knot(s) to be inserted in [u, v] format

  • num (list, tuple) – number of knot insertions in [num_u, num_v] format

Returns:

updated spline geometry

design3d.nurbs.operations.knot_insertion(degree, knotvector, ctrlpts, u, **kwargs)#

Computes the control points of the rational/non-rational spline after knot insertion.

Part of Algorithm A5.1 of The NURBS Book by Piegl & Tiller, 2nd Edition.

Keyword Arguments:
  • num: number of knot insertions. Default: 1

  • s: multiplicity of the knot. Default: computed via :func:`.find_multiplicity`

  • span: knot span. Default: computed via :func:`.find_span_linear`

Parameters:
  • degree (int) – degree

  • knotvector (list, tuple) – knot vector

  • ctrlpts (list) – control points

  • u (float) – knot to be inserted

Returns:

updated control points

Return type:

list

design3d.nurbs.operations.knot_insertion_alpha(u, knotvector, span, idx, leg)#

Computes \(\alpha\) coefficient for knot insertion algorithm.

Parameters:
  • u (float) – knot

  • knotvector (tuple) – knot vector

  • span (int) – knot span

  • idx (int) – index value (degree-dependent)

  • leg (int) – i-th leg of the control points polygon

Returns:

coefficient value

Return type:

float

design3d.nurbs.operations.knot_insertion_kv(knotvector, u, span, num_insertions)#

Computes the knot vector of the rational/non-rational spline after knot insertion.

Part of Algorithm A5.1 of The NURBS Book by Piegl & Tiller, 2nd Edition.

Parameters:
  • knotvector (list, tuple) – knot vector

  • u (float) – knot

  • span (int) – knot span

  • num_insertions (int) – number of knot insertions

Returns:

updated knot vector

Return type:

list

Links the input curves together.

The end control point of the curve k has to be the same with the start control point of the curve k + 1.

Returns:

a tuple containing: knots, knots multiplicities, control points and weights vector

design3d.nurbs.operations.refine_knot_vector_surface(obj, u, v, **kwargs)#

Splits the surface at the input parametric coordinate on the u-direction.

This method splits the surface into two pieces at the given parametric coordinate on the u-direction, generates two different surface objects and returns them. It does not modify the input surface.

Keyword Arguments:
  • find_span_func: FindSpan implementation. Default: helpers.find_span_linear()

  • insert_knot_func: knot insertion algorithm implementation. Default: operations.insert_knot()

Parameters:
  • obj (abstract.Surface) – surface

  • param (float) – parameter for the u-direction

Returns:

a list of surface patches

Return type:

list

design3d.nurbs.operations.separate_ctrlpts_weights(ctrlptsw)#

Divides weighted control points by weights to generate unweighted control points and weights vector.

This function is dimension agnostic, i.e. control points can be in any dimension but the last element of the array should indicate the weight.

Parameters:

ctrlptsw (list, tuple) – weighted control points

Returns:

unweighted control points and weights vector

Return type:

list

design3d.nurbs.operations.split_curve(obj, param, **kwargs)#

Splits the curve at the input parametric coordinate.

This method splits the curve into two pieces at the given parametric coordinate, generates two different curve objects and returns them. It does not modify the input curve.

Keyword Arguments:
  • find_span_func: FindSpan implementation. Default: helpers.find_span_linear()

  • insert_knot_func: knot insertion algorithm implementation. Default: operations.insert_knot()

Parameters:
  • obj (abstract.Curve) – Curve to be split

  • param (float) – parameter

Returns:

a list of curve segments

Return type:

list

design3d.nurbs.operations.split_surface_u(obj, param, **kwargs)#

Splits the surface at the input parametric coordinate on the u-direction.

This method splits the surface into two pieces at the given parametric coordinate on the u-direction, generates two different surface objects and returns them. It does not modify the input surface.

Keyword Arguments:
  • find_span_func: FindSpan implementation. Default: helpers.find_span_linear()

  • insert_knot_func: knot insertion algorithm implementation. Default: operations.insert_knot()

Parameters:
  • obj (abstract.Surface) – surface

  • param (float) – parameter for the u-direction

Returns:

a list of surface patches

Return type:

list

design3d.nurbs.operations.split_surface_v(obj, param, **kwargs)#

Splits the surface at the input parametric coordinate on the v-direction.

This method splits the surface into two pieces at the given parametric coordinate on the v-direction, generates two different surface objects and returns them. It does not modify the input surface.

Keyword Arguments:
  • find_span_func: FindSpan implementation. Default: helpers.find_span_linear()

  • insert_knot_func: knot insertion algorithm implementation. Default: operations.insert_knot()

Parameters:
  • obj (abstract.Surface) – surface

  • param (float) – parameter for the v-direction

Returns:

a list of surface patches

Return type:

list