void DepthFirstSearch(int initial) { bool[] visited = new bool[identifiedPoints.Length]; int numVisited = 0; foreach (bool b in visited) { if (b) { numVisited++; } } Debug.Log("Initial " + numVisited + " / " + visited.Length); Stack <IntPair> dfsStack = new Stack <IntPair>(); dfsStack.Push(new IntPair(initial, initial)); // Keep track of the current chain of points List <int> currentPoints = null; while (dfsStack.Count > 0) { IntPair nextPair = dfsStack.Pop(); int next = nextPair.num; if (visited[next]) { continue; } // Mark this point and all points merged with it as visited foreach (int p in identifiedPoints[next]) { visited[p] = true; } // Add all neighbors of this point to the list // if they haven't been visited already HashSet <int> neighbors = adjacency[next]; foreach (int neighbor in neighbors) { if (!visited[neighbor]) { dfsStack.Push(new IntPair(neighbor, next)); } } // If this is the first point, start a new line segment. if (currentPoints == null) { currentPoints = new List <int>(); currentPoints.Add(next); } // If this point has degree 2 (i.e. is a part of a straight line segment) // then just continue the current segment. else if (neighbors.Count == 2) { currentPoints.Add(next); } // If this point has degree 1, then we just end the current segment. else if (neighbors.Count == 1) { currentPoints.Add(next); // Can filter out single dead-ends to try to remove noise //if (currentPoints.Count > 2) AddLine(currentPoints); // Start a new segment at the predecessor of the most recent branch point. currentPoints = new List <int>(); if (dfsStack.Count > 0) { currentPoints.Add(dfsStack.Peek().predecessor); } } // If the point has degree greater than 2, then it is a branch, and we // start a new segment at the current point. else { currentPoints.Add(next); AddLine(currentPoints); currentPoints = new List <int>(); currentPoints.Add(next); } } numVisited = 0; foreach (bool b in visited) { if (b) { numVisited++; } } Debug.Log("Visited " + numVisited + " / " + visited.Length); }
void DepthFirstSearch(int initial) { GetCanvas(); bool[] visited = new bool[heMesh.Vertices.Length]; int numVisited = 0; foreach (bool b in visited) { if (b) { numVisited++; } } Debug.Log("Initial " + numVisited + " / " + visited.Length); Stack <IntPair> dfsStack = new Stack <IntPair>(); dfsStack.Push(new IntPair(initial, initial)); // Keep track of the current chain of points List <int> currentPoints = null; Dictionary <int, DraggablePoint> bulbDict = new Dictionary <int, DraggablePoint>(); DraggablePoint bulbAtStart = null; while (dfsStack.Count > 0) { IntPair nextPair = dfsStack.Pop(); int next = nextPair.num; if (visited[next]) { continue; } HashSet <int> equivClass = uf.Find(next).MergedPoints; // Mark this point and all points merged with it as visited foreach (int p in equivClass) { visited[p] = true; } // Add all neighbors of this point to the list // if they haven't been visited already HashSet <int> neighbors = adjacency[next]; foreach (int neighbor in neighbors) { if (!visited[neighbor]) { dfsStack.Push(new IntPair(neighbor, next)); } } // If this is the first point, start a new line segment. if (currentPoints == null) { Vector3 pos = transform.rotation * mesh.vertices[next] + transform.position; bulbAtStart = outputCanvas.AddBulb(pos); //bulbAtStart.SetSize(FitSphere(mesh.vertices[next])); bulbAtStart.SetSize(0.1f); bulbDict.Add(next, bulbAtStart); currentPoints = new List <int>(); currentPoints.Add(next); } // If this point has degree 2 (i.e. is a part of a straight line segment) // then just continue the current segment. else if (neighbors.Count == 2) { currentPoints.Add(next); } // If this point has degree 1, then we just end the current segment. else if (neighbors.Count == 1) { currentPoints.Add(next); // Can filter out single dead-ends to try to remove noise //if (currentPoints.Count > 2) CatmullRomSpline spline = AddLine(currentPoints); spline.StartBulb = bulbAtStart; // Start a new segment at the predecessor of the most recent branch point. currentPoints = new List <int>(); if (dfsStack.Count > 0) { currentPoints.Add(dfsStack.Peek().predecessor); DraggablePoint bulb; if (bulbDict.TryGetValue(dfsStack.Peek().predecessor, out bulb)) { bulbAtStart = bulb; } else { bulbAtStart = null; } } } // If the point has degree greater than 2, then it is a branch, and we // start a new segment at the current point. else { Vector3 pos = transform.rotation * mesh.vertices[next] + transform.position; DraggablePoint bulb = outputCanvas.AddBulb(pos); //bulb.SetSize(FitSphere(mesh.vertices[next])); bulb.SetSize(0.1f); bulbDict.Add(next, bulb); currentPoints.Add(next); CatmullRomSpline spline = AddLine(currentPoints); spline.EndBulb = bulb; spline.StartBulb = bulbAtStart; // New segments should start at the bulb we just inserted. bulbAtStart = bulb; currentPoints = new List <int>(); currentPoints.Add(next); } } numVisited = 0; foreach (bool b in visited) { if (b) { numVisited++; } } Debug.Log("Visited " + numVisited + " / " + visited.Length); }