]> git.mxchange.org Git - flightgear.git/blob - src/Time/sunsolver.cxx
Added a <solve-weight> subtag of the approach/cruise parameters that can
[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;
51     fgSunPositionGST( t.getGst(), &sun_lon, &sun_gd_lat );
52     Point3D sunpos = sgGeodToCart(Point3D(sun_lon, sun_gd_lat, 0));
53
54     SG_LOG( SG_EVENT, SG_DEBUG, "    t.cur_time = " << t.get_cur_time() );
55     SG_LOG( SG_EVENT, SG_DEBUG, 
56             "    Sun Geodetic lat = " << sun_gd_lat );
57
58     // calculate the sun's relative angle to local up
59     sgCopyVec3( nup, world_up );
60     sgSetVec3( nsun, sunpos.x(), sunpos.y(), sunpos.z() );
61     sgNormalizeVec3(nup);
62     sgNormalizeVec3(nsun);
63     // cout << "nup = " << nup[0] << "," << nup[1] << "," 
64     //      << nup[2] << endl;
65     // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
66     //      << nsun[2] << endl;
67
68     double sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
69     double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
70     while ( sun_angle_deg < -180 ) { sun_angle += 360; }
71     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
72             << sun_angle_deg );
73
74     return sun_angle_deg;
75 }
76
77
78 /**
79  * Given the current unix time in seconds, calculate seconds to the
80  * specified sun angle (relative to straight up.)  Also specify if we
81  * want the angle while the sun is ascending or descending.  For
82  * instance noon is when the sun angle is 0 (or the closest it can
83  * get.)  Dusk is when the sun angle is 90 and descending.  Dawn is
84  * when the sun angle is 90 and ascending.
85  */
86 time_t fgTimeSecondsUntilSunAngle( time_t cur_time,
87                                    double lon_rad,
88                                    double lat_rad,
89                                    double target_angle_deg,
90                                    bool ascending )
91 {
92     // cout << "location = " << lon_rad * SG_RADIANS_TO_DEGREES << ", "
93     //      << lat_rad * SG_RADIANS_TO_DEGREES << endl;
94     Point3D geod( lon_rad, lat_rad, 0 );
95     Point3D tmp = sgGeodToCart( geod );
96     sgVec3 world_up;
97     sgSetVec3( world_up, tmp.x(), tmp.y(), tmp.z() );
98     SGTime t = SGTime( lon_rad, lat_rad, "", 0 );
99
100     double best_diff = 180.0;
101     double last_angle = -99999.0;
102     time_t best_time = cur_time;
103
104     for ( time_t secs = cur_time - half_day_secs;
105           secs < cur_time + half_day_secs;
106           secs += step_secs )
107     {
108         t.update( lon_rad, lat_rad, secs, 0 );
109         double angle_deg = sun_angle( t, world_up, lon_rad, lat_rad );
110         double diff = fabs( angle_deg - target_angle_deg );
111         if ( diff < best_diff ) {
112             if ( last_angle <= 180.0 && ascending
113                  && ( last_angle > angle_deg ) ) {
114                 // cout << "best angle = " << angle << " offset = "
115                 //      << secs - cur_time << endl;
116                 best_diff = diff;
117                 best_time = secs;
118             } else if ( last_angle <= 180.0 && !ascending
119                         && ( last_angle < angle_deg ) ) {
120                 // cout << "best angle = " << angle << " offset = "
121                 //      << secs - cur_time << endl;
122                 best_diff = diff;
123                 best_time = secs;
124             }
125         }
126
127         last_angle = angle_deg;
128     }
129
130     return best_time - cur_time;
131 }