georust / geo

Geospatial primitives and algorithms for Rust
https://crates.io/crates/geo
Other
1.54k stars 198 forks source link

Create Triangulate algorithm #568

Closed frewsxcv closed 6 months ago

frewsxcv commented 3 years ago

A new algorithm for triangulating geometries with areas.

pub trait Triangulate {
    fn triangulate(&self) -> Vec<Triangle>;
}

impl Triangulate for Polygon { ... }

impl Triangulate for MultiPolygon { ... }

impl Triangulate for Rect { ... }

impl Triangulate for Triangle { ... }

Thoughts:

michaelkirk commented 3 years ago

I think this would be a good addition!

Some design thoughts:

Should we have a separate trait method for returning triangle indices?

Do you mean vertex indices? Otherwise I don't understand why an individual triangle would ever appear more than once.

I've only recently started doing any graphics programming, but the API I typically want is one which returns vertices and their indices - something like:

pub trait Triangulate {
    // returns `(triangles, vertices)` where each `triangle` is represented 
    // by three indices, corresponding to its corners in `vertices`
    fn triangulate(&self) -> (Vec<(usize, usize, usize)>, Vec<Coordinate>);
}

fn triangulate(&self) -> Vec<Triangle>;

Maybe better?: fn triangle_iter(&self) -> impl Iter<Item=Triangle<T>>

If we have a compressed vertex representation (as above) this might be a slightly smaller and more flexible way to store things. I'm not familiar enough with how the underlying triangulation algorithms work to know if this is actually useful though — if they're just internally building up essentially a fully expanded list of triangles, then this doesn't get us anything meaningful.

frewsxcv commented 6 months ago

this is done:

https://docs.rs/geo/latest/geo/algorithm/triangulate_spade/trait.TriangulateSpade.html

https://docs.rs/geo/latest/geo/algorithm/triangulate_earcut/trait.TriangulateEarcut.html