%% Test of spline thin plate functions

x=2*pi*rand(100,1);%set of random x coordinates from 0 to 2pi
y=2*pi*rand(100,1);%set of random y coordinates
%U=exp(-(x-pi).*(x-pi)-(y-pi).*(y-pi));% gaussian
U=x;
[U_smooth,U_tps]=tps_coeff([x y],U,0);%calculate tps coeff, no smoothing
xI=0:0.1:2*pi;%interpolation grid
yI=0:0.1:2*pi;
[XI,YI]=meshgrid(xI,yI);
[npy,npx]=size(XI);
XI=reshape(XI,[],1);
YI=reshape(YI,[],1);
EM = tps_eval([XI YI],[x y]);%evaluate interpolation on the new grid
U_eval=EM*U_tps;
U_eval=reshape(U_eval,npy,npx);
figure(1)
imagesc(U_eval,[0 6])
title('U matrix')
colorbar
[DMX,DMY] = tps_eval_dxy([XI YI],[x y]);
DUX_eval=DMX*U_tps;
DUY_eval=DMY*U_tps;
DUX_eval=reshape(DUX_eval,npy,npx);
% plot(yI,U_eval(:,5))
figure(2)
DUY_eval=reshape(DUY_eval,npy,npx);
imagesc(DUX_eval,[-1 1])
% plot(xI,DU_eval(:,5))
figure(3)
imagesc(DUY_eval,[-1 1])
figure(4)
% plot(x,U_eval(50,:),x,DUX_eval(50,:),x,DUY_eval(50,:))
size(U_eval(50,:))
size(xI)
plot(xI,U_eval(50,:),xI,DUX_eval(50,:),xI,DUY_eval(50,:))
