Skip to content

Commit

Permalink
Make algorithm multithreaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Vort committed Jan 9, 2017
1 parent c78eaa9 commit 0fadaf9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
9 changes: 9 additions & 0 deletions Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public double DifferenceTo(Color c)
return rgb1.Compare(rgb2, cie);
}

public static Color Lerp(Color c1, Color c2, double x)
{
double omx = 1 - x;
return new Color(
(byte)(c1.R * omx + c2.R * x),
(byte)(c1.G * omx + c2.G * x),
(byte)(c1.B * omx + c2.B * x));
}

public byte R;
public byte G;
public byte B;
Expand Down
18 changes: 12 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -81,14 +82,19 @@ static void Main(string[] args)
Config.Write();
if (go)
{
try
if (!Debugger.IsAttached)
{
new Tracer();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
try
{
new Tracer();
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
else
new Tracer();
}
else
Console.WriteLine("<osm version='0.6'></osm>");
Expand Down
17 changes: 0 additions & 17 deletions SimpleBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@

namespace RiverTrace
{
class ColorLerp
{
public static Color Lerp(Color c1, Color c2, double minV, double maxV, double V)
{
return Lerp(c1, c2, (V - minV) / (maxV - minV));
}

public static Color Lerp(Color c1, Color c2, double x)
{
double omx = 1 - x;
return new Color(
(byte)(c1.R * omx + c2.R * x),
(byte)(c1.G * omx + c2.G * x),
(byte)(c1.B * omx + c2.B * x));
}
}

class SimpleBitmap
{
public byte[] Data;
Expand Down
6 changes: 3 additions & 3 deletions TileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public Color GetPixel(double x, double y)
Color c21 = GetPixel(flxi, flyi - 1);
Color c22 = GetPixel(flxi, flyi);

return ColorLerp.Lerp(
ColorLerp.Lerp(c11, c21, frx),
ColorLerp.Lerp(c12, c22, frx), fry);
return Color.Lerp(
Color.Lerp(c11, c21, frx),
Color.Lerp(c12, c22, frx), fry);
}

public Color GetPixel(int x, int y)
Expand Down
31 changes: 18 additions & 13 deletions Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public Tracer()
if (Config.Data.debug)
sb = new SimpleBitmap(pixelRange * 2, pixelRange);
var angles = new Dictionary<double, double>();
for (int i = 0; i < pixelRange; i++)
Parallel.For(0, pixelRange, j =>
{
for (int j = 0; j < pixelRange; j++)
for (int i = 0; i < pixelRange; i++)
{
int x = (int)(lastPoint.X + i - scanRadius);
int y = (int)(lastPoint.Y + j - scanRadius);
Expand All @@ -140,25 +140,29 @@ public Tracer()

Color c = tileMap.GetPixel(x, y);
if (Config.Data.debug)
sb.SetPixel(i, j, c);
sb.SetPixel(i, pixelRange - j - 1, c);

double invDiff = Math.Max(1.0 -
waterColor.DifferenceTo(c) / Config.Data.shoreContrast, 0.0);

if (Config.Data.debug)
{
byte diffColor = (byte)Math.Round(invDiff * 255.0);
sb.SetPixel(i + pixelRange, j, new Color(diffColor, diffColor, diffColor));
sb.SetPixel(i + pixelRange, pixelRange - j - 1, new Color(diffColor, diffColor, diffColor));
}

if (invDiff != 0.0)
{
if (!angles.ContainsKey(angle))
angles[angle] = 0.0;
angles[angle] += invDiff;
lock (angles)
{
if (!angles.ContainsKey(angle))
angles[angle] = invDiff;
else
angles[angle] += invDiff;
}
}
}
}
});

samples.Add(sb);

Expand All @@ -184,18 +188,19 @@ public Tracer()
way.Reverse();
WriteOsm(way);

for (int i = 0; i < 25; i++)
Console.WriteLine();
Console.WriteLine("<!--");
Console.WriteLine("Elapsed = " + sw.Elapsed.TotalSeconds + " sec");
Console.WriteLine("-->");

if (Config.Data.debug)
{
SimpleBitmap sampleChain = new SimpleBitmap(
samples[0].Width, samples[0].Height * samples.Count);
for (int i = 0; i < samples.Count; i++)
samples[i].CopyTo(sampleChain, i * samples[0].Height);
sampleChain.WriteTo("sample_chain.png");
for (int i = 0; i < 50; i++)
Console.WriteLine();
Console.WriteLine("<!--");
Console.WriteLine("Elapsed = " + sw.Elapsed.TotalSeconds);
Console.WriteLine("-->");
}
}
}
Expand Down

0 comments on commit 0fadaf9

Please sign in to comment.