Closed b-spline

13 views (last 30 days)
Priyabrata Das
Priyabrata Das on 6 Nov 2020
Answered: Abhaya on 20 Sep 2024
I want to write a closed b spline function which takes [k (degree is k-1), x coordinate and y coordinates of control points] as inputs. I am unable to generate a basis function which will calculate N0,k value. Any help on this matter will be appreciated.

Answers (1)

Abhaya
Abhaya on 20 Sep 2024
Hi Priyabrata,
To create a closed B-spline curve of degree k-1, you need to append the initial k-1 points of the x and y vectors to the end of their respective vectors.
For instance, if the vector x is [0, 1, 2, 4, 3] and the vector y is [0, 2, 4, 2, 0], and the degree is 3, you should transform these vectors to [0, 1, 2, 4, 3, 0, 1, 2] and [0, 2, 4, 2, 0, 0, 2, 4], respectively. This transformation can be achieved using the following commands:
x = [x, x(1:k-1)];
y = [y, y(1:k-1)];
Once you have the updated vectors, you can utilize the basis function equation to generate the spline curve.
Below is a sample code snippet for the basis function, followed by an example demonstrating how to call this function.
function N = basisFunction(i, k, t, knots)
% Base case for degree 0
if k == 1
if knots(i) <= t && t < knots(i+1)
N = 1;
else
N = 0;
end
else
% Recursive definition
if knots(i+k-1) - knots(i) == 0
term1 = 0;
else
term1 = ((t - knots(i)) / (knots(i+k-1) - knots(i))) * basisFunction(i, k-1, t, knots);
end
if knots(i+k) - knots(i+1) == 0
term2 = 0;
else
term2 = ((knots(i+k) - t) / (knots(i+k) - knots(i+1))) * basisFunction(i+1, k-1, t, knots);
end
N = term1 + term2;
end
end
function [splineX, splineY] = closedBSpline(k, x, y, numPoints)
% To ensure the control points form a closed loop
x = [x, x(1:k-1)];
y = [y, y(1:k-1)];
% Number of control points
n = length(x);
% Knot vector for a closed B-spline
knot = [0:(n + k - 1)];
% Parameter values
tParam = linspace(k-1, n, numPoints);
% Initialize the spline points
splineX = zeros(1, numPoints);
splineY = zeros(1, numPoints);
% Calculate spline points
for j = 1:numPoints
for i = 1:n
N = basisFunction(i, k, tParam(j), knot);
splineX(j) = splineX(j) + N * x(i);
splineY(j) = splineY(j) + N * y(i);
end
end
% Plot the spline
figure;
plot(x, y, 'o-', 'DisplayName', 'Control Points');
hold on;
plot(splineX, splineY, 'r-', 'DisplayName', 'B-Spline');
legend('show');
title('Closed Parametric B-Spline');
xlabel('x');
ylabel('y');
hold off;
end
By applying the code provided above, you can create a cubic B-spline that results in the following graph. The subsequent code details the steps required to achieve this implementation.
% Define the degree of the B-spline (k = degree + 1)
k = 4;
% Define the control points (ensure they form a closed loop)
x = [0, 1, 2, 5, 3];
y = [0, 2, 4, 0.5, 0];
% Number of points to generate along the spline
numPoints = 200;
[splineX, splineY] = closedBSpline(k, x, y, numPoints);
To explore more about basis function, please follow the given link.
I hope this helps in solving the query.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!