function VerMat= ReadSTLFile ( )
filename= 'model1or.stl' ;
fid= fopen ( filename, 'r' ) ;
VerMat ( 1 , : ) = [ 0 0 0 ] ;
k= 0 ;
while ( feof ( fid) == 0 )
line= fgetl ( fid) ;
matchev= findstr ( line, 'vertex' ) ;
if ( matchev)
k= k+ 1 ;
VerMat ( k, : ) = OpenVertexCode ( line) ;
end
end
fclose ( fid) ;
function vec= OpenVertexCode ( line)
n= length ( line) ;
k= 0 ;
BlSp ( 1 ) = 0 ;
for i = 1 : n
if ( line ( i ) == 32 )
k= k+ 1 ;
BlSp ( k) = i ;
end
end
Pos1= BlSp ( 1 ) + 1 ;
Pos2= BlSp ( 2 ) - 1 ;
N= Pos2- Pos1+ 1 ;
NumCode= line ( Pos1: Pos2) ;
vec ( 1 ) = OpenNumCode ( NumCode, N) ;
Pos1= BlSp ( 2 ) + 1 ;
Pos2= BlSp ( 3 ) - 1 ;
N= Pos2- Pos1+ 1 ;
NumCode= line ( Pos1: Pos2) ;
vec ( 2 ) = OpenNumCode ( NumCode, N) ;
Pos1= BlSp ( 3 ) + 1 ;
Pos2= n;
N= Pos2- Pos1+ 1 ;
NumCode= line ( Pos1: Pos2) ;
vec ( 3 ) = OpenNumCode ( NumCode, N) ;
function ab= OpenNumCode ( NumCode, N)
is= 1 ;
if ( NumCode ( 1 ) == 45 )
is= 2 ;
end
for i = 1 : N
if ( NumCode ( i ) == 46 )
ip= i ;
break ;
end
end
isf= ip- 1 ;
ise= ip+ 1 ;
af= 0 ;
posnum= 1 / 10 ;
for i = isf: - 1 : is
posnum= posnum* 10 ;
af= af+ ( NumCode ( i ) - 48 ) * posnum;
end
bf= 0 ;
posnum= 1.0 ;
for i = ise: N
posnum= posnum/ 10 ;
bf= bf+ ( NumCode ( i ) - 48 ) * posnum;
end
ab= af+ bf;
if ( is== 2 )
ab= - ab;
end
function [ pt, pgon] = ConstrucTopology ( VerMat)
N= length ( VerMat ( : , 1 ) ) ;
Num= N/ 3 ;
for i = 1 : Num
pgon{ i } = [ 3 * ( i - 1 ) + 1 3 * ( i - 1 ) + 2 3 * ( i - 1 ) + 3 ] ;
end
pt= VerMat;
function showpgon ( pt, pgon)
view ( 3 ) ;
pat= zeros ( 1 , length ( pgon) ) ;
hold on
for i = 1 : length ( pgon)
ptpgoni= pt ( pgon{ i } , : ) ;
pat ( i ) = patch ( ptpgoni ( : , 1 ) , ptpgoni ( : , 2 ) , ptpgoni ( : , 3 ) , [ 1.000 0.7000 0.3000 ] , 'facelighting' , 'flat' , 'edgecolor' , ...
[ 0 1.00 1.00 ] , 'edgelighting' , 'flat' ) ;
end
hold off
axis equal
function ReadSTLTest ( )
VerMat= ReadSTLFile ( ) ;
[ pt, pgon] = ConstrucTopology ( VerMat) ;
showpgon ( pt, pgon) ;