Home > bioelectromagnetism > freesurfer_read_ascii.m

freesurfer_read_ascii

PURPOSE ^

freesurfer_read_ascii - Read FreeSurfer tesselation (.txt)

SYNOPSIS ^

function [vertices,faces] = freesurfer_read_ascii(file)

DESCRIPTION ^

 freesurfer_read_ascii - Read FreeSurfer tesselation (.txt)

 [vertices,faces] = freesurfer_read_ascii(file)

 This function will load an ascii file that contains a one
 line specification of the number of vertices and faces,
 followed by rows of vertex points and then rows of face
 indices into the vertex rows.  Each vertex row contains
 3 x,y,z coordinates and a colour/potential value.  Each 
 face row contains three vertex indices followed by a
 colour/potential value.  Vertices in the .txt file are
 indexed from zero, but those returned are indexed from
 one.
 
 See also the freesurfer_read_tri function to load
 the data from the BEM .tri tesselations, which have a
 different text format from those of the .txt/.asc files
 that are created by the mris_convert function of freesurfer.

 The freesurfer tesselations may contain too many faces
 for efficient computations.  If so, try 'reducepatch'.

 The vertex coordinates are in mm.  The FreeSurfer coordinate
 system is too confusing to explain here; see the FreeSurfer
 homepage or some nice documentation by Graham Wideman.

 The returned values can be input to the patch command, like so:

    Hpatch = patch('Vertices',vertices,'Faces',faces,...
                   'EdgeColor',[.8 .8 .8],'FaceColor',[0.9 0.9 0.9]);

 This will plot the mesh as a patch object.  See the patch command
 and matlab help for more information on coloring this object.

 Freesurfer website: http://surfer.nmr.mgh.harvard.edu/

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [vertices,faces] = freesurfer_read_ascii(file)
0002 
0003 % freesurfer_read_ascii - Read FreeSurfer tesselation (.txt)
0004 %
0005 % [vertices,faces] = freesurfer_read_ascii(file)
0006 %
0007 % This function will load an ascii file that contains a one
0008 % line specification of the number of vertices and faces,
0009 % followed by rows of vertex points and then rows of face
0010 % indices into the vertex rows.  Each vertex row contains
0011 % 3 x,y,z coordinates and a colour/potential value.  Each
0012 % face row contains three vertex indices followed by a
0013 % colour/potential value.  Vertices in the .txt file are
0014 % indexed from zero, but those returned are indexed from
0015 % one.
0016 %
0017 % See also the freesurfer_read_tri function to load
0018 % the data from the BEM .tri tesselations, which have a
0019 % different text format from those of the .txt/.asc files
0020 % that are created by the mris_convert function of freesurfer.
0021 %
0022 % The freesurfer tesselations may contain too many faces
0023 % for efficient computations.  If so, try 'reducepatch'.
0024 %
0025 % The vertex coordinates are in mm.  The FreeSurfer coordinate
0026 % system is too confusing to explain here; see the FreeSurfer
0027 % homepage or some nice documentation by Graham Wideman.
0028 %
0029 % The returned values can be input to the patch command, like so:
0030 %
0031 %    Hpatch = patch('Vertices',vertices,'Faces',faces,...
0032 %                   'EdgeColor',[.8 .8 .8],'FaceColor',[0.9 0.9 0.9]);
0033 %
0034 % This will plot the mesh as a patch object.  See the patch command
0035 % and matlab help for more information on coloring this object.
0036 %
0037 % Freesurfer website: http://surfer.nmr.mgh.harvard.edu/
0038 %
0039 
0040 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:34 $
0041 
0042 % Licence:  GNU GPL, no implied or express warranties
0043 % History:  03/2002 Darren.Weber_at_radiology.ucsf.edu
0044 %           02/2004 Darren.Weber_at_radiology.ucsf.edu
0045 %                   previously called mesh_freesurfer2matlab
0046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0047 
0048 fid = fopen(file,'r');
0049 
0050 if isequal(fid,-1),
0051     S=sprintf('Could not open file: "%s"',file);
0052     error(S);
0053 else
0054     
0055     fprintf('...Reading FreeSurfer Tesselation Data\n');
0056     
0057     tic;
0058     
0059     % Check for comment on first line of file
0060     frewind(fid); temp = fscanf(fid,'%s',1); frewind(fid);
0061     if findstr(temp,'#'), temp = fgetl(fid); end
0062     
0063     % Read number of vertices/faces
0064     Nvertices = fscanf(fid,'%d',1);
0065     Nfaces    = fscanf(fid,'%d',1);
0066     
0067     % Read vertices
0068     fprintf('...Reading %d Vertices\n',Nvertices);
0069     vertices = fscanf(fid,'%f',[4,Nvertices]);
0070     % remove last row (all zeros) and translate
0071     vertices = vertices(1:3,:)';
0072     
0073     % Read faces
0074     fprintf('...Reading %d Faces\n',Nfaces);
0075     faces = fscanf(fid,'%d',[4,Nfaces]);
0076     % remove last row (all zeros), translate and
0077     % add 1 because FreeSurfer vertices start at zero
0078     faces = faces(1:3,:)' + 1;
0079     
0080     fclose(fid);
0081     
0082     t = toc;
0083     fprintf('...done (%6.2f sec).\n',t);
0084     
0085 end
0086 
0087 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003