function DefTemplate = LDDMMvolumeTemplate2Taget(template,Hmap)

% function DefTemplate = LDDMMvolumeTemplate2Taget(template,Hmap)
% 
% This function interpolates the template file that is stored as Analyze
% format using the transformation file (save as vtk) to generate a deformed
% template. The interpolation method is 'linear' by defdault.
% 
% Inputs:
%   template: an string representing template file name that ios stored as
%   Analyze 7.5 format (Ex. 'Atlas.img')
% 
%   Hmap: A vtk file containing the transformation vectors
%
% Output:
%   DefTemplate: A deformed template.
%
% ICM, Oct. 2011

% Reading the template file. Assuming it has been stored in Analyze 7.5
% format.

I = analyze75read(template);
I = double(I);

% Extracting the transformation information stored as vtk file in
% Hmap000.vtk

[Hx,Hy,Hz] = LoadVtk1a(Hmap,3,'b');


% performing a linear transformation on the template to construct deformed
% template file.

DefTemplate = LDDMMInterp3Atl2Pt(I, Hx, Hy, Hz, 'linear', 0);
DefTemplate(isnan(DefTemplate)) = 0; 