Example #1
0
 public void LoadButtonClick()
 {
     try {
         string path = EditorUtility.OpenFilePanel("Opening file", "Assets/Saves", "txt");
         using (StreamReader sr = File.OpenText(path)) {
             ClearAll.RemoveAll();
             string curr = sr.ReadLine();
             curr = sr.ReadLine();
             int y = 0;
             int pcount;
             int ecount;
             pcount = (int)getNumber(curr, ref y);
             curr   = sr.ReadLine();
             y      = 0;
             ecount = (int)getNumber(curr, ref y);
             for (int i = 1; i <= pcount; i++)
             {
                 int p = 0;
                 curr = sr.ReadLine();
                 float vx = getNumber(curr, ref p);
                 p++;
                 float vy = getNumber(curr, ref p);
                 p++;
                 float      vz      = getNumber(curr, ref p);
                 Vector3    pos     = new Vector3(vx, vy, vz);
                 GameObject currobj = (GameObject)Instantiate(instance, pos, Quaternion.identity);
                 Transform  currt   = currobj.GetComponent <Transform> ();
                 currt.position = pos;
                 Point current = new Point(pos, currobj);
             }
             for (int i = 1; i <= ecount; i++)
             {
                 curr = sr.ReadLine();
                 int p     = 0;
                 int start = (int)getNumber(curr, ref p);
                 p++;
                 int end = (int)getNumber(curr, ref p);
                 p++;
                 int weight = (int)getNumber(curr, ref p);
                 p++;
                 int        orientation = curr[p] - '0';
                 GameObject cylinder;
                 if (orientation == 0)
                 {
                     cylinder = (GameObject)Instantiate(edgeInstance, Vector3.zero, Quaternion.identity);
                 }
                 else
                 {
                     cylinder = (GameObject)Instantiate(orientedInstance, Vector3.zero, Quaternion.identity);
                 }
                 Edge current;
                 if (orientation == 0)
                 {
                     current = new Edge(start, end, weight, cylinder, i);
                 }
                 else
                 {
                     current = new Edge(start, end, weight, cylinder, i, true);
                 }
                 Edge.edges [i] = current;
                 if (Point.Points[start].connected == null)
                 {
                     Point.Points[start].connected = new List <Edge>();
                 }
                 if (orientation == 0)
                 {
                     if (Point.Points[end].connected == null)
                     {
                         Point.Points[end].connected = new List <Edge>();
                     }
                 }
                 Point.Points[start].connected.Add(current);
                 if (orientation == 0)
                 {
                     Point.Points[end].connected.Add(current);
                 }
                 EdgeCreate.InitializeEdge(Point.Points[start].body, Point.Points[end].body, cylinder, current.label);
             }
             Edge.edgeCount   = ecount;
             Point.pointCount = pcount;
         }
     }
     catch (System.Exception k) {
         Debug.Log(k.Message);
     }
 }
