Esempio n. 1
0
    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);
    }