TETGENLINK TUTORIAL

Using TetGenLink

This section shows some of the ways that TetGenLink can be applied.

Convex Hull

To use TetGenLink, it must first be loaded.

In[43]:=
Click for copyable input

Next, some random points are generated and displayed.

In[44]:=
Click for copyable input
Out[45]=

Now you can compute the convex hull of the 3D points. TetGenConvexHull will return a list of points and surface triangles.

In[46]:=
Click for copyable input

Each surface triangle is a list of three integers. These integers refer to the coordinates. You can plot them by using GraphicsComplex.

In[47]:=
Click for copyable input
Out[47]=

To visualize the surface mesh as a wireframe, you can use a Line rather than a Polygon.

In[48]:=
Click for copyable input
Out[48]=

Delaunay Tetrahedralization

To use TetGenLink, it must first be loaded.

In[1]:=
Click for copyable input

This creates some 3D data.

In[2]:=
Click for copyable input

You can compute the Delaunay tetrahedralization of the data with TetGenDelaunay.

In[3]:=
Click for copyable input

The result is a list of points and tetrahedra. The list of tetrahedra is a list of four integers that refer to the coordinate positions. To visualize the tetrahedra as a wireframe, you can use a supporting function.

In[4]:=
Click for copyable input
In[5]:=
Click for copyable input
Out[5]=

To create a surface triangulation of the point data, you first create a TetGen instance.

In[6]:=
Click for copyable input
Out[6]=

To set the points in the TetGen instance, use TetGenSetPoints:

In[8]:=
Click for copyable input

The tetrahedralization is done with TetGenTetrahedralize:

In[9]:=
Click for copyable input

To extract the surface coordinates and surface triangles, use TetGenGetPoints and TetGenGetFaces:

In[13]:=
Click for copyable input

The surface mesh can then be visualized:

In[15]:=
Click for copyable input
Out[15]=

Meshing a Box (Basic)

To generate a tetrahedra mesh from a box, you first set up the corner points of the box.

In[46]:=
Click for copyable input
In[47]:=
Click for copyable input
Out[47]=

To specify how the corner points are connected to each other, you specify facets.

In[48]:=
Click for copyable input
In[49]:=
Click for copyable input
Out[49]=

To use TetGenLink, it must first be loaded.

In[50]:=
Click for copyable input

In order to create a mesh, an instance of TetGen is created. This input instance will hold the points and facets that describe the surface of the object to be tetrahedralized.

In[51]:=
Click for copyable input
Out[51]=

To set the corner points and the facets in the TetGen instance, you use TetGenSetPoints and TetGenSetFacets, respectively.

In[52]:=
Click for copyable input
Out[52]=
Out[53]=

When the operation of setting the points and facets has completed successfully, 0 is returned.

The tetrahedralization is then done with the TetGenTetrahedralize command. The tetrahedralization works on the TetGen input instance. A string of parameters may be given to specify requirements on the resulting mesh. The "p" string tetrahedralizes a piecewise linear complex, and the "q" switch requests a quality mesh generation. A minimum radius-edge ratio may be specified (the default is 2.0). The "a" switch followed by a number applies a maximum tetrahedron volume constraint. Once TetGen has completed the mesh generation process, a new TetGen instance is returned. The output instance contains all the data of the mesh.

In[54]:=
Click for copyable input
Out[54]=

To extract the coordinates and the surface triangles from the output instance, you use TetGenGetPoints and TetGenGetFaces.

In[55]:=
Click for copyable input

A visualization of the triangulated surface can be done with a GraphicsComplex.

In[57]:=
Click for copyable input
Out[57]=

To extract the tetrahedra you use TetGenGetElements.

In[58]:=
Click for copyable input

A simple support function allows you to visualize the wireframe mesh.

In[59]:=
Click for copyable input
In[60]:=
Click for copyable input
Out[60]=

A list of all TetGen instances is accessible through TetGenExpressions.

In[61]:=
Click for copyable input
Out[61]=

To clean up, you delete the input instance and the output instance of TetGen with the TetGenDelete command.

In[62]:=
Click for copyable input
Out[62]=

Meshing a Box (Advanced)

To use TetGenLink, it must first be loaded.

In[63]:=
Click for copyable input

