wo80 / Triangle.NET

C# / .NET version of Jonathan Shewchuk's Triangle mesh generator.
472 stars 85 forks source link

Unable to triangulate concave polygon #47

Closed totenk0pf closed 4 months ago

totenk0pf commented 4 months ago

Hi, I'm trying to triangulate a polygon based on a set of boundary points, but it's giving me a weird looking mesh. Since the resulting mesh has to be concave, i've added both the vertices and a contour but the result is misshapen. Is there something I'm doing wrong here?

var vertices = new List<Vertex>();
var polygon = new Polygon();
foreach (var tile in _edgeTiles)
{
    // Calculate edge point here
    var point = ...
    _edgeVertices.Add(point);
    var vert = new Vertex(point.x, point.z, 1);
    vertices.Add(vert);
}

polygon.Points.AddRange(vertices);
polygon.Add(new Contour(vertices, 1));

// Start triangulation
var options = new ConstraintOptions
{
    ConformingDelaunay = true, Convex = false
};
var mesh = polygon.Triangulate(options);

Thanks in advance.

Here's a visualization of the result (the red circles are the calculated boundary points, the yellow lines are edges from the mesh):

image

wo80 commented 4 months ago

Do not add input vertices twice, add the contour only.

The visualization seems to suggest that your input points are not in order, which they have to be when using the Contour class. If you have an unordered list of segments only, then use those segments like shown here: wiki/Polygon#using-segments

I'd also like to refer you to https://github.com/wo80/Triangle.NET/issues/40#issuecomment-1732079366 for troubleshooting.

totenk0pf commented 4 months ago

Thank you, it turns out my input points weren't in order like you said. Works perfectly now.