This Python script provides a set of functions to compute B-spline basis functions, their derivatives, and construct a collocation matrix for a set of input points. The script can be used for applications involving B-splines, such as numerical methods, interpolation, or computer-aided design (CAD).
- B-spline basis function computation: The
Nfunction computes the B-spline basis functions using Cox-de Boor recursion formula. - First derivative of the B-spline basis functions: The
dNfunction computes the first derivative of the B-spline basis functions. - Higher-order derivatives: The
ddNfunction computes higher-order derivatives of the B-spline basis functions. - Knot span locator: The
findspanfunction finds the knot span for a given value of the parameter. - Collocation matrix construction: The
spcolfunction constructs a collocation matrix for a set of input values (u) based on the B-spline basis functions and their derivatives.
Calculates the B-spline basis function N_i^d(x) recursively:
x: The input parameter.d: Degree of the B-spline.i: Index of the basis function.t: Knot vector.
Computes the first derivative of the B-spline basis function N_i^d(x):
- Same parameters as
N(x, d, i, t).
Calculates the kth derivative of the B-spline basis function N_i^d(x):
x: The input parameter.d: Degree of the B-spline.i: Index of the basis function.t: Knot vector.k: Order of the derivative.
Finds the knot span for a given input x in the knot vector t:
t: Knot vector.x: The input parameter.
Constructs the collocation matrix for a given set of input parameters u:
U: Knot vector.d: Degree of the B-spline.u: Array of input points.
From GitHub:
pip install git+https://github.com/AkshayK325/spcol.gitBelow is a simple example of how to use the script:
import numpy as np
import matplotlib.pyplot as plt
# Assuming the spcol is installed
import spcol as sp
# Knot vector
U = np.array([0, 0, 0, 0, 0.25, 0.5, 0.75, 1, 1, 1, 1])
# Degree of the B-spline
p = 3
# Input points where we want to evaluate the B-spline curve
u = np.linspace(0,1,100)
# Control points for the B-spline in 2D (x, y)
XY = np.array([[0.0, 0.0], # Control point 1 (x1, y1)
[1.0, 1.0], # Control point 2 (x2, y2)
[2.0, 1.0], # Control point 3 (x3, y3)
[3.0, 2.0], # Control point 4 (x4, y4)
[4.0, 1.0], # Control point 5 (x5, y5)
[5.0, 0.0], # Control point 6 (x6, y6)
[1.0, 0.0]]) # Control point 7 (x7, y7)
# Generate the collocation matrix M
M = sp.spcol(U, p, u)
# Calculate the xy coordinates of the B-spline at points u
xy = M @ XY # Matrix multiplication (M * control points)
# Extract the x and y values
x_vals = xy[:, 0]
y_vals = xy[:, 1]
# Generate the collocation matrix M
M = sp.spcol(U, p, np.unique(U))
# Calculate the xy coordinates of the B-spline at points u
xy = M @ XY # Matrix multiplication (M * control points)
# Extract the x and y values
x_valsKnots = xy[:, 0]
y_valsKnots = xy[:, 1]
# Plotting
plt.figure()
# Plot the control polygon (control points)
plt.plot(XY[:, 0], XY[:, 1], 'o--', label='Control Polygon', color='grey')
# Plot the B-spline curve (evaluated points)
plt.plot(x_vals, y_vals, 'r-', label='B-spline Curve')
plt.scatter(x_valsKnots, y_valsKnots,c='b',marker="s", label='Knots')
plt.title("B-spline Curve")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.grid(True)
plt.show()- Python 3.x
- NumPy
You can install the required dependencies via pip:
pip install numpyThis project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
- [email protected]
Created on: October 20, 2021