0001 function [vertex,face,edge,mesh] = mesh_emse2matlab(file,options)
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 if ~exist('options','var'),
0044 options = {'vertex','face','edge'};
0045 end
0046
0047 [path,name,ext] = fileparts(file);
0048 file = fullfile(path,[name ext]);
0049
0050 [fid,msg] = fopen(file,'r');
0051 if ~isempty(msg), error(msg); end
0052
0053 tic;
0054
0055
0056 version =fscanf(fid,'%f',1);
0057 file_type =fscanf(fid,'%f',1);
0058 minor_rev =fscanf(fid,'%f',1);
0059
0060 fprintf('...WFR Version = %d, Minor_Revision = %d, File-Type = %d\n',...
0061 version,minor_rev,file_type);
0062
0063 if ~(file_type==8000 | file_type==4000)
0064 S=sprintf('Could not convert WFR file type: %d',file_type);
0065 error(S);
0066 end
0067
0068
0069 if(minor_rev==3)
0070 type =fscanf(fid,'%f',1);
0071 else
0072 radius =fscanf(fid,'%f',1);
0073 vertex_no =fscanf(fid,'%d',1);
0074 face_no =fscanf(fid,'%d',1);
0075 edge_no =fscanf(fid,'%d',1);
0076
0077 fprintf('...Radius = %f meters\n',radius);
0078
0079
0080
0081 if(minor_rev==1), type = 0;
0082 else, type = fscanf(fid,'%f',1);
0083 end
0084 end
0085
0086 if(minor_rev==1)
0087 mesh = 'unknown';
0088 else
0089 switch type
0090 case 0, mesh = 'unknown';
0091 case { 64, 40}, mesh = 'scalp';
0092 case {128, 80}, mesh = 'outer skull';
0093 case {256, 100}, mesh = 'inner skull';
0094 case {512, 200}, mesh = 'cortex';
0095 otherwise, mesh = 'unknown';
0096 end
0097 end
0098
0099 fprintf('...Mesh type: %s\n',mesh);
0100
0101
0102 vertex = struct([]);
0103 face = struct([]);
0104 edge = struct([]);
0105
0106 if(minor_rev==3)
0107
0108
0109 fprintf('...Reading Minor Revision 3 Data File');
0110 Tmp = fscanf(fid,'%s%f%f%f',[4,inf]);
0111 fprintf('...done\n');
0112
0113
0114 if(max(strcmp('vertex',options)) > 0),
0115 fprintf('...Creating Vertices Struct');
0116
0117 vcode = Tmp(1,1);
0118
0119 vindex = find(Tmp(1,:) == vcode);
0120
0121 tmp = Tmp(2:4,vindex);
0122 tmp = num2cell(tmp);
0123 vertex = struct(...
0124 'index', [1:length(vindex)],...
0125 'x', tmp( 1,:),...
0126 'y', tmp( 2,:),...
0127 'z', tmp( 3,:));
0128 clear tmp;
0129 fprintf('...done\n');
0130 else
0131 fprintf('...Skipping Vertices Struct\n');
0132 end
0133
0134
0135 if(max(strcmp('face',options)) > 0),
0136 fprintf('...Creating Faces Struct');
0137
0138 tcode = Tmp(1,length(vindex)+1);
0139
0140 tindex = find(Tmp(1,:) == tcode);
0141
0142
0143
0144 tmp = Tmp(2:4,tindex) + 1;
0145
0146 tmp = num2cell(tmp);
0147 face = struct(...
0148 'index', [1:length(tindex)],...
0149 'vertex1', tmp( 1,:),...
0150 'vertex2', tmp( 2,:),...
0151 'vertex3', tmp( 3,:));
0152 clear tmp;
0153 fprintf('...done\n');
0154 else
0155 fprintf('...Skipping Faces Struct\n');
0156 end
0157
0158
0159 fprintf('...No edges for minor revision 3\n');
0160 clear Tmp;
0161
0162 elseif(minor_rev~=4)
0163
0164 disp('...Reading Minor Revision 1 or 2 Data');
0165 if(max(strcmp('vertex',options)) > 0),
0166 fprintf('...Reading %d Vertices',vertex_no);
0167
0168 tmp = zeros(13,vertex_no);
0169 tmp = fscanf(fid,'%d%x%d%d%g%g%g%g%g%g%g%g%g',[13,vertex_no]);
0170
0171 tmp(1,:) = tmp(1,:) + 1;
0172 tmp = num2cell(tmp);
0173 vertex = struct(...
0174 'index', tmp( 1,:),...
0175 'address', tmp( 2,:),...
0176 'channel_index',tmp( 3,:),...
0177 'x', tmp( 5,:),...
0178 'y', tmp( 6,:),...
0179 'z', tmp( 7,:),...
0180 'xnormal', tmp( 9,:),...
0181 'ynormal', tmp(10,:),...
0182 'znormal', tmp(11,:),...
0183 'potential', tmp(12,:),...
0184 'curvature', tmp(13,:));
0185 clear tmp;
0186 fprintf('...done\n');
0187 else
0188 fprintf('...Skipping %d Vertices\n',vertex_no);
0189 tmp = zeros(13,vertex_no);
0190 tmp = fscanf(fid,'%d%x%d%d%g%g%g%g%g%g%g%g%g',[13,vertex_no]);
0191 clear tmp;
0192 end
0193
0194 if(max(strcmp('face',options)) > 0),
0195 fprintf('...Reading %d Faces',face_no);
0196
0197 tmp = zeros(18,face_no);
0198 tmp = fscanf(fid,'%d%x%g%g%g%g%g%g%g%g%g%g%x%x%x%x%x%x',[18,face_no]);
0199 tmp(1,:) = tmp(1,:) + 1;
0200 tmp = num2cell(tmp);
0201 face = struct(...
0202 'index', tmp( 1,:),...
0203 'address', tmp( 2,:),...
0204 'solid_angle', tmp( 3,:),...
0205 'magnitude', tmp( 4,:),...
0206 'potential', tmp( 5,:),...
0207 'area', tmp( 6,:),...
0208 'center_x', tmp( 7,:),...
0209 'center_y', tmp( 8,:),...
0210 'center_z', tmp( 9,:),...
0211 'normal_x', tmp(10,:),...
0212 'normal_y', tmp(11,:),...
0213 'normal_z', tmp(12,:),...
0214 'vertex1', tmp(13,:),...
0215 'vertex2', tmp(14,:),...
0216 'vertex3', tmp(15,:),...
0217 'edge1', tmp(16,:),...
0218 'edge2', tmp(17,:),...
0219 'edge3', tmp(18,:));
0220 clear tmp;
0221 fprintf('...done\n');
0222
0223
0224
0225
0226 fprintf('...Converting Face vertices from address to index (this takes a while)');
0227 for i=1:face_no,
0228 face(i).vertex1 = find([vertex.address] == face(i).vertex1);
0229 face(i).vertex2 = find([vertex.address] == face(i).vertex2);
0230 face(i).vertex3 = find([vertex.address] == face(i).vertex3);
0231 end
0232 fprintf('...done\n');
0233 if(max(strcmp('edge',options)) > 0),
0234 fprintf('...Converting Face edges from address to index (this takes a while)');
0235 for i=1:face_no,
0236 face(i).edge1 = find([vertex.address] == face(i).edge1);
0237 face(i).edge2 = find([vertex.address] == face(i).edge2);
0238 face(i).edge3 = find([vertex.address] == face(i).edge3);
0239 end
0240 fprintf('...done\n');
0241 end
0242 else
0243 disp('...Skipping Faces');
0244 tmp = zeros(18,face_no);
0245 tmp = fscanf(fid,'%d%x%g%g%g%g%g%g%g%g%g%g%x%x%x%x%x%x',[18,face_no]);
0246 clear tmp;
0247 end
0248
0249 if(max(strcmp('edge',options)) > 0),
0250 fprintf('...Reading %d Edges',edge_no);
0251
0252 tmp = zeros(4,edge_no);
0253 tmp = fscanf(fid,'%d%x%x%x',[4,edge_no]);
0254
0255 tmp(1,:) = tmp(1,:) + 1;
0256 tmp = num2cell(tmp);
0257 edge = struct(...
0258 'index', tmp(1,:),...
0259 'address', tmp(2,:),...
0260 'vertex1', tmp(3,:),...
0261 'vertex2', tmp(4,:));
0262 clear tmp;
0263 fprintf('...done\n');
0264 fprintf('...Converting Edge vertices from address to index (this takes a while)');
0265 for i=1:edge_no,
0266 edge(i).vertex1 = find([vertex.address] == edge(i).vertex1);
0267 edge(i).vertex2 = find([vertex.address] == edge(i).vertex2);
0268 end
0269 fprintf('...done\n');
0270 else
0271 disp('...Skipping Edges');
0272
0273
0274
0275
0276 end
0277 else
0278
0279 disp('...Reading Minor Revision 4 Data');
0280
0281 if(max(strcmp('vertex',options)) > 0),
0282 fprintf('...Reading %d Vertices',vertex_no);
0283
0284 index = meshgrid(1:1:vertex_no,1);
0285 index = num2cell(index);
0286
0287 tmp = zeros(11,vertex_no);
0288 tmp = fscanf(fid,'%d%d%g%g%g%d%g%g%g%g%g',[11,vertex_no]);
0289
0290 tmp = num2cell(tmp);
0291 vertex = struct(...
0292 'index', index,...
0293 'channel_index',tmp( 1,:),...
0294 'x', tmp( 3,:),...
0295 'y', tmp( 4,:),...
0296 'z', tmp( 5,:),...
0297 'xnormal', tmp( 7,:),...
0298 'ynormal', tmp( 8,:),...
0299 'znormal', tmp( 9,:),...
0300 'potential', tmp(10,:),...
0301 'curvature', tmp(11,:));
0302 clear tmp index;
0303 fprintf('...done\n');
0304 else
0305 disp('...Skipping Vertices');
0306 tmp = zeros(11,vertex_no);
0307 tmp = fscanf(fid,'%d%d%g%g%g%d%g%g%g%g%g',[11,vertex_no]);
0308 end
0309
0310 if(max(strcmp('face',options)) > 0),
0311 fprintf('...Reading %d Faces',face_no);
0312
0313 index = meshgrid(1:1:face_no,1);
0314 index = num2cell(index);
0315
0316 tmp = zeros(16,face_no);
0317 tmp = fscanf(fid,'%g%g%g%g%g%g%g%g%g%g%d%d%d%d%d%d',[16,face_no]);
0318
0319 tmp(11:16,:) = tmp(11:16,:) + 1;
0320
0321 tmp = num2cell(tmp);
0322 face = struct(...
0323 'index', index,...
0324 'solid_angle', tmp( 1,:),...
0325 'magnitude', tmp( 2,:),...
0326 'potential', tmp( 3,:),...
0327 'area', tmp( 4,:),...
0328 'center_x', tmp( 5,:),...
0329 'center_y', tmp( 6,:),...
0330 'center_z', tmp( 7,:),...
0331 'normal_x', tmp( 8,:),...
0332 'normal_y', tmp( 9,:),...
0333 'normal_z', tmp(10,:),...
0334 'vertex1', tmp(11,:),...
0335 'vertex2', tmp(12,:),...
0336 'vertex3', tmp(13,:),...
0337 'edge1', tmp(14,:),...
0338 'edge2', tmp(15,:),...
0339 'edge3', tmp(16,:));
0340 clear tmp index;
0341 fprintf('...done\n');
0342 else
0343 disp('...Skipping Faces');
0344 tmp = zeros(16,face_no);
0345 tmp = fscanf(fid,'%g%g%g%g%g%g%g%g%g%g%d%d%d%d%d%d',[16,face_no]);
0346 end
0347
0348 if(max(strcmp('edge',options)) > 0),
0349 fprintf('...Reading %d Edges',edge_no);
0350
0351 index = meshgrid(1:1:edge_no,1);
0352 address = num2cell(index * 0);
0353 index = num2cell(index);
0354
0355 tmp = zeros(2,edge_no);
0356 tmp = fscanf(fid,'%d%d',[2,edge_no]);
0357 tmp = tmp + 1;
0358 tmp = num2cell(tmp);
0359
0360 edge = struct(...
0361 'index', index,...
0362 'address', address,...
0363 'vertex1', tmp(1,:),...
0364 'vertex2', tmp(2,:));
0365 clear tmp index address;
0366 fprintf('...done\n');
0367 else
0368 fprintf('...Skipping Edges\n');
0369
0370 end
0371 end
0372
0373 fclose(fid);
0374 t=toc;
0375 fprintf('...done (%5.2f sec)\n',t);
0376
0377 return