コード例 #1
0
        private OverlayEdge AddEdge(OverlayGraph graph, double x1, double y1, double x2, double y2)
        {
            var pts = new [] {
                new Coordinate(x1, y1), new Coordinate(x2, y2)
            };

            return(graph.AddEdge(pts, new OverlayLabel()));
        }
コード例 #2
0
        private OverlayGraph CreateGraph(params Coordinate[][] edges)
        {
            var graph = new OverlayGraph();

            foreach (var e in edges)
            {
                graph.AddEdge(e, new OverlayLabel());
            }
            return(graph);
        }
コード例 #3
0
        private static OverlayEdge FindEdge(OverlayGraph graph, double orgx, double orgy, double destx, double desty)
        {
            var edges = graph.Edges;

            foreach (var e in edges)
            {
                if (IsEdgeOrgDest(e, orgx, orgy, destx, desty))
                {
                    return(e);
                }
                if (IsEdgeOrgDest(e.SymOE, orgx, orgy, destx, desty))
                {
                    return(e.SymOE);
                }
            }
            return(null);
        }
コード例 #4
0
        public void TestStar()
        {
            var graph = new OverlayGraph();

            var e1 = AddEdge(graph, 5, 5, 0, 0);
            var e2 = AddEdge(graph, 5, 5, 0, 9);
            var e3 = AddEdge(graph, 5, 5, 9, 9);

            CheckNodeValid(e1);

            CheckNext(e1, e1.SymOE);
            CheckNext(e2, e2.SymOE);
            CheckNext(e3, e3.SymOE);

            CheckPrev(e1, e3.SymOE);
            CheckPrev(e2, e1.SymOE);
            CheckPrev(e3, e2.SymOE);
        }
コード例 #5
0
    public void FindMultiLevelPath()
    {
        DebugDrawer.Clear();
        var pathGrid   = GridController.Path;
        var memoryGrid = createGraph();
        var graph      = new OverlayGraph(memoryGrid);

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        graph.BuildChunk(0);
        graph.BuildChunk(1);
        graph.BuildChunk(2);
        graph.BuildChunk(3);
        UnityEngine.Debug.Log("MultiLayerGraph Building took " + sw.ElapsedMilliseconds + " ms");

        var sizeX = GridController.active.size.x;
        var sizeY = GridController.active.size.y;

        // Set grid weights
        var start = new Vector2Int(-1, -1);
        var end   = new Vector2Int(-1, -1);

        for (var x = 0; x < sizeX; x++)
        {
            for (var y = 0; y < sizeY; y++)
            {
                var pathTile = pathGrid.GetTile(new Vector3Int(x, y, 0));
                if (pathTile != null)
                {
                    if (pathTile == GridController.active.start)
                    {
                        start = new Vector2Int(x, y);
                    }
                    else if (pathTile == GridController.active.end)
                    {
                        end = new Vector2Int(x, y);
                    }
                }
            }
        }

        if (start.x == -1 || end.x == -1)
        {
            Debug.Log("Couldn't find any start or end position");
            return;
        }

        sw.Restart();

        List <OverlayGraphPathfinder.GraphNode> path;
        var pathfinder = new OverlayGraphPathfinder(graph);

        path = pathfinder.BidirectionalDijkstra(start.x, start.y, end.x, end.y);
        UnityEngine.Debug.Log("MultiLayerGraph - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");

        //  graph.DrawCellBorders();
        // graph.DrawOverlayGraph(1);

        // graph.DrawGraph();
        // overlayGraph.DrawGraph();

        if (path != null)
        {
            foreach (var pathTile in path)
            {
                var chunk = graph.GetChunk(pathTile.ChunkID);
                int gridPosition;
                if (pathTile.QueryLevel > 0)
                {
                    gridPosition = chunk.vertices[chunk.overlayVertices[pathTile.VertexID].OriginalVertex].GridPosition;
                }
                else
                {
                    gridPosition = chunk.vertices[pathTile.VertexID].GridPosition;
                }

                var x = gridPosition / graph.sizeY;
                var y = gridPosition % graph.sizeY;
                if (x == start.x && y == start.y)
                {
                    continue;
                }
                if (x == end.x && y == end.y)
                {
                    continue;
                }

                DebugDrawer.DrawCube(new Vector2Int(x, y), Vector2Int.one, Color.blue);
            }
        }
    }