where is the sub "mesh.Triangulate(geometry)"

Jul 1, 2014 at 10:42 PM
Edited Jul 2, 2014 at 12:07 AM
Thanks for all of your effort. I was using the beta 3 version before, with inputGeometry and mesh.Triangulate(geometry) to generate the mesh. For some reason, there was an endless loop in the model when I used maxArea to limit the size of the triangles.

Right now I just updated to the new beta 4 version, I understand that I need to use Ipolygon instead of inputGeometry.

Firstly the geometry is from an input file, can you give a similar example as version beta 3 to create a polygon?

// Create InputGeometry using overloaded constructor which takes the number
// of input points as parameter.
var geometry = new InputGeometry(8);

// Add the points.
geometry.AddPoint(0.0, 0.0); // Index 0
geometry.AddPoint(3.0, 0.0); // Index 1 etc.
geometry.AddPoint(3.0, 3.0);
geometry.AddPoint(0.0, 3.0, 1); // Add a marker.
geometry.AddPoint(1.0, 1.0);
geometry.AddPoint(2.0, 1.0);
geometry.AddPoint(2.0, 2.0);
geometry.AddPoint(1.0, 2.0);

// Add the segments. Notice the zero based indexing (in
// contrast to Triangle's file format).
geometry.AddSegment(0, 1);
geometry.AddSegment(1, 2);
geometry.AddSegment(2, 3, 5); // Add a marker.
geometry.AddSegment(3, 0, 5);
geometry.AddSegment(4, 5);
geometry.AddSegment(5, 6);
geometry.AddSegment(6, 7);
geometry.AddSegment(7, 4);

// Add the hole.
geometry.AddHole(1.5, 1.5);

secondly, I just can not figure out where is the sub to generate the mesh. The testApp has this code to generate the mesh as

mesh = (Mesh)input.Triangulate(options, quality)

it is a c++ code and I am using vb.net. Can you show me how to use created polygon in vb.net to create a mesh?

Another question, it seems by default, the size of the resulting mesh is coarse in the center and fine near the boundary, any idea that I can automatically generate a roughly uniform mesh?

Many thanks.

Victor
Coordinator
Jul 2, 2014 at 9:02 AM
Edited Jul 2, 2014 at 2:17 PM
Have a look at the From Beta 3 To 4 page. The docs will be updated when Beta 4 is released.

EDIT: regarding the endless loop: try reducing the minimum angle or don't set it at all. Instead use the smoother to improve mesh quality.

EDIT2: regarding VB.NET: just use a code converter like http://converter.telerik.com/

Here's the code, using Beta 4:
public static void Test()
{
    // Create a Polygon.
    var poly = new Polygon(8);

    // Add the points.
    poly.Add(new Vertex(0.0, 0.0)); // Index 0
    poly.Add(new Vertex(3.0, 0.0)); // Index 1 etc.
    poly.Add(new Vertex(3.0, 3.0));
    poly.Add(new Vertex(0.0, 3.0, 1)); // Add a marker.
    poly.Add(new Vertex(1.0, 1.0));
    poly.Add(new Vertex(2.0, 1.0));
    poly.Add(new Vertex(2.0, 2.0));
    poly.Add(new Vertex(1.0, 2.0));

    // Add the segments. Notice the zero based indexing (in
    // contrast to Triangle's file format).
    poly.Add(new Edge(0, 1));
    poly.Add(new Edge(1, 2));
    poly.Add(new Edge(2, 3, 5)); // Add a marker.
    poly.Add(new Edge(3, 0, 5));
    poly.Add(new Edge(4, 5));
    poly.Add(new Edge(5, 6));
    poly.Add(new Edge(6, 7));
    poly.Add(new Edge(7, 4));

    // Add the hole.
    poly.Holes.Add(new Point(1.5, 1.5));

    // Setting only a MinimumAngle constraint will produce coarse meshes by default. To get
    // fine meshes, add a MaximumArea constraint. Make sure to use a reasonable value here.

    var quality = new QualityOptions()
    {
        MinimumAngle = 25,
        MaximumArea = 0.5
    };

    // Triangulate the polygon.
    var mesh = poly.Triangulate(quality);
    
    var smoother = new TriangleNet.Smoothing.SimpleSmoother();

    // Optional: do some smoothing.
    smoother.Smooth(mesh);

    foreach (var t in mesh.Triangles)
    {
        // Do something with the mesh ...
    }
}
Jul 2, 2014 at 4:17 PM
Thanks for your quick reply. I have an error at poly.triangulate(quality) saying that triangulate is not a member of TriangleNet.Geometry.polygon.

