MATLAB对ply文件格式的读取和显示.docx

上传人:b****7 文档编号:23593669 上传时间:2023-05-18 格式:DOCX 页数:28 大小:168.06KB
下载 相关 举报
MATLAB对ply文件格式的读取和显示.docx_第1页
第1页 / 共28页
MATLAB对ply文件格式的读取和显示.docx_第2页
第2页 / 共28页
MATLAB对ply文件格式的读取和显示.docx_第3页
第3页 / 共28页
MATLAB对ply文件格式的读取和显示.docx_第4页
第4页 / 共28页
MATLAB对ply文件格式的读取和显示.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

MATLAB对ply文件格式的读取和显示.docx

《MATLAB对ply文件格式的读取和显示.docx》由会员分享,可在线阅读,更多相关《MATLAB对ply文件格式的读取和显示.docx(28页珍藏版)》请在冰豆网上搜索。

MATLAB对ply文件格式的读取和显示.docx

MATLAB对ply文件格式的读取和显示

MATLAB对ply文件格式的读取和显示

首先是这个ply_read.m文件

[plain]viewplaincopyprint?

在CODE上查看代码片派生到我的代码片

function[Elements,varargout]=PLY_READ(Path,Str)

%*****************************************************************************80

%

%%PLY_READreadsaPLY3Ddatafile.

%

%[DATA,COMMENTS]=PLY_READ(FILENAME)readsaversion1.0PLYfile

%FILENAMEandreturnsastructureDATA.Thefieldsinthisstructure

%aredefinedbythePLYheader;eachelementtypeisafieldandeach

%elementpropertyisasubfield.Ifthefilecontainsanycomments,

%theyarereturnedinacellstringarrayCOMMENTS.

%

%[TRI,PTS]=PLY_READ(FILENAME,'tri')or

%[TRI,PTS,DATA,COMMENTS]=PLY_READ(FILENAME,'tri')convertsvertex

%andfacedataintotriangularconnectivityandvertexarrays.The

%meshcanthenbedisplayedusingtheTRISURFcommand.

%

%Note:

Thisfunctionisslowforlargemeshfiles(+50Kfaces),

%especiallywhenreadingdatawithlisttypeproperties.

%

%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);axisequal;

%

%Discussion:

%

%Theoriginalversionofthisprogramhadamistakethatmeantit

%didnotproperlytriangulatefileswhosefaceswerenotalreadytriangular.

%Thishasbeencorrected(JVB,25February2007).

%

%GlennRamseypointedoutandcorrectedaproblemthatoccurred

%withanuninitializedvalueofType2,27August2012.

%

%Licensing:

%

%ThiscodeisdistributedundertheGNULGPLlicense.

%

%Modified:

%

%27August2012

%

%Author:

%

%PascalGetreuer2004

%

%Parameters:

%

%LocalParameters:

%

%COMMENTS,anycommentsfromthefile.

%

%ELEMENTCOUNT,thenumberofeachtypeofelementinfile.

%

%ELEMENTS,theelementdata.

%

%PROPERTYTYPES,theelementpropertytypes.

%

%SIZEOF,sizeinbytesofeachtype.

%

%

%Opentheinputfilein"readtext"mode.

%

[fid,Msg]=fopen(Path,'rt');

if(fid==-1)

error(Msg);

end

Buf=fscanf(fid,'%s',1);

if(~strcmp(Buf,'ply'))

fclose(fid);

error('NotaPLYfile.');

end

%

%Readtheheader.

%

Position=ftell(fid);

Format='';

NumComments=0;

Comments={};

NumElements=0;

NumProperties=0;

Elements=[];

ElementCount=[];

PropertyTypes=[];

ElementNames={};%listofelementnamesintheordertheyarestoredinthefile

PropertyNames=[];%structureoflistsofpropertynames

while

(1)

%

%Readalinefromthefile.

%

Buf=fgetl(fid);

BufRem=Buf;

Token={};

Count=0;

%

%Splitthelineintotokens.

%

while(~isempty(BufRem))

[tmp,BufRem]=strtok(BufRem);

%

%Countthetokens.

%

if(~isempty(tmp))

Count=Count+1;

Token{Count}=tmp;

end

end

%

%Parsetheline.

%

if(Count)

switchlower(Token{1})

%

%Readthedataformat.

%

case'format'

if(2<=Count)

Format=lower(Token{2});

if(Count==3&~strcmp(Token{3},'1.0'))

fclose(fid);

error('OnlyPLYformatversion1.0supported.');

end

end

%

%Readacomment.

%

case'comment'

NumComments=NumComments+1;

Comments{NumComments}='';

fori=2:

Count

Comments{NumComments}=[Comments{NumComments},Token{i},''];

end

%

%Readanelementname.

%

case'element'

if(3<=Count)

if(isfield(Elements,Token{2}))

fclose(fid);

