0001 function mesh_write_freesurfer(p)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
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
0060 vertices = vertices .* 1000;
0061 fs_write_surf(file,vertices,faces);
0062
0063
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
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
0076 file = fullfile(path,[name '.w']);
0077 fs_write_wfile(file,w);
0078
0079
0080
0081
0082
0083 end
0084 end
0085
0086 t=toc; fprintf('...done (%5.2f sec).\n\n',t);
0087
0088 return
0089
0090
0091
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
0115
0116 Nvertex = size(vertex,1);
0117 Nfaces = size(face,1);
0118
0119 fprintf(fid,'%d %d\n',Nvertex,Nfaces);
0120
0121
0122
0123
0124
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
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
0139
0140 end
0141
0142
0143
0144
0145
0146 if size(face,2) < 4,
0147 face(:,4) = ones(size(face,1),1);
0148 end
0149
0150
0151 face = face - 1;
0152
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