function [F]=tps_inplt(st,B)

% tps_inplt
%
%	Description:
%   Interpolate mapping using thin plate spline (support arbitray dimension >= 2)
%   modified from 2-D TPS (Serge Belongie (sjb@eecs.berkeley.edu) Sept. 4, 2000)
%   touched up by Gary K.L. Tam (2008)
% Input:
%   st - struct storing precomputed thin plate spline parameters (see tps_func)
%   B - input points
% Output:
%   F - output mapped points
%

assert(isstruct(st));

w = getfield(st,'w');
a = getfield(st,'a');
X = getfield(st,'X');

d2=dist2(X,B);

%--- increase stability ---
ind = find(d2 < 1e-4);
d2(ind) = 1e-4;
%--- increase stability ---

dim = size(B,2);
nbrCoord = size(B,1);
F = [];

for i=1:dim
  f_aff = a(:,i)'*[ones(1,nbrCoord); B'];
  f_wrp = w(:,i)'*(d2.*log(d2+eps));
  F(:,i) = f_aff+f_wrp;
end

return;