Skip to content

Commit 8e8932c

Browse files
committed
2018/6/15
Part of the Code read the STL files(Binary or ASCII )
1 parent f069311 commit 8e8932c

12 files changed

+281
-0
lines changed
8.06 MB
Binary file not shown.

Test_Models/femur.stl

1.29 MB
Binary file not shown.

Test_Models/femur_O.stl

3.18 MB
Binary file not shown.
57.5 MB
Binary file not shown.

Test_Models/horse.stl

15.9 MB
Binary file not shown.

Test_Models/spookytree.stl

282 KB
Binary file not shown.

infill_outline.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function [Crossing_Point_of_each_Slice]= infill_outline(Crossing_Point_of_All_Slices,Number_of_Slice)
2+
% [Crossing_Poin_of_each_Slice(:,1),Crossing_Poin_of_each_Slice(:,2)]表示该切片层所有交点的XY坐标值
3+
Crossing_Point_of_each_Slice = Crossing_Point_of_All_Slices{Number_of_Slice}; % 通过for循环 逐个提取元胞子矩阵 到矩阵Crossing_Poin_of_each_Slice中
4+
if ~isempty(Crossing_Point_of_each_Slice)
5+
% fill(X,Y,C) 根据 X 和 Y 中的数据创建填充的多边形(顶点颜色由 C 指定)。
6+
% fill 可将最后一个顶点与第一个顶点相连以闭合多边形。X 和 Y 的值可以是数字、日期时间、持续时间或类别值。
7+
X = (Crossing_Point_of_each_Slice(:,1))';
8+
Y = (Crossing_Point_of_each_Slice(:,2))';
9+
fill(X,Y,'w')
10+
end
11+
rotate3d on
12+
end

