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

Contours preview display performance #136

Merged
merged 1 commit into from
Sep 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

<StackPanel Orientation="Horizontal">

<ProgressBar VerticalAlignment="Center" Width="300" Height="15" Margin="5" IsIndeterminate="True"
Visibility="{Binding IsWorking, Converter={StaticResource BooleanToVisibilityConverter}}" />

<Button VerticalAlignment="Center" Click="DispatchViewOSM" Padding="5 2" Visibility="{Binding IsNotWorking, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="5 0">View on OpenStreetMap.org</Button>
<Button VerticalAlignment="Center" Click="DispatchViewOSM" Padding="5 2" Margin="5 0">View on OpenStreetMap.org</Button>

<Button VerticalAlignment="Center" Click="DispatchEditOSM" Padding="5 2" Visibility="{Binding IsNotWorking, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="5 0">Edit on OpenStreetMap.org</Button>
<Button VerticalAlignment="Center" Click="DispatchEditOSM" Padding="5 2" Margin="5 0">Edit on OpenStreetMap.org</Button>

<ProgressBar VerticalAlignment="Center" Width="300" Height="15" Margin="5" IsIndeterminate="True"
Visibility="{Binding IsWorking, Converter={StaticResource BooleanToVisibilityConverter}}" />

</StackPanel>

<grm:GrmMapViewer x:Name="Map" Grid.Row="1" MapData="{Binding PreviewMapData}" SizeInMeters="{Binding SizeInMeters}" />
Expand Down
23 changes: 23 additions & 0 deletions GameRealisticMap.Test/Geometries/TerrainPathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,28 @@ public void TerrainPath_Substract()

Assert.Empty(path.Substract(TerrainPolygon.FromRectangle(new(2.5f, 0f), new(7.5f, 20f))));
}


[Fact]
public void TerrainPath_Simplify()
{
Assert.Equal(new TerrainPoint[] { new(0, 0), new(0, 10) },
TerrainPath.Simplify(new TerrainPoint[] { new(0, 0), new(0, 5), new(0, 10) }));

Assert.Equal(new TerrainPoint[] { new(0, 0), new(0, 10) },
TerrainPath.Simplify(new TerrainPoint[] { new(0, 0), new(0.25f, 5), new(0, 10) }));

Assert.Equal(new TerrainPoint[] { new(0, 0), new(0, 10) },
TerrainPath.Simplify(new TerrainPoint[] { new(0, 0), new(0, 2), new(0, 4), new(0, 6), new(0, 8), new(0, 10) }));

Assert.Equal(new TerrainPoint[] { new(0, 0), new(0, 10), new(10, 10) },
TerrainPath.Simplify(new TerrainPoint[] { new(0, 0), new(0, 10), new(10, 10) }));

Assert.Equal(new TerrainPoint[] { new(0, 0), new(0, 10) },
TerrainPath.Simplify(new TerrainPoint[] { new(0, 0), new(0, 10) }));

Assert.Equal(new TerrainPoint[] { new(0, 0) },
TerrainPath.Simplify(new TerrainPoint[] { new(0, 0) }));
}
}
}
17 changes: 15 additions & 2 deletions GameRealisticMap/ElevationModel/ElevationContourBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GameRealisticMap.Geometries;
using GameRealisticMap.Reporting;
using GeoAPI.Geometries;
using MapToolkit.Contours;

namespace GameRealisticMap.ElevationModel
Expand All @@ -24,9 +23,23 @@ public ElevationContourData Build(IBuildContext context)
contour.Add(elevation.ToDataCell(), new ContourLevelGenerator(-50, 5), false, report);
}

var paths = contour.Lines.Select(l => new TerrainPath(l.Points.Select(p => new TerrainPoint((float)p.Longitude, (float)p.Latitude)).ToList())).ToList();
var paths = contour.Lines.ProgressStep(progress,"Lines").Select(l => new TerrainPath(
TerrainPath.Simplify(l.Points.Select(p => new TerrainPoint((float)p.Longitude, (float)p.Latitude)).ToList()))).ToList();

