Home > SurfStat > SurfStatWriteSurf1.m

SurfStatWriteSurf1

PURPOSE ^

Writes coordinates and triangles to a single .obj or FreeSurfer file.

SYNOPSIS ^

function surf = SurfStatWriteSurf1( filename, surf, ab );

DESCRIPTION ^

Writes coordinates and triangles to a single .obj or FreeSurfer file.

 Usage: surf = SurfStatWriteSurf1( filename, surf [,ab] );

 filename   = .obj or FS file name. If extension=.obj, writes a .obj file
              (ASCII or binary), else writes a FS file (binary only).
 surf.coord = 3 x v matrix of coordinates, v=#vertices.
 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 and returned if not present:
 surf.normal = 3 x v matrix of surface normals.
 surf.colr   = 4 x 1 vector of colours for the whole surface,
            or 4 x v matrix of colours for each vertex, either uint8
            in [0 255], or float in [0 1]. Default is [1 1 1 1]'.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function surf = SurfStatWriteSurf1( filename, surf, ab );
0002 
0003 %Writes coordinates and triangles to a single .obj or FreeSurfer file.
0004 %
0005 % Usage: surf = SurfStatWriteSurf1( filename, surf [,ab] );
0006 %
0007 % filename   = .obj or FS file name. If extension=.obj, writes a .obj file
0008 %              (ASCII or binary), else writes a FS file (binary only).
0009 % surf.coord = 3 x v matrix of coordinates, v=#vertices.
0010 % surf.tri   = t x 3 matrix of triangle indices, 1-based, t=#triangles.
0011 % ab         = 'a' for ASCII (default) or 'b' for binary.
0012 %
0013 % For .obj files, the following are optional and returned if not present:
0014 % surf.normal = 3 x v matrix of surface normals.
0015 % surf.colr   = 4 x 1 vector of colours for the whole surface,
0016 %            or 4 x v matrix of colours for each vertex, either uint8
0017 %            in [0 255], or float in [0 1]. Default is [1 1 1 1]'.
0018 
0019 v=size(surf.coord,2);
0020 t=size(surf.tri,1);
0021 
0022 [pathstr,name,ext] = fileparts(filename);
0023 if strcmp(ext,'.obj')
0024     % It's a .obj file
0025     
0026     if ~isfield(surf,'normal')
0027         u1=surf.coord(:,surf.tri(:,1));
0028         d1=surf.coord(:,surf.tri(:,2))-u1;
0029         d2=surf.coord(:,surf.tri(:,3))-u1;
0030         c=cross(d1,d2,1);
0031         surf.normal=zeros(3,v);
0032         for j=1:3
0033             for k=1:3
0034                 surf.normal(k,:)=surf.normal(k,:)+accumarray(surf.tri(:,j),c(k,:)')';
0035             end
0036         end
0037         surf.normal=surf.normal./(ones(3,1)*sqrt(sum(surf.normal.^2,1)));
0038     end
0039     if ~isfield(surf,'colr')
0040         surf.colr=[1 1 1 1]';
0041     end
0042 
0043     if nargin<3 | ab(1)=='a'
0044         fid=fopen(filename,'w');
0045         fprintf(fid,'P 0.3 0.3 0.4 10 1 %d \n',v);
0046         fprintf(fid,'%f %f %f \n',surf.coord);
0047         fprintf(fid,'  \n');
0048         fprintf(fid,'%f %f %f \n',surf.normal);
0049         fprintf(fid,'  \n');
0050         fprintf(fid,'%d %d \n',t,(size(surf.colr,2)>1)*2);
0051         if isa(surf.colr,'uint8')
0052             fprintf(fid,'%f %f %f %f \n',double(surf.colr)/255);
0053         else
0054             fprintf(fid,'%f %f %f %f \n',surf.colr);
0055         end
0056         fprintf(fid,'  \n');
0057         fprintf(fid,'%d %d %d %d %d %d %d %d \n',(1:t)*3);
0058         fprintf(fid,'  \n');
0059         fprintf(fid,'%d %d %d %d %d %d %d %d \n',surf.tri'-1);
0060         fclose(fid);
0061     else
0062         fid=fopen(filename,'w','b');
0063         fwrite(fid,uint8(112),'uint8');
0064         fwrite(fid,[0.3 0.3 0.4 10 1],'float');
0065         fwrite(fid,v,'int');
0066         fwrite(fid,surf.coord,'float');
0067         fwrite(fid,surf.normal,'float');
0068         fwrite(fid,t,'int');
0069         fwrite(fid,(size(surf.colr,2)>1)*2,'int');
0070         if isa(surf.colr,'uint8')
0071             fwrite(fid,surf.colr,'uint8');
0072         else
0073             fwrite(fid,uint8(round(surf.colr*255)),'uint8');
0074         end
0075         fwrite(fid,(1:t)*3,'int');
0076         fwrite(fid,surf.tri'-1,'int');
0077         fclose(fid);
0078     end
0079 else
0080     % Assume it's a FreeSurfer file
0081     fid = fopen(filename, 'wb', 'b') ;
0082     magic = 16777214;
0083     b1 = bitand(bitshift(magic, -16), 255) ;
0084     b2 = bitand(bitshift(magic, -8), 255) ;
0085     b3 = bitand(magic, 255) ;
0086     fwrite(fid, [b1 b2 b3], 'uchar') ;
0087     fwrite(fid, ['Created by SurfStat on ' datestr(now) char(10) char(10)], 'char');
0088     fwrite(fid, [v t], 'int32') ;
0089     fwrite(fid, surf.coord, 'float') ;
0090     fwrite(fid, surf.tri'-1, 'int32') ;
0091     fclose(fid);
0092 end
0093 
0094 return
0095 
0096

Generated on Fri 26-Sep-2008 14:05:29 by m2html © 2003