www.pudn.com > B-Interpolation.rar > BsplineInterpolate.m, change:2006-03-27,size:1538b


function [P] = BsplineInterpolate(D,k,n) 
%B-spline curve interpolation, 
% this is a global approximation routine 
% input: D (data point),  
%       n (n=# of data point) 
%       k (order of the curve),  
%       h (h=# control points) 
% output: P (control point of the resulting B-spline curve) 
 
% D=[0 0; 3 4; -1 4; -4 0; -4 -3]; % data points 
% k=4; 
% n=5; 
 
%Number of data points should be no less than the Order of the curve 
if(k>n) k = n; 
end 
 
% Step 1: Computing parameters: ub() 
totalChord = 0; 
for i=2:n 
    totalChord = totalChord + sqrt((D(i-1,1)-D(i,1))^2+(D(i-1,2)-D(i,2))^2); 
end 
 
ub(1) = 0.; 
for i=2:n-1 
    ub(i) = ub(i-1) + sqrt((D(i-1,1)-D(i,1))^2+(D(i-1,2)-D(i,2))^2)/totalChord ; 
end 
ub(n) = 1.0 ; 
 
%Step 2: Computing knot vector: U 
for j=1:n-k, 
    U(j+k) = 0.; 
    for i=j:j+k-2 % averaging k-1 parameters 
        U(j+k) = U(j+k)+ub(i+1); 
    end 
    U(j+k) = U(j+k)/(k-1); 
end 
 
for j=1:k, 
    U(j) = 0.; % repeating 1st k knots 
end 
 
for j=n+1:n+k, 
    U(j) = 1.; % repeating last k knots 
end 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%Step 3: Construct the A matrix  
%And then solve Ax = b 
 
for i=2:n-1 
    %Find span 
    span = 0; 
    while ub(i)>U(span+1) 
        span = span+1; 
    end 
    span = span - 1; 
 
    % Define the basis function 
    [N] = Basisfuns(ub(i),U,span,k); 
 
    for j=0:k-1 
        A(i,span-k+2+j) = N(j+1); 
    end 
end 
 
A(1,1) = 1.0 ; 
A(n,n) = 1.0 ; 
 
P(:,1) = A\D(:,1); 
P(:,2) = A\D(:,2);