public MyQuadtree <HabitatFieldInTree> QueryMap(MyRectangle queryArea) { var outTree = new MyQuadtree <HabitatFieldInTree>(); Vector2 reoffsetStartPosition = new Vector2(queryArea.X, queryArea.Y) - _mapStartPosition; Vector2 reoffsetEndPosition = new Vector2(queryArea.X + queryArea.Width, queryArea.Y + queryArea.Height) - _mapStartPosition; var startGridX = Mathf.FloorToInt(reoffsetStartPosition.x / _mapGridSize.x); var startGridY = Mathf.FloorToInt(reoffsetStartPosition.y / _mapGridSize.y); var endGridX = Mathf.Min(_gridCellsCount.X, Mathf.CeilToInt(reoffsetEndPosition.x / _mapGridSize.x)); var endGridY = Mathf.Min(_gridCellsCount.Y, Mathf.CeilToInt(reoffsetEndPosition.y / _mapGridSize.y)); if (startGridX < 0 || startGridY < 0 || endGridX > _gridCellsCount.X || endGridY > _gridCellsCount.Y) { Preconditions.Fail( $"Querying area {queryArea}, makes grids query: x:{startGridX} - {endGridX} y:{startGridY}-{endGridY} which is out of size {_gridCellsCount}"); } var queryEnvelope = MyNetTopologySuiteUtils.ToGeometryEnvelope(queryArea); for (var gridX = startGridX; gridX < endGridX; gridX++) { for (var gridY = startGridY; gridY < endGridY; gridY++) { //todo return other structure - one that uses gridded tree in array List <HabitatFieldInTree> treeElements = null; treeElements = _treeArray[gridX, gridY].QueryAll().Select(c => new { intersection = c.Field.Geometry.Intersection(queryEnvelope), elem = c }) .Where(c => !c.intersection.IsEmpty && !(c.intersection is ILineString)) .SelectMany(c => MyNetTopologySuiteUtils.ToSinglePolygons(c.intersection).Select( k => new HabitatFieldInTree() { Field = new HabitatField() { Geometry = k, Type = c.elem.Field.Type } })).ToList(); treeElements.ForEach(c => outTree.Add(c)); } } return(outTree); }
public List <VegetationSubjectEntity> GetEntiesFrom(IGeometry area, VegetationDetailLevel level) { var outPositions = new List <Vector2>(); if (level == VegetationDetailLevel.FULL) { var envelope = area.EnvelopeInternal; var gridPositionsStart = new IntVector2( Mathf.CeilToInt((float)envelope.MinX / _configuration.PositionsGridSize.x), Mathf.CeilToInt((float)envelope.MinY / _configuration.PositionsGridSize.y)); var afterGridingLength = new Vector2( (float)(envelope.MaxX - gridPositionsStart.X * _configuration.PositionsGridSize.x), (float)(envelope.MaxY - gridPositionsStart.Y * _configuration.PositionsGridSize.y)); var gridLength = new IntVector2( //ceil becouse there is point at length 0 !! Mathf.CeilToInt(afterGridingLength.x / _configuration.PositionsGridSize.x), Mathf.CeilToInt(afterGridingLength.y / _configuration.PositionsGridSize.y)); for (int x = 0; x < gridLength.X; x++) { for (int y = 0; y < gridLength.Y; y++) { var position = new Vector2( (0.5f + (gridPositionsStart.X + x)) * _configuration.PositionsGridSize.x, (0.5f + (gridPositionsStart.Y + y)) * _configuration.PositionsGridSize.y); outPositions.Add(position); } } } return(outPositions.Where(c => area.Contains(MyNetTopologySuiteUtils.ToGeometryEnvelope( MyNetTopologySuiteUtils.ToPointEnvelope(c)))).Select(c => new VegetationSubjectEntity( new DesignBodyLevel0Detail() { Pos2D = c, Radius = 0, Size = 0, SpeciesEnum = VegetationSpeciesEnum.Grass2SpotMarker })).ToList()); }
public static HabitatMap Create( MyRectangle areaOnMap, Vector2 mapGridSize, List <HabitatField> habitatFields, HabitatType defaultHabitatType, HabitatTypePriorityResolver priorityResolver) { var fullTree = new MyQuadtree <HabitatFieldInTree>(); habitatFields.ForEach(c => fullTree.Add(new HabitatFieldInTree() { Field = c })); int gridXCount = Mathf.CeilToInt(areaOnMap.Width / mapGridSize.x); int gridYCount = Mathf.CeilToInt(areaOnMap.Height / mapGridSize.y); var gridTreeArray = new MyQuadtree <HabitatFieldInTree> [gridXCount, gridYCount]; for (int gridX = 0; gridX < gridXCount; gridX++) { for (int gridY = 0; gridY < gridYCount; gridY++) { var gridArea = new MyRectangle( areaOnMap.X + gridX * mapGridSize.x, areaOnMap.Y + gridY * mapGridSize.y, mapGridSize.x, mapGridSize.y); var geometryEnvelope = MyNetTopologySuiteUtils.ToGeometryEnvelope(gridArea); var fieldsInArea = fullTree.QueryWithIntersection(geometryEnvelope); //Debug.Log("U34: "+fieldsInArea.Count(c => c.Field.Type == HabitatType.Fell)); //if (fieldsInArea.Count(c => c.Field.Type == HabitatType.Fell) > 0 ) //{ // Debug.Log($"J2: {StringUtils.ToString(fieldsInArea.Where(c => c.Field.Type == HabitatType.Fell).Select(c => c.Field.Geometry.AsText()))}"); //} var fieldsInEnvelope = fieldsInArea.Select(c => new HabitatField() { Geometry = MySafeIntersection(c.Field.Geometry, geometryEnvelope), Type = c.Field.Type }).Where(c => c.Geometry != null && !c.Geometry.IsEmpty); // removal of elements one on other var sortedFields = fieldsInEnvelope.OrderByDescending(c => priorityResolver.Resolve(c.Type)) .ToList(); for (int i = 0; i < sortedFields.Count; i++) { var current = sortedFields[i]; for (int j = i + 1; j < sortedFields.Count; j++) { var other = sortedFields[j]; other.Geometry = other.Geometry.Difference(current.Geometry); } } sortedFields = sortedFields.Where(c => !c.Geometry.IsEmpty).ToList(); var notUsedAreaInGrid = geometryEnvelope; foreach (var cutGeometry in sortedFields.Select(c => c.Geometry)) { notUsedAreaInGrid = notUsedAreaInGrid.Difference(cutGeometry); } if (!notUsedAreaInGrid.IsEmpty) { sortedFields.Add( new HabitatField() { Type = defaultHabitatType, Geometry = notUsedAreaInGrid } ); } var singleFieldsInTree = sortedFields.SelectMany(c => MyNetTopologySuiteUtils .ToSinglePolygons(c.Geometry).Select(poly => new HabitatField() { Geometry = poly, Type = c.Type })).Select(c => new HabitatFieldInTree() { Field = c }).ToList(); gridTreeArray[gridX, gridY] = MyQuadtree <HabitatFieldInTree> .CreateWithElements(singleFieldsInTree); } } return(new HabitatMap( new Vector2(areaOnMap.X, areaOnMap.Y), mapGridSize, new IntVector2(gridXCount, gridYCount), gridTreeArray)); }
private async Task <Dictionary <HabitatType, Texture2D> > CreateHabitatTexturesDict( Vector2 startOffset, IntVector2 habitatTextureSize, List <HabitatFieldInTree> habitats) { var habitatTextureTemplatesDict = new Dictionary <HabitatType, MyTextureTemplate>(); var allTypes = habitats.Select(c => c.Field.Type).Distinct(); foreach (var type in allTypes) { //var newTex = new Texture2D(habitatTextureSize.X, habitatTextureSize.Y, TextureFormat.RGB24, false, true); var newTexTemplate = new MyTextureTemplate(habitatTextureSize.X, habitatTextureSize.Y, TextureFormat.ARGB32, false, FilterMode.Bilinear) { wrapMode = TextureWrapMode.Clamp }; habitatTextureTemplatesDict[type] = newTexTemplate; } var enlargedTree = GenerateEnlargedTree(habitats.Select(c => c.Field).ToList()); for (int x = 0; x < habitatTextureSize.X; x++) { for (int y = 0; y < habitatTextureSize.Y; y++) { var centerPoint = new Vector2( (x + 0.5f) * (_configuration.HabitatSamplingUnit), (y + 0.5f) * (_configuration.HabitatSamplingUnit) ) + startOffset; var pointGeometry = MyNetTopologySuiteUtils.ToGeometryEnvelope(MyNetTopologySuiteUtils .ToPointEnvelope(centerPoint)); var marginTouchingHabitats = enlargedTree.QueryWithIntersection(pointGeometry); foreach (var texture in habitatTextureTemplatesDict.Values) { texture.SetPixel(x, y, new Color(0, 0, 0)); } foreach (var habitatType in marginTouchingHabitats.Select(c => c.Type).Distinct()) { var maxIntensityValue = 0f; foreach (var habitat in marginTouchingHabitats.Where(c => c.Type == habitatType)) { if (habitat.StandardHabitatField.Contains(pointGeometry)) { maxIntensityValue = 1; } else { var distance = MyNetTopologySuiteUtils.Distance(pointGeometry, habitat.StandardHabitatField, _configuration.HabitatMargin); maxIntensityValue = Mathf.Max(maxIntensityValue, Mathf.Clamp01(1 - (distance / _configuration.HabitatMargin))); } } habitatTextureTemplatesDict[habitatType].SetPixel(x, y, new Color(maxIntensityValue, 0, 0)); } } } var renderedTextures = await TaskUtils.WhenAll( habitatTextureTemplatesDict.Values .Select(async(c) => await _textureConciever.ConcieveTextureAsync(c))); var outDict = Enumerable.Range(0, renderedTextures.Count) .ToDictionary(i => habitatTextureTemplatesDict.Keys.ToList()[i], i => renderedTextures[i]); return(outDict); }
public bool Intersects(IGeometry geometry) { return(MyNetTopologySuiteUtils.ToGeometryEnvelope(CoordedHeightArray.Coords).Intersects(geometry)); }