forked from tux3/Evolve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
345 lines (310 loc) · 9.7 KB
/
widget.cpp
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
#include "widget.h"
#include "ui_widget.h"
#include "progressdialog.h"
#include "settings.h"
#include <QDataStream>
#include <QString>
#include <QPixmap>
#include <QPen>
#include <QPainter>
#include <QRgb>
#include <QtConcurrent/QtConcurrent>
#include <ctime>
unsigned Widget::height;
unsigned Widget::width;
using namespace std;
Widget::Widget() :
QWidget(nullptr),
ui(new Ui::Widget)
{
ui->setupUi(this);
// Build the menu
menuBar = new QMenuBar(this);
QMenu* fileMenu = menuBar->addMenu(tr("&File"));
fileMenu->addAction("&Open image", this, SLOT(openImageClicked()));
fileMenu->addAction("&Save image", this, SLOT(saveImageClicked()));
fileMenu->addAction("&Export as SVG", this, SLOT(saveSVGClicked()));
fileMenu->addSeparator();
startStopAction = fileMenu->addAction("S&tart");
fileMenu->addAction("&Quit", this, SLOT(close()));
QMenu* dnaMenu = menuBar->addMenu(tr("&DNA"));
dnaMenu->addAction("&Import DNA", this, SLOT(importDnaClicked()));
dnaMenu->addAction("&Export DNA", this, SLOT(exportDnaClicked()));
dnaMenu->addAction("&Clean DNA", this, SLOT(cleanDnaClicked()));
dnaMenu->addAction("&Optimize DNA", this, SLOT(optimizeDnaClicked()));
QMenu* settingsMenu = menuBar->addMenu(tr("&Settings"));
settingsMenu->addAction("&Settings", this, SLOT(settingsClicked()));
ui->gridLayout->addWidget(menuBar,0,0,1,4);
menuBar->setFixedHeight(22);
generation = 0;
running = false;
connect(ui->btnOpen, SIGNAL(clicked()), this, SLOT(openImageClicked()));
connect(ui->btnStart, SIGNAL(clicked()), this, SLOT(startClicked()));
qsrand(time(NULL));
}
Widget::~Widget()
{
delete ui;
running = false;
exit(0);
}
int Widget::computeFitness(QImage& target, QRect box)
{
QAtomicInt fitness = 0;
unsigned minx, maxx, miny, maxy;
if (box.isNull())
{
minx = miny = 0;
maxx = width;
maxy = height;
}
else
{
minx = box.x();
miny = box.y();
maxx = minx + box.width();
maxy = miny + box.height();
}
QVector<QRgb*> targetLines;
QVector<QRgb*> originalLines;
for (unsigned i=miny; i<maxy; i++)
originalLines.append((QRgb*)pic.scanLine(i));
for (unsigned i=miny; i<maxy; i++)
targetLines.append((QRgb*)target.scanLine(i));
auto computeSlice = [&](unsigned start, unsigned end)
{
for (unsigned i=0; i<end-start; i++)
{
// Sum of the differences of each pixel's color
for (unsigned j=minx; j<maxx; j++)
{
int tR,tG,tB;
int oR,oG,oB;
QColor(targetLines.at(i)[j]).getRgb(&tR,&tG,&tB);
QColor(originalLines.at(i)[j]).getRgb(&oR,&oG,&oB);
unsigned diff = abs(tR-oR)+abs(tG-oG)+abs(tB-oB);
fitness.fetchAndAddRelaxed(diff);
}
}
};
QFuture<void> firstSlice = QtConcurrent::run(computeSlice, miny, maxy/2);
computeSlice(maxy/2, maxy);
firstSlice.waitForFinished();
return fitness.load();
}
void Widget::run()
{
// Main loop
while (running /* && polys.size() < N_POLYS */ )
{
Poly poly = genPoly();
QImage newGen = generated;
drawPoly(newGen, poly);
generation++;
int newFit = computeFitness(newGen);
if (newFit < fitness)
{
polys.append(poly);
// Optimize colors
QImage clean = generated;
optimizeColors(clean, polys.last());
// Update data
//generated = newGen;
fitness = computeFitness(generated);
// Update GUI
ui->imgBest->setPixmap(QPixmap::fromImage(generated));
ui->polysLabel->setNum(polys.size());
updateGuiFitness();
}
ui->generationLabel->setNum(generation);
app->processEvents();
}
}
QColor Widget::optimizeColors(QImage& target, Poly& poly, bool redraw)
{
// Find the poly's bounding box
int minx,miny,maxx,maxy;
minx = maxx = poly.points[0].x();
miny = maxy = poly.points[0].y();
for (QPoint point : poly.points)
{
minx = min(minx, point.x());
maxx = max(maxx, point.x());
miny = min(miny, point.y());
maxy = max(maxy, point.y());
}
QRect box(minx, miny, maxx-minx, maxy-miny);
// Check if the pic is better, commit and return if it is
auto validate = [&]()
{
QImage newGen = target;
if (redraw)
this->redraw(newGen);
else
drawPoly(newGen, poly);
int newFit = computeFitness(newGen);
generation++;
ui->generationLabel->setNum(generation);
if (newFit < fitness)
{
// Update data
generated = newGen;
fitness = newFit;
// Update GUI
ui->imgBest->setPixmap(QPixmap::fromImage(generated));
updateGuiFitness();
return true;
}
else
return false;
};
int targetColor; // 0=R, 1=G, 2=B, 3=A
// Add
for (targetColor=0; targetColor <= 8; targetColor++)
{
do
{
app->processEvents();
QColor color = poly.color;
if (targetColor == 0)
color = color.lighter(110); // Lighter
else if (targetColor == 1)
color = color.darker(110); // Darker
else if (targetColor == 2)
color.setRed(min(color.red()+N_COLOR_VAR,255)); // More red
else if (targetColor == 3)
color.setBlue(max(color.blue()-N_COLOR_VAR,0)); // Less blue
else if (targetColor == 4)
color.setGreen(min(color.green()+N_COLOR_VAR,255)); // More green
else if (targetColor == 5)
color.setRed(max(color.red()-N_COLOR_VAR,0)); // Less red
else if (targetColor == 6)
color.setBlue(min(color.blue()+N_COLOR_VAR,255)); // More blue
else if (targetColor == 7)
color.setGreen(max(color.green()-N_COLOR_VAR,0)); // Less green
else if (targetColor == 8)
color.setAlpha(max(color.alpha()-N_COLOR_VAR,0)); // Less alpha
else if (targetColor == 9 && OPT_INCREASE_ALPHA)
color.setAlpha(min(color.alpha()+N_COLOR_VAR,255)); // More alpha
poly.color = color;
} while (validate());
}
app->processEvents();
return poly.color;
}
Poly Widget::genPoly()
{
Poly poly;
for (int i=0; i<N_POLY_POINTS; i++)
{
quint16 x,y;
x = qrand()%(int)(width*(((float)FOCUS_RIGHT-FOCUS_LEFT)/100)) + (width*(float)FOCUS_LEFT/100);
y = qrand()%(int)(height*(((float)FOCUS_BOTTOM-FOCUS_TOP)/100)) + (height*(float)FOCUS_TOP/100);
poly.points.append(QPoint(x,y));
}
#if GEN_WITH_RANDOM_COLOR
poly.color = QColor::fromRgb(qrand()*qrand()*qrand());
poly.color.setAlpha(qrand()%130+20);
#else
quint64 avgx=0, avgy=0;
for (QPoint point : poly.points)
{
avgx += point.x();
avgy += point.y();
}
avgx /= N_POLY_POINTS;
avgy /= N_POLY_POINTS;
poly.color = pic.pixel(avgx,avgy);
poly.color.setAlpha(qrand()%130+20);
#endif
return poly;
}
void Widget::drawPoly(QImage& target, Poly& poly)
{
QPainter painter(&target);
painter.setPen(QPen(Qt::NoPen));
QBrush brush(poly.color);
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.drawPolygon(poly.points.data(), poly.points.size());
}
void Widget::redraw(QImage& target)
{
target.fill(Qt::white);
for (Poly& poly : polys)
drawPoly(target, poly);
}
void Widget::cleanDnaClicked()
{
// Make sure we're the only one touching the polys
ui->btnStart->setEnabled(false);
startStopAction->setEnabled(false);
ui->btnStart->setText("Start");
running = false;
app->processEvents();
ProgressDialog progress;
progress.setMax(polys.size());
progress.show();
for (int i=0; i<polys.size();)
{
if (!progress.isVisible())
break;
progress.increment();
app->processEvents();
// Remove broken polys
for (QPoint& point : polys[i].points)
{
if (point.x() > (int)width || point.x() < 0
|| point.y() > (int)height || point.y() < 0)
{
polys.remove(i);
generation++;
ui->generationLabel->setNum(generation);
// Go to the next poly
break;
}
}
// Remove polys that don't change or worsen the fitness
QVector<Poly> polyBak = polys;
polys.remove(i);
redraw(generated);
int newFit = computeFitness(generated);
if (newFit <= fitness)
{
fitness = newFit;
generation++;
ui->generationLabel->setNum(generation);
updateGuiFitness();
ui->imgBest->setPixmap(QPixmap::fromImage(generated));
ui->polysLabel->setNum(polys.size());
app->processEvents();
}
else
{
polys = polyBak;
i++;
}
}
startStopAction->setEnabled(true);
ui->btnStart->setEnabled(true);
}
void Widget::optimizeDnaClicked()
{
ui->btnStart->setEnabled(false);
startStopAction->setEnabled(false);
ui->btnStart->setText("Start");
running = false;
ProgressDialog progress;
progress.setMax(polys.size());
progress.show();
for (Poly& poly : polys)
{
if (!progress.isVisible())
break;
optimizeColors(generated, poly, true);
progress.increment();
app->processEvents();
}
startStopAction->setEnabled(true);
ui->btnStart->setEnabled(true);
}