コード例 #1
0
    /* Given a list of thresholds, connect all the thresholds together in a graph.
     * Also assign thresholds to their respective zones
     * Input: threshold graph
     */
    public void BuildThresholdGraph(List <Threshold> thresholdsList)
    {
        //look at all the thresholds in the list.
        //If threshold A and B have zones in common, then connect them
        for (int i = 0; i < thresholdsList.Count; i++)
        {
            Threshold   threshold         = thresholdsList[i];
            List <Edge> possibleNeighbors = new List <Edge>();

            for (int j = 0; j < thresholdsList.Count; j++)
            {
                if (j == i) //cannot connect with one's self
                {
                    continue;
                }

                int thresholdAID1 = thresholdsList[i].tzoneID; //threshold A's IDs
                int thresholdAID2 = thresholdsList[i].zoneId;
                int thresholdBID1 = thresholdsList[j].tzoneID; //threshold B's IDS
                int thresholdBID2 = thresholdsList[j].zoneId;

                if (thresholdAID1 == -1 || thresholdAID2 == -1 || thresholdBID1 == -1 || thresholdBID2 == -1)
                {
                    //some weird edge case. will have to investigate later
                    continue;
                }

                //if they share any ids in common, then B becomes a neighbor of A (A->B)
                if (thresholdAID1 == thresholdBID1 || thresholdAID1 == thresholdBID2 || thresholdAID2 == thresholdBID1 || thresholdAID2 == thresholdBID2)
                {
                    if (thresholdAID1 == thresholdBID1 && thresholdAID2 == thresholdBID2)
                    {
                        //however, we do not want to consider connecting thresholds that are in the same zones and connect the same zones
                        continue;
                    }

                    float weight = Vector3.Distance(thresholdsList[i].worldPosition, thresholdsList[j].worldPosition);
                    Edge  e      = new Edge(thresholdsList[i], thresholdsList[j], weight);
                    if (!possibleNeighbors.Contains(e))
                    {
                        possibleNeighbors.Add(e);
                    }
                }
            }

            //done looking at this threshold. add all found neighbors to the threshold cell
            Debug.Log("Zone:" + threshold.zoneId);
            Debug.Log("Number of edges: " + possibleNeighbors.Count);
            threshold.AssignNeighbors(possibleNeighbors);
        }

        //finished. add to the global variable
        thresholdGraph = thresholdsList;
    }