Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default areas generation #112

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GameRealisticMap.Arma3/Arma3LayerGeneratorCatalog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GameRealisticMap.Arma3.Assets;
using GameRealisticMap.Arma3.ManMade;
using GameRealisticMap.Arma3.ManMade.Farmlands;
using GameRealisticMap.Arma3.Nature.DefaultAreas;
using GameRealisticMap.Arma3.Nature.Forests;
using GameRealisticMap.Arma3.Nature.Lakes;
using GameRealisticMap.Arma3.Nature.RockAreas;
Expand Down Expand Up @@ -38,6 +39,7 @@ public Arma3LayerGeneratorCatalog(IProgressSystem progress, IArma3RegionAssets a
Generators.Add(new TreeRowsGenerator(progress, assets));
Generators.Add(new WatercourseGenerator(progress, assets));
Generators.Add(new WatercourseRadialGenerator(progress, assets));
Generators.Add(new DefaultAreasGenerator(progress, assets));
}

public List<ITerrainBuilderLayerGenerator> Generators { get; } = new List<ITerrainBuilderLayerGenerator>();
Expand Down
2 changes: 1 addition & 1 deletion GameRealisticMap.Arma3/Nature/BasicGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace GameRealisticMap.Arma3.Nature
{
internal abstract class BasicGeneratorBase<TData> : GeneratorBase<TData> where TData : class, IBasicTerrainData
internal abstract class BasicGeneratorBase<TData> : GeneratorBase<TData> where TData : class, IPolygonTerrainData
{
public BasicGeneratorBase(IProgressSystem progress, IArma3RegionAssets assets)
: base(progress, assets)
Expand Down
4 changes: 1 addition & 3 deletions GameRealisticMap.Arma3/Nature/ClusteredGeneratorBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Numerics;
using GameRealisticMap.Algorithms;
using GameRealisticMap.Algorithms;
using GameRealisticMap.Algorithms.Filling;
using GameRealisticMap.Arma3.Assets;
using GameRealisticMap.Arma3.Assets.Filling;
using GameRealisticMap.Arma3.TerrainBuilder;
using GameRealisticMap.Geometries;
using GameRealisticMap.Nature;
using GameRealisticMap.Reporting;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GameRealisticMap.Arma3.Assets;
using GameRealisticMap.Arma3.Assets.Filling;
using GameRealisticMap.Nature.DefaultAreas;
using GameRealisticMap.Reporting;

namespace GameRealisticMap.Arma3.Nature.DefaultAreas
{
internal class DefaultAreasGenerator : BasicGeneratorBase<DefaultAreasData>
{
public DefaultAreasGenerator(IProgressSystem progress, IArma3RegionAssets assets)
: base(progress, assets)
{
}

protected override bool ShouldGenerate => assets.GetBasicCollections(Id).Count > 0;

protected override BasicCollectionId Id => BasicCollectionId.DefaultAreas;
}
}
10 changes: 8 additions & 2 deletions GameRealisticMap.Arma3/Nature/GeneratorBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Numerics;
using GameRealisticMap.Algorithms;
using GameRealisticMap.Algorithms.Filling;
using GameRealisticMap.Arma3.Assets;
using GameRealisticMap.Arma3.TerrainBuilder;
using GameRealisticMap.Geometries;
Expand All @@ -10,7 +9,7 @@
namespace GameRealisticMap.Arma3.Nature
{
public abstract class GeneratorBase<TData> : ITerrainBuilderLayerGenerator
where TData : class, IBasicTerrainData
where TData : class, IPolygonTerrainData
{
protected readonly IProgressSystem progress;
protected readonly IArma3RegionAssets assets;
Expand All @@ -21,8 +20,15 @@ public GeneratorBase(IProgressSystem progress, IArma3RegionAssets assets)
this.assets = assets;
}

protected virtual bool ShouldGenerate => true;

public IEnumerable<TerrainBuilderObject> Generate(IArma3MapConfig config, IContext context)
{
if (!ShouldGenerate)
{
return new List<TerrainBuilderObject>(0);
}

using var scope = progress.CreateScope(GetType().Name.Replace("Generator",""));

var polygons = context.GetData<TData>().Polygons;
Expand Down
8 changes: 7 additions & 1 deletion GameRealisticMap/BuildContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GameRealisticMap.Osm;
using System.Diagnostics;
using GameRealisticMap.Osm;
using GameRealisticMap.Reporting;
using HugeImages.Storage;

Expand Down Expand Up @@ -52,6 +53,11 @@ public T GetData<T>()
}
}

public IEnumerable<T> GetOfType<T>() where T : class
{
return catalog.GetOfType<T>(this);
}

public void SetData<T>(T value)
where T : class
{
Expand Down
3 changes: 3 additions & 0 deletions GameRealisticMap/BuildersCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using GameRealisticMap.ManMade.Railways;
using GameRealisticMap.ManMade.Roads;
using GameRealisticMap.ManMade.Roads.Libraries;
using GameRealisticMap.Nature;
using GameRealisticMap.Nature.DefaultAreas;
using GameRealisticMap.Nature.Forests;
using GameRealisticMap.Nature.Lakes;
using GameRealisticMap.Nature.Ocean;
Expand Down Expand Up @@ -57,6 +59,7 @@ public BuildersCatalog(IProgressSystem progress, IRoadTypeLibrary<IRoadTypeInfos
Register(new VineyardBuilder(progress));
Register(new OrchardBuilder(progress));
Register(new TreeRowsBuilder(progress));
Register(new DefaultAreasBuilder(progress));
}

public void Register<TData>(IDataBuilder<TData> builder)
Expand Down
2 changes: 2 additions & 0 deletions GameRealisticMap/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface IContext
{
T GetData<T>() where T : class;

IEnumerable<T> GetOfType<T>() where T : class;

IHugeImageStorage HugeImageStorage { get; }
}
}
8 changes: 7 additions & 1 deletion GameRealisticMap/IO/ContextReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HugeImages.Storage;
using System.Diagnostics;
using HugeImages.Storage;

namespace GameRealisticMap.IO
{
Expand Down Expand Up @@ -31,6 +32,11 @@ public T GetData<T>() where T : class
return result;
}

public IEnumerable<T> GetOfType<T>() where T : class
{
return catalog.GetOfType<T>(this);
}

public async Task Visit<TData>(IDataBuilder<TData> builder) where TData : class
{
if (!cache.ContainsKey(typeof(TData)))
Expand Down
5 changes: 4 additions & 1 deletion GameRealisticMap/ManMade/Buildings/BuildingsData.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Text.Json.Serialization;
using GameRealisticMap.Geometries;
using GameRealisticMap.Nature;
using GeoJSON.Text.Feature;
using GeoJSON.Text.Geometry;

namespace GameRealisticMap.ManMade.Buildings
{
public class BuildingsData : IGeoJsonData
public class BuildingsData : IGeoJsonData, INonDefaultArea
{
[JsonConstructor]
public BuildingsData(List<Building> buildings)
Expand All @@ -15,6 +16,8 @@ public BuildingsData(List<Building> buildings)

public List<Building> Buildings { get; }

IEnumerable<TerrainPolygon> INonDefaultArea.Polygons => Buildings.Select(b => b.Box.Polygon);

public IEnumerable<Feature> ToGeoJson(Func<TerrainPoint, IPosition> project)
{
var entrance = new Dictionary<string, object>() {
Expand Down
8 changes: 6 additions & 2 deletions GameRealisticMap/ManMade/CategoryAreaData.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using GeoJSON.Text.Feature;
using GameRealisticMap.Geometries;
using GameRealisticMap.Nature;
using GeoJSON.Text.Feature;

namespace GameRealisticMap.ManMade
{
public class CategoryAreaData
public class CategoryAreaData : INonDefaultArea
{
public CategoryAreaData(List<CategoryArea> areas)
{
Areas = areas;
}

public List<CategoryArea> Areas { get; }

IEnumerable<TerrainPolygon> INonDefaultArea.Polygons => Areas.SelectMany(a => a.PolyList);
}
}
5 changes: 4 additions & 1 deletion GameRealisticMap/ManMade/Railways/RailwaysData.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Text.Json.Serialization;
using GameRealisticMap.Geometries;
using GameRealisticMap.Nature;
using GeoJSON.Text.Feature;
using GeoJSON.Text.Geometry;

namespace GameRealisticMap.ManMade.Railways
{
public class RailwaysData : IGeoJsonData
public class RailwaysData : IGeoJsonData, INonDefaultArea
{
[JsonConstructor]
public RailwaysData(List<Railway> railways)
Expand All @@ -15,6 +16,8 @@ public RailwaysData(List<Railway> railways)

public List<Railway> Railways { get; }

IEnumerable<TerrainPolygon> INonDefaultArea.Polygons => Railways.SelectMany(x => x.ClearPolygons);

public IEnumerable<Feature> ToGeoJson(Func<TerrainPoint, IPosition> project)
{
var properties = new Dictionary<string, object>() {
Expand Down
5 changes: 4 additions & 1 deletion GameRealisticMap/ManMade/Roads/RoadsData.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Text.Json.Serialization;
using GameRealisticMap.Geometries;
using GameRealisticMap.Nature;
using GeoJSON.Text.Feature;
using GeoJSON.Text.Geometry;

namespace GameRealisticMap.ManMade.Roads
{
public class RoadsData : IGeoJsonData
public class RoadsData : IGeoJsonData, INonDefaultArea
{
[JsonConstructor]
public RoadsData(List<Road> roads)
Expand All @@ -15,6 +16,8 @@ public RoadsData(List<Road> roads)

public List<Road> Roads { get; }

IEnumerable<TerrainPolygon> INonDefaultArea.Polygons => Roads.SelectMany(x => x.ClearPolygons);

public IEnumerable<Feature> ToGeoJson(Func<TerrainPoint, IPosition> project)
{
return Roads.Select(r => new Feature(new MultiPolygon(r.Polygons.Select(p => p.ToGeoJson(project))), new Dictionary<string, object>() {
Expand Down
26 changes: 26 additions & 0 deletions GameRealisticMap/Nature/DefaultAreas/DefaultAreasBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using GameRealisticMap.Geometries;
using GameRealisticMap.Reporting;

namespace GameRealisticMap.Nature.DefaultAreas
{
internal class DefaultAreasBuilder : IDataBuilder<DefaultAreasData>
{
private readonly IProgressSystem progress;

public DefaultAreasBuilder(IProgressSystem progress)
{
this.progress = progress;
}

public DefaultAreasData Build(IBuildContext context)
{
var allData = context.GetOfType<INonDefaultArea>().ToList();
var allPolygons = allData.ProgressStep(progress, "Polygons")
.SelectMany(l => l.Polygons)
.ToList();
using var report = progress.CreateStep("SubstractAll", 1);
var polygons = context.Area.TerrainBounds.SubstractAllSplitted(allPolygons).ToList();
return new DefaultAreasData(polygons);
}
}
}
24 changes: 24 additions & 0 deletions GameRealisticMap/Nature/DefaultAreas/DefaultAreasData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using GameRealisticMap.Geometries;
using GeoJSON.Text.Feature;
using GeoJSON.Text.Geometry;

namespace GameRealisticMap.Nature.DefaultAreas
{
public class DefaultAreasData : IGeoJsonData, IPolygonTerrainData
{
public DefaultAreasData(List<TerrainPolygon> polygons)
{
Polygons = polygons;
}

public List<TerrainPolygon> Polygons { get; }

public IEnumerable<Feature> ToGeoJson(Func<TerrainPoint, IPosition> project)
{
var properties = new Dictionary<string, object>() {
{"type", "default" }
};
return Polygons.Select(b => new Feature(b.ToGeoJson(project), properties));
}
}
}
6 changes: 4 additions & 2 deletions GameRealisticMap/Nature/IBasicTerrainData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace GameRealisticMap.Nature
{
public interface IBasicTerrainData : IGeoJsonData
public interface IBasicTerrainData : IGeoJsonData, INonDefaultArea, IPolygonTerrainData
{
List<TerrainPolygon> Polygons { get; }
new List<TerrainPolygon> Polygons { get; }

IEnumerable<TerrainPolygon> INonDefaultArea.Polygons => Polygons;
}
}
9 changes: 9 additions & 0 deletions GameRealisticMap/Nature/INonDefaultArea.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using GameRealisticMap.Geometries;

namespace GameRealisticMap.Nature
{
public interface INonDefaultArea
{
IEnumerable<TerrainPolygon> Polygons { get; }
}
}
9 changes: 9 additions & 0 deletions GameRealisticMap/Nature/IPolygonTerrainData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using GameRealisticMap.Geometries;

namespace GameRealisticMap.Nature
{
public interface IPolygonTerrainData
{
List<TerrainPolygon> Polygons { get; }
}
}
2 changes: 1 addition & 1 deletion GameRealisticMap/Preview/PreviewRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public async Task RenderHtml<TData>(IProgressTask progress, string targetFile)

public static async Task RenderHtml(BuildContext context, string targetFile)
{
await RenderHtml(new FeatureCollection(context.Catalog.GetOfType<IGeoJsonData>(context).SelectMany(b => b.ToGeoJson(p => p)).ToList()), targetFile);
await RenderHtml(new FeatureCollection(context.GetOfType<IGeoJsonData>().SelectMany(b => b.ToGeoJson(p => p)).ToList()), targetFile);
}

public static async Task RenderHtml(FeatureCollection collection, string targetFile)
Expand Down
4 changes: 3 additions & 1 deletion GameRealisticMap/Preview/grm-preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
</div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js">
</script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.pattern.js">
</script>
<script src="grm-preview.js">
</script>
<script>
InitPreview({"type":"FeatureCollection"});
InitPreview({"type":"FeatureCollection"});
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions GameRealisticMap/Preview/grm-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ function InitPreview(geoJson)
L.control.scale({ maxWidth: 200, imperial: false }).addTo(map);
L.control.gridMousePosition().addTo(map);

var stripes = new L.StripePattern();
stripes.addTo(map);

L.geoJSON(geoJson, {
style: function(feature) {
Expand Down Expand Up @@ -483,6 +485,10 @@ function InitPreview(geoJson)

case 'ocean': return { fillColor: 'RoyalBlue', fillOpacity: 0.5, stroke: true, weight: 3, color: 'RoyalBlue' };
case 'coastline': return { stroke: true, weight: 1, dashArray: [5, 5], color: 'yellow', fillOpacity: 0 };
case 'orchard': return { fillColor: 'greenyellow', stroke: false, fillOpacity: 0.35 };
case 'vineyard': return { fillColor: 'greenyellow', stroke: false, fillOpacity: 0.35 };
case 'default':
return { fillPattern: stripes, stroke: false, fillOpacity: 0.2 };
}
return { fillColor:'black', stroke: false, fillOpacity: 0.2 };
},
Expand Down
Loading