]> git.mxchange.org Git - flightgear.git/blobdiff - src/Time/sunsolver.cxx
Logging: less verbose panel loading.
[flightgear.git] / src / Time / sunsolver.cxx
index 0b16f2de777a1bec6abbf7b279e29655a906f155..504de90817387a90011ab97e38eb8fe1085790e9 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Written by Curtis Olson, started September 2003.
  *
- * Copyright (C) 2003  Curtis L. Olson  - curt@flightgear.org
+ * Copyright (C) 2003  Curtis L. Olson  - http://www.flightgear.org/~curt
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
  * $Id$
  */
 
+#ifdef HAVE_CONFIG_H
+#      include <config.h>
+#endif
 
-#include <simgear/math/point3d.hxx>
-#include <simgear/math/sg_geodesy.hxx>
+#include <cmath>
+#include <ctime>
+
+#include <simgear/math/SGMath.hxx>
+#include <simgear/ephemeris/ephemeris.hxx>
 #include <simgear/timing/sg_time.hxx>
 
 #include <Main/globals.hxx>
 
-#include "sunpos.hxx"
-
+#include "tmp.hxx"
 #include "sunsolver.hxx"
 
 
@@ -39,33 +44,62 @@ static const time_t day_secs = 86400;
 static const time_t half_day_secs = day_secs / 2;
 static const time_t step_secs = 60;
 
-static double sun_angle( const SGTime &t, sgVec3 world_up,
-                         double lon_rad, double lat_rad ) {
-    sgVec3 nup, nsun;
-    Point3D p, rel_sunpos;
+/* given a particular time expressed in side real time at prime
+ * meridian (GST), compute position on the earth (lat, lon) such that
+ * sun is directly overhead.  (lat, lon are reported in radians */
+
+void fgSunPositionGST(double gst, double *lon, double *lat) {
+    /* time_t  ssue;           seconds since unix epoch */
+    /* double *lat;            (return) latitude        */
+    /* double *lon;            (return) longitude       */
+
+    double alpha, delta;
+    double tmp;
+
+    double beta = globals->get_ephem()->get_sun()->getLat();
+    // double r = globals->get_ephem()->get_sun()->getDistance();
+    double xs = globals->get_ephem()->get_sun()->getxs();
+    double ys = globals->get_ephem()->get_sun()->getys();
+    double ye = globals->get_ephem()->get_sun()->getye();
+    double ze = globals->get_ephem()->get_sun()->getze();
+    alpha = atan2(ys - tan(beta)*ze/ys, xs);
+    delta = asin(sin(beta)*ye/ys + cos(beta)*ze);
+
+    tmp = alpha - (SGD_2PI/24)*gst;
+    if (tmp < -SGD_PI) {
+        do tmp += SGD_2PI;
+        while (tmp < -SGD_PI);
+    } else if (tmp > SGD_PI) {
+        do tmp -= SGD_2PI;
+        while (tmp < -SGD_PI);
+    }
 
+    *lon = tmp;
+    *lat = delta;
+}
+
+static double sun_angle( const SGTime &t, const SGVec3d& world_up,
+                         double lon_rad, double lat_rad ) {
     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t.getGst() );
 
     double sun_lon, sun_gd_lat;
     fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
-    Point3D sunpos = sgGeodToCart(Point3D(sun_lon, sun_gd_lat, 0));
+    SGVec3d sunpos = SGVec3d::fromGeod(SGGeod::fromRad(sun_lon, sun_gd_lat));
 
     SG_LOG( SG_EVENT, SG_DEBUG, "    t.cur_time = " << t.get_cur_time() );
     SG_LOG( SG_EVENT, SG_DEBUG, 
            "    Sun Geodetic lat = " << sun_gd_lat );
 
     // calculate the sun's relative angle to local up
-    sgCopyVec3( nup, world_up );
-    sgSetVec3( nsun, sunpos.x(), sunpos.y(), sunpos.z() );
-    sgNormalizeVec3(nup);
-    sgNormalizeVec3(nsun);
+    SGVec3f nup = normalize(toVec3f(world_up));
+    SGVec3f nsun = normalize(toVec3f(sunpos));
     // cout << "nup = " << nup[0] << "," << nup[1] << "," 
     //      << nup[2] << endl;
     // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
     //      << nsun[2] << endl;
 
-    double sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
+    double sun_angle = acos( dot( nup, nsun ) );
     double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
     while ( sun_angle_deg < -180 ) { sun_angle += 360; }
     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
@@ -91,10 +125,7 @@ time_t fgTimeSecondsUntilSunAngle( time_t cur_time,
 {
     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
-    Point3D geod( lon_rad, lat_rad, 0 );
-    Point3D tmp = sgGeodToCart( geod );
-    sgVec3 world_up;
-    sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
+    SGVec3d world_up = SGVec3d::fromGeod(SGGeod::fromRad(lon_rad, lat_rad));
     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
 
     double best_diff = 180.0;