public void Init(SparseGraph graph, int source, int target) { this.Clear(); m_Graph = graph; m_iSource = source; m_iTarget = target; m_bFound = false; m_Visited.Capacity = m_Graph.NumNodes(); m_Route.Capacity = m_Graph.NumNodes(); for (int i = 0; i < m_Graph.NumNodes(); i++) { //Debug.Log(m_Graph.NumNodes() + " : " + i); //chamto test m_Visited.Add((int)Aid.unvisited); m_Route.Add((int)Aid.no_parent_assigned); //m_Visited[i] = (int)Aid.unvisited; //m_Route[i] = (int)Aid.no_parent_assigned; } m_bFound = Search(); }
public void CreateGraph() { mNavGraph = new SparseGraph<NavGraphNode, GraphEdge> (mRow * mColumn); mNavGraph.BDrawMap = mBDrawMap; Vector3 nodeposition = new Vector3 (); int nextindex = 0; //SparseGraph nodes data for (int rw = 0; rw < mRow; rw++) { for (int col = 0; col < mColumn; col++) { nodeposition = new Vector3 (rw, 0.0f, col); nextindex = mNavGraph.NextFreeNodeIndex; mNavGraph.AddNode (new NavGraphNode (nextindex, nodeposition, 0.0f, false)); } } //SparseGraph edges data for (int rw = 0; rw < mRow; rw++) { for (int col = 0; col < mColumn; col++) { CreateAllNeighboursToGridNode(rw, col, mRow, mColumn); } } mTotalNodes = mNavGraph.NumNodes(); mTotalEdges = mNavGraph.NumEdges (); }
public SearchAStar(SparseGraph<NavGraphNode, GraphEdge> graph , int source , int target , bool isignorewall , float strickdistance , float hcostpercentage , bool drawexplorepath , float explorepathremaintime) { mGraph = graph; mPQ = new PriorityQueue<int, float>((int)Mathf.Sqrt(mGraph.NumNodes())); mGCosts = new List<float>(graph.NumNodes()); mFCosts = new List<Pair<int, float>>(graph.NumNodes()); mShortestPathTree = new List<GraphEdge>(graph.NumNodes()); mSearchFrontier = new List<GraphEdge>(graph.NumNodes()); //Init G cost and F cost and Cost value for (int i = 0; i < graph.NumNodes(); i++) { mGCosts.Add(0.0f); mFCosts.Add(new Pair<int, float>(i, 0.0f)); mShortestPathTree.Add(new GraphEdge()); mSearchFrontier.Add(new GraphEdge()); } mISource = source; mITarget = target; mOriginalTarget = target; Assert.IsTrue(hcostpercentage >= 0); mHCostPercentage = hcostpercentage; mBDrawExplorePath = drawexplorepath; mExplorePathRemainTime = explorepathremaintime; mAStarPathInfo = new PathInfo(); mIsIgnoreWall = isignorewall; mStrickDistance = strickdistance; //Search(mStrickDistance, mIsIgnoreWall); //GeneratePathToTargetInfo(); }
public void Render(Graphics objGraphics) { //render all the cells for (int nd = 0; nd < m_Graph.NumNodes(); ++nd) { int left = (int)(m_Graph.GetNode(nd).Pos.X - m_dCellWidth / 2.0); int top = (int)(m_Graph.GetNode(nd).Pos.Y - m_dCellHeight / 2.0); int right = (int)(1 + m_Graph.GetNode(nd).Pos.X + m_dCellWidth / 2.0); int bottom = (int)(1 + m_Graph.GetNode(nd).Pos.Y + m_dCellHeight / 2.0); Rectangle rect = Rectangle.FromLTRB(left, top, right, bottom); SolidBrush fillBrush = new SolidBrush(Color.Gray); switch (m_TerrainType[nd]) { case (int)brush_type.normal: fillBrush.Color = Color.White; break; case (int)brush_type.obstacle: fillBrush.Color = Color.Black; break; case (int)brush_type.water: fillBrush.Color = Color.DodgerBlue; break; case (int)brush_type.mud: fillBrush.Color = Color.Brown; break; default: fillBrush.Color = Color.White; break; }//end switch objGraphics.FillRectangle(fillBrush, rect); if (m_bShowTiles) { objGraphics.DrawLine(Pens.Gray, rect.Location, new Point(rect.Right, rect.Top)); objGraphics.DrawLine(Pens.Gray, rect.Location, new Point(rect.Left, rect.Bottom)); } if (nd == m_iTargetCell) { rect.Inflate(-4, -4); objGraphics.FillRectangle(Brushes.Red, rect); objGraphics.DrawString("T", m_font, Brushes.Black, rect.Right - (int)(rect.Width * 0.75), rect.Y); } else if (nd == m_iSourceCell) { rect.Inflate(-4, -4); objGraphics.FillRectangle(Brushes.LightGreen, rect); objGraphics.DrawString("S", m_font, Brushes.Black, rect.Right - (int)(rect.Width * 0.75), rect.Y); } //render dots at the corners of the cells objGraphics.DrawLine(m_ThickBlack, left, top, left + 1, top + 1); } //draw the graph nodes and edges if rqd if (m_bShowGraph) { if (m_Graph.NumNodes() > 0) { SparseGraph.NodeIterator NodeItr = new SparseGraph.NodeIterator(m_Graph); while (NodeItr.MoveNext()) { objGraphics.DrawEllipse(Pens.LightGray, (int)(NodeItr.Current.Pos.X - 2), (int)(NodeItr.Current.Pos.Y - 2), (int)2 * 2, (int)2 * 2); SparseGraph.EdgeIterator EdgeItr = new SparseGraph.EdgeIterator(m_Graph, NodeItr.Current.Index); while (EdgeItr.MoveNext()) { objGraphics.DrawLine(Pens.LightGray, (int)NodeItr.Current.Pos.X, (int)NodeItr.Current.Pos.Y, (int)m_Graph.GetNode(EdgeItr.Current.To).Pos.X, (int)m_Graph.GetNode(EdgeItr.Current.To).Pos.Y); } } } } //draw any tree retrieved from the algorithms for (int e = 0; e < m_SubTree.Count; ++e) { if (!NavGraphEdge.IsNull(m_SubTree[e])) { Vector2D from = m_Graph.GetNode(m_SubTree[e].From).Pos; Vector2D to = m_Graph.GetNode(m_SubTree[e].To).Pos; objGraphics.DrawLine(Pens.Red, (int)from.X, (int)from.Y, (int)to.X, (int)to.Y); } } //draw the path (if any) if (m_Path.Count > 0) { for (int i = 0; i < m_Path.Count; ++i) { if (i > 0) { Point start = new Point((int)m_Graph.GetNode(m_Path[i - 1]).Pos.X, (int)m_Graph.GetNode(m_Path[i - 1]).Pos.Y); Point end = new Point((int)m_Graph.GetNode(m_Path[i]).Pos.X, (int)m_Graph.GetNode(m_Path[i]).Pos.Y); objGraphics.DrawLine(m_ThickBlue, start, end); } } } }