how to convert B-form into piecewise bezier curves

4 views (last 30 days)
I am following the mathlab example to get the smooth curves from discrete points in B-form. The curve looks quite smooth. I wonder if there is any way to convert the B-form into multiple pieces of quadratic or cubic Bezier curves that cover the whole curve.
x = linspace(0,2*pi,51);
noisy_y = cos(x) + .2*(rand(size(x))-.5);
plot(x,noisy_y,'x')
axis([-1 7 -1.2 1.2])
tol = (.05)^2*(2*pi)
fnplt( spaps(x, noisy_y, tol), 'r', 2 )

Answers (1)

Abhaya
Abhaya on 18 Sep 2024
Hi Wong,
To convert a B-form spline into multiple Bézier curves in MATLAB, you can use the MATLAB ‘spbrk’ function. The spbrk’ function breaks the B-form in the given spline into parts of B-form and returns the knots, coefficients, number of breakpoints, and the order of the B-curve.
With the coefficients obtained, we can create the Bézier segments. These segments can then be plotted individually.
Below is a sample code snippet to convert B-form into piecewise Bezier curves.
x = linspace(0, 2*pi, 51);
noisy_y = cos(x) + 0.2 * (rand(size(x)) - 0.5);
figure;
plot(x, noisy_y, 'x');
hold on;
axis([-1 7 -1.2 1.2]);
tol = (0.05)^2 * (2*pi);
% Fit a smoothing spline
sp = spaps(x, noisy_y, tol);
% Plot the B-spline curve
fnplt(sp, 'r', 2);
% break the B-form in SP
[knots, coefs, number, order] = spbrk(sp);
% Insert knots to convert to Bezier form
% Insert knots at each unique knot value to get Bezier segments
unique_knots = unique(knots);
for i = 1:length(unique_knots)
sp = fnrfn(sp, unique_knots(i));
end
% Extract the refined B-form
[refined_knots, refined_coefs, refined_number, refined_order] = spbrk(sp);
% Plot the Bezier curves
for i = 1:refined_number - refined_order
% Extract control points for each Bezier segment
bezier_coefs = refined_coefs(:, i:i+refined_order-1);
t = linspace(0, 1, 100);
bezier_curve = zeros(1, length(t));
for j = 1:refined_order
bezier_curve = bezier_curve + bezier_coefs(j) * nchoosek(refined_order-1, j-1) * (1-t).^(refined_order-j) .* t.^(j-1);
end
plot(linspace(refined_knots(i), refined_knots(i+1), 100), bezier_curve, 'b-', 'LineWidth', 1);
end
hold off;
The result plot of the given code is attached below.
To explore more about MATLAB ‘spbrk’ function, execute following command on MATLAB command window
doc spbrk
Hope this helps.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!