stl文档读取MATLAB代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

%Copyright2018JohnMoosemiller%Permissionisherebygranted,freeofcharge,toanypersonobtainingacopyofthissoftwareandassociateddocumentationfiles(theSoftware),todealintheSoftwarewithoutrestriction,includingwithoutlimitationtherightstouse,copy,modify,merge,publish,distribute,sublicense,and/orsellcopiesoftheSoftware,andtopermitpersonstowhomtheSoftwareisfurnishedtodoso,subjecttothefollowingconditions:%TheabovecopyrightnoticeandthispermissionnoticeshallbeincludedinallcopiesorsubstantialportionsoftheSoftware.%THESOFTWAREISPROVIDEDASIS,WITHOUTWARRANTYOFANYKIND,EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE.function[vertices,faces,color]=stlread(filename)%ThisfunctionreadsanSTLfileinbinaryformatintomatrixesvertices%andfaceswhichcanbeusedbypatchtoviewthestl.%%MATLABcodebyDoronHarlev%OctaveeditsbyJohnMoosemiller&&@zmughalread_norm=0;%whetherwewanttoreadthefacenormals%Processarguments{{{ifnargout3error('Toomanyoutputarguments');enduse_color=(nargout==3);%}}}%Openthefile,assumesSTLBinaryformat.{{{fid=fopen(filename,'r');if(fid==-1)error('Filecouldnotbeopened,checknameorpath.')end%}}}%Readinheader{{{ftitle=fread(fid,80,'uchar=schar');%Readfiletitlenum_facet=fread(fid,1,'int32');%ReadnumberofFacetsfprintf('\nTitle:%s\n',char(ftitle'));fprintf('NumFacets:%d\n',num_facet);%}}}%Preallocatememorytosaverunningtime{{{vertices=zeros(3*num_facet,3);faces=zeros(num_facet,3);ifuse_colorcolor=uint8(zeros(3,num_facet));end%}}}%Readinpackeddataforeachface{{{coord_sz=3*32/8;%[bytes]sizeofvertexcoordinates(x,y,z)'3*float32'color_sz=1*16/8;%[bytes]sizeofcolordata:'1*uint16'block_sz=4*coord_sz+color_sz;%[bytes]4coords(norm,v1,v2,v3)+1colordata%getthestartofwherethefacedataisinthefilefid_face_data_start=ftell(fid);%Readnormoftheface[1*coord_sz]{{{start_offset=0;%atfid_face_data_start+0ifread_normnorm=fread(fid,[3,num_facet],'3*float32',block_sz-coord_sz);endstart_offset=start_offset+1*coord_sz;%}}}%Readall3verticesofeachface[3*coord_sz]{{{%Eachcolumnisthe3verticeswith3coordseachforeachface%face_vertices=%f1_v_1xf2_v_1x...fN_v_1x%f1_v_1yf2_v_1y...fN_v_1y%f1_v_1zf2_v_1z...fN_v_1z%f1_v_2xf2_v_2x...fN_v_2x%f1_v_2yf2_v_2y...fN_v_2y%f1_v_2zf2_v_2z...fN_v_2z%f1_v_3xf2_v_3x...fN_v_3x%f1_v_3yf2_v_3y...fN_v_3y%f1_v_3zf2_v_3z...fN_v_3zfseek(fid,fid_face_data_start+start_offset,-1);face_vertices=fread(fid,[3*3,num_facet],'9*float32',block_sz-3*coord_sz);start_offset=start_offset+3*coord_sz;%}}}%Readthecolorofeachface[1*color_sz]{{{ifuse_colorfseek(fid,fid_face_data_start+start_offset,-1);col=fread(fid,num_facet,'1*uint16',block_sz-1*color_sz);%}}}end%}}}%Reshapedataintofacesandvertices{{{%interleavevertices{{{%sothatweget%vertices=%f1_v_1xf1_v_1yf1_v_1z%f1_v_2xf1_v_2yf1_v_2z%f1_v_3xf1_v_3yf1_v_3z%f2_v_3xf2_v_3yf2_v_3z%.........%fN_v_2xfN_v_2yfN_v_2z%fN_v_3xfN_v_3yfN_v_3zvertices=reshape(face_vertices(:),3,[])';%}}}%facetriangleverticesareinsequentialrowsof`vertices`%{{{%faces=%123%456%...%3N-23N-13Nfaces=reshape(1:3*num_facet,3,[])';%}}}%extractcolorfrombits%{{{ifuse_color&&any(col)valid=bitget(col,16)==1;r=bitshift(bitand(2^16-1,col),-10);g=bitshift(bitand(2^11-1,col),-5);b=bitand(2^6-1,col);%onlykeepthevalidcolordata,setothertozero(black)color=[rgb].*valid;end%}}}%}}}fclose(fid);%Formoreinformationonthestlbinaryformat-

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功