-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropgenerator.cpp
More file actions
315 lines (271 loc) · 9.6 KB
/
dropgenerator.cpp
File metadata and controls
315 lines (271 loc) · 9.6 KB
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
#include "dropgenerator.h"
#include <QtMath>
#include <QVector2D>
#include <QQueue>
#include <QPair>
#include <QDebug>
#include <QDateTime>
#include <QImage>
#include <QVector>
#include <QColor>
QVector<QPointF> DropGenerator::generateTheoreticalModel(TheoreticalModelParameters params)
{
auto type = params.dropType;
auto b = params.b;
auto c = params.c;
auto cutoffMoment = params.cutoffMoment;
auto precision = params.precision;
const int maximumXDirectionChanges = (type == TheoreticalModelParameters::PENDANT ? 2 : 1);
const int maximumYDirectionChanges = (type == TheoreticalModelParameters::PENDANT ? 0 : 1);
int xDirectionChanges = 0, yDirectionChanges = 0;
double currentXDirection = 1, currentYDirection = 1; // Moving upwards to the right
const int maximumPassesOver1 = cutoffMoment;
int passesOver1 = 0;
QVector<QPointF> drop;
const double h = precision;
auto f1=[](double phi){return qCos(phi);};
auto f2=[](double phi){return qSin(phi);};
auto f3p=[b, c](double x, double z, double phi){return x == 0.0 ? b : 2*b + c*z - (qSin(phi) / x);};
auto f3r=[b, c](double x, double z, double phi){return x == 0.0 ? b : 2*b + c*x*x - (qSin(phi) / x);};
double currx = 0, nextx, currz = 0, nextz, currphi = 0, nextphi;
int steps = 0, maxSteps = 5000;
if(type == TheoreticalModelParameters::PENDANT)
{
while(steps <= maxSteps)
{
drop.append({currx, currz});
double k1, k2, k3, k4;
double l1, l2, l3, l4;
double m1, m2, m3, m4;
k1 = h*f1(currphi);
l1 = h*f2(currphi);
m1 = h*f3p(currx, currz, currphi);
k2 = h*f1(currphi + m1/2);
l2 = h*f2(currphi + m1/2);
m2 = h*f3p(currx + k1/2, currz + l1/2, currphi + m1/2);
k3 = h*f1(currphi + m2/2);
l3 = h*f2(currphi + m2/2);
m3 = h*f3p(currx + k2/2, currz + l2/2, currphi + m2/2);
k4 = h*f1(currphi + m3);
l4 = h*f2(currphi + m3);
m4 = h*f3p(currx + k3, currz + l3, currphi + m3);
nextx = currx + 1.0/6 * (k1 + 2*k2 + 2*k3 + k4);
nextz = currz + 1.0/6 * (l1 + 2*l2 + 2*l3 + l4);
nextphi = currphi + 1.0/6 * (m1 + 2*m2 + 2*m3 + m4);
if ((nextx > 1.0 && currx <= 1.0) || (nextx <= 1.0 && currx > 1.0))
if (++passesOver1 > maximumPassesOver1)
break;
if ((nextx - currx) * currentXDirection < 0.0) {
if (++xDirectionChanges > maximumXDirectionChanges)
break;
currentXDirection = nextx - currx;
}
if ((nextz - currz) * currentYDirection < 0.0) {
if (++yDirectionChanges > maximumYDirectionChanges)
break;
currentYDirection = nextz - currz;
}
currx = nextx;
currz = nextz;
currphi = nextphi;
++steps;
}
}
else{
while(steps <= maxSteps)
{
drop.append({currx, currz});
double k1, k2, k3, k4;
double l1, l2, l3, l4;
double m1, m2, m3, m4;
k1 = h*f1(currphi);
l1 = h*f2(currphi);
m1 = h*f3r(currx, currz, currphi);
k2 = h*f1(currphi + m1/2);
l2 = h*f2(currphi + m1/2);
m2 = h*f3r(currx + k1/2, currz + l1/2, currphi + m1/2);
k3 = h*f1(currphi + m2/2);
l3 = h*f2(currphi + m2/2);
m3 = h*f3r(currx + k2/2, currz + l2/2, currphi + m2/2);
k4 = h*f1(currphi + m3);
l4 = h*f2(currphi + m3);
m4 = h*f3r(currx + k3, currz + l3, currphi + m3);
nextx = currx + 1.0/6 * (k1 + 2*k2 + 2*k3 + k4);
nextz = currz + 1.0/6 * (l1 + 2*l2 + 2*l3 + l4);
nextphi = currphi + 1.0/6 * (m1 + 2*m2 + 2*m3 + m4);
if ((nextx - currx) * currentXDirection < 0.0) {
if (++xDirectionChanges > maximumXDirectionChanges)
break;
currentXDirection = nextx - currx;
}
if ((nextz - currz) * currentYDirection < 0.0) {
if (++yDirectionChanges > maximumYDirectionChanges)
break;
currentYDirection = nextz - currz;
}
currx = nextx;
currz = nextz;
currphi = nextphi;
if(currx <= std::numeric_limits<double>::epsilon())
break;
++steps;
}
}
if(steps >= maxSteps)
{
return {};
}
return drop;
}
static QVector<double> distFromApex(const QVector<QPointF> points)
{
QVector<double> result(points.size());
result[0] = 0.0;
double currentDist = 0.0;
for (int i = 1; i < points.size(); ++i) {
currentDist += QVector2D(points[i] - points[i - 1]).length();
result[i] = currentDist;
}
for (double &d : result)
d /= currentDist;
return result;
}
QVector<QPointF> DropGenerator::generateError(const QVector<QPointF> &theoretical, const QVector<QPointF> &experimental)
{
if (theoretical.isEmpty() || experimental.isEmpty()) {
return {};
}
QVector<QPointF> result(experimental.size());
QVector<double> tDistFromApex = distFromApex(theoretical);
QVector<double> eDistFromApex = distFromApex(experimental);
int currT = 0;
double errorX = 0.0;
double lastX = 0.0;
for (int e = 0; e < experimental.size(); ++e) {
errorX += qAbs(lastX - experimental[e].x());
double distFromApex = eDistFromApex[e];
for (int t = currT + 1; t < theoretical.size(); ++t) {
if (qAbs(tDistFromApex[currT] - distFromApex)
< qAbs(tDistFromApex[t] - distFromApex))
break;
++currT;
}
result[e] = QPointF(errorX, QVector2D(experimental[e] - theoretical[currT]).length());
lastX = experimental[e].x();
}
return result;
}
double DropGenerator::calculateError(const QVector<QPointF> &error)
{
if(error.isEmpty())
return qInf();
double errorAcc = 0;
for(auto point : error)
{
errorAcc += point.y() * point.y();
}
return errorAcc;
}
double DropGenerator::calculateError(const QVector<QPointF> &theoretical, const QVector<QPointF> &experimental)
{
if (theoretical.isEmpty() || experimental.isEmpty()) {
return qInf();
}
double result = 0.0;
QVector<double> tDistFromApex = distFromApex(theoretical);
QVector<double> eDistFromApex = distFromApex(experimental);
int currT = 0;
for (int e = 0; e < experimental.size(); ++e) {
double distFromApex = eDistFromApex[e];
for (int t = currT + 1; t < theoretical.size(); ++t) {
if (qAbs(tDistFromApex[currT] - distFromApex)
< qAbs(tDistFromApex[t] - distFromApex))
break;
++currT;
}
result += QVector2D(experimental[e] - theoretical[currT]).lengthSquared();
}
return result;
}
QVector<QPointF> DropGenerator::generateModelFromImage(const QString fileName)
{
QImage image;
if(!image.load(fileName))
{
qDebug() << "no";
return {};
}
int kernelDimention = 3;
QImage blurredImage(image.width(), image.height(), QImage::Format_RGB32);
for(int i = 0; i < image.height(); ++i)
{
for(int j = 0; j < image.width(); ++j)
{
double sum = 0.0;
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
if(i + y >= 0 && i + y < image.height()
&& j + x >= 0 && j + x < image.width())
{
sum += image.pixelColor(j + x, i + y).valueF();
}
else
{
sum += 1.0;
}
}
}
double blurredPixelValue;
blurredPixelValue = sum / (kernelDimention * kernelDimention);
blurredImage.setPixelColor(j, i, QColor(blurredPixelValue*255, blurredPixelValue*255, blurredPixelValue*255));
}
}
double min = qInf(), max = 0;
for(int i = 0; i < blurredImage.height(); ++i)
{
for(int j = 0; j < blurredImage.width(); ++j)
{
int r, g, b;
blurredImage.pixelColor(j, i).getRgb(&r, &g, &b);
double newColor;
newColor = 0.2126*r + 0.7152*g + 0.0722*b;
blurredImage.setPixelColor(j, i, QColor(newColor, newColor, newColor));
double pixelColor = blurredImage.pixelColor(j, i).valueF();
if(pixelColor < min)
min = pixelColor;
if(pixelColor > max)
max = pixelColor;
}
}
double averageValue = (min + max) / 2;
QVector<QPointF> drop;
int apex = 0, apexY = 0;
for(int i = blurredImage.height() - 1; i >= 0; --i)
{
for(int j = 0; j < blurredImage.width(); ++j)
{
double value = blurredImage.pixelColor(j, i).valueF();
if(value < averageValue)
{
drop.append(QPointF(j, i));
if(j > apex)
{
apex = j;
apexY = i;
}
break;
}
}
}
double pivot = drop.last().x();
double scaleFactor = apex - pivot;
for(auto &point : drop)
{
point.setX(point.x() + 2*(apex - point.x()) - apex);
point.setY(apexY - point.y());
point /= scaleFactor;
}
return drop;
}