2D approximation with B-Splines

14 visualizzazioni (ultimi 30 giorni)
Michael Treude
Michael Treude il 24 Lug 2019
Commentato: Ganesh Regoti il 30 Lug 2019
Currently I am working on a script to run a approximation with B-Splines using the de Boor's algorithm.
So I scripted the mathematical functions from:
For getting started I tried to approximate a trepezoid using the control points:
and
It seems that my code works well (picture 1) but when I add more control points,
the results are strange (picture 2).
and
This is my function:
function s = f_Bspline(Cx, Cy, m)
% Calculation of B-Spline
% Cx, Cy: Control points
% m: Polynomal degree
%% Parameters
% Calculate amount of control points
% Control points: n + 1
n = size(Cx,2) - 1;
% Plot intervall
t = 0:0.01:n-m+1;
%% Calculate knot vector
% Knot vector T
T = [];
for j=0:1:n+m+1
if j <= m
a = 0;
elseif j >= m+1 && j <= n
a = j - m;
elseif j > n
a = n - m + 1;
end
T(j+1) = a;
end
%% Calculate splines
% Spline Matrx
% s(1,:) : x values
% s(2,:) : y values
s = zeros(2, size(t,2));
% Calculate B-Spline for all t
for z=1:1:size(t,2)
% s(1,z) = Sum { x_i * B_i,m(t) }, i = 0,1, ... ,n
% s(2,z) = Sum { y_i * B_i,m(t) }, i = 0,1, ... ,n
s(1,z) = 0;
s(2,z) = 0;
for i=0:1:n
% Calculate B-Spline
Bspl = f_BsplineBase(i, m, t(z), T);
% x - Coordinate
s(1,z) = s(1,z) + Cx(i+1) * Bspl;
% y - Coordinate
s(2,z) = s(2,z) + Cy(i+1) * Bspl;
end
end
end
And this my "main" script:
%% Clear console
close all;
clear all;
clc;
%% Parameters
disp('>> Parameters ');
% Control points
Cx = [0 1 4 5.5 6.5 8];
Cy = [0 1 1 0.5 0.5 0];
% Degree of polynom
m = 3;
% Calculate amount of control points
% Control points: n + 1
n = size(Cx,2) - 1;
% Plot intervall
t = 0:0.01:n-m+1;
%% Calculate knot vector
disp('>> Knot vector');
% Knot vector T
T = [];
for j=0:1:n+m+1
if j <= m
a = 0;
elseif j >= m+1 && j <= n
a = j - m;
elseif j > n
a = n - m + 1;
end
T(j+1) = a;
end
% Knot vector output
T
%% Calculate splines
disp('>> Calculate B-Splines');
% Spline Matrx
% s(1,:) : x values
% s(2,:) : y values
s = zeros(2, size(t,2));
% Calculate B-Spline for all t
for z=1:1:size(t,2)
% s(1,z) = Sum { x_i * B_i,m(t) }, i = 0,1, ... ,n
% s(2,z) = Sum { y_i * B_i,m(t) }, i = 0,1, ... ,n
s(1,z) = 0;
s(2,z) = 0;
for i=0:1:n
% Calculate B-Spline
Bspl = f_BsplineBase(i, m, t(z), T);
% x - Coordinate
s(1,z) = s(1,z) + Cx(i+1) * Bspl;
% y - Coordinate
s(2,z) = s(2,z) + Cy(i+1) * Bspl;
end
end
%% Plot
disp('>> Plot');
figure(1);
plot(Cx, Cy, 'o');
hold on;
plot(Cx, Cy, '--');
hold on;
plot(s(1,:), s(2,:),'linewidth',2);
legend({'Control points','Polygon', 'B-Spline'},'Location','northwest');
grid on;
grid minor;
%% End
disp(' >> Finished');
It would be great if anybody could figure out the case of the problem (these strange "spikes" produced by my algorithm).
What went wrong / what is my mistake?
PS: I know that this is not a perfect code file. I am looking forward to seperate some other functions such as the calculation of
the knot vector, because it is not neccessary to calculate it in every function call.

Risposte (0)

Categorie

Scopri di più su Splines in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by