Calling TetGen from Another Program

In addition to being used as a stand alone program, TetGen can be called from another program. The TetGen library provides functions and data structures for calling TetGen. One of the advantages of using the TetGen library is that it can be repeatedly called by other programs without the overhead of reading and writing files. The feature is very useful for applications like adaptive FEM and FVM methods.

This page gives the necessary instructions for using the TetGen library. Users are supposed to be able to use TetGen, i.e., know its command line switches and the input and output file formats.

The Header File

All programs calling TetGen must include the header file "tetgen.h", that is to add the following line into your C++ source file at where you want to call TetGen.

#include "tetgen.h"

The header file includes all data types and function declarations of the TetGen library. More specifically, it defines the function "tetrahedralize()" and the data type "tetgenio", which are provided for users to call TetGen with all its functionalities.

The Calling Convention

The function "tetrahedralize()" is the sole interface you need to know for calling TetGen in your program. It is declared in tetgen.h as follow:

void tetrahedralize(char *switches, tetgenio *in, tetgenio *out);

The parameter "switches" is a string containing the command line switches for this call. In this string, no initial dash '-' is required. The `Q' (quiet) switch is recommended in the final code.

The parameters "in" and "out", which are two pointers pointing to objects of "tetgenio", describing the input and the output. "in" and "out" may never be null pointers.

An Example

Here gives an example of how to call TetGen from another program by using the "tetgenio" data structure and the function "tetrahedralize()".

The geometry is a bar with two boundary markers defined (picture shown below, source file bar.poly).

The complete C++ source code is available at here: tetcall.cxx. It can be compiled into an executable program. It does the following steps:

In addition, for debugging purpose, this program outputs the PLC created in the object "in" into two files (barin.node and barin.poly), and outputs the mesh saved in the object "out" into three files (barout.node, barout.ele, and barout.face). These files can be visualized by TetView.


Return to TetGen home page.
Hang Si