-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tracer.cs
257 lines (217 loc) · 10.3 KB
/
Tracer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace RiverTrace
{
class Tracer
{
private double riverWidthPx;
private double riverWidthM;
private double scanRadius;
private Lab waterColor;
private TileMap tileMap;
void WriteOsm(List<Vector> result)
{
Console.WriteLine("<?xml version='1.0' encoding='UTF-8'?>");
Console.WriteLine("<osm version='0.6'>");
double lat;
double lon;
for (int i = 0; i < result.Count; i++)
{
int nodeId = -i - 2;
Projection.PixToDeg(result[i].X, result[i].Y, Config.Data.zoom, out lat, out lon);
Console.WriteLine(
" <node id='" + nodeId +
"' lat='" + lat +
"' lon='" + lon +
"' version='1' />");
}
Console.WriteLine(" <way id='-1' version='1'>");
for (int i = 0; i < result.Count; i++)
{
int nodeId = -i - 2;
Console.WriteLine(" <nd ref='" + nodeId + "' />");
}
Console.WriteLine(" <tag k='source:tracer' v='RiverTrace' />");
Console.WriteLine(" <tag k='source:zoomlevel' v='" + Config.Data.zoom + "' />");
if (!string.IsNullOrWhiteSpace(Config.Data.imageSourceName))
Console.WriteLine(" <tag k='source:position' v='" + Config.Data.imageSourceName + "' />");
Console.WriteLine(" <tag k='width' v='" + Math.Round(riverWidthM).ToString() + "' />");
Console.WriteLine(" <tag k='waterway' v='river' />");
Console.WriteLine(" </way>");
Console.WriteLine("</osm>");
}
void CalcSampleDimensions(Vector startPoint, Vector direction)
{
int pickCount = 5;
int maxHalfWidth = 50;
Vector[] sideDirs = new Vector[] { direction.Rotated(-90), direction.Rotated(90) };
Vector p1 = startPoint + sideDirs[0] * maxHalfWidth;
Vector p2 = startPoint + sideDirs[1] * maxHalfWidth;
Vector p3 = startPoint + direction * pickCount + sideDirs[0] * maxHalfWidth;
Vector p4 = startPoint + direction * pickCount + sideDirs[1] * maxHalfWidth;
int minX = (int)Math.Min(Math.Min(Math.Min(p1.X, p2.X), p3.X), p4.X);
int minY = (int)Math.Min(Math.Min(Math.Min(p1.Y, p2.Y), p3.Y), p4.Y);
int maxX = (int)Math.Max(Math.Max(Math.Max(p1.X, p2.X), p3.X), p4.X);
int maxY = (int)Math.Max(Math.Max(Math.Max(p1.Y, p2.Y), p3.Y), p4.Y);
tileMap.Load(minX - 4, minY - 4, maxX - minX + 8, maxY - minY + 8);
int waterColorR = 0;
int waterColorG = 0;
int waterColorB = 0;
Vector pickPoint1 = startPoint;
for (int i = 0; i < pickCount; i++)
{
Color c = tileMap.GetPixel(pickPoint1.X, pickPoint1.Y);
waterColorR += c.R;
waterColorG += c.G;
waterColorB += c.B;
pickPoint1 += direction;
}
waterColor = new Color(
(byte)(waterColorR / pickCount),
(byte)(waterColorG / pickCount),
(byte)(waterColorB / pickCount)).ToLab();
pickPoint1 = startPoint;
double[] riverHalfWidth = new double[2];
for (int i = 0; i < pickCount; i++)
{
for (int j = 0; j < 2; j++)
{
Vector pickPoint2 = pickPoint1;
for (int k = 0; k < maxHalfWidth; k++)
{
pickPoint2 += sideDirs[j];
Color checkColor = tileMap.GetPixel(pickPoint2.X, pickPoint2.Y);
double diff = Color.Difference(waterColor, checkColor);
if (diff > Config.Data.shoreContrast)
break;
riverHalfWidth[j] += 1.0;
}
}
pickPoint1 += direction;
}
riverHalfWidth[0] /= pickCount;
riverHalfWidth[1] /= pickCount;
riverWidthPx = riverHalfWidth[0] + riverHalfWidth[1] + 1.0;
scanRadius = riverWidthPx * Config.Data.scanRadiusScale;
Vector wp1 = startPoint + sideDirs[0] * (riverWidthPx / 2.0);
Vector wp2 = startPoint + sideDirs[1] * (riverWidthPx / 2.0);
riverWidthM = Projection.Distance(wp1, wp2, Config.Data.zoom);
}
private double[] Integrate(double[] samples, int sampleCount)
{
if (sampleCount == 1)
return samples;
int padCount = sampleCount / 2;
double[] paddedSamples = new double[padCount * 2 + samples.Length];
Array.Copy(samples, 0, paddedSamples, padCount, samples.Length);
for (int i = 0; i < padCount; i++)
paddedSamples[i] = samples[0];
for (int i = 0; i < padCount; i++)
paddedSamples[samples.Length + padCount + i] = samples[samples.Length - 1];
double[] result = new double[samples.Length];
for (int i = 0; i < samples.Length; i++)
{
double sum = 0.0;
for (int j = 0; j < sampleCount; j++)
sum += paddedSamples[i + j];
result[i] = sum / sampleCount;
}
return result;
}
public Tracer()
{
Stopwatch sw = new Stopwatch();
sw.Start();
tileMap = new TileMap(Config.Data.zoom);
Vector p1 = new Vector();
Vector p2 = new Vector();
Projection.DegToPix(Config.Data.lat1, Config.Data.lon1, Config.Data.zoom, out p1.X, out p1.Y);
Projection.DegToPix(Config.Data.lat2, Config.Data.lon2, Config.Data.zoom, out p2.X, out p2.Y);
Vector lastDirection = p2 - p1;
lastDirection.Normalize();
CalcSampleDimensions(p1, lastDirection);
int angleSamples = (int)Math.Ceiling(scanRadius * Math.PI *
Config.Data.resamplingFactor * Config.Data.angleRange / 90.0);
int radiusSamples = (int)Math.Ceiling(scanRadius * Config.Data.resamplingFactor);
var way = new List<Vector>();
way.Add(p1);
var debugFrames = new List<DebugFrame>();
Vector lastPoint = p1;
for (int z = 0; z < Config.Data.iterationCount; z++)
{
tileMap.Load(
(int)(lastPoint.X - scanRadius) - 4,
(int)(lastPoint.Y - scanRadius) - 4,
(int)(scanRadius * 2) + 8,
(int)(scanRadius * 2) + 8);
DebugFrame debugFrame = null;
double[] anglesGrid = new double[angleSamples];
if (Config.Data.generateDebugInfo)
{
debugFrame = new DebugFrame(scanRadius, angleSamples, radiusSamples);
debugFrame.FillSectors(lastPoint, lastDirection, tileMap, waterColor);
}
Vector startV = lastDirection.Rotated(-Config.Data.angleRange);
Parallel.For(0, angleSamples, i =>
{
Vector advV = startV.Rotated(i / (double)angleSamples * Config.Data.angleRange * 2.0);
for (int j = 0; j < radiusSamples; j++)
{
Vector p = lastPoint + advV * (j + 0.5) / Config.Data.resamplingFactor;
double diff = Color.Difference(waterColor, tileMap.GetPixel(p.X, p.Y));
double normDiff = Math.Max(1.0 - diff / Config.Data.shoreContrast, 0.0);
normDiff *= (j + 0.5) / radiusSamples;
if (Config.Data.generateDebugInfo)
debugFrame.SetPolarTrans(i, j, normDiff);
anglesGrid[i] += normDiff;
}
});
int integrateSamples = (int)(riverWidthPx *
Config.Data.resamplingFactor * Config.Data.noiseReduction);
if (integrateSamples % 2 == 0)
integrateSamples++;
anglesGrid = Integrate(anglesGrid, integrateSamples);
double anglesGridMax = anglesGrid.Max();
if (anglesGridMax == 0.0)
break;
double bestAngle = anglesGrid.ToList().IndexOf(anglesGridMax) /
(double)angleSamples * Config.Data.angleRange * 2.0 - Config.Data.angleRange;
lastDirection = lastDirection.Rotated(bestAngle);
lastPoint += lastDirection * scanRadius * Config.Data.advanceRate;
if (!Intersection.Check(way, lastPoint))
break;
if (Config.Data.generateDebugInfo)
{
debugFrame.SetPolarGrid(anglesGrid);
debugFrames.Add(debugFrame);
}
way.Add(lastPoint);
}
if (Config.Data.simplificationStrength > 0.0)
{
way = Simplify.DouglasPeuckerReduction(way,
riverWidthPx * Config.Data.simplificationStrength);
}
way.Reverse();
sw.Stop();
WriteOsm(way);
for (int i = 0; i < 25; i++)
Console.WriteLine();
Console.WriteLine("<!--");
Console.WriteLine("Elapsed = {0:0.000} sec", sw.Elapsed.TotalSeconds);
Console.WriteLine("-->");
if (Config.Data.generateDebugInfo)
{
SimpleBitmap debugInfo = new SimpleBitmap(
debugFrames[0].Width, debugFrames[0].Height * debugFrames.Count);
for (int i = 0; i < debugFrames.Count; i++)
debugFrames[i].CopyTo(debugInfo, i);
debugInfo.WriteTo("debug_info.tiff");
}
}
}
}