mach/libs/trimesh2d
Stephen Gutekanst 9df6448109 trimesh2d: add library for simple polygon triangulation in linear time
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-10-18 13:42:38 -07:00
..
.github trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
src trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
.gitattributes trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
.gitignore trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
build.zig trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
LICENSE trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
LICENSE-APACHE trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
LICENSE-MIT trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00
README.md trimesh2d: add library for simple polygon triangulation in linear time 2022-10-18 13:42:38 -07:00

mach/trimesh2d - simple polygon triangulation in linear time

Converts 'simple' polygons (i.e. no holes) into triangle meshes in linear time using a modern earcut algorithm that works in linear time and has proven correctness.

This is a Zig implementation of the paper:

"Deterministic Linear Time Constrained Triangulation using Simplified Earcut" - Marco Livesu, Gianmarco Cherchi, Riccardo Scateni, Marco Attene, 2020. IEEE Transactions on Visualization and Computer Graphics, 2021. arXiv:2009.04294

(This repository is a separate copy of the same library in the main Mach repository, and is automatically kept in sync, so that anyone can use this library in their own project if they like!)

Getting started

Adding dependency

In a libs subdirectory of the root of your project:

git clone https://github.com/hexops/mach-trimesh2d

Then in your build.zig add:

...
const trimesh2d = @import("libs/mach-trimesh2d/build.zig");

pub fn build(b: *Builder) void {
    ...
    exe.addPackage(trimesh2d.pkg);
}

Usage

const trimesh2d = @import("trimesh2d");

pub fn main() {
    const allocator = std.heap.page_allocator;

    var polygon = std.ArrayListUnmanaged(f32){};
    // append your polygon vertices:
    // try polygon.append(1.0);

    var out_triangles = std.ArrayListUnmanaged(u32){};
    var processor = trimesh2d.Processor(f32){};
    defer processor.deinit(allocator);

    // Process a polygon.
    try processor.process(allocator, polygon, &out_triangles);

    // out_triangles has indices into polygon.items of our triangle vertices.
    // If desired, call .reset() and call .process() again! Internal buffers will be reused.
}

Join the community

Join the Mach community on Discord or Matrix to discuss this project, ask questions, get help, etc.

Issues

Issues are tracked in the main Mach repository.

Contributing

Contributions are very welcome. Pull requests must be sent to the main repository to avoid some complex merge conflicts we'd get by accepting contributions in both repositories. Once the changes are merged there, they'll get sync'd to this repository automatically.