-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrames.cs
241 lines (198 loc) · 7.49 KB
/
Frames.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
using System;
using System.Drawing;
using System.Collections.Generic;
using Pastel;
using Map;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Frames
{
class Frame
{
/*
A frame is wrapper for a Grid, where further graphical processing may be applied, before
the frame is passed into the Renderer
*/
public Grid G;
public Grid Product;
//final step must be to flatten Grid
public Frame (Grid G)
{
this.G = (Grid)G.Clone();
this.Product = (Grid)G.Clone();
this.Product = FrameTools.Flatten(this.Product);
this.Product.ApplyLuminance();
}
}
static class FrameTools
{
public static Grid Flatten (Grid G)
{
/*
Create a flattened, top down, 2d grid from the 3d grid passed to the frame
*/
Grid product = new Grid(G.width, G.height, 1);
for (int z = G.depth - 1; z >= 0; z--)
for (int y = 0; y < G.height; y++)
for (int x = 0; x < G.width; x++)
{
if (product[0, y, x] == null && G[z, y, x] != null)
product[0, y, x] = G[z, y, x];
}
return product;
}
public static List<Coord> Contrast (Frame F1, Frame F2)
{
/*
Find all differences between two frames.
*/
//only contrasts flattned Frames
List<Coord> changes = new List<Coord> ();
for (int y = 0; y < F1.Product.height; y++)
for (int x = 0; x < F1.Product.width; x++)
if (F1.Product[0, y, x].Character != F2.Product[0, y, x].Character
||
F1.Product[0, y, x].ColorValue != F2.Product[0, y, x].ColorValue)
changes.Add(new Coord(x, y));
return changes;
}
public static void ApplyLuminance (this Grid G)
{
double [,] LumMap = Luminate.GenLumMap(G);
// for (int i = 0; i < 10; i++)
// Console.Write(LumMap[i, i]+" ");
// Console.Read/sz();
for (int y = 0; y < G.height; y++)
{
for (int x = 0; x < G.width; x++)
{
Color C = G[0, y, x].ColorValue ?? default(Color);
Color newC = Color.FromArgb(
(int)(C.R * (LumMap[y, x]>Luminate.minLuminance ? LumMap[y, x] : Luminate.minLuminance)),
(int)(C.G * (LumMap[y, x]>Luminate.minLuminance ? LumMap[y, x] : Luminate.minLuminance)),
(int)(C.B * (LumMap[y, x]>Luminate.minLuminance ? LumMap[y, x] : Luminate.minLuminance))
);
G[0, y, x].ColorValue = newC;
}
}
}
}
class Luminate
{
public const double minLuminance = 0.15;
public const double decayFactor = 0.01;
public static List<Coord> Line (Coord CStart, Coord CEnd)
{
double dX = CEnd.x - CStart.x;
double dY = CEnd.y - CStart.y;
double dErr;
double err = 0.0;
List<Coord> L = new List<Coord>();
Coord C1 = CStart;
Coord C2 = CEnd;
if (dY < 0)
C2.y+= 2*((int)Math.Abs(dY));
if (dX < 0)
C2.x+= 2*((int)Math.Abs(dX));
if ((C2.x-C1.x) > (C2.y-C1.y))
{
dErr = Math.Abs((double)(C2.y-C1.y)/(C2.x-C1.x));
int y = C1.y;
for (int x = C1.x; x < C2.x; x++)
{
L.Add(new Coord(x, y));
err += dErr;
if (err >= 0.5)
{
y += 1;
err -= 1.0;
}
}
}
else
{
dErr = Math.Abs((double)(C2.x-C1.x)/(C2.y-C1.y));
int x = C1.x;
for (int y = C1.y; y < C2.y; y++)
{
L.Add(new Coord(x, y));
err += dErr;
if (err >= 0.5)
{
x += 1;
err -= 1.0;
}
}
}
L.Add(C2);
if (dY < 0)
for (int i = 0; i < L.Count; i++)
{
Coord C = L[i];
C.y-= 2*Math.Abs(C.y-C1.y);
L[i] = C;
}
if (dX < 0)
for (int i = 0; i < L.Count; i++)
{
Coord C = L[i];
C.x-= 2*Math.Abs(C.x-C1.x);
L[i] = C;
}
return L;
}
public static int GetMaxLumDist(double lum)
{
int maxLumDist = 0;
double c = lum;
while (Math.Round(c, 2) >= minLuminance)
{
maxLumDist++;
c = Math.Pow(lum*(1-decayFactor), maxLumDist);
}
return maxLumDist;
}
public static double[,] GenLumMap (Grid G)
{
double [,] LumMap = new double [G.height, G.width];
List<Coord> LuminantUnits = new List<Coord> ();
for (int y = 0; y < G.height; y++)
for (int x = 0; x < G.width; x++)
if (G[0, y, x].HasFlag("lum"))
LuminantUnits.Add(new Coord(x, y));
foreach (Coord C in LuminantUnits)
{
double Luminance = G[C].GetFlag<double>("lum");
int maxDist = GetMaxLumDist(Luminance);
int xMinDist = C.x - maxDist >= 0? C.x - maxDist : 0;
int xMaxDist = C.x + maxDist <= G.width? C.x + maxDist : G.width;
int yMinDist = C.y - maxDist >= 0? C.y - maxDist : 0;
int yMaxDist = C.y + maxDist <= G.height? C.y + maxDist : G.height;
for (int y = yMinDist; y < yMaxDist; y++)
{
for (int x = xMinDist; x < xMaxDist; x++)
{
List<Coord> ray = Line(C, new Coord(x, y));
int i = ray.Count;
foreach (Coord C2 in ray)
{
double l = Math.Pow(Luminance*(1-decayFactor), Math.Abs(C.x-C2.x)+Math.Abs(C.y-C2.y));
if (l > LumMap[C2.y, C2.x])
{
if (l < minLuminance)
LumMap[C2.y, C2.x] = minLuminance;
else
LumMap[C2.y, C2.x] = l;
}
if (G[C2].HasFlag("solid"))
break;
i--;
}
}
}
}
return LumMap;
}
}
}