]> git.mxchange.org Git - flightgear.git/blobdiff - Time/sunpos.cxx
Changes contributed by Durk Talsma:
[flightgear.git] / Time / sunpos.cxx
index cf0b54eefe3e679169dfabb26f3eaae43c28150f..024258828fc0a7da6f831ae70570fd2441726afe 100644 (file)
 #  include <config.h>
 #endif
 
-#include <math.h>
-#include <stdio.h>
-#include <time.h>
+#include <Include/compiler.h>
+
+#ifdef FG_HAVE_STD_INCLUDES
+#  include <cmath>
+#  include <cstdio>
+#  include <ctime>
+#else
+#  include <math.h>
+#  include <stdio.h>
+#  include <time.h>
+#endif
 
-//#include <Astro/orbits.hxx>
+#include <Debug/logstream.hxx>
 #include <Astro/solarsystem.hxx>
 #include <Include/fg_constants.h>
 #include <Main/views.hxx>
 extern SolarSystem *solarSystem;
 
 #undef E
-
-
-/*
- * the epoch upon which these astronomical calculations are based is
- * 1990 january 0.0, 631065600 seconds since the beginning of the
- * "unix epoch" (00:00:00 GMT, Jan. 1, 1970)
- *
- * given a number of seconds since the start of the unix epoch,
- * DaysSinceEpoch() computes the number of days since the start of the
- * astronomical epoch (1990 january 0.0)
- */
-
-#define EpochStart           (631065600)
-#define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
-
-/*
- * assuming the apparent orbit of the sun about the earth is circular,
- * the rate at which the orbit progresses is given by RadsPerDay --
- * FG_2PI radians per orbit divided by 365.242191 days per year:
- */
-
-#define RadsPerDay (FG_2PI/365.242191)
-
-/*
- * details of sun's apparent orbit at epoch 1990.0 (after
- * duffett-smith, table 6, section 46)
- *
- * Epsilon_g    (ecliptic longitude at epoch 1990.0) 279.403303 degrees
- * OmegaBar_g   (ecliptic longitude of perigee)      282.768422 degrees
- * Eccentricity (eccentricity of orbit)                0.016713
- */
-
-#define Epsilon_g    (279.403303*(FG_2PI/360))
-#define OmegaBar_g   (282.768422*(FG_2PI/360))
-#define Eccentricity (0.016713)
-
-/*
- * MeanObliquity gives the mean obliquity of the earth's axis at epoch
- * 1990.0 (computed as 23.440592 degrees according to the method given
- * in duffett-smith, section 27)
- */
 #define MeanObliquity (23.440592*(FG_2PI/360))
 
-/* static double solve_keplers_equation(double); */
-/* static double sun_ecliptic_longitude(time_t); */
 static void   ecliptic_to_equatorial(double, double, double *, double *);
 static double julian_date(int, int, int);
 static double GST(time_t);
 
