Skip to content

Commit

Permalink
Way simplification added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vort committed Jan 9, 2017
1 parent 0fadaf9 commit c7b6b5b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
9 changes: 6 additions & 3 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ class ConfigData
[DataMember]
public int iterationCount;
[DataMember]
public double shoreContrast;
[DataMember]
public double scanRadiusScale;
[DataMember]
public double angleRange;
[DataMember]
public double angleStep;
[DataMember]
public double shoreContrast;
[DataMember]
public double advanceRate;
[DataMember]
public double simplificationStrength;

[DataMember]
public bool debug;
Expand All @@ -52,11 +54,12 @@ static Config()
lat2 = 64.9032122,
lon2 = 52.2213061,
iterationCount = 300,
shoreContrast = 10.0,
scanRadiusScale = 2.0,
angleRange = 90.0,
angleStep = 4.0,
shoreContrast = 10.0,
advanceRate = 0.5,
simplificationStrength = 0.1,
debug = false
};
if (File.Exists(fileName))
Expand Down
1 change: 1 addition & 0 deletions RiverTrace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Projection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleBitmap.cs" />
<Compile Include="Simplify.cs" />
<Compile Include="TileMap.cs" />
<Compile Include="Tracer.cs" />
<Compile Include="Vector.cs" />
Expand Down
75 changes: 75 additions & 0 deletions Simplify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;

namespace RiverTrace
{
// Source:
// https://www.codeproject.com/articles/18936/a-csharp-implementation-of-douglas-peucker-line-ap
class Simplify
{
public static List<Vector> DouglasPeuckerReduction(List<Vector> points, double tolerance)
{
if (points == null || points.Count < 3)
return points;

int firstPoint = 0;
int lastPoint = points.Count - 1;
List<int> pointIndexsToKeep = new List<int>();

pointIndexsToKeep.Add(firstPoint);
pointIndexsToKeep.Add(lastPoint);

while (points[firstPoint].Equals(points[lastPoint]))
lastPoint--;

DouglasPeuckerReduction(points, firstPoint, lastPoint,
tolerance, ref pointIndexsToKeep);

List<Vector> returnPoints = new List<Vector>();
pointIndexsToKeep.Sort();
foreach (int index in pointIndexsToKeep)
returnPoints.Add(points[index]);

return returnPoints;
}

private static void DouglasPeuckerReduction(List<Vector> points,
int firstPoint, int lastPoint, double tolerance, ref List<int> pointIndexsToKeep)
{
double maxDistance = 0;
int indexFarthest = 0;

for (int index = firstPoint; index < lastPoint; index++)
{
double distance = PerpendicularDistance(
points[firstPoint], points[lastPoint], points[index]);
if (distance > maxDistance)
{
maxDistance = distance;
indexFarthest = index;
}
}

if (maxDistance > tolerance && indexFarthest != 0)
{
pointIndexsToKeep.Add(indexFarthest);

DouglasPeuckerReduction(points, firstPoint,
indexFarthest, tolerance, ref pointIndexsToKeep);
DouglasPeuckerReduction(points, indexFarthest,
lastPoint, tolerance, ref pointIndexsToKeep);
}
}

public static double PerpendicularDistance(Vector point1, Vector point2, Vector point)
{
double area = Math.Abs(.5 * (point1.X * point2.Y + point2.X *
point.Y + point.X * point1.Y - point2.X * point1.Y - point.X *
point2.Y - point1.X * point.Y));
double bottom = Math.Sqrt(Math.Pow(point1.X - point2.X, 2) +
Math.Pow(point1.Y - point2.Y, 2));
double height = area / bottom * 2;
return height;
}
}
}
12 changes: 9 additions & 3 deletions Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace RiverTrace
{
class Tracer
{
private double riverWidthPx;
private double riverWidthM;
private double scanRadius;
private Color waterColor;
Expand Down Expand Up @@ -79,7 +80,7 @@ void CalcSampleDimensions(Vector startPoint, Vector direction)
}
riverHalfWidth[0] /= pickCount;
riverHalfWidth[1] /= pickCount;
double riverWidthPx = riverHalfWidth[0] + riverHalfWidth[1] + 1.0;
riverWidthPx = riverHalfWidth[0] + riverHalfWidth[1] + 1.0;
scanRadius = riverWidthPx * Config.Data.scanRadiusScale;

Vector wp1 = startPoint + sideDirs[0] * (riverWidthPx / 2.0);
Expand Down Expand Up @@ -127,10 +128,10 @@ public Tracer()
Vector pixelVector = new Vector(x, y) - lastPoint;
double pixelVectorLen = pixelVector.Length();

if (pixelVector.Length() < 1.0)
continue;
if (pixelVectorLen > scanRadius)
continue;
if (pixelVectorLen < 1.0)
continue;

double angle = lastDirection.AngleTo(pixelVector);
if (angle < -Config.Data.angleRange)
Expand Down Expand Up @@ -186,6 +187,11 @@ public Tracer()
sw.Stop();

way.Reverse();
if (Config.Data.simplificationStrength > 0.0)
{
way = Simplify.DouglasPeuckerReduction(way,
riverWidthPx * Config.Data.simplificationStrength);
}
WriteOsm(way);

for (int i = 0; i < 25; i++)
Expand Down

0 comments on commit c7b6b5b

Please sign in to comment.