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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "SunLocator.h"
// C++ includes
#include <cmath>
// Qt includes
#include <QDateTime>
// Local includes
#include "MarbleGlobal.h"
#include "MarbleClock.h"
#include "Planet.h"
#include "MarbleMath.h"
#include "digikam_debug.h"
namespace Marble
{
using std::sin;
class Q_DECL_HIDDEN SunLocatorPrivate
{
public:
SunLocatorPrivate(const MarbleClock* clock, const Planet* planet)
: m_twilightZone(planet->twilightZone()),
m_clock(clock),
m_planet(planet)
{
planet->sunPosition(m_lon, m_lat, clock->dateTime());
}
qreal m_lon = 0.0;
qreal m_lat = 0.0;
qreal m_twilightZone = 0.0;
const MarbleClock* const m_clock = nullptr;
const Planet* m_planet = nullptr;
};
SunLocator::SunLocator(const MarbleClock* clock, const Planet* planet)
: QObject(),
d(new SunLocatorPrivate(clock, planet))
{
}
SunLocator::~SunLocator()
{
delete d;
}
qreal SunLocator::shading(qreal lon, qreal a, qreal c) const
{
// haversine formula
qreal b = sin((lon - d->m_lon) / 2.0);
// qreal g = sin((lat-d->m_lat)/2.0);
// qreal h = (g*g)+cos(lat)*cos(d->m_lat)*(b*b);
qreal h = (a * a) + c * (b * b);
/*
h = 0.0 // directly beneath sun
h = 0.5 // sunrise/sunset line
h = 1.0 // opposite side of earth to the sun
theta = 2*asin(sqrt(h))
*/
qreal brightness;
if (h <= 0.5 - d->m_twilightZone / 2.0)
{
brightness = 1.0;
}
else if (h >= 0.5 + d->m_twilightZone / 2.0)
{
brightness = 0.0;
}
else
{
brightness = (0.5 + d->m_twilightZone / 2.0 - h) / d->m_twilightZone;
}
return brightness;
}
void SunLocator::shadePixel(QRgb& pixcol, qreal brightness)
{
// daylight - no change
if (brightness > 0.99999)
{
return;
}
if (brightness < 0.00001)
{
// night
// Doing "pixcol = qRgb(r/2, g/2, b/2);" by shifting some electrons around ;)
// by shifting some electrons around ;)
pixcol = qRgb(qRed(pixcol) * 0.35, qGreen(pixcol) * 0.35, qBlue(pixcol) * 0.35);
// pixcol = (pixcol & 0xff000000) | ((pixcol >> 1) & 0x7f7f7f);
}
else
{
// gradual shadowing
int r = qRed(pixcol);
int g = qGreen(pixcol);
int b = qBlue(pixcol);
qreal d = 0.65 * brightness + 0.35;
pixcol = qRgb((int)(d * r), (int)(d * g), (int)(d * b));
}
}
void SunLocator::shadePixelComposite(QRgb& pixcol, const QRgb& dpixcol,
qreal brightness)
{
// daylight - no change
if (brightness > 0.99999)
{
return;
}
if (brightness < 0.00001)
{
// night
pixcol = dpixcol;
}
else
{
// gradual shadowing
qreal& d = brightness;<--- Variable 'd' can be declared as reference to const
int r = qRed(pixcol);
int g = qGreen(pixcol);
int b = qBlue(pixcol);
int dr = qRed(dpixcol);
int dg = qGreen(dpixcol);
int db = qBlue(dpixcol);
pixcol = qRgb((int)(d * r + (1 - d) * dr),
(int)(d * g + (1 - d) * dg),
(int)(d * b + (1 - d) * db));
}
}
void SunLocator::update()
{
d->m_planet->sunPosition(d->m_lon, d->m_lat, d->m_clock->dateTime());
Q_EMIT positionChanged(getLon(), getLat());
}
void SunLocator::setPlanet(const Planet* planet)
{
/*
// This won't work as expected if the same pointer
// points to different planets
if ( planet == d->m_planet ) {
return;
}
*/
const Planet* previousPlanet = d->m_planet;
qCDebug(DIGIKAM_MARBLE_LOG) << "SunLocator::setPlanet(Planet*)";
d->m_planet = planet;
d->m_twilightZone = planet->twilightZone();
planet->sunPosition(d->m_lon, d->m_lat, d->m_clock->dateTime());
// Initially there might be no planet set.
// In that case we don't want an update.
// Update the shading in all other cases.
if (!previousPlanet->id().isEmpty())
{
Q_EMIT positionChanged(getLon(), getLat());
}
}
qreal SunLocator::getLon() const
{
return d->m_lon * RAD2DEG;
}
qreal SunLocator::getLat() const
{
return d->m_lat * RAD2DEG;
}
} // namespace Marble
#include "moc_SunLocator.cpp"
|