-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
398 lines (316 loc) · 13.2 KB
/
MainWindow.xaml.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Uses a deterministic crowding genetic algorithm to potentially generate images
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.30.8270&rep=rep1&type=pdf
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace GeneticTest {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public int SIZE = 16;
public int PIXELS;
public int ZOOM = 8;
public int COUNT = 16;
public float[][] images;
public float[] child1, child2;
public Random rnd;
public int[] pairs;
public int pos;
public bool visU;
public bool visD;
public bool pair1;
public MainWindow() {
InitializeComponent();
rnd = new Random();
PIXELS = 3 * SIZE * SIZE;
pairs = new int[COUNT];
images = new float[COUNT][];
for(int i = 0; i < COUNT; ++i) {
pairs[i] = i;
images[i] = new float[PIXELS];
for(int z = 0; z < 3; ++z)
for(int y = 0; y < SIZE; ++y)
for(int x = 0; x < SIZE; ++x) {
int j = 3 * (y * SIZE + x) + z;
images[i][j] = (float)(2.0 * rnd.NextDouble() - 1.0) * 5.0f / (y + 5) / (x + 5);
}
}
pos = COUNT;
pair1 = false;
visU = visD = false;
Next();
}
public void Next() {
if(pair1) {
if(visD)
images[pairs[pos]] = child1;
imgU.Source = Render(images[pairs[pos + 1]], ZOOM);
imgD.Source = Render(child2, ZOOM);
pos += 2;
pair1 = false;
} else {
if(visD)
images[pairs[pos - 1]] = child2;
if(pos >= COUNT) {
for(int i = COUNT - 1; i >= 0; --i) {
int n = rnd.Next(i + 1);
int temp = pairs[i];
pairs[i] = pairs[n];
pairs[n] = temp;
}
pos = 0;
}
int crossR = rnd.Next(1, 2 * SIZE - 2);
int crossG = rnd.Next(1, 2 * SIZE - 2);
int crossB = rnd.Next(1, 2 * SIZE - 2);
child1 = new float[PIXELS];
child2 = new float[PIXELS];
for(int y = 0; y < SIZE; ++y)
for(int x = 0; x < SIZE; ++x) {
int i = 3 * (y * SIZE + x);
if(x + y < crossR) {
child1[i] = Mutate(images[pairs[pos]][i]);
child2[i] = Mutate(images[pairs[pos + 1]][i]);
} else {
child2[i] = Mutate(images[pairs[pos]][i]);
child1[i] = Mutate(images[pairs[pos + 1]][i]);
}
if(x + y < crossG) {
child1[i + 1] = Mutate(images[pairs[pos]][i + 1]);
child2[i + 1] = Mutate(images[pairs[pos + 1]][i + 1]);
} else {
child2[i + 1] = Mutate(images[pairs[pos]][i + 1]);
child1[i + 1] = Mutate(images[pairs[pos + 1]][i + 1]);
}
if(x + y < crossB) {
child1[i + 2] = Mutate(images[pairs[pos]][i + 2]);
child2[i + 2] = Mutate(images[pairs[pos + 1]][i + 2]);
} else {
child2[i + 2] = Mutate(images[pairs[pos]][i + 2]);
child1[i + 2] = Mutate(images[pairs[pos + 1]][i + 2]);
}
}
if(Similarity(images[pairs[pos]], child1) + Similarity(images[pairs[pos + 1]], child2) < Similarity(images[pairs[pos]], child2) + Similarity(images[pairs[pos + 1]], child1)) {
float[] temp = child1;
child1 = child2;
child2 = temp;
}
imgU.Source = Render(images[pairs[pos]], ZOOM);
imgD.Source = Render(child1, ZOOM);
pair1 = true;
}
next.IsEnabled = false;
visU = visD = false;
selectU.Visibility = System.Windows.Visibility.Hidden;
selectD.Visibility = System.Windows.Visibility.Hidden;
}
private float Similarity(float[] p, float[] c) {
float diff = 0.0f;
for(int i = 0; i < PIXELS; ++i)
diff += Math.Sign(p[i]) * Math.Sign(c[i]);
return diff;
}
private WriteableBitmap Render(float[] p, int zoom) {
WriteableBitmap bmp = new WriteableBitmap(SIZE * zoom, SIZE * zoom, 96.0, 96.0, PixelFormats.Rgb24, null);
byte[] pixels = new byte[PIXELS * zoom * zoom];
for(int z = 0; z < 3; ++z)
for(int y = 0; y < SIZE; ++y)
for(int x = 0; x < SIZE; ++x) {
int i;
double total = 0.0;
for(int dy = 0; dy < SIZE; ++dy)
for(int dx = 0; dx < SIZE; ++dx) {
i = 3 * (dy * SIZE + dx) + z;
total += p[i] * Math.Cos(Math.PI * (x + 0.5) * dx / 16) * Math.Cos(Math.PI * (y + 0.5) * dy / 16);
}
i = 3 * (y * SIZE + x) + z;
byte c = (byte)(255.0 * Math.Min(1.0, Math.Max(0.0, (total + 1.0) / 2.0)));
for(int v = 0; v < zoom; ++v)
for(int u = 0; u < zoom; ++u) {
int j = 3 * (((y * zoom + v) * SIZE + x) * zoom + u) + z;
pixels[j] = c;
}
}
bmp.WritePixels(new Int32Rect(0, 0, SIZE * zoom, SIZE * zoom), pixels, 3 * SIZE * zoom, 0);
return bmp;
}
private float Mutate(float p) {
return (float)(p + (rnd.NextDouble() + rnd.NextDouble() + rnd.NextDouble() - 1.5) * 0.03);
}
private void next_Click(object sender, RoutedEventArgs e) {
Next();
}
private void imgU_MouseDown(object sender, MouseButtonEventArgs e) {
if(!visU) {
selectU.Visibility = System.Windows.Visibility.Visible;
selectD.Visibility = System.Windows.Visibility.Hidden;
visU = true;
visD = false;
} else {
selectU.Visibility = System.Windows.Visibility.Hidden;
visU = false;
}
UpdateButton();
}
private void imgD_MouseDown(object sender, MouseButtonEventArgs e) {
if(!visD) {
selectD.Visibility = System.Windows.Visibility.Visible;
selectU.Visibility = System.Windows.Visibility.Hidden;
visD = true;
visU = false;
} else {
selectD.Visibility = System.Windows.Visibility.Hidden;
visD = false;
}
UpdateButton();
}
private void UpdateButton() {
if(visU != visD) {
next.IsEnabled = true;
exportMenu.IsEnabled = true;
} else {
next.IsEnabled = false;
exportMenu.IsEnabled = false;
}
}
private void UpdateSizes() {
row1.Height = new GridLength(SIZE * ZOOM + 16);
row2.Height = new GridLength(SIZE * ZOOM + 16);
col1.Width = new GridLength(SIZE * ZOOM + 16);
}
private void New_Click(object sender, RoutedEventArgs e) {
NewDialog dialog = new NewDialog();
dialog.imageSize.Text = SIZE.ToString();
dialog.imageZoom.Text = ZOOM.ToString();
dialog.poolSize.Text = COUNT.ToString();
dialog.ShowDialog();
if(dialog.result == true) {
SIZE = 16;
int.TryParse(dialog.imageSize.Text, out SIZE);
SIZE = Math.Max(SIZE, 1);
PIXELS = 3 * SIZE * SIZE;
ZOOM = 8;
int.TryParse(dialog.imageZoom.Text, out ZOOM);
ZOOM = Math.Max(ZOOM, 1);
COUNT = 16;
int.TryParse(dialog.poolSize.Text, out COUNT);
COUNT = Math.Max(COUNT, 2);
UpdateSizes();
pairs = new int[COUNT];
images = new float[COUNT][];
for(int i = 0; i < COUNT; ++i) {
pairs[i] = i;
images[i] = new float[PIXELS];
for(int z = 0; z < 3; ++z)
for(int y = 0; y < SIZE; ++y)
for(int x = 0; x < SIZE; ++x) {
int j = 3 * (y * SIZE + x) + z;
images[i][j] = (float)(2.0 * rnd.NextDouble() - 1.0) * 11.0f / (y + 5) / (x + 5);
}
}
pos = COUNT;
pair1 = false;
visU = visD = false;
exportMenu.IsEnabled = false;
Next();
}
}
private void Save_Click(object sender, RoutedEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Pool Files (*.pool)|*.pool|All Files (*.*)|*.*";
dialog.DefaultExt = ".pool";
dialog.RestoreDirectory = true;
if(dialog.ShowDialog() == true) {
string filename = dialog.FileName;
using(BinaryWriter bw = new BinaryWriter(File.Open(filename, FileMode.Create))) {
bw.Write(SIZE);
bw.Write(ZOOM);
bw.Write(COUNT);
for(int i = 0; i < COUNT; ++i) {
for(int j = 0; j < PIXELS; ++j) {
bw.Write(images[i][j]);
}
}
}
}
}
private void Open_Click(object sender, RoutedEventArgs e) {
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Pool Files (*.pool)|*.pool|All Files (*.*)|*.*";
dialog.DefaultExt = ".pool";
dialog.RestoreDirectory = true;
if(dialog.ShowDialog() == true) {
string filename = dialog.FileName;
using(BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open))) {
SIZE = br.ReadInt32();
PIXELS = 3 * SIZE * SIZE;
ZOOM = br.ReadInt32();
COUNT = br.ReadInt32();
UpdateSizes();
pairs = new int[COUNT];
images = new float[COUNT][];
for(int i = 0; i < COUNT; ++i) {
pairs[i] = i;
images[i] = new float[PIXELS];
for(int j = 0; j < PIXELS; ++j) {
images[i][j] = br.ReadSingle();
}
}
}
pos = COUNT;
pair1 = false;
visU = visD = false;
Next();
}
}
private void Export_Click(object sender, RoutedEventArgs e) {
float[] toWrite = null;
if(pair1) {
if(visU) {
toWrite = images[pairs[pos]];
} else if(visD) {
toWrite = child1;
} else {
return;
}
} else {
if(visU) {
toWrite = images[pairs[pos - 1]];
} else if(visD) {
toWrite = child2;
} else {
return;
}
}
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "PNG Files (*.png)|*.png|All Files (*.*)|*.*";
dialog.DefaultExt = ".png";
dialog.RestoreDirectory = true;
if(dialog.ShowDialog() == true) {
string filename = dialog.FileName;
using(FileStream file = new FileStream(filename, FileMode.Create)) {
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(Render(toWrite, 1)));
encoder.Save(file);
}
}
}
private void Exit_Click(object sender, RoutedEventArgs e) {
Close();
}
}
}