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" );
261 FG_LOG( FG_EVENT, FG_INFO, " Gst = " << t->getGst() );
263 fgSunPositionGST(t->getGst(), &l->sun_lon, &sun_gd_lat);
265 fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
267 p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
268 l->fg_sunpos = fgPolarToCart3d(p);
270 FG_LOG( FG_EVENT, FG_INFO, " t->cur_time = " << t->get_cur_time() );
271 FG_LOG( FG_EVENT, FG_INFO,
272 " Sun Geodetic lat = " << sun_gd_lat
273 << " Geocentric lat = " << l->sun_gc_lat );
275 // update the sun light vector
276 sgSetVec4( l->sun_vec,
277 l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z(), 0.0 );
278 sgNormalizeVec4( l->sun_vec );
279 sgCopyVec4( l->sun_vec_inv, l->sun_vec );
280 sgNegateVec4( l->sun_vec_inv );
282 // make sure these are directional light sources only
283 l->sun_vec[3] = l->sun_vec_inv[3] = 0.0;
284 // cout << " l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
285 // << ","<< l->sun_vec[2] << endl;
287 // calculate the sun's relative angle to local up
288 sgCopyVec3( nup, v->get_local_up() );
289 sgSetVec3( nsun, l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z() );
290 sgNormalizeVec3(nup);
291 sgNormalizeVec3(nsun);
292 // cout << "nup = " << nup[0] << "," << nup[1] << ","
293 // << nup[2] << endl;
294 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
295 // << nsun[2] << endl;
297 l->sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
298 cout << "sun angle relative to current location = " << l->sun_angle << endl;
300 // calculate vector to sun's position on the earth's surface
301 rel_sunpos = l->fg_sunpos - (v->get_view_pos() + scenery.center);
302 v->set_to_sun( rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
303 // printf( "Vector to sun = %.2f %.2f %.2f\n",
304 // v->to_sun[0], v->to_sun[1], v->to_sun[2]);
306 // make a vector to the current view position
307 Point3D view_pos = v->get_view_pos();
308 sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
310 // Given a vector from the view position to the point on the
311 // earth's surface the sun is directly over, map into onto the
312 // local plane representing "horizontal".
314 sgmap_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_sun(),
316 sgNormalizeVec3(surface_to_sun);
317 v->set_surface_to_sun( surface_to_sun[0], surface_to_sun[1],
319 // cout << "(sg) Surface direction to sun is "
320 // << surface_to_sun[0] << ","
321 // << surface_to_sun[1] << ","
322 // << surface_to_sun[2] << endl;
323 // cout << "Should be close to zero = "
324 // << sgScalarProductVec3(nup, surface_to_sun) << endl;
326 // calculate the angle between v->surface_to_sun and
327 // v->surface_east. We do this so we can sort out the acos()
328 // ambiguity. I wish I could think of a more efficient way ... :-(
329 east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
330 // cout << " East dot product = " << east_dot << endl;
332 // calculate the angle between v->surface_to_sun and
333 // v->surface_south. this is how much we have to rotate the sky
334 // for it to align with the sun
335 dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
336 // cout << " Dot product = " << dot << endl;
338 if ( east_dot >= 0 ) {
339 l->sun_rotation = acos(dot);
341 l->sun_rotation = -acos(dot);
343 // cout << " Sky needs to rotate = " << angle << " rads = "
344 // << angle * RAD_TO_DEG << " degrees." << endl;