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