//Sorts edges by adding weight to them static List <STEdge> SetWeights(TriangleNet.Mesh mesh, int v) { List <STEdge> STEdges = new List <STEdge>(); foreach (var edge in mesh.Edges) { Vertex v0 = mesh.vertices[edge.P0]; Vertex v1 = mesh.vertices[edge.P1]; //Weight is the distance between the 2 points of an edge float weight = Vector3.Distance(new Vector3((float)v0.x, (float)v0.y, 0), new Vector3((float)v1.x, (float)v1.y, 0)); STEdge stEdge = new STEdge(); stEdge.weight = weight; stEdge.edge = edge; STEdges.Add(stEdge); } //Ascending order STEdges.Sort((a, b) => a.weight.CompareTo(b.weight)); vertices = v; return(STEdges); }
private void DrawMinimumSpanningTree() { if (!m_showMinimumSpanningTree) { return; } if (m_voronoiDiagram == null) { return; } if (m_voronoiDiagram.Edges == null || m_voronoiDiagram.Edges.Count == 0) { return; } if (m_spanningTree == null || m_delaunayTriangulationEdges != m_voronoiDiagram.Edges) { m_delaunayTriangulationEdges = m_voronoiDiagram.Edges; List <STEdge> edges = new List <STEdge>(); STEdge cacheEdge = null; for (int i = 0; i < m_delaunayTriangulationEdges.Count; i++) { cacheEdge = new STEdge(); cacheEdge.PointA = m_delaunayTriangulationEdges[i].LeftSite; cacheEdge.PointB = m_delaunayTriangulationEdges[i].RightSite; edges.Add(cacheEdge); } m_spanningTree = new MinimumSpanningTree(edges.ToArray()); } Gizmos.color = m_minimumSpanningTreeColor; for (int i = 0; i < m_spanningTree.Segments.Count; i++) { DrawThickLine(m_spanningTree.Segments[i].PointA, m_spanningTree.Segments[i].PointB); } }
private void GenerateSpannningTree() { List <Vector3> roomCenters = new List <Vector3>(); for (int i = 0; i < m_rooms.Length; i++) { roomCenters.Add(m_rooms[i].Center + m_rooms[i].CenterBias); } m_voronoiDiagram = new VoronoiDiagram(roomCenters.ToArray(), new VBorder(m_mapSize.x, m_mapSize.z)); STEdge cacheEdge = null; List <STEdge> stEdges = new List <STEdge>(); for (int i = 0; i < m_voronoiDiagram.Edges.Count; i++) { cacheEdge = new STEdge(); cacheEdge.PointA = m_voronoiDiagram.Edges[i].LeftSite; cacheEdge.PointB = m_voronoiDiagram.Edges[i].RightSite; stEdges.Add(cacheEdge); } m_spanningTree = new MinimumSpanningTree(stEdges.ToArray()); }
private void GenerateEdges() { int cacheValue1; int cacheValue2; m_edges = new List <STEdge>(); STEdge cacheEdge = new STEdge(); for (int h = 0; h < m_height; h++) { cacheValue1 = 0; cacheValue2 = 1; while (!IsOutOfBoundary(cacheValue1, h) && m_cells[cacheValue1, h].IsWall) { cacheValue1++; cacheValue2 = cacheValue1 + 1; } while (cacheValue2 < m_width) { if (m_cells[cacheValue2, h].IsWall) { cacheValue2++; continue; } cacheEdge = new STEdge(); cacheEdge.PointA = m_cells[cacheValue1, h].Center; cacheEdge.PointB = m_cells[cacheValue2, h].Center; m_edges.Add(cacheEdge); cacheValue1 = cacheValue2; cacheValue2 = cacheValue1 + 1; } } for (int w = 0; w < m_width; w++) { cacheValue1 = 0; cacheValue2 = 1; while (!IsOutOfBoundary(w, cacheValue1) && m_cells[0, cacheValue1].IsWall) { cacheValue1++; cacheValue2 = cacheValue1 + 1; } while (cacheValue2 < m_height) { if (m_cells[w, cacheValue2].IsWall) { cacheValue2++; continue; } cacheEdge = new STEdge(); cacheEdge.PointA = m_cells[w, cacheValue1].Center; cacheEdge.PointB = m_cells[w, cacheValue2].Center; m_edges.Add(cacheEdge); cacheValue1 = cacheValue2; cacheValue2 = cacheValue1 + 1; } } }
static public List <STEdge> FormTree(TriangleNet.Mesh mesh, int v, bool AddExtraEdges = false) { //Initialized here because static List <STEdge> result = new List <STEdge>(); List <STEdge> STEdges = SetWeights(mesh, v); List <Set> Sets = new List <Set>(); //# of sets are equal to number of vertices in graph for (int i = 0; i < vertices; i++) { Set set = new Set(); set.parent = i; set.rank = 0; Sets.Add(set); } int e = 0; int w = 0; int cycles = 0; //for MST edges = v - 1 while (e < vertices - 1) { //Edge to evaluate STEdge currentEdge = STEdges[w]; // Increment the index for next iteration w++; //Finds the parent of each node int x = Find(currentEdge.edge.P0, Sets); int y = Find(currentEdge.edge.P1, Sets); //If parents are equal including causes cycles so nothing happpens if (x == y) { cycles++; } //If parents are not equal including does not cause a cycle if (x != y) { e++; result.Add(currentEdge); Union(x, y, Sets); } } //This section is still in development. Produces inconsistent results of good to terrible. //This is mainly used to prevent the spanning tree from becoming too linear if (AddExtraEdges == true) { float extra = cycles * 0.15f; if (extra <= 1) { extra = 1; } for (int z = 0; z < extra; z++) { STEdge currentEdge = STEdges[Random.Range(0, STEdges.Count)]; for (int i = 0; i < STEdges.Count; i++) { if (result[i].edge == currentEdge.edge) { currentEdge = STEdges[Random.Range(0, STEdges.Count)]; continue; } else { result.Add(currentEdge); break; } } } } Debug.Log("Spanning Tree Complete!"); for (int i = 0; i < result.Count; i++) { int x = result[i].edge.P0; int y = result[i].edge.P1; //Debug.Log(x + " " + y); } return(result); }