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 SG_HAVE_STD_INCLUDES
56 #include <simgear/constants.h>
57 #include <simgear/debug/logstream.hxx>
58 #include <simgear/ephemeris/ephemeris.hxx>
59 #include <simgear/math/point3d.hxx>
60 #include <simgear/math/polar3d.hxx>
61 #include <simgear/math/sg_geodesy.hxx>
62 #include <simgear/math/vector.hxx>
63 #include <simgear/timing/sg_time.hxx>
65 #include <Main/globals.hxx>
66 #include <Main/viewer.hxx>
67 #include <Scenery/scenery.hxx>
68 #include <Time/light.hxx>
72 // #undef E // should no longer be needed
73 #define MeanObliquity (23.440592*(SGD_2PI/360))
75 static void ecliptic_to_equatorial(double, double, double *, double *);
76 static double julian_date(int, int, int);
77 static double GST(time_t);
79 static void ecliptic_to_equatorial(double lambda, double beta,
80 double *alpha, double *delta) {
81 /* double lambda; ecliptic longitude */
82 /* double beta; ecliptic latitude */
83 /* double *alpha; (return) right ascension */
84 /* double *delta; (return) declination */
89 sin_e = sin(MeanObliquity);
90 cos_e = cos(MeanObliquity);
94 *alpha = atan2(sin_l*cos_e - tan(beta)*sin_e, cos_l);
95 *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin_l);
99 /* computing julian dates (assuming gregorian calendar, thus this is
100 * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
103 static double julian_date(int y, int m, int d) {
104 /* int y; year (e.g. 19xx) */
105 /* int m; month (jan=1, feb=2, ...) */
106 /* int d; day of month */
111 /* lazy test to ensure gregorian calendar */
113 SG_LOG( SG_EVENT, SG_ALERT,
114 "WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
117 if ((m == 1) || (m == 2)) {
124 C = (int)(365.25 * y);
125 D = (int)(30.6001 * (m + 1));
127 JD = B + C + D + d + 1720994.5;
133 /* compute greenwich mean sidereal time (GST) corresponding to a given
134 * number of seconds since the unix epoch (after duffett-smith,
136 static double GST(time_t ssue) {
137 /* time_t ssue; seconds since unix epoch */
146 JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
147 T = (JD - 2451545) / 36525;
149 T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
152 if (T0 < 0) T0 += 24;
154 UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
156 T0 += UT * 1.002737909;
158 if (T0 < 0) T0 += 24;
164 /* given a particular time (expressed in seconds since the unix
165 * epoch), compute position on the earth (lat, lon) such that sun is
166 * directly overhead. (lat, lon are reported in radians */
168 void fgSunPosition(time_t ssue, double *lon, double *lat) {
169 /* time_t ssue; seconds since unix epoch */
170 /* double *lat; (return) latitude */
171 /* double *lon; (return) longitude */
177 /* lambda = sun_ecliptic_longitude(ssue); */
178 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
179 //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
181 /* **********************************************************************
182 * NOTE: in the next function, each time the sun's position is updated, the
183 * the sun's longitude is returned from solarSystem->sun. Note that the
184 * sun's position is updated at a much higher frequency than the rate at
185 * which the solar system's rebuilds occur. This is not a problem, however,
186 * because the fgSunPosition we're talking about here concerns the changing
187 * position of the sun due to the daily rotation of the earth.
188 * The ecliptic longitude, however, represents the position of the sun with
189 * respect to the stars, and completes just one cycle over the course of a
190 * year. Its therefore pretty safe to update the sun's longitude only once
191 * every ten minutes. (Comment added by Durk Talsma).
192 ************************************************************************/
194 ecliptic_to_equatorial( globals->get_ephem()->get_sun()->getLon(),
195 0.0, &alpha, &delta );
196 tmp = alpha - (SGD_2PI/24)*GST(ssue);
199 while (tmp < -SGD_PI);
200 } else if (tmp > SGD_PI) {
202 while (tmp < -SGD_PI);
210 /* given a particular time expressed in side real time at prime
211 * meridian (GST), compute position on the earth (lat, lon) such that
212 * sun is directly overhead. (lat, lon are reported in radians */
214 static void fgSunPositionGST(double gst, double *lon, double *lat) {
215 /* time_t ssue; seconds since unix epoch */
216 /* double *lat; (return) latitude */
217 /* double *lon; (return) longitude */
223 /* lambda = sun_ecliptic_longitude(ssue); */
224 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
225 //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
226 ecliptic_to_equatorial( globals->get_ephem()->get_sun()->getLon(),
227 globals->get_ephem()->get_sun()->getLat(),
230 // tmp = alpha - (SGD_2PI/24)*GST(ssue);
231 tmp = alpha - (SGD_2PI/24)*gst;
234 while (tmp < -SGD_PI);
235 } else if (tmp > SGD_PI) {
237 while (tmp < -SGD_PI);
245 // update the cur_time_params structure with the current sun position
246 void fgUpdateSunPos( void ) {
250 Point3D p, rel_sunpos;
251 double dot, east_dot;
252 double sun_gd_lat, sl_radius;
254 // vector in cartesian coordinates from current position to the
255 // postion on the earth's surface the sun is directly over
258 // surface direction to go to head towards sun
259 sgVec3 surface_to_sun;
261 l = &cur_light_params;
262 SGTime *t = globals->get_time_params();
263 v = globals->get_current_view();
265 SG_LOG( SG_EVENT, SG_INFO, " Updating Sun position" );
266 SG_LOG( SG_EVENT, SG_INFO, " Gst = " << t->getGst() );
268 fgSunPositionGST(t->getGst(), &l->sun_lon, &sun_gd_lat);
270 sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
272 p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
273 l->fg_sunpos = sgPolarToCart3d(p);
275 SG_LOG( SG_EVENT, SG_INFO, " t->cur_time = " << t->get_cur_time() );
276 SG_LOG( SG_EVENT, SG_INFO,
277 " Sun Geodetic lat = " << sun_gd_lat
278 << " Geocentric lat = " << l->sun_gc_lat );
280 // update the sun light vector
281 sgSetVec4( l->sun_vec,
282 l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z(), 0.0 );
283 sgNormalizeVec4( l->sun_vec );
284 sgCopyVec4( l->sun_vec_inv, l->sun_vec );
285 sgNegateVec4( l->sun_vec_inv );
287 // make sure these are directional light sources only
288 l->sun_vec[3] = l->sun_vec_inv[3] = 0.0;
289 // cout << " l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
290 // << ","<< l->sun_vec[2] << endl;
292 // calculate the sun's relative angle to local up
293 sgCopyVec3( nup, v->get_world_up() );
294 sgSetVec3( nsun, l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z() );
295 sgNormalizeVec3(nup);
296 sgNormalizeVec3(nsun);
297 // cout << "nup = " << nup[0] << "," << nup[1] << ","
298 // << nup[2] << endl;
299 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
300 // << nsun[2] << endl;
302 l->sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
303 SG_LOG( SG_EVENT, SG_INFO, "sun angle relative to current location = "
306 // calculate vector to sun's position on the earth's surface
307 Point3D vp( v->get_view_pos()[0],
308 v->get_view_pos()[1],
309 v->get_view_pos()[2] );
310 rel_sunpos = l->fg_sunpos - ( vp + globals->get_scenery()->get_center() );
311 sgSetVec3( to_sun, rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
312 // printf( "Vector to sun = %.2f %.2f %.2f\n",
313 // v->to_sun[0], v->to_sun[1], v->to_sun[2]);
315 // Given a vector from the view position to the point on the
316 // earth's surface the sun is directly over, map into onto the
317 // local plane representing "horizontal".
319 sgmap_vec_onto_cur_surface_plane( v->get_world_up(), v->get_view_pos(),
320 to_sun, surface_to_sun );
321 sgNormalizeVec3(surface_to_sun);
322 // cout << "(sg) Surface direction to sun is "
323 // << surface_to_sun[0] << ","
324 // << surface_to_sun[1] << ","
325 // << surface_to_sun[2] << endl;
326 // cout << "Should be close to zero = "
327 // << sgScalarProductVec3(nup, surface_to_sun) << endl;
329 // calculate the angle between surface_to_sun and
330 // v->get_surface_east(). We do this so we can sort out the
331 // acos() ambiguity. I wish I could think of a more efficient
333 east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
334 // cout << " East dot product = " << east_dot << endl;
336 // calculate the angle between v->surface_to_sun and
337 // v->surface_south. this is how much we have to rotate the sky
338 // for it to align with the sun
339 dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
340 // cout << " Dot product = " << dot << endl;
342 if ( east_dot >= 0 ) {
343 l->sun_rotation = acos(dot);
345 l->sun_rotation = -acos(dot);
347 // cout << " Sky needs to rotate = " << angle << " rads = "
348 // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;