]> git.mxchange.org Git - flightgear.git/blobdiff - src/Time/sunsolver.cxx
plib-1.8.0 no longer defines the cchar type (which we used because we were
[flightgear.git] / src / Time / sunsolver.cxx
index c04e366dd2a70286c741df704033301f5f898ad8..0b16f2de777a1bec6abbf7b279e29655a906f155 100644 (file)
@@ -35,7 +35,9 @@
 #include "sunsolver.hxx"
 
 
-const time_t day_secs = 86400;
+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 ) {
@@ -45,18 +47,13 @@ static double sun_angle( const SGTime &t, sgVec3 world_up,
     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t.getGst() );
 
-    double sun_lon, sun_gd_lat, sun_gc_lat, sl_radius;
+    double sun_lon, sun_gd_lat;
     fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
-
-    sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &sun_gc_lat);
-
-    p = Point3D( sun_lon, sun_gc_lat, sl_radius );
-    Point3D sunpos = sgPolarToCart3d(p);
+    Point3D sunpos = sgGeodToCart(Point3D(sun_lon, sun_gd_lat, 0));
 
     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
-           << " Geocentric lat = " << sun_gc_lat );
+           "    Sun Geodetic lat = " << sun_gd_lat );
 
     // calculate the sun's relative angle to local up
     sgCopyVec3( nup, world_up );
@@ -79,48 +76,18 @@ static double sun_angle( const SGTime &t, sgVec3 world_up,
 
 
 /**
- * Given the current unix time in seconds, calculate seconds to noon
+ * Given the current unix time in seconds, calculate seconds to the
+ * specified sun angle (relative to straight up.)  Also specify if we
+ * want the angle while the sun is ascending or descending.  For
+ * instance noon is when the sun angle is 0 (or the closest it can
+ * get.)  Dusk is when the sun angle is 90 and descending.  Dawn is
+ * when the sun angle is 90 and ascending.
  */
-time_t fgTimeSecondsUntilNoon( time_t cur_time,
-                               double lon_rad,
-                               double lat_rad )
-{
-    // 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() );
-    SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
-
-    double best_angle = 180.0;
-    time_t best_time = cur_time;
-
-    for ( time_t secs = cur_time; secs < cur_time + day_secs; secs += 300 ) {
-        t.update( lon_rad, lat_rad, secs, 0 );
-        double angle = sun_angle( t, world_up, lon_rad, lat_rad );
-        if ( angle < best_angle ) {
-            // cout << "best angle = " << angle << " offset = "
-            // << secs - cur_time << endl;
-            best_angle = angle;
-            best_time = secs;
-        }
-    }
-
-    if ( best_time > day_secs / 2 ) {
-        best_time -= day_secs;
-    }
-
-    return best_time - cur_time;
-}
-
-
-/**
- * Given the current unix time in seconds, calculate seconds to midnight
- */
-time_t fgTimeSecondsUntilMidnight( time_t cur_time,
+time_t fgTimeSecondsUntilSunAngle( time_t cur_time,
                                    double lon_rad,
-                                   double lat_rad )
+                                   double lat_rad,
+                                   double target_angle_deg,
+                                   bool ascending )
 {
     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
@@ -130,96 +97,26 @@ time_t fgTimeSecondsUntilMidnight( time_t cur_time,
     sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
 
-    double best_angle = 0.0;
-    time_t best_time = cur_time;
-
-    for ( time_t secs = cur_time; secs < cur_time + day_secs; secs += 300 ) {
-        t.update( lon_rad, lat_rad, secs, 0 );
-        double angle = sun_angle( t, world_up, lon_rad, lat_rad );
-        if ( angle > best_angle ) {
-            // cout << "best angle = " << angle << " offset = "
-            //      << secs - cur_time << endl;
-            best_angle = angle;
-            best_time = secs;
-        }
-    }
-
-    if ( best_time > day_secs / 2 ) {
-        best_time -= day_secs;
-    }
-
-    return best_time - cur_time;
-}
-
-
-/**
- * Given the current unix time in seconds, calculate seconds to dusk
- */
-time_t fgTimeSecondsUntilDusk( time_t cur_time,
-                               double lon_rad,
-                               double lat_rad )
-{
-    // 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() );
-    SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
-
-    double best_diff = 90.0;
+    double best_diff = 180.0;
     double last_angle = -99999.0;
     time_t best_time = cur_time;
 
-    for ( time_t secs = cur_time; secs < cur_time + day_secs; secs += 300 ) {
+    for ( time_t secs = cur_time - half_day_secs;
+          secs < cur_time + half_day_secs;
+          secs += step_secs )
+    {
         t.update( lon_rad, lat_rad, secs, 0 );
-        double angle = sun_angle( t, world_up, lon_rad, lat_rad );
-        double diff = fabs( angle - 90.0 );
+        double angle_deg = sun_angle( t, world_up, lon_rad, lat_rad );
+        double diff = fabs( angle_deg - target_angle_deg );
         if ( diff < best_diff ) {
-            if ( last_angle <= 180.0 && ( last_angle < angle ) ) {
+            if ( last_angle <= 180.0 && ascending
+                 && ( last_angle > angle_deg ) ) {
                 // cout << "best angle = " << angle << " offset = "
                 //      << secs - cur_time << endl;
                 best_diff = diff;
                 best_time = secs;
-            }
-        }
-
-        last_angle = angle;
-    }
-
-    if ( best_time > day_secs / 2 ) {
-        best_time -= day_secs;
-    }
-
-    return best_time - cur_time;
-}
-
-
-/**
- * Given the current unix time in seconds, calculate seconds to dawn
- */
-time_t fgTimeSecondsUntilDawn( time_t cur_time,
-                               double lon_rad,
-                               double lat_rad )
-{
-    // 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() );
-    SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
-
-    double best_diff = 90.0;
-    double last_angle = -99999.0;
-    time_t best_time = cur_time;
-
-    for ( time_t secs = cur_time; secs < cur_time + day_secs; secs += 300 ) {
-        t.update( lon_rad, lat_rad, secs, 0 );
-        double angle = sun_angle( t, world_up, lon_rad, lat_rad );
-        double diff = fabs( angle - 90.0 );
-        if ( diff < best_diff ) {
-            if ( last_angle <= 180.0 && ( last_angle > angle ) ) {
+            } else if ( last_angle <= 180.0 && !ascending
+                        && ( last_angle < angle_deg ) ) {
                 // cout << "best angle = " << angle << " offset = "
                 //      << secs - cur_time << endl;
                 best_diff = diff;
@@ -227,11 +124,7 @@ time_t fgTimeSecondsUntilDawn( time_t cur_time,
             }
         }
 
-        last_angle = angle;
-    }
-
-    if ( best_time > day_secs / 2 ) {
-        best_time -= day_secs;
+        last_angle = angle_deg;
     }
 
     return best_time - cur_time;