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/point3d.hxx>
60 #include <simgear/math/polar3d.hxx>
61 #include <simgear/math/sg_geodesy.hxx>
62 #include <simgear/math/vector.hxx>
64 #include <Main/globals.hxx>
65 #include <Scenery/scenery.hxx>
66 #include <Time/light.hxx>
70 // #undef E // should no longer be needed
71 #define MeanObliquity (23.440592*(FG_2PI/360))
73 static void ecliptic_to_equatorial(double, double, double *, double *);
74 static double julian_date(int, int, int);
75 static double GST(time_t);
77 static void ecliptic_to_equatorial(double lambda, double beta,
78 double *alpha, double *delta) {
79 /* double lambda; ecliptic longitude */
80 /* double beta; ecliptic latitude */
81 /* double *alpha; (return) right ascension */
82 /* double *delta; (return) declination */
87 sin_e = sin(MeanObliquity);
88 cos_e = cos(MeanObliquity);
92 *alpha = atan2(sin_l*cos_e - tan(beta)*sin_e, cos_l);
93 *delta = asin(sin(beta)*cos_e + cos(beta)*sin_e*sin_l);
97 /* computing julian dates (assuming gregorian calendar, thus this is
98 * only valid for dates of 1582 oct 15 or later) (after duffett-smith,
101 static double julian_date(int y, int m, int d) {
102 /* int y; year (e.g. 19xx) */
103 /* int m; month (jan=1, feb=2, ...) */
104 /* int d; day of month */
109 /* lazy test to ensure gregorian calendar */
111 FG_LOG( FG_EVENT, FG_ALERT,
112 "WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
115 if ((m == 1) || (m == 2)) {
122 C = (int)(365.25 * y);
123 D = (int)(30.6001 * (m + 1));
125 JD = B + C + D + d + 1720994.5;
131 /* compute greenwich mean sidereal time (GST) corresponding to a given
132 * number of seconds since the unix epoch (after duffett-smith,
134 static double GST(time_t ssue) {
135 /* time_t ssue; seconds since unix epoch */
144 JD = julian_date(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
145 T = (JD - 2451545) / 36525;
147 T0 = ((T + 2.5862e-5) * T + 2400.051336) * T + 6.697374558;
150 if (T0 < 0) T0 += 24;
152 UT = tm->tm_hour + (tm->tm_min + tm->tm_sec / 60.0) / 60.0;
154 T0 += UT * 1.002737909;
156 if (T0 < 0) T0 += 24;
162 /* given a particular time (expressed in seconds since the unix
163 * epoch), compute position on the earth (lat, lon) such that sun is
164 * directly overhead. (lat, lon are reported in radians */
166 void fgSunPosition(time_t ssue, double *lon, double *lat) {
167 /* time_t ssue; seconds since unix epoch */
168 /* double *lat; (return) latitude */
169 /* double *lon; (return) longitude */
175 /* lambda = sun_ecliptic_longitude(ssue); */
176 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
177 //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
179 /* **********************************************************************
180 * NOTE: in the next function, each time the sun's position is updated, the
181 * the sun's longitude is returned from solarSystem->sun. Note that the
182 * sun's position is updated at a much higher frequency than the rate at
183 * which the solar system's rebuilds occur. This is not a problem, however,
184 * because the fgSunPosition we're talking about here concerns the changing
185 * position of the sun due to the daily rotation of the earth.
186 * The ecliptic longitude, however, represents the position of the sun with
187 * respect to the stars, and completes just one cycle over the course of a
188 * year. Its therefore pretty safe to update the sun's longitude only once
189 * every ten minutes. (Comment added by Durk Talsma).
190 ************************************************************************/
192 ecliptic_to_equatorial( globals->get_ephem()->get_sun()->getLon(),
193 0.0, &alpha, &delta );
194 tmp = alpha - (FG_2PI/24)*GST(ssue);
197 while (tmp < -FG_PI);
198 } else if (tmp > FG_PI) {
200 while (tmp < -FG_PI);
208 /* given a particular time expressed in side real time at prime
209 * meridian (GST), compute position on the earth (lat, lon) such that
210 * sun is directly overhead. (lat, lon are reported in radians */
212 static void fgSunPositionGST(double gst, double *lon, double *lat) {
213 /* time_t ssue; seconds since unix epoch */
214 /* double *lat; (return) latitude */
215 /* double *lon; (return) longitude */
221 /* lambda = sun_ecliptic_longitude(ssue); */
222 /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
223 //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
224 ecliptic_to_equatorial( globals->get_ephem()->get_sun()->getLon(),
225 globals->get_ephem()->get_sun()->getLat(),
228 // tmp = alpha - (FG_2PI/24)*GST(ssue);
229 tmp = alpha - (FG_2PI/24)*gst;
232 while (tmp < -FG_PI);
233 } else if (tmp > FG_PI) {
235 while (tmp < -FG_PI);
243 // update the cur_time_params structure with the current sun position
244 void fgUpdateSunPos( void ) {
247 sgVec3 nup, nsun, surface_to_sun;
248 Point3D p, rel_sunpos;
249 double dot, east_dot;
250 double sun_gd_lat, sl_radius;
252 l = &cur_light_params;
253 SGTime *t = globals->get_time_params();
254 v = globals->get_current_view();
256 FG_LOG( FG_EVENT, FG_INFO, " Updating Sun position" );
257 FG_LOG( FG_EVENT, FG_INFO, " Gst = " << t->getGst() );
259 fgSunPositionGST(t->getGst(), &l->sun_lon, &sun_gd_lat);
261 sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
263 p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
264 l->fg_sunpos = sgPolarToCart3d(p);
266 FG_LOG( FG_EVENT, FG_INFO, " t->cur_time = " << t->get_cur_time() );
267 FG_LOG( FG_EVENT, FG_INFO,
268 " Sun Geodetic lat = " << sun_gd_lat
269 << " Geocentric lat = " << l->sun_gc_lat );
271 // update the sun light vector
272 sgSetVec4( l->sun_vec,
273 l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z(), 0.0 );
274 sgNormalizeVec4( l->sun_vec );
275 sgCopyVec4( l->sun_vec_inv, l->sun_vec );
276 sgNegateVec4( l->sun_vec_inv );
278 // make sure these are directional light sources only
279 l->sun_vec[3] = l->sun_vec_inv[3] = 0.0;
280 // cout << " l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
281 // << ","<< l->sun_vec[2] << endl;
283 // calculate the sun's relative angle to local up
284 sgCopyVec3( nup, v->get_local_up() );
285 sgSetVec3( nsun, l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z() );
286 sgNormalizeVec3(nup);
287 sgNormalizeVec3(nsun);
288 // cout << "nup = " << nup[0] << "," << nup[1] << ","
289 // << nup[2] << endl;
290 // cout << "nsun = " << nsun[0] << "," << nsun[1] << ","
291 // << nsun[2] << endl;
293 l->sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
294 cout << "sun angle relative to current location = " << l->sun_angle << endl;
296 // calculate vector to sun's position on the earth's surface
297 Point3D vp( v->get_view_pos()[0],
298 v->get_view_pos()[1],
299 v->get_view_pos()[1] );
300 rel_sunpos = l->fg_sunpos - ( vp + 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 // Given a vector from the view position to the point on the
306 // earth's surface the sun is directly over, map into onto the
307 // local plane representing "horizontal".
309 sgmap_vec_onto_cur_surface_plane( v->get_local_up(), v->get_view_pos(),
310 v->get_to_sun(), surface_to_sun );
311 sgNormalizeVec3(surface_to_sun);
312 v->set_surface_to_sun( surface_to_sun[0], surface_to_sun[1],
314 // cout << "(sg) Surface direction to sun is "
315 // << surface_to_sun[0] << ","
316 // << surface_to_sun[1] << ","
317 // << surface_to_sun[2] << endl;
318 // cout << "Should be close to zero = "
319 // << sgScalarProductVec3(nup, surface_to_sun) << endl;
321 // calculate the angle between v->surface_to_sun and
322 // v->surface_east. We do this so we can sort out the acos()
323 // ambiguity. I wish I could think of a more efficient way ... :-(
324 east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
325 // cout << " East dot product = " << east_dot << endl;
327 // calculate the angle between v->surface_to_sun and
328 // v->surface_south. this is how much we have to rotate the sky
329 // for it to align with the sun
330 dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
331 // cout << " Dot product = " << dot << endl;
333 if ( east_dot >= 0 ) {
334 l->sun_rotation = acos(dot);
336 l->sun_rotation = -acos(dot);
338 // cout << " Sky needs to rotate = " << angle << " rads = "
339 // << angle * RAD_TO_DEG << " degrees." << endl;