28

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.

Ashwin Nanjappa
  • 68,458
  • 72
  • 198
  • 283
Agnel Kurian
  • 53,593
  • 39
  • 135
  • 210

9 Answers9

20

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
  • 13,359
  • 2
  • 25
  • 31
  • 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 May 01 '09 at 01:42
  • 1
    Changing the selected answer to this one since I have actually got this to work. – Agnel Kurian Mar 16 '10 at 19:47
  • 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! – Agnel Kurian Mar 17 '10 at 05:29
  • 10
    @agnel-kurian Triangle cannot be used in a commercial application BTW and even meshes generated with it are supposed to include acknowledgements. – Jason Goemaat Jun 27 '12 at 23:08
  • 2
    @Jason, The site says "may not be sold or included in commercial products without a license". So... it might be possible to obtain a license for commercial use. – Agnel Kurian Jun 28 '12 at 02:44
  • Bindings for R exist, too: https://cran.r-project.org/web/packages/triangle/index.html – anonymous Apr 13 '16 at 19:11
19

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
  • 77,289
  • 24
  • 142
  • 216
  • 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. – SmacL Jan 02 '09 at 13:06
  • Not sure about GLU. gluNewTess() apparently segfaults on Linux if you don't have a working GL context, which it should not require, but it calls glGetError so it does. I found this info on the internet so it is not 100%, but the segfault is real (that's exactly why I looked into it). Creating a GL context could be an option (not for me). – szali Feb 25 '20 at 09:13
12

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
  • 1,590
  • 10
  • 11
  • 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. – SmacL Jan 02 '09 at 13:29
  • 1
    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 Jan 03 '09 at 21:34
  • "each time you cross a constraint"? How do I figure that out? – Agnel Kurian Jan 22 '10 at 06:14
  • Do I take each triangle and test it with every available constraint? Is there a quicker way to do this? – Agnel Kurian Jan 22 '10 at 06:15
  • 1
    This is also said in the FAQ of CGAL: http://www.cgal.org/FAQ.html#polygon_triangulation – Ivan Xiao Jun 30 '11 at 04:26
  • 1
    here is actually example http://doc.cgal.org/latest/Triangulation_2/index.html#title29, if some one need it. – Volodymyr B. Oct 02 '15 at 10:35
5

I have found the poly2tri library to be exactly what I needed for triangulation. It produces a much cleaner mesh than other libraries I've tried (including libtess), and it does support holes as well. It's been converted to a bunch of languages. The license is New BSD, so you can use it in any project.

Poly2tri library on Google Code

Guavaman
  • 440
  • 5
  • 12
3

try libtess2

https://code.google.com/p/libtess2/downloads/list

based on the original SGI GLU tesselator (with liberal licensing). Solves some memory management issues around lots of small mallocs.

2

I have implemented a 3D polygon triangulator in C# using the ear clipping method. It is easy to use, supports holes, is numerically robust, and supports aribtrary (not self-intersecting) convex/non-convex polygons.

NMO
  • 558
  • 4
  • 15
2

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.

SmacL
  • 21,621
  • 10
  • 89
  • 143
  • 1
    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 Jan 02 '09 at 12:29
  • @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. – SmacL Jan 02 '09 at 13:03
1

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
  • 293,097
  • 41
  • 348
  • 541
0

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
  • 522,342
  • 70
  • 1,092
  • 1,340