Home > vbmeg > external > freesurfer > read_surf.m

read_surf

PURPOSE ^

SYNOPSIS ^

function [vertex_coords, faces] = read_surf(fname)

DESCRIPTION ^

 [vertex_coords, faces] = read_surf(fname)
 reads a the vertex coordinates and face lists from a surface file
 note that reading the faces from a quad file can take a very long
 time due to the goofy format that they are stored in. If the faces
 output variable is not specified, they will not be read so it 
 should execute pretty quickly.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [vertex_coords, faces] = read_surf(fname)
0002 %
0003 % [vertex_coords, faces] = read_surf(fname)
0004 % reads a the vertex coordinates and face lists from a surface file
0005 % note that reading the faces from a quad file can take a very long
0006 % time due to the goofy format that they are stored in. If the faces
0007 % output variable is not specified, they will not be read so it
0008 % should execute pretty quickly.
0009 %
0010 
0011 
0012 %
0013 % read_surf.m
0014 %
0015 % Original Author: Bruce Fischl
0016 % CVS Revision Info:
0017 %    $Author: nicks $
0018 %    $Date: 2007/01/10 22:55:10 $
0019 %    $Revision: 1.3 $
0020 %
0021 % Copyright (C) 2002-2007,
0022 % The General Hospital Corporation (Boston, MA).
0023 % All rights reserved.
0024 %
0025 % Distribution, usage and copying of this software is covered under the
0026 % terms found in the License Agreement file named 'COPYING' found in the
0027 % FreeSurfer source code root directory, and duplicated here:
0028 % https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
0029 %
0030 % General inquiries: freesurfer@nmr.mgh.harvard.edu
0031 % Bug reports: analysis-bugs@nmr.mgh.harvard.edu
0032 %
0033 
0034 
0035 %fid = fopen(fname, 'r') ;
0036 %nvertices = fscanf(fid, '%d', 1);
0037 %all = fscanf(fid, '%d %f %f %f %f\n', [5, nvertices]) ;
0038 %curv = all(5, :)' ;
0039 
0040 % open it as a big-endian file
0041 
0042 
0043 %QUAD_FILE_MAGIC_NUMBER =  (-1 & 0x00ffffff) ;
0044 %NEW_QUAD_FILE_MAGIC_NUMBER =  (-3 & 0x00ffffff) ;
0045 
0046 TRIANGLE_FILE_MAGIC_NUMBER =  16777214 ;
0047 QUAD_FILE_MAGIC_NUMBER =  16777215 ;
0048 
0049 fid = fopen(fname, 'rb', 'b') ;
0050 if (fid < 0)
0051   str = sprintf('could not open curvature file %s.', fname) ;
0052   error(str) ;
0053 end
0054 magic = fread3(fid) ;
0055 
0056 if(magic == QUAD_FILE_MAGIC_NUMBER)
0057   vnum = fread3(fid) ;
0058   fnum = fread3(fid) ;
0059   vertex_coords = fread(fid, vnum*3, 'int16') ./ 100 ; 
0060   if (nargout > 1)
0061     for i=1:fnum
0062       for n=1:4
0063     faces(i,n) = fread3(fid) ;
0064       end
0065     end
0066   end
0067 elseif (magic == TRIANGLE_FILE_MAGIC_NUMBER)
0068   fgets(fid) ;
0069   fgets(fid) ;
0070   vnum = fread(fid, 1, 'int32') ;
0071   fnum = fread(fid, 1, 'int32') ;
0072   vertex_coords = fread(fid, vnum*3, 'float32') ; 
0073   faces = fread(fid, fnum*3, 'int32') ;
0074   faces = reshape(faces, 3, fnum)' ;
0075 end
0076 
0077 vertex_coords = reshape(vertex_coords, 3, vnum)' ;
0078 fclose(fid) ;

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