-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapclass.cpp
More file actions
307 lines (272 loc) · 10.9 KB
/
mapclass.cpp
File metadata and controls
307 lines (272 loc) · 10.9 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
#include "mapclass.hpp"
MapClass::MapClass(QSettings &s, int width, int height) : settings(s)
{
mapWidth = width;
mapHeight = height;
renderingMaps=false;
}
MapClass::~MapClass()
{
}
bool MapClass::isRenderingMaps()
{
return renderingMaps;
}
void MapClass::generateMaps()
{
while (renderingMaps); // wait until other rendering process is complete
renderingMaps=true;
//QElapsedTimer timer;
//timer.start();
double centerLatitude = 0.0;
double centerLongitude = 0.0;
locator2longlat(¢erLongitude,
¢erLatitude,
settings.value(s_gridlocator, s_gridlocator_def).toString().toLatin1().data());
if (settings.value(s_mapFile, s_mapFile_def).toString().isNull()
|| settings.value(s_mapFile, s_mapFile_def).toString().isEmpty() ) {
return;
}
QImage rectMap(settings.value(s_mapFile, s_mapFile_def).toString());
QImage azimuthalMap(rectMap.height(),rectMap.height(),QImage::Format_ARGB32);
double centerLatitudeRadians = centerLatitude * M_PI / 180.0;
double centerLongitudeRadians = centerLongitude * M_PI / 180.0;
for (int azimY=0; azimY < azimuthalMap.height(); ++azimY) {
for (int azimX =0; azimX < azimuthalMap.width(); ++azimX) {
double distance;
double bearing;
if (pixelAz2DistAz(azimX,azimY,
azimuthalMap.width(),MAX_KM,
distance, bearing)) {
double longitude;
double latitude;
if (distAz2LongLat(distance, bearing,
centerLongitudeRadians, centerLatitudeRadians,
longitude, latitude)) {
int rectX;
int rectY;
if (longlat2PixelRect(longitude, latitude,
rectMap.width(), rectMap.height(),
rectX, rectY)) {
azimuthalMap.setPixelColor(azimX, azimY, rectMap.pixelColor(rectX, rectY));
//rectMap.pixelColor(rectX, rectY); // test
}
}
}
}
}
//qDebug() << timer.restart() << "ms";
QList<QPixmap> maps;
int steps = sqrt(azimuthalMap.width() - mapWidth) / 2;
for (int i=steps; i>0; --i) {
QImage image(azimuthalMap.scaled(
mapWidth + i * i * 2,
mapHeight + i * i * 2,
Qt::KeepAspectRatio,
Qt::SmoothTransformation
));
image = image.copy(QRect(image.width()/2 - mapWidth/2,
image.height()/2 - mapHeight/2,
mapWidth,
mapHeight) );
QImage clipped(mapWidth,mapHeight,QImage::Format_ARGB32);
clipped.fill(Qt::transparent);
QBrush brush(image); // textured brush
QPainter p(&clipped);
p.setBrush(brush);
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::Antialiasing);
//p.setRenderHint(QPainter::SmoothPixmapTransform);
p.drawEllipse(0,0,mapWidth,mapHeight);
p.end();
maps << QPixmap::fromImage(clipped);
}
//qDebug() << "generateMaps " << timer.elapsed() << "ms";
emit mapsReady(maps);
renderingMaps=false;
}
void MapClass::generateNightMaps()
{
//qDebug() << "map thread" << QThread::currentThreadId();
if (renderingMaps) return;
renderingMaps=true;
using namespace vector3;
//QElapsedTimer timer;
//timer.start();
// Qt methods
QDate date = QDateTime::currentDateTimeUtc().date();
QTime time = QDateTime::currentDateTimeUtc().time();
double hoursSinceMidnight = time.msecsSinceStartOfDay() / 1000.0 / 3600.0;
//int june21 = 172;
//if (date.isLeapYear(date.currentDate().year())) june21 = 173;
//int daysInYear = date.daysInYear();
int dayOfYear = date.dayOfYear();
//int year = date.currentDate().year();
/*
// c++ std
auto now = std::chrono::system_clock::now();
time_t tnow = std::chrono::system_clock::to_time_t(now);
tm *date_tm = std::gmtime(&tnow);
double hoursSinceMidnight = date_tm->tm_hour + date_tm->tm_min / 60.0 + date_tm->tm_sec / 3600.0;
int year = date_tm->tm_year + 1900;
int dayOfYear = date_tm->tm_yday + 1;
int daysInYear = 365;
int june21 = 172;
//date_tm->tm_hour = 0;
//date_tm->tm_min = 0;
//date_tm->tm_sec = 0;
//auto midnight = std::chrono::system_clock::from_time_t(timegm(date_tm)); // _mkgmtime() windows
//auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now-midnight);
//double hoursSinceMidnight = seconds.count() / 3600;
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { // leap year
daysInYear = 366;
june21 = 173;
}
*/
//double degPerDay = 360.0*M_PI/180.0/365.24;
//double radPerDay = TWO_PI/365.24;
//double tilt = 23.44 * cos(TWO_PI / daysInYear * (dayOfYear - june21)); // summer sol ref
//double tilt = -23.44 * cos(TWO_PI / daysInYear * ((dayOfYear-1) + 10)); // winter sol ref
//qDebug() << "Observable tilt " << tilt;
//hoursSinceMidnight += 6.0; // a
//double solarNoon = hoursSinceMidnight - 12.0;
//double solarNoonScaled = solarNoon / 24.0;
//hoursSinceMidnight += -3.0 + 24.0; // e
//while (hoursSinceMidnight > 24) hoursSinceMidnight -= 24;
//hoursSinceMidnight = fmod(hoursSinceMidnight + 6.0, 24.0);
//hoursSinceMidnight = fmod(hoursSinceMidnight, 24.0);
double daySinceMidnight = hoursSinceMidnight / 24.0;
double dayOfYearF = (dayOfYear-1) + daySinceMidnight;
double tilt = -23.44 * cos(TWO_PI / 365 * (dayOfYearF + 10.0)); // winter sol ref
/*
qDebug() << "June 21: " << june21;
qDebug() << "Days in current year " << daysInYear;
qDebug() << "Day of the year " << dayOfYear;
qDebug() << "Observable tilt " << tilt;
//qDebug() << "Current year " << year;
qDebug() << "hrs since midnight utc " << hoursSinceMidnight;
qDebug() << "hrs since midnight scaled " << hoursSinceMidnightScaled;
*/
Vec3d seasonOffset(0, 0, tan(TWO_PI * (tilt/360.0)));
Vec3d pointingFromEarthToSun(-cos((TWO_PI) * daySinceMidnight),
sin((TWO_PI) * daySinceMidnight),
0 );
//pointingFromEarthToSun = pointingFromEarthToSun + seasonOffset;
pointingFromEarthToSun += seasonOffset;
pointingFromEarthToSun.normalize();
/*
// plate carree projection
QImage nightRectMap(2048,1024,QImage::Format_ARGB32);
nightRectMap.fill(Qt::transparent);
for (int u=0; u < nightRectMap.width(); ++u) {
for (int v=0; v < nightRectMap.height(); ++v) {
double phi = ((double(v) / (double(nightRectMap.height()) * 2.0)) - 1.0) * TWO_PI;
double lambda = (double(u) / double(nightRectMap.width())) * TWO_PI;
double x = sin(phi) * cos(lambda);
double y = cos(phi);
double z = sin(phi) * sin(lambda);
Vec3d earthNormal(x, y, z);
earthNormal.normalize();
double angleBetweenSurfaceAndSunlight = dot(pointingFromEarthToSun, earthNormal);
//qDebug() << "angle " << angleBetweenSurfaceAndSunlight;
if (angleBetweenSurfaceAndSunlight <= -0.1) { // 6 deg
nightRectMap.setPixelColor(u, v, QColor(0,0,0,130)); // night
} else if (angleBetweenSurfaceAndSunlight < 0.0) {
nightRectMap.setPixelColor(u, v,
QColor(0,
0,
0,
90 - int(angleBetweenSurfaceAndSunlight * 400.0))); // twilight
//130));
} else {
// day
}
}
}
//nightRectMap.save("/home/eric/.config/softrx-client/nightRectMap.png");
*/
// equidistant azimuthal projection
//qDebug() << timer.restart() << "ms";
if (settings.value(s_mapFile, s_mapFile_def).toString().isNull()
|| settings.value(s_mapFile, s_mapFile_def).toString().isEmpty() ) {
return;
}
//qDebug() << "source map: " << settings.value(s_mapFile, s_mapFile_def).toString();
double centerLatitude = 0.0;
double centerLongitude = 0.0;
locator2longlat(¢erLongitude,
¢erLatitude,
settings.value(s_gridlocator, s_gridlocator_def).toString().toLatin1().data());
double centerLatitudeRadians = centerLatitude * M_PI / 180.0;
double centerLongitudeRadians = centerLongitude * M_PI / 180.0;
QImage rectMap(settings.value(s_mapFile, s_mapFile_def).toString());
QImage nightAzMap(rectMap.height(),rectMap.height(),QImage::Format_ARGB32);
nightAzMap.fill(Qt::transparent);
int transparency = settings.value(s_nighttransparency,s_nighttransparency_def).toInt();
transparency = transparency * 10 + 50;
for (int azimY=0; azimY < nightAzMap.height(); ++azimY) {
//QRgb *line = (QRgb *)nightAzMap.scanLine(azimY);
for (int azimX =0; azimX < nightAzMap.width(); ++azimX) {
double distance;
double bearing;
if (pixelAz2DistAz(azimX,azimY,
nightAzMap.width(),MAX_KM,
distance, bearing)) {
double longitude;
double latitude;
if (distAz2LongLat(distance, bearing,
centerLongitudeRadians, centerLatitudeRadians,
longitude, latitude
)) {
Vec3d earthNormal(
cos(latitude) * cos(longitude),
cos(latitude) * sin(longitude),
sin(latitude)
);
//earthNormal2.normalize();
double angleBetweenSurfaceAndSunlight = dot(pointingFromEarthToSun, earthNormal);
if (angleBetweenSurfaceAndSunlight < -0.1) { // 6 degrees
nightAzMap.setPixelColor(azimX,
azimY,
QColor(0,0,0,transparency)); // night
//line[azimX] = qRgba(qRed(0), qGreen(0), qBlue(0), qAlpha(70));
} else if (angleBetweenSurfaceAndSunlight < 0.0) {
int alpha = (transparency - 40) - int(angleBetweenSurfaceAndSunlight * 400.0);
nightAzMap.setPixelColor(azimX, azimY,QColor(0,0,0,alpha)); // twilight
//line[azimX] = qRgba(qRed(0), qGreen(0), qBlue(0), qAlpha(alpha));
}
}
}
}
}
//qDebug() << timer.restart() << "ms";
//nightAzMap.save("/home/eric/.config/softrx-client/nightAzMap.png");
QList<QPixmap> maps;
int steps = sqrt(nightAzMap.width() - mapWidth) / 2;
for (int i=steps; i>0; --i) {
QImage image(nightAzMap.scaled(
mapWidth + i * i * 2,
mapHeight + i * i * 2,
Qt::KeepAspectRatio,
Qt::SmoothTransformation
));
image = image.copy(QRect(image.width()/2 - mapWidth/2,
image.height()/2 - mapHeight/2,
mapWidth,
mapHeight) );
QImage clipped(mapWidth,mapHeight,QImage::Format_ARGB32);
clipped.fill(Qt::transparent);
QBrush brush(image); // textured brush
QPainter p(&clipped);
p.setBrush(brush);
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::Antialiasing);
//p.setRenderHint(QPainter::SmoothPixmapTransform);
p.drawEllipse(0,0,mapWidth,mapHeight);
p.end();
maps << QPixmap::fromImage(clipped);
}
//qDebug() << "generateNightMaps " << timer.elapsed() << "ms";
emit nightMapsReady(maps);
renderingMaps=false;
}