mach/libs/trimesh2d
Stephen Gutekanst 628387764c all: CI: update to latest Zig master version
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
2022-10-22 09:11:16 -07:00
..
.github all: CI: update to latest Zig master version 2022-10-22 09:11:16 -07:00
src trimesh2d: clip ears with smallest triangle area first 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: clip ears with smallest triangle area first 2022-10-18 13:42:38 -07:00

mach/trimesh2d - simple polygon triangulation

Triangulates 'simple' polygons (i.e. no holes) into triangle meshes.

This uses inspiration/ideas from 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

Notably, this paper does not consider lateral ears ("we never consider lateral ears in our triangulation algorithm"); and so we found many polygons that failed to triangulate with it due to extremas. To correct this, we adjusted the algorithm to clip ears with the smallest produced triangle area first.

(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.