Home

Awesome

Robust Triangulation with Louvre 🌙

Crates.io docs.rs

▶️ Live demo page

Louvre is a robust triangulation algorithm, which can handle self-intersecting polygons' triangulation.

What is triangulation? "Polygon triangulation" makes a polygon's vertex coordinates array into a set of coordinates of triangles, whose areas' sum equals to the polygon's.

As most of computational graphic processing systems (like opengl/webgl) handle polygons by decomposing them into triangles, a good and fast triangulation algorithm is crucial.

Earcut(mapbox/earcut.js) is one of the most widely used triangulation algorithm. However simple earcut cannot properly decompose self-intersecting polygons.

Louvre widely refered to mapbox/earcut.js to implement basic logics and utilities of simple earcut algorithm. Making further contribution, louvre can handle self-intersecting polygons, which is not viable in most open source algorithms including mapbox/earcut.js.

See the live demo of louvre. Try drawing some complex polygons, with self-intersecting lines.

Surely, louvre is FAST and ROBUST. You can use this in rust native or rust supporting wasm environment.

Ex

use louvre::triangulate;

let mut data: Vec<f64> = vec![
  [0., 0.], [0., 3.], [3., 0.], [3., 4.], [-1., 0.]
].concat();

let (new_data, indices) = triangulate(&mut data, 2);

assert_eq!(new_data, 
  vec![
    3.0, 0.0,  3.0, 4.0,  1.0, 2.0, 
    0.0, 0.0,  0.0, 1.0,  -1.0, 0.0,
    0.0, 1.0,  1.0, 2.0,  0.0, 3.0
  ]
);
assert_eq!(indices, vec![
  1, 2, 0, 
  4, 5, 3, 
  7, 8, 6
]);
</br> Belows are visual examples of triangulating polygons on html canvas using rust's wasm.

fig1. Parsing .json file: Triangulated polygon is portrayed on the right box. fig1

fig2. Triangulating random (self-intersecting) polygons:
fig2

Performance

Average performance time required for triangulate processing per item (in milliseconds).

rustwasm
hilbert7.2221.47
water27.2426.48
inter10.0.09
inter20.0.13
inter30.9.07
inter40.0.13

Unsafe linked list

There are lots of unsafe codes and raw pointers inside of this crate. They are used to implement linked list. For linked list in rust, using raw pointers is possibly the best (and fasted) option among others. The unsafe codes went through rust's miri test and they are designed to be safe.

more?

Handling 3d coordinates and complex polygons with holes inside is not implemented yet.

The original goal of this project was to use Rust to cover basic compuational geometry problems. However at this moment further expansion is not tightly scheduled.

Logs