function [spherehl, ind] = sphere_resample(sphhl, sphere)
%
%function h = sphere_resample(sphhl, sphere)
%
% The function resamples the mesh resolution of 
% mesh sphhl to that of a given mesh sphere. The resultion of sphere shoul be
% larger.
%
% spherehl:  resampled sphere
%          ind: vertex index of sphhl that is mapped to the index of sphere.
%
%
% (C) Moo K. Chung 2007
%     Department of Biostatistics and Medical Informatics
%     Waisman Laboratory for Brain Imaging
%     University of Wisconsin-Maison
%  
% email://mkchung@wisc.edu


p=sphhl.vertices;
q=sphere.vertices;

n=size(p,1);
m=size(q,1);

for i=1:size(q,1)
    Q=repmat(q(i,:),n,1);
    mind=L2norm(p-Q);
    ind_qtop(i)=find(mind==min(mind));
end;

spherehl.faces=sphere.faces;
spherehl.vertices=p(ind_qtop,:);

ind=ind_qtop;


function L2=L2norm(vector)
% It computes the L2 norm vector of array n by 3 matrix

L2=sqrt(sum(vector.^2,2));
