www.pudn.com > B-Interpolation.rar > AppForSurf.m, change:2006-03-28,size:1391b


function [P] = AppForSurf(D,k,n,iCtrl,ub,U) 
%B-spline curve approximation, subroutine of BsplineSurfApp 
% this is a global approximation routine 
% input: D (data point -- 1D),  
%       n (n=# of data point) 
%       k (order of the curve)  
%       iCtrl (iCtrl=# control points), 
% output: P (control point of the resulting B-spline curve -- 1D) 
 
% step 1: compute Qk & N 
N(1,1) = 1.0; 
N(n,iCtrl) = 1.0; 
for i=0:n-1 
    %Find span 
    if  ub(i+1)>=U(iCtrl+1), 
        span = iCtrl - 1; 
    else if ub(i+1)<=U(k), 
           span = k-1; 
        else 
            span = 0; 
            while ub(i+1)>U(span+1) 
                span = span+1; 
            end 
            span = span - 1; 
        end 
    end 
    % Define the basis function 
    funs = Basisfuns(ub(i+1),U,span,k); 
    for j=0:k-1 
        N(i+1,span-k+2+j) = funs(j+1); 
    end 
    Qk(i+1,1) = D(i+1,1) - N(i+1,1)*D(1,1) - N(i+1,iCtrl)*D(n,1); 
end 
 
% step 2: compute Q 
 
for i=0:iCtrl-1, 
    Q(i+1,1) = 0.; 
    for j=0:n-1, 
        Q(i+1,1) = Q(i+1,1) + N(j+1,i+1)*Qk(j+1,1);   
    end 
end 
 
% step 3: solve the function NT*N*P=Q 
%         notice that P0 & Pn-1 are not included 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
Q = Q(2:iCtrl-1,1); 
N = N(2:n-1,2:iCtrl-1); 
 
P(2:iCtrl-1,1) = (N'*N)\Q(:,1); 
 
P(1,1) = D(1,1); 
P(iCtrl,1) = D(n,1);