1998-10-17 01:33:52 +00:00
|
|
|
// sunpos.cxx (adapted from XEarth)
|
1998-04-25 20:24:00 +00:00
|
|
|
// kirk johnson
|
|
|
|
// july 1993
|
|
|
|
//
|
|
|
|
// code for calculating the position on the earth's surface for which
|
|
|
|
// the sun is directly overhead (adapted from _practical astronomy
|
|
|
|
// with your calculator, third edition_, peter duffett-smith,
|
|
|
|
// cambridge university press, 1988.)
|
|
|
|
//
|
|
|
|
// Copyright (C) 1989, 1990, 1993, 1994, 1995 Kirk Lauritz Johnson
|
|
|
|
//
|
|
|
|
// Parts of the source code (as marked) are:
|
|
|
|
// Copyright (C) 1989, 1990, 1991 by Jim Frost
|
|
|
|
// Copyright (C) 1992 by Jamie Zawinski <jwz@lucid.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify and freely distribute xearth for
|
|
|
|
// non-commercial and not-for-profit purposes is hereby granted
|
|
|
|
// without fee, provided that both the above copyright notice and this
|
|
|
|
// permission notice appear in all copies and in supporting
|
|
|
|
// documentation.
|
|
|
|
//
|
|
|
|
// The author makes no representations about the suitability of this
|
|
|
|
// software for any purpose. It is provided "as is" without express or
|
|
|
|
// implied warranty.
|
|
|
|
//
|
|
|
|
// THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
|
|
|
|
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
|
|
|
|
// OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// $Id$
|
1997-08-01 15:27:56 +00:00
|
|
|
|
|
|
|
|
1998-04-24 00:52:24 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2000-02-15 03:30:01 +00:00
|
|
|
#include <simgear/compiler.h>
|
1999-02-26 22:08:34 +00:00
|
|
|
|
1999-01-07 20:25:32 +00:00
|
|
|
#ifdef FG_HAVE_STD_INCLUDES
|
|
|
|
# include <cmath>
|
|
|
|
# include <cstdio>
|
|
|
|
# include <ctime>
|
2000-04-27 21:57:08 +00:00
|
|
|
# ifdef MACOS
|
|
|
|
FG_USING_STD(time_t);
|
|
|
|
# endif
|
1999-01-07 20:25:32 +00:00
|
|
|
#else
|
|
|
|
# include <math.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2000-02-15 03:30:01 +00:00
|
|
|
#include <simgear/constants.h>
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
2000-07-05 02:39:30 +00:00
|
|
|
#include <simgear/ephemeris/ephemeris.hxx>
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/math/fg_geodesy.hxx>
|
|
|
|
#include <simgear/math/point3d.hxx>
|
|
|
|
#include <simgear/math/polar3d.hxx>
|
|
|
|
#include <simgear/math/vector.hxx>
|
2000-07-06 22:13:24 +00:00
|
|
|
#include <simgear/timing/sg_time.hxx>
|
2000-02-15 03:30:01 +00:00
|
|
|
|
1998-04-22 13:24:04 +00:00
|
|
|
#include <Main/views.hxx>
|
1998-04-30 12:35:59 +00:00
|
|
|
#include <Scenery/scenery.hxx>
|
1998-04-22 13:24:04 +00:00
|
|
|
|
|
|
|
#include "sunpos.hxx"
|
1997-08-13 20:23:49 +00:00
|
|
|
|
2000-03-16 04:18:24 +00:00
|
|
|
// extern SolarSystem *solarSystem;
|
|
|
|
extern FGEphemeris *ephem;
|
1997-12-30 20:47:34 +00:00
|
|
|
|
1997-08-01 15:27:56 +00:00
|
|
|
#undef E
|
|
|
|
#define MeanObliquity (23.440592*(FG_2PI/360))
|
|
|
|
|
|
|
|
static void ecliptic_to_equatorial(double, double, double *, double *);
|
|
|
|
static double julian_date(int, int, int);
|
|
|
|
static double GST(time_t);
|
|
|
|
|
|
|
|
static void ecliptic_to_equatorial(double lambda, double beta,
|
|
|
|
double *alpha, double *delta) {
|
|
|
|
/* double lambda; ecliptic longitude */
|
|
|
|
/* double beta; ecliptic latitude */
|
|
|
|
/* double *alpha; (return) right ascension */
|
|
|
|
/* double *delta; (return) declination */
|
|
|
|
|
|
|
|
double sin_e, cos_e;
|
1998-08-12 21:13:22 +00:00
|
|
|
double sin_l, cos_l;
|
1997-08-01 15:27:56 +00:00
|
|
|
|
|
|
|
sin_e = sin(MeanObliquity);
|
|
|
|
cos_e = cos(MeanObliquity);
|
1998-08-12 21:13:22 +00:00
|
|
|
sin_l = sin(lambda);
|
|
|
|
cos_l = cos(lambda);
|
1997-08-01 15:27:56 +00:00
|
|
|
|
1998-08-12 21:13:22 +00:00
|
|
|
*alpha = atan2(sin_l*cos_e - tan(beta)*sin_e, cos_l);
|
|
|
|
*delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin_l);
|
1997-08-01 15:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* computing julian dates (assuming gregorian calendar, thus this is
|
|
|
|
* only valid for dates of 1582 oct 15 or later) (after duffett-smith,
|
|
|
|
* section 4) */
|
|
|
|
|
|
|
|
static double julian_date(int y, int m, int d) {
|
|
|
|
/* int y; year (e.g. 19xx) */
|
|
|
|
/* int m; month (jan=1, feb=2, ...) */
|
|
|
|
/* int d; day of month */
|
|
|
|
|
|
|
|
int A, B, C, D;
|
|
|
|
double JD;
|
|
|
|
|
|
|
|
/* lazy test to ensure gregorian calendar */
|
|
|
|
if (y < 1583) {
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_ALERT,
|
1998-11-07 19:07:06 +00:00
|
|
|
"WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
|
1997-08-01 15:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((m == 1) || (m == 2)) {
|
|
|
|
y -= 1;
|
|
|
|
m += 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
A = y / 100;
|
|
|
|
B = 2 - A + (A / 4);
|
1998-02-09 15:07:47 +00:00
|
|
|
C = (int)(365.25 * y);
|
|
|
|
D = (int)(30.6001 * (m + 1));
|
1997-08-01 15:27:56 +00:00
|
|
|
|
|
|
|
JD = B + C + D + d + 1720994.5;
|
|
|
|
|
|
|
|
return JD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* compute greenwich mean sidereal time (GST) corresponding to a given
|
|
|
|
* number of seconds since the unix epoch (after duffett-smith,
|
|
|
|
* section 12) */
|
|
|
|
static double GST(time_t ssue) {
|
|
|
|
/* time_t ssue; seconds since unix epoch */
|
|
|
|
|
|
|
|
double JD;
|
|
|
|
double T, T0;
|
|
|
|
double UT;
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
tm = gmtime(&ssue);
|
|
|
|
|
|
|
|
JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
|
|
|
|
T = (JD - 2451545) / 36525;
|
|
|
|
|
|
|
|
T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
|
|
|
|
|
|
|
|
T0 = fmod(T0, 24.0);
|
|
|
|
if (T0 < 0) T0 += 24;
|
|
|
|
|
|
|
|
UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
|
|
|
|
|
|
|
|
T0 += UT * 1.002737909;
|
|
|
|
T0 = fmod(T0, 24.0);
|
|
|
|
if (T0 < 0) T0 += 24;
|
|
|
|
|
|
|
|
return T0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* given a particular time (expressed in seconds since the unix
|
|
|
|
* epoch), compute position on the earth (lat, lon) such that sun is
|
|
|
|
* directly overhead. (lat, lon are reported in radians */
|
|
|
|
|
|
|
|
void fgSunPosition(time_t ssue, double *lon, double *lat) {
|
|
|
|
/* time_t ssue; seconds since unix epoch */
|
|
|
|
/* double *lat; (return) latitude */
|
|
|
|
/* double *lon; (return) longitude */
|
|
|
|
|
1998-02-23 19:07:49 +00:00
|
|
|
/* double lambda; */
|
1997-08-01 15:27:56 +00:00
|
|
|
double alpha, delta;
|
|
|
|
double tmp;
|
|
|
|
|
1998-02-23 19:07:49 +00:00
|
|
|
/* lambda = sun_ecliptic_longitude(ssue); */
|
|
|
|
/* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
|
1998-09-15 04:27:49 +00:00
|
|
|
//ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
|
|
|
|
|
|
|
|
/* **********************************************************************
|
|
|
|
* NOTE: in the next function, each time the sun's position is updated, the
|
|
|
|
* the sun's longitude is returned from solarSystem->sun. Note that the
|
|
|
|
* sun's position is updated at a much higher frequency than the rate at
|
|
|
|
* which the solar system's rebuilds occur. This is not a problem, however,
|
|
|
|
* because the fgSunPosition we're talking about here concerns the changing
|
|
|
|
* position of the sun due to the daily rotation of the earth.
|
|
|
|
* The ecliptic longitude, however, represents the position of the sun with
|
|
|
|
* respect to the stars, and completes just one cycle over the course of a
|
|
|
|
* year. Its therefore pretty safe to update the sun's longitude only once
|
|
|
|
* every ten minutes. (Comment added by Durk Talsma).
|
|
|
|
************************************************************************/
|
|
|
|
|
2000-03-16 04:18:24 +00:00
|
|
|
ecliptic_to_equatorial( ephem->get_sun()->getLon(),
|
1998-09-15 04:27:49 +00:00
|
|
|
0.0, &alpha, &delta );
|
1997-08-01 15:27:56 +00:00
|
|
|
tmp = alpha - (FG_2PI/24)*GST(ssue);
|
|
|
|
if (tmp < -FG_PI) {
|
|
|
|
do tmp += FG_2PI;
|
|
|
|
while (tmp < -FG_PI);
|
|
|
|
} else if (tmp > FG_PI) {
|
|
|
|
do tmp -= FG_2PI;
|
|
|
|
while (tmp < -FG_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
*lon = tmp;
|
|
|
|
*lat = delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-12 21:13:22 +00:00
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
static void fgSunPositionGST(double gst, double *lon, double *lat) {
|
|
|
|
/* time_t ssue; seconds since unix epoch */
|
|
|
|
/* double *lat; (return) latitude */
|
|
|
|
/* double *lon; (return) longitude */
|
|
|
|
|
|
|
|
/* double lambda; */
|
|
|
|
double alpha, delta;
|
|
|
|
double tmp;
|
|
|
|
|
|
|
|
/* lambda = sun_ecliptic_longitude(ssue); */
|
|
|
|
/* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
|
1998-09-15 04:27:49 +00:00
|
|
|
//ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
|
2000-03-16 04:18:24 +00:00
|
|
|
ecliptic_to_equatorial( ephem->get_sun()->getLon(),
|
|
|
|
ephem->get_sun()->getLat(),
|
|
|
|
&alpha, &delta );
|
1998-08-12 21:13:22 +00:00
|
|
|
|
|
|
|
// tmp = alpha - (FG_2PI/24)*GST(ssue);
|
|
|
|
tmp = alpha - (FG_2PI/24)*gst;
|
|
|
|
if (tmp < -FG_PI) {
|
|
|
|
do tmp += FG_2PI;
|
|
|
|
while (tmp < -FG_PI);
|
|
|
|
} else if (tmp > FG_PI) {
|
|
|
|
do tmp -= FG_2PI;
|
|
|
|
while (tmp < -FG_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
*lon = tmp;
|
|
|
|
*lat = delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-04-25 20:24:00 +00:00
|
|
|
// update the cur_time_params structure with the current sun position
|
1998-01-19 18:40:15 +00:00
|
|
|
void fgUpdateSunPos( void ) {
|
1998-04-26 05:10:00 +00:00
|
|
|
fgLIGHT *l;
|
2000-07-06 22:13:24 +00:00
|
|
|
SGTime *t;
|
1998-12-09 18:50:12 +00:00
|
|
|
FGView *v;
|
1999-10-03 18:49:39 +00:00
|
|
|
sgVec3 nup, nsun, v0, surface_to_sun;
|
1998-10-16 00:51:46 +00:00
|
|
|
Point3D p, rel_sunpos;
|
1998-07-22 21:45:37 +00:00
|
|
|
double dot, east_dot;
|
1998-04-25 20:24:00 +00:00
|
|
|
double sun_gd_lat, sl_radius;
|
1997-08-13 20:23:49 +00:00
|
|
|
|
1997-12-09 04:25:25 +00:00
|
|
|
l = &cur_light_params;
|
2000-07-06 22:13:24 +00:00
|
|
|
t = SGTime::cur_time_params;
|
1997-09-04 02:17:18 +00:00
|
|
|
v = ¤t_view;
|
1997-08-13 20:23:49 +00:00
|
|
|
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO, " Updating Sun position" );
|
1997-12-30 20:47:34 +00:00
|
|
|
|
1999-04-08 19:53:46 +00:00
|
|
|
fgSunPositionGST(t->getGst(), &l->sun_lon, &sun_gd_lat);
|
1997-08-13 20:23:49 +00:00
|
|
|
|
1997-12-09 04:25:25 +00:00
|
|
|
fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
|
1997-08-13 20:23:49 +00:00
|
|
|
|
1998-10-18 01:17:16 +00:00
|
|
|
p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
|
1998-05-02 01:53:17 +00:00
|
|
|
l->fg_sunpos = fgPolarToCart3d(p);
|
1997-08-27 03:29:38 +00:00
|
|
|
|
1999-04-08 19:53:46 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO, " t->cur_time = " << t->get_cur_time() );
|
1998-11-09 23:41:51 +00:00
|
|
|
FG_LOG( FG_EVENT, FG_INFO,
|
1998-11-07 19:07:06 +00:00
|
|
|
" Sun Geodetic lat = " << sun_gd_lat
|
|
|
|
<< " Geocentric lat = " << l->sun_gc_lat );
|
1997-09-04 02:17:18 +00:00
|
|
|
|
1999-10-03 18:49:39 +00:00
|
|
|
// update the sun light vector
|
|
|
|
sgSetVec4( l->sun_vec,
|
|
|
|
l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z(), 0.0 );
|
|
|
|
sgNormalizeVec4( l->sun_vec );
|
|
|
|
sgCopyVec4( l->sun_vec_inv, l->sun_vec );
|
|
|
|
sgNegateVec4( l->sun_vec_inv );
|
1997-11-15 18:15:39 +00:00
|
|
|
|
1998-04-25 20:24:00 +00:00
|
|
|
// make sure these are directional light sources only
|
1999-10-03 18:49:39 +00:00
|
|
|
l->sun_vec[3] = l->sun_vec_inv[3] = 0.0;
|
|
|
|
// cout << " l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
|
|
|
|
// << ","<< l->sun_vec[2] << endl;
|
1997-12-30 23:10:19 +00:00
|
|
|
|
1998-04-25 20:24:00 +00:00
|
|
|
// calculate the sun's relative angle to local up
|
1999-10-03 18:49:39 +00:00
|
|
|
sgCopyVec3( nup, v->get_local_up() );
|
|
|
|
sgSetVec3( nsun, l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z() );
|
|
|
|
sgNormalizeVec3(nup);
|
|
|
|
sgNormalizeVec3(nsun);
|
|
|
|
// cout << "nup = " << nup[0] << "," << nup[1] << ","
|
|
|
|
// << nup[2] << endl;
|
|
|
|
// cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
|
|
|
|
// << nsun[2] << endl;
|
|
|
|
|
|
|
|
l->sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
|
|
|
|
cout << "sun angle relative to current location = " << l->sun_angle << endl;
|
1998-04-25 20:24:00 +00:00
|
|
|
|
|
|
|
// calculate vector to sun's position on the earth's surface
|
1998-12-09 18:50:12 +00:00
|
|
|
rel_sunpos = l->fg_sunpos - (v->get_view_pos() + scenery.center);
|
|
|
|
v->set_to_sun( rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
|
1998-04-25 20:24:00 +00:00
|
|
|
// 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
|
1998-12-09 18:50:12 +00:00
|
|
|
Point3D view_pos = v->get_view_pos();
|
1999-10-03 18:49:39 +00:00
|
|
|
sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
|
1998-04-25 20:24:00 +00:00
|
|
|
|
|
|
|
// 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".
|
1999-10-03 18:49:39 +00:00
|
|
|
|
|
|
|
sgmap_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_sun(),
|
|
|
|
surface_to_sun );
|
|
|
|
sgNormalizeVec3(surface_to_sun);
|
1998-12-09 18:50:12 +00:00
|
|
|
v->set_surface_to_sun( surface_to_sun[0], surface_to_sun[1],
|
|
|
|
surface_to_sun[2] );
|
1999-10-03 18:49:39 +00:00
|
|
|
// cout << "(sg) Surface direction to sun is "
|
|
|
|
// << surface_to_sun[0] << ","
|
|
|
|
// << surface_to_sun[1] << ","
|
|
|
|
// << surface_to_sun[2] << endl;
|
|
|
|
// cout << "Should be close to zero = "
|
|
|
|
// << sgScalarProductVec3(nup, surface_to_sun) << endl;
|
1998-07-22 21:45:37 +00:00
|
|
|
|
|
|
|
// 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 ... :-(
|
1999-10-03 18:49:39 +00:00
|
|
|
east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
|
|
|
|
// cout << " East dot product = " << east_dot << endl;
|
1998-07-22 21:45:37 +00:00
|
|
|
|
|
|
|
// 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
|
1999-10-03 18:49:39 +00:00
|
|
|
dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
|
|
|
|
// cout << " Dot product = " << dot << endl;
|
|
|
|
|
1998-07-22 21:45:37 +00:00
|
|
|
if ( east_dot >= 0 ) {
|
|
|
|
l->sun_rotation = acos(dot);
|
|
|
|
} else {
|
|
|
|
l->sun_rotation = -acos(dot);
|
|
|
|
}
|
1999-10-03 18:49:39 +00:00
|
|
|
// cout << " Sky needs to rotate = " << angle << " rads = "
|
|
|
|
// << angle * RAD_TO_DEG << " degrees." << endl;
|
1997-08-13 20:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|