After reading some stuff on STL files, I realized that they do no contain information about a mesh (connectivity for instance) , but they represent the surface geometry of the part to be meshed.

However, some available tools like the open source Gmsh, could import the STL file and create the mesh from the defined vertices within the STL file. Can anyone guide to some algorithm that performs such conversion ?

  • $\begingroup$ I believe Gmsh is not the only tool that can mesh STL models. For example, open source Netgen can also do that. $\endgroup$
    – martemyev
    Aug 13, 2013 at 15:58

1 Answer 1


What you need to do is to analyze the set of input vertices (each triangle contributes three of these) and assign consecutive indices to them so that vertices that appear multiple times (i.e. are shared by different triangles) are assigned the same index every time. This leaves you with a list of (unique) vertices ${\bf x}_i$, and a list of index triples $(i, j, k)$ indexing into the vertex list so that the $({\bf x}_i, {\bf x}_j, {\bf x}_k)$'s represent the original triangles.

In principle this is easy to do - just use a hash table to associate indices to 3D points and fill it as you iterate over all vertices.

In practice, the coordinates of equivalent vertices will possibly not be exactly identical but differ by small amounts due to roundoff problems. In this case, discretize your input data with a grid resolution of $\delta$ (which should be a small number corresponding to the maximum expected roundoff error, e.g. $10^{-6}$). Use the grid coordinates of your vertices thus obtained (rather than their 3D positions) as keys in your hashtable to find the associated vertex index, but make sure to check whether any one of the neighboring grid cells was assigned an index earlier before generating a new index. The last step ensures that close vertices will receive the same index.

  • $\begingroup$ Actually I used c++ to map vertices to their coordinates, it was then when i realized that some vertices may have different slightly different coordinates in some triangles. Now i can solve it thanks to the details you provided. Thanks huter bar $\endgroup$
    – SAAD
    Aug 18, 2013 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.