【源码】二进制立体光刻文件(STL)的MATLAB读取函数stlread

本示例对人体股骨的3D模型进行加载并渲染,展示了MATLAB的一些高级图形处理特点,包括照明和镜面反射。

立体光刻(STL)文件是一种用于存储网格数据的通用格式,STL网格只是三角形面的集合。这种类型的模型非常适合与MATLAB的PATCH图形对象一起使用。

% Import an STL mesh, returning a PATCH-compatible face-vertex structure

fv = stlread(‘femur.stl’);

用PATCH图形对象渲染模型。我们还添加了一些动态照明,并调整材料属性以改变镜面强调照射的效果。

patch(fv,‘FaceColor’, [0.8 0.8 1.0], …

     'EdgeColor',       'none',        ...

     'FaceLighting',    'gouraud',     ...

     'AmbientStrength', 0.15);

% Add a camera light, and tone down the specular highlighting

camlight(‘headlight’);

material(‘dull’);

% Fix the axes scaling, and set a nice view angle

axis(‘image’);

view([-135 35]);

【源码】二进制立体光刻文件(STL)的MATLAB读取函数stlread_第1张图片

部分MATLAB代码:

function varargout = stlread(file)

% STLREAD imports geometry from an STL file into MATLAB.

% FV = STLREAD(FILENAME) imports triangular faces from the ASCII or binary

% STL file idicated by FILENAME, and returns the patch struct FV, with fields

% ‘faces’ and ‘vertices’.

%

% [F,V] = STLREAD(FILENAME) returns the faces F and vertices V separately.

%

% [F,V,N] = STLREAD(FILENAME) also returns the face normal vectors.

%

% The faces and vertices are arranged in the format used by the PATCH plot

% object.

% Copyright 2011 The MathWorks, Inc.

if ~exist(file,'file')

    error(['File ''%s'' not found. If the file is not on MATLAB''s path' ...

           ', be sure to specify the full path to the file.'], file);

end



fid = fopen(file,'r');    

if ~isempty(ferror(fid))

    error(lasterror); %#ok

end



M = fread(fid,inf,'uint8=>uint8');

fclose(fid);



[f,v,n] = stlbinary(M);

完整MATLAB下载地址:

http://page2.dfpan.com/fs/8lcj9221e291b6ce395/

更多精彩文章请关注微信号:【源码】二进制立体光刻文件(STL)的MATLAB读取函数stlread_第2张图片

你可能感兴趣的:(【源码】二进制立体光刻文件(STL)的MATLAB读取函数stlread)