-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoptimize.cpp
233 lines (220 loc) · 7.34 KB
/
optimize.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
#include "widget.h"
#include "ui_widget.h"
#include "settings.h"
#include <QPainter>
#include <QPen>
#include <QDebug>
using namespace std;
void Widget::optimizeColors(int polyIndex, QVector<Poly>& newPolys)
{
QImage predrawn = predraw(polyIndex);
optimizeColors(polyIndex, newPolys, predrawn);
}
void Widget::optimizeColors(int polyIndex, QVector<Poly>& newPolys, QImage& predrawn)
{
/*
// 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);
*/
int polysSize = newPolys.size();
Poly& poly = newPolys[polyIndex];
static QBrush brush(Qt::SolidPattern);
int processEventsRatelimit = 0;
// Check if the pic is better, commit and return if it is
auto validate = [&]()
{
QImage newGen = predrawn;
QPainter painter(&newGen);
painter.setPen(QPen(Qt::NoPen));
for (int i=polyIndex; i<polysSize; ++i)
{
brush.setColor(newPolys[i].color);
painter.setBrush(brush);
painter.drawPolygon(newPolys[i].points.data(), newPolys[i].points.size());
}
quint64 newFit = computeFitness(newGen);
generation++;
ui->generationLabel->setNum(generation);
if (newFit < fitness)
{
// Update data
generated = newGen;
fitness = newFit;
// Update GUI
if (processEventsRatelimit==0)
{
ui->imgBest->setPixmap(QPixmap::fromImage(generated));
updateGuiFitness();
}
return true;
}
else
return false;
};
app->processEvents();
int targetColor;
for (targetColor=0; targetColor <= 8; targetColor++)
{
for(;;)
{
if (processEventsRatelimit == GUI_REFRESH_RATE) // processEvents is a massive slowdown
{
processEventsRatelimit=0;
app->processEvents();
}
else
processEventsRatelimit++;
QColor color = poly.color, oldcolor=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;
if (!validate())
{
poly.color = oldcolor;
break;
}
}
}
generated = predrawn;
Poly::drawPoly(generated, poly);
app->processEvents();
}
void Widget::optimizeShape(int polyIndex, QVector<Poly>& newPolys)
{
QImage predrawn = predraw(polyIndex);
optimizeShape(polyIndex, newPolys, predrawn);
}
void Widget::optimizeShape(int polyIndex, QVector<Poly>& newPolys, QImage& predrawn)
{
int polysSize = newPolys.size();
Poly& poly = newPolys[polyIndex];
static QBrush brush(Qt::SolidPattern);
// Check if the pic is better, commit and return if it is
auto validate = [&]()
{
QImage newGen = predrawn;
QPainter painter(&newGen);
painter.setPen(QPen(Qt::NoPen));
for (int i=polyIndex; i<polysSize; ++i)
{
brush.setColor(newPolys[i].color);
painter.setBrush(brush);
painter.drawPolygon(newPolys[i].points.data(), newPolys[i].points.size());
}
quint64 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 processEventsRatelimit = 0;
for (QPoint& point : poly.points)
{
// Only try once each directions until they stop working
// Instead of retrying other directions after one stops working
// Call repeatedly to optimize further
int direction;
for (direction=0; direction<4; direction++)
{
for(;;)
{
if (processEventsRatelimit == GUI_REFRESH_RATE) // processEvents is a massive slowdown
{
processEventsRatelimit=0;
app->processEvents();
}
else
processEventsRatelimit++;
if (direction==0)
{
int oldy = point.y();
point.setY(max(oldy-N_POS_VAR,0));
if (!validate())
{
point.setY(oldy);
break;
}
else if (point.y()==0)
break;
}
else if (direction==1)
{
int oldx = point.x();
point.setX(min((unsigned)oldx+N_POS_VAR, width));
if (!validate())
{
point.setX(oldx);
break;
}
else if (point.x()==(int)width)
break;
}
else if (direction==2)
{
int oldy = point.y();
point.setY(min((unsigned)oldy+N_POS_VAR, height));
if (!validate())
{
point.setY(oldy);
break;
}
else if (point.y()==(int)height)
break;
}
else if (direction==3)
{
int oldx = point.x();
point.setX(max(oldx-N_POS_VAR,0));
if (!validate())
{
point.setX(oldx);
break;
}
else if (point.x()==0)
break;
}
}
}
}
generated = predrawn;
Poly::drawPoly(generated, poly);
app->processEvents();
}