Triangle is a C library for generating high-quality triangular meshes. You may download the C code from the Triangle's home website if you want, however it won't work out of the box since you need to set a few preprocessor options. I suggest that you use the modified version that I have provided here instead:
Modified triangle: triangle.h triangle.c
My modifications amount to setting the preprocessor options that make it into a C module rather than a stand-alone program. The triangulation code itself is not touched.
The file triangle.h is the module's interface.
It includes extensive comments on the module's use.
The file triangle.c (which is over 16,000 lines long!)
contains the module's implementation
(and built-in documentation for the stand-alone version).
The code implements a few kludges in the interest of
minimizing memory usage, therefore you will get warnings when you
compile it with gcc's -Wall -pedantic
flags. Nevertheless,
the compiled program seems to work on the machines I have tried.
You may drop the files triangle.h and triangle.c into your project's directory and treat them like any *.c and *.h file of your own. Although that will work, it's not a good idea—mixing triangle.h and triangle.c with your own files is ugly.
I suggest that you put triangle.h and triangle.c in a directory of their own, let's say ../triangle/ relative to your project's directory. Go to that directory and compile Triangle:
$ cc -Wall -pedantic -std=c89 -O2 triangle.c -cThis will create the object file triangle.o. You will see several warnings due to the kludges noted above. Ignore the warnings.
Now go to your project's directory and establish symbolic links to Triangle:
$ ln -s ../triangle/triangle.[ho] .(Don't overlook the space and the period at the end of that command.) This makes the files triangle.h and triangle.o available to your project. Compile and link with the rest of your project's files as usual. This technique should work on any operating system that supports symbolic links. These include Unix, MacOS, and Windows.
Aside:
If you know how to make libraries on your operating system,
you may build a true library out of
triangle.o and install it in a standard place in your compiler's
search path. Then you may link your program with it by specifying the
-ltriangle
flag at the linking stage.
To complete your project, you will need the following files:
mesh-to-eps.h mesh-to-eps.c mesh.cThe significance and uses of these files are described in the book, so I won't elaborate on them here.
The file problem-spec.c
outlined in Listing 232
on page 306 contains the descriptions of four domains that
are used in this chapter to illustrate the various capabilities of
Triangle.
The file mesh-demo.c
described in Section 23.7
provides the driver (function main()
)
that invokes those descriptions and triangulates the four domains.
In retrospect, defining four domains in a single file, and triangulating them all at once, is not such a great idea. It will make better sense to split the monolithic demo into four individual demos.
Consider, therefore, replacing problem-spec.c
with four files
and replacingsquare-spec.c
triangle-with-hole-spec.c
annulus-spec.c
three-holes-spec.c
,
mesh-demo.c
with four individual driverssquare.c
triangle-with-hole.c
annulus.c
three-holes.c
.
That simplifies the coding since the domains may be coded and debugged individually. It also makes it easy to add new domains without disturbing the existing ones.
The book suggests producing graphical representation
of the generated meshes through the supplied files
mesh-to-eps.[ch]
. [See the links above.]
These provide a function mesh_to_eps()
that receives a mesh structure and an output file name,
e.g., square.eps
,
it extracts the triangulation
information from the mesh structure and writes a PostScript
script into the file square.eps
that can be fed into a PostScript
viewer for viewing.
With the wide availability of python in these days,
it may be convenient to use python's
matplotlib
plotting utility instead.
That provides you the additional opportunity of writing your
own equivalent of mesh-to-eps.[ch]
, let's call it
mesh-to-py.[ch]
, which
receives a mesh structure and an output file name.
It extracts the triangulation information from the mesh structure and writes a
python script, say square.py
,
that can be fed
into python for viewing.
Here is a minimal sample of a python plotting script:
import matplotlib.pyplot as plt
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=['black'])
plt.gca().set_aspect('equal')
plt.plot([1,2],[4,7])
plt.plot([4,2],[0,4])
plt.show()
The first three lines and the last line are always the same.
Each of the remaining two lines draws a line segment. The syntax
for drawing a line from the point
(x1,y1)
to the point
(x2,y2)
is
plt.plot([x1,x2],[y1,y2])
You may repeat that line as many times as needed. If the script is written
to a file try-me.py
, you may view the graphics by enteringon the Unix command-line.python try-me.py
Project: Write files mesh-to-py.[ch]
that
provide a function, say
void mesh_to_py(struct mesh *mesh, char *outfile);
that receives a mesh structure, walks through the mesh->edges
array, determines the coordinates of the endpoints of each edge, and writes
a python script in
the outfile
.
for plotting the mesh.
Programming Projects in C |
![]() ![]() |