error(['Duplicateelementname,''',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(['Badelementdefinition:

',Buf]);

end

else

error(['Badelementdefinition:

',Buf]);

end

%

%Readanelementproperty.

%

case'property'

if(~isempty(CurElement)&Count>=3)

NumProperties=NumProperties+1;

eval(['tmp=isfield(Elements.',CurElement,',Token{Count});'],...

'fclose(fid);error([''Errorreadingproperty:

'',Buf])');

if(tmp)

error(['Duplicatepropertyname,''',CurElement,'.',Token{2},'''.']);

end

%

%AddpropertysubfieldtoElements.

%

eval(['Elements.',CurElement,'.',Token{Count},'=[];'],...

'fclose(fid);error([''Errorreadingproperty:

'',Buf])');

%

%AddpropertysubfieldtoPropertyTypesandsavetype.

%

eval(['PropertyTypes.',CurElement,'.',Token{Count},'={Token{2:

Count-1}};'],...

'fclose(fid);error([''Errorreadingproperty:

'',Buf])');

%

%Recordpropertynameorder.

%

eval(['PropertyNames.',CurElement,'{NumProperties}=Token{Count};'],...

'fclose(fid);error([''Errorreadingproperty:

'',Buf])');

else

fclose(fid);

if(isempty(CurElement))

error(['Propertydefinitionwithoutelementdefinition:

',Buf]);

else

error(['Badpropertydefinition:

',Buf]);

end

end

%

%Endofheader.

%

case'end_header'

break;

end

end

end

%

%Setreadingforspecifieddataformat.

%

if(isempty(Format))

warning('Dataformatunspecified,assumingASCII.');

Format='ascii';

end

switchFormat

case'ascii'

Format=0;

case'binary_little_endian'

Format=1;

case'binary_big_endian'

Format=2;

otherwise

fclose(fid);

error(['Dataformat''',Format,'''notsupported.']);

end

%

%ReadtherestofthefileasASCIIdata...

%

if(~Format)

Buf=fscanf(fid,'%f');

BufOff=1;

else

%

%...or,closethefile,andreopenin"readbinary"mode.

%

fclose(fid);

%

%ReopenthebinaryfileasLITTLE_ENDIANorBIG_ENDIAN.

%

if(Format==1)

fid=fopen(Path,'r','ieee-le.l64');

else

fid=fopen(Path,'r','ieee-be.l64');

end

%

%Findtheendoftheheaderagain.

%Usingftellontheoldhandledoesn'tgivethecorrectposition.

%

BufSize=8192;

Buf=[blanks(10),char(fread(fid,BufSize,'uchar')')];

i=[];

tmp=-11;

while(isempty(i))

i=findstr(Buf,['end_header',13,10]);%lookforend_header+CR/LF

i=[i,findstr(Buf,['end_header',10])];%lookforend_header+LF

if(isempty(i))

tmp=tmp+BufSize;

Buf=[Buf(BufSize+1:

BufSize+10),char(fread(fid,BufSize,'uchar')')];

end

end

%

%seektojustafterthelinefeed

%

fseek(fid,i+tmp+11+(Buf(i+10)==13),-1);

end

%

%Readelementdata.

%

%PLYandMATLABdatatypes(forfread)

%

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=[1,1,2,2,4,4,4,8];

fori=1:

NumElements

%

%getcurrentelementpropertyinformation

%

eval(['CurPropertyNames=PropertyNames.',ElementNames{i},';']);

eval(['CurPropertyTypes=PropertyTypes.',ElementNames{i},';']);

NumProperties=size(CurPropertyNames,2);

%fprintf('Reading%s...\n',ElementNames{i});

%

%ReadASCIIdata.

%

if(~Format)

forj=1:

NumProperties

Token=getfield(CurPropertyTypes,CurPropertyNames{j});

if(strcmpi(Token{1},'list'))

Type(j)=1;

else

Type(j)=0;

end

%

%GlennRamsey20120827

%InitialiseType2{}topreventuninitialisedvalueerror.

%

Type2{j}='';

end

%

%Parsethebuffer.

%

if(~any(Type))

%nolisttypes

Data=reshape(...

Buf(BufOff:

BufOff+ElementCount(i)*NumProperties-1),...

NumProperties,ElementCount(i))';

BufOff=BufOff+ElementCount(i)*NumProperties;

else

ListData=cell(NumProperties,1);

fork=1:

NumProperties

ListData{k}=cell(ElementCount(i),1);

end

%

%listtype

%

forj=1:

ElementCount(i)

fork=1:

NumProperties

if(~Type(k))

Data(j,k)=Buf(BufOff);

BufOff=BufOff+1;

else

tmp=Buf(BufOff);

ListData{k}{j}=Buf(BufOff+(1:

tmp))';

BufOff=BufOff+tmp+1;

end

end

end

end

%

%Readbinarydata.

%

else

%translatePLYdatatypenamestoMATLABdatatypenames

ListFlag=0;%=1ifereisalisttype

SameFlag=1;%=1ifalltypesarethesame

forj=1:

NumProperties

Token=getfield(CurPropertyTypes,CurPropertyNames{j});

%

%Non-listtype.

%

if(~strcmp(Token{1},'list'))

tmp=rem(strmatch(Token{1},PlyTypeNames,'exact')-1,8)+1;

if(~isempty(tmp))

TypeSize(j)=SizeOf(tmp);

Type{j}=MatlabTypeNames{tmp};

TypeSize2(j)=0;

Type2{j}='';

SameFlag=SameFlag&strcmp(Type{1},Type{j});

else

fclose(fid);

error(['Unknownpropertydatatype,''',Token{1},''',in',...

ElementNames{i},'.',CurPropertyNames{j},'.']);

end

else%listtype

if(length(Token)==3)

ListFlag=1;

SameFlag=0;

tmp=rem(strmatch(Token{2},PlyTypeNames,'exact')-1,8)+1;

tmp2=rem(strmatch(Token{3},PlyTypeNames,'exact')-1,8)+1;

if(~isempty(tmp)&~isempty(tmp2))

TypeSize(j)=SizeOf(tmp);

Type{j}=MatlabTypeNames{tmp};

TypeSize2(j)=SizeOf(tmp2);

Type2{j}=MatlabTypeNames{tmp2};

lse

fclose(fid);

error(['Unknownpropertydatatype,''list',Token{2},'',Token{3},''',in',...

ElementNames{i},'.',CurPropertyNames{j},'.']);

end

else

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1