I checked class polygon and can not find "triangulate". Do I miss anything there? Or is there another "polygon" defined in another place?

Imports T = TriangleNet
Imports TG = TriangleNet.Geometry
Imports TR = TriangleNet.Rendering
Imports TM = TriangleNet.Meshing
Private Sub triangle()
    Dim poly As TG.Polygon = New TG.Polygon(60)
    Dim vertex As TG.Vertex
    Dim edge As TG.Edge
    Dim mesh As T.Mesh = New T.Mesh
    Dim quality As TM.QualityOptions = New TM.QualityOptions
    Dim options As TM.ConstraintOptions = New TM.ConstraintOptions
    Dim polyline As csPolyline = New csPolyline  ' this is my only class
    Dim i As Integer
    Dim maxEdgeLength As Double = -0.1, edgeLength As Double, maxArea As Double
    If myFeature.polyline.Length > 0 Then polyline = myFeature.polyline(0)
    For i = 0 To polyline.NumPoints - 2
        edgeLength = Math.Sqrt((polyline.x(i + 1) - polyline.x(i)) ^ 2 + (polyline.y(i + 1) - polyline.y(i)) ^ 2)
        If maxEdgeLength < edgeLength Then maxEdgeLength = edgeLength
    Next
    maxArea = maxEdgeLength * maxEdgeLength * 0.5 * Math.Sqrt(3.0) / 2.0


    For i = 0 To polyline.NumPoints - 1
        'geometry.AddPoint(polyline.x(i), polyline.y(i))
        vertex = New TG.Vertex(polyline.x(i), polyline.y(i))
        poly.Add(vertex)
        'geometry.Points.Add(vertex)
    Next

    For i = 0 To polyline.NumPoints - 1
        If i < polyline.NumPoints - 1 Then
            'geometry.AddSegment(i, i + 1)
            edge = New TG.Edge(i, i + 1)
        Else
            'geometry.AddSegment(i, 0)
            edge = New TG.Edge(i, i + 0)
        End If
        poly.Add(edge)
    Next
    'geometry.AddPoint(1511176.0, 1429892.0)
    'geometry.AddPoint(1510914.0, 1429535.0)


    quality.MinimumAngle = 25.0
    quality.MaximumAngle = 90.0
    'quality.MaximumArea = maxArea

    Try 'Open file and trap any errors using handler
        'mesh.Triangulate(geometry)
        mesh = poly.triangulate(quality)             ' that is where the error is during compile "triangulate is not a member of TriangleNet.Geometry.polygon"
        'mesh.Behavior.MaxArea = maxArea
        'mesh.Refine()
    Catch ex As Exception
        MessageBox.Show("Error: mesh:Triangulate")
    Finally
    End Try
   ' where add smooth here



    MessageBox.Show("mesh done")
    'For Each element In mesh.Triangles
    'polyline = New csPolyline
    'polyline.addPoint(element.GetVertex(0).X, element.GetVertex(0).Y)
    'polyline.addPoint(element.GetVertex(1).X, element.GetVertex(1).Y)
    'polyline.addPoint(element.GetVertex(2).X, element.GetVertex(2).Y)
    'polyline.addPoint(element.GetVertex(0).X, element.GetVertex(0).Y)
    'csFeature.drawPolyline(polyline, myPenSelected, mySymbolSize)
    'Next
End Sub
Coordinator
Jul 2, 2014 at 5:17 PM
Edited Jul 2, 2014 at 5:25 PM
Right. Triangulate is actually an extension method on the IPolygon interface, which is available if you import the TriangleNet.Geometry namespace.

You might want to use the GenericMesher (in the TriangleNet.Meshing namespace) directly:
var mesher = new GenericMesher();
var mesh = mesher.Triangulate(poly);
EDIT: be careful with setting large minimum or small maximum angles (like quality.MaximumAngle = 90.0) AND maximum area. If mesh creation doesn't converge, relax the constraints.
Jul 2, 2014 at 6:11 PM
Edited Jul 2, 2014 at 6:43 PM
Great, It works now. Thanks so much, WO.