What follows is a more detailed example of meshing a box. Here you will see how to specify the input to TetGen to create a hole in the box, an opening in the surface with a cavity, different (material) regions, and attribute markers to coordinates, faces, and elements. Also, a refinement of the mesh is shown.

You start by specifying the coordinates and displaying them.

In[64]:=
Click for copyable input
In[65]:=
Click for copyable input
Out[65]=

Then you create the input instance and set the points.

In[66]:=
Click for copyable input
Out[66]=
In[67]:=
Click for copyable input
Out[67]=

Next, the list of facets is created. If a facet list has more than one entry, like the third facet in this example, the first facet represents the bounding facet and the subsequent facets indicate where the bounding facet should have an opening.

In[68]:=
Click for copyable input

Here you can see one facet with an outline of where the facet should be open.

In[69]:=
Click for copyable input
Out[69]=

You can set the facets in the input instance of TetGen.

In[70]:=
Click for copyable input
Out[70]=

For TetGen to open the facet, you set up a facet hole.

In[71]:=
Click for copyable input

Then set the facet holes in the TetGen input instance with the TetGenSetFacetHoles command.

In[73]:=
Click for copyable input
Out[73]=

Here is the bounding facet and the facet hole position.

In[74]:=
Click for copyable input
Out[74]=

In the next step you can specify markers. TetGen provides you with the ability to propagate so-called markers. Markers are a convenient way to access grouped parts of the mesh object. This grouping can then be used to specify boundary conditions and regions in the mesh. TetGen allows you to specify markers for points, faces, and elements.

You start by assigning each coordinate point an integer, also called a marker. Once the mesh is created, TetGen will mark each newly created coordinate point according to the markers specified in the input instance of TetGen. In a similar manner, facets and regions are marked.

For each node and facet, you assign an integer according to the following table. This will group different parts of the surface representation together.
1 — bottom part
2 — top part
3 — side walls
4 — cavity/hole
9 — middle part

The point markers are set up and visualized in the following manner.

In[75]:=
Click for copyable input
Out[76]=

You can attribute integer markers to each of the facets.

In[77]:=
Click for copyable input

Both the point markers and the facet markers are set in the TetGen input instance.

In[78]:=
Click for copyable input
Out[78]=
Out[79]=

To visually explore the facets and the attributed markers, you can use a Manipulate. By selecting and deselecting you can visualize facets according to the markers they have been assigned.

In[80]:=
Click for copyable input
Out[80]=

TetGen works by meshing the entire object first and then in a second step removing the mesh from holes and cavities. Note that the cavity also needs a point to specify where it is situated. The point that specifies the opening is not sufficient. To specify where TetGen should put holes, you use TetGenSetHoles.

In[81]:=
Click for copyable input
In[82]:=
Click for copyable input

You can specify two different material domains by placing a point in each of them and then assigning a region number to them. Contrary to the point and facet markers, the region attributes are real numbers. With a region constraint you can additionally specify the required coarseness of the mesh in the different regions.

In[83]:=
Click for copyable input
In[86]:=
Click for copyable input

Here is the position of all the different points specified.

In[87]:=
Click for copyable input
Out[88]=

To generate a mesh from the facets, you give the options "p" and "q" as above, and the options "a" and "A" are needed for the maximum element size you specified with the region attribute. The switch "n" directs TetGen to compute the neighbors and the switch "e" directs it to compute the edges. For a complete list of switches, visit http://tetgen.org.

In[89]:=
Click for copyable input
Out[89]=

The extraction of the coordinates and the coordinate markers can be done with TetGenGetPoints and TetGenGetPointMarkers.

In[90]:=
Click for copyable input
In[92]:=
Click for copyable input
Out[92]=

Note that the marker 0 is present. Coordinates that have been attributed the marker 0 are interior coordinates.

In[93]:=
Click for copyable input
Out[93]=

To extract the faces and visualize them, you use TetGenGetFaces.

In[94]:=
Click for copyable input
Out[95]=

To extract the face markers, you use TetGenGetFaceMarkers.

In[96]:=
Click for copyable input

You can explore the facets and their markers with a Manipulate.

In[97]:=
Click for copyable input
Out[97]=
In[98]:=
Click for copyable input
Out[98]=

TetGenGetElements gets the tetrahedra from the output instance.

In[99]:=
Click for copyable input

To efficiently visualize them, you use the following supporting function. This sorts each face of every tetrahedron, and, by finding the union of all sorted faces, the duplicate interior faces are removed. For very large meshes, making use of this technique will result in a shorter rendering time for the graphics.

