]> git.mxchange.org Git - flightgear.git/blob - SplitTris/fg_geodesy.c
Incorporated into gnu automake/autoconf system.
[flightgear.git] / SplitTris / fg_geodesy.c
1 /**************************************************************************
2  * fg_geodesy.c -- routines to convert between geodetic and geocentric 
3  *                 coordinate systems.
4  *
5  * Copied and adapted directly from LaRCsim/ls_geodesy.c
6  *
7  * See below for the complete original LaRCsim comments.
8  *
9  * $Id$
10  * (Log is kept at end of this file)
11  **************************************************************************/
12
13
14 #include <math.h>
15
16 #include <Include/fg_constants.h>
17
18 #include "fg_geodesy.h"
19
20
21 /* ONE_SECOND is pi/180/60/60, or about 100 feet at earths' equator */
22 #define ONE_SECOND 4.848136811E-6
23
24
25 /* fgGeocToGeod(lat_geoc, radius, *lat_geod, *alt, *sea_level_r)
26  *     INPUTS:  
27  *         lat_geoc     Geocentric latitude, radians, + = North
28  *         radius       C.G. radius to earth center, ft
29  *
30  *     OUTPUTS:
31  *         lat_geod     Geodetic latitude, radians, + = North
32  *         alt          C.G. altitude above mean sea level, ft
33  *         sea_level_r  radius from earth center to sea level at
34  *                      local vertical (surface normal) of C.G.
35  */
36
37 void fgGeocToGeod( double lat_geoc, double radius, double
38                    *lat_geod, double *alt, double *sea_level_r )
39 {
40     double t_lat, x_alpha, mu_alpha, delt_mu, r_alpha, l_point, rho_alpha;
41     double sin_mu_a, denom,delt_lambda, lambda_sl, sin_lambda_sl;
42
43     if( ( (FG_PI_2 - lat_geoc) < ONE_SECOND )     /* near North pole */
44         || ( (FG_PI_2 + lat_geoc) < ONE_SECOND ) )   /* near South pole */
45     {
46         *lat_geod = lat_geoc;
47         *sea_level_r = EQUATORIAL_RADIUS_KM*E;
48         *alt = radius - *sea_level_r;
49     } else {
50         t_lat = tan(lat_geoc);
51         x_alpha = E*EQUATORIAL_RADIUS_KM/sqrt(t_lat*t_lat + E*E);
52         mu_alpha = atan2(sqrt(RESQ_KM - x_alpha*x_alpha),E*x_alpha);
53         if (lat_geoc < 0) mu_alpha = - mu_alpha;
54         sin_mu_a = sin(mu_alpha);
55         delt_lambda = mu_alpha - lat_geoc;
56         r_alpha = x_alpha/cos(lat_geoc);
57         l_point = radius - r_alpha;
58         *alt = l_point*cos(delt_lambda);
59         denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
60         rho_alpha = EQUATORIAL_RADIUS_KM*(1-EPS)/
61             (denom*denom*denom);
62         delt_mu = atan2(l_point*sin(delt_lambda),rho_alpha + *alt);
63         *lat_geod = mu_alpha - delt_mu;
64         lambda_sl = atan( E*E * tan(*lat_geod) ); /* SL geoc. latitude */
65         sin_lambda_sl = sin( lambda_sl );
66         *sea_level_r = 
67             sqrt(RESQ_KM / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
68     }
69 }
70
71
72 /* fgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
73  *     INPUTS:  
74  *         lat_geod     Geodetic latitude, radians, + = North
75  *         alt          C.G. altitude above mean sea level, ft
76  *
77  *     OUTPUTS:
78  *         sl_radius    SEA LEVEL radius to earth center, ft (add Altitude to
79  *                      get true distance from earth center.
80  *         lat_geoc     Geocentric latitude, radians, + = North
81  *
82  */
83
84 void fgGeodToGeoc( double lat_geod, double alt, double *sl_radius,
85                       double *lat_geoc )
86 {
87     double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
88     
89     lambda_sl = atan( E*E * tan(lat_geod) ); /* sea level geocentric latitude */
90     sin_lambda_sl = sin( lambda_sl );
91     cos_lambda_sl = cos( lambda_sl );
92     sin_mu = sin(lat_geod);     /* Geodetic (map makers') latitude */
93     cos_mu = cos(lat_geod);
94     *sl_radius = 
95         sqrt(RESQ_KM / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
96     py = *sl_radius*sin_lambda_sl + alt*sin_mu;
97     px = *sl_radius*cos_lambda_sl + alt*cos_mu;
98     *lat_geoc = atan2( py, px );
99 }
100
101
102 /***************************************************************************
103
104         TITLE:  ls_geodesy
105         
106 ----------------------------------------------------------------------------
107
108         FUNCTION:       Converts geocentric coordinates to geodetic positions
109
110 ----------------------------------------------------------------------------
111
112         MODULE STATUS:  developmental
113
114 ----------------------------------------------------------------------------
115
116         GENEALOGY:      Written as part of LaRCSim project by E. B. Jackson
117
118 ----------------------------------------------------------------------------
119
120         DESIGNED BY:    E. B. Jackson
121         
122         CODED BY:       E. B. Jackson
123         
124         MAINTAINED BY:  E. B. Jackson
125
126 ----------------------------------------------------------------------------
127
128         MODIFICATION HISTORY:
129         
130         DATE    PURPOSE                                         BY
131         
132         930208  Modified to avoid singularity near polar region.        EBJ
133         930602  Moved backwards calcs here from ls_step.                EBJ
134         931214  Changed erroneous Latitude and Altitude variables to 
135                 *lat_geod and *alt in routine ls_geoc_to_geod.          EBJ
136         940111  Changed header files from old ls_eom.h style to ls_types, 
137                 and ls_constants.  Also replaced old DATA type with new
138                 SCALAR type.                                            EBJ
139
140         CURRENT RCS HEADER:
141
142 $Header$
143 $Log$
144 Revision 1.1  1998/04/08 23:21:11  curt
145 Adopted Gnu automake/autoconf system.
146
147 Revision 1.4  1998/01/27 00:47:59  curt
148 Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
149 system and commandline/config file processing code.
150
151 Revision 1.3  1998/01/19 19:27:12  curt
152 Merged in make system changes from Bob Kuehne <rpk@sgi.com>
153 This should simplify things tremendously.
154
155 Revision 1.2  1997/12/15 23:54:54  curt
156 Add xgl wrappers for debugging.
157 Generate terrain normals on the fly.
158
159 Revision 1.1  1997/07/31 23:13:14  curt
160 Initial revision.
161
162 Revision 1.1  1997/05/29 00:09:56  curt
163 Initial Flight Gear revision.
164
165  * Revision 1.5  1994/01/11  18:47:05  bjax
166  * Changed include files to use types and constants, not ls_eom.h
167  * Also changed DATA type to SCALAR type.
168  *
169  * Revision 1.4  1993/12/14  21:06:47  bjax
170  * Removed global variable references Altitude and Latitude.   EBJ
171  *
172  * Revision 1.3  1993/06/02  15:03:40  bjax
173  * Made new subroutine for calculating geodetic to geocentric; changed name
174  * of forward conversion routine from ls_geodesy to ls_geoc_to_geod.
175  *
176
177 ----------------------------------------------------------------------------
178
179         REFERENCES:
180
181                 [ 1]    Stevens, Brian L.; and Lewis, Frank L.: "Aircraft 
182                         Control and Simulation", Wiley and Sons, 1992.
183                         ISBN 0-471-61397-5                    
184
185
186 ----------------------------------------------------------------------------
187
188         CALLED BY:      ls_aux
189
190 ----------------------------------------------------------------------------
191
192         CALLS TO:
193
194 ----------------------------------------------------------------------------
195
196         INPUTS: 
197                 lat_geoc        Geocentric latitude, radians, + = North
198                 radius          C.G. radius to earth center, ft
199
200 ----------------------------------------------------------------------------
201
202         OUTPUTS:
203                 lat_geod        Geodetic latitude, radians, + = North
204                 alt             C.G. altitude above mean sea level, ft
205                 sea_level_r     radius from earth center to sea level at
206                                 local vertical (surface normal) of C.G.
207
208 --------------------------------------------------------------------------*/
209
210
211 /* $Log$
212 /* Revision 1.1  1998/04/08 23:21:11  curt
213 /* Adopted Gnu automake/autoconf system.
214 /*
215  * Revision 1.4  1998/01/27 00:47:59  curt
216  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
217  * system and commandline/config file processing code.
218  *
219  * Revision 1.3  1998/01/19 19:27:12  curt
220  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
221  * This should simplify things tremendously.
222  *
223  * Revision 1.2  1997/12/15 23:54:54  curt
224  * Add xgl wrappers for debugging.
225  * Generate terrain normals on the fly.
226  *
227  * Revision 1.1  1997/07/31 23:13:14  curt
228  * Initial revision.
229  *
230  */