//private void CollectEdgesR <EdgeType> (List<EdgeType> collectedEdges, List<LinkedGraphEdge> seenEdges, bool allConsecutive, SearchFilter filter, HashSet<EdgeType> mask) where EdgeType : LinkedGraphEdge //{ // EdgeType thisInstance = null; // if (this is EdgeType) // { // thisInstance = (EdgeType)this; // } // if ((thisInstance == null && allConsecutive) || collectedEdges.Contains(thisInstance) || seenEdges.Contains(this)) // { // return; // } // else if ((filter == null || filter(this)) && (mask == null || mask.Contains(thisInstance))) // { // collectedEdges.Add(thisInstance); // } // else if (allConsecutive)//if all the passing edge should be attached, or consecutive, then if this fails, return. // { // return; // } // EnumerateNeighborEdges((LinkedGraphEdge edge) => // { // if (!seenEdges.Contains(edge)) // { // edge.CollectEdgesR(collectedEdges, seenEdges, allConsecutive, filter, mask); // } // }); //} public void EnumerateNeighborEdges(System.Action <LinkedGraphEdge> action) { foreach (LinkedGraphEdge edge in a.GetConnections()) { if (edge != this) { action(edge); } } foreach (LinkedGraphEdge edge in b.GetConnections()) { if (edge != this) { action(edge); } } }
public List <EdgeType> GetLocalLoop(EdgeType startingEdge, bool ccw) { List <EdgeType> foundEdges = new List <EdgeType>(); foundEdges.Add(startingEdge); LinkedGraphVertex firstVertex = startingEdge.a; LinkedGraphVertex lastVertex = startingEdge.b; bool foundLoop = false; int edgesSeen = 0; while (!foundLoop) { EdgeLoopEdge lastEdge = foundEdges[foundEdges.Count - 1]; Vector2 lastEdgeDirection = lastEdge.GetOppositeVertex(lastVertex).pt - lastVertex.pt; float minAngle = float.MaxValue; EdgeType minAngleEdge = null; if (lastVertex.NumConnections() <= 1) { Debug.LogWarning("Reached end of loop while collecting loop."); return(null); } foreach (LinkedGraphEdge connection in lastVertex.GetConnections()) { if (connection == lastEdge) { continue;//ignore the connection if it is this instance. } //all connections must share lastVertex with lastEdge Vector2 thisEdgeDirection = connection.GetOppositeVertex(lastVertex).pt - lastVertex.pt; float angle = HelperFunctions.AngleBetween(thisEdgeDirection, lastEdgeDirection); if (!ccw) { //we want to invert the smallest angle when its cw since angleBetween gets the ccw angle angle = Mathf.PI * 2 - angle; } if (angle < minAngle) { if (connection is EdgeType) { minAngle = angle; minAngleEdge = (EdgeType)connection; } else { Debug.LogWarning("Could not isolate loop because connected edges were not EdgeLoopEdges"); } } edgesSeen++; if (edgesSeen > MAXLOOPSIZE) { Debug.LogWarning("Couldn't close loop. Failing"); DebugLines debug = GameObject.FindObjectOfType <DebugLines>(); foreach (EdgeType edge in foundEdges) { debug.AddEdge(edge, Color.red); } return(null); } } foundEdges.Add(minAngleEdge); lastVertex = minAngleEdge.GetOppositeVertex(lastVertex); if (lastVertex == firstVertex) { foundLoop = true; } } //maintain the loops go ccw paradigm if (!ccw) { foundEdges.Reverse(); } return(foundEdges); }