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, 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,
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 );
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);
94 if( fabs(s) < 0.01 ) { // distance < centimeter => congruency
98 if( *az2 > 360.0 ) *az2 -= 360.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);
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,
113 // FIRST ESTIMATE OF SIGMA (SIG)
114 double first = s/(b*ta); // !!
116 double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
118 c2sigm = cos(2.0*sig1+sig);
119 sinsig = sin(sig); cossig = cos(sig);
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)
126 } while( fabs(sig-temp) > testv);
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);
134 rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
135 *lat2 = atan2(rnumer,denom)/RADDEG;
137 // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
138 rnumer = sinsig*sinaz1;
139 denom = cosu1*cossig-sinu1*sinsig*cosaz1;
140 dlams = atan2(rnumer,denom);
143 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
145 // DIFFERENCE IN LONGITUDE
146 dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
150 *lon2 = (lam1+dlam)/RADDEG;
151 if (*lon2 > 180.0 ) *lon2 -= 360.0;
152 if (*lon2 < -180.0 ) *lon2 += 360.0;
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;
159 } else { // phi1 == 90 degrees, polar origin
160 double dM = a*M0(e2) - s;
161 double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
163 return geo_direct_wgs_84( alt, zero, lon1, paz, dM, lat2, lon2, az2 );
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,
176 double a = EQURAD, rf = iFLATTENING;
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);
187 if( (fabs(lat1-lat2) < testv &&
188 ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
190 // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
191 *az1 = 0.0; *az2 = 0.0; *s = 0.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;
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,
204 k = k; // avoid compiler error since return result is unused
207 if( *az2 > 360.0 ) *az2 -= 360.0;
209 } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) &&
210 (fabs(lat1+lat2) < testv) )
212 // Geodesic passes through the pole (antipodal)
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 );
220 // antipodal and polar points don't get here
221 double dlam = lam2 - lam1, dlams = dlam;
222 double sdlams,cdlams, sig,sinsig,cossig, sinaz,
224 double tc,temp, us,rnumer,denom, ta,tb;
225 double cosu1,sinu1, sinu2,cosu2;
228 temp = (1.0-f)*sinphi1/cosphi1;
229 cosu1 = 1.0/sqrt(1.0+temp*temp);
231 temp = (1.0-f)*sinphi2/cosphi2;
232 cosu2 = 1.0/sqrt(1.0+temp*temp);
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;
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;
249 dlams = dlam+(1.0-tc)*f*sinaz*
251 (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
252 if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
255 } while ( fabs(temp-dlams) > testv);
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;
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;
273 ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
275 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
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)/