]> git.mxchange.org Git - flightgear.git/blob - Astro/orbits.cxx
C++ - ifing the code a bit.
[flightgear.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 #include <config.h>
27
28 #include <math.h>
29 #include <string.h>
30
31 #include <Debug/fg_debug.h>
32 #include <Include/fg_constants.h>
33 #include <Include/general.h>
34 #include <Time/fg_time.h>
35
36 #include "orbits.hxx"
37
38
39 struct OrbElements pltOrbElements[9];
40
41
42 double fgCalcActTime(struct fgTIME t)
43 {
44   return (t.mjd - 36523.5);
45 }
46
47
48 double fgCalcEccAnom(double M, double e)
49 {
50     double
51         eccAnom, E0, E1, diff;
52
53     eccAnom = M + e * sin(M) * (1.0 + e * cos(M));
54     /* iterate to achieve a greater precision for larger eccentricities */
55     if (e > 0.05)
56     {
57         E0 = eccAnom;
58         do
59         {
60                 E1 = E0 - (E0 - e * sin(E0) - M) / (1 - e * cos(E0));
61             diff = fabs(E0 - E1);
62             E0 = E1;
63                 }
64         while (diff > (DEG_TO_RAD * 0.001));
65         return E0;
66         }
67     return eccAnom;
68 }
69
70
71 /* This function assumes that if the FILE ptr is valid that the
72    contents will be valid. Should we check the file for validity? */
73   
74 /* Sounds like a good idea to me. What type of checks are you thinking
75    of, other than feof(FILE*)? That's currently the only check I can
76    think of (Durk) */
77
78 int fgReadOrbElements(struct OrbElements *dest, FILE *src)
79 {
80         char line[256];
81     int i,j;
82     j = 0;
83     do
84     {
85         if (feof (src)) {
86             fgPrintf (FG_ASTRO, FG_ALERT,
87                       "End of file found while reading planetary positions:\n");
88             return 0;
89         }
90  
91         fgets(line, 256,src);
92         for (i = 0; i < 256; i++)
93         {
94                 if (line[i] == '#')
95                 line[i] = 0;
96         }
97         /*printf("Reading line %d\n", j++); */
98     }
99     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);
107
108     return(1);
109 }
110
111
112 int fgSolarSystemInit(struct fgTIME t)
113 {
114     fgGENERAL *g;
115     char path[80];
116     int i;
117     FILE *data;
118     int ret_val = 0;
119
120     fgPrintf( FG_ASTRO, FG_INFO, "Initializing solar system\n");
121
122    /* build the full path name to the orbital elements database file */
123     g = &general;
124     path[0] = '\0';
125     strcat(path, g->root_dir);
126     strcat(path, "/Scenery/");
127     strcat(path, "Planets.dat");
128
129     if ( (data = fopen(path, "r")) == NULL )
130     {
131             fgPrintf( FG_ASTRO, FG_ALERT,
132                       "Cannot open data file: '%s'\n", path);
133     } else {
134         /* printf("  reading datafile %s\n", path); */
135         fgPrintf( FG_ASTRO, FG_INFO, "  reading datafile %s\n", path);
136
137         /* for all the objects... */
138         for (i = 0; i < 9; i ++)
139             {
140                 /* ...read from the data file ... */
141                 if (!(fgReadOrbElements (&pltOrbElements[i], data))) {
142                     ret_val = 0;
143                 }
144                 /* ...and calculate the actual values */
145                 fgSolarSystemUpdate(&pltOrbElements[i], t);
146             }
147         ret_val = 1;
148     }
149     return ret_val;
150 }
151
152
153 void fgSolarSystemUpdate(struct OrbElements *planet, struct fgTIME t)
154 {
155    double
156          actTime;
157
158    actTime = fgCalcActTime(t);
159
160    /* calculate the actual orbital elements */
161    planet->M = DEG_TO_RAD * (planet->MFirst + (planet->MSec * actTime));
162    planet->w = DEG_TO_RAD * (planet->wFirst + (planet->wSec * actTime));
163    planet->N = DEG_TO_RAD * (planet->NFirst + (planet->NSec * actTime));
164    planet->i = DEG_TO_RAD * (planet->iFirst + (planet->iSec * actTime));
165    planet->e = planet->eFirst + (planet->eSec * actTime);
166    planet->a = planet->aFirst + (planet->aSec * actTime);
167 }
168
169
170 /* $Log$
171 /* Revision 1.1  1998/04/22 13:21:29  curt
172 /* C++ - ifing the code a bit.
173 /*
174  * Revision 1.10  1998/04/18 04:13:57  curt
175  * Moved fg_debug.c to it's own library.
176  *
177  * Revision 1.9  1998/03/14 00:27:12  curt
178  * Updated fgGENERAL to a "type" of struct.
179  *
180  * Revision 1.8  1998/02/23 19:07:55  curt
181  * Incorporated Durk's Astro/ tweaks.  Includes unifying the sun position
182  * calculation code between sun display, and other FG sections that use this
183  * for things like lighting.
184  *
185  * Revision 1.7  1998/02/12 21:59:33  curt
186  * Incorporated code changes contributed by Charlie Hotchkiss
187  * <chotchkiss@namg.us.anritsu.com>
188  *
189  * Revision 1.6  1998/02/03 23:20:11  curt
190  * Lots of little tweaks to fix various consistency problems discovered by
191  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
192  * passed arguments along to the real printf().  Also incorporated HUD changes
193  * by Michele America.
194  *
195  * Revision 1.5  1998/02/02 20:53:22  curt
196  * To version 0.29
197  *
198  * Revision 1.4  1998/01/27 00:47:47  curt
199  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
200  * system and commandline/config file processing code.
201  *
202  * Revision 1.3  1998/01/22 02:59:27  curt
203  * Changed #ifdef FILE_H to #ifdef _FILE_H
204  *
205  * Revision 1.2  1998/01/19 19:26:58  curt
206  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
207  * This should simplify things tremendously.
208  *
209  * Revision 1.1  1998/01/07 03:16:17  curt
210  * Moved from .../Src/Scenery/ to .../Src/Astro/
211  *
212  * Revision 1.6  1997/12/30 20:47:52  curt
213  * Integrated new event manager with subsystem initializations.
214  *
215  * Revision 1.5  1997/12/15 23:55:02  curt
216  * Add xgl wrappers for debugging.
217  * Generate terrain normals on the fly.
218  *
219  * Revision 1.4  1997/12/10 22:37:51  curt
220  * Prepended "fg" on the name of all global structures that didn't have it yet.
221  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
222  *
223  * Revision 1.3  1997/11/25 23:20:44  curt
224  * Changed planets.dat Planets.dat
225  *
226  * Revision 1.2  1997/11/25 19:25:36  curt
227  * Changes to integrate Durk's moon/sun code updates + clean up.
228  *
229  * Revision 1.1  1997/10/25 03:16:10  curt
230  * Initial revision of code contributed by Durk Talsma.
231  *
232  */