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