/// <summary>
        /// Creates an instance of BarrierPolygons from its string representation
        /// </summary>
        /// <param name="s">The string representation.</param>
        /// <returns>BarrierPolygons.</returns>
        /// <exception cref="ArgumentException">
        /// Cannot create a polygon from an empty string
        /// or
        /// Failed to parse polygon's closed property: " + strings[strings.Length - 1]
        /// or
        /// Failed to parse polygon's points: " + strings[2 * i] + ", " + strings[2 * i + 1]
        /// </exception>
        public static BarrierPolygon FromStringRepresentation(string s)
        {
            if (string.IsNullOrWhiteSpace(s) || string.IsNullOrEmpty(s))
            {
                throw new ArgumentException("Cannot create a polygon from an empty string");
            }
            var  strings = s.Split(',');
            bool isclosed;

            if (!bool.TryParse(strings[strings.Length - 1], out isclosed))
            {
                throw new ArgumentException("Failed to parse polygon's closed property: " + strings[strings.Length - 1]);
            }
            UV[] pnts = new UV[(strings.Length - 1) / 2];
            for (int i = 0; i < pnts.Length; i++)
            {
                double u = 0, v = 0;
                if (double.TryParse(strings[2 * i], out u) && double.TryParse(strings[2 * i + 1], out v))
                {
                    pnts[i] = new UV(double.Parse(strings[2 * i]), double.Parse(strings[2 * i + 1]));
                }
                else
                {
                    throw new ArgumentException("Failed to parse polygon's points: " + strings[2 * i] + ", " + strings[2 * i + 1]);
                }
            }
            BarrierPolygon polygon = new BarrierPolygon(pnts);

            polygon.IsClosed = isclosed;
            strings          = null;
            return(polygon);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PLine"/> class. This method does not include any rounding to test equality!
 /// </summary>
 /// <param name="points">The points.</param>
 /// <param name="closed">if set to <c>true</c> the polyline is closed.</param>
 /// <exception cref="ArgumentException">Points are duplicated</exception>
 //public PLine(List<UV> points, bool closed)
 //{
 //    this._end = points[points.Count - 1];
 //    this._start = points[0];
 //    this._pntList = points;
 //    this._pntSet = new HashSet<UV>();//(new UVComparer(fractionalDigits));
 //    foreach (var item in this._pntList)
 //    {
 //        if (this._pntSet.Contains(item))
 //        {
 //            throw new ArgumentException("Points are duplicated");
 //        }
 //        else
 //        {
 //            this._pntSet.Add(item);
 //        }
 //    }
 //    this.Closed = closed;
 //}
 /// <summary>
 /// Determines whether this instance is convex.
 /// </summary>
 /// <returns><c>true</c> if this instance is convex; otherwise, <c>false</c>.</returns>
 public bool IsConvex()
 {
     return(BarrierPolygon.IsConvex(this._pntList.ToArray()));
 }
        void _close_Click(object sender, RoutedEventArgs e)
        {
            HashSet <Index> allIndices = new HashSet <Index>();

            foreach (SpatialAnalysis.Geometry.BarrierPolygon item in this._barriers)
            {
                allIndices.UnionWith(this._host.cellularFloor.GetIndicesInsideBarrier(item, 0.0000001));
            }
            var visibleCells = new HashSet <int>();

            foreach (Index item in allIndices)
            {
                if (this._host.cellularFloor.ContainsCell(item) &&
                    this._host.cellularFloor.Cells[item.I, item.J].VisualOverlapState == OverlapState.Outside)
                {
                    visibleCells.Add(this._host.cellularFloor.Cells[item.I, item.J].ID);
                }
            }
            allIndices.Clear();
            allIndices = null;
            foreach (Cell item in this._destinations)
            {
                if (item.VisualOverlapState == OverlapState.Outside)
                {
                    visibleCells.Add(item.ID);
                }
            }
            if (visibleCells.Count == 0)
            {
                MessageBox.Show("Cannot Proceed without Setting Visibility Targets!");
                return;
            }
            this._settings._panel1.Visibility = this._settings._panel2.Visibility = System.Windows.Visibility.Collapsed;
            this._settings._panel4.Visibility = System.Windows.Visibility.Visible;
            var            cellsOnEdge = CellUtility.GetEdgeOfField(this._host.cellularFloor, visibleCells);
            List <Isovist> isovists    = new List <Isovist>(cellsOnEdge.Count);

            this._settings.progressBar.Maximum = cellsOnEdge.Count;
            this._settings.progressBar.Minimum = 0;
            double depth = this._host.cellularFloor.Origin.DistanceTo(this._host.cellularFloor.TopRight) + 1;

            foreach (int item in cellsOnEdge)
            {
                isovists.Add(new Isovist(this._host.cellularFloor.FindCell(item)));
            }
            var timer = new System.Diagnostics.Stopwatch();

            timer.Start();
            Parallel.ForEach(isovists, (a) =>
            {
                a.Compute(depth, BarrierType.Visual, this._host.cellularFloor, 0.0000001);
                Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                       (SendOrPostCallback) delegate
                {
                    double val = this._settings.progressBar.Value + 1;
                    this._settings.progressBar.SetValue(ProgressBar.ValueProperty, val);
                }
                                       , null);
                Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                       (SendOrPostCallback) delegate
                {
                    double x       = this._settings.progressBar.Value / this._settings.progressBar.Maximum;
                    int percent    = (int)(x * 100);
                    int minutes    = (int)Math.Floor(timer.Elapsed.TotalMinutes);
                    int seconds    = (int)(timer.Elapsed.Seconds);
                    string message = string.Format("{0} minutes and {1} seconds\n%{2} completed",
                                                   minutes.ToString(), seconds.ToString(), percent.ToString());
                    this._settings.IsovistPrgressReport.SetValue(TextBlock.TextProperty, message);
                }
                                       , null);
                Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { })).Wait(TimeSpan.FromMilliseconds(1));
            });
            var t = timer.Elapsed.TotalMilliseconds;

            timer.Stop();
            HashSet <int> visibleArea = new HashSet <int>();

            foreach (Isovist item in isovists)
            {
                visibleArea.UnionWith(item.VisibleCells);
            }

            foreach (Cell item in this._destinations)
            {
                UV  p1      = item;
                UV  p2      = item + this._host.cellularFloor.CellSize * UV.UBase;
                UV  p3      = item + this._host.cellularFloor.CellSize * (UV.UBase + UV.VBase);
                UV  p4      = item + this._host.cellularFloor.CellSize * UV.VBase;
                var polygon = new SpatialAnalysis.Geometry.BarrierPolygon(new UV[4] {
                    p1, p2, p3, p4
                });
                this._barriers.Add(polygon);
            }
            this._host.VisualEventSettings = new VisibilityTarget(visibleArea,
                                                                  isovists, this._barriers.ToArray());
            #region visualization
            double _h = ((UIElement)this.Parent).RenderSize.Height;
            double _w = ((UIElement)this.Parent).RenderSize.Width;
            this._host.agentVisiblArea.Source = null;
            WriteableBitmap _view = BitmapFactory.New((int)_w, (int)_h);
            this._host.agentVisiblArea.Source     = _view;
            this._host.agentVisiblArea.Visibility = System.Windows.Visibility.Visible;
            this._showVisibleArea.Header          = "Hide Visible Area";
            switch (this._settings._colorCode.IsChecked.Value)
            {
            case true:
                using (_view.GetBitmapContext())
                {
                    int max = int.MinValue;
                    foreach (var item in this._host.VisualEventSettings.ReferencedVantageCells.Values)
                    {
                        if (max < item.Count)
                        {
                            max = item.Count;
                        }
                    }
                    byte  alpha  = (byte)(255 * 0.4);
                    Color yellow = Color.Add(Colors.Yellow, Color.Multiply(Colors.Red, 4.0f));
                    yellow.ScA = 0.7f;
                    foreach (int cellID in visibleArea)
                    {
                        Cell  item = this._host.cellularFloor.FindCell(cellID);
                        Point p1   = this._host.Transform(item);
                        Point p2   = this._host.Transform(item + new UV(this._host.cellularFloor.CellSize, this._host.cellularFloor.CellSize));
                        _view.FillRectangle((int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, GetColor(this._host.VisualEventSettings.ReferencedVantageCells[cellID].Count, max, alpha));
                    }
                    foreach (int cellID in visibleCells)
                    {
                        Cell  item = this._host.cellularFloor.FindCell(cellID);
                        Point p1   = this._host.Transform(item);
                        Point p2   = this._host.Transform(item + new UV(this._host.cellularFloor.CellSize, this._host.cellularFloor.CellSize));
                        _view.FillRectangle((int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, yellow);
                    }
                }
                break;

            case false:
                using (_view.GetBitmapContext())
                {
                    Color green = Colors.GreenYellow;
                    green.ScA = 0.4f;
                    Color yellow = Color.Add(Colors.Yellow, Color.Multiply(Colors.Red, 4.0f));
                    yellow.ScA = 0.7f;
                    foreach (int cellID in visibleArea)
                    {
                        Cell  item = this._host.cellularFloor.FindCell(cellID);
                        Point p1   = this._host.Transform(item);
                        Point p2   = this._host.Transform(item + new UV(this._host.cellularFloor.CellSize, this._host.cellularFloor.CellSize));
                        _view.FillRectangle((int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, green);
                    }
                    foreach (int cellID in visibleCells)
                    {
                        Cell  item = this._host.cellularFloor.FindCell(cellID);
                        Point p1   = this._host.Transform(item);
                        Point p2   = this._host.Transform(item + new UV(this._host.cellularFloor.CellSize, this._host.cellularFloor.CellSize));
                        _view.FillRectangle((int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y, yellow);
                    }
                }
                break;
            }
            #endregion
            //cleanup
            isovists.Clear();
            isovists = null;
            this._settings.Close();
        }
 /// <summary>
 /// Determines whether this instance is convex.
 /// </summary>
 /// <returns><c>true</c> if this instance is convex; otherwise, <c>false</c>.</returns>
 public bool IsConvex()
 {
     return(BarrierPolygon.IsConvex(this.BoundaryPoints));
 }