Home > matGeom > meshes3d > writeMesh_ply.m

writeMesh_ply

PURPOSE ^

WRITEMESH_PLY Write a mesh into a file in PLY format.

SYNOPSIS ^

function writeMesh_ply(fileName, vertices, faces, varargin)

DESCRIPTION ^

WRITEMESH_PLY Write a mesh into a file in PLY format.

   writeMesh_ply(FNAME, VERTICES, FACES)

   writeMesh_ply(FNAME, MESH)

   writeMesh_ply(..., FORMAT) also specifies a file format for the written
   file. FORMAT can be either 'binary' (default) or 'ascii'.

   Example
   mesh = createSoccerBall;
   fileName = 'SoccerBall.ply';
   writeMesh_ply(fileName, mesh, 'Bin');
   mesh2 = readMesh(fileName);
   drawMesh(mesh2); axis equal

   See also
   meshes3d, writeMesh, readMesh_ply, writeMesh_off, writeMesh_stl

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function writeMesh_ply(fileName, vertices, faces, varargin)
0002 %WRITEMESH_PLY Write a mesh into a file in PLY format.
0003 %
0004 %   writeMesh_ply(FNAME, VERTICES, FACES)
0005 %
0006 %   writeMesh_ply(FNAME, MESH)
0007 %
0008 %   writeMesh_ply(..., FORMAT) also specifies a file format for the written
0009 %   file. FORMAT can be either 'binary' (default) or 'ascii'.
0010 %
0011 %   Example
0012 %   mesh = createSoccerBall;
0013 %   fileName = 'SoccerBall.ply';
0014 %   writeMesh_ply(fileName, mesh, 'Bin');
0015 %   mesh2 = readMesh(fileName);
0016 %   drawMesh(mesh2); axis equal
0017 %
0018 %   See also
0019 %   meshes3d, writeMesh, readMesh_ply, writeMesh_off, writeMesh_stl
0020 
0021 % ------
0022 % Author: David Legland, oqilipo
0023 % e-mail: david.legland@inrae.fr
0024 % Created: 2018-04-26,    using Matlab 9.4.0.813654 (R2018a)
0025 % Copyright 2018 INRA - Cepia Software Platform.
0026 
0027 
0028 %% Check inputs
0029 
0030 % optionnaly parses data
0031 if isstruct(vertices)
0032     if nargin > 2
0033         varargin = [{faces} varargin{:}];
0034     end
0035     faces = vertices.faces;
0036     vertices = vertices.vertices;
0037 end
0038 
0039 % Parsing
0040 p = inputParser;
0041 addRequired(p,'fileName',@(x) validateattributes(x,{'char'},{'nonempty'}));
0042 suppModes = {'ascii','binary_little_endian'};
0043 addOptional(p,'mode','binary_little_endian',@(x) any(validatestring(x,suppModes)));
0044 parse(p,fileName,varargin{:});
0045 fileName = p.Results.fileName;
0046 mode = suppModes{startsWith(suppModes, p.Results.mode, 'IgnoreCase',1)};
0047 
0048 %% Initializations
0049 
0050 % number of vertices and faces
0051 nVertices = size(vertices, 1);
0052 nFaces = size(faces, 1);
0053 if iscell(faces)
0054     nFaces = length(faces);
0055 end
0056 
0057 % open file for writing text
0058 f = fopen(fileName, 'wt');
0059 if (f == -1)
0060     error('Couldn''t open the file %s', fileName);
0061 end
0062 
0063 
0064 %% Write Header
0065 
0066 % write the header line
0067 fprintf(f, 'ply\n');
0068 
0069 % write format (only ASCII supported)
0070 fprintf(f, 'format %s 1.0\n', mode);
0071 
0072 % some comments
0073 fprintf(f, 'comment Created by MatGeom for MATLAB\n');
0074 
0075 % write declaration for vertices
0076 fprintf(f, 'element vertex %d\n', nVertices);
0077 fprintf(f, 'property double x\n');
0078 fprintf(f, 'property double y\n');
0079 fprintf(f, 'property double z\n');
0080 
0081 % write declaration for faces
0082 fprintf(f, 'element face %d\n', nFaces);
0083 fprintf(f, 'property list int int vertex_index\n');
0084 
0085 % end of header
0086 fprintf(f, 'end_header\n');
0087 
0088 %% Write data
0089 
0090 switch mode
0091     case 'ascii'
0092         % write vertex info
0093         format = '%0.17f %0.17f %0.17f\n';
0094         fprintf(f, format, vertices');
0095         
0096         % write face info
0097         if isnumeric(faces)
0098             % simply write face vertex indices
0099             ns = size(faces, 2);
0100             plyFaces = [ns * ones(nFaces, 1) faces-1];
0101             format = ['%d' repmat(' %d', 1, ns) '\n'];
0102             fprintf(f, format, plyFaces');
0103         else
0104             % if faces are stored in a cell array, the number of vertices in each
0105             % face may be different, and we need to process each face individually
0106             for iFace = 1:nFaces
0107                 ns = length(faces{iFace});
0108                 format = ['%d' repmat(' %d', 1, ns) '\n'];
0109                 fprintf(f, format, ns, faces{iFace} - 1);
0110             end
0111         end
0112     case 'binary_little_endian'
0113         % close the file
0114         fclose(f);
0115         % open file with little-endian format
0116         f = fopen(fileName,'a','ieee-le');
0117         % write vertex info
0118         fwrite(f, vertices', 'double');
0119         
0120         % write face info
0121         if isnumeric(faces)
0122             % simply write face vertex indices
0123             plyFaces = [size(faces, 2) * ones(nFaces, 1) faces-1];
0124             fwrite(f, plyFaces', 'int');
0125         else
0126             % if faces are stored in a cell array, the number of vertices in each
0127             % face may be different, and we need to process each face individually
0128             for iFace = 1:nFaces
0129                 fwrite(f, [length(faces{iFace}), faces{iFace}-1], 'int');
0130             end
0131         end
0132     otherwise
0133         error(['Format ''' mode ''' is not supported'])
0134 end
0135 
0136 % close the file
0137 fclose(f);
0138 
0139 end

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