0001 function [trans_pos] = mne_transform_coordinates(filename,pos,from,to)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 me = 'mne:mne_transform_coordinates';
0037 global FIFF;
0038 if isempty(FIFF)
0039 FIFF = fiff_define_constants();
0040 end
0041
0042 if nargin ~= 4
0043 error(me,'Some input arguments are missing');
0044 end
0045
0046
0047
0048 [ fid, tree, dir ] = fiff_open(filename);
0049
0050 T0 = [];
0051 T1 = [];
0052 T2 = [];
0053 T3plus = [];
0054 T3minus = [];
0055 for k = 1:length(dir)
0056 if dir(k).kind == FIFF.FIFF_COORD_TRANS
0057 tag = fiff_read_tag(fid,dir(k).pos);
0058 trans = tag.data;
0059 if trans.from == FIFF.FIFFV_COORD_MRI && trans.to == FIFF.FIFFV_COORD_HEAD
0060 T0 = fiff_invert_transform(trans);
0061 elseif trans.from == FIFF.FIFFV_COORD_MRI && trans.to == FIFF.FIFFV_MNE_COORD_RAS
0062 T1 = trans;
0063 elseif trans.from == FIFF.FIFFV_MNE_COORD_RAS && trans.to == FIFF.FIFFV_MNE_COORD_MNI_TAL
0064 T2 = trans;
0065 elseif trans.from == FIFF.FIFFV_MNE_COORD_MNI_TAL
0066 if trans.to == FIFF.FIFFV_MNE_COORD_FS_TAL_GTZ
0067 T3plus = trans;
0068 elseif trans.to == FIFF.FIFFV_MNE_COORD_FS_TAL_LTZ
0069 T3minus = trans;
0070 end
0071 end
0072 end
0073 end
0074 fclose(fid);
0075
0076
0077
0078 if (from == FIFF.FIFFV_COORD_HEAD && isempty(T0)) || isempty(T1) || isempty(T2) || ...
0079 (to == FIFF.FIFFV_MNE_COORD_FS_TAL && (isempty(T3minus) || isempty(T3minus)))
0080 error(me,'All required coordinate transforms not found');
0081 end
0082
0083
0084
0085 if ~isempty(pos)
0086 if size(pos,2) ~= 3
0087 error(me,'Coordinates must be given in a N x 3 array');
0088 end
0089 if to == from
0090 trans_pos = pos;
0091 else
0092 pos = [ pos ones(size(pos,1),1) ]';
0093 if from == FIFF.FIFFV_COORD_HEAD
0094 pos = T0.trans*pos;
0095 elseif from ~= FIFF.FIFFV_COORD_MRI
0096 error(me,'Input data must be in MEG head or surface RAS coordinates');
0097 end
0098 if to == FIFF.FIFFV_COORD_HEAD
0099 pos = inv(T0.trans)*pos;
0100 elseif to ~= FIFF.FIFFV_COORD_MRI
0101 pos = (T2.trans*T1.trans)*pos;
0102 if to ~= FIFF.FIFFV_MNE_COORD_MNI_TAL
0103 if to == FIFF.FIFFV_MNE_COORD_FS_TAL
0104 for k = 1:size(pos,2)
0105 if pos(3,k) > 0
0106 pos(:,k) = T3plus.trans*pos(:,k);
0107 else
0108 pos(:,k) = T3minus.trans*pos(:,k);
0109 end
0110 end
0111 else
0112 error(me,'Illegal choice for the output coordinates');
0113 end
0114 end
0115 end
0116 trans_pos = pos(1:3,:)';
0117 end
0118 end
0119 end
0120