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