vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
470 stars 75 forks source link

Triangulate polygons? #18

Closed bertt closed 5 years ago

bertt commented 5 years ago

Can this library triangulate polygons (as list of vertex(x,y,z)) too? Or is there a method like primitive.AddPolygon?

If not, is there a recommended library to do triangulation?

vpenades commented 5 years ago

No, the library only supports points, lines and triangles.

If your polygons are convex, you can just iterate over the vertices; the test library already has that code: ToolkitUtils.cs

For non contex polygons, or polygons with holes etc, more exotic algorythms are required. The most commonly used algorythm I know is the ear clipping algorythm, although most implementations you'll find around only work in 2D Space.

Finding a good, general purpose polygon triangulation algorythm that works in 3D Space is quite difficult, I actually expent some time trying to implement it, but it was taking too much time, and I finally considered it to be beyond the scope of the library.

Let me know if you find something useful, I'm interested too!

bertt commented 5 years ago

ok thanks for clearing up, I'll try first the convex way.

bertt commented 5 years ago

update: I think I'll be using ST_DelaunayTriangles function from PostGIS (https://postgis.net/docs/ST_DelaunayTriangles.html), adds a database dependency but thats no problem in my case.

vpenades commented 5 years ago

That's another reason why I didn't implement a polygon triangulator, there's more than one algorithm, and there's no algorithm that fits all.