1 /**************************************************************************
2 * orbits.c - calculates the orbital elements of the sun, moon and planets.
3 * For inclusion in flight gear
5 * Written 1997 by Durk Talsma, started October 19, 1997.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * (Log is kept at end of this file)
23 **************************************************************************/
33 #include <Debug/fg_debug.h>
34 #include <Include/fg_constants.h>
35 #include <Include/fg_zlib.h>
36 #include <Main/options.hxx>
37 #include <Time/fg_time.hxx>
42 struct OrbElements pltOrbElements[9];
47 double fgCalcActTime(fgTIME t)
49 return (t.mjd - 36523.5);
53 double fgCalcEccAnom(double M, double e)
56 eccAnom, E0, E1, diff;
58 eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
59 /* iterate to achieve a greater precision for larger eccentricities */
65 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
69 while (diff > (DEG_TO_RAD * 0.001));
76 /* This function assumes that if the FILE ptr is valid that the
77 contents will be valid. Should we check the file for validity? */
79 /* Sounds like a good idea to me. What type of checks are you thinking
80 of, other than feof(FILE*)? That's currently the only check I can
83 int fgReadOrbElements(struct OrbElements *dest, gzFile src) {
88 if ( fggets(src, line, 256) == NULL ) {
89 fgPrintf (FG_ASTRO, FG_ALERT,
90 "End of file found while reading planetary positions:\n");
93 for (i = 0; i < 256; i++) {
97 // printf("Reading line %d = %s\n", j++, line);
98 } while (!(strlen(line)));
100 sscanf(line, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",
101 &dest->NFirst, &dest->NSec,
102 &dest->iFirst, &dest->iSec,
103 &dest->wFirst, &dest->wSec,
104 &dest->aFirst, &dest->aSec,
105 &dest->eFirst, &dest->eSec,
106 &dest->MFirst, &dest->MSec);
112 int fgSolarSystemInit(fgTIME t)
115 char path[256], gzpath[256];
118 fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
120 /* build the full path name to the orbital elements database file */
121 o = ¤t_options;
123 strcat(path, o->fg_root);
124 strcat(path, "/Scenery/");
125 strcat(path, "Planets");
127 if ( (data = fgopen(path, "rb")) == NULL ) {
128 strcpy(gzpath, path);
129 strcat(gzpath, ".gz");
130 if ( (data = fgopen(gzpath, "rb")) == NULL ) {
131 fgPrintf( FG_ASTRO, FG_EXIT,
132 "Cannot open data file: '%s'\n", path);
136 /* printf(" reading datafile %s\n", path); */
137 fgPrintf( FG_ASTRO, FG_INFO, " reading datafile %s\n", path);
139 /* for all the objects... */
140 for (i = 0; i < 9; i ++) {
141 /* ...read from the data file ... */
142 if (!(fgReadOrbElements (&pltOrbElements[i], data))) {
145 /* ...and calculate the actual values */
146 fgSolarSystemUpdate(&pltOrbElements[i], t);
155 void fgSolarSystemUpdate(struct OrbElements *planet, fgTIME t)
160 actTime = fgCalcActTime(t);
162 /* calculate the actual orbital elements */
163 planet->M = DEG_TO_RAD * (planet->MFirst + (planet->MSec * actTime));
164 planet->w = DEG_TO_RAD * (planet->wFirst + (planet->wSec * actTime));
165 planet->N = DEG_TO_RAD * (planet->NFirst + (planet->NSec * actTime));
166 planet->i = DEG_TO_RAD * (planet->iFirst + (planet->iSec * actTime));
167 planet->e = planet->eFirst + (planet->eSec * actTime);
168 planet->a = planet->aFirst + (planet->aSec * actTime);
173 /* Revision 1.6 1998/05/29 20:35:41 curt
174 /* Added zlib support for reading in compressed data files.
176 * Revision 1.5 1998/05/13 18:25:34 curt
177 * Root path info moved to fgOPTIONS.
179 * Revision 1.4 1998/04/28 01:19:00 curt
180 * Type-ified fgTIME and fgVIEW
182 * Revision 1.3 1998/04/25 22:06:25 curt
183 * Edited cvs log messages in source files ... bad bad bad!
185 * Revision 1.2 1998/04/24 00:45:01 curt
186 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
187 * Fixed a bug when generating sky colors.
189 * Revision 1.1 1998/04/22 13:21:29 curt
190 * C++ - ifing the code a bit.
192 * Revision 1.10 1998/04/18 04:13:57 curt
193 * Moved fg_debug.c to it's own library.
195 * Revision 1.9 1998/03/14 00:27:12 curt
196 * Updated fgGENERAL to a "type" of struct.
198 * Revision 1.8 1998/02/23 19:07:55 curt
199 * Incorporated Durk's Astro/ tweaks. Includes unifying the sun position
200 * calculation code between sun display, and other FG sections that use this
201 * for things like lighting.
203 * Revision 1.7 1998/02/12 21:59:33 curt
204 * Incorporated code changes contributed by Charlie Hotchkiss
205 * <chotchkiss@namg.us.anritsu.com>
207 * Revision 1.6 1998/02/03 23:20:11 curt
208 * Lots of little tweaks to fix various consistency problems discovered by
209 * Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
210 * passed arguments along to the real printf(). Also incorporated HUD changes
211 * by Michele America.
213 * Revision 1.5 1998/02/02 20:53:22 curt
216 * Revision 1.4 1998/01/27 00:47:47 curt
217 * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
218 * system and commandline/config file processing code.
220 * Revision 1.3 1998/01/22 02:59:27 curt
221 * Changed #ifdef FILE_H to #ifdef _FILE_H
223 * Revision 1.2 1998/01/19 19:26:58 curt
224 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
225 * This should simplify things tremendously.
227 * Revision 1.1 1998/01/07 03:16:17 curt
228 * Moved from .../Src/Scenery/ to .../Src/Astro/
230 * Revision 1.6 1997/12/30 20:47:52 curt
231 * Integrated new event manager with subsystem initializations.
233 * Revision 1.5 1997/12/15 23:55:02 curt
234 * Add xgl wrappers for debugging.
235 * Generate terrain normals on the fly.
237 * Revision 1.4 1997/12/10 22:37:51 curt
238 * Prepended "fg" on the name of all global structures that didn't have it yet.
239 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
241 * Revision 1.3 1997/11/25 23:20:44 curt
242 * Changed planets.dat Planets.dat
244 * Revision 1.2 1997/11/25 19:25:36 curt
245 * Changes to integrate Durk's moon/sun code updates + clean up.
247 * Revision 1.1 1997/10/25 03:16:10 curt
248 * Initial revision of code contributed by Durk Talsma.