| 1 | /* |
| 2 | * bodysolver.cxx - given a location on earth and a time of day/date, |
| 3 | * find the number of seconds to various solar system body |
| 4 | * positions. |
| 5 | * |
| 6 | * Written by Curtis Olson, started September 2003. |
| 7 | * |
| 8 | * Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | * |
| 24 | * $Id$ |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | # include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <cmath> |
| 32 | #include <ctime> |
| 33 | #include <cassert> |
| 34 | |
| 35 | #include <simgear/math/SGMath.hxx> |
| 36 | #include <simgear/timing/sg_time.hxx> |
| 37 | |
| 38 | #include <Main/globals.hxx> |
| 39 | #include <Main/fg_props.hxx> |
| 40 | |
| 41 | #include "bodysolver.hxx" |
| 42 | |
| 43 | |
| 44 | static const time_t day_secs = 86400; |
| 45 | static const time_t half_day_secs = day_secs / 2; |
| 46 | static const time_t step_secs = 60; |
| 47 | |
| 48 | /* given a particular time expressed in side real time at prime |
| 49 | * meridian (GST), compute position on the earth (lat, lon) such that |
| 50 | * solar system body is directly overhead. (lat, lon are reported in |
| 51 | * radians) */ |
| 52 | |
| 53 | void fgBodyPositionGST(double gst, double& lon, double& lat, bool sun_not_moon) { |
| 54 | /* time_t ssue; seconds since unix epoch */ |
| 55 | /* double& lat; (return) latitude */ |
| 56 | /* double& lon; (return) longitude */ |
| 57 | |
| 58 | double tmp; |
| 59 | |
| 60 | std::string body = sun_not_moon ? "sun" : "moon"; |
| 61 | SGPropertyNode* body_node = fgGetNode("/ephemeris/" + body); |
| 62 | assert(body_node); |
| 63 | double xs = sun_not_moon ? body_node->getDoubleValue("xs") |
| 64 | : body_node->getDoubleValue("xg"); |
| 65 | //double ys = body_node->getDoubleValue("ys"); |
| 66 | double ye = body_node->getDoubleValue("ye"); |
| 67 | double ze = body_node->getDoubleValue("ze"); |
| 68 | double ra = atan2(ye, xs); |
| 69 | double dec = atan2(ze, sqrt(xs * xs + ye * ye)); |
| 70 | |
| 71 | tmp = ra - (SGD_2PI/24)*gst; |
| 72 | |
| 73 | double signedPI = (tmp < 0.0) ? -SGD_PI : SGD_PI; |
| 74 | tmp = fmod(tmp+signedPI, SGD_2PI) - signedPI; |
| 75 | |
| 76 | lon = tmp; |
| 77 | lat = dec; |
| 78 | } |
| 79 | |
| 80 | static double body_angle( const SGTime &t, const SGVec3d& world_up, bool sun_not_moon) { |
| 81 | const char *body = sun_not_moon ? "sun" : "moon"; |
| 82 | SG_LOG( SG_EVENT, SG_DEBUG, " Updating " << body << " position" ); |
| 83 | SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t.getGst() ); |
| 84 | |
| 85 | double lon, gc_lat; |
| 86 | fgBodyPositionGST( t.getGst(), lon, gc_lat, body ); |
| 87 | SGVec3d bodypos = SGVec3d::fromGeoc(SGGeoc::fromRadM(lon, gc_lat, |
| 88 | SGGeodesy::EQURAD)); |
| 89 | |
| 90 | SG_LOG( SG_EVENT, SG_DEBUG, " t.cur_time = " << t.get_cur_time() ); |
| 91 | SG_LOG( SG_EVENT, SG_DEBUG, |
| 92 | " " << body << " geocentric lat = " << gc_lat ); |
| 93 | |
| 94 | // calculate the body's relative angle to local up |
| 95 | SGVec3d nup = normalize(world_up); |
| 96 | SGVec3d nbody = normalize(bodypos); |
| 97 | // cout << "nup = " << nup[0] << "," << nup[1] << "," |
| 98 | // << nup[2] << endl; |
| 99 | // cout << "nbody = " << nbody[0] << "," << nbody[1] << "," |
| 100 | // << nbody[2] << endl; |
| 101 | |
| 102 | double body_angle = acos( dot( nup, nbody ) ); |
| 103 | |
| 104 | double signedPI = (body_angle < 0.0) ? -SGD_PI : SGD_PI; |
| 105 | body_angle = fmod(body_angle+signedPI, SGD_2PI) - signedPI; |
| 106 | |
| 107 | double body_angle_deg = body_angle * SG_RADIANS_TO_DEGREES; |
| 108 | SG_LOG( SG_EVENT, SG_DEBUG, body << " angle relative to current location = " |
| 109 | << body_angle_deg ); |
| 110 | |
| 111 | return body_angle_deg; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Given the current unix time in seconds, calculate seconds to the |
| 117 | * specified body angle (relative to straight up.) Also specify if we |
| 118 | * want the angle while the body is ascending or descending. For |
| 119 | * instance noon is when the sun angle is 0 (or the closest it can |
| 120 | * get.) Dusk is when the sun angle is 90 and descending. Dawn is |
| 121 | * when the sun angle is 90 and ascending. |
| 122 | */ |
| 123 | time_t fgTimeSecondsUntilBodyAngle( time_t cur_time, |
| 124 | const SGGeod& loc, |
| 125 | double target_angle_deg, |
| 126 | bool ascending, |
| 127 | bool sun_not_moon ) |
| 128 | { |
| 129 | SGVec3d world_up = SGVec3d::fromGeod(loc); |
| 130 | SGTime t = SGTime( loc, SGPath(), 0 ); |
| 131 | |
| 132 | double best_diff = 180.0; |
| 133 | double last_angle = -99999.0; |
| 134 | time_t best_time = cur_time; |
| 135 | |
| 136 | for ( time_t secs = cur_time - half_day_secs; |
| 137 | secs < cur_time + half_day_secs; |
| 138 | secs += step_secs ) |
| 139 | { |
| 140 | t.update( loc, secs, 0 ); |
| 141 | double angle_deg = body_angle( t, world_up, sun_not_moon ); |
| 142 | double diff = fabs( angle_deg - target_angle_deg ); |
| 143 | if ( diff < best_diff ) { |
| 144 | if ( last_angle <= 180.0 && ascending |
| 145 | && ( last_angle > angle_deg ) ) { |
| 146 | // cout << "best angle = " << angle << " offset = " |
| 147 | // << secs - cur_time << endl; |
| 148 | best_diff = diff; |
| 149 | best_time = secs; |
| 150 | } else if ( last_angle <= 180.0 && !ascending |
| 151 | && ( last_angle < angle_deg ) ) { |
| 152 | // cout << "best angle = " << angle << " offset = " |
| 153 | // << secs - cur_time << endl; |
| 154 | best_diff = diff; |
| 155 | best_time = secs; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | last_angle = angle_deg; |
| 160 | } |
| 161 | |
| 162 | return best_time - cur_time; |
| 163 | } |