function deformed=apply_deformation(image,deformation); 
%deformed=apply_deformation(image,deformation); 
%
% (C) 2008. Moo K. Chung

d=size(image);
cube=zeros(d(1),d(2),d(3));

rvolx=round(deformation.x);
rvoly=round(deformation.y);
rvolz=round(deformation.z);

% x direction
%dispx=kron([1:d(1)], ones(d(2),1));
%dispx=repmat(dispx',[1 1 d(3)]);

%y direction
%dispy=kron([1:d(2)],ones(d(1),1));
%dispy=repmat(dispy,[1 1 d(3)]);

% z direction
%dispz=kron([1:d(3)],ones(d(2),1));
%dispz=repmat(dispz,[d(1) 1 1]);
%dispz=reshape(dispz,d(1),d(2),d(3));

for i=1:d(1)
    for j=1:d(2)
        for k=1:d(3)
            intensity=image(i,j,k); 
            x= i+rvolx(i,j,k);
            y=j+rvoly(i,j,k);
            z=k+rvolz(i,j,k);
            cube(x,y,z) = intensity;
        end;
    end;
end;

deformed=cube;