nurbspy.nurbs_curve module#
- class nurbspy.nurbs_curve.NurbsCurve(control_points=None, weights=None, degree=None, knots=None)[source]#
Bases:
objectCreate a NURBS (Non-Uniform Rational Basis Spline) curve object
- Parameters:
- control_pointsndarray with shape (ndim, n+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)
- weightsndarray with shape (n+1,)
Array containing the weight of the control points
- degreeint
Degree of the basis polynomials
- knotsndarray with shape (r+1=n+p+2,)
The knot vector in the u-direction Set the multiplicity of the first and last entries equal to p+1 to obtain a clamped NURBS
Methods
compute_bspline_coordinates(P, p, U, u)Evaluate the coordinates of a B-Spline curve for the input u parametrization
compute_bspline_derivatives(P, p, U, u, ...)Compute the derivatives of a B-Spline (or NURBS curve in homogeneous space) up to order derivative_order
compute_nurbs_coordinates(P, W, p, U, u)Evaluate the coordinates of a NURBS curve for the input u parametrization
compute_nurbs_derivatives(P, W, p, U, u, ...)Compute the derivatives of a NURBS curve in ordinary space up to the desired order
get_arclength([u1, u2])Compute the arc length of a parametric curve in the interval [u1,u2] using numerical quadrature
get_binormal(u)Evaluate the unitary binormal vector to the curve for the input u-parametrization
Evaluate the curvature of the curve for the input u-parametrization
get_derivative(u, order)Evaluate the derivative of the curve for the input u-parametrization
get_normal(u)Evaluate the unitary normal vector to the curve for the input u-parametrization
Evaluate the the unitary normal vector using the special formula 2D formula
get_tangent(u)Evaluate the unitary tangent vector to the curve for the input u-parametrization
get_torsion(u)Evaluate the torsion of the curve for the input u-parametrization
get_value(u)Evaluate the coordinates of the curve for the input u parametrization
plot([fig, ax, curve, control_points, ...])Create a plot and return the figure and axes handles
plot_control_points(fig, ax[, linewidth, ...])Plot the control points of the NURBS curve
plot_curve(fig, ax[, linewidth, linestyle, ...])Plot the coordinates of the NURBS curve
plot_frenet_serret(fig, ax[, frame_number, ...])Plot some Frenet-Serret reference frames along the NURBS curve
Solve the point projection problem for the prescribed point P
rescale_plot(fig, ax)Adjust the aspect ratio of the figure
PointToCurveProjectionProblem
plot_curvature
plot_torsion
Notes
This class includes methods to compute:
Curve coordinates for any number of dimensions
Analytic curve derivatives of any order and number of dimensions
The unitary tangent, normal and binormal vectors (Frenet-Serret reference frame) in 2D and 3D
The analytic curvature and torsion in 2D and 3D
- The arc length of the curve in any number of dimensions.
The arc length is compute by numerical quadrature using analytic derivative information
The class can be used to represent polynomial and rational Bézier, B-Spline and NURBS curves The type of curve 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, degree and knot vector
NURBS: Provide the arrays of control points and weights, degree and knot vector
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
- static compute_bspline_coordinates(P, p, U, u)[source]#
Evaluate the coordinates of a B-Spline curve for the input u parametrization
This function computes the coordinates of a B-Spline curve as given by equation 3.1. See algorithm A3.1
- Parameters:
- Pndarray with shape (ndim, n+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)
- pint
Degree of the B-Spline 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
- uscalar or ndarray with shape (N,)
Parameter used to evaluate the curve
- Returns:
- Cndarray with shape (ndim, N)
Array containing the coordinates of the curve The first dimension of C spans the (x,y,z) coordinates The second dimension of C spans the u parametrization sample points
- static compute_bspline_derivatives(P, p, U, u, up_to_order)[source]#
Compute the derivatives of a B-Spline (or NURBS curve in homogeneous space) up to order derivative_order
This function computes the analytic derivatives of a B-Spline curve using equation 3.3. See algorithm A3.2
- Parameters:
- Pndarray with shape (ndim, n+1)
Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (x,y,z) The second dimension of P spans the u-direction control points (0,1,…,n)
- pint
Degree of the B-Spline 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
- uscalar or ndarray with shape (N,)
Parameter used to evaluate the curve
- up_to_orderinteger
Order of the highest derivative
- Returns:
- bspline_derivatives: ndarray of shape (up_to_order+1, ndim, Nu)
The first dimension spans the order of the derivatives (0, 1, 2, …) The second dimension spans the coordinates (x,y,z,…) The third dimension spans u-parametrization sample points
- static compute_nurbs_coordinates(P, W, p, U, u)[source]#
Evaluate the coordinates of a NURBS curve for the input u parametrization
This function computes the coordinates of the NURBS curve in homogeneous space using equation 4.5 and then maps the coordinates to ordinary space using the perspective map given by equation 1.16. See algorithm A4.1
- Parameters:
- Pndarray with shape (ndim, n+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)
- Wndarray with shape (n+1,)
Array containing the weight of the control points
- pint
Degree of the B-Spline 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
- uscalar or ndarray with shape (N,)
Parameter used to evaluate the curve
- Returns:
- Cndarray with shape (ndim, N)
Array containing the coordinates of the curve The first dimension of C spans the (x,y,z) coordinates The second dimension of C spans the u parametrization sample points
- compute_nurbs_derivatives(P, W, p, U, u, up_to_order)[source]#
Compute the derivatives of a NURBS curve in ordinary space up to the desired order
This function computes the analytic derivatives of the NURBS curve in ordinary space using equation 4.8 and the derivatives of the NURBS curve in homogeneous space obtained from compute_bspline_derivatives()
The derivatives are computed recursively in a fashion similar to algorithm A4.2
- Parameters:
- Pndarray with shape (ndim, n+1)
Array containing the coordinates of the control points The first dimension of P spans the coordinates of the control points (x,y,z) The second dimension of P spans the u-direction control points (0,1,…,n)
- Wndarray with shape (n+1,)
Array containing the weight of the control points
- pint
Degree of the B-Spline 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
- uscalar or ndarray with shape (N,)
Parameter used to evaluate the curve
- up_to_orderinteger
Order of the highest derivative
- Returns:
- nurbs_derivatives: ndarray of shape (up_to_order+1, ndim, Nu)
The first dimension spans the order of the derivatives (0, 1, 2, …) The second dimension spans the coordinates (x,y,z,…) The third dimension spans u-parametrization sample points
- get_arclength(u1=0.0, u2=1.0)[source]#
Compute the arc length of a parametric curve in the interval [u1,u2] using numerical quadrature
The definition of the arc length is given by equation 10.3 (Farin’s textbook)
- Parameters:
- u1scalar
Lower limit of integration for the arc length computation
- u2scalar
Upper limit of integration for the arc length computation
- Returns:
- Lscalar
Arc length of NURBS curve in the interval [u1, u2]
- get_binormal(u)[source]#
Evaluate the unitary binormal vector to the curve for the input u-parametrization
The definition of the unitary binormal vector is given by equation 10.5 (Farin’s textbook)
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the function
- Returns:
- binormalndarray with shape (ndim, N)
Array containing the unitary binormal vector The first dimension of binormal spans the (x,y,z) coordinates The second dimension of binormal spans the u parametrization sample points
- get_curvature(u)[source]#
Evaluate the curvature of the curve for the input u-parametrization
The definition of the curvature is given by equation 10.7 (Farin’s textbook)
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the curvature
- Returns:
- curvaturescalar or ndarray with shape (N, )
Scalar or array containing the curvature of the curve
- get_derivative(u, order)[source]#
Evaluate the derivative of the curve for the input u-parametrization
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the curve
- orderinteger
Order of the derivative
- Returns:
- dCndarray 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_normal(u)[source]#
Evaluate the unitary normal vector to the curve for the input u-parametrization
The definition of the unitary normal vector is given by equation 10.5 (Farin’s textbook)
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the function
- Returns:
- normalndarray with shape (ndim, N)
Array containing the unitary normal vector The first dimension of normal spans the (x,y,z) coordinates The second dimension of normal spans the u parametrization sample points
- get_normal_2D(u)[source]#
Evaluate the the unitary normal vector using the special formula 2D formula
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the function
- Returns:
- normalndarray with shape (2, N)
Array containing the unitary normal vector The first dimension of normal spans the (x,y,z) coordinates The second dimension of normal spans the u parametrization sample points
- get_tangent(u)[source]#
Evaluate the unitary tangent vector to the curve for the input u-parametrization
The definition of the unitary tangent vector is given by equation 10.5 (Farin’s textbook)
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the function
- Returns:
- tangentndarray with shape (ndim, N)
Array containing the unitary tangent vector The first dimension of tangent spans the (x,y,z) coordinates The second dimension of tangent spans the u parametrization sample points
- get_torsion(u)[source]#
Evaluate the torsion of the curve for the input u-parametrization
The definition of the torsion is given by equation 10.8 (Farin’s textbook)
- Parameters:
- uscalar or ndarray with shape (N,)
Scalar or array containing the u-parameter used to evaluate the torsion
- Returns:
- torsionscalar or ndarray with shape (N, )
Scalar or array containing the torsion of the curve
- get_value(u)[source]#
Evaluate the coordinates of the curve for the input u parametrization
- Parameters:
- uscalar or ndarray with shape (N,)
Parameter used to evaluate the curve
- Returns:
- Cndarray with shape (ndim, N)
Array containing the coordinates of the curve The first dimension of C spans the (x,y,z) coordinates The second dimension of C spans the u parametrization sample points
- plot(fig=None, ax=None, curve=True, control_points=True, frenet_serret=False, axis_off=False, ticks_off=False)[source]#
Create a plot and return the figure and axes handles
- plot_control_points(fig, ax, linewidth=1.25, linestyle='-.', color='red', markersize=5, markerstyle='o')[source]#
Plot the control points of the NURBS curve
- plot_curve(fig, ax, linewidth=1.5, linestyle='-', color='black', u1=0.0, u2=1.0)[source]#
Plot the coordinates of the NURBS curve
- nurbspy.nurbs_curve.merge_nurbs_curves(nurbs_list)[source]#
Merge multiple NURBS curves into a single continuous curve with C⁰ continuity.
- Parameters:
- nurbs_listlist of NurbsCurve
List of NURBS curve instances to merge. All must have the same polynomial degree.
- Returns:
- mergedNurbsCurve
A new NURBS curve representing the concatenation of all input curves.
Notes
Each curve is mapped to a subinterval of [0, 1] with equal length.
Adjacent curves are connected with multiplicity p + 1 (C⁰ continuity).
A small epsilon offset is applied at internal joins to avoid Gmsh issues related to repeated knots with multiplicity exactly equal to degree.