把STL文件ACSII格式读入matlab
2012-02-13 19:31
337 查看
function [fout, vout, cout] = ReadSTLACSII(filename)
fid=fopen(filename, 'r');
if fid == -1
error('文件打开错误!.')
end
%读取文件头,STL文件第一行是文件名
File_name = sscanf(fgetl(fid), '%*s %s'); %CAD object name, if needed.
%定义变量
vnum=0; %Vertex number counter.
report_num=0; %Report the status as we go.
STLvcolor = 0;
STLxyz=0;
%开始读整个文件,直到结束符为止
while feof(fid) == 0 % test for end of file, if not then do stuff
tline = fgetl(fid); % reads a line of data from file.
fword = sscanf(tline, '%s'); % make the line a character string
%检查颜色
if strncmpi(fword, 'facet normal',12) == 1; % Checking if a "C"olor line, as "C" is 1st char.
STLvcolor = sscanf(tline, '%*s %f %f %f'); % & if a C, get the RGB color data of the face. % we "*s" skip the name "color" and get the data.
end % Keep this color, until the next color is used.
%检查坐标
if strncmpi(fword, 'vertex',6) == 1; % Checking if a "V"ertex line, as "V" is 1st char.
STLxyz= sscanf(tline, '%*s %lf %lf %lf'); % & if a V, get the XYZ data of it.
vnum = vnum + 1;
end
%检查坐标是否增加
if vnum~=report_num
report_num=vnum;
v(:,vnum)=STLxyz;
c(:,vnum) = STLvcolor; % A color for each vertex, which will color the
if mod(report_num,249)==0;
disp(sprintf('Reading vertix num: %d.',vnum));
end
end
end
%关闭文件
fclose(fid);
% Build face list; The vertices are in order, so just number them.
fnum = vnum/3; %Number of faces, vnum is number of vertices. STL is triangles.
flist = 1:vnum; %Face list of vertices, all in order.
F = reshape(flist, 3,fnum); %Make a "3 by fnum" matrix of face list data.
%
% Return the faces and vertexs.
%
fout = F'; %Orients the array for direct use in patch.
vout = v'; % "
cout = c';
end
fid=fopen(filename, 'r');
if fid == -1
error('文件打开错误!.')
end
%读取文件头,STL文件第一行是文件名
File_name = sscanf(fgetl(fid), '%*s %s'); %CAD object name, if needed.
%定义变量
vnum=0; %Vertex number counter.
report_num=0; %Report the status as we go.
STLvcolor = 0;
STLxyz=0;
%开始读整个文件,直到结束符为止
while feof(fid) == 0 % test for end of file, if not then do stuff
tline = fgetl(fid); % reads a line of data from file.
fword = sscanf(tline, '%s'); % make the line a character string
%检查颜色
if strncmpi(fword, 'facet normal',12) == 1; % Checking if a "C"olor line, as "C" is 1st char.
STLvcolor = sscanf(tline, '%*s %f %f %f'); % & if a C, get the RGB color data of the face. % we "*s" skip the name "color" and get the data.
end % Keep this color, until the next color is used.
%检查坐标
if strncmpi(fword, 'vertex',6) == 1; % Checking if a "V"ertex line, as "V" is 1st char.
STLxyz= sscanf(tline, '%*s %lf %lf %lf'); % & if a V, get the XYZ data of it.
vnum = vnum + 1;
end
%检查坐标是否增加
if vnum~=report_num
report_num=vnum;
v(:,vnum)=STLxyz;
c(:,vnum) = STLvcolor; % A color for each vertex, which will color the
if mod(report_num,249)==0;
disp(sprintf('Reading vertix num: %d.',vnum));
end
end
end
%关闭文件
fclose(fid);
% Build face list; The vertices are in order, so just number them.
fnum = vnum/3; %Number of faces, vnum is number of vertices. STL is triangles.
flist = 1:vnum; %Face list of vertices, all in order.
F = reshape(flist, 3,fnum); %Make a "3 by fnum" matrix of face list data.
%
% Return the faces and vertexs.
%
fout = F'; %Orients the array for direct use in patch.
vout = v'; % "
cout = c';
end
相关文章推荐
- 将CCD的RAW图像格式文件直接读入到MATLAB中实现
- Matlab中用一个For循环连续读入并显示几个图像文件 [转]
- matlab 以excel格式将字符串数组写入TXT文件
- matlab——连续读入多个文件技巧集合
- matlab的各种数据读取(txt,dat,mat等格式),文件打开关闭
- matlab多文件读入问题的几行代码
- Matlab Tricks( 八)—— 将 pgm 格式文件转换为 png 格式
- Matlab保存uint16格式文件的相关注意事项
- CxImage与OpenGL结合,用于读入多种格式的纹理以及用来把屏幕保存为各种格式的图像文件
- 常见三维文件格式之STL, VRML, X3D
- pgm格式文件及其在matlab中的读取存储方法(2)
- CxImage与OpenGL结合,用于读入多种格式的纹理以及用来把屏幕保存为各种格式的图像文件
- 利用FFmpeg将H.264文件读入内存,再输出封装格式文件
- .stl文件(CAD三维模型)格式 及 基于C/C++的数据读取
- 模型文件格式:off stl obj
- PCL系列——读入PCD格式文件
- Matlab读入含有特殊分隔符的文件(textread)
- 把MATLAB数据转换成1QN格式,并保存到文件供ISE使用
- OpenCv下的xml文件在matlab下的读入
- CxImage与OpenGL结合,用于读入多种格式的纹理以及用来把屏幕保存为各种格式的图像文件