1、MATLAB对ply文件格式的读取和显示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 struc
2、ture 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 = P
3、LY_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:
4、% 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 trian
5、gular. % 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: %
6、% 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 i
7、nput 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;
8、 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 = ; Cou
9、nt = 0; % % Split the line into tokens. % while ( isempty(BufRem) ) tmp, BufRem = strtok(BufRem); % % Count the tokens. % if ( isempty ( tmp ) ) Count = Count + 1; TokenCount = tmp; end end % % Parse the line. % if ( Count ) switch lower ( Token1 ) % % Read the data format. % case format if ( 2 = Co
10、unt ) Format = lower ( Token2 ); if ( Count = 3 & strcmp ( Token3, 1.0 ) ) fclose ( fid ); error(Only PLY format version 1.0 supported.); end end % % Read a comment. % case comment NumComments = NumComments + 1; CommentsNumComments = ; for i = 2 : Count CommentsNumComments = CommentsNumComments,Toke
11、ni, ; end % % Read an element name. % case element if ( 3 = 3 ) NumProperties = NumProperties + 1; eval(tmp=isfield(Elements.,CurElement,TokenCount);,. fclose(fid);error(Error reading property: ,Buf); if ( tmp ) error(Duplicate property name, ,CurElement,.,Token2,.); end % % Add property subfield to
12、 Elements. % eval(Elements.,CurElement,.,TokenCount,=;, . fclose(fid);error(Error reading property: ,Buf); % % Add property subfield to PropertyTypes and save type. % eval(PropertyTypes.,CurElement,.,TokenCount,=Token2:Count-1;, . fclose(fid);error(Error reading property: ,Buf); % % Record property
13、name order. % eval(PropertyNames.,CurElement,NumProperties=TokenCount;, . fclose(fid);error(Error reading property: ,Buf); else fclose ( fid ); if ( isempty(CurElement) ) error(Property definition without element definition: ,Buf); else error(Bad property definition: ,Buf); end end % % End of header
14、. % case end_header break; end end end % % Set reading for specified data format. % if ( isempty ( Format ) ) warning(Data format unspecified, assuming ASCII.); Format = ascii; end switch Format case ascii Format = 0; case binary_little_endian Format = 1; case binary_big_endian Format = 2; otherwise
15、 fclose ( fid ); error(Data format ,Format, not supported.); end % % Read the rest of the file as ASCII data. % if ( Format ) Buf = fscanf ( fid, %f ); BufOff = 1; else % % .or, close the file, and reopen in read binary mode. % fclose ( fid ); % % Reopen the binary file as LITTLE_ENDIAN or BIG_ENDIA
16、N. % if ( Format = 1 ) fid = fopen ( Path, r, ieee-le.l64 ); else fid = fopen ( Path, r, ieee-be.l64 ); end % % Find the end of the header again. % Using ftell on the old handle doesnt give the correct position. % BufSize = 8192; Buf = blanks(10), char(fread(fid,BufSize,uchar) ; i = ; tmp = -11; whi
17、le ( isempty(i) ) i = findstr(Buf,end_header,13,10); % look for end_header + CR/LF i = i,findstr(Buf,end_header,10); % look for end_header + LF if ( isempty(i) ) tmp = tmp + BufSize; Buf = Buf(BufSize+1:BufSize+10),char(fread(fid,BufSize,uchar); end end % % seek to just after the line feed % fseek (
18、 fid, i + tmp + 11 + (Buf(i + 10) = 13), -1 ); end % % Read element data. % % PLY and MATLAB data types (for fread) % PlyTypeNames = char,uchar,short,ushort,int,uint,float,double, . char8,uchar8,short16,ushort16,int32,uint32,float32,double64; MatlabTypeNames = schar,uchar,int16,uint16,int32,uint32,s
19、ingle,double; SizeOf = 1,1,2,2,4,4,4,8; for i = 1 : NumElements % % get current element property information % eval(CurPropertyNames=PropertyNames.,ElementNamesi,;); eval(CurPropertyTypes=PropertyTypes.,ElementNamesi,;); NumProperties = size(CurPropertyNames,2); % fprintf(Reading %s.n,ElementNamesi)
20、; % % Read ASCII data. % if ( Format ) for j = 1 : NumProperties Token = getfield(CurPropertyTypes,CurPropertyNamesj); if ( strcmpi(Token1,list) ) Type(j) = 1; else Type(j) = 0; end % % Glenn Ramsey 20120827 % Initialise Type2 to prevent uninitialised value error. % Type2j = ; end % % Parse the buff
21、er. % if ( any(Type) ) % no list types Data = reshape ( . Buf(BufOff:BufOff+ElementCount(i)*NumProperties-1), . NumProperties, ElementCount(i) ); BufOff = BufOff + ElementCount(i) * NumProperties; else ListData = cell(NumProperties,1); for k = 1 : NumProperties ListDatak = cell(ElementCount(i),1); e
22、nd % % list type % for j = 1 : ElementCount(i) for k = 1 : NumProperties if ( Type(k) ) Data(j,k) = Buf(BufOff); BufOff = BufOff + 1; else tmp = Buf(BufOff); ListDatakj = Buf(BufOff+(1:tmp); BufOff = BufOff + tmp + 1; end end end end % % Read binary data. % else % translate PLY data type names to MA
23、TLAB data type names ListFlag = 0; % = 1 if ere is a list type SameFlag = 1; % = 1 if all types are the same for j = 1 : NumProperties Token = getfield(CurPropertyTypes,CurPropertyNamesj); % % Non-list type. % if ( strcmp(Token1,list ) ) tmp = rem(strmatch(Token1,PlyTypeNames,exact)-1,8)+1; if ( ise
24、mpty(tmp) ) TypeSize(j) = SizeOf(tmp); Typej = MatlabTypeNamestmp; TypeSize2(j) = 0; Type2j = ; SameFlag = SameFlag & strcmp(Type1,Typej); else fclose(fid); error(Unknown property data type, ,Token1, in , . ElementNamesi,.,CurPropertyNamesj,.); end else % list type if ( length(Token) = 3 ) ListFlag
25、= 1; SameFlag = 0; tmp = rem(strmatch(Token2,PlyTypeNames,exact)-1,8)+1; tmp2 = rem(strmatch(Token3,PlyTypeNames,exact)-1,8)+1; if ( isempty(tmp) & isempty(tmp2) ) TypeSize(j) = SizeOf(tmp); Typej = MatlabTypeNamestmp; TypeSize2(j) = SizeOf(tmp2); Type2j = MatlabTypeNamestmp2; lse fclose(fid); error(Unknown property data type, list ,Token2, ,Token3, in , . ElementNamesi,.,CurPropertyNamesj,.); end else
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1