READMESH Read a 3D mesh by inferring the format from the file extension. [V, F] = readMesh(FILEPATH) Read the data stored in file FILEPATH and return the vertex and face arrays as NV-by-3 array and NF-by-N array respectively, where NV is the number of vertices and NF is the number of faces. MESH = readMesh(FILEPATH) Read the data stored in file FILEPATH and return the mesh into a struct with the fields 'vertices' and 'faces'. In addition, the struct contains the fields 'name' and 'filePath': 'name' corresponds to the base name of the file (without path and extension) 'filePath' corresponds to the full (relative) path of the file Example mesh = readMesh('apple.ply'); figure; drawMesh(mesh); view([180 -70]); axis equal; See also meshes3d, writeMesh, readMesh_off, readMesh_ply, readMesh_stl
0001 function varargout = readMesh(filePath, varargin) 0002 %READMESH Read a 3D mesh by inferring the format from the file extension. 0003 % 0004 % [V, F] = readMesh(FILEPATH) 0005 % Read the data stored in file FILEPATH and return the vertex and face 0006 % arrays as NV-by-3 array and NF-by-N array respectively, where NV is the 0007 % number of vertices and NF is the number of faces. 0008 % 0009 % MESH = readMesh(FILEPATH) 0010 % Read the data stored in file FILEPATH and return the mesh into a struct 0011 % with the fields 'vertices' and 'faces'. 0012 % In addition, the struct contains the fields 'name' and 'filePath': 0013 % 'name' corresponds to the base name of the file (without path and 0014 % extension) 0015 % 'filePath' corresponds to the full (relative) path of the file 0016 % 0017 % Example 0018 % mesh = readMesh('apple.ply'); 0019 % figure; drawMesh(mesh); 0020 % view([180 -70]); axis equal; 0021 % 0022 % See also 0023 % meshes3d, writeMesh, readMesh_off, readMesh_ply, readMesh_stl 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2020-11-20, using Matlab 9.8.0.1323502 (R2020a) 0030 % Copyright 2020-2024 INRAE - BIA Research Unit - BIBS Platform (Nantes) 0031 0032 % extract base name and file extension 0033 [~, baseName, ext] = fileparts(filePath); 0034 switch lower(ext) 0035 case '.off' 0036 mesh = readMesh_off(filePath); 0037 case '.ply' 0038 mesh = readMesh_ply(filePath); 0039 case '.stl' 0040 mesh = readMesh_stl(filePath); 0041 case '.obj' 0042 mesh = readMesh_obj(filePath); 0043 otherwise 0044 error('readMesh.m function does not support %s files.', upper(ext(2:end))); 0045 end 0046 0047 % format output arguments 0048 varargout = formatMeshOutput(nargout, mesh.vertices, mesh.faces); 0049 0050 % in case of mesh returned as a struct, also include the file name as field 0051 if isstruct(varargout{1}) 0052 varargout{1}.name = baseName; 0053 varargout{1}.filePath = filePath; 0054 end