예제 #1
0
        public InfoBox(ThreadedInfoBox _tInfoBox, Operation _OperationType, string DisplayText)
        {
            InitializeComponent();

            this.Title      = _tInfoBox.Title;
            TinfoBox        = _tInfoBox;
            OperationType   = _OperationType;
            lblText.Content = DisplayText;

            btnClose.Click  += (s, e) => this.Close();
            btnCancel.Click += (s, e) => { TinfoBox.Canceled?.Invoke((OperationType == Operation.Creating) ? Operation.Creating : Operation.Processing); this.Close(); };

            TinfoBox.DisplayTextChanged += (text) => this.Dispatcher.BeginInvoke(new Action(() =>
            {
                lblText.Content = text;

                if (text.Split(':')[0] == "Number of steps traveled")
                {
                    try
                    {
                        btnCancel.IsEnabled = false;
                        img.Margin          = new Thickness(28, 10, 20, 35);
                        WpfAnimatedGif.ImageBehavior.SetRepeatBehavior(img, new System.Windows.Media.Animation.RepeatBehavior(1));
                        WpfAnimatedGif.ImageBehavior.SetAnimatedSource(img, new BitmapImage(new Uri("pack://application:,,,/Resources/done.gif")));
                    }
                    catch { }
                }
            }));
        }
예제 #2
0
        public void InitDrawGrid(TableObject tableObj = null)
        {
            try
            {
                ThreadedInfoBox TinfoBox = new ThreadedInfoBox();
                TinfoBox.Canceled += (o) => {
                    if (o == Operation.Creating)
                    {
                        this.Dispatcher.BeginInvoke(new Action(() => this.Close()));
                    }
                };
                TinfoBox.StartNewThreadInfoBox(Operation.Creating, "Creating table structure ...", this.Title);

                DrawGrid();
                if (tableObj != null)
                {
                    FillSavedMapFields(tableObj);
                }
                this.Show();

                TinfoBox.EndNewThreadInfoBox();
            }
            catch { }
        }
예제 #3
0
        private void DrawPath(List <List <Rectangle> > MasterPath, List <Rectangle> AccumulatedEndPointList, Rectangle MasterStartPoint, ThreadedInfoBox TinfoBox, PathType PathType, int Steps = 0)
        {
            this.Dispatcher.BeginInvoke(new Action(() => ClearYellowAndCarTracks()));

            TinfoBox.DisplayTextChanged?.Invoke((PathType == PathType.StartToStop) ? "Reconstructing path ..." : "Going back the path ...");

            MasterPath.ForEach(subPath => subPath.Reverse());      // Reversi items mbrenda listave

            Rectangle OldRect = MasterPath.FirstOrDefault().FirstOrDefault();

            Task.Factory.StartNew(() =>
            {
                MasterPath.ForEach(subPath =>
                {
                    if (CancelProcessing)
                    {
                        return;
                    }
                    subPath.ForEach(r =>
                    {
                        if (CancelProcessing)
                        {
                            return;
                        }
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            if (AccumulatedEndPointList.Any(rList => (rList.Tag as RectangleParameter).GridPosition.Location == (OldRect.Tag as RectangleParameter).GridPosition.Location &&
                                                            (rList.Tag as RectangleParameter).GridPosition.Location != (MasterStartPoint.Tag as RectangleParameter).GridPosition.Location))
                            {
                                OldRect.Fill = RedFlagChecked;
                            }
                            else if ((MasterStartPoint.Tag as RectangleParameter).GridPosition.Location == (OldRect.Tag as RectangleParameter).GridPosition.Location)
                            {
                                OldRect.Fill = GreenFlag;
                            }
                            else
                            {
                                OldRect.Fill = YellowSpace;
                            }
                            r.Fill  = new ImageBrush(CarOrientaion(OldRect, r));
                            OldRect = r;
                        }));
                        Steps++;
                        AllowUIToUpdate();          // Qiky funksion e mundeson te behet Update UI, ende pa u krye kjo method
                        Thread.Sleep(_AnimationSpeed);
                    });
                });
            }).ContinueWith(task => {
                if (!CancelProcessing)
                {
                    this.Dispatcher.BeginInvoke(new Action(() => {
                        OldRect.Fill = (BackPathType == BackPathType.Reversed) ?
                                       ((PathType == PathType.StartToStop) ? RedFlagChecked : YellowSpace) : GreenFlag;
                    }));

                    TinfoBox.DisplayTextChanged?.Invoke("Waiting for confirmation ...");
                    //ShowMessage(true, PathType);
                    if (PathType == PathType.StartToStop && BackPathType == BackPathType.Reversed)
                    {
                        MasterPath.Reverse();       // Reversi listat mbrenda MasterPath e pastaj ne fillim te methodes bohen ashtu kshtu reverse items mbrenda seciles liste
                        DrawPath(MasterPath, AccumulatedEndPointList, MasterStartPoint, TinfoBox, PathType.StopToStart, Steps);
                    }
                }
                CancelProcessing       = false;
                ApplyingAStarAlgorithm = false;
                if (PathType == PathType.StopToStart || BackPathType == BackPathType.Shortest)
                {
                    TinfoBox.DisplayTextChanged?.Invoke($"Number of steps traveled: {Steps}");
                    //TinfoBox.EndNewThreadInfoBox();
                }
            }, TaskScheduler.Current);
        }
