title: A method to calculate the centre of mass from a .stl (stereo lithography) file?
body: <p>I am trying to calculate the centre of mass (x,y,z) coordinates of an object defined in an STL file (stereo lithography, not to be confused with the standard template library). The STL file contains a closed object (or objects) defined by a boundary made of triangles. The triangles themselves are not necessarily in any order, the file is simply the coordinates 3 vertices of each triangle floating in 3D space plus a normal vector to the triangle (the normal should be disregarded as it is not always done properly). There is nothing that links each triangle to one another, it is assumed that the object is closed.</p>
  
  <p>One simple approach would be to divide a volume (in this case, a box) into millions of elements and determine if each element is inside the object defined in the STL file or not, then sum up the moments and calculate the centre of mass. This would work but its far from elegant and extremely slow. </p>
  
  <p>Another method would be to convert the boundary representation into a number of packed tetrahedron solids. Form that I could calculate the centre of mass of each tetrahedron, its volume, and resulting moment and thus calculate the overall centre of mass from the sum of all tetrahedrons. The problem with this is that I don't know how to convert a surface representation of triangles into a volume representation of tetrahedrons (I'm assuming its a fairly non trivial task). </p>
  
  <p>Dose anyone know of any methods or can think up of any methods that I could try? Or maybe even any reference material that talks about this? </p>
  
  <p>For more information about STL files (only the first 2 sections are important, everything else is useless): <a href="http://en.wikipedia.org/wiki/STL_%28file_format%29" rel="noreferrer">http://en.wikipedia.org/wiki/STL_%28file_format%29</a></p>