• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

如何用MATLAB读stl并显示点云文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
function [VertexData,FVCD,isBinary]=stl2matlab(stlfile)
% STL2MATLAB reads STL-file, ASCII or binary format, into Matlab
%
% Usage:
%
% [VertexData, FVCD]=stl2matlab(stlfile)
%
% Input:
%
% stlfile - STL-file
%
% Output:
%
% VertexData - Cellarray containing matrices with vertices
% FVCD - FaceVertexColorData
% isBinary - Flag with STL-file information
%
% To plot the STL-object use plotSTL(VertexData) or plotSTL(VertexData,FVCD)
%
% m-file can be downloaded at
% http://www.mathworks.com/matlabcentral/fileexchange/13253-iges-toolbox
%
% written by Per Bergstrm 2012-01-09
%

if nargin == 0
    warning('No STL-file is specified');
end

VertexData=cell(1,3);

try
    
    % Try to read an STL ASCII file
    [VertexData{1}, VertexData{2}, VertexData{3}, FVCD] = stlAread(stlfile);
    isBinary=false;
    
catch
    
    try
        
        % Try to read an STL binary file
        [VertexData{1}, VertexData{2}, VertexData{3}, FVCD] = stlBread(stlfile);
        isBinary=true;
        
    catch
        
        error('File could not be read!')
        
    end
    
end


function [X, Y, Z, FVCD] = stlAread(stlfile)
% Reads an STL ASCII file

fid=fopen(stlfile,'r');

fileTitle=sscanf(fgetl(fid),'%*s %s');

vnum=0;
fclr=0;
testASCII=true;
lineCount=0;

while feof(fid) == 0
    stlLine=fgetl(fid);
    keyWord=sscanf(stlLine,'%s');
    if strncmpi(keyWord,'c',1) == 1;
        fclr=sscanf(stlLine,'%*s %f %f %f');
    elseif strncmpi(keyWord,'v',1) == 1;
        vnum=vnum+1;
        vertex(:,vnum)=sscanf(stlLine,'%*s %f %f %f');
        clr(:,vnum)=fclr;
    elseif testASCII
        lineCount=lineCount+1;
        if lineCount>20
            if vnum>2
                testASCII=false;
            else
                error('File is not an STL ASCII file!')
            end
        end
    end
end

X=[vertex(1,1:3:end);vertex(1,2:3:end);vertex(1,3:3:end)];
Y=[vertex(2,1:3:end);vertex(2,2:3:end);vertex(2,3:3:end)];
Z=[vertex(3,1:3:end);vertex(3,2:3:end);vertex(3,3:3:end)];

FVCD=reshape(clr,3,vnum/3);

fclose(fid);


function [X, Y, Z, FVCD] = stlBread(stlfile)
% Reads an STL binary file

fid=fopen(stlfile,'r');

fileTitle=fread(fid,80,'uchar=>schar');
fnum=fread(fid,1,'int32');

X=zeros(3,fnum);
Y=zeros(3,fnum);
Z=zeros(3,fnum);

FVCD=uint8(zeros(3,fnum));

for i=1:fnum,
    normal=fread(fid,3,'float32');
    vertex1=fread(fid,3,'float32');
    vertex2=fread(fid,3,'float32');
    vertex3=fread(fid,3,'float32');
    clr=fread(fid,1,'uint16');
    if bitget(clr,16)==1
        rd=bitshift(bitand(65535,clr),-10);
        grn=bitshift(bitand(2047,clr),-5);
        bl=bitand(63,clr);
        FVCD(:,i)=[rd;grn;bl];
    end
    X(:,i)=[vertex1(1); vertex2(1); vertex3(1)];
    Y(:,i)=[vertex1(2); vertex2(2); vertex3(2)];
    Z(:,i)=[vertex1(3); vertex2(3); vertex3(3)];
end

fclose(fid);

把这个函数放到Matlab中去,然后

b = figure(2);
model = stl2matlab('rawmodel-983.stl');
patch(model{1},model{2},model{3},'b');
axis equal; 
view(30,60);
mesh(x, y,z);
title('topographie map')

具体什么原理不太懂,反正这样真的可以

就是图丑了一点2333333333333333333


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DELPHI下的SOCK编程(转)发布时间:2022-07-18
下一篇:
DelphiExcel导入的通用程序2.发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap