Home > bioelectromagnetism > mesh_open.m

mesh_open

PURPOSE ^

mesh_open - calls functions to read a triangulation file

SYNOPSIS ^

function [p] = mesh_open(p)

DESCRIPTION ^

 mesh_open - calls functions to read a triangulation file
 
 Usage: [p] = mesh_open(p)
 
 p is a parameter structure (see eeg_toolbox_defaults for
 more details). In this function, it should contain at least
 the following string fields:

       p.mesh.path - the directory location of the file to load
       p.mesh.file - the name of the file to load
       p.mesh.type - the file format:

      'emse'
      'brainstorm'
      'ascii' or 'freesurfer_ascii' for *.asc/.tri files
      'freesurfer_surf' for freesurfer binary surface
      'freesurfer_curv' for freesurfer binary curvature
      'freesurfer_overlay' for freesurfer binary overlay (.w)  
 
 The last two file types are associated with the current cortex,
 if it contains the same number of vertices (eg, load 'freesurfer_surf'
 first, then 'freesurfer_curv' or 'freesurfer_overlay').
 
 The file formats supported here are described in more detail 
 at their respective websites:
 
       FreeSurfer: http://surfer.nmr.mgh.harvard.edu
       EMSE:       http://www.sourcesignal.com
       BrainStorm: http://neuroimage.usc.edu/brainstorm
 
 The return structure creates or updates p.mesh.data, which 
 contains cell arrays:

       p.mesh.data.meshtype     type of surface, strings
       p.mesh.data.vertices     Mx3 (x,y,z) vertices
       p.mesh.data.faces        Mx3 vertex indices
       p.mesh.data.Cdata        scalar overlay, M vert x N overlays

 For example, having loaded a scalp and an inner skull mesh, 
 p.mesh.data.meshtype could be:
 
       p.mesh.data.meshtype{1} = 'scalp'
       p.mesh.data.meshtype{2} = 'inner skull'

 To plot the data returned, see mesh_plot or try
 
 Hpatch = patch('Vertices',p.mesh.data.vertices{1}',...
                'Faces',p.mesh.data.faces{1},...
                'Property','PropertyValue',...);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [p] = mesh_open(p)
