You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-49Lines changed: 40 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,11 @@
1
1
## Earcut
2
2
3
3
A fast, [header-only](https://github.com/mapbox/earcut.hpp/blob/master/include/mapbox/earcut.hpp) C++ port of [earcut.js](https://github.com/mapbox/earcut), the fastest and smallest JavaScript polygon triangulation library.
Earcut favors raw speed and simplicity over triangulation quality, while being robust enough to handle most practical datasets without crashing or producing garbage, with an option to [refine](#refinement-optional-delaunay-post-pass) the result to [Delaunay](https://en.wikipedia.org/wiki/Delaunay_triangulation) quality at a small cost. Originally built for [Mapbox GL](https://www.mapbox.com/), it's a good fit for real-time triangulation of geographical shapes and other practical data.
9
10
10
11
It implements a modified ear slicing algorithm, optimized by [z-order curve](http://en.wikipedia.org/wiki/Z-order_curve) and spatial hashing and extended to handle holes, twisted polygons, degeneracies and self-intersections in a way that doesn't _guarantee_ correctness of triangulation, but attempts to always produce acceptable results for practical data. It's based on ideas from [FIST: Fast Industrial-Strength Triangulation of Polygons](http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html) by Martin Held and [Triangulation by Ear Clipping](http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf) by David Eberly.
@@ -15,62 +16,42 @@ It implements a modified ear slicing algorithm, optimized by [z-order curve](htt
15
16
#include<earcut.hpp>
16
17
```
17
18
```cpp
18
-
// The number type to use for tessellation
19
-
using Coord = double;
19
+
// A point is any type with x/y accessors; std::array works out of the box.
20
+
using Point = std::array<double, 2>;
21
+
22
+
// A polygon is a list of rings. The first ring is the outer boundary; the rest are holes.
23
+
// Winding order doesn't matter, and rings can be given in any order.
// Returns array of indices that refer to the vertices of the input polygon.
37
-
// e.g: the index 6 would refer to {25, 75} in this example.
38
-
// Three subsequent indices form a triangle. Output triangles have a consistent winding order
39
-
// regardless of the input winding: counter-clockwise in a y-up coordinate system (clockwise in
32
+
// Triangulate. The result is a flat list of indices into the input vertices (numbered ring after
33
+
// ring, so index 6 is {25, 75} here), three per triangle. Output triangles have a consistent
34
+
// winding regardless of the input: counter-clockwise in a y-up coordinate system (clockwise in
40
35
// y-down/screen space). Call std::reverse on the result if you need the opposite orientation.
41
36
std::vector<N> indices = mapbox::earcut<N>(polygon);
42
37
```
43
38
44
39
Earcut can triangulate a simple, planar polygon of any winding order including holes. It will even return a robust, acceptable solution for non-simple polygons. Earcut works on a 2D plane: only `x` and `y` are used, so if you have three or more dimensions, project them onto a 2D surface before triangulation, or use a more suitable library for the task (e.g. [CGAL](https://doc.cgal.org/latest/Triangulation_3/index.html)).
45
40
46
-
It is also possible to use your custom point type as input. There are default accessors defined for `std::tuple`, `std::pair`, and `std::array`. For a custom type (like Clipper's `IntPoint` type), do this:
41
+
Any point type works as input — earcut reads coordinates through the `nth` accessor. Accessors for `std::tuple`, `std::pair`, and `std::array` ship by default; for a custom type (like Clipper's `IntPoint`), specialize `nth` for it:
You can also use a custom container type for your polygon. Similar to std::vector<T>, it has to meet the requirements of [Container](https://en.cppreference.com/w/cpp/named_req/Container), in particular `size()`, `empty()` and `operator[]`.
54
+
The polygon and ring containers are just as flexible: any type that meets the [Container](https://en.cppreference.com/w/cpp/named_req/Container) requirements (`size()`, `empty()`, `operator[]`) works in place of `std::vector`.
It assumes a valid manifold triangulation, such as the output of `earcut` (though any manifold triangle-index array works), and reads `coords` through the same `nth<0>`/`nth<1>` accessors. It doesn't repair invalid polygon input or make the mesh conforming. Note also that `refine` uses **non-robust** predicates: float input is fine, and the worst case is a not-quite-Delaunay edge, never an invalid mesh — but unlike `earcut` it does not promise bit-identical output across compilers.
Earcut is heavily optimized for its primary workload — triangulating polygons from
@@ -113,7 +90,7 @@ triangulation even on bad data, use a library like [CGAL](https://www.cgal.org/)
113
90
114
91
The output is also not _conforming_ — a vertex may land in the middle of another triangle's edge (a
115
92
T-junction). This is harmless for rendering but can break navmesh or FEM use; if you need a
116
-
conforming mesh, remove T-junctions in a post-process.
93
+
conforming mesh, [remove T-junctions in a post-process](https://github.com/mapbox/earcut/issues/74#issuecomment-4826113682).
117
94
118
95
## Additional build instructions
119
96
In case you just want to use the earcut triangulation library; copy and include the header file [`<earcut.hpp>`](https://github.com/mapbox/earcut.hpp/blob/master/include/mapbox/earcut.hpp) in your project and follow the steps documented in the section [Usage](#usage).
@@ -151,6 +128,20 @@ Build options (all default to their common value): `-DEARCUT_BUILD_TESTS=ON`,
151
128
CMake can also generate IDE projects (Visual Studio, Xcode, etc.) — e.g.
152
129
`cmake -B build -G "Visual Studio 17 2022"` — or import the folder directly in CLion / VS.
153
130
131
+
### Visualizer
132
+
133
+
There's an interactive OpenGL viewer for inspecting the triangulation of the bundled test fixtures.
134
+
It's off by default (it needs an OpenGL SDK and GLFW); enable and run it with:
0 commit comments