/// <summary> /// Get an image of a graph described by a Delaunay tree /// </summary> /// <param name="tree"></param> /// <returns></returns> private Texture2D GetDebugImage(DelaunayTree.DelaunayTree <T> tree) { Texture2D texture = new Texture2D(1024, 1024); List <EuclideanGraphTriangle <T> > triangles = tree.GetCurrentTriangles(); foreach (EuclideanGraphTriangle <T> triangle in triangles) { EuclideanGraphNode <T>[] corners = triangle.GetCorners(); foreach (EuclideanGraphNode <T> corner in corners) { Vector2 position = corner.Position * (1024 / (4 * Radius)) + new Vector2(1024 / 2, 1024 / 2); Drawing.DrawCircle(texture, position, 10, Color.red); } Queue <EuclideanGraphNode <T>[]> edges = triangle.GetEdges(); foreach (EuclideanGraphNode <T>[] edge in edges) { Vector2 position1 = edge[0].Position * (1024 / (4 * Radius)) + new Vector2(1024 / 2, 1024 / 2); Vector2 position2 = edge[1].Position * (1024 / (4 * Radius)) + new Vector2(1024 / 2, 1024 / 2); Drawing.DrawLine(texture, position1, position2, Color.black); } } return(texture); }
/// <summary> /// Attempt to generate the Delaunay triangulation for this graph. /// Returns null on success. Otherwise returns the node which could not /// be traingulated. /// </summary> /// <returns></returns> private EuclideanGraphNode <T> AttemptDelaunayTriangulation() { // Create convex hull which spans the data EuclideanGraphNode <T> anchor1 = new EuclideanGraphNode <T>(new Vector2(0f, 2f * Radius)); EuclideanGraphNode <T> anchor2 = new EuclideanGraphNode <T>(new Vector2(Mathf.Sqrt(3f) * Radius, -Radius)); EuclideanGraphNode <T> anchor3 = new EuclideanGraphNode <T>(new Vector2(-Mathf.Sqrt(3f) * Radius, -Radius)); DelaunayTree.DelaunayTree <T> tree = new DelaunayTree.DelaunayTree <T>(new EuclideanGraphTriangle <T>(anchor1, anchor2, anchor3)); int nodesInserted = 0; // Insert one node at a time, always updating the delaunay triangulation foreach (EuclideanGraphNode <T> node in Nodes) { ProgressTracker.Instance.PushActivity("Inserting node " + (nodesInserted++).ToString() + "/" + Nodes.Count.ToString()); EuclideanGraphTriangle <T> entryTriangle = tree.GetTriangle(node.GetPosition()); if (entryTriangle == null) { ProgressTracker.Instance.PopActivity(); return(node); } EuclideanGraphTriangle <T>[] subdivisions = entryTriangle.Subdivide(node); tree.Subdivide(entryTriangle, subdivisions); Queue <EuclideanGraphNode <T>[]> edgesToVerify = entryTriangle.GetEdges(); while (edgesToVerify.Count > 0) { VerifyNextEdge(tree, edgesToVerify); } ProgressTracker.Instance.PopActivity(); } ProgressTracker.Instance.PushActivity("Gathering edges"); // Transfer to edge data structure List <EuclideanGraphTriangle <T> > triangles = tree.GetCurrentTriangles(); foreach (EuclideanGraphTriangle <T> triangle in triangles) { Queue <EuclideanGraphNode <T>[]> edges = triangle.GetEdges(); foreach (EuclideanGraphNode <T>[] edge in edges) { if (edge[0].HasData && edge[1].HasData) { AddEdge(edge[0], edge[1]); } } } ProgressTracker.Instance.PopActivity(); return(null); }