1 // sunpos.cxx (adapted from XEarth)
5 // code for calculating the position on the earth's surface for which
6 // the sun is directly overhead (adapted from _practical astronomy
7 // with your calculator, third edition_, peter duffett-smith,
8 // cambridge university press, 1988.)
10 // Copyright (C) 1989, 1990, 1993, 1994, 1995 Kirk Lauritz Johnson
12 // Parts of the source code (as marked) are:
13 // Copyright (C) 1989, 1990, 1991 by Jim Frost
14 // Copyright (C) 1992 by Jamie Zawinski <jwz@lucid.com>
16 // Permission to use, copy, modify and freely distribute xearth for
17 // non-commercial and not-for-profit purposes is hereby granted
18 // without fee, provided that both the above copyright notice and this
19 // permission notice appear in all copies and in supporting
22 // The author makes no representations about the suitability of this
23 // software for any purpose. It is provided "as is" without express or
26 // THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
27 // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
28 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
29 // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
30 // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
31 // NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
32 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 #include <simgear/compiler.h>
43 #ifdef FG_HAVE_STD_INCLUDES
56 #include <simgear/constants.h>
57 #include <simgear/debug/logstream.hxx>
58 #include <simgear/ephemeris/ephemeris.hxx>
59 #include <simgear/math/fg_geodesy.hxx>
60 #include <simgear/math/point3d.hxx>
61 #include <simgear/math/polar3d.hxx>
62 #include <simgear/math/vector.hxx>
63 #include <simgear/timing/sg_time.hxx>
65 #include <Main/views.hxx>
66 #include <Scenery/scenery.hxx>
70 // extern SolarSystem *solarSystem;
71 extern FGEphemeris *ephem;
74 #define MeanObliquity (23.440592*(FG_2PI/360))
76 static void ecliptic_to_equatorial(double, double, double *, double *);
77 static double julian_date(int, int, int);
78 static double GST(time_t);
80 static void ecliptic_to_equatorial(double lambda, double beta,
81 double *alpha, double *delta) {
82 /* double lambda; ecliptic longitude */
83 /* double beta; ecliptic latitude */
84 /* double *alpha; (return) right ascension */
85 /* double *delta; (return) declination */
90 sin_e = sin(MeanObliquity);
91 cos_e = cos(MeanObliquity);
95 *alpha = atan2(sin_l*cos_e - tan(beta)*sin_e, cos_l);
96 *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin_l);
100 /* computing julian dates (assuming gregorian calendar, thus this is
101 * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
104 static double julian_date(int y, int m, int d) {
105 /* int y; year (e.g. 19xx) */
106 /* int m; month (jan=1, feb=2, ...) */
107 /* int d; day of month */
112 /* lazy test to ensure gregorian calendar */
114 FG_LOG( FG_EVENT, FG_ALERT,
115 "WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
118 if ((m == 1) || (m == 2)) {
125 C = (int)(365.25 * y);
126 D = (int)(30.6001 * (m + 1));
128 JD = B + C + D + d + 1720994.5;
134 /* compute greenwich mean sidereal time (GST) corresponding to a given
135 * number of seconds since the unix epoch (after duffett-smith,
137 static double GST(time_t ssue) {
138 /* time_t ssue; seconds since unix epoch */
147 JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
148 T = (JD - 2451545) / 36525;
150 T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
153 if (T0 < 0) T0 += 24;
155 UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
157 T0 += UT * 1.002737909;
159 if (T0 < 0) T0 += 24;
165 /* given a particular time (expressed in seconds since the unix
166 * epoch), compute position on the earth (lat, lon) such that sun is
167 * directly overhead. (lat, lon are reported in radians */
169 void fgSunPosition(time_t ssue, double *lon, double *lat) {
170 /* time_t ssue; seconds since unix epoch */
171 /* double *lat; (return) latitude */
172 /* double *lon; (return) longitude */
178 /* lambda = sun_ecliptic_longitude(ssue); */
179 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
180 //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
182 /* **********************************************************************
183 * NOTE: in the next function, each time the sun's position is updated, the
184 * the sun's longitude is returned from solarSystem->sun. Note that the
185 * sun's position is updated at a much higher frequency than the rate at
186 * which the solar system's rebuilds occur. This is not a problem, however,
187 * because the fgSunPosition we're talking about here concerns the changing
188 * position of the sun due to the daily rotation of the earth.
189 * The ecliptic longitude, however, represents the position of the sun with
190 * respect to the stars, and completes just one cycle over the course of a
191 * year. Its therefore pretty safe to update the sun's longitude only once
192 * every ten minutes. (Comment added by Durk Talsma).
193 ************************************************************************/
195 ecliptic_to_equatorial( ephem->get_sun()->getLon(),
196 0.0, &alpha, &delta );
197 tmp = alpha - (FG_2PI/24)*GST(ssue);
200 while (tmp < -FG_PI);
201 } else if (tmp > FG_PI) {
203 while (tmp < -FG_PI);
211 /* given a particular time expressed in side real time at prime
212 * meridian (GST), compute position on the earth (lat, lon) such that
213 * sun is directly overhead. (lat, lon are reported in radians */
215 static void fgSunPositionGST(double gst, double *lon, double *lat) {
216 /* time_t ssue; seconds since unix epoch */
217 /* double *lat; (return) latitude */
218 /* double *lon; (return) longitude */
224 /* lambda = sun_ecliptic_longitude(ssue); */
225 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
226 //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
227 ecliptic_to_equatorial( ephem->get_sun()->getLon(),
228 ephem->get_sun()->getLat(),
231 // tmp = alpha - (FG_2PI/24)*GST(ssue);
232 tmp = alpha - (FG_2PI/24)*gst;
235 while (tmp < -FG_PI);
236 } else if (tmp > FG_PI) {
238 while (tmp < -FG_PI);
246 // update the cur_time_params structure with the current sun position
247 void fgUpdateSunPos( void ) {
251 sgVec3 nup, nsun, v0, surface_to_sun;
252 Point3D p, rel_sunpos;
253 double dot, east_dot;
254 double sun_gd_lat, sl_radius;
256 l = &cur_light_params;
257 t = SGTime::cur_time_params;
260 FG_LOG( FG_EVENT, FG_INFO, " Updating Sun position" );
262 fgSunPositionGST(t->getGst(), &l->sun_lon, &sun_gd_lat);
264 fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
266 p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
267 l->fg_sunpos = fgPolarToCart3d(p);
269 FG_LOG( FG_EVENT, FG_INFO, " t->cur_time = " << t->get_cur_time() );
270 FG_LOG( FG_EVENT, FG_INFO,
271 " Sun Geodetic lat = " << sun_gd_lat
272 << " Geocentric lat = " << l->sun_gc_lat );
274 // update the sun light vector
275 sgSetVec4( l->sun_vec,
276 l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z(), 0.0 );
277 sgNormalizeVec4( l->sun_vec );
278 sgCopyVec4( l->sun_vec_inv, l->sun_vec );
279 sgNegateVec4( l->sun_vec_inv );
281 // make sure these are directional light sources only
282 l->sun_vec[3] = l->sun_vec_inv[3] = 0.0;
283 // cout << " l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
284 // << ","<< l->sun_vec[2] << endl;
286 // calculate the sun's relative angle to local up
287 sgCopyVec3( nup, v->get_local_up() );
288 sgSetVec3( nsun, l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z() );
289 sgNormalizeVec3(nup);
290 sgNormalizeVec3(nsun);
291 // cout << "nup = " << nup[0] << "," << nup[1] << ","
292 // << nup[2] << endl;
293 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
294 // << nsun[2] << endl;
296 l->sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
297 cout << "sun angle relative to current location = " << l->sun_angle << endl;
299 // calculate vector to sun's position on the earth's surface
300 rel_sunpos = l->fg_sunpos - (v->get_view_pos() + scenery.center);
301 v->set_to_sun( rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
302 // printf( "Vector to sun = %.2f %.2f %.2f\n",
303 // v->to_sun[0], v->to_sun[1], v->to_sun[2]);
305 // make a vector to the current view position
306 Point3D view_pos = v->get_view_pos();
307 sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
309 // Given a vector from the view position to the point on the
310 // earth's surface the sun is directly over, map into onto the
311 // local plane representing "horizontal".
313 sgmap_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_sun(),
315 sgNormalizeVec3(surface_to_sun);
316 v->set_surface_to_sun( surface_to_sun[0], surface_to_sun[1],
318 // cout << "(sg) Surface direction to sun is "
319 // << surface_to_sun[0] << ","
320 // << surface_to_sun[1] << ","
321 // << surface_to_sun[2] << endl;
322 // cout << "Should be close to zero = "
323 // << sgScalarProductVec3(nup, surface_to_sun) << endl;
325 // calculate the angle between v->surface_to_sun and
326 // v->surface_east. We do this so we can sort out the acos()
327 // ambiguity. I wish I could think of a more efficient way ... :-(
328 east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
329 // cout << " East dot product = " << east_dot << endl;
331 // calculate the angle between v->surface_to_sun and
332 // v->surface_south. this is how much we have to rotate the sky
333 // for it to align with the sun
334 dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
335 // cout << " Dot product = " << dot << endl;
337 if ( east_dot >= 0 ) {
338 l->sun_rotation = acos(dot);
340 l->sun_rotation = -acos(dot);
342 // cout << " Sky needs to rotate = " << angle << " rads = "
343 // << angle * RAD_TO_DEG << " degrees." << endl;