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