[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [marble] src/lib/marble: Refactor SunLocator to use astrolib instead of homebrewn code.
From:       Torsten Rahn <torsten.rahn () lge ! com>
Date:       2013-12-31 22:26:39
Message-ID: E1Vy7ln-0002Jr-UU () scm ! kde ! org
[Download RAW message or body]

Git commit 8219bb376fee182daa439956e9209c7af88b1b3a by Torsten Rahn.
Committed on 31/12/2013 at 22:23.
Pushed by rahn into branch 'master'.

Refactor SunLocator to use astrolib instead of homebrewn code.
This links libmarblewidget against astrolib directly.
Formerly we just got sunshading for earth and an approximation for the
moon. No we got phases for all planets and moons in the solar system!

Tested for Earth, the Moon and Mars (by comparing against the software
Mars24 which shows exactly the same phases).

M  +5    -4    src/lib/marble/CMakeLists.txt
M  +25   -60   src/lib/marble/SunLocator.cpp

http://commits.kde.org/marble/8219bb376fee182daa439956e9209c7af88b1b3a

diff --git a/src/lib/marble/CMakeLists.txt b/src/lib/marble/CMakeLists.txt
index 58c0474..cf83956 100644
--- a/src/lib/marble/CMakeLists.txt
+++ b/src/lib/marble/CMakeLists.txt
@@ -25,6 +25,7 @@ INCLUDE_DIRECTORIES(
  ${CMAKE_CURRENT_SOURCE_DIR}/graphicsview
  ${CMAKE_CURRENT_SOURCE_DIR}/layers
  ${CMAKE_CURRENT_SOURCE_DIR}/cloudsync
+ ${CMAKE_SOURCE_DIR}/src/lib/astro
  ${QT_INCLUDE_DIR}
 )
 
@@ -340,21 +341,21 @@ endif()
 
 if( PHONON_FOUND AND NOT QT5BUILD )
   INCLUDE_DIRECTORIES( ${PHONON_INCLUDES} )
-  TARGET_LINK_LIBRARIES(marblewidget ${PHONON_LIBS} )
+  TARGET_LINK_LIBRARIES(marblewidget astro ${PHONON_LIBS} )
   set (HAVE_PHONON TRUE)
 endif( PHONON_FOUND AND NOT QT5BUILD )
 
 if (APPLE)
   #defined in top level makefile
-  TARGET_LINK_LIBRARIES(marblewidget ${MAC_EXTRA_LIBS} )
+  TARGET_LINK_LIBRARIES(marblewidget astro ${MAC_EXTRA_LIBS} )
 endif (APPLE)
 
 if (CMAKE_SYSTEM_NAME MATCHES "SunOS")
-  TARGET_LINK_LIBRARIES(marblewidget m)
+  TARGET_LINK_LIBRARIES(marblewidget astro m)
 endif (CMAKE_SYSTEM_NAME MATCHES "SunOS")
 
 if(WIN32)
-  TARGET_LINK_LIBRARIES(marblewidget ws2_32 imm32 winmm)
+  TARGET_LINK_LIBRARIES(marblewidget astro ws2_32 imm32 winmm)
 endif(WIN32)
 
 
diff --git a/src/lib/marble/SunLocator.cpp b/src/lib/marble/SunLocator.cpp
index 4b4d35f..52d7c98 100644
--- a/src/lib/marble/SunLocator.cpp
+++ b/src/lib/marble/SunLocator.cpp
@@ -22,6 +22,8 @@
  
 #include "MarbleDebug.h"
 
+#include "src/lib/astro/solarsystem.h"
+
 #include <cmath>
 // M_PI is sometimes defined in <cmath>
 #ifndef M_PI 
@@ -80,66 +82,26 @@ SunLocator::~SunLocator()
 
 void SunLocator::updatePosition()
 {
-    if( d->m_planet->id() == "moon" ) {
-        // days since the first full moon of the 20th century
-        qreal days = (qreal)d->m_clock->dateTime().date().toJulianDay() + \
                d->m_clock->dayFraction() - MOON_EPOCH;
-
-        // number of orbits the moon has made (relative to the sun as observed from \
                earth)
-        days /= MOON_SYNODIC_PERIOD;
-
-        // take fractional part
-        days = days - (int)days;
-
-        // for dates before MOON_EPOCH
-        if (days < 0.0)
-            days += 1.0;
-
-        mDebug() << "MOON:" << (int)(days*100) << "% of orbit completed and"
-                 << (int)(abs((days-0.5)*2) * 100) << "% illuminated";
-
-        d->m_lon = (1-days) * 2*M_PI;
-
-        // not necessarily accurate but close enough
-        // (only differs by about +-6 degrees of this value)
-        d->m_lat = 0.0;
-        return;
-    }
-
-    // find current Julian day number relative to epoch J2000
-    long day = d->m_clock->dateTime().date().toJulianDay() - J2000;
-
-    // from http://www.astro.uu.nl/~strous/AA/en/reken/zonpositie.html
-    // mean anomaly
-    qreal M = d->m_planet->M_0() + d->m_planet->M_1()*day;
-
-    // equation of center
-    qreal C = d->m_planet->C_1()*sin(M) + d->m_planet->C_2()*sin(2*M)
-            + d->m_planet->C_3()*sin(3*M) + d->m_planet->C_4()*sin(4*M)
-            + d->m_planet->C_5()*sin(5*M) + d->m_planet->C_6()*sin(6*M);
-
-    // true anomaly
-    qreal nu = M + C;
-
-    // ecliptic longitude of sun as seen from planet
-    qreal lambda_sun = nu + d->m_planet->Pi() + M_PI;
-
-    // declination of sun as seen from planet
-    qreal delta_sun = asin(sin(d->m_planet->epsilon())*sin(lambda_sun));
-
-    // right ascension of sun as seen from planet
-    qreal alpha_sun = atan2(cos(d->m_planet->epsilon())*sin(lambda_sun), \
                cos(lambda_sun));
-
-    // solar noon occurs when sidereal time is equal to alpha_sun
-    qreal theta = alpha_sun;
-
-    // convert sidereal time to geographic longitude
-    d->m_lon = M_PI - (d->m_planet->theta_0() + d->m_planet->theta_1()
-                       * (day + d->m_clock->dayFraction()) - theta);
-
-    while(d->m_lon < 0)
-        d->m_lon += 2*M_PI;
-
-    d->m_lat = delta_sun;
+    QString planetId = d->m_planet->id();
+    SolarSystem sys;
+
+    QDateTime dateTime = d->m_clock->dateTime();
+    sys.setCurrentMJD(
+                dateTime.date().year(), dateTime.date().month(), \
dateTime.date().day(), +                dateTime.time().hour(), \
dateTime.time().minute(), +                (double)dateTime.time().second());
+    QString pname = planetId.at(0).toUpper() + planetId.right(planetId.size() - 1);
+    char *centralBody = pname.toLatin1().data();
+    sys.setCentralBody( centralBody );
+
+    double ra = 0.0;
+    double decl = 0.0;
+    sys.getSun( ra, decl );
+    double lon = 0.0;
+    double lat = 0.0;
+    sys.getPlanetographic (ra, decl, lon, lat);
+    d->m_lon = lon * DEG2RAD;
+    d->m_lat = lat * DEG2RAD;
 }
 
 
@@ -163,6 +125,9 @@ qreal SunLocator::shading(qreal lon, qreal a, qreal c) const
     if ( d->m_planet->id() == "earth" || d->m_planet->id() == "venus" ) {
         twilightZone = 0.1; // this equals 18 deg astronomical twilight.
     }
+    else if ( d->m_planet->id() == "mars" ) {
+        twilightZone = 0.05;
+    }
 
     qreal brightness;
     if ( h <= 0.5 - twilightZone / 2.0 )


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic