| function ft_write_headshape(filename, bnd, varargin) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| fileformat = ft_getopt(varargin, 'format', 'unknown'); |
| data = ft_getopt(varargin, 'data'); |
| unit = ft_getopt(varargin, 'unit'); |
| metadata = ft_getopt(varargin, 'metadata'); |
|
|
| if ~isfield(bnd, 'pnt') && isfield(bnd, 'pos') |
| bnd.pnt = bnd.pos; |
| end |
|
|
| if ~isstruct(bnd) |
| bnd.pnt = bnd; |
| end |
|
|
| if ~isempty(unit) |
| |
| bnd = ft_convert_units(bnd, unit); |
| end |
|
|
| switch fileformat |
| case 'mne_pos' |
| fid = fopen(filename, 'wt'); |
| |
| bnd = ft_convert_units(bnd, 'mm'); |
| n=size(bnd.pnt,1); |
| for line = 1:n |
| num = bnd.pnt(line,1); |
| fprintf(fid, '%-1.0f ',num); |
| num = bnd.pnt(line,2); |
| fprintf(fid, '%-1.0f ',num); |
| num = bnd.pnt(line,3); |
| fprintf(fid, '%-1.0f\n',num); |
| end |
| fclose(fid); |
| |
| case 'mne_tri' |
| fid = fopen(filename, 'wt'); |
| |
| bnd = ft_convert_units(bnd, 'mm'); |
| n=size(bnd.pnt,1); |
| fprintf(fid, '%-1.0f\n',n); |
| for line = 1:n |
| num=bnd.pnt(line,1); |
| fprintf(fid,'%g ', num); |
| num = bnd.pnt(line,2); |
| fprintf(fid, '%g ',num); |
| num = bnd.pnt(line,3); |
| fprintf(fid, '%g\n',num); |
| end |
| n=size(bnd.tri,1); |
| fprintf(fid, '%-1.0f\n',n); |
| for line = 1:n |
| num=bnd.tri(line,1); |
| fprintf(fid,'%-1.0f ', num); |
| num = bnd.tri(line,2); |
| fprintf(fid, '%-1.0f ',num); |
| num = bnd.tri(line,3); |
| fprintf(fid, '%-1.0f\n',num); |
| end |
| fclose(fid); |
| |
| case 'off' |
| write_off(filename,bnd.pnt,bnd.tri); |
| |
| case 'vista' |
| if ft_hastoolbox('simbio',1) |
| |
| if isfield(bnd,'hex') |
| write_vista_mesh(filename,bnd.pnt,bnd.hex,bnd.index); |
| elseif isfield(bnd,'tet') |
| write_vista_mesh(filename,bnd.pnt,bnd.tet,bnd.index); |
| else |
| error('unknown format') |
| end |
| else |
| error('You need Simbio/Vista toolbox to write a .v file') |
| end |
| |
| case 'tetgen' |
| |
| |
| surf_to_tetgen(filename, bnd.pnt, bnd.tri, 302*ones(size(bnd.tri,1),1),[],[]); |
| |
| case 'vtk' |
| [p, f, x] = fileparts(filename); |
| filename = fullfile(p, [f, '.vtk']); |
| if isfield(bnd, 'tri') |
| write_vtk(filename, bnd.pnt, bnd.tri); |
| elseif isfield(bnd, 'tet') |
| write_vtk(filename, bnd.pnt, bnd.tet); |
| elseif isfield(bnd, 'hex') |
| write_vtk(filename, bnd.pnt, bnd.hex); |
| end |
| |
| case {'ply', 'ply_ascii', 'ply_binary'} |
| [p, f, x] = fileparts(filename); |
| filename = fullfile(p, [f, '.ply']); |
| |
| if isfield(bnd, 'pnt') |
| vertices = bnd.pnt; |
| elseif isfield(bnd, 'pos') |
| vertices = bnd.pos; |
| end |
| |
| if isfield(bnd, 'tri') |
| elements = bnd.tri; |
| elseif isfield(bnd, 'tet') |
| elements = bnd.tet; |
| elseif isfield(bnd, 'hex') |
| elements = bnd.hex; |
| end |
| |
| if length(fileformat)>4 |
| write_ply(filename, vertices, elements, fileformat(5:end)); |
| else |
| write_ply(filename, vertices, elements, 'ascii'); |
| end |
| |
| case 'stl' |
| nrm = normals(bnd.pnt, bnd.tri, 'triangle'); |
| write_stl(filename, bnd.pnt, bnd.tri, nrm); |
| |
| case 'gifti' |
| ft_hastoolbox('gifti', 1); |
| |
| bnd = ft_convert_units(bnd, 'mm'); |
| |
| |
| tmp = []; |
| tmp.vertices = bnd.pnt; |
| tmp.faces = bnd.tri; |
| if ~isempty(data) |
| tmp.cdata = data; |
| end |
| tmp = gifti(tmp); |
| |
| |
| if ~isempty(metadata) |
| if isstruct(metadata) |
| fnames = fieldnames(metadata); |
| if any(strcmp(fnames,'name')) && any(strcmp(fnames,'value')) |
| |
| |
| for k = 1:numel(tmp.private.data) |
| n(k,1) = size(tmp.private.data{k}.data,1); |
| end |
| ix = find(n==size(tmp.vertices,1)); |
| tmp.private.data{ix}.metadata = metadata; |
| else |
| error('the metadata structure should contain the fields ''name'' and ''value'''); |
| end |
| else |
| error('metadata should be provided as a struct-array'); |
| end |
| end |
| |
| save(tmp, filename); |
|
|
| case 'freesurfer' |
| ft_hastoolbox('freesurfer', 1); |
| write_surf(filename, bnd.pnt, bnd.tri); |
| |
| case [] |
| error('you must specify the output format'); |
| |
| otherwise |
| error('unsupported output format "%s"', fileformat); |
| end |
|
|
|
|