void _generateDataTermination_Click(object sender, RoutedEventArgs e)
        {
            #region Input validation
            if (string.IsNullOrEmpty(this._settings._dataName.Text))
            {
                MessageBox.Show("Enter Data Field Name");
                return;
            }
            if (this._host.cellularFloor.AllSpatialDataFields.ContainsKey(this._settings._dataName.Text))
            {
                MessageBox.Show("The Data Field Name Exists. Try a Different Name...");
                return;
            }
            double _in = 0, _out = 0, _on = 0;
            if (!double.TryParse(this._settings._valueOfVantageCells.Text, out _on))
            {
                MessageBox.Show("Value of visibility vantage areas is invalid!");
                return;
            }
            if (!double.TryParse(this._settings._valueOutside.Text, out _out))
            {
                MessageBox.Show("Value of not visible areas is invalid!");
                return;
            }
            if (!this._settings._interpolationMethod.IsChecked.Value && !double.TryParse(this._settings._constantValue.Text, out _in))
            {
                MessageBox.Show("Value of visible areas is invalid!");
                return;
            }
            if (this._settings._interpolationMethod.IsChecked.Value)
            {
                //test the interpolation function
                if (!this._settings.LoadFunction())
                {
                    return;
                }
            }
            #endregion

            #region get the vantage Cells
            HashSet <Index> allIndices = new HashSet <Index>();
            foreach (SpatialAnalysis.Geometry.BarrierPolygon item in this._barriers)
            {
                allIndices.UnionWith(this._host.cellularFloor.GetIndicesInsideBarrier(item, OSMDocument.AbsoluteTolerance));
            }
            var vantageCells           = 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)
                {
                    vantageCells.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)
                {
                    vantageCells.Add(item.ID);
                }
            }
            if (vantageCells.Count == 0)
            {
                MessageBox.Show("Cannot Proceed without Setting Visibility Targets!");
                return;
            }
            #endregion


            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, vantageCells);
            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);
            }
            if (this._settings._interpolationMethod.IsChecked.Value)
            {
                //HashSet<int> notVisibleCellIds = new HashSet<int>();
                //foreach (var item in this._host.cellularFloor.Cells)
                //{
                //    if (item.FieldOverlapState != OverlapState.Outside)
                //    {
                //        if (!vantageCells.Contains(item.ID) && !visibleArea.Contains(item.ID))
                //        {
                //            notVisibleCellIds.Add(item.ID);
                //        }
                //    }
                //}
                //var newData = this._host.FieldGenerator.GetSpatialDataField(vantageCells, _on, notVisibleCellIds, _out, this._settings._dataName.Text, this._settings.InterpolationFunction);
                //this._host.cellularFloor.AddSpatialDataField(newData);
                Dictionary <Cell, double> data = new Dictionary <Cell, double>();
                foreach (var item in this._host.cellularFloor.Cells)
                {
                    if (item.FieldOverlapState != OverlapState.Outside)
                    {
                        if (vantageCells.Contains(item.ID))
                        {
                            data.Add(item, _on);
                        }
                        else
                        {
                            if (visibleArea.Contains(item.ID))
                            {
                                UV     visiblePoint = this._host.cellularFloor.FindCell(item.ID);
                                double dist         = double.PositiveInfinity;
                                foreach (var id in cellsOnEdge)
                                {
                                    var cell = this._host.cellularFloor.FindCell(id);
                                    dist = Math.Min(UV.GetLengthSquared(cell, visiblePoint), dist);
                                }
                                double value = _on + this._settings.InterpolationFunction(Math.Sqrt(dist));
                                data.Add(item, Math.Max(value, _out));
                            }
                            else
                            {
                                data.Add(item, _out);
                            }
                        }
                    }
                }
                Data.SpatialDataField newData = new Data.SpatialDataField(this._settings._dataName.Text, data);
                this._host.cellularFloor.AddSpatialDataField(newData);
            }
            else
            {
                Dictionary <Cell, double> data = new Dictionary <Cell, double>();
                foreach (var item in this._host.cellularFloor.Cells)
                {
                    if (item.FieldOverlapState != OverlapState.Outside)
                    {
                        if (vantageCells.Contains(item.ID))
                        {
                            data.Add(item, _on);
                        }
                        else
                        {
                            if (visibleArea.Contains(item.ID))
                            {
                                data.Add(item, _in);
                            }
                            else
                            {
                                data.Add(item, _out);
                            }
                        }
                    }
                }
                Data.SpatialDataField newData = new Data.SpatialDataField(this._settings._dataName.Text, data);
                this._host.cellularFloor.AddSpatialDataField(newData);
            }

            this._settings.Close();
        }
