]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_geodesy.cxx
Mathias Fröhlich:
[simgear.git] / simgear / math / sg_geodesy.cxx
1 #include <simgear/constants.h>
2 #include "SGMath.hxx"
3 #include "sg_geodesy.hxx"
4
5 // Notes:
6 //
7 // The XYZ/cartesian coordinate system in use puts the X axis through
8 // zero lat/lon (off west Africa), the Z axis through the north pole,
9 // and the Y axis through 90 degrees longitude (in the Indian Ocean).
10 //
11 // All latitude and longitude values are in radians.  Altitude is in
12 // meters, with zero on the WGS84 ellipsoid.
13 //
14 // The code below makes use of the notion of "squashed" space.  This
15 // is a 2D cylindrical coordinate system where the radius from the Z
16 // axis is multiplied by SQUASH; the earth in this space is a perfect
17 // circle with a radius of POLRAD.
18 //
19 // Performance: with full optimization, a transformation from
20 // lat/lon/alt to XYZ and back takes 5263 CPU cycles on my 2.2GHz
21 // Pentium 4.  About 83% of this is spent in the iterative sgCartToGeod()
22 // algorithm.
23
24 // These are hard numbers from the WGS84 standard.  DON'T MODIFY
25 // unless you want to change the datum.
26 static const double EQURAD = 6378137;
27 static const double iFLATTENING = 298.257223563;
28
29 // These are derived quantities more useful to the code:
30 #if 0
31 static const double SQUASH = 1 - 1/iFLATTENING;
32 static const double STRETCH = 1/SQUASH;
33 static const double POLRAD = EQURAD * SQUASH;
34 #else
35 // High-precision versions of the above produced with an arbitrary
36 // precision calculator (the compiler might lose a few bits in the FPU
37 // operations).  These are specified to 81 bits of mantissa, which is
38 // higher than any FPU known to me:
39 static const double SQUASH  = 0.9966471893352525192801545;
40 static const double STRETCH = 1.0033640898209764189003079;
41 static const double POLRAD  = 6356752.3142451794975639668;
42 #endif
43
44 ////////////////////////////////////////////////////////////////////////
45 //
46 // Direct and inverse distance functions 
47 //
48 // Proceedings of the 7th International Symposium on Geodetic
49 // Computations, 1985
50 //
51 // "The Nested Coefficient Method for Accurate Solutions of Direct and
52 // Inverse Geodetic Problems With Any Length"
53 //
54 // Zhang Xue-Lian
55 // pp 747-763
56 //
57 // modified for FlightGear to use WGS84 only -- Norman Vine
58
59 static const double GEOD_INV_PI = SGD_PI;
60
61 // s == distance
62 // az = azimuth
63
64 static inline double M0( double e2 ) {
65     //double e4 = e2*e2;
66     return GEOD_INV_PI*(1.0 - e2*( 1.0/4.0 + e2*( 3.0/64.0 + 
67                                                   e2*(5.0/256.0) )))/2.0;
68 }
69
70
71 // given, alt, lat1, lon1, az1 and distance (s), calculate lat2, lon2
72 // and az2.  Lat, lon, and azimuth are in degrees.  distance in meters
73 int geo_direct_wgs_84 ( double alt, double lat1,
74                         double lon1, double az1,
75                         double s, double *lat2, double *lon2,
76                         double *az2 )
77 {
78     double a = EQURAD, rf = iFLATTENING;
79     double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
80     double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
81     double b = a*(1.0-f);
82     double e2 = f*(2.0-f);
83     double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
84     double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
85     double azm1 = az1*RADDEG;
86     double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
87         
88         
89     if( fabs(s) < 0.01 ) {      // distance < centimeter => congruency
90         *lat2 = lat1;
91         *lon2 = lon1;
92         *az2 = 180.0 + az1;
93         if( *az2 > 360.0 ) *az2 -= 360.0;
94         return 0;
95     } else if( cosphi1 ) {      // non-polar origin
96         // u1 is reduced latitude
97         double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
98         double sig1 = atan2(tanu1,cosaz1);
99         double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
100         double sinaz =  cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
101         double us = cos2saz*e2/(1.0-e2);
102
103         // Terms
104         double  ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
105             tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
106             tc = 0;
107
108         // FIRST ESTIMATE OF SIGMA (SIG)
109         double first = s/(b*ta);  // !!
110         double sig = first;
111         double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
112         do {
113             c2sigm = cos(2.0*sig1+sig);
114             sinsig = sin(sig); cossig = cos(sig);
115             temp = sig;
116             sig = first + 
117                 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm) - 
118                                       tb*c2sigm*(-3.0+4.0*sinsig*sinsig)
119                                       *(-3.0+4.0*c2sigm*c2sigm)/6.0)
120                            /4.0);
121         } while( fabs(sig-temp) > testv);
122
123         // LATITUDE OF POINT 2
124         // DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER)
125         temp = sinu1*sinsig-cosu1*cossig*cosaz1;
126         denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
127
128         // NUMERATOR
129         rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
130         *lat2 = atan2(rnumer,denom)/RADDEG;
131
132         // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
133         rnumer = sinsig*sinaz1;
134         denom = cosu1*cossig-sinu1*sinsig*cosaz1;
135         dlams = atan2(rnumer,denom);
136
137         // TERM C
138         tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
139
140         // DIFFERENCE IN LONGITUDE
141         dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
142                                        (c2sigm+
143                                         tc*cossig*(-1.0+2.0*
144                                                    c2sigm*c2sigm)));
145         *lon2 = (lam1+dlam)/RADDEG;
146         if (*lon2 > 180.0  ) *lon2 -= 360.0;
147         if (*lon2 < -180.0 ) *lon2 += 360.0;
148
149         // AZIMUTH - FROM NORTH
150         *az2 = atan2(-sinaz,temp)/RADDEG;
151         if ( fabs(*az2) < testv ) *az2 = 0.0;
152         if( *az2 < 0.0) *az2 += 360.0;
153         return 0;
154     } else {                    // phi1 == 90 degrees, polar origin
155         double dM = a*M0(e2) - s;
156         double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
157         double zero = 0.0f;
158         return geo_direct_wgs_84( alt, zero, lon1, paz, dM, lat2, lon2, az2 );
159     } 
160 }
161
162
163 // given alt, lat1, lon1, lat2, lon2, calculate starting and ending
164 // az1, az2 and distance (s).  Lat, lon, and azimuth are in degrees.
165 // distance in meters
166 int geo_inverse_wgs_84( double alt, double lat1,
167                         double lon1, double lat2,
168                         double lon2, double *az1, double *az2,
169                         double *s )
170 {
171     double a = EQURAD, rf = iFLATTENING;
172     int iter=0;
173     double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
174     double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
175     double b = a*(1.0-f);
176     // double e2 = f*(2.0-f); // unused in this routine
177     double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
178     double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
179     double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
180     double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
181         
182     if( (fabs(lat1-lat2) < testv && 
183          ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
184     {   
185         // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
186         *az1 = 0.0; *az2 = 0.0; *s = 0.0;
187         return 0;
188     } else if(  fabs(cosphi1) < testv ) {
189         // initial point is polar
190         int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
191         k = k; // avoid compiler error since return result is unused
192         b = *az1; *az1 = *az2; *az2 = b;
193         return 0;
194     } else if( fabs(cosphi2) < testv ) {
195         // terminal point is polar
196         double _lon1 = lon1 + 180.0f;
197         int k = geo_inverse_wgs_84( alt, lat1, lon1, lat1, _lon1, 
198                                     az1, az2, s );
199         k = k; // avoid compiler error since return result is unused
200         *s /= 2.0;
201         *az2 = *az1 + 180.0;
202         if( *az2 > 360.0 ) *az2 -= 360.0; 
203         return 0;
204     } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) && 
205                (fabs(lat1+lat2) < testv) ) 
206     {
207         // Geodesic passes through the pole (antipodal)
208         double s1,s2;
209         geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
210         geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
211         *az2 = *az1;
212         *s = s1 + s2;
213         return 0;
214     } else {
215         // antipodal and polar points don't get here
216         double dlam = lam2 - lam1, dlams = dlam;
217         double sdlams,cdlams, sig,sinsig,cossig, sinaz,
218             cos2saz, c2sigm;
219         double tc,temp, us,rnumer,denom, ta,tb;
220         double cosu1,sinu1, sinu2,cosu2;
221
222         // Reduced latitudes
223         temp = (1.0-f)*sinphi1/cosphi1;
224         cosu1 = 1.0/sqrt(1.0+temp*temp);
225         sinu1 = temp*cosu1;
226         temp = (1.0-f)*sinphi2/cosphi2;
227         cosu2 = 1.0/sqrt(1.0+temp*temp);
228         sinu2 = temp*cosu2;
229     
230         do {
231             sdlams = sin(dlams), cdlams = cos(dlams);
232             sinsig = sqrt(cosu2*cosu2*sdlams*sdlams+
233                           (cosu1*sinu2-sinu1*cosu2*cdlams)*
234                           (cosu1*sinu2-sinu1*cosu2*cdlams));
235             cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
236             
237             sig = atan2(sinsig,cossig);
238             sinaz = cosu1*cosu2*sdlams/sinsig;
239             cos2saz = 1.0-sinaz*sinaz;
240             c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig : 
241                       cossig-2.0*sinu1*sinu2/cos2saz);
242             tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
243             temp = dlams;
244             dlams = dlam+(1.0-tc)*f*sinaz*
245                 (sig+tc*sinsig*
246                  (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
247             if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
248                 return iter;
249             }
250         } while ( fabs(temp-dlams) > testv);
251
252         us = cos2saz*(a*a-b*b)/(b*b); // !!
253         // BACK AZIMUTH FROM NORTH
254         rnumer = -(cosu1*sdlams);
255         denom = sinu1*cosu2-cosu1*sinu2*cdlams;
256         *az2 = atan2(rnumer,denom)/RADDEG;
257         if( fabs(*az2) < testv ) *az2 = 0.0;
258         if(*az2 < 0.0) *az2 += 360.0;
259
260         // FORWARD AZIMUTH FROM NORTH
261         rnumer = cosu2*sdlams;
262         denom = cosu1*sinu2-sinu1*cosu2*cdlams;
263         *az1 = atan2(rnumer,denom)/RADDEG;
264         if( fabs(*az1) < testv ) *az1 = 0.0;
265         if(*az1 < 0.0) *az1 += 360.0;
266
267         // Terms a & b
268         ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
269             16384.0;
270         tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
271
272         // GEODETIC DISTANCE
273         *s = b*ta*(sig-tb*sinsig*
274                    (c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm)-tb*
275                                c2sigm*(-3.0+4.0*sinsig*sinsig)*
276                                (-3.0+4.0*c2sigm*c2sigm)/6.0)/
277                     4.0));
278         return 0;
279     }
280 }