Home > bioelectromagnetism > freesurfer_read_tri.m

freesurfer_read_tri

PURPOSE ^

freesurfer_read_tri - Read FreeSurfer tesselation (.tri)

SYNOPSIS ^

function [vertices,faces] = freesurfer_read_tri(file)

DESCRIPTION ^

 freesurfer_read_tri - Read FreeSurfer tesselation (.tri)
 
 [vertices,faces] = freesurfer_read_tri(file)
 
 This function will load an ascii file that contains a one
 line specification of the number of vertices followed 
 by rows of vertex points.  It then reads a one line
 specification of the number of faces followed by rows
 of face indices into the vertex rows.  Each vertex row 
 contains a vertex index number and 3 x,y,z coordinates. 
 Each face row contains a face index and three vertex 
 indices.  Vertices in the .tri file are indexed from one
 and those returned are indexed from one.
 
 See also the mesh_freesurfer2matlab function to load
 the tesselations that are created by the mris_convert
 function of freesurfer, which have a different text format
 from those of the BEM .tri files.

 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 matrices 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.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [vertices,faces] = freesurfer_read_tri(file)
0002 
0003 % freesurfer_read_tri - Read FreeSurfer tesselation (.tri)
0004 %
0005 % [vertices,faces] = freesurfer_read_tri(file)
0006 %
0007 % This function will load an ascii file that contains a one
0008 % line specification of the number of vertices followed
0009 % by rows of vertex points.  It then reads a one line
0010 % specification of the number of faces followed by rows
0011 % of face indices into the vertex rows.  Each vertex row
0012 % contains a vertex index number and 3 x,y,z coordinates.
0013 % Each face row contains a face index and three vertex
0014 % indices.  Vertices in the .tri file are indexed from one
0015 % and those returned are indexed from one.
0016 %
0017 % See also the mesh_freesurfer2matlab function to load
0018 % the tesselations that are created by the mris_convert
0019 % function of freesurfer, which have a different text format
0020 % from those of the BEM .tri files.
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 matrices 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 
0038 
0039 % Copyright (C) 2002  Darren L. Weber
0040 %
0041 % This program is free software; you can redistribute it and/or
0042 % modify it under the terms of the GNU General Public License
0043 % as published by the Free Software Foundation; either version 2
0044 % of the License, or (at your option) any later version.
0045 %
0046 % This program is distributed in the hope that it will be useful,
0047 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0048 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0049 % GNU General Public License for more details.
0050 %
0051 % You should have received a copy of the GNU General Public License
0052 % along with this program; if not, write to the Free Software
0053 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
0054 % USA.
0055 
0056 % History:  03/2002 Darren.Weber_at_radiology.ucsf.edu
0057 %           02/2004 Darren.Weber_at_radiology.ucsf.edu
0058 %                   previously called mesh_freesurferTRI2matlab
0059 %
0060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0061 
0062 ver = '$Revision: 1.2 $ $Date: 2005/01/21 04:33:50 $';
0063 fprintf('FREESURFER_READ_TRI [v %s]\n',ver(11:15));
0064 
0065 fid = fopen(file,'r');
0066 
0067 if isequal(fid,-1),
0068     msg = sprintf('Could not open file: %s',file);
0069     error(msg);
0070 else
0071     
0072     fprintf('...reading FreeSurfer triangulation (.tri)\n');
0073     
0074     tic;
0075     
0076     % Check for comment on first line of file
0077     frewind(fid); temp = fscanf(fid,'%s',1); frewind(fid);
0078     if findstr(temp,'#'), temp = fgetl(fid); end
0079     
0080     % Read vertices
0081     Nvertices = fscanf(fid,'%d',1);
0082     fprintf('...Reading %d Vertices\n',Nvertices);
0083     vertices = fscanf(fid,'%f',[4,Nvertices]);
0084     % remove first row (index) and translate
0085     vertices = vertices(2:4,:)';
0086     
0087     % Read faces
0088     Nfaces = fscanf(fid,'%d',1);
0089     fprintf('...Reading %d Faces\n',Nfaces);
0090     faces = fscanf(fid,'%d',[4,Nfaces]);
0091     % remove first row (index) & translate
0092     faces = faces(2:4,:)';
0093     
0094     fclose(fid);
0095     
0096     t = toc;
0097     fprintf('...done (%6.2f sec).\n\n',t);
0098     
0099 end
0100 
0101 return

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