Exemple #1
0
        public QGPathFinder(Status status, MainForm parent)
        {
            m_parent = parent;
            m_status = status;

            graph    = new AdjacencyGraph <string, Edge <string> >(true);
            edgeCost = new Dictionary <Edge <string>, double>(graph.EdgeCount);
            this.fp  = m_status.floorPlan;
            if (this.m_status.position != null)
            {
                startPoint = this.m_status.position.location.X + "_" + this.m_status.position.location.Y;
                m_parent.PostMessage("start: " + startPoint);
            }
            else
            {
                startPoint = "4_4";
            }
            if (this.m_status.endPoint != null)
            {
                targetPoint = this.m_status.endPoint.X + "_" + this.m_status.endPoint.Y;
                m_parent.PostMessage("end: " + targetPoint);
            }
            else
            {
                targetPoint = "4_6";
            }

            buildGraph();
        }
Exemple #2
0
        public QGPathFinder(Status status, MainForm parent)
        {
            m_parent = parent;
            m_status = status;

            graph = new AdjacencyGraph<string, Edge<string>>(true);
            edgeCost = new Dictionary<Edge<string>, double>(graph.EdgeCount);
            this.fp = m_status.floorPlan;
            if (this.m_status.position != null)
            {
                startPoint = this.m_status.position.location.X + "_" + this.m_status.position.location.Y;
                m_parent.PostMessage("start: " + startPoint);
            }
            else
            {
                startPoint = "4_4";
            }
            if (this.m_status.endPoint!= null)
            {
                targetPoint = this.m_status.endPoint.X + "_" + this.m_status.endPoint.Y;
                m_parent.PostMessage("end: " + targetPoint);
            }
            else
            {
                targetPoint = "4_6";
            }

            buildGraph();
        }
Exemple #3
0
        public List <FloorTile> getPath()
        {
            if (this.m_status.position != null)
            {
                startPoint = this.m_status.position.location.X + "_" + this.m_status.position.location.Y;
                m_parent.PostMessage("start: " + startPoint);
            }
            else
            {
                startPoint = "4_4";
            }
            if (this.m_status.endPoint != null)
            {
                targetPoint = this.m_status.endPoint.X + "_" + this.m_status.endPoint.Y;
                m_parent.PostMessage("end: " + targetPoint);
            }
            else
            {
                targetPoint = "4_6";
            }



            //startPoint = txtStartPoint.Text;
            //targetPoint = txtTargetPoint.Text;

            DijkstraShortestPathAlgorithm <string, Edge <string> > dijkstra = new DijkstraShortestPathAlgorithm <string, Edge <string> >(graph, AlgorithmExtensions.GetIndexer <Edge <string>, double>(edgeCost));

            // Attach a Vertex Predecessor Recorder Observer to give us the paths
            QuickGraph.Algorithms.Observers.VertexPredecessorRecorderObserver <string, Edge <string> > predecessorObserver = new QuickGraph.Algorithms.Observers.VertexPredecessorRecorderObserver <string, Edge <string> >();
            predecessorObserver.Attach(dijkstra);

            // attach a distance observer to give us the shortest path distances
            VertexDistanceRecorderObserver <string, Edge <string> > distObserver = new VertexDistanceRecorderObserver <string, Edge <string> >(AlgorithmExtensions.GetIndexer <Edge <string>, double>(edgeCost));

            distObserver.Attach(dijkstra);

            // Run the algorithm with A set to be the source
            dijkstra.Compute(startPoint);

            String outString = "";

            //outString += distObserver.Distances[targetPoint] + "\n";

            IEnumerable <Edge <string> > path;

            if (predecessorObserver.TryGetPath(targetPoint, out path))
            {
                foreach (var u in path)
                {
                    outString += u + ";";
                }
            }

            List <FloorTile> retval = new List <FloorTile>();

            string[] outEdges = Regex.Split(outString, ";");
            if (outEdges.Length > 0)
            {
                for (int i = 0; i < outEdges.Length; i++)
                {
                    if (outEdges[i].Length > 0)
                    {
                        m_parent.PostMessage(outEdges[i]);
                        string[] outPoint = Regex.Split(outEdges[i], "->");
                        //start points
                        retval.Add(getTileByIndex(fp, outPoint[0]));
                    }
                }
                //add target
                retval.Add(getTileByIndex(fp, targetPoint));
            }

            m_parent.PostMessage(retval.Count.ToString());
            m_parent.PostMessage(outString);

            if (retval.Count == 1 && retval[0].Equals(getTileByIndex(fp, targetPoint)))
            {
                m_parent.PostMessage("Can't find path. Start or end point is not walkable or no available walkable tiles");
                return(null);
            }



            return(condenseList(retval));
        }