Home > vbmeg > external > mne > mne_read_surface.m

mne_read_surface

PURPOSE ^

SYNOPSIS ^

function [verts, faces] = mne_read_surface(fname)

DESCRIPTION ^

 [verts, faces] = mne_read_surface(fname)

 Reads a FreeSurfer surface file

 fname       - The file to read
 verts       - Vertex coordinates in meters
 faces       - The triangle descriptions
               NOTE: The quad file faces are split by this routine to
               create triangular meshes for all files

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [verts, faces] = mne_read_surface(fname)
0002 %
0003 % [verts, faces] = mne_read_surface(fname)
0004 %
0005 % Reads a FreeSurfer surface file
0006 %
0007 % fname       - The file to read
0008 % verts       - Vertex coordinates in meters
0009 % faces       - The triangle descriptions
0010 %               NOTE: The quad file faces are split by this routine to
0011 %               create triangular meshes for all files
0012 %
0013 
0014 %
0015 %
0016 %   Author : Matti Hamalainen, MGH Martinos Center
0017 %   License : BSD 3-clause
0018 %
0019 %
0020 %   Revision 1.4  2009/02/03 10:26:12  msh
0021 %   Added reading of 'new' quadrangle files
0022 %
0023 %   Revision 1.3  2006/05/26 03:39:12  msh
0024 %   fread3 was called instead of mne_fread3 for the magic number.
0025 %
0026 %   Revision 1.2  2006/05/22 11:01:47  msh
0027 %   Deleted superfluous text from the comments.
0028 %
0029 %   Revision 1.1  2006/05/22 10:44:44  msh
0030 %   Added surface and curvature reading routines.
0031 %   Fixed error in mne_read_source_spaces: triangle normals were not normalized
0032 %
0033 me='MNE:mne_read_surface';
0034 %
0035 %   The input file is big endian
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 %   Magic numbers to identify QUAD and TRIANGLE files
0044 %
0045 %   QUAD_FILE_MAGIC_NUMBER =  (-1 & 0x00ffffff) ;
0046 %   NEW_QUAD_FILE_MAGIC_NUMBER =  (-3 & 0x00ffffff) ;
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         %   Face splitting follows
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;                   % Our numbering starts from one
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;                   % Our numbering starts from one
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

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005