Home > bioelectromagnetism > mesh_write_freesurfer.m

mesh_write_freesurfer

PURPOSE ^

mesh_write_freesurfer - Save mesh to FreeSurfer (.asc) file

SYNOPSIS ^

function mesh_write_freesurfer(p)

DESCRIPTION ^

 mesh_write_freesurfer - Save mesh to FreeSurfer (.asc) file

 USEAGE: mesh_write_freesurfer(p)

 Write a binary surface file for each mesh in p.mesh.data.  If any 
 cells of p.mesh.data.meshtype are empty, they will be skipped.
 
 This function will also output an *.w overlay file from the
 vertex scalar data contained in p.mesh.data.Cdata - if it 
 contains a timeseries, the output values will be taken from 
 the column specified in p.mesh.samplePoint.
 
 All output is handled by fs_write_surf and fs_write_wfile.
 
 See the FreeSurfer website at http://surfer.nmr.mgh.harvard.edu/
 for more information.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function mesh_write_freesurfer(p)
0002 
0003 % mesh_write_freesurfer - Save mesh to FreeSurfer (.asc) file
0004 %
0005 % USEAGE: mesh_write_freesurfer(p)
0006 %
0007 % Write a binary surface file for each mesh in p.mesh.data.  If any
0008 % cells of p.mesh.data.meshtype are empty, they will be skipped.
0009 %
0010 % This function will also output an *.w overlay file from the
0011 % vertex scalar data contained in p.mesh.data.Cdata - if it
0012 % contains a timeseries, the output values will be taken from
0013 % the column specified in p.mesh.samplePoint.
0014 %
0015 % All output is handled by fs_write_surf and fs_write_wfile.
0016 %
0017 % See the FreeSurfer website at http://surfer.nmr.mgh.harvard.edu/
0018 % for more information.
0019 %
0020 
0021 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0022 
0023 % Licence:  GNU GPL, no implied or express warranties
0024 % History:  10/2002, Darren.Weber_at_radiology.ucsf.edu
0025 %           01/2003, Darren.Weber_at_radiology.ucsf.edu
0026 %                    now using fs_write_surf and fs_write_wfile
0027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0028 
0029 fprintf('\nMESH_WRITE_FREESURFER...\n');
0030 
0031 if ~exist('p','var'),
0032     error('...no input p struct.\n');
0033 elseif isempty(p),
0034     error('...input p struct is empty.\n');
0035 elseif isempty(p.mesh.data),
0036     error('...input p struct has no mesh data.\n');
0037 end
0038 
0039 [path,name,ext] = fileparts(strcat(p.mesh.path,filesep,p.mesh.file));
0040 file = fullfile(path,[name ext]);
0041 
0042 fprintf('...writing FreeSurfer/ASCII meshes to:\n\t%s\n',path);
0043 
0044 tic;
0045 
0046 Meshes = p.mesh.data.meshtype;
0047 
0048 for i=1:size(Meshes,2),
0049     
0050     if Meshes{i},
0051         
0052         vertices = p.mesh.data.vertices{i};
0053         faces    = p.mesh.data.faces{i};
0054         
0055         [path,name,ext] = fileparts(file);
0056         name = strcat(name,'.',Meshes{i});
0057         file = fullfile(path,[name ext]);
0058         
0059         % Convert vertices from meters to mm
0060         vertices = vertices .* 1000;
0061         fs_write_surf(file,vertices,faces);
0062         
0063         % check for mesh Cdata
0064         if size(p.mesh.data.Cdata,2) >= i,
0065             if p.mesh.data.Cdata{i},
0066                 if size(p.mesh.data.Cdata{i},2) > 1,
0067                     % Obtain the Cdata at the selected time point
0068                     w = p.mesh.data.Cdata{i}(:,p.mesh.samplePoint);
0069                 else
0070                     w = p.mesh.data.Cdata{i};
0071                 end
0072             end
0073         end
0074         
0075         % Output associated overlay file (*.w)
0076         file = fullfile(path,[name '.w']);
0077         fs_write_wfile(file,w);
0078         
0079         
0080         % ascii output format replaced by binary option 01/2003
0081         % write_freesurfer(file,vertices,faces,Meshes{i});
0082         
0083     end
0084 end
0085 
0086 t=toc; fprintf('...done (%5.2f sec).\n\n',t);
0087 
0088 return
0089 
0090 
0091 % following asc output is obsolete, given fs_write_surf, 01/2003
0092 
0093 
0094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0096 function write_freesurfer(file,vertex,face,meshtype)
0097 
0098     [path,name,ext] = fileparts(file);
0099     ext = [ext,'.asc'];
0100     
0101     name = strcat(name,'.',meshtype);
0102     file = fullfile(path,[name ext]);
0103     
0104     fid = fopen(file,'w','ieee-le');
0105     
0106     if(fid == -1),
0107         msg = sprintf('...could not open file:\n\t%s\n',file);
0108         error(msg);
0109     else
0110         
0111         fprintf('...writing tesselation: %s\n',[name ext]);
0112         
0113         %-------------------------------------------------
0114         % Output header
0115         
0116         Nvertex = size(vertex,1);
0117         Nfaces  = size(face,1);
0118         
0119         fprintf(fid,'%d %d\n',Nvertex,Nfaces);
0120         
0121         %-------------------------------------------------
0122         % Output vertices
0123         
0124         % Convert vertices from meters to mm
0125         vertex(:,1) = vertex(:,1) .* 1000;
0126         vertex(:,2) = vertex(:,2) .* 1000;
0127         vertex(:,3) = vertex(:,3) .* 1000;
0128         
0129         if size(vertex,2) < 4,
0130             vertex(:,4) = zeros(size(vertex,1),1);
0131         end
0132         
0133         vertex(:,4) = zeros(size(vertex,1),1);
0134         
0135         % Write vertex matrix
0136         for v = 1:Nvertex,
0137             fprintf(fid,'%f %f %f %g\n',vertex(v,1),vertex(v,2),vertex(v,3),vertex(v,4));
0138             %fprintf(fid,'%12.6f %12.6f %12.6f %+20.8g\n',vertex(v,1),vertex(v,2),vertex(v,3),vertex(v,4));
0139             %fprintf(fid,'%12.6f %12.6f %12.6f %12.6f\n',vertex(v,1),vertex(v,2),vertex(v,3),vertex(v,4));
0140         end
0141         
0142         %-------------------------------------------------
0143         % Output faces
0144         
0145         % Check for last column of face
0146         if size(face,2) < 4,
0147             face(:,4) = ones(size(face,1),1); % -1 below
0148         end
0149         % matlab vertex indices start at one, so
0150         % subtract 1 because FreeSurfer vertices start at zero
0151         face = face - 1;
0152         % Write face matrix
0153         for t = 1:Nfaces,
0154             fprintf(fid,'%d %d %d %d\n',face(t,1),face(t,2),face(t,3),face(t,4));
0155         end
0156         
0157         
0158         fclose(fid);
0159         
0160     end
0161     
0162 return

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