function [varargout] = spm_mni2class(XYZmm)
% Format 1: class_brain = spm_mni2class(XYZmm)
% Format 2: [class_brain, class_brain_s] = spm_mni2class(XYZmm)

%----------------------------------------------------------------------------------
% FORMAT class_brain=mni2class(XYZmm)
% SPM MNI coordinates to class identitification
% and display the identification results on the Matlab results
% XYZmm         - {1 x 3} matrix of coordinates of points (MNI coordinates)
% class_brain   - returned results of the class identification of 5 types
%                 in cell data structure 
% class_brain_s - returned results of the class identification of 5 types
%                 in cell data structure (in short format)
%----------------------------------------------------------------------------------
%   17.02.04    Yuqing GONG
%   29.09.04    Last modified
%   For bugs and fix, please contact yqgong@stanford.edu
%----------------------------------------------------------------------------------

%----------------------------------------------------------------------------------
% DB strcture
%----------------------------------------------------------------------------------

%------------------------------- 
% Classification parameters:
%------------------------------- 
% class_name        - Names for these five different Class types
% XYZtd             - Talairach coordinates indexed w.r.t. Z Y Z, of size 192595*3
% brod              - Indices for the class of brodmann area w.r.t. its
%                     Talairach index, of size 192595*1 
% brod_i            - Indices for the class of brodmann area (1~101, not
%                     evenly distributed), of size 73*1
% brod_ll           - Full names for the 73 different brodmann area, of size 73*1
% gyrus             - Similary structure with 'brod' class
% hem               - Similary structure with 'brod' class
% lobe              - Similary structure with 'brod' class
% ttype             - Similary structure with 'brod' class
%----------------------------------------------------------------------------------

% ==========================================================
% My codes start here!
% ==========================================================

%----------------------------------------------------------------------------------
% Read Tal to classification data base into DB
%----------------------------------------------------------------------------------
baseFileName = 'TDdb.mat';
load(baseFileName, '-mat');
class_name={'Brod','Gyrus','Hem','Lobe','Ttype'};


%----------------------------------------------------------------------------------
% Read input coordinates in MNI
%----------------------------------------------------------------------------------
% XYZmm = [];
% 
% while ( isempty(XYZmm) )
%     XYZmm = input('Please input Target MNI coordinate   [X,Y,Z]  : ') ;
% end


%----------------------------------------------------------------------------------
% Check if it is in the TAL to MNI mapping database
% If not, just display the error message and return
% For implementation into GUI, now we change the returned 
% class_brain to be 'Nonexist'.
%----------------------------------------------------------------------------------
Tal_coord=mni2tal(XYZmm);
Tal_coord=2*round(Tal_coord/2);
class_brain=cell(5,1);
class_brain_s=cell(5,1);


Tal_index=find( XYZtd(:,1)==Tal_coord(1) & XYZtd(:,2)==Tal_coord(2) & XYZtd(:,3)==Tal_coord(3));

if (isempty(Tal_index))
    
    for index=1:5
        class_brain{index}={'Nonexist'};
        class_brain_s{index}={'None'};
    end %for index=1:5
    if (nargout ==0),
        disp('This MNI coordinate does not exist in the Talyor Space!');
        spm_popup_class(class_name,class_brain);
    elseif (nargout ==1),
        varargout{1} = class_brain;
    elseif (nargout ==2),
        varargout{1}= class_brain;
        varargout{2}= class_brain_s;
    end
    
    return;
end %if (isempty(Tal_index))



class=[brod gyrus hem lobe ttype];
class_i={brod_i gyrus_i hem_i lobe_i ttype_i};
class_ll={brod_ll gyrus_ll hem_ll lobe_ll ttype_ll};
class_l={brod_l gyrus_l hem_l lobe_l ttype_l};


%----------------------------------------------------------------------------------
% Find the classification identification for Talairach index and
% display the results
%----------------------------------------------------------------------------------
for index=1:5
    
    if ~( class(Tal_index,index) )
        class_brain{index} = {'Unidentified'};
        class_brain_s{index} = {'Unid?'};
    else
        index_t=find( class_i{index}==class(Tal_index,index) );
        class_brain{index} = class_ll{ index }(index_t);
        class_brain_s{index} = class_l{ index }(index_t);
    end
    
    if (nargout < 2),
        disp( sprintf( '%s: %s',class_name{index},cell2mat(class_brain{index}) ) );    
    end
end    


if (nargout ==0),
    spm_popup_class(class_name,class_brain);   
elseif (nargout ==1),
    varargout{1} = class_brain;
elseif (nargout ==2),
    varargout{1}= class_brain;
    varargout{2}= class_brain_s;
end

return


% -----------------------------------------------------------------------
%   Pop up a figure to display the results of the classification
% -----------------------------------------------------------------------
function spm_popup_class(class_name,class_brain)

h_main=figure('Units','normalized','Position',[0.1 .5 .3 .1],...
    'Name','Classification Labeling(By Yuqing)','MenuBar','none',...
    'NumberTitle','off','Resize','off');
h_text=zeros(1,5);

for index=1:5
    h_text(index)=uicontrol('Style','edit','Units','normalized',...
        'Position',[.2, .9-index*.17 .5 .17],...
        'Tag',['MyText' int2str(index)],...
        'String',sprintf( '%s: %s',class_name{index},cell2mat(class_brain{index}) )  );

end


% ==========================================================
% My codes end here!
% ==========================================================
