function varargout = ma_show_neighbours(varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% this function shows the points nearest a choosen coordinate both in a
%%% table and on an spm brain
%%% the inputs are points which is a 1x3 vector in MNI coordinates
%%% and nbour which is the number of neighbours you want to see

global ma

switch nargin
case 1
    point = varargin{1};
    if(~all(size(point)==[1,3]))
        point
        disp('Input Point must be 1x3')
        return
    end
    nbour = ma_defaults.neighbours.nbour;
case 2
    point = varargin{1};
    if(~all(size(point)==[1,3]))
        point
        disp('Input Point must be 1x3')
        return
    end
    nbour = varargin{2};
case 0
    ma_interactive('clear');
    point = spm_input('Enter the data point in MNI coords',1,'e','',[1,3])
    nbour = spm_input('How many neighbours to show?',2,'e','30',1)
    %delete(gcf)  %% close interactive box
end

%%% get points and convert to mni
dbpts = ma.ba.mxyz;
dist = sqrt( (point(1)-dbpts(:,1)).^2 + (point(2)-dbpts(:,2)).^2 ...
    + (point(3)-dbpts(:,3)).^2 );
[d,ord] = sort(dist);

near.dist = dist(ord(1:nbour));
near.area = ma.ba.area(ord(1:nbour));
near.brod = ma.ba.brod(ord(1:nbour));
near.descrip = ma.ba.descrip(ord(1:nbour));
near.dbpts = dbpts(ord(1:nbour),:);
near.pmid = ma.ba.pmid(ord(1:nbour));

pid = cat(1,ma.pubmed.id);
for i=1:length(near.pmid)
    pind(i) = find(pid==near.pmid(i));
    near.year{i} = ma.pubmed(pind(i)).year;
    near.journal{i} = ma.pubmed(pind(i)).journal;
    near.authors{i} = ma.pubmed(pind(i)).authors;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% show data in new window

%%% mx,my,mz,area,descrip,year,aut
plen = [8,8,8,8,40,75,8,100];
cols = [0,cumsum(plen)];
cols = 100*cols./max(cols);

Ftable = ma_figure('GetWin','Table');
ma_figure('Clear',Ftable)

title_ax = axes('Visible','off','XLim',[-1,105],'YLim',[-1.5,0.5], ...
    'DrawMode','fast','Parent',Ftable,'YDir','Reverse','Position',[0.02,0.93,0.97,0.07]);

ttl = ['Nearest neighbours to point ',num2str(point),', MNI coordinates'];
text(0,1,ttl,'FontSize',9,'FontWeight','Bold')
hold on
plot([0,100],[2,2],'k-')
header = {'dist','mx','my','mz','Area','Description','Year','Authors'};
for j=1:8
    nc = length(header{j});
    nc(nc>plen(j)) = round(plen(j));
    hh(j) = text(cols(j),2,header{j}(1:nc));
end
set(hh,'FontSize',9,'Margin',2,'BackgroundColor',[1,1,1],'FontWeight','Bold')
set(title_ax,'XLim',[-1,105],'YLim',[0.5,2.5],'YDir','Reverse')

mainax = axes('Visible','on','DrawMode','fast','Parent',Ftable,...
    'Position',[0.02,0.02,0.97,0.90],'YDir','Reverse','XLim',[-1,105], ...
    'YLim',[0,32]);

for i=1:length(near.pmid)
    
    c{1} = num2str(near.dist(i,1),3);
    c{2} = num2str(round(near.dbpts(i,1)));
    c{3} = num2str(round(near.dbpts(i,2)),3);
    c{4} = num2str(round(near.dbpts(i,3)),3);
    c{5} = near.area{i};
    c{6} = near.descrip{i};
    c{7} = near.year{i};
    aut = cat(1,near.authors{i});
    c{8} = [];
    for j=1:length(aut)
         c{8} = [c{8},aut{j},'  '];
    end
    
    for j=1:length(c)
        nc = length(c{j});
        nc(nc>plen(j)) = round(plen(j));
        th(i,j) = text(cols(j),i,c{j}(1:nc));
    end
end
set(th,'FontSize',8,'BackgroundColor',[1,1,1],'Margin',1)
set(mainax,'Visible','off')
%set(th,'Interpreter','tex')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% show points
sz = 2;

fbrain = ma_figure('GetWin','Graphics');
ma_figure('Clear',fbrain)
cUnits = get(fbrain,'Units');
set(fbrain,'Units','Normalized');
        
global st
bb      = [ [-78 78]' [-112 76]' [-50 85]' ];
st      = struct('n', 0, 'vols',[], 'bb',bb,'Space',eye(4),'centre',[0 0 0],...
    'callback',';','xhairs',1,'hld',1,'fig',fbrain,'mode',1,'plugins',[]);
st.vols = cell(24,1);

%% show hires
hname = 'single_subj_T1.mnc';
H = spm_vol(hname);
axhires = spm_orthviews('Image',H,[0.01,0.05,0.9,0.9]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% generate sphere
[x,y,z] = ndgrid(-10:10,-10:10,-10:10);
sph = [x(:),y(:),z(:)];
d = sqrt(sum(sph.^2,2));
sph = sph(d<=sz,:);

%% plot database points
mpts = near.dbpts;
npts = size(mpts,1);
ccc = ( mpts-repmat(H.mat(1:3,4)',npts,1) )/2;
cci = [];
for i=1:size(sph,1)
    cci = [cci;ccc+repmat(sph(i,:),npts,1)];
end
spm_orthviews('AddColouredBlobs',axhires,cci',1*ones(length(cci),1),H.mat,[0,1,0])

%% plot central point
ccc = ( point-H.mat(1:3,4)' )/2;
cci = sph+repmat(ccc,length(sph),1);
spm_orthviews('AddColouredBlobs',axhires,cci',1*ones(length(cci),1),H.mat,[1,0,0])
pause(0.1)
spm_orthviews('AddColouredBlobs',axhires,cci',1*ones(length(cci),1),H.mat,[1,0,0])

spm_orthviews('Reposition',point)











