• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
SunLocator.cpp
Go to the documentation of this file.
1 // Copyright 2007-2009 David Roberts <dvdr18@gmail.com>
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 
16 #include "SunLocator.h"
17 
18 #include "MarbleGlobal.h"
19 #include "MarbleClock.h"
20 #include "Planet.h"
21 #include "MarbleMath.h"
22 
23 #include "MarbleDebug.h"
24 
25 #include "src/lib/astro/solarsystem.h"
26 
27 #include <cmath>
28 // M_PI is sometimes defined in <cmath>
29 #ifndef M_PI
30 #define M_PI 3.14159265358979323846264338327950288419717
31 #endif
32 
33 namespace Marble
34 {
35 
36 using std::sin;
37 using std::cos;
38 using std::asin;
39 using std::abs;
40 
41 class SunLocatorPrivate
42 {
43 public:
44  SunLocatorPrivate( const MarbleClock *clock, const Planet *planet )
45  : m_lon( 0.0 ),
46  m_lat( 0.0 ),
47  m_clock( clock ),
48  m_planet( planet )
49  {
50  }
51 
52  qreal m_lon;
53  qreal m_lat;
54 
55  const MarbleClock *const m_clock;
56  const Planet *m_planet;
57 };
58 
59 
60 SunLocator::SunLocator( const MarbleClock *clock, const Planet *planet )
61  : QObject(),
62  d( new SunLocatorPrivate( clock, planet ))
63 {
64 }
65 
66 SunLocator::~SunLocator()
67 {
68  delete d;
69 }
70 
71 void SunLocator::updatePosition()
72 {
73  QString planetId = d->m_planet->id();
74  SolarSystem sys;
75 
76  QDateTime dateTime = d->m_clock->dateTime();
77  sys.setCurrentMJD(
78  dateTime.date().year(), dateTime.date().month(), dateTime.date().day(),
79  dateTime.time().hour(), dateTime.time().minute(),
80  (double)dateTime.time().second());
81  QString const pname = planetId.at(0).toUpper() + planetId.right(planetId.size() - 1);
82  QByteArray const name = pname.toLatin1();
83  sys.setCentralBody( name.data() );
84 
85  double ra = 0.0;
86  double decl = 0.0;
87  sys.getSun( ra, decl );
88  double lon = 0.0;
89  double lat = 0.0;
90  sys.getPlanetographic (ra, decl, lon, lat);
91  d->m_lon = lon * DEG2RAD;
92  d->m_lat = lat * DEG2RAD;
93 }
94 
95 
96 qreal SunLocator::shading(qreal lon, qreal a, qreal c) const
97 {
98  // haversine formula
99  qreal b = sin((lon-d->m_lon)/2.0);
100 // qreal g = sin((lat-d->m_lat)/2.0);
101 // qreal h = (g*g)+cos(lat)*cos(d->m_lat)*(b*b);
102  qreal h = (a*a) + c * (b*b);
103 
104  /*
105  h = 0.0 // directly beneath sun
106  h = 0.5 // sunrise/sunset line
107  h = 1.0 // opposite side of earth to the sun
108  theta = 2*asin(sqrt(h))
109  */
110 
111  qreal twilightZone = 0.0;
112 
113  QString planetId = d->m_planet->id();
114  if ( planetId == "earth" || planetId == "venus") {
115  twilightZone = 0.1; // this equals 18 deg astronomical twilight.
116  }
117  else if ( planetId == "mars" ) {
118  twilightZone = 0.05;
119  }
120 
121  qreal brightness;
122  if ( h <= 0.5 - twilightZone / 2.0 )
123  brightness = 1.0;
124  else if ( h >= 0.5 + twilightZone / 2.0 )
125  brightness = 0.0;
126  else
127  brightness = ( 0.5 + twilightZone/2.0 - h ) / twilightZone;
128 
129  return brightness;
130 }
131 
132 void SunLocator::shadePixel(QRgb& pixcol, qreal brightness) const
133 {
134  // daylight - no change
135  if ( brightness > 0.99999 )
136  return;
137 
138  if ( brightness < 0.00001 ) {
139  // night
140  // Doing "pixcol = qRgb(r/2, g/2, b/2);" by shifting some electrons around ;)
141  // by shifting some electrons around ;)
142  pixcol = qRgb(qRed(pixcol) * 0.35, qGreen(pixcol) * 0.35, qBlue(pixcol) * 0.35);
143  // pixcol = (pixcol & 0xff000000) | ((pixcol >> 1) & 0x7f7f7f);
144  } else {
145  // gradual shadowing
146  int r = qRed( pixcol );
147  int g = qGreen( pixcol );
148  int b = qBlue( pixcol );
149  qreal d = 0.65 * brightness + 0.35;
150  pixcol = qRgb((int)(d * r), (int)(d * g), (int)(d * b));
151  }
152 }
153 
154 void SunLocator::shadePixelComposite(QRgb& pixcol, const QRgb& dpixcol,
155  qreal brightness) const
156 {
157  // daylight - no change
158  if ( brightness > 0.99999 )
159  return;
160 
161  if ( brightness < 0.00001 ) {
162  // night
163  pixcol = dpixcol;
164  } else {
165  // gradual shadowing
166  qreal& d = brightness;
167 
168  int r = qRed( pixcol );
169  int g = qGreen( pixcol );
170  int b = qBlue( pixcol );
171 
172  int dr = qRed( dpixcol );
173  int dg = qGreen( dpixcol );
174  int db = qBlue( dpixcol );
175 
176  pixcol = qRgb( (int)( d * r + (1 - d) * dr ),
177  (int)( d * g + (1 - d) * dg ),
178  (int)( d * b + (1 - d) * db ) );
179  }
180 }
181 
182 void SunLocator::update()
183 {
184  updatePosition();
185 
186  emit positionChanged( getLon(), getLat() );
187 }
188 
189 void SunLocator::setPlanet( const Planet *planet )
190 {
191  /*
192  // This won't work as expected if the same pointer
193  // points to different planets
194  if ( planet == d->m_planet ) {
195  return;
196  }
197  */
198 
199  const Planet *previousPlanet = d->m_planet;
200 
201  mDebug() << "SunLocator::setPlanet(Planet*)";
202  d->m_planet = planet;
203  updatePosition();
204 
205  // Initially there might be no planet set.
206  // In that case we don't want an update.
207  // Update the shading in all other cases.
208  if ( !previousPlanet->id().isEmpty() ) {
209  emit positionChanged( getLon(), getLat() );
210  }
211 }
212 
213 qreal SunLocator::getLon() const
214 {
215  return d->m_lon * RAD2DEG;
216 }
217 
218 qreal SunLocator::getLat() const
219 {
220  return d->m_lat * RAD2DEG;
221 }
222 
223 }
224 
225 #include "SunLocator.moc"
Marble::RAD2DEG
const qreal RAD2DEG
Definition: MarbleGlobal.h:220
QTime::minute
int minute() const
Marble::Planet::id
QString id() const
The internal, nonlocalized name of the planet.
Definition: Planet.cpp:262
SolarSystem::setCentralBody
void setCentralBody(const char *pname)
Definition: solarsystem.cpp:293
MarbleMath.h
QByteArray
Marble::SunLocator::update
void update()
Definition: SunLocator.cpp:182
QString::size
int size() const
QDateTime::time
QTime time() const
SunLocator.h
Planet.h
QDate::month
int month() const
MarbleDebug.h
QTime::second
int second() const
QObject::name
const char * name() const
Marble::SunLocator::getLat
qreal getLat() const
Definition: SunLocator.cpp:218
QObject
SolarSystem
Definition: solarsystem.h:17
QString::isEmpty
bool isEmpty() const
QDate::day
int day() const
Marble::Planet
Definition: Planet.h:25
Marble::SunLocator::setPlanet
void setPlanet(const Planet *planet)
Definition: SunLocator.cpp:189
QDate::year
int year() const
QString
Marble::SunLocator::shading
qreal shading(qreal lon, qreal a, qreal c) const
Definition: SunLocator.cpp:96
MarbleGlobal.h
Marble::DEG2RAD
const qreal DEG2RAD
Definition: MarbleGlobal.h:219
abs
double abs(const Vec3 &c)
Definition: attlib.cpp:100
QString::right
QString right(int n) const
QTime::hour
int hour() const
solarsystem.h
MarbleClock.h
Marble::SunLocator::getLon
qreal getLon() const
Definition: SunLocator.cpp:213
Marble::SunLocator::shadePixelComposite
void shadePixelComposite(QRgb &pixcol, const QRgb &dpixcol, qreal shade) const
Definition: SunLocator.cpp:154
Marble::SunLocator::shadePixel
void shadePixel(QRgb &pixcol, qreal shade) const
Definition: SunLocator.cpp:132
Marble::SunLocator::SunLocator
SunLocator(const MarbleClock *clock, const Planet *planet)
Definition: SunLocator.cpp:60
QString::toLatin1
QByteArray toLatin1() const
QDateTime::date
QDate date() const
QChar::toUpper
QChar toUpper() const
SolarSystem::getSun
void getSun(double &ra, double &decl)
Definition: solarsystem.cpp:471
Marble::SunLocator::~SunLocator
virtual ~SunLocator()
Definition: SunLocator.cpp:66
QString::at
const QChar at(int position) const
QByteArray::data
char * data()
Marble::SunLocator::positionChanged
void positionChanged(qreal lon, qreal lat)
SolarSystem::getPlanetographic
void getPlanetographic(double ra, double decl, double &lng, double &lat)
Definition: solarsystem.cpp:1387
SolarSystem::setCurrentMJD
void setCurrentMJD(int year, int month, int day, int hour, int min, double sec)
Definition: solarsystem.cpp:193
Marble::MarbleClock
Definition: MarbleClock.h:25
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
QDateTime
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal