]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_geodesy.cxx
Redefine the default PLIB loader behavior : don't clear the texture cache after every...
[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, alt, 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 alt, double lat1,
79                         double lon1, double az1,
80                         double s, double *lat2, double *lon2,
81                         double *az2 )
82 {
83     double a = EQURAD, rf = iFLATTENING;
84     double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
85     double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
86     double b = a*(1.0-f);
87     double e2 = f*(2.0-f);
88     double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
89     double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
90     double azm1 = az1*RADDEG;
91     double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
92         
93         
94     if( fabs(s) < 0.01 ) {      // distance < centimeter => congruency
95         *lat2 = lat1;
96         *lon2 = lon1;
97         *az2 = 180.0 + az1;
98         if( *az2 > 360.0 ) *az2 -= 360.0;
99         return 0;
100     } else if( cosphi1 ) {      // non-polar origin
101         // u1 is reduced latitude
102         double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
103         double sig1 = atan2(tanu1,cosaz1);
104         double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
105         double sinaz =  cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
106         double us = cos2saz*e2/(1.0-e2);
107
108         // Terms
109         double  ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
110             tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
111             tc = 0;
112
113         // FIRST ESTIMATE OF SIGMA (SIG)
114         double first = s/(b*ta);  // !!
115         double sig = first;
116         double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
117         do {
118             c2sigm = cos(2.0*sig1+sig);
119             sinsig = sin(sig); cossig = cos(sig);
120             temp = sig;
121             sig = first + 
122                 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm) - 
123                                       tb*c2sigm*(-3.0+4.0*sinsig*sinsig)
124                                       *(-3.0+4.0*c2sigm*c2sigm)/6.0)
125                            /4.0);
126         } while( fabs(sig-temp) > testv);
127
128         // LATITUDE OF POINT 2
129         // DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER)
130         temp = sinu1*sinsig-cosu1*cossig*cosaz1;
131         denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
132
133         // NUMERATOR
134         rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
135         *lat2 = atan2(rnumer,denom)/RADDEG;
136
137         // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
138         rnumer = sinsig*sinaz1;
139         denom = cosu1*cossig-sinu1*sinsig*cosaz1;
140         dlams = atan2(rnumer,denom);
141
142         // TERM C
143         tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
144
145         // DIFFERENCE IN LONGITUDE
146         dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
147                                        (c2sigm+
148                                         tc*cossig*(-1.0+2.0*
149                                                    c2sigm*c2sigm)));
150         *lon2 = (lam1+dlam)/RADDEG;
151         if (*lon2 > 180.0  ) *lon2 -= 360.0;
152         if (*lon2 < -180.0 ) *lon2 += 360.0;
153
154         // AZIMUTH - FROM NORTH
155         *az2 = atan2(-sinaz,temp)/RADDEG;
156         if ( fabs(*az2) < testv ) *az2 = 0.0;
157         if( *az2 < 0.0) *az2 += 360.0;
158         return 0;
159     } else {                    // phi1 == 90 degrees, polar origin
160         double dM = a*M0(e2) - s;
161         double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
162         double zero = 0.0f;
163         return geo_direct_wgs_84( alt, zero, lon1, paz, dM, lat2, lon2, az2 );
164     } 
165 }
166
167
168 // given alt, lat1, lon1, lat2, lon2, calculate starting and ending
169 // az1, az2 and distance (s).  Lat, lon, and azimuth are in degrees.
170 // distance in meters
171 int geo_inverse_wgs_84( double alt, double lat1,
172                         double lon1, double lat2,
173                         double lon2, double *az1, double *az2,
174                         double *s )
175 {
176     double a = EQURAD, rf = iFLATTENING;
177     int iter=0;
178     double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
179     double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
180     double b = a*(1.0-f);
181     // double e2 = f*(2.0-f); // unused in this routine
182     double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
183     double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
184     double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
185     double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
186         
187     if( (fabs(lat1-lat2) < testv && 
188          ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
189     {   
190         // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
191         *az1 = 0.0; *az2 = 0.0; *s = 0.0;
192         return 0;
193     } else if(  fabs(cosphi1) < testv ) {
194         // initial point is polar
195         int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
196         k = k; // avoid compiler error since return result is unused
197         b = *az1; *az1 = *az2; *az2 = b;
198         return 0;
199     } else if( fabs(cosphi2) < testv ) {
200         // terminal point is polar
201         double _lon1 = lon1 + 180.0f;
202         int k = geo_inverse_wgs_84( alt, lat1, lon1, lat1, _lon1, 
203                                     az1, az2, s );
204         k = k; // avoid compiler error since return result is unused
205         *s /= 2.0;
206         *az2 = *az1 + 180.0;
207         if( *az2 > 360.0 ) *az2 -= 360.0; 
208         return 0;
209     } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) && 
210                (fabs(lat1+lat2) < testv) ) 
211     {
212         // Geodesic passes through the pole (antipodal)
213         double s1,s2;
214         geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
215         geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
216         *az2 = *az1;
217         *s = s1 + s2;
218         return 0;
219     } else {
220         // antipodal and polar points don't get here
221         double dlam = lam2 - lam1, dlams = dlam;
222         double sdlams,cdlams, sig,sinsig,cossig, sinaz,
223             cos2saz, c2sigm;
224         double tc,temp, us,rnumer,denom, ta,tb;
225         double cosu1,sinu1, sinu2,cosu2;
226
227         // Reduced latitudes
228         temp = (1.0-f)*sinphi1/cosphi1;
229         cosu1 = 1.0/sqrt(1.0+temp*temp);
230         sinu1 = temp*cosu1;
231         temp = (1.0-f)*sinphi2/cosphi2;
232         cosu2 = 1.0/sqrt(1.0+temp*temp);
233         sinu2 = temp*cosu2;
234     
235         do {
236             sdlams = sin(dlams), cdlams = cos(dlams);
237             sinsig = sqrt(cosu2*cosu2*sdlams*sdlams+
238                           (cosu1*sinu2-sinu1*cosu2*cdlams)*
239                           (cosu1*sinu2-sinu1*cosu2*cdlams));
240             cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
241             
242             sig = atan2(sinsig,cossig);
243             sinaz = cosu1*cosu2*sdlams/sinsig;
244             cos2saz = 1.0-sinaz*sinaz;
245             c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig : 
246                       cossig-2.0*sinu1*sinu2/cos2saz);
247             tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
248             temp = dlams;
249             dlams = dlam+(1.0-tc)*f*sinaz*
250                 (sig+tc*sinsig*
251                  (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
252             if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
253                 return iter;
254             }
255         } while ( fabs(temp-dlams) > testv);
256
257         us = cos2saz*(a*a-b*b)/(b*b); // !!
258         // BACK AZIMUTH FROM NORTH
259         rnumer = -(cosu1*sdlams);
260         denom = sinu1*cosu2-cosu1*sinu2*cdlams;
261         *az2 = atan2(rnumer,denom)/RADDEG;
262         if( fabs(*az2) < testv ) *az2 = 0.0;
263         if(*az2 < 0.0) *az2 += 360.0;
264
265         // FORWARD AZIMUTH FROM NORTH
266         rnumer = cosu2*sdlams;
267         denom = cosu1*sinu2-sinu1*cosu2*cdlams;
268         *az1 = atan2(rnumer,denom)/RADDEG;
269         if( fabs(*az1) < testv ) *az1 = 0.0;
270         if(*az1 < 0.0) *az1 += 360.0;
271
272         // Terms a & b
273         ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
274             16384.0;
275         tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
276
277         // GEODETIC DISTANCE
278         *s = b*ta*(sig-tb*sinsig*
279                    (c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm)-tb*
280                                c2sigm*(-3.0+4.0*sinsig*sinsig)*
281                                (-3.0+4.0*c2sigm*c2sigm)/6.0)/
282                     4.0));
283         return 0;
284     }
285 }