]> git.mxchange.org Git - simgear.git/blob - Astro/orbits.cxx
In fgCalculatePlanet() pass a pointer to a structure to be modified, rather
[simgear.git] / Astro / orbits.cxx
1 /**************************************************************************
2  * orbits.c - calculates the orbital elements of the sun, moon and planets.
3  *            For inclusion in flight gear
4  *
5  * Written 1997 by Durk Talsma, started October 19, 1997.
6  *
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.
11  *
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.
16  *
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.
20  *
21  * $Id$
22  * (Log is kept at end of this file)
23  **************************************************************************/
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <math.h>
31 #include <string.h>
32
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>
38
39 #include "orbits.hxx"
40
41
42 struct OrbElements pltOrbElements[9];
43
44 static fgFile data;
45
46
47 double fgCalcActTime(fgTIME t)
48 {
49   return (t.mjd - 36523.5);
50 }
51
52
53 double fgCalcEccAnom(double M, double e)
54 {
55     double
56         eccAnom, E0, E1, diff;
57
58     eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
59     /* iterate to achieve a greater precision for larger eccentricities */
60     if (e > 0.05)
61     {
62         E0 = eccAnom;
63         do
64         {
65                 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
66             diff = fabs(E0 - E1);
67             E0 = E1;
68                 }
69         while (diff > (DEG_TO_RAD * 0.001));
70         return E0;
71         }
72     return eccAnom;
73 }
74
75
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? */
78   
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
81    think of (Durk) */
82
83 int fgReadOrbElements(struct OrbElements *dest, gzFile src) {
84     char line[256];
85     int i,j;
86     j = 0;
87     do {
88         if ( fggets(src, line, 256) == NULL ) {
89             fgPrintf (FG_ASTRO, FG_ALERT,
90                       "End of file found while reading planetary positions:\n");
91             return 0;
92         }
93         for (i = 0; i < 256; i++) {
94             if (line[i] == '#')
95                 line[i] = 0;
96         }
97         // printf("Reading line %d = %s\n", j++, line);
98     } while (!(strlen(line)));
99
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);
107
108     return(1);
109 }
110
111
112 int fgSolarSystemInit(fgTIME t)
113 {
114     fgOPTIONS *o;
115     char path[256], gzpath[256];
116     int i, ret_val;
117
118     fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
119
120     /* build the full path name to the orbital elements database file */
121     o = &current_options;
122     path[0] = '\0';
123     strcat(path, o->fg_root);
124     strcat(path, "/Scenery/");
125     strcat(path, "Planets");
126
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);
133         }
134     }
135
136     /* printf("  reading datafile %s\n", path); */
137     fgPrintf( FG_ASTRO, FG_INFO, "  reading datafile %s\n", path);
138
139     /* for all the objects... */
140     for (i = 0; i < 9; i ++) {
141         /* ...read from the data file ... */
142         if (!(fgReadOrbElements (&pltOrbElements[i], data))) {
143             ret_val = 0;
144         }
145         /* ...and calculate the actual values */
146         fgSolarSystemUpdate(&pltOrbElements[i], t);
147     }
148
149     fgclose(data);
150
151     return ( 1 );
152 }
153
154
155 void fgSolarSystemUpdate(struct OrbElements *planet, fgTIME t)
156 {
157    double
158          actTime;
159
160    actTime = fgCalcActTime(t);
161
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);
169 }
170
171
172 /* $Log$
173 /* Revision 1.6  1998/05/29 20:35:41  curt
174 /* Added zlib support for reading in compressed data files.
175 /*
176  * Revision 1.5  1998/05/13 18:25:34  curt
177  * Root path info moved to fgOPTIONS.
178  *
179  * Revision 1.4  1998/04/28 01:19:00  curt
180  * Type-ified fgTIME and fgVIEW
181  *
182  * Revision 1.3  1998/04/25 22:06:25  curt
183  * Edited cvs log messages in source files ... bad bad bad!
184  *
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.
188  *
189  * Revision 1.1  1998/04/22 13:21:29  curt
190  * C++ - ifing the code a bit.
191  *
192  * Revision 1.10  1998/04/18 04:13:57  curt
193  * Moved fg_debug.c to it's own library.
194  *
195  * Revision 1.9  1998/03/14 00:27:12  curt
196  * Updated fgGENERAL to a "type" of struct.
197  *
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.
202  *
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>
206  *
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.
212  *
213  * Revision 1.5  1998/02/02 20:53:22  curt
214  * To version 0.29
215  *
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.
219  *
220  * Revision 1.3  1998/01/22 02:59:27  curt
221  * Changed #ifdef FILE_H to #ifdef _FILE_H
222  *
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.
226  *
227  * Revision 1.1  1998/01/07 03:16:17  curt
228  * Moved from .../Src/Scenery/ to .../Src/Astro/
229  *
230  * Revision 1.6  1997/12/30 20:47:52  curt
231  * Integrated new event manager with subsystem initializations.
232  *
233  * Revision 1.5  1997/12/15 23:55:02  curt
234  * Add xgl wrappers for debugging.
235  * Generate terrain normals on the fly.
236  *
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 {}"
240  *
241  * Revision 1.3  1997/11/25 23:20:44  curt
242  * Changed planets.dat Planets.dat
243  *
244  * Revision 1.2  1997/11/25 19:25:36  curt
245  * Changes to integrate Durk's moon/sun code updates + clean up.
246  *
247  * Revision 1.1  1997/10/25 03:16:10  curt
248  * Initial revision of code contributed by Durk Talsma.
249  *
250  */