/// <summary>
        /// creates an Activity Destination from its string representation
        /// </summary>
        /// <param name="lines">The lines.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="cellularFloor">The cellular floor.</param>
        /// <param name="tolerance">The tolerance.</param>
        /// <returns>ActivityDestination.</returns>
        /// <exception cref="System.ArgumentException">
        /// Activity does not include a name!
        /// or
        /// Failed to parse activity's engagement duration: " + lines[startIndex + 3]
        /// or
        /// Activity does not include cell origins!
        /// or
        /// Failed to set activity engagement duration!
        /// </exception>
        public static ActivityDestination FromString(List <string> lines, int startIndex, Length_Unit_Types unitType, CellularFloor cellularFloor, double tolerance = 0.0000001d)
        {
            string name = lines[startIndex];

            if (string.IsNullOrEmpty(lines[startIndex]) || string.IsNullOrWhiteSpace(lines[startIndex]))
            {
                throw new ArgumentException("Activity does not include a name!");
            }
            StateBase      state   = StateBase.FromStringRepresentation(lines[startIndex + 1]);
            BarrierPolygon barrier = BarrierPolygon.FromStringRepresentation(lines[startIndex + 2]);

            //unit converion
            UnitConversion.Transform(state.Location, unitType, cellularFloor.UnitType);
            UnitConversion.Transform(state.Velocity, unitType, cellularFloor.UnitType);
            UnitConversion.Transform(barrier.BoundaryPoints, unitType, cellularFloor.UnitType);

            var    strings = lines[startIndex + 3].Split(',');
            double min = 0, max = 0;

            if (!double.TryParse(strings[0], out min) || !double.TryParse(strings[1], out max))
            {
                throw new ArgumentException("Failed to parse activity's engagement duration: " + lines[startIndex + 3]);
            }
            HashSet <Cell> origins = new HashSet <Cell>();
            var            indices = cellularFloor.GetIndicesInsideBarrier(barrier, tolerance);

            if (indices.Count > 0)
            {
                foreach (var index in indices)
                {
                    Cell cell = cellularFloor.FindCell(index);
                    if (cell != null && cell.FieldOverlapState == OverlapState.Inside)
                    {
                        origins.Add(cell);
                    }
                }
            }
            if (origins.Count == 0)
            {
                throw new ArgumentException("Activity does not include cell origins!");
            }
            ActivityDestination dest = new ActivityDestination(name, origins, state, barrier);

            if (!dest.TrySetEngagementTime(min, max))
            {
                throw new ArgumentException("Failed to set activity engagement duration!");
            }
            return(dest);
        }
Beispiel #2
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;
        }