Lab4: Mesh Generation

The goal of this lab will be to generate a finite element mesh from the segmentation of the pig liver of the previous lab.

If you have not done the segmentation in the previous lab you can download the file which includes a segmented map for the liver and the ribs. The objective of this part will be to generate a finite element mesh of the liver, as well as a visualization and collision mesh for the liver and the ribs.

Generation of simulation meshes.

The mesh can be generated from the surface mesh (using GMsh or TetGen for example) or directly from the image using CGAL. Unfortunately surface meshes obtained directly from segmentation have many issues (badly shaped surface triangles, mesh too dense, limited choice of volume element size). Therefore we will focus on the generation of 3D tetrahedral mesh directly from the image using CGAL.

SOFA includes a plugin which is a wrapper of CGAL classes and allows you to change parameters, generate and export the mesh interactively. You can download the scene files at

Open the SOFA scene (note that kate is a text editor you can use your preferred one) :

cd MeshGeneration
kate MeshGenerationLiver.scn

The image is loaded by “ImageContainer ” which takes the name of the segmented map as “filename”.

The meshing is done using component “MeshGenerationFromImage ” which has 5 main parameters that can be adjusted:

  • angle view: quality of surface elements [default: 30]
  • cell radius-edge ratio: quality of the elements [default: 3]
  • facet distance: accuracy of meshing wrt the given segmentation [test 4, 6, 8]
  • facet size: size of the surface 2D element [test 4, 6, 8]
  • cell size: 3D element size [test 4, 6, 8]

The generated mesh is stored using VTKExporter in ~/PATH-TO-SCN/meshes.

Generate 3 volume meshes containing respectively about 250, 500 and 2000 nodes.

Visualization tool: Paraview

You can view tetrahedral meshes with paraview.

Open one of the tetrahedral model previously generated, click Apply, and select the Surface with edges view mode. In the inspector check the number of points and elements of the mesh. If the inspector is not activated you can activate it in the menu view -> statistic inspector .

Generation of the visual mesh

We will now generate the viewing surface. Load the 2000-point FEM mesh and apply the surface extraction filter: Filters -> Alphabetical -> Extract Surface, then click Apply. Note that depending on your version of Paraview, filters may only be available from the search menu (ctrl + space).

A new mesh is created which includes only the triangles on the surface of the tetrahedral mesh. Check that elements types are Triangles in the inspector.

We will now check that the normals of the surface mesh are well oriented (ie the indices of the triangles are numbered so that the normal of the triangles point towards the outside of the object). See an example below of flipped normals. Apply the Generate Surface Normals filter.

Optional: Before applying the filter deselect the Splitting and Non-Manifold Traversal option that allows you to change the orientation of the normals if the angle between the neighboring triangle is too high. This should not be necessary for the meshes provided in the previous link.

In the visualization properties of the Backface Styling select Cull Backface which means that the back faces of the triangles are not drawn (depending on your version of Paraview you may have to activate the advanced parameters to access this field or simply type backface in the search bar). If you see the inside of the object as in the picture below it means that normals are misdirected. In this case check the Normals flip in the filter properties and click Apply again.

To obtain a smooth surface, we will start by subdividing this surface mesh. To do this, apply the Subdivide filter, then set the Number Of Subdivision parameter to generate a triangular mesh of about 6000 points (you can know the number of points by activating the view-> Statistics Inspector menu). Selecting the Surface with edges view shows that the mesh is subdivided, but the added points remain on the initial surface.

Smooth the surface and texture coordinates

Apply the smooth filter and set the number of iterations to finally get a smooth surface. We will also generate the texture coordinates on the surface. Download the file.

Apply the Texture Map To Sphere filter . This filter will add texture coordinates considering that your object is a sphere. Attention it is important to understand that you have just generated the coordinates of textures ie the coordinates that associate to each 3D point of mulching a 2D coordinate of an image. However, the image will not be saved in the output file.

In the Miscellaneous display properties of the generated mesh, you can plate an image and view the result (note that depending on your version of Paraview this option may only be available through the search menu). Choose load and load an image file (for example the liver-cyrrhosis.png file ) to visualize the result.

The last step is to export the mesh in vtk format. Select the textured mesh and export the File->Save Data mesh and choose the vtk extension, and choose ascii.

Generation of Collision Mesh

The goal now is to generate the collision mesh. The collision mesh is a simplified mesh of the surface with which the collisions will be calculated. The collision mesh will not be visualized in the simulation, so it is not necessary to generate the texture coordinates.

Generate a triangular mesh with about 500 nodes. For that, you can use the filter of decimage which makes it possible to decrease the number of points of the mesh. In this case, select the preserve topology option of the filter. Another option would be to load the tetrahedral mesh of 250 nodes and extract the surface.

Mesh of the ribs

Finally, repeat the operation to generate the surface and collision meshes for the ribs. For this, a segmented map is provided in the file. Use this segmentation to generate a solid mesh that will then be used to generate visualization meshes and rib collisions. The collision mesh must not exceed 1500 nodes.