function [vertxcolor,varargout] = interp_mail(coordmail,coordelec,data);
%INTERP_MAIL - (French) Interpolate coordinates of the "maillage"
% function [vertxcolor,varargout] = interp_mail(coordmail,coordelec,data);
% function vertxcolor = interp_mail(coordmail,coordelec,data);
% vertxcolor = interp_mail(coordmail,coordelec,data);
% coordmail = coordonnees des vertex du maillage
% coordelec = coordonnees des points de mesure du maillage;
% data = donnees aux points de mesure

%<autobegin> ---------------------- 26-May-2004 11:30:45 -----------------------
% --------- Automatically Generated Comments Block Using AUTO_COMMENTS ---------
%
% CATEGORY: Visualization
%
% Alphabetical list of external functions (non-Matlab):
%   toolbox\shepards.m
%
% At Check-in: $Author: Mosher $  $Revision: 16 $  $Date: 5/26/04 9:59a $
%
% This software is part of BrainStorm Toolbox Version 2.0 (Alpha) 24-May-2004
% 
% Principal Investigators and Developers:
% ** Richard M. Leahy, PhD, Signal & Image Processing Institute,
%    University of Southern California, Los Angeles, CA
% ** John C. Mosher, PhD, Biophysics Group,
%    Los Alamos National Laboratory, Los Alamos, NM
% ** Sylvain Baillet, PhD, Cognitive Neuroscience & Brain Imaging Laboratory,
%    CNRS, Hopital de la Salpetriere, Paris, France
% 
% See BrainStorm website at http://neuroimage.usc.edu for further information.
% 
% Copyright (c) 2004 BrainStorm by the University of Southern California
% This software distributed  under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPL
% license can be found at http://www.gnu.org/copyleft/gpl.html .
% 
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%<autoend> ------------------------ 26-May-2004 11:30:45 -----------------------

% /---Script Author--------------------------------------\
% |                                                      |
% | *** Sylvain Baillet, Ph.D.                           |
% | Cognitive Neuroscience & Brain Imaging Laboratory    |
% | CNRS UPR640 - LENA                                   | 
% | Hopital de la Salpetriere, Paris, France             |
% | sylvain.baillet@chups.jussieu.fr                     |
% |                                                      |
% \------------------------------------------------------/


% History --------------------------------
% SB 13 11 97
% JCM 08-Sep-2003 Commenting
% ----------------------------------------


nmes= size(coordelec,1);
nsom = size(coordmail,1);
% vertxcolor = ones(nsom,1)*0;
% 
% [mx imx]= min(coordelec(:,1));
% [Mx iMx]= max(coordelec(:,1));
% mz = coordelec(imx,3);
% Mz = coordelec(iMx,3);
% 
% p = [mean([mx,Mx]);0;mean([mz,Mz])];

% Axe X pointant vers l'avant 
%coordmail(:,1) = coordmail(:,1)-p(1); % epilepsie

% % Axe X pointant vers l'arriere 
% coordmail(:,1) = (coordmail(:,1)-p(1));
% coordmail(:,2) = coordmail(:,2)-p(2);
% coordmail(:,3) = coordmail(:,3)-p(3);
% 
% selct = [-.8*[Mz-mz];0;[Mx-mx]];
% selct = selct/norm(selct);

%Isom = find( coordmail*selct >= 0 ); 


%%% Si interp sur toute la tete
%Isom = (1:size(coordmail,1))';

% if size(coordmail,1) > 10000
%     hw = waitbar(0,'Please wait for color interpolation...');
%     t0 = clock;
% end

% Wmat = spalloc(size(coordmail,1),nmes,size(coordmail,1)*4);
% for i = 1:size(coordmail,1) 
%     dist = norlig(coordelec-ones(nmes,1)*[coordmail(i,:)]);
%     [D Isort] = sort(dist);
%     D = 1./(D(1:4)+eps);
%     %   vertxcolor(i) = sum( D'.*data(Isort(1:4)))/S; 
%     Wmat(i,Isort(1:4)) = D./sum(D);
%     if ~rem(i,1000) & exist('hw','var')
%         waitbar(i/size(coordmail,1));
%     end
% end

[vertxcolor, Wmat] = shepards(coordmail,coordelec,data,4);

% if exist('hw','var')
%     close(hw)
%     etime(clock, t0)
% end

%vertxcolor = Wmat*data;

if nargout > 1
    varargout{1} = Wmat;
end
