Trying to triangulate a simple polygon with holes

469 views
Skip to first unread message

Carlos Eduardo Alvarado Pedreros

unread,
May 15, 2013, 5:58:57 PM5/15/13
to poly...@googlegroups.com
Hello Poly2Tri community

I'm trying to triangulate a simple polygon that has a hole inside of it, but the results are not the expected.



But when I triangulate it I'm getting the following output


I'm using the following code:


List<TriangulationPoint> puntosContenedor = new List<TriangulationPoint>();

                        ConstrainedPointSet cps = null;

                        if (poligono is DotSpatial.Topology.Polygon)

                        {

                            DotSpatial.Topology.Polygon poly = poligono as DotSpatial.Topology.Polygon;

                            foreach (Coordinate c in poly.Shell.Coordinates)

                                puntosContenedor.Add(new TriangulationPoint(c.X, c.Y));

                            puntosContenedor.Add(puntosContenedor.First());

                            cps = new ConstrainedPointSet(puntosContenedor);



                            if ((poligono as DotSpatial.Topology.Polygon).NumHoles > 0)

                            {

                                foreach (ILinearRing hole in (poligono as DotSpatial.Topology.Polygon).Holes)

                                {

                                    List<TriangulationPoint> holeForTri = new List<TriangulationPoint>();

                                    foreach (Coordinate c in hole.Coordinates)

                                    {

                                        holeForTri.Add(new TriangulationPoint(c.X, c.Y));

                                    }

                                    cps.AddHole(holeForTri, "hole_" + contHoles.ToString());

                                    contHoles++;

                                }

                            }

                        }

                        else if(poligono is DotSpatial.Topology.LinearRing)

                        {

                            foreach (Coordinate c in poligono.Coordinates)

                                puntosContenedor.Add(new TriangulationPoint(c.X, c.Y));

                            puntosContenedor.Add(puntosContenedor.First());

                            cps = new ConstrainedPointSet(puntosContenedor);

                        }



                        if (cps != null)

                        {

                            P2T.Triangulate(cps);

                            foreach (DelaunayTriangle triangulo in cps.Triangles)

                            {                                

                                List<Polyline3dVertex> verticesTri = new List<Polyline3dVertex>();

                                bool dibujar = true;

                                foreach (TriangulationPoint puntoTri in triangulo.Points)

                                {

                                   bool isContrained = triangulo.GetConstrainedEdgeCCW(puntoTri);

                                   if (isContrained)

                                       dibujar = false;

                                   verticesTri.Add(new Polyline3dVertex((float)puntoTri.X, (float)puntoTri.Y, 6000));

                                }

                                if (dibujar)

                                {

                                    Polyline3d polyTri = new Polyline3d(verticesTri, true);

                                    polyTri.Layer = new netDxf.Tables.Layer("TRIANGULACION");

                                    polyTri.Color = AciColor.Green;

                                    dxf.AddEntity(polyTri);

                                }

                            }

                        }


Thanks in advance for the help.



obi

unread,
May 15, 2013, 6:22:51 PM5/15/13
to poly...@googlegroups.com
Didn't know the C# version had been altered so much from the Java version which it is a port of.

Clearly the one who did the port or altered ConstrainedPointSet did not understand the usage of this class.
A cps should only be used when you have a bunch of points to triangulate but also enforce one or more edges in the  triangulation.

AddHole isn't even a valid concept for a ConstrainedPointSet. A bit sad to see the C# version looking like this.

What you want to do is:
1. Use the class Polygon instead of  ConstrainedPointSet. 
2. Remove puntosContenedor.Add(puntosContenedor.First());. You can never add same point twice to the list of polygon points. If you add a list of point when creating the polygon it is assumed that last and first point in the list will form an edge.

Do this changes and your output should hopefully look good.

-Thomas


Carlos Eduardo Alvarado Pedreros

unread,
May 15, 2013, 6:42:40 PM5/15/13
to poly...@googlegroups.com

Thanks Obi, I implemented your solution and it worked great, I owe you a Beer man :)

obi

unread,
May 15, 2013, 6:47:04 PM5/15/13
to poly...@googlegroups.com
Great :-)
Reply all
Reply to author
Forward
0 new messages