Home > bioelectromagnetism > mesh_bet2matlab.m

mesh_bet2matlab

PURPOSE ^

mesh_bet2matlab - Read FSL BET tesselation (.coo/.dat)

SYNOPSIS ^

function [FV] = mesh_bet2matlab(fileprefix)

DESCRIPTION ^

 mesh_bet2matlab - Read FSL BET tesselation (.coo/.dat)
 
 USEAGE: [FV] = mesh_bet2matlab(fileprefix)
 
 This function will load ascii files created by the FSL
 BET function, see http://www.fmrib.ox.ac.uk/fsl
 
 When used with the -x option, the FSL BET function can 
 output the tesselation of the brain surface (ie, only 
 a fairly smooth, non-detailed, surface), eg:
 
 bet T1 T1_brain -x
 
 The brain tesselation is output into two files, eg:
 
   T1_brain.coo
   T1_brain.dat
 
 The T1_brain.coo file contains the co-ordinates of the
 vertices, and the .dat file contains the details of 
 the links between vertices (each line corresponds to 
 each vertex in the .coo file, and the 1's correspond 
 to which other vertices they are connected to).
 
 The returned FV struct contains the 'vertices' and
 'faces' matrices, which can be input to the patch 
 command, eg:
 
 Hpatch = patch('Vertices',FV.vertices,'Faces',FV.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.
 
 See also: mesh_freesurfer2matlab

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [FV] = mesh_bet2matlab(fileprefix)
0002 
0003 % mesh_bet2matlab - Read FSL BET tesselation (.coo/.dat)
0004 %
0005 % USEAGE: [FV] = mesh_bet2matlab(fileprefix)
0006 %
0007 % This function will load ascii files created by the FSL
0008 % BET function, see http://www.fmrib.ox.ac.uk/fsl
0009 %
0010 % When used with the -x option, the FSL BET function can
0011 % output the tesselation of the brain surface (ie, only
0012 % a fairly smooth, non-detailed, surface), eg:
0013 %
0014 % bet T1 T1_brain -x
0015 %
0016 % The brain tesselation is output into two files, eg:
0017 %
0018 %   T1_brain.coo
0019 %   T1_brain.dat
0020 %
0021 % The T1_brain.coo file contains the co-ordinates of the
0022 % vertices, and the .dat file contains the details of
0023 % the links between vertices (each line corresponds to
0024 % each vertex in the .coo file, and the 1's correspond
0025 % to which other vertices they are connected to).
0026 %
0027 % The returned FV struct contains the 'vertices' and
0028 % 'faces' matrices, which can be input to the patch
0029 % command, eg:
0030 %
0031 % Hpatch = patch('Vertices',FV.vertices,'Faces',FV.faces,...
0032 %                'EdgeColor',[.8 .8 .8],...
0033 %                'FaceColor',[0.9 0.9 0.9]);
0034 %
0035 % This will plot the mesh as a patch object.  See the patch
0036 % command and matlab help for more information on coloring
0037 % this object.
0038 %
0039 % See also: mesh_freesurfer2matlab
0040 %
0041 
0042 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0043 
0044 % Licence:  GNU GPL, no implied or express warranties
0045 % History:  03/02 Darren.Weber_at_radiology.ucsf.edu
0046 %
0047 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0048 
0049 if ~exist('fileprefix','var'),
0050     error('No input fileprefix');
0051 end
0052 
0053 if findstr('.coo',fileprefix),
0054     fprintf('MESH_BET2MATLAB: Removing .coo extension from ''%s''\n',fileprefix);
0055     fileprefix = strrep(fileprefix,'.coo','');
0056 end
0057 if findstr('.dat',fileprefix),
0058     fprintf('MESH_BET2MATLAB: Removing .dat extension from ''%s''\n',fileprefix);
0059     fileprefix = strrep(fileprefix,'.dat','');
0060 end
0061 
0062 FV.vertices = readCOO(fileprefix);
0063 
0064 Nvertices   = size(FV.vertices,1);
0065 
0066 edgematrix  = readDAT(fileprefix,Nvertices);
0067 
0068 FV.faces    = edge2face(FV.vertices,edgematrix);
0069 
0070 return
0071 
0072 
0073 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0075 function faces = edge2face(vertices,edgematrix)
0076     
0077     Nvertices = size(vertices,1);
0078     
0079     plot3(vertices(:,1),vertices(:,2),vertices(:,3),'b.'), hold on
0080     
0081     for i = 1:Nvertices,
0082         
0083         % Get current vertex XYZ coordinates
0084         v  = vertices(i,:);
0085         
0086         plot3(v(:,1),v(:,2),v(:,3),'go');
0087         
0088         % Get neighbouring vertex XYZ coordinates
0089         vn = vertices(find(edgematrix(i,:)),:);
0090         
0091         plot3(vn(:,1),vn(:,2),vn(:,3),'ro');
0092         
0093         
0094         faces = tri([v;vn]);
0095         
0096         
0097         
0098         
0099         return
0100         
0101         
0102         
0103         
0104         
0105     end
0106     
0107     faces = [];
0108     
0109 return
0110 
0111 
0112 function faces = tri(V)
0113     
0114     % Must identify angle of rotation from
0115     % central vertex, V(1,:), to all other vertices
0116     % in a counterclockwise direction
0117     
0118     % First translate all vertices to an origin
0119     % given by the central vertex
0120     Vo = V - repmat(V(1,:),size(V,1),1);
0121     
0122     % Find direction cosines for line from centre to vertex
0123     d = zeros(size(Vo,1),1);
0124     l = zeros(size(Vo,1),1);
0125     m = zeros(size(Vo,1),1);
0126     n = zeros(size(Vo,1),1);
0127     
0128     for i = 1:length(Vo),
0129         
0130         x = Vo(i,1);  y = Vo(i,2);  z = Vo(i,3);
0131         
0132         d(i) = sqrt( (x)^2 + (y)^2 + (z)^2 );
0133         
0134         if d(i) > 0,
0135             l(i) = x/d(i); % cos alpha
0136             m(i) = y/d(i); % cos beta
0137             n(i) = z/d(i); % cos gamma
0138         end
0139     end
0140     
0141     
0142     faces = [];
0143     
0144     
0145     %hold off
0146     %plot3(Vo(:,1),Vo(:,2),Vo(:,3),'b.'), view(2), hold on
0147     
0148     
0149 return
0150 
0151 
0152 
0153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0154 function vertices = readCOO(fileprefix)
0155     
0156     [path,name,ext] = fileparts(strcat(fileprefix,'.coo'));
0157     file = fullfile(path,[name ext]);
0158     
0159     fid = fopen(file,'r');
0160     
0161     if isequal(fid,-1),
0162         S=sprintf('Could not open file: "%s"',file);
0163         error(S);
0164     else
0165         fprintf('...Reading BET vertices...');
0166         tic;
0167         
0168         % Read vertices
0169         vertices = fscanf(fid,'%s%f%f%f',[4,inf]);
0170         fclose(fid);
0171         
0172         % remove last row (all zeros) and translate
0173         vertices = vertices(2:4,:)';
0174         
0175         t = toc;
0176         fprintf('...done (%6.2f sec).\n',t);
0177     end
0178 
0179 return
0180 
0181 
0182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0183 function edgematrix = readDAT(fileprefix,Nvertices)
0184     
0185     [path,name,ext] = fileparts(strcat(fileprefix,'.dat'));
0186     file = fullfile(path,[name ext]);
0187     
0188     fid = fopen(file,'r');
0189     
0190     if isequal(fid,-1),
0191         S=sprintf('Could not open file: "%s"',file);
0192         error(S);
0193     else
0194         fprintf('...Reading BET edge matrix...');
0195         tic;
0196         
0197         % Read faces
0198         edgematrix = fscanf(fid,'%1d',[Nvertices,Nvertices]);
0199         fclose(fid);
0200         
0201         t = toc;
0202         fprintf('...done (%6.2f sec).\n',t);
0203     end
0204 return

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