function outpoints = tal2mni(inpoints)
% Converts coordinates from Talairach coordinates to best guess
%  of equivalent MNI brain coordinates
%
% FORMAT outpoints = tal2mni(inpoints)
%
% inpoints is N by xyz or xyz by N matrix of coordinates
%  (N being the number of points)
%
% outpoints is the coordinate matrix with Talairach points
%

dimdim = find(size(inpoints) == 3);
if isempty(dimdim)
  error('input must be a N by 3 or 3 by N matrix')
end
if dimdim == 2
  inpoints = inpoints'; %'
end

N = size(inpoints,2);
inpoints = [inpoints; ones(1,N)];
% inpoints is now 4 x N %

% MNI2Tal Transformation matrices, different zooms above/below AC
TAbove = spm_matrix([0 0 0 0.05 0 0 0.99 0.97 0.92]);
TBelow = spm_matrix([0 0 0 0.05 0 0 0.99 0.97 0.84]);

% Find mni z coordinate at which talairach z = 0 %
[tmp1 tmp2 mniz0] = mni2tal([0 0 0]);

indBelow = find(inpoints(3,:) <  0);  % Below AC/PC
indAbove = find(inpoints(3,:) >= 0);  % Below AC/PC

outpoints = zeros(size(inpoints));
outpoints(:,indBelow) = TBelow * inpoints(:,indBelow);
outpoints(:,indAbove) = TAbove * inpoints(:,indAbove);
outpoints = outpoints(1:3,:);

if dimdim == 2
  outpoints = outpoints'; %'
end

