非均匀B样条拟合MATLAB程序

直接上代码,多的不再说了。

1、写一个Base函数

function result = Bbase(i,k,u,t)
%第i段k次B样条基,Deboor递推递归算法
%t为变量,u(i)<=t<u(i+),k=0时result=;
if(k==)
if(u(i)<=t && t<u(i+))%注意1=u(i)<=t<u(i+)=1时的情况,这里要用t<=u(i+);
result=;
return;
else
result=;
return;
end
else
if(u(i+k)-u(i)==)
alpha=;
else
alpha=(t-u(i))/(u(i+k)-u(i));
end
if(u(i+k+)-u(i+)==)
beta=;
else
beta=(u(i+k+)-t)/(u(i+k+)-u(i+));
end
end
result=alpha*Bbase(i,k-,u,t)+beta*Bbase(i+,k-,u,t);
2、B样条程序
%------------------非均匀B样条拟合MATLAB程序-----------------
clear
k=;
x=load('data.txt');
[n,m]=size(x);
%-----------弦长参数化--------------------------------------
u(k+n)=;
for i=:n-
u(k+i+)=u(k+i)+sqrt((x(i+,)-x(i,))^+(x(i+,)-x(i,))^);
end;
L=u(n+k);
for i=:n
u(k+i)=u(k+i)/L;
end;
for i=:
u(k+i+n)=;
end
%控制多边线
plot(x(:,),x(:,),'o');
hold on
%------------反求n+2个控制点--------------------
%首位重节点v1=v2
%首位与控制多边形相切
A=zeros(n+);
A(,)=;A(,)=-;
A(,)=;
A(n+,n+)=-;A(n+,n+)=;
A(n+,n+)=;
for i=:n
for j=:
A(i,i+j-)=Base(i+j-,k,u,u(i+));
end
end
%e:方程右边.
e=;
for i=:m
e(n+,i)=;
end
for i=:n
e(i+,:)=x(i,:);
end
%求出控制点d
d=inv(A)*e;
plot(d(:,),d(:,),'g'); hold on
%------------插值并作出样条曲线-----------------
x=;y=;down=;
for j=:(n-)
uu=(u(j+)):0.0005:u(j+);
for kk=:length(uu)
down=down+;
x(down)=d(j,)*Base(j,,u,uu(kk))+d(j+,)*Base(j+,,u,uu(kk))+d(j+,)*Base(j+,,u,uu(kk))+d(j+,)*Base(j+,,u,uu(kk));
y(down)=d(j,)*Base(j,,u,uu(kk))+d(j+,)*Base(j+,,u,uu(kk))+d(j+,)*Base(j+,,u,uu(kk))+d(j+,)*Base(j+,,u,uu(kk));
end
end
axis('equal'); plot(x,y,'red'); xlabel('x');ylabel('y');
grid on

参考文章

3次非均匀B样条拟合程序

上一篇:maven deploy 上传jar包到私有仓库


下一篇:原生HTML5 input type=file按钮UI自定义