A C++ library to triangulate polygons, that actually works.
This C++ library implements the Ear Clipping algorithm for polygon triangulation. It's designed to be lightweight and performant, able to easly deal with concave polygons.
This library is part of a broader framework focused on polygon splitting and triangulation, designed to support advanced mesh generation for numerical methods. It has been applied in two distinct areas:
- AR-for-beams, a project using augmented reality and structural mechanics to visualize in real time physical beam deformation and stress state.
- xx-VEM, for scientific computing with the Virtual Element Method.
include/— Public headerssrc/— Implementation filestest/— (Optional) Tests and benchmarks
makeOr use your own build system, just include the headers and link the sources.
make wolfram WOLFRAM_KERNEL_INCLUDE=<path-to-Mathematica>/Contents/SystemFiles/IncludeFiles/CRepalce the <path-to-Mathematica> placeholder with your path, for instance on MacOS generally it is: /Applications/Wolfram.app/
Then you need create the library link between build/Triangulate.dylib and Mathematica:
libpath = FileNameJoin[{<path-to-Earcut>, "build/Triangulate.dylib"}];
lib = LibraryFunctionLoad[libpath, "TriangulateEarClipping", {{Real, 2}, {Integer, 1}}, {Integer, 2}]and so you can call it directly with:
pts = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
conn = {1, 2, 3, 4};
tri = lib[pts, conn]See test.nb
#include "EarClippingCore.h"
#include "Geometry.h"
int main() {
Polygon poly = {/* your points here */};
std::vector<Triangle> result = EarClipping::triangulate(poly);
return 0;
}or
Easily benchmark it with:
make benchmark
./build/benchmark > benchmark.csvThe Earcut++ library is marked as
MineCpp, the same logic implemented in Mathematica isMinewhileCppreferer topoly2tri.

