writevtkfile

PURPOSE ^

Useage: writevtkfile(filename, eidors_image );

SYNOPSIS ^

function writevtkfile(fn,varargin)

DESCRIPTION ^

 Useage: writevtkfile(filename, eidors_image );
      or writevtkfile(filename,model,image)
      or writevtkfile(fn,vtx,simp,vals);

 Example:  img= compare_3d_algs(1);
           writevtkfile('fname',img);

 This function writes 3D meshes and associated scalar/vector/tensor fields to a VTK (Visualization Tool Kit)(www.kitware.com) dataset file


 'filename' = name of the vtk file, the ".vtk" extension will be addedd automatically 
 vtx = matrix of the verticies
 elem = matrix desribing the simplecies
 val = matrix holding the field values (scalars/vectros/tensors)
 
 Depending wether the field is piecewise linear or piecewise constant and if it it scalar, vector or tensor, val can assume different valid sizes:

 val should have a number of rows equal to the number of simplecies or to the number of nodes in the mesh (piecewise constant/piecewise linear dataset)
 val should have a number of columns equal to 1,3 or 9 (scalar field / vector field / tensor field)

 The generated VTK file can then be displayed with MaiaVi (http://mayavi.sourceforge.net/)
 which has lots of nice features and is generally faster than MATALB 3D graphics

 CAVEAT: VTK supports only symmetrix tensors. It is left to the user to ensure that the data he is exporting 
 consists of symmetric tensors (as if such a check were performed by writeVTKfile, performance of the filter would suffer)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function writevtkfile(fn,varargin)
0002 % Useage: writevtkfile(filename, eidors_image );
0003 %      or writevtkfile(filename,model,image)
0004 %      or writevtkfile(fn,vtx,simp,vals);
0005 %
0006 % Example:  img= compare_3d_algs(1);
0007 %           writevtkfile('fname',img);
0008 %
0009 % This function writes 3D meshes and associated scalar/vector/tensor fields to a VTK (Visualization Tool Kit)(www.kitware.com) dataset file
0010 %
0011 %
0012 % 'filename' = name of the vtk file, the ".vtk" extension will be addedd automatically
0013 % vtx = matrix of the verticies
0014 % elem = matrix desribing the simplecies
0015 % val = matrix holding the field values (scalars/vectros/tensors)
0016 %
0017 % Depending wether the field is piecewise linear or piecewise constant and if it it scalar, vector or tensor, val can assume different valid sizes:
0018 %
0019 % val should have a number of rows equal to the number of simplecies or to the number of nodes in the mesh (piecewise constant/piecewise linear dataset)
0020 % val should have a number of columns equal to 1,3 or 9 (scalar field / vector field / tensor field)
0021 %
0022 % The generated VTK file can then be displayed with MaiaVi (http://mayavi.sourceforge.net/)
0023 % which has lots of nice features and is generally faster than MATALB 3D graphics
0024 %
0025 % CAVEAT: VTK supports only symmetrix tensors. It is left to the user to ensure that the data he is exporting
0026 % consists of symmetric tensors (as if such a check were performed by writeVTKfile, performance of the filter would suffer)
0027 
0028 if nargin==2
0029    img  = varargin{1};
0030    fmdl = img.fwd_model;
0031    writevtkfile_old(fn, fmdl.nodes, fmdl.elems, img.elem_data);
0032 elseif nargin ==3
0033 %vtx = varargin{1}.nodes;
0034 %simp= varargin{1}.elems;
0035 %vals= varargin{2}.elem_data;
0036    fmdl = varargin{1};
0037    img  = varargin{2};
0038    writevtkfile_old(fn,fmdl.nodes, fmdl.elems, img.elem_data);
0039 elseif nargin==4
0040    writevtkfile_old(fn,varargin{1},varargin{2},varargin{3})
0041 else
0042  error('Writevtkfile: Wrong number of arguments');
0043 end 
0044 
0045 function writevtkfile_old(filename,vtx,elem,val)
0046 %
0047 % Copyright (c) 2002-2003 Andrea Borsic, SC-AIP s.a.s.
0048 % Scientific Computing & Applied Inverse Problems
0049 % www.sc-aip.com
0050 % Revision 1.2 Original Date 12/08/02
0051 % Modified 11/06/03, exports vectors
0052 % Modified 31/08/03, exports tensors, fixes some problems regarding number rapresentation
0053 % Modified 26/03/03 in order to support both triangular and tetrahedral meshes
0054 % 25/06/2005 Changed name, writevtkfile is now overloaded to handle objects as data WRBL
0055 % This function writes 3D meshes and associated scalar/vector/tensor fields to a VTK (Visualization Tool Kit)(www.kitware.com) dataset file
0056 %
0057 % Use: writevtkfile_old('filename',vtx,elem,val)
0058 %
0059 % 'filename' = name of the vtk file, the ".vtk" extension will be addedd automatically
0060 % vtx = matrix of the verticies
0061 % elem = matrix desribing the simplecies
0062 % val = matrix holding the field values (scalars/vectros/tensors)
0063 %
0064 % Depending wether the field is piecewise linear or piecewise constant and if it it scalar, vector or tensor, val can assume different valid sizes:
0065 %
0066 % val should have a number of rows equal to the number of simplecies or to the number of nodes in the mesh (piecewise constant/piecewise linear dataset)
0067 % val should have a number of columns equal to 1,3 or 9 (scalar field / vector field / tensor field)
0068 %
0069 % The generated VTK file can then be displayed with MaiaVi (http://mayavi.sourceforge.net/)
0070 % which has lots of nice features and is generally faster than MATALB 3D graphics
0071 %
0072 % CAVEAT: VTK supports only symmetrix tensors. It is left to the user to ensure that the data he is exporting
0073 % consists of symmetric tensors (as if such a check were performed by writeVTKfile, performance of the filter would suffer)
0074 
0075 % DA FARE: mettere check sul numero di parametri di ingresso (e sul tipo, es controllare che filename sia una stringa)
0076 
0077 num_vtx=size(vtx,1);
0078 num_elem=size(elem,1);
0079 
0080 filename=strcat(filename,'.vtk');
0081 
0082 fid=fopen(filename,'wt');
0083 if fid==-1
0084     error('Cannot open vtk file');
0085 end % if FID
0086 
0087 frewind(fid);
0088 
0089 fprintf(fid,'# vtk DataFile Version 3.0\n');
0090 fprintf(fid,'%s %s exported from MATLAB\n',date,filename);
0091 fprintf(fid,'ASCII\n');
0092 
0093 fprintf(fid,'\n');
0094 
0095 % Here we write the verticies
0096 
0097 fprintf(fid,'DATASET UNSTRUCTURED_GRID\n');
0098 fprintf(fid,'POINTS %i double\n',num_vtx);
0099 
0100 for i=1:num_vtx
0101     fprintf(fid,'%d %d %d\n',vtx(i,1),vtx(i,2),vtx(i,3));
0102 end % for
0103 
0104 fprintf(fid,'\n');
0105 
0106 % Here we write the simplecies
0107 
0108 switch size(elem,2)
0109     
0110     case 3 % 2D triangular mesh
0111         
0112         fprintf(fid,'CELLS %i %i\n',num_elem,4*num_elem);
0113         
0114         for i=1:num_elem
0115             fprintf(fid,'%i %i %i %i \n',3,elem(i,1)-1,elem(i,2)-1,elem(i,3)-1);
0116         end % for
0117         
0118         fprintf(fid,'\n');
0119         
0120         fprintf(fid,'CELL_TYPES %i\n',num_elem);
0121         
0122         for i=1:num_elem
0123             fprintf(fid,'5\n');
0124         end % for
0125         
0126     case 4 % 3D tetrahedral mesh
0127         
0128         fprintf(fid,'CELLS %i %i\n',num_elem,5*num_elem); % per ogni tetraedro 4 dati + 1 che specifica numpint=4 in totale 5 vedi pag 357
0129         
0130         for i=1:num_elem
0131             fprintf(fid,'%i %i %i %i %i\n',4,elem(i,1)-1,elem(i,2)-1,elem(i,3)-1,elem(i,4)-1);
0132         end % for
0133         
0134         fprintf(fid,'\n');
0135         
0136         fprintf(fid,'CELL_TYPES %i\n',num_elem);
0137         
0138         for i=1:num_elem
0139             fprintf(fid,'10\n');
0140         end % for
0141         
0142     otherwise
0143         
0144         error('Simplecies other than triangles and tetrahydra are not currently supported')
0145         
0146 end
0147 
0148 fprintf(fid,'\n');
0149 
0150 switch size(val,1)
0151     
0152     case num_elem
0153         
0154         switch size(val,2)
0155             
0156             case 1 % scalar field, cell data
0157                 
0158                 fprintf(fid,'CELL_DATA %i\n',num_elem);
0159                 fprintf(fid,'SCALARS MatlabExportedScalars double\n');
0160                 fprintf(fid,'LOOKUP_TABLE default\n');
0161                 
0162                 for i=1:num_elem
0163                     fprintf(fid,'%d\n',val(i));
0164                 end % for
0165                 
0166             case 3 % vector field, cell data
0167                 
0168                 fprintf(fid,'CELL_DATA %i\n',num_elem);
0169                 fprintf(fid,'VECTORS MatlabExportedVectors double\n');
0170                 % No LUT for vectors !
0171                 
0172                 for i=1:num_elem
0173                     fprintf(fid,'%d %d %d\n',val(i,1),val(i,2),val(i,3));
0174                 end % for
0175                 
0176             case 9 % tensor field, cell data
0177                 
0178                 fprintf(fid,'CELL_DATA %i\n',num_elem);
0179                 fprintf(fid,'TENSORS MatlabExportedTensors double\n');
0180                 % No LUT for tensors !
0181                 
0182                 for i=1:num_elem
0183                     for j=1:3
0184                         fprintf(fid,'%d %d %d\n',val(i,(j-1)*3+1),val(i,(j-1)*3+2),val(i,(j-1)*3+3));
0185                     end % for j
0186                 end % for
0187                 
0188             otherwise
0189                 
0190                 fprintf('The number of columns in the val vector should be 1,3 or 9, depending if it reperesents a scalar, a vector field or a tensor field.\n');
0191                 fprintf('A VTK file containing just mesh information was written.\n');     
0192                 
0193         end % switch size(val,2)
0194         
0195     case num_vtx
0196         
0197         switch size(val,2)
0198             
0199             case 1 % scalar field, point data
0200                 
0201                 fprintf(fid,'POINT_DATA %i\n',num_vtx);
0202                 fprintf(fid,'SCALARS MatlabExportedScalars double\n');
0203                 fprintf(fid,'LOOKUP_TABLE default\n');
0204                 
0205                 for i=1:num_vtx
0206                     fprintf(fid,'%d\n',val(i));
0207                 end % for
0208                 
0209             case 3 % vector field, point data
0210                 
0211                 fprintf(fid,'POINT_DATA %i\n',num_vtx);
0212                 fprintf(fid,'VECTORS MatlabExportedVectors double\n');
0213                 % No LUT for vectors !
0214                 
0215                 for i=1:num_vtx
0216                     fprintf(fid,'%d %d %d\n',val(i,1),val(i,2),val(i,3));
0217                 end % for
0218                 
0219             case 9 % tensor field, point data
0220                 
0221                 fprintf(fid,'POINT_DATA %i\n',num_vtx);
0222                 fprintf(fid,'TENSORS MatlabExportedTensors double\n');
0223                 % No LUT for tensors !
0224                 
0225                 for i=1:num_vtx
0226                     for j=1:3
0227                         fprintf(fid,'%d %d %d\n',val(i,(j-1)*3+1),val(i,(j-1)*3+2),val(i,(j-1)*3+3));
0228                     end % for j
0229                 end % for
0230                 
0231             otherwise
0232                 
0233                 fprintf('The number of columns in the val vector should be 1,3 or 9, depending if it reperesents a scalar, a vector field or a tensor field.\n');
0234                 fprintf('A VTK file containing just mesh information was written.\n');
0235                 
0236         end % switch size(val,2)
0237         
0238     otherwise
0239         
0240         fprintf('The number of rows of the val vector did not match the number of rows in vtx or in elem.\n');
0241         fprintf('A VTK file containing just mesh information was written.\n');
0242         
0243 end % switch size(val,1)
0244 
0245 fclose(fid);
0246 
0247 % Bye Bye

Generated on Wed 21-Jun-2017 09:29:07 by m2html © 2005