MATLAB对ply文件格式读取及显示_第1页
MATLAB对ply文件格式读取及显示_第2页
MATLAB对ply文件格式读取及显示_第3页
MATLAB对ply文件格式读取及显示_第4页
MATLAB对ply文件格式读取及显示_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、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 field

2、s 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

3、) 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

4、(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 be

5、en 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 20

6、04 % % 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 te

7、xt 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;

8、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 th

9、e 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 = Count ) Format = lower

10、 ( 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,Tokeni, ; end % % Read a

11、n 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 Elements. % eval(El

12、ements.,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 name order. % eval(P

13、ropertyNames.,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. % case end_header

14、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 fclose ( fid ); err

15、or(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_ENDIAN. % if ( Format = 1

16、 ) 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; while ( isempty(i) ) i

17、= 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 ( fid, i + tmp + 11 +

18、 (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,single,double; SizeOf

19、 = 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); % % Read ASCII dat

20、a. % 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 buffer. % if ( any(Type)

21、 ) % 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); end % % list type % f

22、or 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 MATLAB data type names

23、 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 ( isempty(tmp) ) TypeSize

24、(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 = 1; SameFlag = 0; t

25、mp = 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 da

26、ta type, list ,Token2, ,Token3, in , . ElementNamesi,.,CurPropertyNamesj,.); end else fclose(fid); error(Invalid list syntax in ,ElementNamesi,.,CurPropertyNamesj,.); end end end % read file if ( ListFlag ) % % No list types, all the same type (fast) % if ( SameFlag ) Data = fread(fid,NumProperties,

27、ElementCount(i),Type1); % % No list types, mixed type. % else Data = zeros(ElementCount(i),NumProperties); for j = 1 : ElementCount(i) for k = 1 : NumProperties Data(j,k) = fread(fid,1,Typek); end end end else ListData = cell(NumProperties,1); for k = 1 : NumProperties ListDatak = cell(ElementCount(

28、i),1); end if ( NumProperties = 1 ) BufSize = 512; SkipNum = 4; j = 0; % % List type, one property (fast if lists are usually the same length) % while ( j ElementCount(i) ) BufSize = min(ElementCount(i)-j,BufSize); Position = ftell(fid); % % Read in BufSize count values, assuming all counts = SkipNu

29、m % Buf,BufSize = fread(fid,BufSize,Type1,SkipNum*TypeSize2(1); Miss = find(Buf = SkipNum); % find first count that is not SkipNum fseek(fid,Position + TypeSize(1),-1); % seek back to after first count if ( isempty(Miss) ) % all counts are SkipNum Buf = fread(fid,SkipNum,BufSize,int2str(SkipNum),*,T

30、ype21,TypeSize(1); fseek(fid,-TypeSize(1),0); % undo last skip for k = 1:BufSize ListData1j+k = Buf(k,:); end j = j + BufSize; BufSize = floor(1.5*BufSize); else % % Some counts are SkipNum. % if ( 1 1 & strcmpi(Str,Tri) ) | nargout 2 ) % % Find vertex element field % Name = vertex,Vertex,point,Poin

31、t,pts,Pts; Names = ; for i = 1 : length(Name) if ( any ( strcmp ( ElementNames, Namei ) ) ) Names = getfield(PropertyNames,Namei); Name = Namei; break; end end if ( any(strcmp(Names,x) & any(strcmp(Names,y) & any(strcmp(Names,z) ) eval(varargout1=Elements.,Name,.x,Elements.,Name,.y,Elements.,Name,.z

32、;); else varargout1 = zeros(1,3); end varargout1 = varargout1; varargout2 = Elements; varargout3 = Comments; Elements = ; % % Find face element field % Name = face,Face,poly,Poly,tri,Tri; Names = ; for i = 1 : length(Name) if ( any(strcmp(ElementNames,Namei) ) Names = getfield(PropertyNames,Namei);

33、Name = Namei; break; end end if ( isempty(Names) ) % find vertex indices property subfield PropertyName = vertex_indices,vertex_indexes,vertex_index,indices,indexes; for i = 1 : length(PropertyName) if ( any(strcmp(Names,PropertyNamei) ) PropertyName = PropertyNamei; break; end end % % Convert face

34、index list to triangular connectivity. % if ( iscell(PropertyName) ) eval(FaceIndices=varargout2.,Name,.,PropertyName,;); N = th(FaceIndices); Elements = zeros(3,N*2); Extra = 0; for k = 1 : N Elements(1:3,k) = FaceIndicesk(1:3); % % The original code had an error in the following loop. % for j = 4 : length

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论