3 # include <simgear_config.h>
6 #include <simgear/constants.h>
8 #include "sg_geodesy.hxx"
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).
16 // All latitude and longitude values are in radians. Altitude is in
17 // meters, with zero on the WGS84 ellipsoid.
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.
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()
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;
34 // These are derived quantities more useful to the code:
36 static const double SQUASH = 1 - 1/iFLATTENING;
37 static const double STRETCH = 1/SQUASH;
38 static const double POLRAD = EQURAD * SQUASH;
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;
49 ////////////////////////////////////////////////////////////////////////
51 // Direct and inverse distance functions
53 // Proceedings of the 7th International Symposium on Geodetic
56 // "The Nested Coefficient Method for Accurate Solutions of Direct and
57 // Inverse Geodetic Problems With Any Length"
62 // modified for FlightGear to use WGS84 only -- Norman Vine
64 static const double GEOD_INV_PI = SGD_PI;
69 static inline double M0( double 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;
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,
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 );
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);
93 if( fabs(s) < 0.01 ) { // distance < centimeter => congruency
97 if( *az2 > 360.0 ) *az2 -= 360.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);
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,
112 // FIRST ESTIMATE OF SIGMA (SIG)
113 double first = s/(b*ta); // !!
115 double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
117 c2sigm = cos(2.0*sig1+sig);
118 sinsig = sin(sig); cossig = cos(sig);
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)
125 } while( fabs(sig-temp) > testv);
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);
133 rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
134 *lat2 = atan2(rnumer,denom)/RADDEG;
136 // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
137 rnumer = sinsig*sinaz1;
138 denom = cosu1*cossig-sinu1*sinsig*cosaz1;
139 dlams = atan2(rnumer,denom);
142 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
144 // DIFFERENCE IN LONGITUDE
145 dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
149 *lon2 = (lam1+dlam)/RADDEG;
150 if (*lon2 > 180.0 ) *lon2 -= 360.0;
151 if (*lon2 < -180.0 ) *lon2 += 360.0;
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;
158 } else { // phi1 == 90 degrees, polar origin
159 double dM = a*M0(e2) - s;
160 double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
162 return geo_direct_wgs_84( zero, lon1, paz, dM, lat2, lon2, az2 );
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,
174 double a = EQURAD, rf = iFLATTENING;
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);
185 if( (fabs(lat1-lat2) < testv &&
186 ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
188 // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
189 *az1 = 0.0; *az2 = 0.0; *s = 0.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;
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,
202 k = k; // avoid compiler error since return result is unused
205 if( *az2 > 360.0 ) *az2 -= 360.0;
207 } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) &&
208 (fabs(lat1+lat2) < testv) )
210 // Geodesic passes through the pole (antipodal)
212 geo_inverse_wgs_84( lat1,lon1, lat1,lon2, az1,az2, &s1 );
213 geo_inverse_wgs_84( lat2,lon2, lat1,lon2, az1,az2, &s2 );
218 // antipodal and polar points don't get here
219 double dlam = lam2 - lam1, dlams = dlam;
220 double sdlams,cdlams, sig,sinsig,cossig, sinaz,
222 double tc,temp, us,rnumer,denom, ta,tb;
223 double cosu1,sinu1, sinu2,cosu2;
226 temp = (1.0-f)*sinphi1/cosphi1;
227 cosu1 = 1.0/sqrt(1.0+temp*temp);
229 temp = (1.0-f)*sinphi2/cosphi2;
230 cosu2 = 1.0/sqrt(1.0+temp*temp);
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;
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;
247 dlams = dlam+(1.0-tc)*f*sinaz*
249 (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
250 if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
253 } while ( fabs(temp-dlams) > testv);
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;
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;
271 ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
273 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
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)/