-
Notifications
You must be signed in to change notification settings - Fork 1
/
DebugFrame.cs
115 lines (99 loc) · 4.26 KB
/
DebugFrame.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
using System;
using System.Linq;
using System.Threading.Tasks;
namespace RiverTrace
{
class DebugFrame
{
private int pixelRange;
private int angleSamples;
private int radiusSamples;
private double scanRadius;
private SimpleBitmap sectorRgb;
private SimpleBitmap sectorDiff;
private SimpleBitmap polarTrans;
private SimpleBitmap polarGrid;
public int Height { get { return Math.Max(pixelRange, radiusSamples); } }
public int Width { get { return pixelRange * 2 + angleSamples * 2; } }
public DebugFrame(double scanRadius, int angleSamples, int radiusSamples)
{
this.scanRadius = scanRadius;
this.angleSamples = angleSamples;
this.radiusSamples = radiusSamples;
pixelRange = (int)(scanRadius * 2) + 1;
sectorRgb = new SimpleBitmap(pixelRange, pixelRange);
sectorDiff = new SimpleBitmap(pixelRange, pixelRange);
polarTrans = new SimpleBitmap(angleSamples, radiusSamples);
polarGrid = new SimpleBitmap(angleSamples, radiusSamples);
}
private byte MultiplyIntensity(byte intensity, double factor)
{
int intensityMul = (int)(intensity * factor);
if (intensityMul > 255)
intensityMul = 255;
return (byte)intensityMul;
}
public void SetPolarGrid(double[] anglesGrid)
{
double agm = radiusSamples / anglesGrid.Max();
for (int i = 0; i < angleSamples; i++)
{
int h = (int)(agm * anglesGrid[i]);
byte c1 = 255;
byte c2 = (byte)((agm * anglesGrid[i] - h) * 255.0);
if (i == angleSamples / 2)
{
c1 = MultiplyIntensity(c1, 0.8);
c2 = MultiplyIntensity(c2, 0.8);
}
for (int j = 0; j < h; j++)
polarGrid.SetPixel(i, radiusSamples - j - 1, c1);
if (c2 != 0)
polarGrid.SetPixel(i, radiusSamples - h - 1, c2);
}
}
public void SetPolarTrans(int x, int y, double diff)
{
byte diffColor = (byte)Math.Round(diff * 255.0);
if (x == angleSamples / 2)
diffColor = MultiplyIntensity(diffColor, 1.25);
polarTrans.SetPixel(x, radiusSamples - y - 1, diffColor);
}
public void FillSectors(Vector lastPoint,
Vector lastDirection, TileMap tileMap, Lab waterColor)
{
Parallel.For(0, pixelRange, j =>
{
for (int i = 0; i < pixelRange; i++)
{
int x = (int)(lastPoint.X + i - scanRadius);
int y = (int)(lastPoint.Y + j - scanRadius);
Vector pixelVector = new Vector(x, y) - lastPoint;
if (pixelVector.Length() > scanRadius)
continue;
double angle = lastDirection.AngleTo(pixelVector);
if (angle < -Config.Data.angleRange)
continue;
if (angle > Config.Data.angleRange)
continue;
Color c = tileMap.GetPixel(x, y);
sectorRgb.SetPixel(i, j, c);
double invDiff = Math.Max(1.0 -
Color.Difference(waterColor, c) / Config.Data.shoreContrast, 0.0);
byte diffColor = (byte)Math.Round(invDiff * 255.0);
sectorDiff.SetPixel(i, j, diffColor);
}
});
}
public void CopyTo(SimpleBitmap debugInfo, int index)
{
int polarOfs = 0;
if (Height > radiusSamples)
polarOfs = (Height - radiusSamples) / 2;
sectorRgb.CopyTo(debugInfo, 0, index * Height);
sectorDiff.CopyTo(debugInfo, pixelRange, index * Height);
polarTrans.CopyTo(debugInfo, pixelRange * 2, index * Height + polarOfs);
polarGrid.CopyTo(debugInfo, pixelRange * 2 + angleSamples, index * Height + polarOfs);
}
}
}