Exemplo n.º 1
0
        public DTMesh PolygonToMesh(DTPolygon subject)
        {
            // Mark any unmarked holes in the contour, otherwise Triangle.NET won't handle them properly
            subject = DTUtility.IdentifyHoles(subject);
            if (subject.Contour.Count < 3)
            {
                return(new DTMesh());
            }
            // Don't triangulate if this is already a triangle
            else if (subject.Contour.Count == 3 && subject.Holes.Count == 0)
            {
                return(new DTMesh(subject.Contour, new List <List <int> >()
                {
                    new List <int>()
                    {
                        0, 1, 2
                    }
                }));
            }
            // If the polygon is convex, use simple fanning technique to triangulate
            else if (subject.IsConvex())
            {
                DTMesh mesh = new DTMesh(subject.Contour, new List <List <int> >());
                for (int i = 1; i < subject.Contour.Count - 1; i++)
                {
                    mesh.Partitions.Add(new List <int>()
                    {
                        0, i, i + 1
                    });
                }
                return(mesh);
            }

            // Format polygon input and execute
            Polygon polygon = new Polygon();

            polygon.Add(new Contour(subject.Contour.ToVertexList()), false);
            foreach (var hole in subject.Holes)
            {
                if (hole.Count >= 3)
                {
                    try {
                        polygon.Add(new Contour(hole.ToVertexList()), true);
                    }
                    catch (Exception) { }
                }
            }
            DTProfilerMarkers.TriangleNet.Begin();
            IMesh triangleNetOutput = polygon.Triangulate();

            ++callCount;
            DTProfilerMarkers.TriangleNet.End();

            // Convert Triangle.NET output into DTMesh
            List <Vector2>     vertices  = triangleNetOutput.Vertices.ToVector2List();
            List <List <int> > triangles = triangleNetOutput.Triangles.ToPartitionList();

            return(new DTMesh(vertices, triangles));
        }
Exemplo n.º 2
0
        public DTConvexPolygroup PolygonToTriangleList(DTPolygon subject)
        {
            // Mark any unmarked holes in the contour, otherwise Triangle.NET won't handle them properly
            subject = DTUtility.IdentifyHoles(subject);
            if (subject.Contour.Count < 3)
            {
                return(new DTConvexPolygroup());
            }
            // Don't triangulate if this is already a triangle
            else if (subject.Contour.Count == 3 && subject.Holes.Count == 0)
            {
                return(new DTConvexPolygroup(new List <DTPolygon>()
                {
                    subject
                }));
            }

            // Format polygon input and execute
            Polygon polygon = new Polygon();

            polygon.Add(new Contour(subject.Contour.ToVertexList()), false);
            foreach (var hole in subject.Holes)
            {
                if (hole.Count >= 3)
                {
                    try {
                        polygon.Add(new Contour(hole.ToVertexList()), true);
                    }
                    catch (Exception) {}
                }
            }
            DTProfilerMarkers.TriangleNet.Begin();
            IMesh triangleNetOutput = polygon.Triangulate();

            ++callCount;
            DTProfilerMarkers.TriangleNet.End();

            // Convert Triangle.NET output into poly group
            return(new DTConvexPolygroup(triangleNetOutput.Triangles.Select(t => t.ToVertexList()).ToList()));
        }