Пример #1
0
        private void ShowLine()
        {
            IWriteLineInGridEngine writeLineInGridEngine = new Line2Block();
            //IWriteLineInGridEngine writeLineInGridEngine = new Line2Block();

            IPositionSet line = writeLineInGridEngine.WriteLineInGrid(configurationForWriteLineInGridAlgorithm.GridWidth,
                                                                      configurationForWriteLineInGridAlgorithm.GridHeight,
                                                                      configurationForWriteLineInGridAlgorithm.StartPosition,
                                                                      configurationForWriteLineInGridAlgorithm.EndPosition);

            painterDialog.Clear();
            painterDialog.Layers.Add(new Layer_Grid(10, 10, configurationForWriteLineInGridAlgorithm.GridWidth,
                                                    configurationForWriteLineInGridAlgorithm.GridHeight, 0, 0));

            IPositionSetEdit startAndEnd = new PositionSetEdit_ImplementByICollectionTemplate();

            startAndEnd.AddPosition(configurationForWriteLineInGridAlgorithm.StartPosition);
            startAndEnd.AddPosition(configurationForWriteLineInGridAlgorithm.EndPosition);

            painterDialog.HoldOnMode();
            painterDialog.SetShowModeToForm();

            if (line != null)
            {
                Layer_PositionSet_Point layer = new Layer_PositionSet_Point(line);
                layer.Point.PointRadius = 2;
                layer.Point.PointColor  = Color.Blue;
                painterDialog.Layers.Add(layer);
            }

            Layer_PositionSet_Path layer2 = new Layer_PositionSet_Path((IPositionSet)startAndEnd);

            layer2.PathLine.LineWidth = 2;
            layer2.PathLine.LineColor = Color.Red;
            painterDialog.Layers.Add(layer2);

            painterDialog.Show();
        }
Пример #2
0
        private void OnGetRepresentativeHullInSpecificLevel(ILevel level, int levelSequence, IPositionSet positionSet)
        {
            lock (layers)
            {
                if (representativeHullLayer == null)
                {
                    representativeHullLayer = new Layer_PositionSet_Path(positionSet);
                    representativeHullLayer.PositionSetTranslationX = -(level.ConvertRealValueToRelativeValueX(0));
                    representativeHullLayer.PositionSetTranslationY = -(level.ConvertRealValueToRelativeValueY(0));
                    representativeHullLayer.MainColor                   = Color.FromArgb(255, 128, 0);
                    representativeHullLayer.PathLine.LineWidth          = 2;
                    representativeHullLayer.PathLine.LineStyle          = System.Drawing.Drawing2D.DashStyle.Dash;
                    representativeHullLayer.PathPoint.PointColor        = Color.FromArgb(255, 128, 0);
                    representativeHullLayer.PathPoint.IsDrawPointBorder = true;
                    representativeHullLayer.PathPoint.PointRadius       = 2;
                    representativeHullLayer.Active = true;
                    layers.Add(representativeHullLayer);
                }
                representativeHullLayer.SpringLayerMaxRectChangedEvent();
            }

            flowControlerForm.BeginInvoke(Update);
            flowControlerForm.SuspendAndRecordWorkerThread();
        }
        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);
        }