How to Construct a B-Spline Curve from Control Points Using NumPy

How to Construct a B-Spline Curve from Control Points Using NumPy
Image by Editor

Introduction to B-Spline Curves

B-spline curves are used to make smooth and flexible shapes. They are helpful in computer graphics, animation, and design. Unlike Bézier curves, B-splines let you change one control point without changing the whole curve, which makes them easier to adjust and work with. In this article, we will build a B-spline curve using Python.

A B-spline curve is a linear combination of basis functions and control points. Mathematically, a B-spline curve C(u) is expressed as:

\[
C(u) = \sum_{i=0}^{n} N_{i,p}(u) \, P_{i}
\]

where:

  • Pi are the control points
  • Ni,p(u) are the B-spline basis functions of degree p
  • u is the parameter that varies over a given knot vector

A B-spline curve is defined by:

  • Control points: These points determine the shape of the curve
  • Degree: The polynomial degree of the basis functions used in the curve formulation
  • Knots: A sequence of parameter values that determines how the control points influence the curve

Setting Up the Environment

Before we begin coding, make sure you have the necessary libraries installed. You can install them using:

pip install numpy scipy matplotlib

For reference, we will use NumPy for arrays, SciPy for the spline implementation, and Matplotlib for plotting.

Implementing a B-Spline Curve in Python

Step 1: Define Control Points

First, we define the control points using a NumPy array.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import BSpline

control_points = np.array([[0, 0], [1, 3], [4, 4], [6, 2], [7, 5]])
x, y = control_points[:, 0], control_points[:, 1]
  • control_points[:, 0]: Extracts the x-coordinates
  • control_points[:, 1]: Extracts the y-coordinates

Step 2: Define the Knot Vector

For a uniform, clamped B-spline, we choose the knot vector based on the number of control points and the curve’s degree. The knot vector controls how much influence each control point has. It must be nondecreasing and typically repeats the end knots to “clamp” the curve to the first and last control points.

degree = 3  # Degree of the B-spline
n_control_points = len(control_points)

# Uniform knot vector with clamping at the ends (length = n_control_points + degree + 1)
knots = np.concatenate((
    np.zeros(degree), 
    np.linspace(0, 1, n_control_points - degree + 1), 
    np.ones(degree)
))
  • np.zeros(degree): Creates degree zeros at the start of the knot vector
  • np.linspace(0, 1, n_control_points – degree + 1): Generates evenly spaced values between 0 and 1 (inclusive)
  • np.ones(degree): Adds degree ones at the end of the knot vector

Step 3: Compute the B-Spline

We use the BSpline class from SciPy to construct and evaluate the curve. We generate a parameter array to sample multiple points so the curve is smooth.

t_values = np.linspace(0, 1, 100)  # 100 points for a smooth curve
spline_x = BSpline(knots, x, degree)
spline_y = BSpline(knots, y, degree)

curve_x = spline_x(t_values)
curve_y = spline_y(t_values)
  • BSpline(knots, x, degree): Creates a B-spline function for the x-coordinates
  • BSpline(knots, y, degree): Creates a B-spline function for the y-coordinates
  • curve_x = spline_x(t_values): Computes the x-coordinates of the B-spline curve
  • curve_y = spline_y(t_values): Computes the y-coordinates of the B-spline curve

Step 4: Visualizing the B-Spline Curve

Matplotlib plots the computed B-spline curve. We also display the control points with markers to show their influence on the curve.

plt.plot(x, y, 'ro--', label="Control Points")  # Control polygon
plt.plot(curve_x, curve_y, 'b-', label="B-Spline Curve")  # B-spline curve
plt.legend()
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("B-Spline Curve from Control Points")
plt.grid()
plt.show()

B-Spline Curve

Conclusion

B-spline curves are smooth and flexible, which makes them useful in graphics and design. NumPy helps create and manipulate the control points efficiently. The degree and knot vector determine how the curve behaves. The Cox–de Boor algorithm computes the basis functions that weight each control point, and evaluating these weighted functions yields points on the final curve. Matplotlib then helps visualize the curve and its control polygon. Learning these concepts enables you to create accurate, adjustable curves.

Leave a Reply

Your email address will not be published. Required fields are marked *