0001 function [verts, faces] = mne_read_surface(fname)
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 me='MNE:mne_read_surface';
0034
0035
0036
0037 fid = fopen(fname,'rb','ieee-be');
0038
0039 if (fid < 0)
0040 error(me,'Cannot open file %s', fname);
0041 end;
0042
0043
0044
0045
0046
0047
0048 NEW_QUAD_FILE_MAGIC_NUMBER = 16777213;
0049 TRIANGLE_FILE_MAGIC_NUMBER = 16777214;
0050 QUAD_FILE_MAGIC_NUMBER = 16777215;
0051
0052 magic = mne_fread3(fid) ;
0053 if(magic == QUAD_FILE_MAGIC_NUMBER || magic == NEW_QUAD_FILE_MAGIC_NUMBER)
0054 nvert = mne_fread3(fid);
0055 nquad = mne_fread3(fid);
0056 if magic == QUAD_FILE_MAGIC_NUMBER
0057 fprintf(1,'\t%s is a quad file (nvert = %d nquad = %d)\n', ...
0058 fname,nvert,nquad);
0059 else
0060 fprintf(1,'\t%s is a new quad file (nvert = %d nquad = %d)\n', ...
0061 fname,nvert,nquad);
0062 end
0063 if magic == QUAD_FILE_MAGIC_NUMBER
0064 verts = fread(fid, nvert*3, 'int16') ./ 100 ;
0065 else
0066 verts = fread(fid, nvert*3, 'single');
0067 end
0068 if (nargout > 1)
0069 quads = fread3_many(fid,nquad*4);
0070 quads = reshape(quads,4,nquad)';
0071
0072
0073
0074 faces = zeros(2*nquad,3);
0075 nface = 0;
0076 for k = 1:nquad
0077 quad = quads(k,:);
0078 if mod(quad(1), 2) == 0
0079 nface = nface + 1;
0080 faces(nface,1) = quad(1);
0081 faces(nface,2) = quad(2);
0082 faces(nface,3) = quad(4);
0083
0084 nface = nface + 1;
0085 faces(nface,1) = quad(3);
0086 faces(nface,2) = quad(4);
0087 faces(nface,3) = quad(2);
0088 else
0089 nface = nface + 1;
0090 faces(nface,1) = quad(1);
0091 faces(nface,2) = quad(2);
0092 faces(nface,3) = quad(3);
0093
0094 nface = nface + 1;
0095 faces(nface,1) = quad(1);
0096 faces(nface,2) = quad(3);
0097 faces(nface,3) = quad(4);
0098 end
0099 end
0100 faces = faces + 1;
0101 end
0102 elseif (magic == TRIANGLE_FILE_MAGIC_NUMBER)
0103 s = fgets(fid);
0104 fgets(fid);
0105 nvert = fread(fid, 1, 'int32') ;
0106 nface = fread(fid, 1, 'int32') ;
0107 fprintf(1,'\t%s is a triangle file (nvert = %d ntri = %d)\n',fname,nvert,nface);
0108 fprintf(1,'\t%s',s);
0109 verts = fread(fid, nvert*3, 'float32') ;
0110 faces = fread(fid, nface*3, 'int32') ;
0111 faces = reshape(faces, 3, nface)';
0112 faces = faces + 1;
0113 else
0114 fclose(fid);
0115 error(me,'Bad magic number (%d) in surface file %s',magic,fname);
0116 end
0117 verts = 0.001*reshape(verts, 3, nvert)';
0118 fclose(fid);
0119 fprintf(1,'\tRead a surface with %d vertices from %s\n',nvert,fname);
0120
0121 return;
0122
0123 function [res] = fread3_many(fid,count)
0124 res = reshape(fread(fid, 3*count, 'uchar'), 3, []);
0125 res = bitshift(res(1,:), 16) + bitshift(res(2,:), 8) + res(3,:);
0126 end
0127
0128 end