-
Notifications
You must be signed in to change notification settings - Fork 0
/
circleapproximator.cpp
339 lines (293 loc) · 11.9 KB
/
circleapproximator.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
#include "circleapproximator.h"
namespace Circle{
void Approximator::processImage(QImage orig){
printf("starting circle approximation - random selector\n");
QTime timer;
timer.start();
//Some work to be done before getting started
origImage = orig.convertToFormat(QImage::Format_ARGB32_Premultiplied); // make sure we know the format
keepGoing = true;
Settings* sett = dynamic_cast<Settings*>(settingsObject);
int numCircles = sett->numCircles();
minRadius = sett->minRadius();
maxRadius = sett->maxRadius();
if(minRadius<1) minRadius = 1;
if(maxRadius>origImage.width()/2) maxRadius=origImage.width()/2;
if(maxRadius>origImage.height()/2) maxRadius=origImage.height()/2;
if(minRadius>maxRadius) minRadius = maxRadius;
precomputedDistance.clear(); // array used in the score and draw functions
precomputedDistance.reserve(maxRadius+2);
for(int z=0; z<maxRadius+2; z++) precomputedDistance.push_back(z*z);
currentApproximation = QImage(origImage.width(),origImage.height(),QImage::Format_ARGB32_Premultiplied);
currentApproximation.fill(0); // clear the image
#pragma omp parallel shared(keepGoing)
{
int circlesPerThread;
#pragma omp critical
{
int num_threads = omp_get_num_threads();
circlesPerThread = numCircles / num_threads;
if(omp_get_thread_num() == num_threads-1)
circlesPerThread += numCircles % num_threads; //the last thread gets the left over number of circles
}
int circle_num = 0;
while(circle_num<circlesPerThread){
struct Circle circle;
do{ //find a circle that helps make things better
randomSelectCurrentCircle(&circle);
circle.score = getScore(origImage,currentApproximation,
circle.centerX,circle.centerY,circle.radius,circle.color);
}while(circle.score<=0);
//permutate the circle until optimized
while(tryPermutationForBetterCircle(&circle));
drawCircle(currentApproximation,&circle);
if(!keepGoing)break;
#pragma omp master
{ // emit details of progress
double percentage = (circle_num+1)/(double)circlesPerThread*100;
emit progressMade(currentApproximation,percentage);
QCoreApplication::processEvents();
}
circle_num++;
}
}
printf("Done Approximating after %d ms\n",timer.elapsed());
emit doneProcessing(currentApproximation);
}
Approximator::Approximator(BaseSettings *sett):BaseApproximator(sett){}
int Approximator::randRange(int low, int high){
int range = high - low;
double percentage = (rand()/(double)RAND_MAX);
return percentage*range+low;
}
double Approximator::getScore(QImage &wantedImage, QImage &approximatedImage, int centerX, int centerY, int radius, QColor color){
//it returns a score of how much BETTER the circle will be than the current approximation
//A postitive return is a better circle than what is currently there
//consider distance = sqrt(x*x + y*y);
//if you know distance and y, solving for x terms gives
//d*d - y*y = x*x
//so since we know radius of the circle and the current height
//we can find the max horizontal distance to move
//this function only counts the pixels around the circumference of the circle
//this covers the typical case as we only really need to know if it is
//intersecting any other colored areas on the circumference
//so it gets the total difference of the current approximation and the difference the
//circle will have if drawn. If the new<current, then the new is less different than current frrom the wanted
//This also favors larger circles as a larger circle will potentially cover more area that
//is wrong and so has the chance to accrue more improvment over its longer circumference
long long totalApprox=0; // where we store the combined error of the pixels
long long totalColor=0;
int width = wantedImage.width();
int minx=centerX-radius;
int maxx=centerX+radius;
int miny=centerY-radius;
int maxy=centerY+radius;
if(minx<0) minx=0;
if(miny<0) miny=0;
if(maxx>=wantedImage.width()) maxx=wantedImage.width()-1;
if(maxy>=wantedImage.height()) maxy=wantedImage.height()-1;
if(maxy<=miny || maxx<=minx) return -10000; //something is not making sense, return a really bad estimate
int unSqauredRadius = precomputedDistance[radius]; // the max distance from the center anything should go
for(int y=miny;y<=maxy;y++){
int unSquaredHeight = precomputedDistance[abs(centerY-y)];
int maxXDist = unSqauredRadius - unSquaredHeight;
int x=1;
for(; precomputedDistance[x]<=maxXDist;++x); // get so x is JUST outside of the circle
--x; // go back inside the circle
int temp;
//right side
temp = centerX+x;
if(temp>=0 && temp<width){
QColor c1 = QColor::fromRgba(wantedImage.pixel(temp,y));
QColor c2 = QColor::fromRgba(approximatedImage.pixel(temp,y));
totalApprox += getColorDelta(c1,c2);
totalColor += getColorDelta(c1,color);
}
//left side
temp = centerX-x;
if(temp>=0 && temp<width){
QColor c1 = QColor::fromRgba(wantedImage.pixel(temp,y));
QColor c2 = QColor::fromRgba(approximatedImage.pixel(temp,y));
totalApprox += getColorDelta(c1,c2);
totalColor += getColorDelta(c1,color);
}
}
return totalApprox-totalColor;
}
int Approximator::getColorDelta(QColor c1, QColor c2){
int temp,total=0;
int h1,s1,l1,a1;
c1.getHsl(&h1,&s1,&l1,&a1);
int h2,s2,l2,a2;
c2.getHsl(&h2,&s2,&l2,&a2);
temp = abs(h1-h2);
total += temp;
temp = abs(s1-s2);
total += temp;
temp = abs(l1-l2);
total += temp*temp;
temp = abs(a1-a2);
total += temp;
return total;
}
void Approximator::drawCircle(QImage &image, struct Circle * circle){
//consider distance = sqrt(x*x + y*y);
//if you know distance and y, solving for x terms gives
//d*d - y*y = x*x
//so since we know radius of the circle and the current height
//we can find the max horizontal distance to move
//it starts from the top of the circle and goes down
//for every row, it starts from the middle and moves outwards
//This is used because it is faster the the QPainter drawing the circle
int ¢erX = circle->centerX;
int ¢erY = circle->centerY;
int &radius = circle->radius;
QColor &color = circle->color;
int width;
int height;
uchar *bits;// faster to directly modify bytes - the setPixel method is slow
#pragma omp critical
{
width = image.width();
height = image.height();
bits = image.bits(); // faster to directly modify bytes - the setPixel method is slow
}
int &unSqrtedDistance = precomputedDistance[radius]; // the max distance from the center anything should go
int yStartingNum = centerY-radius; // start at the top of the circle
if(yStartingNum<0) yStartingNum=0; // dont start outside the image
for(int y=yStartingNum; y<=centerY+radius && y<height; y++){
int lineOffset = y*width*4;
int unSqrtedHeight = precomputedDistance[abs(centerY-y)];
int maxXDist = unSqrtedDistance - unSqrtedHeight;
for(int x=0; precomputedDistance[x]<=maxXDist; x++){
int pixelOffset;
int temp;
//right side
temp = centerX+x;
if(temp<width){
pixelOffset = lineOffset + (temp*4);
bits[pixelOffset + 0] = color.blue();
bits[pixelOffset + 1] = color.green();
bits[pixelOffset + 2] = color.red();
bits[pixelOffset + 3] = color.alpha();
}
//left side
temp = centerX-x;
if(temp >= 0){
pixelOffset = lineOffset + (temp*4);
bits[pixelOffset + 0] = color.blue();
bits[pixelOffset + 1] = color.green();
bits[pixelOffset + 2] = color.red();
bits[pixelOffset + 3] = color.alpha();
}
}
}
}
void Approximator::randomSelectCurrentCircle(struct Circle * circle){
circle->centerX=randRange(0,origImage.width()-1);
circle->centerY=randRange(0,origImage.height()-1);
circle->radius = minRadius;
circle->color = QColor::fromRgba(origImage.pixel(circle->centerX,circle->centerY));
}
bool Approximator::tryPermutationForBetterCircle(struct Circle *circle){
double
score1 = getScore(origImage,currentApproximation,circle->centerX+1,circle->centerY,circle->radius,circle->color),
score2 = getScore(origImage,currentApproximation,circle->centerX-1,circle->centerY,circle->radius,circle->color),
score3 = getScore(origImage,currentApproximation,circle->centerX,circle->centerY+1,circle->radius,circle->color),
score4 = getScore(origImage,currentApproximation,circle->centerX,circle->centerY-1,circle->radius,circle->color),
score5 = getScore(origImage,currentApproximation,circle->centerX,circle->centerY,circle->radius+1,circle->color),
score6 = getScore(origImage,currentApproximation,circle->centerX,circle->centerY,circle->radius-1,circle->color)
;
if( circle->centerX<origImage.width() && score1>circle->score && score1>score2 && score1>score3 && score1>score4 && score1>score5 && score1>score6 ){
circle->centerX += 1;
circle->score = score1;
return true;
} else if( circle->centerX>0 && score2>circle->score && score2>score3 && score2>score4 && score2>score5 && score2>score6 ){
circle->centerX -= 1;
circle->score = score2;
return true;
} else if( circle->centerY<origImage.height() && score3>circle->score && score3>score4 && score3>score5 && score3>score6 ){
circle->centerY += 1;
circle->score = score3;
return true;
} else if( circle->centerY>0 && score4>circle->score && score4>score5 && score4>score6 ){
circle->centerY -= 1;
circle->score = score4;
return true;
} else if( circle->radius<maxRadius && score5>circle->score && score5>score6 ){
circle->radius += 1;
circle->score = score5;
return true;
} else if( circle->radius>minRadius && score6>circle->score ){
circle->radius -= 1;
circle->score = score6;
return true;
}
return false;
}
//======================================================================================================================================
//======================================================================================================================================
// Settings Class Below
//======================================================================================================================================
//======================================================================================================================================
Settings::Settings():BaseSettings(){
localApproximator = dynamic_cast<BaseApproximator*>(new Approximator(this));
makeWidgets();
layoutWidgets();
makeConnections();
}
void Settings::keepRadiusEntriesInSync(){
QObject *sender = QObject::sender();
int min = minRadiusEntry->value();
int max = maxRadiusEntry->value();
if(min<=max) return; // makes sense, the max value is large than the min
if(sender == minRadiusEntry){
maxRadiusEntry->setValue(min);
}else{
minRadiusEntry->setValue(max);
}
}
int Settings::numCircles(){
return numCirclesEntry->value();
}
int Settings::minRadius(){
return minRadiusEntry->value();
}
int Settings::maxRadius(){
return maxRadiusEntry->value();
}
QString Settings::getApproximatorName(){
return "Circle-Random";
}
void Settings::makeWidgets(){
description = new QLabel("This randomly chooses where to put a circle at");
numCirclesEntry = new QSpinBox();
minRadiusEntry = new QSpinBox();
maxRadiusEntry = new QSpinBox();
description->setWordWrap(true);
numCirclesEntry->setMinimum(1);
numCirclesEntry->setMaximum(1000000000);
numCirclesEntry->setValue(15000);
minRadiusEntry->setMinimum(1);
minRadiusEntry->setMaximum(9999);
minRadiusEntry->setValue(5);
maxRadiusEntry->setMinimum(1);
maxRadiusEntry->setMaximum(10000);
maxRadiusEntry->setValue(150);
}
void Settings::layoutWidgets(){
mainLayout = new QGridLayout();
setLayout(mainLayout);
mainLayout->addWidget(description,0,0,1,3);
mainLayout->addWidget(new QLabel("Num Circles"),1,0);
mainLayout->addWidget(numCirclesEntry,2,0);
mainLayout->addWidget(new QLabel("Min Radius"),1,1);
mainLayout->addWidget(minRadiusEntry,2,1);
mainLayout->addWidget(new QLabel("Max Radius"),1,2);
mainLayout->addWidget(maxRadiusEntry,2,2);
}
void Settings::makeConnections(){
connect(minRadiusEntry,SIGNAL(valueChanged(int)),this,SLOT(keepRadiusEntriesInSync()) );
connect(maxRadiusEntry,SIGNAL(valueChanged(int)),this,SLOT(keepRadiusEntriesInSync()) );
}
}//namespace