Writing STL Files from ITK for 3D Printing

3D printers are quickly becoming a commodity technology. One can easily find 3D printers at supply stores, and they are sold at what used to be the price of Laser printers. The process of preparing 3D models as input for the 3D printer has also been de-facto standardized with the wide adoption of STL files. Today, the STL file is to 3D printing as the PDF file is to standard 2D ink printing.

Due to this ubiquitous adoption, it is important for any software that can potentially produce models for 3D printing to be able to export the models into STL files.

ITK currently provides data structures to represent meshes, in particular 2D manifolds in 3D space, which are probably the most common type of structure printed. However, until recently, ITK did not have a writer class capable of exporting a surface model into an STL file. Therefore, we wrote one.

A full description of this new C++ class is available in this article in the Insight Journal:
“STL file format MeshIO class for ITK”
http://www.insight-journal.org/browse/publication/913

The source code is available as an ITK remote module at:
https://github.com/luisibanez/ITKSTLMeshIO

The new itkSTLMeshIO class can write surfaces to STL files and can also read the content of an STL file into an ITK mesh, typically a QuadEdgeMesh.

An STL file contains the description of a surface in the form of a collection of triangles. This description is typically passed to a “slicer” application (such as skeinforge or Slic3r, not to be confused with “www.slicer.org”) that will be aware of the geometric parameters of a specific printer and the materials it uses. By combining this information, the slicer application will generate GCode, which is a description of the paths and actions that the heads (extruders) in the printer must take in order to progressively deposit the material and compose the shape. Since GCode files are fine-tuned to the specific characteristics of the printer, they are not portable across different devices.

Implementation

The STL standard supports two modes: ASCII and Binary. In both cases, the file contains a list of triangles, each of which is defined by three points. The points are specified by their x,y,z coordinates.
The ASCII version of an STL file starts with the line: “solid ascii” and ends with the line “endsolid.” In between, it has a sequence of triangles. Each one is specified by the block of text with the form:

The first line indicates the components of the normal to the triangle, and the three “vertex” lines inside the “outer loop” provide the coordinates of every vertex of the triangle. In principle, an STL file could represent polygons with more than three edges. In practice, however, most STL files contain only triangles.

The Binary version of an STL file starts with an 80-bytes header. The header should not have the characters “solid” in its first five bytes, since those are commonly used as an indication that the file format is ASCII and would, therefore, misguide readers of the file.

After the header, the total number of triangles in the shape is encoded into a 32-bit little endian integer. This already defines a limit to the total number of triangles that can be stored in an STL file. From then on, the file contains blocks of data per triangle, where every block has:

Luis Ibáñez is a Technical Leader at Kitware, Inc. He is one of the main developers of the Insight Toolkit (ITK). Luis is a strong supporter of Open Access publishing and the verification of reproducibility in scientific publications.