-
Notifications
You must be signed in to change notification settings - Fork 3
/
stlread.m
107 lines (89 loc) · 3.09 KB
/
stlread.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function [v, f, n, c, stltitle] = stlread(filename, verbose,slim)
% This function reads an STL file in binary format into vertex and face
% matrices v and f.
%
% USAGE: [v, f, n, c, stltitle] = stlread(filename, verbose,slim);
%
% verbose is an optional logical argument for displaying some loading
% information (default is false).
%
% v contains the vertices for all triangles [3*n x 3].
% f contains the vertex lists defining each triangle face [n x 3].
% n contains the normals for each triangle face [n x 3].
% c is optional and contains color rgb data in 5 bits [n x 3].
% stltitle contains the title of the specified stl file [1 x 80].
%
% To see plot the 3D surface use:
% patch('Faces',f,'Vertices',v,'FaceVertexCData',c);
% or
% plot3(v(:,1),v(:,2),v(:,3),'.');
%
% Duplicate vertices can be removed using:
% [v, f]=patchslim(v, f);
%
% For more information see:
% http://www.esmonde-white.com/home/diversions/matlab-program-for-loading-stl-files
%
% Based on code originally written by:
% Doron Harlev
% and combined with some code by:
% Eric C. Johnson, 11-Dec-2008
% Copyright 1999-2008 The MathWorks, Inc.
%
% Re-written and optimized by Francis Esmonde-White, May 2010.
% Slightly tweaked by Robert Ellenberg, July 2013
use_color=(nargout>=4);
fid=fopen(filename, 'r'); %Open the file, assumes STL Binary format.
if fid == -1
error('File could not be opened, check name or path.')
end
if ~exist('verbose','var')
verbose = false;
end
ftitle=fread(fid,80,'uchar=>schar'); % Read file title
numFaces=fread(fid,1,'int32'); % Read number of Faces
T = fread(fid,inf,'uint8=>uint8'); % read the remaining values
fclose(fid);
stltitle = char(ftitle');
if verbose
fprintf('\nTitle: %s\n', stltitle);
fprintf('Number of Faces: %d\n', numFaces);
disp('Please wait...');
end
% Each facet is 50 bytes
% - Three single precision values specifying the face normal vector
% - Three single precision values specifying the first vertex (XYZ)
% - Three single precision values specifying the second vertex (XYZ)
% - Three single precision values specifying the third vertex (XYZ)
% - Two color bytes (possibly zeroed)
% 3 dimensions x 4 bytes x 4 vertices = 48 bytes for triangle vertices
% 2 bytes = color (if color is specified)
trilist = 1:48;
ind = reshape(repmat(50*(0:(numFaces-1)),[48,1]),[1,48*numFaces])+repmat(trilist,[1,numFaces]);
Tri = reshape(typecast(T(ind),'single'),[3,4,numFaces]);
n=squeeze(Tri(:,1,:))';
n=double(n);
v=Tri(:,2:4,:);
v = reshape(v,[3,3*numFaces]);
v = double(v)';
f = reshape(1:3*numFaces,[3,numFaces])';
if use_color
c0 = typecast(T(49:50),'uint16');
if (bitget(c0(1),16)==1)
trilist = 49:50;
ind = reshape(repmat(50*(0:(numFaces-1)),[2,1]),[1,2*numFaces])+repmat(trilist,[1,numFaces]);
c0 = reshape(typecast(T(ind),'uint16'),[1,numFaces]);
r=bitshift(bitand(2^16-1, c0),-10);
g=bitshift(bitand(2^11-1, c0),-5);
b=bitand(2^6-1, c0);
c=[r; g; b]';
else
c = zeros(numFaces,3);
end
end
if verbose
disp('Done!');
end
if exist('slim','var') && slim
[v,f]=patchslim(v,f);
end