STL geometry file writer

Most of the 3D printer and CAD software are compatible with the STL file format. One of the reason is probably because it’s the least common denominator to describe a 3D geometry tessellation (assembly of triangles files). It can be in ASCII (like a .txt) or, to save some space and accelerate the loading/writing in binary format.

I will concentrate on the binary format as it’s the easiest to handle. The structure of the file is very simple:

  1. A header of 80 character
  2. A number of face/triangle
  3. The list of triangle with:
    • Normal to the surface (optional can be set to 0)
    • 3x Vertex coordinates in space (float32)
    • 1 uin16 for the status (the usage is not normalized, can be used for color or other things)

From the from this description it’s obvious that it’s not very optimized as the normal can be computed from the 3 vertexes and each vertex is stored multiple time for each face in witch is appears. There is also no dimension units it’s just a geometry and the user will have to select before printing if the coordinates are in mm or inches. The usage of the last status bit is not very clear and quite useless.

Everything considered the good thing is it’s very simple to write a STL file. The only caveat is to write the 3 points in the right order to have the normal of the surface pointing outside of the object (uses the write hand rule).

A lot of data can be represented as a color map (the color represent the value of a particular point in the image). So I wanted to be able to transform a map into an printable STL file. The example I will take is a measurement of my hand on a Projected capacitive touch sensor that I find nice. This is the touch technology used in cellphone where each ‘pixel/electrode’ of the image is a touch intensity reading.

handThe matlab writer (zMapToSTL function) is transforming a matrix of value in a block that can be printed. That means a border and some thickness is added under the top surface. One parameter defines the number of interpolation step that needs to be done on the surface data. It is used to smooth the output but generates a lot of extra points (heavy STL).

hand_surface_noInterpGeometry output without Interpolation

The Interpolation parameter makes the number of face grow exponentially and should be used with caution ūüôā

  • No Interpolation (0) gives a surface with 10800 triangles and an output STL size of 500kB
  • Interpolation (1) gives a surface with 42000 triangles and an output STL size of 2MB
  • Interpolation (2) gives a surface with 160000 triangles and an output STL size of 8MB

This is a detail of the (nicely) smooth result with interp = 2:


The function prototype is:

function zMapToSTL(Zmap, fileName, interpFactor, minThickness, maxThickness, maxWidth)
% Function to convert a Zmap into a STL file + visualisation of the result
% Parameters:
%   - ZMap (compulsory) : Map to represent
%   - fileName (compulsory) : STL file name
%   - interpFactor (default 0) : Interpolation factor of the map (0 = disable)
%  Scaling parameters:
%   - minThickness (default 2) : Minimum thickness of the output solid
%   - maxThickness (default 10) : Maximum thickness of the output solid
%   - maxWidth (default = max dimension of the image) : surface scaling

Note: The Matlab  function and the image are available HERE, all code are copyrighted, only usable for non-commercial purpose and provided as is with no guaranty of any sort,

The resulting STL files are here.

[<<Prev. РTBD]    [3D Printer articles]   [What is a Slicer РNext>>]


2 thoughts on “STL geometry file writer

    1. damienideas Post author

      I do like the single center point to build the faces under the block it’s very elegant.
      The shell/thickness option is interesting to save material.
      Thanks for the reference!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s