2.4 KiB
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(allocator, 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.