# 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[1]:=

Next, some random points are generated and displayed.

 In[1]:=
 Out[2]=

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

 In[3]:=

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

 In[4]:=
 Out[4]=

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

 In[5]:=
 Out[5]=

## Delaunay Tetrahedralization

To use TetGenLink, it must first be loaded.

 In[6]:=

This creates some 3D data.

 In[7]:=

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

 In[8]:=

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[9]:=
 In[10]:=
 Out[10]=

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

 In[11]:=
 Out[11]=

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

 In[12]:=

The tetrahedralization is done with TetGenTetrahedralize.

 In[13]:=

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

 In[14]:=

The surface mesh can then be visualized.

 In[16]:=
 Out[16]=

## Meshing a Box (Basic)

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

 In[17]:=
 In[18]:=
 Out[18]=

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

 In[19]:=
 In[20]:=
 Out[20]=

To use TetGenLink, it must first be loaded.

 In[21]:=

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[22]:=
 Out[22]=

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

 In[23]:=
 Out[23]=
 Out[24]=

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 (TetGen string codes) 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[25]:=
 Out[25]=

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

 In[26]:=

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

 In[28]:=
 Out[28]=

To extract the tetrahedra you use TetGenGetElements.

 In[29]:=

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

 In[30]:=
 In[31]:=
 Out[31]=

A list of all TetGen instances is accessible through TetGenExpressions.

 In[32]:=
 Out[32]=

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

 In[33]:=
 Out[33]=

## Meshing a Box (Advanced)

To use TetGenLink, it must first be loaded.

 In[34]:=

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[35]:=
 In[36]:=
 Out[36]=

Then you create the input instance and set the points.

 In[37]:=
 Out[37]=
 In[38]:=
 Out[38]=

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[39]:=

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

 In[40]:=
 Out[40]=

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

 In[41]:=
 Out[41]=

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

 In[42]:=

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

 In[44]:=
 Out[44]=

Here is the bounding facet and the facet hole position.

 In[45]:=
 Out[45]=

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[46]:=
 Out[47]=

You can attribute integer markers to each of the facets.

 In[48]:=

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

 In[49]:=
 Out[49]=
 Out[50]=

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[51]:=
 Out[51]=

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[52]:=
 In[53]:=

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[54]:=
 In[57]:=

Here is the position of all the different points specified.

 In[58]:=
 Out[59]=

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[60]:=
 Out[60]=

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

 In[61]:=
 In[63]:=
 Out[63]=

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

 In[64]:=
 Out[64]= Play Animation

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

 In[65]:=
 Out[66]=

To extract the face markers, you use TetGenGetFaceMarkers.

 In[67]:=

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

 In[68]:=
 Out[68]=
 In[69]:=
 Out[69]=

TetGenGetElements gets the tetrahedra from the output instance.

 In[70]:=

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[71]:=
 In[72]:=
 Out[72]=

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[73]:=

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[74]:=
 Out[75]=

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[76]:=
 Out[77]=

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[78]:=
 In[79]:=
 Out[79]=

For illustration, the position of the boundary tetrahedra are:

 In[80]:=
 Out[80]//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[81]:=
 Out[81]=
 In[82]:=
 In[84]:=
 Out[84]=

Now clean up the created TetGen instances.

 In[85]:=
 Out[85]=

## Engineering Example

To use TetGenLink, it must first be loaded.

 In[86]:=

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[87]:=
 In[88]:=

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

 In[89]:=
 Out[89]=

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

 In[90]:=

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

 In[91]:=
 Out[91]=
 Out[92]=

The tetrahedralization step is then given as follows.

 In[93]:=
 Out[93]=

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

 In[94]:=

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

 In[96]:=
 Out[96]=

You extract the tetrahedron elements with the TetGenGetElements command.

 In[97]:=

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

 In[98]:=

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

 In[99]:=
 Out[99]=

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

 In[100]:=
 In[101]:=
 Out[101]=

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

 In[102]:=
 Out[102]=

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[103]:=
 Out[103]=

The list of distances is then rescaled.

 In[104]:=

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

 In[105]:=
 Out[105]=

To compute the surface, you write a compiled function.

 In[106]:=

The total surface of the object can then be calculated.

 In[107]:=
 Out[107]=

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[108]:=

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[109]:=
 In[111]:=

You can then display the intersecting facets.

 In[112]:=
 Out[112]=