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