views:

3883

answers:

8

I am looking for an algorithm or library (better) to break down a polygon into triangles. I will be using these triangles in a Direct3D application. What are the best available options?

Here is what I have found so far:

  1. Ben Discoe's notes
  2. FIST: Fast Industrial-Strength Triangulation of Polygons
  3. I know that CGAL provides triangulation but am not sure if it supports holes.

I would really appreciate some opinions from people with prior experience in this area.

Edit: This is a 2D polygon.

A: 

CGAL definitly does not support holes, it always triangulates the complex hull of the polygon (complete triangulation)

You can always decompose them into a set of convex polygons and triangulate them. (Depending on how you get/create your polygons it might be an easy or hard task)

DR
How do I decompose them into a set of convex polygons? Did you mean manually?
Vulcan Eager
CGAL triangulates the convex hull, but it DEFINITELY DOES support internal constraints (such as the boundaries of holes).
Camille
+1  A: 

You can add the holes relatively easily yourself. Basically triangulate to the convex hull of the input points, as per CGAL, and then delete any triangle whose incentre lies inside any of the hole polygons (or outside any of the external boundaries). When dealing with lots of holes in a large dataset, masking techniques may be used to significantly speed this process up.

edit: A common extension to this technique is to weed weak triangles on the hull, where the longest edge or smallest internal angle exceeds a given value. This will form a better concave hull.

Shane MacLaughlin
This approach would not work: you need to use a constrained triangulation, otherwise, you may encounter triangles that are partially inside and partially outside a hole.
Camille
@camille - a triangulated polygon with holes is always constrained. The polygon edges and holes are, by definition, contraints. If a triangle edge crossed a hole, the hole would be partially covered. If it crossed a polygon edge, the TIN would not be a triangulation of that polygon.
Shane MacLaughlin
+2  A: 

CGAL has the tool you need: Constrained Triangulations

You can simply provide boundaries of your polygon (incuding the boundaries of the holes) as constraints (the best would be that you insert all vertices, and then specify the constraints as pairs of Vertex_handles).

You can then tag the triangles of the triangulation by any traversal algorithm: start with a triangle incident to the infinite vertex and tag it as being outside, and each time you cross a constraint, switch to the opposite tag (inside if you were previously tagging the triangles as outsider, outside if you were tagging triangles as insider before).

Camille
Its a good enough solution for simple cases. Where you have overlapping holes, and holes within holes, it falls over. I prefer to have explicit internal and external boundaries.
Shane MacLaughlin
In case you have overlapping holes, you should indeed retain the list of holes you have already entered into (instead of just an inside/outside tag). Apart from that, it is exactly the same.
Camille
"each time you cross a constraint"? How do I figure that out?
Vulcan Eager
Do I take each triangle and test it with every available constraint? Is there a quicker way to do this?
Vulcan Eager
+5  A: 

To give you some more choices of libraries out there:

Polyboolean. I never tried this one, but it looks promising: http://www.complex-a5.ru/polyboolean/index.html

General Polygon Clipper. This one works very well in practice and does triangulation as well as clipping and holes holes: http://www.cs.man.ac.uk/~toby/alan/software/

My personal recommendation: Use the tesselation from the GLU (OpenGL Utility Library). The code is rock solid, faster than GPC and generates less triangles. You don't need an initialized OpenGL-Handle or anything like this to use the lib.

If you don't like the idea to include OpenGL system libs in a DirectX application there is a solution as well: Just download the SGI OpenGL reference implementation code and lift the triangulator from it. It just uses the OpenGL-Typedef names and a hand full of enums. That's it. You can extract the code and make a stand alone lib in an hour or two.


In general my advice would be to use something that alreay works and don't start to write your own triangulation.

It is tempting to roll your own if you have read about the ear-clipping or sweep-line algorithm, but fact is that computational geometry algorithms are incredible hard to write in a way that they work stable, never crash and always return a meaningful result. Numerical roundoff errors will accumulate and kill you in the end.

I wrote a triangulation algorithm in C for the company I work with. Getting the core algorithm working took two days. Getting it working with all kinds of degenerated inputs took another two years (I wasn't working fulltime on it, but trust me - I spent more time on it than I should have).

Nils Pipenbrinck
Wrote all my own TIN stuff as well, and agree 100% about the many degenerate cases. Wouldn't ever move from my own libs for this reason, Some of the newer CG books out there are excellent though.
Shane MacLaughlin
+1  A: 

This is a common problem in finite element analysis. It's called "automatic mesh generation". Google found this site with links to commercial and open source software. They usually presume some kind of CAD representation of the geometry to start.

duffymo
+2  A: 

Jonathan Shewchuk's Triangle library is phenomenal; I've used it for automating triangulation in the past. You can ask it to attempt to avoid small/narrow triangles, etc., so you come up with "good" triangulations instead of just any triangulation.

zweiterlinde
I can vouch that Triangle is indeed a great tool. It also won the prestigious "J. H. Wilkinson Prize for Numerical Software," given out only once every 4 years.
batty
Changing the selected answer to this one since I have actually got this to work.
Vulcan Eager
One of the biggest advantages here is that Triangle makes it very easy to construct separate vertex and index buffers of the triangulation. Love it!
Vulcan Eager
A: 

Another option (with a very flexible license) is to port the algorithm from VTK:

vtkDelaunay2D

This algorithm works fairly well. Using it directly is possible, but requires links to VTK, which may have more overhead than you want (although it has many other nice features, as well).

It supports constraints (holes/boundaries/etc), as well as triangulating a surface that isn't necessarily in the XY plane. It also supports some features I haven't seen elsewhere (see the notes on Alpha values).

Reed Copsey
A: 

I just tried the Triangle library with great success. I only had one problem when triangulation my polygon, and it's that the triangle indices created were in clockwise order. Any Triangle user out there that can tell (or give some pointer) why it's so?

esak