STL file format – Matlab

7 September 2017

The STL format is a file format invented by the computer manufacturing company 3D Systems in 1987. Nowadays, it is one of the most common formats used in Rapid Prototyping industry and it is supported by many CAD softwares.

The name STL has at least a couples of backronyms like Standard Triangulation Language and STereo Litography interface format. To open an STL file (whose extension is .stl) without using a CAD software, there are specific free-source softwares like STLView for Windows and StL viewer for Mac OS.

What is an STL file

A STL file is a description of an unstructured triangular mesh which approximates the surface geometry of a 3-dimensional object.

In order to create an STL file, the object described has to have a closed surface, that is, the object has to be "solid", though a very thin one. This is needed because both the "exterior" and the "interior" of the surface have to be well determined.

Also, a STL file doesn't contain any information about color nor surface's texture, and coordinates are expressed in abritrary units.

What is needed for

A STL file is usually required when an object has to be created by any machine employing, for example, additive manufacturing technology.

Suppose you have at home a desktop 3D printer and you want to print an object. Normally, the first step is to create your object using a CAD software and save it in a format that can be read from your printer. In this case you have to create the STL file containing your "digital" object.

3D printers make use of particular softwares usually called Slicers which read the mesh describing the shape and determine a set of horizontal cross-sections of the 3D object. The Slicers will also allow you to choose the actual units, i.e., cm, mm, of the printed object.

Format specifications

A typical STL object would appear in this way:

Responsive image

You will se your object as a 3-dimensional triangular mesh. This is in fact what the .stl file is all about.

It is nothing but that a list of facets (the triangles) which are uniquely determined by their three vertices and their unique exterior unit normals $\textbf{n} = (n_x, n_y, n_z)$. Since each vertex has three components, $V_{1} = (V_{1}^x, V_{1}^y, V_{1}^z)$, $V_{2} = (V_{2}^x, V_{2}^y, V_{2}^z)$, and $V_{3} = (V_{3}^x, V_{3}^y, V_{3}^z)$, the number of data that have to be stored for each facet is 12.

The are three main features that the mesh satisfies.

1. Since the exterior normals have to be well defined, the vertices of a facet are numbered counterclockwise when looking at the object from the outside (right-hand rule).

Responsive image

The exterior normal of the facet in the above right figure is easily obtained using \[ \mathbf{n} = \frac{(V^{2} - V^{1}) \times (V^{3}-V^{1}) }{|(V^{2} - V^{1})\times (V^{3}-V^{1})|}, \] where $\times$ denotes the cross product. In Matlab, we can easily compute $\mathbf{n}$ via

v = V2 - V1;
w = V3 - V1;
n = cross(v,w) / norm(cross(v,w),1);

2. Each facet must share two vertices with each of its adjacent facets. That is, a vertex of one facet cannot lie on the side of another. This is tipically known as vertex-to-vertex rule. It easier to understand it by looking at the picture below:

Responsive image

The figure on the left violates the rule and such mesh cannot be represented in a STL file.

3. Although no information on the scale is given and units are arbitrary, the object described has to be located in the positive octant. This means that vertices coordinates have to be (strictly) positive numbers.

STL ASCII format

There are two formats to write a STL file, ASCII and binary. Although the latter would uses less memory and it is in fact employed when the object requires large meshes, the ASCII format is easier to read, and in this case, the .stl file can be opened and modified using a simple text editor.

The following, is a typical .stl file

solid nameOfTheSolid
   facet normal nx ny nz
      outer loop
         vertex V1x V1y V1z
         vertex V1x V1y V1z
         vertex V1x V1y V1z
   facet normal nx ny nz
      outer loop
         vertex V1x V1y V1z
         vertex V1x V1y V1z
         vertex V1x V1y V1z
endsolid nameOfTheSolid      

Words in bold denotes keywords that have to be lower case. "nameOfTheSolid" is tipically the name given to the object which can be also the name given to the .stl file, that is, nameOfTheSolid.stl.

Values for vertex's coordinates have to be float numbers.

Finally, below you find a simple Matlab scriptfile which creates a STL file from a surface or patch previously saved.

% Write STL ASCII file from patch or a surface defined by 
% a list of facets (structures F) with vertices as fields 
fileName  = 'nameOfTheFile'; 
solidName = 'nameOfTheSolid';
% Open the file for writing
fid = fopen(fileName ,'w');
% Write the file contents 
fprintf(fid,'solid %s\n',solidName);
for f = 1:numberFacets
   % Unit exterior normal
   F(f).n = cross( F(f).P2-F(f).P1, F(f).P3-F(f).P1 ); 
   F(f).n = F(f).n / norm(F(f).n);
   'facet normal %.7E %.7E %.7E\n'... 
   '  outer loop\n'...
   '    vertex %.7E %.7E %.7E\n'... 
   '    vertex %.7E %.7E %.7E\n'... 
   '    vertex %.7E %.7E %.7E\n'...
   '  endloop\n'...
   'endfacet\n'],F(f).n(1), F(f).n(2), F(f).n(3), ...
% Close the file
fprintf(fid,'endsolid %s',solidName);