private static void FixMesh()

in GraphLayout/MSAGL/GraphmapsWithMesh/MeshCreator.cs [1246:1353]


        private static void FixMesh(Tiling g)
        {
            for (int i = 0; i < g.NumOfnodes; i++)
            {
                if (g.DegList[i] == 0) continue;
                for (int j = g.N; j < g.NumOfnodes; j++)
                {
                    if (i == j) continue;
                    if (g.VList[i].XLoc == g.VList[j].XLoc && g.VList[i].YLoc == g.VList[j].YLoc)
                    {
                        for (int neighborIndex = 0; neighborIndex < g.DegList[j]; neighborIndex++)
                        {
                            if (g.DegList[g.EList[j, neighborIndex].NodeId] == 0) continue;
                            g.AddEdge(i, g.EList[j, neighborIndex].NodeId);
                        }
                        g.DegList[j] = 0;
                        g.VList[j].Invalid = true;
                    }
                }
            }


            for (int nodeIndex1 = 0; nodeIndex1 < g.N; nodeIndex1++)
            {
                foreach (LineSegment ls1 in g.VList[nodeIndex1].SegmentList.Keys)
                {
                    for (int nodeIndex2 = nodeIndex1 + 1; nodeIndex2 < g.N; nodeIndex2++)
                    {
                        foreach (LineSegment ls2 in g.VList[nodeIndex2].SegmentList.Keys)
                        {
                            if (ls1.Start.Equals(ls2.End) && ls2.Start.Equals(ls1.End))
                            {
                                List<Core.Geometry.Point> list = new List<Core.Geometry.Point>();
                                for (int index = 0; index < g.N; index++)
                                {
                                    Core.Geometry.Point p = new Core.Geometry.Point(g.VList[index].XLoc, g.VList[index].YLoc);
                                    if (MsaglUtilities.PointIsOnAxisAlignedSegment(ls1, p) ||
                                        MsaglUtilities.PointIsOnAxisAlignedSegment(ls2, p))
                                        list.Add(p);
                                }
                                if (list.Count > 2)
                                {
                                    list.Sort();
                                    Core.Geometry.Point[] points = list.ToArray();

                                    for (int i = 0; i < points.Length; i++)
                                        for (int j = i + 1; j < points.Length; j++)
                                            g.RemoveEdge(g.GetNode((int)points[i].X, (int)points[i].Y),
                                                g.GetNode((int)points[j].X, (int)points[j].Y));

                                    for (int i = 0; i < points.Length - 1; i++)
                                    {
                                        g.AddEdge(g.GetNode((int)points[i].X, (int)points[i].Y),
                                            g.GetNode((int)points[i + 1].X, (int)points[i + 1].Y));
                                    }
                                }
                            }
                        }
                    }
                }
            }


            for (int nodeIndex = 0; nodeIndex < g.NumOfnodes; nodeIndex++)
                g.nodeTree.Add(new Rectangle(new Core.Geometry.Point(g.VList[nodeIndex].XLoc, g.VList[nodeIndex].YLoc)),
                    nodeIndex);


            bool searchNew = true;
            while (searchNew)
            {
                searchNew = false;
                for (int nodeIndex1 = 0; nodeIndex1 < g.NumOfnodes; nodeIndex1++)
                {
                    for (int neighborIndex = 0; neighborIndex < g.DegList[nodeIndex1]; neighborIndex++)
                    {
                        int neighborId = g.EList[nodeIndex1, neighborIndex].NodeId;

                        Core.Geometry.Point a = new Core.Geometry.Point(g.VList[nodeIndex1].XLoc, g.VList[nodeIndex1].YLoc);
                        Core.Geometry.Point b = new Core.Geometry.Point(g.VList[neighborId].XLoc, g.VList[neighborId].YLoc);

                        int[] intersectedVertices = g.nodeTree.GetAllIntersecting(new Rectangle(a, b));

                        //check if there is any other node on this edge
                        for (int nodeIndex2 = 0; nodeIndex2 < intersectedVertices.Length; nodeIndex2++)
                        {
                            int currentVertexId = intersectedVertices[nodeIndex2];
                            if (g.VList[currentVertexId].Invalid) continue;
                            if (currentVertexId == nodeIndex1 || currentVertexId == neighborId) continue;

                            Core.Geometry.Point p = new Point(g.VList[currentVertexId].XLoc, g.VList[currentVertexId].YLoc);
                            LineSegment ls = new LineSegment(a, b);

                            if (MsaglUtilities.PointIsOnSegment(ls, p))
                            {
                                g.RemoveEdge(nodeIndex1, neighborId);
                                g.AddEdge(nodeIndex1, currentVertexId);
                                g.AddEdge(currentVertexId, neighborId);
                                searchNew = true;
                                break;
                            }
                        }
                        if (searchNew) break;
                    }
                    if (searchNew) break;
                }
            }
        }