Home > matGeom > meshes3d > writeMesh_off.m

writeMesh_off

PURPOSE ^

WRITEMESH_OFF Write a mesh into a text file in OFF format.

SYNOPSIS ^

function writeMesh_off(fileName, vertices, faces)

DESCRIPTION ^

WRITEMESH_OFF Write a mesh into a text file in OFF format.

   writeMesh_off(FNAME, V, F)

   Example
   writeMesh_off

   See also
      meshes3d, writeMesh, readMesh_off, writeMesh_ply

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function writeMesh_off(fileName, vertices, faces)
0002 %WRITEMESH_OFF Write a mesh into a text file in OFF format.
0003 %
0004 %   writeMesh_off(FNAME, V, F)
0005 %
0006 %   Example
0007 %   writeMesh_off
0008 %
0009 %   See also
0010 %      meshes3d, writeMesh, readMesh_off, writeMesh_ply
0011  
0012 % ------
0013 % Author: David Legland
0014 % e-mail: david.legland@inrae.fr
0015 % Created: 2018-04-26,    using Matlab 9.4.0.813654 (R2018a)
0016 % Copyright 2018 INRA - Cepia Software Platform.
0017 
0018 if ~ischar(fileName)
0019     error('First argument must contain the name of the file');
0020 end
0021 
0022 % optionnaly parses data
0023 if isstruct(vertices)
0024     faces = vertices.faces;
0025     vertices = vertices.vertices;
0026 end
0027 
0028 % open file for writing text
0029 f = fopen(fileName, 'wt');
0030 if (f == -1)
0031     error('Couldn''t open the file %s', fileName);
0032 end
0033 
0034 % write the header line
0035 fprintf(f, 'OFF\n');
0036 
0037 % write number of vertices and of faces
0038 nVertices = size(vertices, 1);
0039 nFaces = size(faces, 1);
0040 if iscell(faces)
0041     nFaces = length(faces);
0042 end
0043 fprintf(f, '%d %d 0\n', nVertices, nFaces);
0044 
0045 % Write vertex info
0046 format = '%g %g %g\n';
0047 for iv = 1:nVertices
0048     fprintf(f, format, vertices(iv, :));
0049 end
0050 
0051 % Write face info
0052 if isnumeric(faces)
0053     % simply write face vertex indices
0054     ns = size(faces, 2);
0055     format = ['%d' repmat(' %d', 1, ns) '\n'];
0056     for iFace = 1:nFaces
0057         fprintf(f, format, ns, faces(iFace, :)-1);
0058     end
0059 else
0060     % if faces are stored in a cell array, the number of vertices in each
0061     % face may be different, and we need to process each face individually
0062     for iFace = 1:nFaces
0063         ns = length(faces{iFace});
0064         format = ['%d' repmat(' %d', 1, ns) '\n'];
0065         fprintf(f, format, ns, faces{iFace}-1);
0066     end
0067 end
0068 
0069 % close the file
0070 fclose(f);

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019