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