]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_geodesy.cxx
Fix a build order problem.
[simgear.git] / simgear / math / sg_geodesy.cxx
1 // sg_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
10 #include <simgear/compiler.h>
11
12 #ifdef FG_HAVE_STD_INCLUDES
13 # include <cmath>
14 # include <cerrno>
15 #else
16 # include <math.h>
17 # include <errno.h>
18 #endif
19
20 #include <simgear/constants.h>
21 #include <simgear/debug/logstream.hxx>
22
23 #include "point3d.hxx"
24 #include "sg_geodesy.hxx"
25 #include "localconsts.hxx"
26
27
28 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
29 FG_USING_STD(cout);
30 #endif
31
32 // ONE_SECOND is pi/180/60/60, or about 100 feet at earths' equator
33 #define ONE_SECOND 4.848136811E-6
34
35
36 // sgGeocToGeod(lat_geoc, radius, *lat_geod, *alt, *sea_level_r)
37 //     INPUTS:  
38 //         lat_geoc     Geocentric latitude, radians, + = North
39 //         radius       C.G. radius to earth center (meters)
40 //
41 //     OUTPUTS:
42 //         lat_geod     Geodetic latitude, radians, + = North
43 //         alt          C.G. altitude above mean sea level (meters)
44 //         sea_level_r  radius from earth center to sea level at
45 //                      local vertical (surface normal) of C.G. (meters)
46
47
48 void sgGeocToGeod( double lat_geoc, double radius, double
49                    *lat_geod, double *alt, double *sea_level_r )
50 {
51     double t_lat, x_alpha, mu_alpha, delt_mu, r_alpha, l_point, rho_alpha;
52     double sin_mu_a, denom,delt_lambda, lambda_sl, sin_lambda_sl;
53
54     if( ( (FG_PI_2 - lat_geoc) < ONE_SECOND )        // near North pole
55         || ( (FG_PI_2 + lat_geoc) < ONE_SECOND ) )   // near South pole
56     {
57         *lat_geod = lat_geoc;
58         *sea_level_r = EQUATORIAL_RADIUS_M*E;
59         *alt = radius - *sea_level_r;
60     } else {
61         // cout << "  lat_geoc = " << lat_geoc << endl;
62         t_lat = tan(lat_geoc);
63         // cout << "  tan(t_lat) = " << t_lat << endl;
64         x_alpha = E*EQUATORIAL_RADIUS_M/sqrt(t_lat*t_lat + E*E);
65         // cout << "  x_alpha = " << x_alpha << endl;
66         double tmp = RESQ_M - x_alpha * x_alpha;
67         if ( tmp < 0.0 ) { tmp = 0.0; }
68         mu_alpha = atan2(sqrt(tmp),E*x_alpha);
69         if (lat_geoc < 0) mu_alpha = - mu_alpha;
70         sin_mu_a = sin(mu_alpha);
71         delt_lambda = mu_alpha - lat_geoc;
72         r_alpha = x_alpha/cos(lat_geoc);
73         l_point = radius - r_alpha;
74         *alt = l_point*cos(delt_lambda);
75
76         // check for domain error
77         if ( errno == EDOM ) {
78             FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
79             *alt = 0.0;
80         }
81
82         denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
83         rho_alpha = EQUATORIAL_RADIUS_M*(1-EPS)/
84             (denom*denom*denom);
85         delt_mu = atan2(l_point*sin(delt_lambda),rho_alpha + *alt);
86         *lat_geod = mu_alpha - delt_mu;
87         lambda_sl = atan( E*E * tan(*lat_geod) ); // SL geoc. latitude
88         sin_lambda_sl = sin( lambda_sl );
89         *sea_level_r = 
90             sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
91
92         // check for domain error
93         if ( errno == EDOM ) {
94             FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in sgGeocToGeod!!!!" );
95             *sea_level_r = 0.0;
96         }
97     }
98
99 }
100
101
102 // sgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
103 //     INPUTS:  
104 //         lat_geod     Geodetic latitude, radians, + = North
105 //         alt          C.G. altitude above mean sea level (meters)
106 //
107 //     OUTPUTS:
108 //         sl_radius    SEA LEVEL radius to earth center (meters)
109 //                      (add Altitude to get true distance from earth center.
110 //         lat_geoc     Geocentric latitude, radians, + = North
111 //
112
113
114 void sgGeodToGeoc( double lat_geod, double alt, double *sl_radius,
115                       double *lat_geoc )
116 {
117     double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
118     
119     lambda_sl = atan( E*E * tan(lat_geod) ); // sea level geocentric latitude
120     sin_lambda_sl = sin( lambda_sl );
121     cos_lambda_sl = cos( lambda_sl );
122     sin_mu = sin(lat_geod);                  // Geodetic (map makers') latitude
123     cos_mu = cos(lat_geod);
124     *sl_radius = 
125         sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
126     py = *sl_radius*sin_lambda_sl + alt*sin_mu;
127     px = *sl_radius*cos_lambda_sl + alt*cos_mu;
128     *lat_geoc = atan2( py, px );
129 }
130
131
132 // Direct and inverse distance functions 
133 //
134 // Proceedings of the 7th International Symposium on Geodetic
135 // Computations, 1985
136 //
137 // "The Nested Coefficient Method for Accurate Solutions of Direct and
138 // Inverse Geodetic Problems With Any Length"
139 //
140 // Zhang Xue-Lian
141 // pp 747-763
142 //
143 // modified for FlightGear to use WGS84 only -- Norman Vine
144
145 #define GEOD_INV_PI FG_PI
146
147 // s == distance
148 // az = azimuth
149
150 // for WGS_84 a = 6378137.000, rf = 298.257223563;
151
152 static double M0( double e2 ) {
153     //double e4 = e2*e2;
154     return GEOD_INV_PI*(1.0 - e2*( 1.0/4.0 + e2*( 3.0/64.0 + 
155                                                   e2*(5.0/256.0) )))/2.0;
156 }
157
158
159 // given, alt, lat1, lon1, az1 and distance (s), calculate lat2, lon2
160 // and az2.  Lat, lon, and azimuth are in degrees.  distance in meters
161 int geo_direct_wgs_84 ( double alt, double lat1, double lon1, double az1, 
162                         double s, double *lat2, double *lon2,  double *az2 )
163 {
164     double a = 6378137.000, rf = 298.257223563;
165     double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
166     double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
167     double b = a*(1.0-f);
168     double e2 = f*(2.0-f);
169     double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
170     double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
171     double azm1 = az1*RADDEG;
172     double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
173         
174         
175     if( fabs(s) < 0.01 ) {      // distance < centimeter => congruency
176         *lat2 = lat1;
177         *lon2 = lon1;
178         *az2 = 180.0 + az1;
179         if( *az2 > 360.0 ) *az2 -= 360.0;
180         return 0;
181     } else if( cosphi1 ) {      // non-polar origin
182         // u1 is reduced latitude
183         double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
184         double sig1 = atan2(tanu1,cosaz1);
185         double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
186         double sinaz =  cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
187         double us = cos2saz*e2/(1.0-e2);
188
189         // Terms
190         double  ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
191             tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
192             tc = 0;
193
194         // FIRST ESTIMATE OF SIGMA (SIG)
195         double first = s/(b*ta);  // !!
196         double sig = first;
197         double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
198         do {
199             c2sigm = cos(2.0*sig1+sig);
200             sinsig = sin(sig); cossig = cos(sig);
201             temp = sig;
202             sig = first + 
203                 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm) - 
204                                       tb*c2sigm*(-3.0+4.0*sinsig*sinsig)
205                                       *(-3.0+4.0*c2sigm*c2sigm)/6.0)
206                            /4.0);
207         } while( fabs(sig-temp) > testv);
208
209         // LATITUDE OF POINT 2
210         // DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER)
211         temp = sinu1*sinsig-cosu1*cossig*cosaz1;
212         denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
213
214         // NUMERATOR
215         rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
216         *lat2 = atan2(rnumer,denom)/RADDEG;
217
218         // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
219         rnumer = sinsig*sinaz1;
220         denom = cosu1*cossig-sinu1*sinsig*cosaz1;
221         dlams = atan2(rnumer,denom);
222
223         // TERM C
224         tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
225
226         // DIFFERENCE IN LONGITUDE
227         dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
228                                        (c2sigm+
229                                         tc*cossig*(-1.0+2.0*
230                                                    c2sigm*c2sigm)));
231         *lon2 = (lam1+dlam)/RADDEG;
232         if (*lon2 > 180.0  ) *lon2 -= 360.0;
233         if (*lon2 < -180.0 ) *lon2 += 360.0;
234
235         // AZIMUTH - FROM NORTH
236         *az2 = atan2(-sinaz,temp)/RADDEG;
237         if ( fabs(*az2) < testv ) *az2 = 0.0;
238         if( *az2 < 0.0) *az2 += 360.0;
239         return 0;
240     } else {                    // phi1 == 90 degrees, polar origin
241         double dM = a*M0(e2) - s;
242         double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
243         return geo_direct_wgs_84( alt, 0.0, lon1, paz, dM,lat2,lon2,az2 );
244     } 
245 }
246
247
248 // given alt, lat1, lon1, lat2, lon2, calculate starting and ending
249 // az1, az2 and distance (s).  Lat, lon, and azimuth are in degrees.
250 // distance in meters
251 int geo_inverse_wgs_84( double alt, double lat1, double lon1, double lat2,
252                         double lon2, double *az1, double *az2, double *s )
253 {
254     double a = 6378137.000, rf = 298.257223563;
255     int iter=0;
256     double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
257     double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
258     double b = a*(1.0-f);
259     // double e2 = f*(2.0-f); // unused in this routine
260     double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
261     double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
262     double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
263     double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
264         
265     if( (fabs(lat1-lat2) < testv && 
266          ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
267     {   
268         // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
269         *az1 = 0.0; *az2 = 0.0; *s = 0.0;
270         return 0;
271     } else if(  fabs(cosphi1) < testv ) {
272         // initial point is polar
273         int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
274         k = k; // avoid compiler error since return result is unused
275         b = *az1; *az1 = *az2; *az2 = b;
276         return 0;
277     } else if( fabs(cosphi2) < testv ) {
278         // terminal point is polar
279         int k = geo_inverse_wgs_84( alt, lat1,lon1,lat1,lon1+180.0, 
280                                     az1,az2,s );
281         k = k; // avoid compiler error since return result is unused
282         *s /= 2.0;
283         *az2 = *az1 + 180.0;
284         if( *az2 > 360.0 ) *az2 -= 360.0; 
285         return 0;
286     } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) && 
287                (fabs(lat1+lat2) < testv) ) 
288     {
289         // Geodesic passes through the pole (antipodal)
290         double s1,s2;
291         geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
292         geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
293         *az2 = *az1;
294         *s = s1 + s2;
295         return 0;
296     } else {
297         // antipodal and polar points don't get here
298         double dlam = lam2 - lam1, dlams = dlam;
299         double sdlams,cdlams, sig,sinsig,cossig, sinaz,
300             cos2saz, c2sigm;
301         double tc,temp, us,rnumer,denom, ta,tb;
302         double cosu1,sinu1, sinu2,cosu2;
303
304         // Reduced latitudes
305         temp = (1.0-f)*sinphi1/cosphi1;
306         cosu1 = 1.0/sqrt(1.0+temp*temp);
307         sinu1 = temp*cosu1;
308         temp = (1.0-f)*sinphi2/cosphi2;
309         cosu2 = 1.0/sqrt(1.0+temp*temp);
310         sinu2 = temp*cosu2;
311     
312         do {
313             sdlams = sin(dlams), cdlams = cos(dlams);
314             sinsig = sqrt(cosu2*cosu2*sdlams*sdlams+
315                           (cosu1*sinu2-sinu1*cosu2*cdlams)*
316                           (cosu1*sinu2-sinu1*cosu2*cdlams));
317             cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
318             
319             sig = atan2(sinsig,cossig);
320             sinaz = cosu1*cosu2*sdlams/sinsig;
321             cos2saz = 1.0-sinaz*sinaz;
322             c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig : 
323                       cossig-2.0*sinu1*sinu2/cos2saz);
324             tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
325             temp = dlams;
326             dlams = dlam+(1.0-tc)*f*sinaz*
327                 (sig+tc*sinsig*
328                  (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
329             if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
330                 return iter;
331             }
332         } while ( fabs(temp-dlams) > testv);
333
334         us = cos2saz*(a*a-b*b)/(b*b); // !!
335         // BACK AZIMUTH FROM NORTH
336         rnumer = -(cosu1*sdlams);
337         denom = sinu1*cosu2-cosu1*sinu2*cdlams;
338         *az2 = atan2(rnumer,denom)/RADDEG;
339         if( fabs(*az2) < testv ) *az2 = 0.0;
340         if(*az2 < 0.0) *az2 += 360.0;
341
342         // FORWARD AZIMUTH FROM NORTH
343         rnumer = cosu2*sdlams;
344         denom = cosu1*sinu2-sinu1*cosu2*cdlams;
345         *az1 = atan2(rnumer,denom)/RADDEG;
346         if( fabs(*az1) < testv ) *az1 = 0.0;
347         if(*az1 < 0.0) *az1 += 360.0;
348
349         // Terms a & b
350         ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
351             16384.0;
352         tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
353
354         // GEODETIC DISTANCE
355         *s = b*ta*(sig-tb*sinsig*
356                    (c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm)-tb*
357                                c2sigm*(-3.0+4.0*sinsig*sinsig)*
358                                (-3.0+4.0*c2sigm*c2sigm)/6.0)/
359                     4.0));
360         return 0;
361     }
362 }
363
364
365 /***************************************************************************
366
367         TITLE:  ls_geodesy
368         
369 ----------------------------------------------------------------------------
370
371         FUNCTION:       Converts geocentric coordinates to geodetic positions
372
373 ----------------------------------------------------------------------------
374
375         MODULE STATUS:  developmental
376
377 ----------------------------------------------------------------------------
378
379         GENEALOGY:      Written as part of LaRCSim project by E. B. Jackson
380
381 ----------------------------------------------------------------------------
382
383         DESIGNED BY:    E. B. Jackson
384         
385         CODED BY:       E. B. Jackson
386         
387         MAINTAINED BY:  E. B. Jackson
388
389 ----------------------------------------------------------------------------
390
391         MODIFICATION HISTORY:
392         
393         DATE    PURPOSE                                         BY
394         
395         930208  Modified to avoid singularity near polar region.        EBJ
396         930602  Moved backwards calcs here from ls_step.                EBJ
397         931214  Changed erroneous Latitude and Altitude variables to 
398                 *lat_geod and *alt in routine ls_geoc_to_geod.          EBJ
399         940111  Changed header files from old ls_eom.h style to ls_types, 
400                 and ls_constants.  Also replaced old DATA type with new
401                 SCALAR type.                                            EBJ
402
403         CURRENT RCS HEADER:
404
405 $Header$
406  * Revision 1.5  1994/01/11  18:47:05  bjax
407  * Changed include files to use types and constants, not ls_eom.h
408  * Also changed DATA type to SCALAR type.
409  *
410  * Revision 1.4  1993/12/14  21:06:47  bjax
411  * Removed global variable references Altitude and Latitude.   EBJ
412  *
413  * Revision 1.3  1993/06/02  15:03:40  bjax
414  * Made new subroutine for calculating geodetic to geocentric; changed name
415  * of forward conversion routine from ls_geodesy to ls_geoc_to_geod.
416  *
417
418 ----------------------------------------------------------------------------
419
420         REFERENCES:
421
422                 [ 1]    Stevens, Brian L.; and Lewis, Frank L.: "Aircraft 
423                         Control and Simulation", Wiley and Sons, 1992.
424                         ISBN 0-471-61397-5                    
425
426
427 ----------------------------------------------------------------------------
428
429         CALLED BY:      ls_aux
430
431 ----------------------------------------------------------------------------
432
433         CALLS TO:
434
435 ----------------------------------------------------------------------------
436
437         INPUTS: 
438                 lat_geoc        Geocentric latitude, radians, + = North
439                 radius          C.G. radius to earth center, ft
440
441 ----------------------------------------------------------------------------
442
443         OUTPUTS:
444                 lat_geod        Geodetic latitude, radians, + = North
445                 alt             C.G. altitude above mean sea level, ft
446                 sea_level_r     radius from earth center to sea level at
447                                 local vertical (surface normal) of C.G.
448
449 --------------------------------------------------------------------------*/
450
451