예제 #4
0
        private void AStarAlgorithm()
        {
            #region "Initiate"

            CancelProcessing       = false;
            ApplyingAStarAlgorithm = true;
            bool             AtLeastOnePathOpen = false;
            List <Rectangle> OpenList           = new List <Rectangle>();
            List <Rectangle> ClosedList         = new List <Rectangle>();
            ClearYellowAndCarTracks();
            ClearSearchPaths();
            List <List <Rectangle> > MasterPath = new List <List <Rectangle> >();

            Rectangle MasterStartPoint = (myGrid.Children.OfType <Rectangle>()).Where(r => (r.Fill as ImageBrush).ImageSource == GreenFlag.ImageSource).FirstOrDefault();
            Rectangle StartPoint       = MasterStartPoint;
            Rectangle Current          = StartPoint;

            List <Rectangle> AccumulatedEndPointList = new List <Rectangle>();
            List <Rectangle> CheckedEndPoints        = new List <Rectangle>();
            List <Rectangle> EndPointList            = CreateEndPointList(StartPoint, CheckedEndPoints);

            int       NumberOfEndPoints = EndPointList.Count;
            Rectangle EndPoint          = EndPointList.FirstOrDefault();

            #endregion

            #region "Starto nje ThreadedInfoBox me tregue procesimin"

            ThreadedInfoBox TinfoBox = new ThreadedInfoBox(WindowStartupLocation.Manual, 50, 250);
            TinfoBox.Canceled += (o) =>
            {
                if (o == Operation.Processing)
                {
                    CancelProcessing = true;
                }
            };
            TinfoBox.StartNewThreadInfoBox(Operation.Processing, "Processing path ...", this.Title);

            #endregion

            #region "Algoritmi"

MoveNext:

            OpenList.Add(Current);

            while (OpenList.Count != 0)
            {
                if (CancelProcessing)
                {
                    TinfoBox.EndNewThreadInfoBox(); return;
                }

                Current = OpenList.ApplyUncertainty(UncertaintyLevel);           // Merre Current rectangle amo me nji uncertainty

                if (NumberOfEndPoints != 0)
                {
                    EndPointList = CreateEndPointList(Current, CheckedEndPoints);     // Checkirati distancat me endpoints qe jane me afer Current
                    EndPoint     = EndPointList.FirstOrDefault();
                }

                if ((Current.Tag as RectangleParameter).GridPosition.Location == (EndPoint.Tag as RectangleParameter).GridPosition.Location)
                {
                    #region "EndPoint Found"

                    AtLeastOnePathOpen = true;
                    List <Rectangle> Path = new List <Rectangle>();
                    Path = ReconstructPath(StartPoint, EndPoint);

                    if (!MasterPath.Contains(Path))
                    {
                        MasterPath.Add(Path);
                    }

                    if (!AccumulatedEndPointList.Contains(EndPoint))
                    {
                        AccumulatedEndPointList.Add(EndPoint);
                    }

                    NumberOfEndPoints--;
                    if (NumberOfEndPoints > 0)
                    {
                        if (!CheckedEndPoints.Contains(EndPoint))
                        {
                            CheckedEndPoints.Add(EndPoint);
                        }

                        StartPoint = Current;

                        ClosedList.Clear();
                        OpenList.Clear();
                        goto MoveNext;
                    }
                    else if (NumberOfEndPoints == 0 && BackPathType == BackPathType.Shortest)
                    {
                        StartPoint  = Current;
                        EndPoint    = MasterStartPoint;
                        IsGoingBack = true;

                        ClosedList.Clear();
                        OpenList.Clear();
                        goto MoveNext;
                    }

                    IsGoingBack = false;
                    DrawPath(MasterPath, AccumulatedEndPointList, MasterStartPoint, TinfoBox, PathType.StartToStop);
                    return;

                    #endregion
                }

                OpenList.Remove(Current);
                ClosedList.Add(Current);

                List <Rectangle> NeighborRectangles = ((myGrid.Children.OfType <Rectangle>()).CalculateHValue(EndPoint)).
                                                      Where(r => IsNodeAdjacent(r, Current) &&
                                                            IsNodeWalkable(r)).ToList();

                foreach (Rectangle rect in NeighborRectangles.Except(ClosedList))
                {
                    if (IsNodeDiagonal(rect, Current))
                    {
                        continue;           // Mos i merr parasysh levizjen ne diagonale
                    }
                    if ((rect.Fill as ImageBrush).ImageSource != GreenFlag.ImageSource && (rect.Fill as ImageBrush).ImageSource != RedFlag.ImageSource && (rect.Fill as ImageBrush).ImageSource != RedFlagChecked.ImageSource)
                    {
                        rect.Fill = IsGoingBack ? VioletSpace : CyanSpace;
                        AllowUIToUpdate();
                    }

                    if (!OpenList.Contains(rect))
                    {
                        OpenList.Add(rect);
                        (rect.Tag as RectangleParameter).NodeParameters.Parent = Current;
                        rect.CalculateGValue().CalculateFValue();
                    }
                    else
                    {
                        Rectangle temp = new Rectangle();
                        temp.Tag = new RectangleParameter()
                        {
                            GridPosition = new GridPosition((rect.Tag as RectangleParameter).GridPosition), NodeParameters = new NodeParameters(Current)
                        };
                        temp.CalculateGValue();

                        if ((temp.Tag as RectangleParameter).NodeParameters.G_Value < (rect.Tag as RectangleParameter).NodeParameters.G_Value)
                        {
                            (rect.Tag as RectangleParameter).NodeParameters.Parent = Current;
                            rect.CalculateFValue();
                        }
                    }
                }
            }

            #endregion

            #region "Handel 'EndPoint not found'"

            NumberOfEndPoints--;
            if (NumberOfEndPoints > 0)
            {
                if (!CheckedEndPoints.Contains(EndPoint))
                {
                    CheckedEndPoints.Add(EndPoint);
                }

                Current = StartPoint;
                ClosedList.Clear();
                ClosedList.Add(MasterStartPoint);
                OpenList.Clear();
                goto MoveNext;
            }
            else if (NumberOfEndPoints == 0 && AtLeastOnePathOpen)
            {
                if (BackPathType == BackPathType.Shortest)
                {
                    StartPoint = AccumulatedEndPointList.LastOrDefault();
                    Current    = StartPoint;
                    EndPoint   = MasterStartPoint;

                    ClosedList.Clear();
                    OpenList.Clear();
                    goto MoveNext;
                }
                else if (BackPathType == BackPathType.Reversed)
                {
                    DrawPath(MasterPath, AccumulatedEndPointList, MasterStartPoint, TinfoBox, PathType.StartToStop);
                    return;
                }
            }

            ApplyingAStarAlgorithm = false;
            TinfoBox.EndNewThreadInfoBox();
            ShowMessage(false, PathType.StartToStop);

            #endregion
        }