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

Keep orientation of watercourse #153

Closed
Closed
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
29 changes: 26 additions & 3 deletions GameRealisticMap/Geometries/TerrainPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public List<TerrainPath> Substract(TerrainPolygon polygon)
return result.Childs.Select(c => new TerrainPath(c.Contour.Select(p => new TerrainPoint(p)).ToList())).ToList();
}


public IEnumerable<TerrainPath> SubstractAll(IEnumerable<TerrainPolygon> others)
{
var result = new List<TerrainPath>() { this };
Expand All @@ -145,6 +146,26 @@ public IEnumerable<TerrainPath> SubstractAll(IEnumerable<TerrainPolygon> others)
return result;
}

public List<TerrainPath> SubstractKeepOrientation(TerrainPolygon polygon)
{
return KeepOrientation(Substract(polygon));
}

public IEnumerable<TerrainPath> SubstractAllKeepOrientation(IEnumerable<TerrainPolygon> others)
{
var result = new List<TerrainPath>() { this };
foreach (var other in others.Where(o => GeometryHelper.EnveloppeIntersects(this, o)))
{
var previousResult = result.ToList();
result.Clear();
foreach (var subjet in previousResult)
{
result.AddRange(subjet.SubstractKeepOrientation(other));
}
}
return result;
}

public IEnumerable<TerrainPath> ClippedByEnveloppe(ITerrainEnvelope other)
{
if (!EnveloppeIntersects(other))
Expand Down Expand Up @@ -272,14 +293,16 @@ public IEnumerable<TerrainPath> ClippedKeepOrientation(TerrainPolygon polygon)
{
return Enumerable.Empty<TerrainPath>();
}
return KeepOrientation(Intersection(polygon));
}

private List<TerrainPath> KeepOrientation(List<TerrainPath> clipped)
{
var initialPoints = Points.Select(p => p.ToIntPointPrecision()).ToList();

var clipped = Intersection(polygon);
foreach (var result in clipped)
{
var indices = result.Points.Select(np => initialPoints.FindIndex(p => TerrainPoint.Equals(np, p))).Where(i => i != -1).Take(2).ToList();
if (indices.Count > 1)
if (indices.Count > 1)
{
if (indices[0] > indices[1])
{
Expand Down
4 changes: 2 additions & 2 deletions GameRealisticMap/Nature/Watercourses/WatercoursesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ private List<Watercourse> GetPaths(IBuildContext context, List<TerrainPolygon> l
.OfType<ILineString>()
.Where(l => !l.IsClosed)
.SelectMany(geometry => TerrainPath.FromGeometry(geometry, context.Area.LatLngToTerrainPoint))
.SelectMany(path => path.ClippedBy(context.Area.TerrainBounds))
.SelectMany(p => p.SubstractAll(lakesPolygons)))
.SelectMany(path => path.ClippedKeepOrientation(context.Area.TerrainBounds))
.SelectMany(p => p.SubstractAllKeepOrientation(lakesPolygons)))
{
waterwaysPaths.Add(new Watercourse(segment, kind.Value));
}
Expand Down
Loading