Esempio n. 2
0
 /// <summary>
 /// Gets the cell IDs at the edges of the visibility area.
 /// </summary>
 /// <param name="cellularFloor">The cellularFloor.</param>
 /// <returns>HashSet&lt;System.Int32&gt;.</returns>
 public HashSet <int> GetIsovistEdge(CellularFloor cellularFloor)
 {
     return(CellUtility.GetEdgeOfField(cellularFloor, this.VisibleCells));
 }
        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();
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VisibilityEvaluationEvent"/> class.
        /// </summary>
        /// <param name="visualTargets">The visual targets.</param>
        /// <param name="cellularFloor">The cellular floor.</param>
        /// <param name="tolerance">The tolerance by default set to the main document's absolute tolerance value.</param>
        /// <exception cref="System.ArgumentException">Cannot generate 'Occupancy Visual Event' with no visibility target cells!</exception>
        public VisibilityTarget(ICollection <SpatialAnalysis.Geometry.BarrierPolygon> visualTargets, CellularFloor cellularFloor, double tolerance = OSMDocument.AbsoluteTolerance)
        {
            this.VisualTargets = visualTargets.ToArray();
            HashSet <Index> allIndices = new HashSet <Index>();

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

            foreach (Index item in allIndices)
            {
                if (cellularFloor.ContainsCell(item) &&
                    cellularFloor.Cells[item.I, item.J].VisualOverlapState == OverlapState.Outside)
                {
                    visibleCells.Add(cellularFloor.Cells[item.I, item.J].ID);
                }
            }
            allIndices.Clear();
            allIndices = null;
            if (visibleCells.Count == 0)
            {
                throw new ArgumentException("Cannot generate 'Occupancy Visual Event' with no visibility target cells!");
            }
            var            cellsOnEdge = CellUtility.GetEdgeOfField(cellularFloor, visibleCells);
            List <Isovist> isovists    = new List <Isovist>(cellsOnEdge.Count);
            double         depth       = cellularFloor.Origin.DistanceTo(cellularFloor.TopRight) + 1;

            foreach (int item in cellsOnEdge)
            {
                isovists.Add(new Isovist(cellularFloor.FindCell(item)));
            }
            Parallel.ForEach(isovists, (a) =>
            {
                a.Compute(depth, BarrierType.Visual, cellularFloor, 0.0000001);
            });
            HashSet <int> visibleArea = new HashSet <int>();

            foreach (Isovist item in isovists)
            {
                visibleArea.UnionWith(item.VisibleCells);
            }
            this.AllVisibleCells        = visibleArea;
            this.ReferencedVantageCells = new Dictionary <int, List <int> >();
            var vantageCells = new List <int>(isovists.Count);

            foreach (var item in this.AllVisibleCells)
            {
                this.ReferencedVantageCells.Add(item, new List <int>());
            }
            foreach (var isovist in isovists)
            {
                vantageCells.Add(isovist.VantageCell.ID);
                foreach (var cellID in isovist.VisibleCells)
                {
                    this.ReferencedVantageCells[cellID].Add(isovist.VantageCell.ID);
                }
            }
            this.VantageCells = vantageCells;
        }