原文的license
Copyright (c) 2009, Dr. Murtaza Khan
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
这个是文件的README
N-Dimensional Cardinal(Catmull-Rom) Spline Interpolation
========================================================
evalcrdnd.倫
------------
Evaluates N Dimensional Cardinal Spline at
parameter value u
crdatnplusoneval.m
-------------------------------------
Evaluate cardinal spline for given four points and
tesion at N+1 values of u (parameter u varies b/w 0
and 1). Uniform parameterization is used.
testcrdnd.m
------------------------------------------
A Simple Test program to do 1D, 2D, 3D Cardinal
Spline interpolation of given data with Tension=0
(Catmull-Rom)
% % % --------------------------------
% % % Author: Dr. Murtaza Khan
% % % Email : drkhanmurtaza@gmail.com
% % % -------------------------------
这个是三维拟合的效果图,当然这个也可以用作二维。
包含有crdatnplusoneval.m
% Evaluate Cubic Cardinal spline at N+1 values for given four points and tesion.
% Uniform parameterization is used.
% P0,P1,P2 and P3 are given four points.
% T is tension.
% N is number of intervals (spline is evaluted at N+1 values).
function [MatNbyNPlusOne]=crdatnplusoneval(P0,P1,P2,P3,T,N)
MatNbyNPlusOne=[];
% u vareis b/w 0 and 1.
% at u=0 cardinal spline reduces to P1.
% at u=1 cardinal spline reduces to P2.
u=0;
MatNbyNPlusOne(:,1)=[evalcrdnd(P0,P1,P2,P3,T,u)]'; % MatNbyNPlusOne(:,1)=length(P0)
du=1/N;
for k=1:N
u=k*du;
MatNbyNPlusOne(:,k+1)=[evalcrdnd(P0,P1,P2,P3,T,u)]';
end
% % % --------------------------------
% % % Author: Dr. Murtaza Khan
% % % Email : drkhanmurtaza@gmail.com
% % % --------------------------------
evalcrdnd.m
% Evaluates ND Cubic Cardinal Spline at parameter value u
% INPUT
% P0,P1,P2,P3 are given four points.
% P1 and P2 are endpoints of curve.
% P0 and P3 are used to calculate the slope of the endpoints (i.e slope of P1 and
% P2).
% T is tension (T=0 for Catmull-Rom type)
% u is parameter at which spline is evaluated
% OUTPUT
% ND-cardinal spline evaluated values at parameter value u
function [Pu] =evalcrdnd(P0,P1,P2,P3,T,u)
Pu=[];
s= (1-T)./2;
% MC is cardinal matrix
MC=[-s 2-s s-2 s;
2.*s s-3 3-(2.*s) -s;
-s 0 s 0;
0 1 0 0];
for i=1:length(P0)
G(:,i)=[P0(i); P1(i); P2(i); P3(i)];
end
U=[u.^3 u.^2 u 1];
for i=1:length(P0)
Pu(i)=U*MC*G(:,i);
end
% This program or any other program(s) supplied with it does not provide any
% warranty direct or implied. This program is free to use/share for
% non-commerical purpose only.
% contact: M A Khan
% Email: khan_goodluck@yahoo.com
% % http://www.geocities.com/mak2000sw/
testcrdnd.m
close all, clear all,clc
disp('N-Dimensional Cardinal (Catmull-Rom) Spline Interpolation v1.2 ')
str=sprintf('%s','Copyright M Khan: khan_goodluck@yahoo.com'); disp(str);
n=100; % number of intervals (i.e. parametric curve would be evaluted n+1 times)
%%%% Cardinal Spline 1D Interpolation %%%%%%%%%%
x=[35 35 16 15 25 40 65 50 60 80 80];
figure
hold on
Tension=0;
for k=1:length(x)-3
xi=crdatnplusoneval([x(k)],[x(k+1)],[x(k+2)],[x(k+3)],Tension,n);
plot(xi,'linewidth',2);
plot(xi(1),'ro','linewidth',2) ;
plot(length(xi),xi(length(xi)),'ro','linewidth',2) ;
end
%%% Cardinal Spline 1D Interpolation
%%%% Cardinal Spline 2D Interpolation %%%%%%%%%%
Px=[35 35 16 15 25 40 65 50 60 80 80];
Py=[47 47 40 15 36 15 25 40 42 27 27];
% Note first and last points are repeated so that spline curve passes
% through all points
% when Tension=0 the class of Cardinal spline is known as Catmull-Rom spline
Tension=0;
figure
% plot(Px,Py,'ro','linewidth',2)
hold on
for k=1:length(Px)-3
[MatOut2]=crdatnplusoneval([Px(k),Py(k)],[Px(k+1),Py(k+1)],[Px(k+2),Py(k+2)],[Px(k+3),Py(k+3)],Tension,n);
% Between each pair of control points plotting n+1 values of first two rows of MatOut
plot(MatOut2(1,:),MatOut2(2,:),'b','linewidth',2)
end
title('\bf2D Cardinal Spline')
%%%% Cardinal Spline 2D Interpolation
%%%% Cardinal Spline 3D Interpolation %%%%%%%%%%
Px=[35 35 16 15 25 40 65 50 60 80 80];
Py=[47 47 40 15 36 15 25 40 42 27 27];
Pz=[-17 -17 20 15 36 15 25 20 25 -7 -7];
% Note first and last points are repeated so that spline curve passes
% through all points
figure
hold on
plot3(Px,Py,Pz,'ro','linewidth',2)
Tension=0;
for k=1:length(Px)-3
[MatOut3]=crdatnplusoneval([Px(k),Py(k),Pz(k)],[Px(k+1),Py(k+1),Pz(k+1)],[Px(k+2),Py(k+2),Pz(k+2)],[Px(k+3),Py(k+3),Pz(k+3)],Tension,n);
% Between each pair of control points plotting n+1 values of first three rows of MatOut
plot3(MatOut3(1,:),MatOut3(2,:),MatOut3(3,:),'b','linewidth',2)
end
title('\bf3D Cardinal Spline')
view(3);
box;
%%%% Cardinal Spline 3D Interpolation
% Using similar approach you can do Cardinal Spline interpolation for
% N-Dimensional data
% % % --------------------------------
% % % Author: Dr. Murtaza Khan
% % % Email : drkhanmurtaza@gmail.com
% % % --------------------------------