Example #1
0
 protected string SearchForPath(ISearchPathEngine searchEngine, bool countTimeEnable)
 {
     start            = map.GetStartPosition();
     end              = map.GetEndPosition();
     searchPathEngine = searchEngine;
     if (start != null && end != null)
     {
         searchEngine.InitEngineForMap(map.GetPositionSet_Connected());
         path = searchEngine.SearchPath(start, end);
         if (path != null)
         {
             double length = 0, dx, dy;
             for (int i = 1; i < path.Count; i++)
             {
                 dx      = path[i].GetX() - path[i - 1].GetX();
                 dy      = path[i].GetY() - path[i - 1].GetY();
                 length += Math.Sqrt(dx * dx + dy * dy);
             }
             if (countTimeEnable)
             {
                 return(string.Format("Time:{0} milliseconds.\nPath length:{1}", timeCounter.CountTimeForRepeatableMethod(SearchForPath_countTime), length));
             }
             else
             {
                 return(string.Format("Path length:{1}", length));
             }
         }
     }
     return("There is not any path between the start point and the end point!");
 }
Example #2
0
        protected void performTest(ISearchPathEngine searchEngine)
        {
            List <IPosition_Connected> newPath;

            for (int i = 0; i < path.Count; i++)
            {
                searchEngine.InitEngineForMap(set[i]);
                newPath = searchEngine.SearchPath(start[i], end[i]);
                Console.WriteLine("There are " + newPath.Count.ToString() + " nodes in the path.");
                foreach (IPosition_Connected p in newPath)
                {
                    Console.WriteLine(p.ToString() + "\t" + p.GetAttachment().ToString());
                }
                Assert.AreEqual(newPath.Count, path[i].Count);
                for (int j = 0; j < newPath.Count; j++)
                {
                    Assert.AreEqual(com.Compare(newPath[j], path[i][j]), 0);
                }
            }
        }
        private void pathFindingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPositionSet_Connected positionSet_Connected = (IPositionSet_Connected)positionSetContainer.GetPositionSet();

            Dijkstra dijkstra = new Dijkstra();
            AStar    astar    = new AStar();
            M2M_PF   m2m_PF   = new M2M_PF();

            IM2MStructure m2mStructure = null;

            List <IPosition_Connected> m2mPath = null;
            List <IPosition_Connected> path    = null;

            //计算算法运行时间用
            //IPosition_Connected start = null;
            //IPosition_Connected end = null;
            ISearchPathEngine searchPathEngine1 = m2m_PF;
            ISearchPathEngine searchPathEngine2 = dijkstra;

            m2m_PF.GetM2MStructure             += delegate(IM2MStructure m2mS) { m2mStructure = m2mS; };
            m2m_PF.GetM2MStructureInPreprocess += delegate(IM2MStructure m2mS) { m2mStructure = m2mS; };
            List <ushort> timeStampList = new List <ushort>();

            m2m_PF.GetTimeStamp += delegate(ushort timeStamp) { timeStampList.Add(timeStamp); };

            searchPathEngine1.InitEngineForMap(positionSet_Connected);
            searchPathEngine2.InitEngineForMap(positionSet_Connected);

            //以下代码必须在UI线程中调用,即不能在另开的线程中调用
            LayersExOptDlg       layers               = new LayersExOptDlg();
            LayersPainterForm    layersPainterForm    = new LayersPainterForm(layers);
            LayersPaintedControl layersPaintedControl = layersPainterForm.LayersPaintedControl;
            LayersEditedControl  layersEditedControl  = new LayersEditedControl();

            layersEditedControl.Dock = DockStyle.Top;
            layersEditedControl.LayersPaintedControl = layersPaintedControl;
            layersPainterForm.Controls.Add(layersEditedControl);
            layersPainterForm.Show();

            //打开一个Worker线程来进行算法流程的演示(否则会阻塞UI线程以至于演示不能进行)
            IAsyncResult result = new dDemoProcess(delegate
            {
                Layer_PositionSet_Connected layer_PositionSet_Connected = new Layer_PositionSet_Connected(positionSet_Connected);
                layer_PositionSet_Connected.Point.PointColor            = Color.Yellow;
                layer_PositionSet_Connected.Connection.LineColor        = Color.Blue;
                layer_PositionSet_Connected.Connection.LineWidth        = 0.6f;
                layer_PositionSet_Connected.Point.PointRadius           = 1.2f;
                layers.Add(layer_PositionSet_Connected);
                layer_PositionSet_Connected.SpringLayerRepresentationChangedEvent(layer_PositionSet_Connected);

                for (int levelSequence = 1; levelSequence < m2mStructure.GetLevelNum(); levelSequence++)
                {
                    Layer_PartSet_Connected layer_PartSet_Connected = new Layer_PartSet_Connected(m2mStructure, levelSequence);
                    layer_PartSet_Connected.Visible = false;
                    layers.Add(layer_PartSet_Connected);
                }

                while (true)
                {
                    IPosition_Connected start = (IPosition_Connected)layersEditedControl.GetMouseDoubleChickedNearestPositionInCurrentPositionSet(positionSet_Connected);
                    PositionSet_ConnectedEdit startPointSet = new PositionSet_ConnectedEdit();
                    startPointSet.AddPosition_Connected(start);
                    Layer_PositionSet_Point layerStartPoint = new Layer_PositionSet_Point(startPointSet);
                    layerStartPoint.Active = true;
                    layerStartPoint.Point.IsDrawPointBorder = true;
                    layerStartPoint.Point.PointRadius       = 5;
                    layerStartPoint.Point.PointColor        = Color.PaleGreen;
                    layers.Add(layerStartPoint);
                    layerStartPoint.SpringLayerRepresentationChangedEvent(layerStartPoint);

                    IPosition_Connected end = (IPosition_Connected)layersEditedControl.GetMouseDoubleChickedNearestPositionInCurrentPositionSet(positionSet_Connected);
                    PositionSet_ConnectedEdit endPointSet = new PositionSet_ConnectedEdit();
                    endPointSet.AddPosition_Connected(end);
                    Layer_PositionSet_Point layerEndPoint = new Layer_PositionSet_Point(endPointSet);
                    layerEndPoint.Active = true;
                    layerEndPoint.Point.IsDrawPointBorder = true;
                    layerEndPoint.Point.PointRadius       = 5;
                    layerEndPoint.Point.PointColor        = Color.Cyan;
                    layers.Add(layerEndPoint);
                    layerEndPoint.SpringLayerRepresentationChangedEvent(layerEndPoint);

                    CountTimeTool.TimeCounter timeCounter = new CountTimeTool.TimeCounter();

                    searchPathEngine2.InitEngineForMap(positionSet_Connected);
                    double time2      = timeCounter.CountTimeForRepeatableMethod(delegate { searchPathEngine2.SearchPath(start, end); });
                    path              = searchPathEngine2.SearchPath(start, end);
                    float pathLength2 = ((Dijkstra)searchPathEngine2).GetPathLength();
                    Console.WriteLine("Dijkstra consume time: " + time2 + " path Length = " + pathLength2);

                    searchPathEngine1.InitEngineForMap(positionSet_Connected);

                    //清空timeStampList以记录寻径过程各层的timeStamp
                    double time1 = timeCounter.CountTimeForRepeatableMethod(delegate { searchPathEngine1.SearchPath(start, end); });

                    timeStampList.Clear();
                    m2mPath           = searchPathEngine1.SearchPath(start, end);
                    float pathLength1 = ((M2M_PF)searchPathEngine1).GetPathLength();

                    Console.WriteLine("M2M_PF   consume time: " + time1 + " path Length = " + pathLength1);
                    //searchPathEngine1.SearchPath(start, end);

                    if (searchPathEngine1 is M2M_PF && m2mStructure != null)
                    {
                        for (int levelSequence = 1; levelSequence < m2mStructure.GetLevelNum(); levelSequence++)
                        {
                            ILevel level             = m2mStructure.GetLevel(levelSequence);
                            IPart rootPart           = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0);
                            IPositionSet positionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0);

                            List <IPosition_Connected> position_ConnectedList = new List <IPosition_Connected>();
                            positionSet.InitToTraverseSet();
                            while (positionSet.NextPosition())
                            {
                                if (positionSet.GetPosition() is IPart_Multi)
                                {
                                    IPart_Multi partMulti = (IPart_Multi)positionSet.GetPosition();
                                    IEnumerable <IPart_Connected> part_ConnectedEnumerable = partMulti.GetSubPartSet();

                                    foreach (IPart_Connected part in part_ConnectedEnumerable)
                                    {
                                        IPosition_Connected tempPartConnected = (IPosition_Connected)part;
                                        if (((Tag_M2M_Part)tempPartConnected.GetAttachment()).isNeedToSearch == true)
                                        {
                                            if (timeStampList[levelSequence - 1] == ((Tag_M2M_Part)tempPartConnected.GetAttachment()).timeStamp)
                                            {
                                                position_ConnectedList.Add(tempPartConnected);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    IPosition_Connected tempPartConnected = (IPosition_Connected)positionSet.GetPosition();
                                    if (((Tag_M2M_Part)tempPartConnected.GetAttachment()).isNeedToSearch == true)
                                    {
                                        if (timeStampList[levelSequence - 1] == ((Tag_M2M_Part)tempPartConnected.GetAttachment()).timeStamp)
                                        {
                                            position_ConnectedList.Add(tempPartConnected);
                                        }
                                    }
                                }
                            }

                            IPositionSet partSet = new PositionSet_Connected(position_ConnectedList);

                            Layer_M2MPartSetInSpecificLevel layer_M2MPartSetInSpecificLevel = new Layer_M2MPartSetInSpecificLevel(m2mStructure.GetLevel(levelSequence), partSet);
                            layer_M2MPartSetInSpecificLevel.Active = true;
                            layer_M2MPartSetInSpecificLevel.Alpha  = 100;
                            //layer_M2MPartSetInSpecificLevel.

                            layers.Add(layer_M2MPartSetInSpecificLevel);

                            //PositionSet_Connected partSet_Connected = new PositionSet_Connected(position_ConnectedList);

                            //Layer_PositionSet_Connected layer_PartSet_Connected = new Layer_PositionSet_Connected(partSet_Connected);
                            ////layer_PartSet_Connected.MainColor
                            ////layer_PartSet_Connected.Active = true;
                            //layer_PartSet_Connected.SetPositionSetTransformByM2MLevel(level);

                            //layers.Add(layer_PartSet_Connected);
                        }
                    }

                    if (path != null)
                    {
                        Layer_PositionSet_Path layer = new Layer_PositionSet_Path(new PositionSet_Connected(path));
                        layer.Active                      = true;
                        layer.PathLine.LineColor          = Color.Black;
                        layer.PathLine.LineWidth          = 2;
                        layer.PathPoint.PointRadius       = 2;
                        layer.PathPoint.IsDrawPointBorder = true;

                        //layer.EditAble = true;
                        //layer.Point.PointColor = Color.Yellow;
                        //layer.Point.PointRadius = 2;
                        //layer.Point.IsDrawPointBorder = true;
                        layers.Add(layer);
                        layer.SpringLayerRepresentationChangedEvent(layer);
                    }
                    else
                    {
                        this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "There is no path between two node" });
                    }

                    if (m2mPath != null)
                    {
                        Layer_PositionSet_Path layer = new Layer_PositionSet_Path(new PositionSet_Connected(m2mPath));
                        layer.Active                      = true;
                        layer.PathLine.LineColor          = Color.Red;
                        layer.PathLine.LineWidth          = 2;
                        layer.PathPoint.PointRadius       = 2;
                        layer.PathPoint.IsDrawPointBorder = true;

                        //layer.EditAble = true;
                        //layer.Point.PointColor = Color.Yellow;
                        //layer.Point.PointRadius = 2;
                        //layer.Point.IsDrawPointBorder = true;
                        layers.Add(layer);
                        layer.SpringLayerRepresentationChangedEvent(layer);
                    }
                    else
                    {
                        this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "There is no path between two node by M2M_PF" });
                    }
                }
            }).BeginInvoke(null, null);
        }