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