0002 
0003 % mesh_open - calls functions to read a triangulation file
0004 %
0005 % Usage: [p] = mesh_open(p)
0006 %
0007 % p is a parameter structure (see eeg_toolbox_defaults for
0008 % more details). In this function, it should contain at least
0009 % the following string fields:
0010 %
0011 %       p.mesh.path - the directory location of the file to load
0012 %       p.mesh.file - the name of the file to load
0013 %       p.mesh.type - the file format:
0014 %
0015 %      'emse'
0016 %      'brainstorm'
0017 %      'ascii' or 'freesurfer_ascii' for *.asc/.tri files
0018 %      'freesurfer_surf' for freesurfer binary surface
0019 %      'freesurfer_curv' for freesurfer binary curvature
0020 %      'freesurfer_overlay' for freesurfer binary overlay (.w)
0021 %
0022 % The last two file types are associated with the current cortex,
0023 % if it contains the same number of vertices (eg, load 'freesurfer_surf'
0024 % first, then 'freesurfer_curv' or 'freesurfer_overlay').
0025 %
0026 % The file formats supported here are described in more detail
0027 % at their respective websites:
0028 %
0029 %       FreeSurfer: http://surfer.nmr.mgh.harvard.edu
0030 %       EMSE:       http://www.sourcesignal.com
0031 %       BrainStorm: http://neuroimage.usc.edu/brainstorm
0032 %
0033 % The return structure creates or updates p.mesh.data, which
0034 % contains cell arrays:
0035 %
0036 %       p.mesh.data.meshtype     type of surface, strings
0037 %       p.mesh.data.vertices     Mx3 (x,y,z) vertices
0038 %       p.mesh.data.faces        Mx3 vertex indices
0039 %       p.mesh.data.Cdata        scalar overlay, M vert x N overlays
0040 %
0041 % For example, having loaded a scalp and an inner skull mesh,
0042 % p.mesh.data.meshtype could be:
0043 %
0044 %       p.mesh.data.meshtype{1} = 'scalp'
0045 %       p.mesh.data.meshtype{2} = 'inner skull'
0046 %
0047 % To plot the data returned, see mesh_plot or try
0048 %
0049 % Hpatch = patch('Vertices',p.mesh.data.vertices{1}',...
0050 %                'Faces',p.mesh.data.faces{1},...
0051 %                'Property','PropertyValue',...);
0052 %
0053 
0054 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0055 
0056 % Licence:  GNU GPL, no express or implied warranties
0057 % History:  02/2002 Darren.Weber_at_radiology.ucsf.edu
0058 %
0059 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0060 
0061 fprintf('\nMESH_OPEN...\n'); tic;
0062 
0063 if ~exist('p','var'),
0064  [p] = eeg_toolbox_defaults;
0065   fprintf('...creating default p structure.\n');
0066 end
0067 
0068 [path,name,ext] = fileparts(strcat(p.mesh.path,filesep,p.mesh.file));
0069 file = fullfile(path,[name ext]);
0070 
0071 if exist(file) ~= 2,
0072   msg = sprintf('\n\n...file does not exist:\n\t%s\n',file);
0073   error(msg);
0074 end
0075 
0076 type = lower(p.mesh.type);
0077 
0078 switch type,
0079   
0080   case 'emse',
0081     
0082     fprintf('...loading EMSE mesh from:\n\t%s\n',file);
0083     
0084     % Get EMSE .wfr data (in meters)
0085     options = {'vertex','face'};
0086     [vertices,faces,edges,meshtype] = mesh_emse2matlab(file,options);
0087     
0088     vertex_matrix = [vertices.x; vertices.y; vertices.z]';
0089     face_matrix = [faces.vertex1;faces.vertex2;faces.vertex3]';
0090     
0091     % Rotate 90 degrees around Z for EMSE data
0092     vertex_matrix = Rz(vertex_matrix,90,'degrees');
0093     
0094     % Is this a new or replacement mesh?
0095     [p.mesh.current,meshExists] = mesh_check(p,meshtype);
0096     
0097     p.mesh.data.meshtype{p.mesh.current}    = meshtype;
0098     p.mesh.data.vertices{p.mesh.current}    = vertex_matrix;
0099     p.mesh.data.faces{p.mesh.current}       = face_matrix;
0100     p.mesh.data.lapmat{p.mesh.current}      = [];
0101     p.mesh.data.lapint{p.mesh.current}      = [];
0102     p.mesh.data.timeseries{p.mesh.current}  = [];
0103     p.mesh.data.Cdata{p.mesh.current}       = [];
0104     
0105   case 'brainstorm',
0106     
0107     fprintf('...loading BrainStorm data from:\n\t%s\n',file);
0108     load(file);
0109     
0110     p.mesh.data = [];
0111     
0112     for i=1:size(Comment,2),
0113       if isempty(Comment{i}), continue; end
0114       fprintf('...converting tesselation: %s\n',Comment{i});
0115       p.mesh.data.meshtype{i}   = Comment{i};
0116       p.mesh.data.vertices{i}   = Vertices{i}';  % transpose Vertices
0117       p.mesh.data.faces{i}      = Faces{i};
0118       p.mesh.data.lapmat{i}     = [];
0119       p.mesh.data.lapint{i}     = [];
0120       p.mesh.data.timeseries{i} = [];
0121       p.mesh.data.Cdata{i}      = [];
0122       p.mesh.current = i;
0123     end
0124     
0125   case {'ascii','freesurfer_ascii'},
0126     
0127     fprintf('...loading ASCII or FreeSurfer data from:\n\t%s\n',file);
0128     
0129     % Get Freesurfer data
0130     if findstr(file,'.tri'),
0131       [vertex_matrix,face_matrix] = freesurfer_read_tri(file);
0132     else
0133       [vertex_matrix,face_matrix] = freesurfer_read_ascii(file);
0134     end
0135     
0136     fprintf('...converting surface coordinates (mm to meters)\n');
0137     vertex_matrix = vertex_matrix ./ 1000;
0138     
0139     meshtype = freesurfer_meshtype(file); % see function below
0140     
0141     [p.mesh.current,meshExists] = mesh_check(p,meshtype);
0142     
0143     p.mesh.data.meshtype{p.mesh.current}   = meshtype;
0144     p.mesh.data.vertices{p.mesh.current}   = vertex_matrix;
0145     p.mesh.data.faces{p.mesh.current}      = face_matrix;
0146     p.mesh.data.lapmat{p.mesh.current}     = [];
0147     p.mesh.data.lapint{p.mesh.current}     = [];
0148     p.mesh.data.timeseries{p.mesh.current} = [];
0149     p.mesh.data.Cdata{p.mesh.current}      = [];
0150     
0151   case 'freesurfer_surf',
0152     
0153     fprintf('...loading FreeSurfer binary surface from:\n\t%s\n',file);
0154     
0155     meshtype = freesurfer_meshtype(file); % see function below
0156     
0157     [p.mesh.current,meshExists] = mesh_check(p,meshtype);
0158     
0159     [vertex_matrix, face_matrix] = freesurfer_read_surf(file);
0160     
0161     fprintf('...converting surface coordinates (mm to meters)\n');
0162     vertex_matrix = vertex_matrix ./ 1000;
0163     
0164     p.mesh.data.meshtype{p.mesh.current}   = meshtype;
0165     p.mesh.data.vertices{p.mesh.current}   = vertex_matrix;
0166     p.mesh.data.faces{p.mesh.current}      = face_matrix;
0167     p.mesh.data.lapmat{p.mesh.current}     = [];
0168     p.mesh.data.lapint{p.mesh.current}     = [];
0169     p.mesh.data.timeseries{p.mesh.current} = [];
0170     p.mesh.data.Cdata{p.mesh.current}      = [];
0171     
0172   case 'freesurfer_curv',
0173     
0174     fprintf('...loading FreeSurfer binary curvature from:\n\t%s\n',file);
0175     
0176     [curv, Nfaces] = freesurfer_read_curv(file);
0177     
0178     allocated = 0;
0179     for meshN = 1:length(p.mesh.data.meshtype),
0180       
0181       type = p.mesh.data.meshtype{meshN};
0182       
0183       if size(p.mesh.data.vertices{meshN},1) == size(curv,1),
0184         fprintf('...allocating overlay to Cdata for ''%s'' surface.\n',type);
0185         p.mesh.data.Cdata{meshN} = curv;
0186         p.mesh.current = meshN;
0187         allocated = 1;
0188       end
0189       
0190     end
0191     if allocated < 1,
0192       fprintf('...failed to allocate curvature to any surface, incompatible Nfaces!\n');
0193     end
0194     
0195   case 'freesurfer_overlay',
0196     
0197     fprintf('...loading FreeSurfer binary overlay from:\n\t%s\n',file);
0198     
0199     [w,vert] = freesurfer_read_wfile(file);
0200     
0201     Nvert = length(vert); clear vert;
0202     
0203     allocated = 0;
0204     for meshN = 1:length(p.mesh.data.meshtype),
0205       
0206       type = p.mesh.data.meshtype{meshN};
0207       
0208       if size(p.mesh.data.vertices{meshN},1) == Nvert,
0209         fprintf('...allocating overlay to Cdata for ''%s'' surface.\n',type);
0210         p.mesh.data.Cdata{meshN} = w;
0211         p.mesh.current = meshN;
0212         allocated = 1;
0213       end
0214       
0215     end
0216     if allocated < 1,
0217       fprintf('...failed to allocate overlay to any surface, incompatible vertices!\n');
0218     end
0219     
0220   otherwise,
0221     fprintf('...mesh format: %s\n', p.mesh.type);
0222     fprintf('...sorry, cannot load this data format at present.\n');
0223     return;
0224 end
0225 
0226 t=toc; fprintf('...done (%5.2f sec).\n\n',t);
0227 
0228 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003