]> git.mxchange.org Git - flightgear.git/blob - src/Time/sunsolver.cxx
c4e48c43403e21fe376b737f3ff2ed3c5958f56c
[flightgear.git] / src / Time / sunsolver.cxx
1 /*
2  * sunsolver.cxx - given a location on earth and a time of day/date,
3  *                 find the number of seconds to various sun positions.
4  *
5  * Written by Curtis Olson, started September 2003.
6  *
7  * Copyright (C) 2003  Curtis L. Olson  - curt@flightgear.org
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26
27 #include <simgear/math/point3d.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/timing/sg_time.hxx>
30
31 #include <Main/globals.hxx>
32
33 #include "sunpos.hxx"
34
35 #include "sunsolver.hxx"
36
37
38 static const time_t day_secs = 86400;
39 static const time_t half_day_secs = day_secs / 2;
40 static const time_t step_secs = 60;
41
42 static double sun_angle( const SGTime &t, sgVec3 world_up,
43                          double lon_rad, double lat_rad ) {
44     sgVec3 nup, nsun;
45     Point3D p, rel_sunpos;
46
47     SG_LOG( SG_EVENT, SG_DEBUG, "  Updating Sun position" );
48     SG_LOG( SG_EVENT, SG_DEBUG, "  Gst = " << t.getGst() );
49
50     double sun_lon, sun_gd_lat, sun_gc_lat, sl_radius;
51     fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
52
53     sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &sun_gc_lat);
54
55     p = Point3D( sun_lon, sun_gc_lat, sl_radius );
56     Point3D sunpos = sgPolarToCart3d(p);
57
58     SG_LOG( SG_EVENT, SG_DEBUG, "    t.cur_time = " << t.get_cur_time() );
59     SG_LOG( SG_EVENT, SG_DEBUG, 
60             "    Sun Geodetic lat = " << sun_gd_lat
61             << " Geocentric lat = " << sun_gc_lat );
62
63     // calculate the sun's relative angle to local up
64     sgCopyVec3( nup, world_up );
65     sgSetVec3( nsun, sunpos.x(), sunpos.y(), sunpos.z() );
66     sgNormalizeVec3(nup);
67     sgNormalizeVec3(nsun);
68     // cout << "nup = " << nup[0] << "," << nup[1] << "," 
69     //      << nup[2] << endl;
70     // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
71     //      << nsun[2] << endl;
72
73     double sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
74     double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
75     while ( sun_angle_deg < -180 ) { sun_angle += 360; }
76     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
77             << sun_angle_deg );
78
79     return sun_angle_deg;
80 }
81
82
83 /**
84  * Given the current unix time in seconds, calculate seconds to noon
85  */
86 time_t fgTimeSecondsUntilNoon( time_t cur_time,
87                                double lon_rad,
88                                double lat_rad )
89 {
90     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
91     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
92     Point3D geod( lon_rad, lat_rad, 0 );
93     Point3D tmp = sgGeodToCart( geod );
94     sgVec3 world_up;
95     sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
96     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
97
98     double best_angle = 180.0;
99     time_t best_time = cur_time;
100
101     for ( time_t secs = cur_time - half_day_secs;
102           secs < cur_time + half_day_secs;
103           secs += step_secs )
104     {
105         t.update( lon_rad, lat_rad, secs, 0 );
106         double angle = sun_angle( t, world_up, lon_rad, lat_rad );
107         if ( angle < best_angle ) {
108             // cout << "best angle = " << angle << " offset = "
109             // << secs - cur_time << endl;
110             best_angle = angle;
111             best_time = secs;
112         }
113     }
114
115     return best_time - cur_time;
116 }
117
118
119 /**
120  * Given the current unix time in seconds, calculate seconds to midnight
121  */
122 time_t fgTimeSecondsUntilMidnight( time_t cur_time,
123                                    double lon_rad,
124                                    double lat_rad )
125 {
126     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
127     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
128     Point3D geod( lon_rad, lat_rad, 0 );
129     Point3D tmp = sgGeodToCart( geod );
130     sgVec3 world_up;
131     sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
132     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
133
134     double best_angle = 0.0;
135     time_t best_time = cur_time;
136
137     for ( time_t secs = cur_time - half_day_secs;
138           secs < cur_time + half_day_secs;
139           secs += step_secs )
140     {
141         t.update( lon_rad, lat_rad, secs, 0 );
142         double angle = sun_angle( t, world_up, lon_rad, lat_rad );
143         if ( angle > best_angle ) {
144             // cout << "best angle = " << angle << " offset = "
145             //      << secs - cur_time << endl;
146             best_angle = angle;
147             best_time = secs;
148         }
149     }
150
151     return best_time - cur_time;
152 }
153
154
155 /**
156  * Given the current unix time in seconds, calculate seconds to dusk
157  */
158 time_t fgTimeSecondsUntilDusk( time_t cur_time,
159                                double lon_rad,
160                                double lat_rad )
161 {
162     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
163     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
164     Point3D geod( lon_rad, lat_rad, 0 );
165     Point3D tmp = sgGeodToCart( geod );
166     sgVec3 world_up;
167     sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
168     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
169
170     double best_diff = 90.0;
171     double last_angle = -99999.0;
172     time_t best_time = cur_time;
173
174     for ( time_t secs = cur_time - half_day_secs;
175           secs < cur_time + half_day_secs;
176           secs += step_secs )
177     {
178         t.update( lon_rad, lat_rad, secs, 0 );
179         double angle = sun_angle( t, world_up, lon_rad, lat_rad );
180         double diff = fabs( angle - 90.0 );
181         if ( diff < best_diff ) {
182             if ( last_angle <= 180.0 && ( last_angle < angle ) ) {
183                 // cout << "best angle = " << angle << " offset = "
184                 //      << secs - cur_time << endl;
185                 best_diff = diff;
186                 best_time = secs;
187             }
188         }
189
190         last_angle = angle;
191     }
192
193     return best_time - cur_time;
194 }
195
196
197 /**
198  * Given the current unix time in seconds, calculate seconds to dawn
199  */
200 time_t fgTimeSecondsUntilDawn( time_t cur_time,
201                                double lon_rad,
202                                double lat_rad )
203 {
204     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
205     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
206     Point3D geod( lon_rad, lat_rad, 0 );
207     Point3D tmp = sgGeodToCart( geod );
208     sgVec3 world_up;
209     sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
210     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
211
212     double best_diff = 90.0;
213     double last_angle = -99999.0;
214     time_t best_time = cur_time;
215
216     for ( time_t secs = cur_time - half_day_secs;
217           secs < cur_time + half_day_secs;
218           secs += step_secs )
219     {
220         t.update( lon_rad, lat_rad, secs, 0 );
221         double angle = sun_angle( t, world_up, lon_rad, lat_rad );
222         double diff = fabs( angle - 90.0 );
223         if ( diff < best_diff ) {
224             if ( last_angle <= 180.0 && ( last_angle > angle ) ) {
225                 // cout << "best angle = " << angle << " offset = "
226                 //      << secs - cur_time << endl;
227                 best_diff = diff;
228                 best_time = secs;
229             }
230         }
231
232         last_angle = angle;
233     }
234
235     return best_time - cur_time;
236 }