In[100]:=
Click for copyable input
In[101]:=
Click for copyable input
Out[101]=

Please note that the lower part is meshed finer than the upper part. This is the result of specifying different region constraints.

To extract the attributes of the elements, you use TetGenGetElementAttributes.

In[102]:=
Click for copyable input

You can pick the elements that belong to the region with attribute 10.; those are the elements in the lower part of the box.

In[103]:=
Click for copyable input
Out[104]=

TetGen can also provide the edges of the object. To have TetGen compute them, the option "e" needs to be specified in the tetrahedralization step above.

In[105]:=
Click for copyable input
Out[106]=

Also, the list of neighboring elements can be retrieved. Each entry in the neighbor list is a list of four neighboring tetrahedra. Should a tetrahedron be on a boundary, then the respective entry is set to .

In[107]:=
Click for copyable input
In[108]:=
Click for copyable input
Out[108]=

For illustration, the position of the boundary tetrahedra are:

In[109]:=
Click for copyable input
Out[109]//Short=

To refine an existing mesh, TetGenTetrahedralize is called once more on a TetGen instance. Now, however, with the "r" switch to request a refinement and the "a" switch with a new maximum tetrahedron volume. The "a" switch will now override the region constraints.

In[110]:=
Click for copyable input
Out[110]=
In[111]:=
Click for copyable input
In[113]:=
Click for copyable input
Out[113]=

Now clean up the created TetGen instances.

In[114]:=
Click for copyable input
Out[114]=

Engineering Example

To use TetGenLink, it must first be loaded.

In[7]:=
Click for copyable input


Download an engineering surface mesh of a mechanical object. First you load a package to fetch the URL. This will allow you to retrieve the data from the web and subsequently use the data without further internet access.

In[8]:=
Click for copyable input
In[9]:=
Click for copyable input

To make the tetrahedralization, you create an instance of TetGen.

In[10]:=
Click for copyable input
Out[10]=

The data in the surface mesh is imported into the TetGen instance.

In[11]:=
Click for copyable input

You can access the points and facets in the TetGen instance.

In[12]:=
Click for copyable input
Out[12]=
Out[13]=

The tetrahedralization step is then given as follows.

In[14]:=
Click for copyable input
Out[14]=

To extract the coordinates from the output instance, you use TetGenGetPoints and TetGenGetFaces.

In[15]:=
Click for copyable input

The surface of the meshed object can be displayed through a GraphicsComplex.

In[17]:=
Click for copyable input
Out[17]=

You extract the tetrahedron elements with the TetGenGetElements command.

In[18]:=
Click for copyable input

To compute the volume of each of the tetrahedra of the object, you write a support function first.

In[19]:=
Click for copyable input

The total volume can then be computed in the following manner.

In[20]:=
Click for copyable input
Out[20]=

The center of mass can be computed with a compiled function.

In[21]:=
Click for copyable input
In[22]:=
Click for copyable input
Out[22]=

You can visualize the center of mass as a red point within the object.

In[23]:=
Click for copyable input
Out[23]=

To color the surface of the object according to the distance to the center of mass, you write a support function that computes the distance from each coordinate to the center of mass.

In[69]:=
Click for copyable input
Out[69]=

The list of distances is then rescaled.

In[67]:=
Click for copyable input

The surface is colored by specifying the VertexColors and making use of ColorData.

In[68]:=
Click for copyable input
Out[68]=

To compute the surface, you write a compiled function.

In[24]:=
Click for copyable input

The total surface of the object can then be calculated.

In[25]:=
Click for copyable input
Out[25]=

If you have a C compiler, you also might want to give the option CompilationTarget->"C" to gain further speedups.

Possible Issues

To use TetGenLink, it must first be loaded.

In[9]:=
Click for copyable input

A potential issue that will result in a failed tetrahedralization can be caused by intersecting facets. To detect if a surface mesh has intersecting facets, TetGenDetectIntersectingFacets can be used. TetGenDetectIntersectingFacets returns a list of points and intersecting facets, should there be any.

In[10]:=
Click for copyable input
In[12]:=
Click for copyable input

You can then display the intersecting facets.

In[13]:=
Click for copyable input
Out[13]=
New to Mathematica? Find your learning path »
Have a question? Ask support »