var slices = CreateSlices(context.Area.TerrainBounds).ToList();

paths = slices.SelectManyParallel(progress, "Slices", s => paths.SelectMany(p => p.ClippedByEnveloppe(s))).ToList();

return new ElevationContourData(paths);
}

private IEnumerable<ITerrainEnvelope> CreateSlices(ITerrainEnvelope envelope)
{
if ( envelope.MaxPoint.X - envelope.MinPoint.X > 2000 )
{
return envelope.SplitQuad().SelectMany(CreateSlices);
}
return new[] { envelope };
}
}
}
32 changes: 32 additions & 0 deletions GameRealisticMap/Geometries/TerrainPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public IEnumerable<TerrainPath> SubstractAll(IEnumerable<TerrainPolygon> others)
return result;
}

public IEnumerable<TerrainPath> ClippedByEnveloppe(ITerrainEnvelope other)
{
if (!EnveloppeIntersects(other))
{
return Enumerable.Empty<TerrainPath>();
}
return Intersection(TerrainPolygon.FromRectangle(other.MinPoint, other.MaxPoint));
}

public IEnumerable<TerrainPath> ClippedBy(TerrainPolygon polygon)
{
if (!EnveloppeIntersects(polygon))
Expand Down Expand Up @@ -306,5 +315,28 @@ public TerrainPolygon ToPolygon()
}
return new TerrainPolygon(Points);
}

public static List<TerrainPoint> Simplify(IReadOnlyList<TerrainPoint> input)
{
if (input.Count < 3)
{
return input.ToList();
}
var previousPoint = input[0];
var result = new List<TerrainPoint>() { previousPoint };
for (int i = 1; i < input.Count - 1; i++)
{
var thisPoint = input[i];
var nextPoint = input[i + 1];
var extrapolated = Vector2.Lerp(previousPoint.Vector, nextPoint.Vector, (thisPoint.Vector - previousPoint.Vector).Length() / (nextPoint.Vector - previousPoint.Vector).Length());
if ((extrapolated - thisPoint.Vector).Length() > 0.5)
{
result.Add(thisPoint);
previousPoint = thisPoint;
}
}
result.Add(input[input.Count - 1]);
return result;
}
}
}
36 changes: 35 additions & 1 deletion GameRealisticMap/Reporting/ProgressSystemHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace GameRealisticMap.Reporting
using GameRealisticMap.Geometries;
using System.Collections.Concurrent;

namespace GameRealisticMap.Reporting
{
public static class ProgressSystemHelper
{
Expand Down Expand Up @@ -46,5 +49,36 @@ public static IEnumerable<T> Progress<T>(this IEnumerable<T> input, IProgressInt
report.ReportOneDone();
}
}

public static IReadOnlyCollection<TTarget> SelectManyParallel<TSource,TTarget>(this IReadOnlyCollection<TSource> source, IProgressSystem progress, string stepName, Func<TSource,IEnumerable<TTarget>> transform)
{
var result = new ConcurrentQueue<TTarget>();
using (var report = progress.CreateStep(stepName, source.Count))
{
Parallel.ForEach(source, sourceItem =>
{
foreach (var resultItem in transform(sourceItem))
{
result.Enqueue(resultItem);
}
report.ReportOneDone();
});
}
return result;
}

public static IReadOnlyCollection<TTarget> SelectParallel<TSource, TTarget>(this IReadOnlyCollection<TSource> source, IProgressSystem progress, string stepName, Func<TSource, TTarget> transform)
{
var result = new ConcurrentQueue<TTarget>();
using (var report = progress.CreateStep(stepName, source.Count))
{
Parallel.ForEach(source, sourceItem =>
{
result.Enqueue(transform(sourceItem));
report.ReportOneDone();
});
}
return result;
}
}
}
Loading