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