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