plot_stl.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function [] = plot_stl(v)
2+
%%% plots the triangles in stl file. The vertices in v need to be
3+
%%% continuous.
4+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5+
%%%%Author: Sunil Bhandari%%%%%%%%
6+
%%%%Date: Mar 29, 2017 %%%%%%%%%%%
7+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8+
vr = reshape(v',9,size(v,1)/3)';
9+
vr = [vr(:,:),vr(:,1:3),ones(size(vr,1),1)*[NaN, NaN, NaN]];
10+
vr = reshape(vr',3,size(vr,1)*5)';
11+
h = figure(1);
12+
set(h, 'renderer','opengl')
13+
view(3)
14+
plot3(vr(:,1),vr(:,2),vr(:,3))
15+
end

read_ascii_stl_file.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
%%% read ascii stl file
2+
function [numTriangles,triangles] = read_ascii_stl_file(filename, header_lines_num)
3+
% header_lines_num表示文件开头占用的行数,
4+
% 一般标准的ascii文件开头的第一行是文件的属性信息,
5+
% 第二行及以下行才是三角面片的详细顶点坐标和法向量
6+
%以下举例ascii STL文件的数据结构
7+
%*******************************************************
8+
% solid D638_TypeI_ascii
9+
% facet normal 2.111806e-016 0.000000e+000 1.000000e+000
10+
% outer loop
11+
% vertex 3.285763e+001 3.200000e+000 1.900000e+001
12+
% vertex 0.000000e+000 3.200000e+000 1.900000e+001
13+
% vertex 3.285763e+001 0.000000e+000 1.900000e+001
14+
% endloop
15+
% endfacet
16+
% ......
17+
% endsolid
18+
%*********************************************************
19+
fid = fopen(filename);
20+
for i = 1: header_lines_num
21+
tline = fgetl(fid);
22+
end
23+
i = 1;
24+
tline = fgetl(fid);
25+
z1 = {'proceed'};
26+
while ~strcmp(z1{1}, 'endsolid')
27+
normals = strsplit(tline,' ');
28+
% triangles(i,10:12)是三角面片的法矢量坐标
29+
triangles(i,10:12) = str2double(normals(end-2:end));
30+
tline = fgetl(fid);
31+
tline = fgetl(fid);
32+
vertex1 = strsplit(tline, ' ');
33+
% triangles(i,1:3)是三角面片的第一顶点坐标
34+
triangles(i,1:3) = str2double(vertex1(end-2:end));
35+
tline = fgetl(fid);
36+
vertex2 = strsplit(tline, ' ');
37+
% triangles(i,4:6)是三角面片第二顶点坐标
38+
triangles(i,4:6) = str2double(vertex2(end-2:end));
39+
tline = fgetl(fid);
40+
vertex3 = strsplit(tline, ' ');
41+
% triangles(i,7:9)是三角面片的第三顶点坐标
42+
triangles(i,7:9) = str2double(vertex3(end-2:end));
43+
tline = fgetl(fid);
44+
tline = fgetl(fid);
45+
i = i + 1;
46+
tline = fgetl(fid);
47+
z1 = strsplit(tline, ' ');
48+
end
49+
numTriangles = size(triangles,1);
50+
fclose(fid);
51+
end
52+
%fclose(fid)

read_binary_or_ascii_stl.m

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
function varargout = read_binary_or_ascii_stl(file)
2+
% STLREAD imports geometry from an STL file into MATLAB.
3+
% FV = STLREAD(FILENAME) imports triangular faces from the ASCII or binary
4+
% STL file idicated by FILENAME, and returns the patch struct FV, with fields
5+
% 'faces' and 'vertices'.
6+
%
7+
% [F,V] = STLREAD(FILENAME) returns the faces F and vertices V separately.
8+
%
9+
% [F,V,N] = STLREAD(FILENAME) also returns the face normal vectors.
10+
%
11+
% The faces and vertices are arranged in the format used by the PATCH plot
12+
% object.
13+
14+
% Copyright 2011 The MathWorks, Inc.
15+
16+
if ~exist(file,'file')
17+
error(['File ''%s'' not found. If the file is not on MATLAB''s path' ...
18+
', be sure to specify the full path to the file.'], file);
19+
end
20+
21+
fid = fopen(file,'r');
22+
if ~isempty(ferror(fid))
23+
error(lasterror); %#ok
24+
end
25+
26+
M = fread(fid,inf,'uint8=>uint8');
27+
fclose(fid);
28+
29+
[f,v,n] = stlbinary(M);
30+
%if( isbinary(M) ) % This may not be a reliable test
31+
% [f,v,n] = stlbinary(M);
32+
%else
33+
% [f,v,n] = stlascii(M);
34+
%end
35+
36+
varargout = cell(1,nargout);
37+
switch nargout
38+
case 2
39+
varargout{1} = f;
40+
varargout{2} = v;
41+
case 3
42+
varargout{1} = f;
43+
varargout{2} = v;
44+
varargout{3} = n;
45+
otherwise
46+
varargout{1} = struct('faces',f,'vertices',v);
47+
end
48+
49+
end
50+
51+
52+
function [F,V,N] = stlbinary(M)
53+
54+
F = [];
55+
V = [];
56+
N = [];
57+
58+
if length(M) < 84
59+
error('MATLAB:stlread:incorrectFormat', ...
60+
'Incomplete header information in binary STL file.');
61+
end
62+
63+
% Bytes 81-84 are an unsigned 32-bit integer specifying the number of faces
64+
% that follow.
65+
numFaces = typecast(M(81:84),'uint32');
66+
%numFaces = double(numFaces);
67+
if numFaces == 0
68+
warning('MATLAB:stlread:nodata','No data in STL file.');
69+
return
70+
end
71+
72+
T = M(85:end);
73+
F = NaN(numFaces,3);
74+
V = NaN(3*numFaces,3);
75+
N = NaN(numFaces,3);
76+
77+
numRead = 0;
78+
while numRead < numFaces
79+
% Each facet is 50 bytes
80+
% - Three single precision values specifying the face normal vector
81+
% - Three single precision values specifying the first vertex (XYZ)
82+
% - Three single precision values specifying the second vertex (XYZ)
83+
% - Three single precision values specifying the third vertex (XYZ)
84+
% - Two unused bytes
85+
i1 = 50 * numRead + 1;
86+
i2 = i1 + 50 - 1;
87+
facet = T(i1:i2)';
88+
89+
n = typecast(facet(1:12),'single');
90+
v1 = typecast(facet(13:24),'single');
91+
v2 = typecast(facet(25:36),'single');
92+
v3 = typecast(facet(37:48),'single');
93+
94+
n = double(n);
95+
v = double([v1; v2; v3]);
96+
97+
% Figure out where to fit these new vertices, and the face, in the
98+
% larger F and V collections.
99+
fInd = numRead + 1;
100+
vInd1 = 3 * (fInd - 1) + 1;
101+
vInd2 = vInd1 + 3 - 1;
102+
103+
V(vInd1:vInd2,:) = v;
104+
F(fInd,:) = vInd1:vInd2;
105+
N(fInd,:) = n;
106+
107+
numRead = numRead + 1;
108+
end
109+
110+
end
111+
112+
113+
function [F,V,N] = stlascii(M)
114+
warning('MATLAB:stlread:ascii','ASCII STL files currently not supported.');
115+
F = [];
116+
V = [];
117+
N = [];
118+
end
119+
120+
% TODO: Change the testing criteria! Some binary STL files still begin with
121+
% 'solid'.
122+
function tf = isbinary(A)
123+
% ISBINARY uses the first line of an STL file to identify its format.
124+
if isempty(A) || length(A) < 5
125+
error('MATLAB:stlread:incorrectFormat', ...
126+
'File does not appear to be an ASCII or binary STL file.');
127+
end
128+
if strcmpi('solid',char(A(1:5)'))
129+
tf = false; % ASCII
130+
else
131+
tf = true; % Binary
132+
end
133+
end
134+
135+

0 commit comments

Comments
 (0)