In order to simulate a mechanical object using Finite Element Modeling (FEM), a discrete version of its volume is required. This volume, referred as volumetric mesh will serve as a domain for FEM computation. The aim of this tutorial is to explain how to create such a volumetric mesh from a closed surface description made of triangles. Several tools exist for volume meshing such as GID (http://www.gidhome.com/), CGAL (http://www.cgal.org/) or Gmsh (http://geuz.org/gmsh/). In the following, we will use CGAL since a dedicated plugin is available within the SOFA framework.
A. First generic example
CGAL creates a 3D volumetric mesh composed of tetrahedra from any closed surface. The SOFA Python file to perform such a procedure should contain at least:
 The directive that will dynamically link the plugin to SOFA executable:
rootNode.createObject(‘RequiredPlugin’, pluginName=‘CGALPlugin’)  We then create a node object that will gather the input and the processing through
node = rootNode.createChild(‘node’)  The closed 3D surface which should be described in any 3D format supported by SOFA (OBJ, VTK STL,…)
node.createObject(‘Mesh’,name=‘mesh’,filename=path+‘bunny.obj’)  The CGAL method that will process the surface to compute the volumetric mesh:
node.createObject(‘MeshGenerationFromPolyhedron’,name=‘gen’,inputPoints=‘@mesh.position’, inputTriangles=‘@mesh.triangles’, drawTetras=‘1’)  The parameters used for this component are related to the input data: inputPoints, inputTriangles or inputQuads describe the composition of the input mesh
 The resulting volumetric mesh is then exported in a VTK file by:
node.createObject(‘Mesh’, position=‘@gen.outputPoints’, tetrahedra=‘@gen.outputTetras’)
node.createObject(‘VTKExporter’, filename=‘bunny’, edges=‘0’, tetras=‘1’, exportAtBegin=‘1’)
The first line creates a Mesh object that will be defined by the vertices and the tetrahedra that were computed by CGAL and the second line allows to export the resulting volumetric mesh at the beginning of the SOFA simulation (please note that the .vtu file extension is automatically added at the end of the filename).
By adding the usual SOFA Python directives, the complete scene file is available at the following location on the virtual image hard drive. Running this scene file with SOFA using the Stanford Bunny 3D model (https://graphics.stanford.edu/data/3Dscanrep/ ) leads to the following result:
B. Additional information
Using CGAL with default parameters often creates a very fine mesh composed of many vertices. This may involve a large computational fingerprint that is not compatible with realtime control of the soft robot. The MeshGenerationFromPolyhedron component allows to tune the tetrahedra budget in order to optimize the tradeoff between simulation speed and accuracy. The finetuning of these parameters requires some background on Delaunay Triangulation (https://en.wikipedia.org/wiki/Delaunay_triangulation) but some rules of thumb may be provided. There are four parameters that can be modified to change the coarseness of the volumetric mesh:


 facetSize: this parameter provides an upper bound for the radii of the surface Delaunay ball; a larger value may lead to larger tetrahedra.
 facetApproximation: the approximation error between the boundary and the subdivision surface. It provides an upper bound for the distance between the circumcenter of a surface facet and the center of a surface Delaunay ball of this facet.
 cellRatio: This parameter controls the shape of mesh cells. Actually, it is an upper bound for the ratio between the circumradius of a mesh tetrahedron and its shortest edge. There is a theoretical bound for this parameter: the Delaunay refinement process is guaranteed to terminate for values larger than 2.
 cellSize: This parameter controls the size of mesh tetrahedra. It provides an upper bound on the circumradius of the mesh tetrahedra.
 facetAngle: This parameter controls the shape of surface facets. Actually, it is a lower bound for the angle (in degree) of surface facets. When boundary surfaces are smooth, the termination of the meshing process is guaranteed if the angular bound is at most 30 degrees.

Starting from the previous SOFA scene file, we add these four parameters to the MeshGenerationFromPolyhedron component leading to:
node.createObject(‘MeshGenerationFromPolyhedron’, inputPoints=‘@mesh.position’, inputTriangles=‘@mesh.triangles’, drawTetras=‘1’, facetSize=“1.5”, facetApproximation=“0.15”, cellRatio=“2”, cellSize=“1.5”) 
Instead of the 26k vertices for the tetrahedral mesh, a coarser mesh is obtained with only 600 vertices. In order to refine the mesh, we can decrease the value of these parameters. For instance, with:
node.createObject(‘MeshGenerationFromPolyhedron’, inputPoints=‘@mesh.position’, inputTriangles=‘@mesh.triangles’, drawTetras=‘1’, facetSize=“0.35”, facetApproximation=“0.1”, cellRatio=“2”, cellSize=“0.5” ) 
The resulting mesh is composed of 2250 vertices, which provides good accuracy and is within the acceptable range for realtime computations: our models are usually composed of 12k vertices.