Writes coordinates and triangles to an array of .obj or FreeSurfer files. Usage: SurfStatWriteSurf( filenames, surf [,ab] ); filenames = .obj or FS file name (n=1) or n x k cell array of file names. If extension=.obj, writes a .obj file (ASCII or binary), else writes a FS file (binary only). If k>1 then the data is split equally between the k files. surf.coord = n x v x 3 array of coordinates, v=#vertices, 3 x v if n=1. surf.tri = t x 3 matrix of triangle indices, 1-based, t=#triangles. ab = 'a' for ASCII (default) or 'b' for binary. For .obj files, the following are optional: surf.normal = n x v x 3 array of surface normals, 3 x v if n=1. surf.colr = n x 4 matrix of colours for the whole surface, or n x v x 4 aray of colours for each vertex, 4 x v if n=1, either uint8 in [0 255], or float in [0 1]. Default is ones(n,4).
0001 function SurfStatWriteSurf( filenames, surf, ab ); 0002 0003 %Writes coordinates and triangles to an array of .obj or FreeSurfer files. 0004 % 0005 % Usage: SurfStatWriteSurf( filenames, surf [,ab] ); 0006 % 0007 % filenames = .obj or FS file name (n=1) or n x k cell array of file names. 0008 % If extension=.obj, writes a .obj file (ASCII or binary), 0009 % else writes a FS file (binary only). 0010 % If k>1 then the data is split equally between the k files. 0011 % surf.coord = n x v x 3 array of coordinates, v=#vertices, 3 x v if n=1. 0012 % surf.tri = t x 3 matrix of triangle indices, 1-based, t=#triangles. 0013 % ab = 'a' for ASCII (default) or 'b' for binary. 0014 % 0015 % For .obj files, the following are optional: 0016 % surf.normal = n x v x 3 array of surface normals, 3 x v if n=1. 0017 % surf.colr = n x 4 matrix of colours for the whole surface, 0018 % or n x v x 4 aray of colours for each vertex, 4 x v if n=1, 0019 % either uint8 in [0 255], or float in [0 1]. Default is ones(n,4). 0020 0021 if nargin<3 0022 ab='a'; 0023 end 0024 0025 if isstr(filenames) 0026 sf=filenames; 0027 filenames=cell(1,1); 0028 filenames(1)={sf}; 0029 end 0030 [n,k]=size(filenames); 0031 0032 v=size(surf.coord,2+(n>1)); 0033 t=size(surf.tri,1); 0034 0035 if rem(v,k)>0 | rem(t,k)>0 0036 return 0037 end 0038 0039 vk=round(v/k); 0040 v1=1:vk:v; 0041 v2=vk:vk:v; 0042 tk=round(t/k); 0043 t1=1:tk:t; 0044 t2=tk:tk:t; 0045 0046 for j=1:k 0047 surf1.tri=surf.tri(t1(j):t2(j),:)-v1(j)+1; 0048 for i=1:n 0049 if n==1 0050 surf1.coord=surf.coord(:,v1(j):v2(j)); 0051 if isfield(surf,'colr') 0052 if size(colr,1)==1 0053 surf1.colr=surf.colr'; 0054 else 0055 surf1.colr=surf.colr(:,v1(j):v2(j)); 0056 end 0057 end 0058 if isfield(surf,'normal') 0059 surf1.normal=surf.normal(:,v1(j):v2(j)); 0060 end 0061 else 0062 surf1.coord=squeeze(surf.coord(i,v1(j):v2(j),:))'; 0063 if isfield(surf,'colr') 0064 if ndims(colr)==2 0065 surf1.colr=surf.colr(i,:)'; 0066 else 0067 surf1.colr=squeeze(surf.colr(i,v1(j):v2(j),:))'; 0068 end 0069 end 0070 if isfield(surf,'normal') 0071 surf1.normal=squeeze(surf.normal(i,v1(j):v2(j),:))'; 0072 end 0073 end 0074 SurfStatWriteSurf1(filenames{i,j},surf1,ab); 0075 end 0076 end 0077 0078 return 0079 end 0080 0081