//--------------------------------------------------------------------------------------------------------------- // weights all graph nodes to given source node public static bool Perform( Graph graph, Node source ) { if( source == null || !graph.Nodes.Contains(source) ) return false; if(_DEBUG) Debug.Log("----->START DIJKSTRA " + source.ID); MGraph = graph; MGraph.Reset(); source.AggregatedCost = 0; bool finished = false; Queue = new List<Node>(graph.Nodes); int count = 0; while(!finished) { Node nextNode = Queue.OrderBy( c => c.AggregatedCost ).FirstOrDefault( c => !float.IsPositiveInfinity(c.AggregatedCost) ); if(nextNode != null) { ProcessNode(nextNode); Queue.Remove(nextNode); count++; } else finished = true; } if(_DEBUG) Debug.Log ("------>Finish Dijkstra " + count); return true; }
/// <summary> /// fetches pathnode data from the scene and constructs the levelgraph /// </summary> public static void SetupGraph(GraphData data) { graph = new Graph(); if(data.hasContent()) { // create nodes var nodes = data.nodes.Select((graphNode n)=> { Node node = new Node(); node.pos = n.position; node.ID = n.ID; return node; }) .ToList(); // create edges var edges = data.edges.Select((graphConnection c)=> { Node a = nodes.Find(x=> x.ID==c.A); Node b = nodes.Find(x=> x.ID==c.B); if(a == null || b == null) { throw new System.Exception("could not load edge, wrong IDs stored: " + c.A + " / " + c.B + " nodecount: " + nodes.Count); } if(!c.supressed && (c.forced || GetEdgeCost(a, b) <= data.autoConnectionDist)) { Edge e = new Edge(a, b, c.dir, GetEdgeCost(a, b)); a.AdjacentEdges.Add(e); b.AdjacentEdges.Add(e); return e; } else return null; }) .Where(z=> z != null) .ToList(); // create graph graph.Nodes = nodes; graph.Edges = edges; Debug.Log(graph.PrintEdges()); } else { throw new System.Exception("could not load graph; Graphdata is empty."); } }