Skip to content

mastroalex/Earcut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Earcut++

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.

Triangulation demo

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.

📦 Structure

  • include/ — Public headers
  • src/ — Implementation files
  • test/ — (Optional) Tests and benchmarks

🛠️ Build

make

Or use your own build system, just include the headers and link the sources.

⚙️ Integration with Wolfram Mathematica

make wolfram WOLFRAM_KERNEL_INCLUDE=<path-to-Mathematica>/Contents/SystemFiles/IncludeFiles/C

Repalce 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

📄 Example Usage

#include "EarClippingCore.h"
#include "Geometry.h"

int main() {
    Polygon poly = {/* your points here */};
    std::vector<Triangle> result = EarClipping::triangulate(poly);
    return 0;
}

or

📈 Computational cost

Easily benchmark it with:

make benchmark
./build/benchmark > benchmark.csv

The Earcut++ library is marked as MineCpp, the same logic implemented in Mathematica is Mine while Cpp referer to poly2tri.

About

A C++ library to triangulate polygons, that actually works.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published