-/*
- * solve Kepler's equation via Newton's method
- * (after duffett-smith, section 47)
- */
-/*
-static double solve_keplers_equation(double M) {
-    double E;
-    double delta;
-
-    E = M;
-    while (1) {
-       delta = E - Eccentricity*sin(E) - M;
-       if (fabs(delta) <= 1e-10) break;
-       E -= delta / (1 - Eccentricity*cos(E));
-    }
-
-    return E;
-}
-*/
-
-
-/* compute ecliptic longitude of sun (in radians) (after
- * duffett-smith, section 47) */
-/*
-static double sun_ecliptic_longitude(time_t ssue) {
-    // time_t ssue;              //  seconds since unix epoch
-    double D, N;
-    double M_sun, E;
-    double v;
-
-    D = DaysSinceEpoch(ssue);
-
-    N = RadsPerDay * D;
-    N = fmod(N, FG_2PI);
-    if (N < 0) N += FG_2PI;
-
-    M_sun = N + Epsilon_g - OmegaBar_g;
-    if (M_sun < 0) M_sun += FG_2PI;
-
-    E = solve_keplers_equation(M_sun);
-    v = 2 * atan(sqrt((1+Eccentricity)/(1-Eccentricity)) * tan(E/2));
-
-    return (v + OmegaBar_g);
-}
-*/
-
-
-/* convert from ecliptic to equatorial coordinates (after
- * duffett-smith, section 27) */
-
 static void ecliptic_to_equatorial(double lambda, double beta, 
                                   double *alpha, double *delta) {
     /* double  lambda;            ecliptic longitude       */
@@ -193,7 +108,8 @@ static double julian_date(int y, int m, int d) {
 
     /* lazy test to ensure gregorian calendar */
     if (y < 1583) {
-       printf("WHOOPS! Julian dates only valid for 1582 oct 15 or later\n");
+       FG_LOG( FG_EVENT, FG_ALERT, 
+               "WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
     }
 
     if ((m == 1) || (m == 2)) {
@@ -306,7 +222,8 @@ static void fgSunPositionGST(double gst, double *lon, double *lat) {
     /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
     //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
     ecliptic_to_equatorial( SolarSystem::theSolarSystem->getSun()->getLon(),
-                           0.0, &alpha,  &delta );
+                           SolarSystem::theSolarSystem->getSun()->getLat(),
+                           &alpha,  &delta );
 
 //    tmp = alpha - (FG_2PI/24)*GST(ssue);
     tmp = alpha - (FG_2PI/24)*gst;     
@@ -327,8 +244,8 @@ static void fgSunPositionGST(double gst, double *lon, double *lat) {
 void fgUpdateSunPos( void ) {
     fgLIGHT *l;
     fgTIME *t;
-    fgVIEW *v;
-    MAT3vec nup, nsun, v0;
+    FGView *v;
+    MAT3vec nup, nsun, v0, surface_to_sun;
     Point3D p, rel_sunpos;
     double dot, east_dot;
     double sun_gd_lat, sl_radius;
@@ -338,7 +255,7 @@ void fgUpdateSunPos( void ) {
     t = &cur_time_params;
     v = &current_view;
 
-    printf("  Updating Sun position\n");
+    FG_LOG( FG_EVENT, FG_INFO, "  Updating Sun position" );
 
     // (not sure why there was two)
     // fgSunPosition(t->cur_time, &l->sun_lon, &sun_gd_lat);
@@ -346,12 +263,13 @@ void fgUpdateSunPos( void ) {
 
     fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
 
-    p.setvals( l->sun_lon, l->sun_gc_lat, sl_radius );
+    p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
     l->fg_sunpos = fgPolarToCart3d(p);
 
-    printf( "    t->cur_time = %ld\n", t->cur_time);
-    printf( "    Sun Geodetic lat = %.5f Geocentric lat = %.5f\n",
-           sun_gd_lat, l->sun_gc_lat);
+    FG_LOG( FG_EVENT, FG_INFO, "    t->cur_time = " << t->cur_time );
+    FG_LOG( FG_EVENT, FG_INFO, 
+           "    Sun Geodetic lat = " << sun_gd_lat
+           << " Geocentric lat = " << l->sun_gc_lat );
 
     // I think this will work better for generating the sun light vector
     l->sun_vec[0] = l->fg_sunpos.x();
@@ -368,7 +286,7 @@ void fgUpdateSunPos( void ) {
     //        l->sun_vec[2]);
 
     // calculate the sun's relative angle to local up
-    MAT3_COPY_VEC(nup, v->local_up);
+    MAT3_COPY_VEC(nup, v->get_local_up());
     nsun[0] = l->fg_sunpos.x(); 
     nsun[1] = l->fg_sunpos.y();
     nsun[2] = l->fg_sunpos.z();
@@ -380,22 +298,23 @@ void fgUpdateSunPos( void ) {
     //        l->sun_angle);
     
     // calculate vector to sun's position on the earth's surface
-    rel_sunpos = l->fg_sunpos - (v->view_pos + scenery.center);
-    v->to_sun[0] = rel_sunpos.x();
-    v->to_sun[1] = rel_sunpos.y();
-    v->to_sun[2] = rel_sunpos.z();
+    rel_sunpos = l->fg_sunpos - (v->get_view_pos() + scenery.center);
+    v->set_to_sun( rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
     // printf( "Vector to sun = %.2f %.2f %.2f\n",
     //         v->to_sun[0], v->to_sun[1], v->to_sun[2]);
 
     // make a vector to the current view position
-    MAT3_SET_VEC(v0, v->view_pos.x(), v->view_pos.y(), v->view_pos.z());
+    Point3D view_pos = v->get_view_pos();
+    MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
 
     // Given a vector from the view position to the point on the
     // earth's surface the sun is directly over, map into onto the
     // local plane representing "horizontal".
-    map_vec_onto_cur_surface_plane(v->local_up, v0, v->to_sun, 
-                                  v->surface_to_sun);
-    MAT3_NORMALIZE_VEC(v->surface_to_sun, ntmp);
+    map_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_sun(), 
+                                   surface_to_sun );
+    MAT3_NORMALIZE_VEC(surface_to_sun, ntmp);
+    v->set_surface_to_sun( surface_to_sun[0], surface_to_sun[1], 
+                          surface_to_sun[2] );
     // printf("Surface direction to sun is %.2f %.2f %.2f\n",
     //        v->surface_to_sun[0], v->surface_to_sun[1], v->surface_to_sun[2]);
     // printf("Should be close to zero = %.2f\n", 
@@ -404,13 +323,13 @@ void fgUpdateSunPos( void ) {
     // calculate the angle between v->surface_to_sun and
     // v->surface_east.  We do this so we can sort out the acos()
     // ambiguity.  I wish I could think of a more efficient way ... :-(
-    east_dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_east);
+    east_dot = MAT3_DOT_PRODUCT( surface_to_sun, v->get_surface_east() );
     // printf("  East dot product = %.2f\n", east_dot);
 
     // calculate the angle between v->surface_to_sun and
     // v->surface_south.  this is how much we have to rotate the sky
     // for it to align with the sun
-    dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_south);
+    dot = MAT3_DOT_PRODUCT( surface_to_sun, v->get_surface_south() );
     // printf("  Dot product = %.2f\n", dot);
     if ( east_dot >= 0 ) {
        l->sun_rotation = acos(dot);
@@ -423,6 +342,38 @@ void fgUpdateSunPos( void ) {
 
 
 // $Log$
+// Revision 1.21  1999/03/22 02:08:18  curt
+// Changes contributed by Durk Talsma:
+//
+// Here's a few changes I made to fg-0.58 this weekend. Included are the
+// following features:
+// - Sun and moon have a halo
+// - The moon has a light vector, moon_angle, etc. etc. so that we can have
+//   some moonlight during the night.
+// - Lot's of small changes tweakes, including some stuff Norman Vine sent
+//   me earlier.
+//
+// Revision 1.20  1999/02/26 22:10:11  curt
+// Added initial support for native SGI compilers.
+//
+// Revision 1.19  1999/01/07 20:25:37  curt
+// Portability changes and updates from Bernie Bright.
+//
+// Revision 1.18  1998/12/09 18:50:36  curt
+// Converted "class fgVIEW" to "class FGView" and updated to make data
+// members private and make required accessor functions.
+//
+// Revision 1.17  1998/11/09 23:41:53  curt
+// Log message clean ups.
+//
+// Revision 1.16  1998/11/07 19:07:14  curt
+// Enable release builds using the --without-logging option to the configure
+// script.  Also a couple log message cleanups, plus some C to C++ comment
+// conversion.
+//
+// Revision 1.15  1998/10/18 01:17:24  curt
+// Point3D tweaks.
+//
 // Revision 1.14  1998/10/17 01:34:32  curt
 // C++ ifying ...
 //