0001 function [p] = mesh_open(p)
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
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
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
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
0092 vertex_matrix = Rz(vertex_matrix,90,'degrees');
0093
0094
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}';
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
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);
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);
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