Example #2
0
    IEnumerator JohnsonSupport()
    {
        AlgoSearch.waitTime = 0.2f;
        int realSelected = EdgeCreate.selectedNumber;

        //Point[] sPoints = new Point[100];
        //Point.Points.CopyTo(sPoints,0);
        //Edge[] sEdges = new Edge[100];
        //Edge.edges.CopyTo(sEdges,0);
        //int sedgeCount = Edge.edgeCount;
        //int spointCount = Point.pointCount;
        int[] sWeights = new int[100];
        for (int i = 1; i <= Edge.edgeCount; i++)
        {
            sWeights [i] = Edge.edges [i].weight;
        }
        Vector3 sPos = new Vector3(0, 12, 0);

        while (!CreatePoint.checkDistance(sPos))
        {
            sPos += new Vector3(2, 2, 2);
        }
        GameObject sBody = (GameObject)Instantiate(instance, sPos, Quaternion.identity);

        List <Edge>[] sConnected = new List <Edge> [1000];
        for (int i = 1; i <= Point.pointCount; i++)
        {
            sConnected[i] = Point.Points[i].connected;
        }

        Point s    = new Point(sPos, sBody);
        int   sNum = Point.pointCount;

        Debug.Log("Modifying graph ...");
        yield return(new WaitForSeconds(0.5f));

        for (int i = 1; i < Point.pointCount; i++)
        {
            GameObject cylinder = (GameObject)Instantiate(edgeInstance, Vector3.zero, Quaternion.identity);
            Edge.edgeCount++;
            Edge current;
            current = new Edge(sNum, i, 0, cylinder, Edge.edgeCount, true);
            Edge.edges [Edge.edgeCount] = current;
            EdgeCreate.InitializeEdge(sBody, Point.Points [i].body, cylinder, current.label);
            if (Point.Points [sNum].connected == null)
            {
                Point.Points [sNum].connected = new List <Edge> ();
            }
            Point.Points [sNum].connected.Add(current);
            yield return(new WaitForSeconds(0.5f));
        }
        EdgeCreate.selectedNumber = Point.pointCount;
        Debug.Log("Running Bellman ...");
        Bellman();
        yield return(new WaitForSeconds(command.load * AlgoSearch.waitTime));

        yield return(new WaitForSeconds(1));

        Debug.Log("Changing weights ...");
        for (int i = 1; i <= Edge.edgeCount - Point.pointCount + 1; i++)
        {
            int nWeight = Point.Points [Edge.edges [i].start].supportNumber - Point.Points [Edge.edges [i].end].supportNumber;
            Edge.edges [i].ChangeWeight(Edge.edges [i].weight + nWeight);
            yield return(new WaitForSeconds(0.5f));
        }


        yield return(new WaitForSeconds(1));

        for (int i = 1; i < Edge.edgeCount; i++)
        {
            Edge curr = Edge.edges [i];
            Table.values [curr.start, curr.end] = curr.weight;
            if (!curr.oriented)
            {
                Table.values [curr.end, curr.start] = curr.weight;
            }
        }
        EdgeCreate.selectedNumber = realSelected;
        Debug.Log("Running Dijkstra ... ");
        Dijkstra();

        yield return(new WaitForSeconds(command.load * AlgoSearch.waitTime));

        Debug.Log("Gathering result ... ");
        List <int> path = shortPath [EdgeCreate.anotherNumber];

        GameObject.DestroyObject(Point.Points [Point.pointCount].body);
        GameObject.DestroyObject(Point.Points [Point.pointCount].label);
        Point.Points [Point.pointCount].DestroySupportLabel();

        for (int j = 1; j <= Point.pointCount - 1; j++)
        {
            GameObject.Destroy(Edge.edges [Edge.edgeCount].body);
            GameObject.Destroy(Edge.edges [Edge.edgeCount].label);
            Table.values [Point.pointCount, j] = Edge.MaxW;
            Table.values [j, Point.pointCount] = Edge.MaxW;
            Edge.edgeCount--;
        }
        Point.pointCount--;
        for (int i = 1; i <= Edge.edgeCount; i++)
        {
            Edge.edges [i].ChangeWeight(sWeights [i]);
        }
        //Redraw();
        int        another = EdgeCreate.anotherNumber;
        List <int> cList   = shortPath [another];
        int        result  = 0;

        if (cList.Count == 0)
        {
            Debug.Log("No path available");
            command.CancelAll();
            return(true);
        }
        cList.Add(another);

        for (int k = 0; k < cList.Count - 1; k++)
        {
            Edge nextDraw = getEdge(cList [k], cList [k + 1]);
            // add connected
            result += nextDraw.weight;
            command.AddCommand(-nextDraw.number, Color.red);
        }
        string str = " Shortest path: ";

        for (int i = 0; i < cList.Count - 1; i++)
        {
            str += cList[i].ToString();
            str += " -> ";
        }
        str += cList[cList.Count - 1];
        Debug.Log("Johnsons Done. Result : " + result.ToString() + str);
        EdgeCreate.anotherPoint   = null;
        EdgeCreate.anotherNumber  = 0;
        EdgeCreate.selectedNumber = 0;
        EdgeCreate.selectedPoint  = null;
        Redraw();
        for (int i = 1; i < Point.pointCount; i++)
        {
            Point.Points[i].connected = sConnected[i];
        }
        StartCoroutine(command.support());
        AlgoSearch.waitTime = 1f;
    }