Home > vbmeg > external > mne > mne_transform_coordinates.m

mne_transform_coordinates

PURPOSE ^

SYNOPSIS ^

function [trans_pos] = mne_transform_coordinates(filename,pos,from,to)

DESCRIPTION ^

 [trans_pos] = mne_transform_coordinates(filename,pos,from,to)

 Transform locations between various MRI-related coordinate frames

 filename   - Name of a fif file containing the coordinate transformations
              This file can be conveniently created with mne_collect_transforms
 pos        - N x 3 array of locations to transform (in meters)
 from       - Coordinate frame of the above locations
              Allowed choices are: FIFFV_COORD_MRI (surface RAS coordinates)
              and FIFFV_COORD_HEAD (MEG head coordinates)
 to         - Coordinate frame of the result
              Allowed choices are: FIFFV_COORD_MRI, FIFFV_COORD_HEAD,
              FIFFV_MNE_COORD_MNI_TAL (MNI Talairach), and
              FIFFV_MNE_COORD_FS_TAL (FreeSurfer Talairach)

              All of the above constants are define in fiff_define_constants

 trans_pos  - The transformed locations

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [trans_pos] = mne_transform_coordinates(filename,pos,from,to)
0002 %
0003 % [trans_pos] = mne_transform_coordinates(filename,pos,from,to)
0004 %
0005 % Transform locations between various MRI-related coordinate frames
0006 %
0007 % filename   - Name of a fif file containing the coordinate transformations
0008 %              This file can be conveniently created with mne_collect_transforms
0009 % pos        - N x 3 array of locations to transform (in meters)
0010 % from       - Coordinate frame of the above locations
0011 %              Allowed choices are: FIFFV_COORD_MRI (surface RAS coordinates)
0012 %              and FIFFV_COORD_HEAD (MEG head coordinates)
0013 % to         - Coordinate frame of the result
0014 %              Allowed choices are: FIFFV_COORD_MRI, FIFFV_COORD_HEAD,
0015 %              FIFFV_MNE_COORD_MNI_TAL (MNI Talairach), and
0016 %              FIFFV_MNE_COORD_FS_TAL (FreeSurfer Talairach)
0017 %
0018 %              All of the above constants are define in fiff_define_constants
0019 %
0020 % trans_pos  - The transformed locations
0021 %
0022 
0023 %
0024 %
0025 %   Author : Matti Hamalainen, MGH Martinos Center
0026 %   License : BSD 3-clause
0027 %
0028 %   No part of this program may be photocopied, reproduced,
0029 %   or translated to another program language without
0030 %   prior written consent of the author.
0031 %   Revision 1.1  2008/11/16 21:31:24  msh
0032 %   Added mne_transform_coordinates and new coordinate frame definitions
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 %   Read the fif file containing all necessary transformations
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 %   Check we have everything we need
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 %   Go ahead and transform the data
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005