MATLAB对ply文件格式的读取和显示
- 格式:doc
- 大小:243.00 KB
- 文档页数:17
MATLAB对ply文件格式的读取和显示 首先是这个ply_read.m文件 [plain] view plain copy print?在CODE上查看代码片派生到我的代码片 function [ Elements, varargout ] = PLY_READ ( Path, Str )
%*****************************************************************************80 % %% PLY_READ reads a PLY 3D data file. % % [DATA,COMMENTS] = PLY_READ(FILENAME) reads a version 1.0 PLY file % FILENAME and returns a structure DATA. The fields in this structure % are defined by the PLY header; each element type is a field and each % element property is a subfield. If the file contains any comments, % they are returned in a cell string array COMMENTS. % % [TRI,PTS] = PLY_READ(FILENAME,'tri') or % [TRI,PTS,DATA,COMMENTS] = PLY_READ(FILENAME,'tri') converts vertex % and face data into triangular connectivity and vertex arrays. The % mesh can then be displayed using the TRISURF command. % % Note: This function is slow for large mesh files (+50K faces), % especially when reading data with list type properties. % % Example: % [Tri,Pts] = PLY_READ('cow.ply','tri'); % [Tri,Pts] = PLY_READ('bunny.ply','tri'); % trisurf(Tri,Pts(:,1),Pts(:,2),Pts(:,3)); % colormap(gray); axis equal; % % Discussion: % % The original version of this program had a mistake that meant it % did not properly triangulate files whose faces were not already triangular. % This has been corrected (JVB, 25 February 2007). % % Glenn Ramsey pointed out and corrected a problem that occurred % with an uninitialized value of Type2, 27 August 2012. % % Licensing: % % This code is distributed under the GNU LGPL license. % % Modified: % % 27 August 2012 % % Author: % % Pascal Getreuer 2004 % % Parameters: % % Local Parameters: % % COMMENTS, any comments from the file. % % ELEMENTCOUNT, the number of each type of element in file. % % ELEMENTS, the element data. % % PROPERTYTYPES, the element property types. % % SIZEOF, size in bytes of each type. %
% % Open the input file in "read text" mode. % [ fid, Msg ] = fopen ( Path, 'rt' );
if ( fid == -1 ) error ( Msg ); end
Buf = fscanf ( fid, '%s', 1 ); if ( ~strcmp ( Buf, 'ply' ) ) fclose ( fid ); error('Not a PLY file.'); end % % Read the header. % Position = ftell(fid); Format = ''; NumComments = 0; Comments = {}; NumElements = 0; NumProperties = 0; Elements = []; ElementCount = []; PropertyTypes = []; ElementNames = {}; % list of element names in the order they are stored in the file PropertyNames = []; % structure of lists of property names
while ( 1 ) % % Read a line from the file. % Buf = fgetl ( fid ); BufRem = Buf; Token = {}; Count = 0; % % Split the line into tokens. % while ( ~isempty(BufRem) )
[ tmp, BufRem ] = strtok(BufRem); % % Count the tokens. % if ( ~isempty ( tmp ) ) Count = Count + 1; Token{Count} = tmp; end
end % % Parse the line. % if ( Count )
switch lower ( Token{1} ) % % Read the data format. % case 'format'
if ( 2 <= Count ) Format = lower ( Token{2} ); if ( Count == 3 & ~strcmp ( Token{3}, '1.0' ) ) fclose ( fid ); error('Only PLY format version 1.0 supported.'); end end % % Read a comment. % case 'comment'
NumComments = NumComments + 1; Comments{NumComments} = ''; for i = 2 : Count Comments{NumComments} = [Comments{NumComments},Token{i},' ']; end % % Read an element name. % case 'element'
if ( 3 <= Count ) if ( isfield(Elements,Token{2}) ) fclose ( fid ); error(['Duplicate element name, ''',Token{2},'''.']); end
NumElements = NumElements + 1; NumProperties = 0; Elements = setfield(Elements,Token{2},[]); PropertyTypes = setfield(PropertyTypes,Token{2},[]); ElementNames{NumElements} = Token{2}; PropertyNames = setfield(PropertyNames,Token{2},{}); CurElement = Token{2}; ElementCount(NumElements) = str2double(Token{3});
if ( isnan(ElementCount(NumElements)) ) fclose ( fid ); error(['Bad element definition: ',Buf]); end