C library for Geodesics  1.48
geodesic.c
Go to the documentation of this file.
1 /**
2  * \file geodesic.c
3  * \brief Implementation of the geodesic routines in C
4  *
5  * For the full documentation see geodesic.h.
6  **********************************************************************/
7 
8 /** @cond SKIP */
9 
10 /*
11  * This is a C implementation of the geodesic algorithms described in
12  *
13  * C. F. F. Karney,
14  * Algorithms for geodesics,
15  * J. Geodesy <b>87</b>, 43--55 (2013);
16  * https://doi.org/10.1007/s00190-012-0578-z
17  * Addenda: https://geographiclib.sourceforge.io/geod-addenda.html
18  *
19  * See the comments in geodesic.h for documentation.
20  *
21  * Copyright (c) Charles Karney (2012-2017) <charles@karney.com> and licensed
22  * under the MIT/X11 License. For more information, see
23  * https://geographiclib.sourceforge.io/
24  */
25 
26 #include "geodesic.h"
27 #include <math.h>
28 
29 #define GEOGRAPHICLIB_GEODESIC_ORDER 6
30 #define nA1 GEOGRAPHICLIB_GEODESIC_ORDER
31 #define nC1 GEOGRAPHICLIB_GEODESIC_ORDER
32 #define nC1p GEOGRAPHICLIB_GEODESIC_ORDER
33 #define nA2 GEOGRAPHICLIB_GEODESIC_ORDER
34 #define nC2 GEOGRAPHICLIB_GEODESIC_ORDER
35 #define nA3 GEOGRAPHICLIB_GEODESIC_ORDER
36 #define nA3x nA3
37 #define nC3 GEOGRAPHICLIB_GEODESIC_ORDER
38 #define nC3x ((nC3 * (nC3 - 1)) / 2)
39 #define nC4 GEOGRAPHICLIB_GEODESIC_ORDER
40 #define nC4x ((nC4 * (nC4 + 1)) / 2)
41 #define nC (GEOGRAPHICLIB_GEODESIC_ORDER + 1)
42 
43 typedef double real;
44 typedef int boolx;
45 
46 static unsigned init = 0;
47 static const int FALSE = 0;
48 static const int TRUE = 1;
49 static unsigned digits, maxit1, maxit2;
50 static real epsilon, realmin, pi, degree, NaN,
51  tiny, tol0, tol1, tol2, tolb, xthresh;
52 
53 static void Init() {
54  if (!init) {
55 #if defined(__DBL_MANT_DIG__)
56  digits = __DBL_MANT_DIG__;
57 #else
58  digits = 53;
59 #endif
60 #if defined(__DBL_EPSILON__)
61  epsilon = __DBL_EPSILON__;
62 #else
63  epsilon = pow(0.5, digits - 1);
64 #endif
65 #if defined(__DBL_MIN__)
66  realmin = __DBL_MIN__;
67 #else
68  realmin = pow(0.5, 1022);
69 #endif
70 #if defined(M_PI)
71  pi = M_PI;
72 #else
73  pi = atan2(0.0, -1.0);
74 #endif
75  maxit1 = 20;
76  maxit2 = maxit1 + digits + 10;
77  tiny = sqrt(realmin);
78  tol0 = epsilon;
79  /* Increase multiplier in defn of tol1 from 100 to 200 to fix inverse case
80  * 52.784459512564 0 -52.784459512563990912 179.634407464943777557
81  * which otherwise failed for Visual Studio 10 (Release and Debug) */
82  tol1 = 200 * tol0;
83  tol2 = sqrt(tol0);
84  /* Check on bisection interval */
85  tolb = tol0 * tol2;
86  xthresh = 1000 * tol2;
87  degree = pi/180;
88  {
89  real minus1 = -1;
90  NaN = sqrt(minus1);
91  }
92  init = 1;
93  }
94 }
95 
96 enum captype {
97  CAP_NONE = 0U,
98  CAP_C1 = 1U<<0,
99  CAP_C1p = 1U<<1,
100  CAP_C2 = 1U<<2,
101  CAP_C3 = 1U<<3,
102  CAP_C4 = 1U<<4,
103  CAP_ALL = 0x1FU,
104  OUT_ALL = 0x7F80U
105 };
106 
107 static real sq(real x) { return x * x; }
108 static real log1px(real x) {
109  volatile real
110  y = 1 + x,
111  z = y - 1;
112  /* Here's the explanation for this magic: y = 1 + z, exactly, and z
113  * approx x, thus log(y)/z (which is nearly constant near z = 0) returns
114  * a good approximation to the true log(1 + x)/x. The multiplication x *
115  * (log(y)/z) introduces little additional error. */
116  return z == 0 ? x : x * log(y) / z;
117 }
118 
119 static real atanhx(real x) {
120  real y = fabs(x); /* Enforce odd parity */
121  y = log1px(2 * y/(1 - y))/2;
122  return x < 0 ? -y : y;
123 }
124 
125 static real copysignx(real x, real y) {
126  return fabs(x) * (y < 0 || (y == 0 && 1/y < 0) ? -1 : 1);
127 }
128 
129 static real hypotx(real x, real y)
130 { return sqrt(x * x + y * y); }
131 
132 static real cbrtx(real x) {
133  real y = pow(fabs(x), 1/(real)(3)); /* Return the real cube root */
134  return x < 0 ? -y : y;
135 }
136 
137 static real sumx(real u, real v, real* t) {
138  volatile real s = u + v;
139  volatile real up = s - v;
140  volatile real vpp = s - up;
141  up -= u;
142  vpp -= v;
143  if (t) *t = -(up + vpp);
144  /* error-free sum:
145  * u + v = s + t
146  * = round(u + v) + t */
147  return s;
148 }
149 
150 static real polyval(int N, const real p[], real x) {
151  real y = N < 0 ? 0 : *p++;
152  while (--N >= 0) y = y * x + *p++;
153  return y;
154 }
155 
156 /* mimic C++ std::min and std::max */
157 static real minx(real a, real b)
158 { return (b < a) ? b : a; }
159 
160 static real maxx(real a, real b)
161 { return (a < b) ? b : a; }
162 
163 static void swapx(real* x, real* y)
164 { real t = *x; *x = *y; *y = t; }
165 
166 static void norm2(real* sinx, real* cosx) {
167  real r = hypotx(*sinx, *cosx);
168  *sinx /= r;
169  *cosx /= r;
170 }
171 
172 static real AngNormalize(real x) {
173  x = fmod(x, (real)(360));
174  return x <= -180 ? x + 360 : (x <= 180 ? x : x - 360);
175 }
176 
177 static real LatFix(real x)
178 { return fabs(x) > 90 ? NaN : x; }
179 
180 static real AngDiff(real x, real y, real* e) {
181  real t, d = AngNormalize(sumx(AngNormalize(-x), AngNormalize(y), &t));
182  /* Here y - x = d + t (mod 360), exactly, where d is in (-180,180] and
183  * abs(t) <= eps (eps = 2^-45 for doubles). The only case where the
184  * addition of t takes the result outside the range (-180,180] is d = 180
185  * and t > 0. The case, d = -180 + eps, t = -eps, can't happen, since
186  * sum would have returned the exact result in such a case (i.e., given t
187  * = 0). */
188  return sumx(d == 180 && t > 0 ? -180 : d, t, e);
189 }
190 
191 static real AngRound(real x) {
192  const real z = 1/(real)(16);
193  volatile real y;
194  if (x == 0) return 0;
195  y = fabs(x);
196  /* The compiler mustn't "simplify" z - (z - y) to y */
197  y = y < z ? z - (z - y) : y;
198  return x < 0 ? -y : y;
199 }
200 
201 static void sincosdx(real x, real* sinx, real* cosx) {
202  /* In order to minimize round-off errors, this function exactly reduces
203  * the argument to the range [-45, 45] before converting it to radians. */
204  real r, s, c; int q;
205  r = fmod(x, (real)(360));
206  q = (int)(floor(r / 90 + (real)(0.5)));
207  r -= 90 * q;
208  /* now abs(r) <= 45 */
209  r *= degree;
210  /* Possibly could call the gnu extension sincos */
211  s = sin(r); c = cos(r);
212  switch ((unsigned)q & 3U) {
213  case 0U: *sinx = s; *cosx = c; break;
214  case 1U: *sinx = c; *cosx = -s; break;
215  case 2U: *sinx = -s; *cosx = -c; break;
216  default: *sinx = -c; *cosx = s; break; /* case 3U */
217  }
218  if (x) { *sinx += (real)(0); *cosx += (real)(0); }
219 }
220 
221 static real atan2dx(real y, real x) {
222  /* In order to minimize round-off errors, this function rearranges the
223  * arguments so that result of atan2 is in the range [-pi/4, pi/4] before
224  * converting it to degrees and mapping the result to the correct
225  * quadrant. */
226  int q = 0; real ang;
227  if (fabs(y) > fabs(x)) { swapx(&x, &y); q = 2; }
228  if (x < 0) { x = -x; ++q; }
229  /* here x >= 0 and x >= abs(y), so angle is in [-pi/4, pi/4] */
230  ang = atan2(y, x) / degree;
231  switch (q) {
232  /* Note that atan2d(-0.0, 1.0) will return -0. However, we expect that
233  * atan2d will not be called with y = -0. If need be, include
234  *
235  * case 0: ang = 0 + ang; break;
236  */
237  case 1: ang = (y >= 0 ? 180 : -180) - ang; break;
238  case 2: ang = 90 - ang; break;
239  case 3: ang = -90 + ang; break;
240  }
241  return ang;
242 }
243 
244 static void A3coeff(struct geod_geodesic* g);
245 static void C3coeff(struct geod_geodesic* g);
246 static void C4coeff(struct geod_geodesic* g);
247 static real SinCosSeries(boolx sinp,
248  real sinx, real cosx,
249  const real c[], int n);
250 static void Lengths(const struct geod_geodesic* g,
251  real eps, real sig12,
252  real ssig1, real csig1, real dn1,
253  real ssig2, real csig2, real dn2,
254  real cbet1, real cbet2,
255  real* ps12b, real* pm12b, real* pm0,
256  real* pM12, real* pM21,
257  /* Scratch area of the right size */
258  real Ca[]);
259 static real Astroid(real x, real y);
260 static real InverseStart(const struct geod_geodesic* g,
261  real sbet1, real cbet1, real dn1,
262  real sbet2, real cbet2, real dn2,
263  real lam12, real slam12, real clam12,
264  real* psalp1, real* pcalp1,
265  /* Only updated if return val >= 0 */
266  real* psalp2, real* pcalp2,
267  /* Only updated for short lines */
268  real* pdnm,
269  /* Scratch area of the right size */
270  real Ca[]);
271 static real Lambda12(const struct geod_geodesic* g,
272  real sbet1, real cbet1, real dn1,
273  real sbet2, real cbet2, real dn2,
274  real salp1, real calp1,
275  real slam120, real clam120,
276  real* psalp2, real* pcalp2,
277  real* psig12,
278  real* pssig1, real* pcsig1,
279  real* pssig2, real* pcsig2,
280  real* peps,
281  real* pgomg12,
282  boolx diffp, real* pdlam12,
283  /* Scratch area of the right size */
284  real Ca[]);
285 static real A3f(const struct geod_geodesic* g, real eps);
286 static void C3f(const struct geod_geodesic* g, real eps, real c[]);
287 static void C4f(const struct geod_geodesic* g, real eps, real c[]);
288 static real A1m1f(real eps);
289 static void C1f(real eps, real c[]);
290 static void C1pf(real eps, real c[]);
291 static real A2m1f(real eps);
292 static void C2f(real eps, real c[]);
293 static int transit(real lon1, real lon2);
294 static int transitdirect(real lon1, real lon2);
295 static void accini(real s[]);
296 static void acccopy(const real s[], real t[]);
297 static void accadd(real s[], real y);
298 static real accsum(const real s[], real y);
299 static void accneg(real s[]);
300 
301 void geod_init(struct geod_geodesic* g, real a, real f) {
302  if (!init) Init();
303  g->a = a;
304  g->f = f;
305  g->f1 = 1 - g->f;
306  g->e2 = g->f * (2 - g->f);
307  g->ep2 = g->e2 / sq(g->f1); /* e2 / (1 - e2) */
308  g->n = g->f / ( 2 - g->f);
309  g->b = g->a * g->f1;
310  g->c2 = (sq(g->a) + sq(g->b) *
311  (g->e2 == 0 ? 1 :
312  (g->e2 > 0 ? atanhx(sqrt(g->e2)) : atan(sqrt(-g->e2))) /
313  sqrt(fabs(g->e2))))/2; /* authalic radius squared */
314  /* The sig12 threshold for "really short". Using the auxiliary sphere
315  * solution with dnm computed at (bet1 + bet2) / 2, the relative error in the
316  * azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2. (Error
317  * measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a given f and
318  * sig12, the max error occurs for lines near the pole. If the old rule for
319  * computing dnm = (dn1 + dn2)/2 is used, then the error increases by a
320  * factor of 2.) Setting this equal to epsilon gives sig12 = etol2. Here
321  * 0.1 is a safety factor (error decreased by 100) and max(0.001, abs(f))
322  * stops etol2 getting too large in the nearly spherical case. */
323  g->etol2 = 0.1 * tol2 /
324  sqrt( maxx((real)(0.001), fabs(g->f)) * minx((real)(1), 1 - g->f/2) / 2 );
325 
326  A3coeff(g);
327  C3coeff(g);
328  C4coeff(g);
329 }
330 
331 static void geod_lineinit_int(struct geod_geodesicline* l,
332  const struct geod_geodesic* g,
333  real lat1, real lon1,
334  real azi1, real salp1, real calp1,
335  unsigned caps) {
336  real cbet1, sbet1, eps;
337  l->a = g->a;
338  l->f = g->f;
339  l->b = g->b;
340  l->c2 = g->c2;
341  l->f1 = g->f1;
342  /* If caps is 0 assume the standard direct calculation */
343  l->caps = (caps ? caps : GEOD_DISTANCE_IN | GEOD_LONGITUDE) |
344  /* always allow latitude and azimuth and unrolling of longitude */
346 
347  l->lat1 = LatFix(lat1);
348  l->lon1 = lon1;
349  l->azi1 = azi1;
350  l->salp1 = salp1;
351  l->calp1 = calp1;
352 
353  sincosdx(AngRound(l->lat1), &sbet1, &cbet1); sbet1 *= l->f1;
354  /* Ensure cbet1 = +epsilon at poles */
355  norm2(&sbet1, &cbet1); cbet1 = maxx(tiny, cbet1);
356  l->dn1 = sqrt(1 + g->ep2 * sq(sbet1));
357 
358  /* Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0), */
359  l->salp0 = l->salp1 * cbet1; /* alp0 in [0, pi/2 - |bet1|] */
360  /* Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
361  * is slightly better (consider the case salp1 = 0). */
362  l->calp0 = hypotx(l->calp1, l->salp1 * sbet1);
363  /* Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
364  * sig = 0 is nearest northward crossing of equator.
365  * With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
366  * With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
367  * With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
368  * Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
369  * With alp0 in (0, pi/2], quadrants for sig and omg coincide.
370  * No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
371  * With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi. */
372  l->ssig1 = sbet1; l->somg1 = l->salp0 * sbet1;
373  l->csig1 = l->comg1 = sbet1 != 0 || l->calp1 != 0 ? cbet1 * l->calp1 : 1;
374  norm2(&l->ssig1, &l->csig1); /* sig1 in (-pi, pi] */
375  /* norm2(somg1, comg1); -- don't need to normalize! */
376 
377  l->k2 = sq(l->calp0) * g->ep2;
378  eps = l->k2 / (2 * (1 + sqrt(1 + l->k2)) + l->k2);
379 
380  if (l->caps & CAP_C1) {
381  real s, c;
382  l->A1m1 = A1m1f(eps);
383  C1f(eps, l->C1a);
384  l->B11 = SinCosSeries(TRUE, l->ssig1, l->csig1, l->C1a, nC1);
385  s = sin(l->B11); c = cos(l->B11);
386  /* tau1 = sig1 + B11 */
387  l->stau1 = l->ssig1 * c + l->csig1 * s;
388  l->ctau1 = l->csig1 * c - l->ssig1 * s;
389  /* Not necessary because C1pa reverts C1a
390  * B11 = -SinCosSeries(TRUE, stau1, ctau1, C1pa, nC1p); */
391  }
392 
393  if (l->caps & CAP_C1p)
394  C1pf(eps, l->C1pa);
395 
396  if (l->caps & CAP_C2) {
397  l->A2m1 = A2m1f(eps);
398  C2f(eps, l->C2a);
399  l->B21 = SinCosSeries(TRUE, l->ssig1, l->csig1, l->C2a, nC2);
400  }
401 
402  if (l->caps & CAP_C3) {
403  C3f(g, eps, l->C3a);
404  l->A3c = -l->f * l->salp0 * A3f(g, eps);
405  l->B31 = SinCosSeries(TRUE, l->ssig1, l->csig1, l->C3a, nC3-1);
406  }
407 
408  if (l->caps & CAP_C4) {
409  C4f(g, eps, l->C4a);
410  /* Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0) */
411  l->A4 = sq(l->a) * l->calp0 * l->salp0 * g->e2;
412  l->B41 = SinCosSeries(FALSE, l->ssig1, l->csig1, l->C4a, nC4);
413  }
414 
415  l->a13 = l->s13 = NaN;
416 }
417 
418 void geod_lineinit(struct geod_geodesicline* l,
419  const struct geod_geodesic* g,
420  real lat1, real lon1, real azi1, unsigned caps) {
421  real salp1, calp1;
422  azi1 = AngNormalize(azi1);
423  /* Guard against underflow in salp0 */
424  sincosdx(AngRound(azi1), &salp1, &calp1);
425  geod_lineinit_int(l, g, lat1, lon1, azi1, salp1, calp1, caps);
426 }
427 
429  const struct geod_geodesic* g,
430  real lat1, real lon1, real azi1,
431  unsigned flags, real a12_s12,
432  unsigned caps) {
433  geod_lineinit(l, g, lat1, lon1, azi1, caps);
434  geod_gensetdistance(l, flags, a12_s12);
435 }
436 
437 void geod_directline(struct geod_geodesicline* l,
438  const struct geod_geodesic* g,
439  real lat1, real lon1, real azi1,
440  real s12, unsigned caps) {
441  geod_gendirectline(l, g, lat1, lon1, azi1, GEOD_NOFLAGS, s12, caps);
442 }
443 
444 real geod_genposition(const struct geod_geodesicline* l,
445  unsigned flags, real s12_a12,
446  real* plat2, real* plon2, real* pazi2,
447  real* ps12, real* pm12,
448  real* pM12, real* pM21,
449  real* pS12) {
450  real lat2 = 0, lon2 = 0, azi2 = 0, s12 = 0,
451  m12 = 0, M12 = 0, M21 = 0, S12 = 0;
452  /* Avoid warning about uninitialized B12. */
453  real sig12, ssig12, csig12, B12 = 0, AB1 = 0;
454  real omg12, lam12, lon12;
455  real ssig2, csig2, sbet2, cbet2, somg2, comg2, salp2, calp2, dn2;
456  unsigned outmask =
457  (plat2 ? GEOD_LATITUDE : 0U) |
458  (plon2 ? GEOD_LONGITUDE : 0U) |
459  (pazi2 ? GEOD_AZIMUTH : 0U) |
460  (ps12 ? GEOD_DISTANCE : 0U) |
461  (pm12 ? GEOD_REDUCEDLENGTH : 0U) |
462  (pM12 || pM21 ? GEOD_GEODESICSCALE : 0U) |
463  (pS12 ? GEOD_AREA : 0U);
464 
465  outmask &= l->caps & OUT_ALL;
466  if (!( TRUE /*Init()*/ &&
467  (flags & GEOD_ARCMODE || (l->caps & (GEOD_DISTANCE_IN & OUT_ALL))) ))
468  /* Uninitialized or impossible distance calculation requested */
469  return NaN;
470 
471  if (flags & GEOD_ARCMODE) {
472  /* Interpret s12_a12 as spherical arc length */
473  sig12 = s12_a12 * degree;
474  sincosdx(s12_a12, &ssig12, &csig12);
475  } else {
476  /* Interpret s12_a12 as distance */
477  real
478  tau12 = s12_a12 / (l->b * (1 + l->A1m1)),
479  s = sin(tau12),
480  c = cos(tau12);
481  /* tau2 = tau1 + tau12 */
482  B12 = - SinCosSeries(TRUE,
483  l->stau1 * c + l->ctau1 * s,
484  l->ctau1 * c - l->stau1 * s,
485  l->C1pa, nC1p);
486  sig12 = tau12 - (B12 - l->B11);
487  ssig12 = sin(sig12); csig12 = cos(sig12);
488  if (fabs(l->f) > 0.01) {
489  /* Reverted distance series is inaccurate for |f| > 1/100, so correct
490  * sig12 with 1 Newton iteration. The following table shows the
491  * approximate maximum error for a = WGS_a() and various f relative to
492  * GeodesicExact.
493  * erri = the error in the inverse solution (nm)
494  * errd = the error in the direct solution (series only) (nm)
495  * errda = the error in the direct solution (series + 1 Newton) (nm)
496  *
497  * f erri errd errda
498  * -1/5 12e6 1.2e9 69e6
499  * -1/10 123e3 12e6 765e3
500  * -1/20 1110 108e3 7155
501  * -1/50 18.63 200.9 27.12
502  * -1/100 18.63 23.78 23.37
503  * -1/150 18.63 21.05 20.26
504  * 1/150 22.35 24.73 25.83
505  * 1/100 22.35 25.03 25.31
506  * 1/50 29.80 231.9 30.44
507  * 1/20 5376 146e3 10e3
508  * 1/10 829e3 22e6 1.5e6
509  * 1/5 157e6 3.8e9 280e6 */
510  real serr;
511  ssig2 = l->ssig1 * csig12 + l->csig1 * ssig12;
512  csig2 = l->csig1 * csig12 - l->ssig1 * ssig12;
513  B12 = SinCosSeries(TRUE, ssig2, csig2, l->C1a, nC1);
514  serr = (1 + l->A1m1) * (sig12 + (B12 - l->B11)) - s12_a12 / l->b;
515  sig12 = sig12 - serr / sqrt(1 + l->k2 * sq(ssig2));
516  ssig12 = sin(sig12); csig12 = cos(sig12);
517  /* Update B12 below */
518  }
519  }
520 
521  /* sig2 = sig1 + sig12 */
522  ssig2 = l->ssig1 * csig12 + l->csig1 * ssig12;
523  csig2 = l->csig1 * csig12 - l->ssig1 * ssig12;
524  dn2 = sqrt(1 + l->k2 * sq(ssig2));
526  if (flags & GEOD_ARCMODE || fabs(l->f) > 0.01)
527  B12 = SinCosSeries(TRUE, ssig2, csig2, l->C1a, nC1);
528  AB1 = (1 + l->A1m1) * (B12 - l->B11);
529  }
530  /* sin(bet2) = cos(alp0) * sin(sig2) */
531  sbet2 = l->calp0 * ssig2;
532  /* Alt: cbet2 = hypot(csig2, salp0 * ssig2); */
533  cbet2 = hypotx(l->salp0, l->calp0 * csig2);
534  if (cbet2 == 0)
535  /* I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case */
536  cbet2 = csig2 = tiny;
537  /* tan(alp0) = cos(sig2)*tan(alp2) */
538  salp2 = l->salp0; calp2 = l->calp0 * csig2; /* No need to normalize */
539 
540  if (outmask & GEOD_DISTANCE)
541  s12 = flags & GEOD_ARCMODE ? l->b * ((1 + l->A1m1) * sig12 + AB1) : s12_a12;
542 
543  if (outmask & GEOD_LONGITUDE) {
544  real E = copysignx(1, l->salp0); /* east or west going? */
545  /* tan(omg2) = sin(alp0) * tan(sig2) */
546  somg2 = l->salp0 * ssig2; comg2 = csig2; /* No need to normalize */
547  /* omg12 = omg2 - omg1 */
548  omg12 = flags & GEOD_LONG_UNROLL
549  ? E * (sig12
550  - (atan2( ssig2, csig2) - atan2( l->ssig1, l->csig1))
551  + (atan2(E * somg2, comg2) - atan2(E * l->somg1, l->comg1)))
552  : atan2(somg2 * l->comg1 - comg2 * l->somg1,
553  comg2 * l->comg1 + somg2 * l->somg1);
554  lam12 = omg12 + l->A3c *
555  ( sig12 + (SinCosSeries(TRUE, ssig2, csig2, l->C3a, nC3-1)
556  - l->B31));
557  lon12 = lam12 / degree;
558  lon2 = flags & GEOD_LONG_UNROLL ? l->lon1 + lon12 :
559  AngNormalize(AngNormalize(l->lon1) + AngNormalize(lon12));
560  }
561 
562  if (outmask & GEOD_LATITUDE)
563  lat2 = atan2dx(sbet2, l->f1 * cbet2);
564 
565  if (outmask & GEOD_AZIMUTH)
566  azi2 = atan2dx(salp2, calp2);
567 
568  if (outmask & (GEOD_REDUCEDLENGTH | GEOD_GEODESICSCALE)) {
569  real
570  B22 = SinCosSeries(TRUE, ssig2, csig2, l->C2a, nC2),
571  AB2 = (1 + l->A2m1) * (B22 - l->B21),
572  J12 = (l->A1m1 - l->A2m1) * sig12 + (AB1 - AB2);
573  if (outmask & GEOD_REDUCEDLENGTH)
574  /* Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure
575  * accurate cancellation in the case of coincident points. */
576  m12 = l->b * ((dn2 * (l->csig1 * ssig2) - l->dn1 * (l->ssig1 * csig2))
577  - l->csig1 * csig2 * J12);
578  if (outmask & GEOD_GEODESICSCALE) {
579  real t = l->k2 * (ssig2 - l->ssig1) * (ssig2 + l->ssig1) / (l->dn1 + dn2);
580  M12 = csig12 + (t * ssig2 - csig2 * J12) * l->ssig1 / l->dn1;
581  M21 = csig12 - (t * l->ssig1 - l->csig1 * J12) * ssig2 / dn2;
582  }
583  }
584 
585  if (outmask & GEOD_AREA) {
586  real
587  B42 = SinCosSeries(FALSE, ssig2, csig2, l->C4a, nC4);
588  real salp12, calp12;
589  if (l->calp0 == 0 || l->salp0 == 0) {
590  /* alp12 = alp2 - alp1, used in atan2 so no need to normalize */
591  salp12 = salp2 * l->calp1 - calp2 * l->salp1;
592  calp12 = calp2 * l->calp1 + salp2 * l->salp1;
593  } else {
594  /* tan(alp) = tan(alp0) * sec(sig)
595  * tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
596  * = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
597  * If csig12 > 0, write
598  * csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
599  * else
600  * csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
601  * No need to normalize */
602  salp12 = l->calp0 * l->salp0 *
603  (csig12 <= 0 ? l->csig1 * (1 - csig12) + ssig12 * l->ssig1 :
604  ssig12 * (l->csig1 * ssig12 / (1 + csig12) + l->ssig1));
605  calp12 = sq(l->salp0) + sq(l->calp0) * l->csig1 * csig2;
606  }
607  S12 = l->c2 * atan2(salp12, calp12) + l->A4 * (B42 - l->B41);
608  }
609 
610  if (outmask & GEOD_LATITUDE)
611  *plat2 = lat2;
612  if (outmask & GEOD_LONGITUDE)
613  *plon2 = lon2;
614  if (outmask & GEOD_AZIMUTH)
615  *pazi2 = azi2;
616  if (outmask & GEOD_DISTANCE)
617  *ps12 = s12;
618  if (outmask & GEOD_REDUCEDLENGTH)
619  *pm12 = m12;
620  if (outmask & GEOD_GEODESICSCALE) {
621  if (pM12) *pM12 = M12;
622  if (pM21) *pM21 = M21;
623  }
624  if (outmask & GEOD_AREA)
625  *pS12 = S12;
626 
627  return flags & GEOD_ARCMODE ? s12_a12 : sig12 / degree;
628 }
629 
630 void geod_setdistance(struct geod_geodesicline* l, real s13) {
631  l->s13 = s13;
632  l->a13 = geod_genposition(l, GEOD_NOFLAGS, l->s13, 0, 0, 0, 0, 0, 0, 0, 0);
633 }
634 
635 static void geod_setarc(struct geod_geodesicline* l, real a13) {
636  l->a13 = a13; l->s13 = NaN;
637  geod_genposition(l, GEOD_ARCMODE, l->a13, 0, 0, 0, &l->s13, 0, 0, 0, 0);
638 }
639 
641  unsigned flags, real s13_a13) {
642  flags & GEOD_ARCMODE ? geod_setarc(l, s13_a13) : geod_setdistance(l, s13_a13);
643 }
644 
645 void geod_position(const struct geod_geodesicline* l, real s12,
646  real* plat2, real* plon2, real* pazi2) {
647  geod_genposition(l, FALSE, s12, plat2, plon2, pazi2, 0, 0, 0, 0, 0);
648 }
649 
650 real geod_gendirect(const struct geod_geodesic* g,
651  real lat1, real lon1, real azi1,
652  unsigned flags, real s12_a12,
653  real* plat2, real* plon2, real* pazi2,
654  real* ps12, real* pm12, real* pM12, real* pM21,
655  real* pS12) {
656  struct geod_geodesicline l;
657  unsigned outmask =
658  (plat2 ? GEOD_LATITUDE : 0U) |
659  (plon2 ? GEOD_LONGITUDE : 0U) |
660  (pazi2 ? GEOD_AZIMUTH : 0U) |
661  (ps12 ? GEOD_DISTANCE : 0U) |
662  (pm12 ? GEOD_REDUCEDLENGTH : 0U) |
663  (pM12 || pM21 ? GEOD_GEODESICSCALE : 0U) |
664  (pS12 ? GEOD_AREA : 0U);
665 
666  geod_lineinit(&l, g, lat1, lon1, azi1,
667  /* Automatically supply GEOD_DISTANCE_IN if necessary */
668  outmask |
669  (flags & GEOD_ARCMODE ? GEOD_NONE : GEOD_DISTANCE_IN));
670  return geod_genposition(&l, flags, s12_a12,
671  plat2, plon2, pazi2, ps12, pm12, pM12, pM21, pS12);
672 }
673 
674 void geod_direct(const struct geod_geodesic* g,
675  real lat1, real lon1, real azi1,
676  real s12,
677  real* plat2, real* plon2, real* pazi2) {
678  geod_gendirect(g, lat1, lon1, azi1, GEOD_NOFLAGS, s12, plat2, plon2, pazi2,
679  0, 0, 0, 0, 0);
680 }
681 
682 static real geod_geninverse_int(const struct geod_geodesic* g,
683  real lat1, real lon1, real lat2, real lon2,
684  real* ps12,
685  real* psalp1, real* pcalp1,
686  real* psalp2, real* pcalp2,
687  real* pm12, real* pM12, real* pM21,
688  real* pS12) {
689  real s12 = 0, m12 = 0, M12 = 0, M21 = 0, S12 = 0;
690  real lon12, lon12s;
691  int latsign, lonsign, swapp;
692  real sbet1, cbet1, sbet2, cbet2, s12x = 0, m12x = 0;
693  real dn1, dn2, lam12, slam12, clam12;
694  real a12 = 0, sig12, calp1 = 0, salp1 = 0, calp2 = 0, salp2 = 0;
695  real Ca[nC];
696  boolx meridian;
697  /* somg12 > 1 marks that it needs to be calculated */
698  real omg12 = 0, somg12 = 2, comg12 = 0;
699 
700  unsigned outmask =
701  (ps12 ? GEOD_DISTANCE : 0U) |
702  (pm12 ? GEOD_REDUCEDLENGTH : 0U) |
703  (pM12 || pM21 ? GEOD_GEODESICSCALE : 0U) |
704  (pS12 ? GEOD_AREA : 0U);
705 
706  outmask &= OUT_ALL;
707  /* Compute longitude difference (AngDiff does this carefully). Result is
708  * in [-180, 180] but -180 is only for west-going geodesics. 180 is for
709  * east-going and meridional geodesics. */
710  lon12 = AngDiff(lon1, lon2, &lon12s);
711  /* Make longitude difference positive. */
712  lonsign = lon12 >= 0 ? 1 : -1;
713  /* If very close to being on the same half-meridian, then make it so. */
714  lon12 = lonsign * AngRound(lon12);
715  lon12s = AngRound((180 - lon12) - lonsign * lon12s);
716  lam12 = lon12 * degree;
717  if (lon12 > 90) {
718  sincosdx(lon12s, &slam12, &clam12);
719  clam12 = -clam12;
720  } else
721  sincosdx(lon12, &slam12, &clam12);
722 
723  /* If really close to the equator, treat as on equator. */
724  lat1 = AngRound(LatFix(lat1));
725  lat2 = AngRound(LatFix(lat2));
726  /* Swap points so that point with higher (abs) latitude is point 1
727  * If one latitude is a nan, then it becomes lat1. */
728  swapp = fabs(lat1) < fabs(lat2) ? -1 : 1;
729  if (swapp < 0) {
730  lonsign *= -1;
731  swapx(&lat1, &lat2);
732  }
733  /* Make lat1 <= 0 */
734  latsign = lat1 < 0 ? 1 : -1;
735  lat1 *= latsign;
736  lat2 *= latsign;
737  /* Now we have
738  *
739  * 0 <= lon12 <= 180
740  * -90 <= lat1 <= 0
741  * lat1 <= lat2 <= -lat1
742  *
743  * longsign, swapp, latsign register the transformation to bring the
744  * coordinates to this canonical form. In all cases, 1 means no change was
745  * made. We make these transformations so that there are few cases to
746  * check, e.g., on verifying quadrants in atan2. In addition, this
747  * enforces some symmetries in the results returned. */
748 
749  sincosdx(lat1, &sbet1, &cbet1); sbet1 *= g->f1;
750  /* Ensure cbet1 = +epsilon at poles */
751  norm2(&sbet1, &cbet1); cbet1 = maxx(tiny, cbet1);
752 
753  sincosdx(lat2, &sbet2, &cbet2); sbet2 *= g->f1;
754  /* Ensure cbet2 = +epsilon at poles */
755  norm2(&sbet2, &cbet2); cbet2 = maxx(tiny, cbet2);
756 
757  /* If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
758  * |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
759  * a better measure. This logic is used in assigning calp2 in Lambda12.
760  * Sometimes these quantities vanish and in that case we force bet2 = +/-
761  * bet1 exactly. An example where is is necessary is the inverse problem
762  * 48.522876735459 0 -48.52287673545898293 179.599720456223079643
763  * which failed with Visual Studio 10 (Release and Debug) */
764 
765  if (cbet1 < -sbet1) {
766  if (cbet2 == cbet1)
767  sbet2 = sbet2 < 0 ? sbet1 : -sbet1;
768  } else {
769  if (fabs(sbet2) == -sbet1)
770  cbet2 = cbet1;
771  }
772 
773  dn1 = sqrt(1 + g->ep2 * sq(sbet1));
774  dn2 = sqrt(1 + g->ep2 * sq(sbet2));
775 
776  meridian = lat1 == -90 || slam12 == 0;
777 
778  if (meridian) {
779 
780  /* Endpoints are on a single full meridian, so the geodesic might lie on
781  * a meridian. */
782 
783  real ssig1, csig1, ssig2, csig2;
784  calp1 = clam12; salp1 = slam12; /* Head to the target longitude */
785  calp2 = 1; salp2 = 0; /* At the target we're heading north */
786 
787  /* tan(bet) = tan(sig) * cos(alp) */
788  ssig1 = sbet1; csig1 = calp1 * cbet1;
789  ssig2 = sbet2; csig2 = calp2 * cbet2;
790 
791  /* sig12 = sig2 - sig1 */
792  sig12 = atan2(maxx((real)(0), csig1 * ssig2 - ssig1 * csig2),
793  csig1 * csig2 + ssig1 * ssig2);
794  Lengths(g, g->n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
795  cbet1, cbet2, &s12x, &m12x, 0,
796  outmask & GEOD_GEODESICSCALE ? &M12 : 0,
797  outmask & GEOD_GEODESICSCALE ? &M21 : 0,
798  Ca);
799  /* Add the check for sig12 since zero length geodesics might yield m12 <
800  * 0. Test case was
801  *
802  * echo 20.001 0 20.001 0 | GeodSolve -i
803  *
804  * In fact, we will have sig12 > pi/2 for meridional geodesic which is
805  * not a shortest path. */
806  if (sig12 < 1 || m12x >= 0) {
807  /* Need at least 2, to handle 90 0 90 180 */
808  if (sig12 < 3 * tiny)
809  sig12 = m12x = s12x = 0;
810  m12x *= g->b;
811  s12x *= g->b;
812  a12 = sig12 / degree;
813  } else
814  /* m12 < 0, i.e., prolate and too close to anti-podal */
815  meridian = FALSE;
816  }
817 
818  if (!meridian &&
819  sbet1 == 0 && /* and sbet2 == 0 */
820  /* Mimic the way Lambda12 works with calp1 = 0 */
821  (g->f <= 0 || lon12s >= g->f * 180)) {
822 
823  /* Geodesic runs along equator */
824  calp1 = calp2 = 0; salp1 = salp2 = 1;
825  s12x = g->a * lam12;
826  sig12 = omg12 = lam12 / g->f1;
827  m12x = g->b * sin(sig12);
828  if (outmask & GEOD_GEODESICSCALE)
829  M12 = M21 = cos(sig12);
830  a12 = lon12 / g->f1;
831 
832  } else if (!meridian) {
833 
834  /* Now point1 and point2 belong within a hemisphere bounded by a
835  * meridian and geodesic is neither meridional or equatorial. */
836 
837  /* Figure a starting point for Newton's method */
838  real dnm = 0;
839  sig12 = InverseStart(g, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
840  lam12, slam12, clam12,
841  &salp1, &calp1, &salp2, &calp2, &dnm,
842  Ca);
843 
844  if (sig12 >= 0) {
845  /* Short lines (InverseStart sets salp2, calp2, dnm) */
846  s12x = sig12 * g->b * dnm;
847  m12x = sq(dnm) * g->b * sin(sig12 / dnm);
848  if (outmask & GEOD_GEODESICSCALE)
849  M12 = M21 = cos(sig12 / dnm);
850  a12 = sig12 / degree;
851  omg12 = lam12 / (g->f1 * dnm);
852  } else {
853 
854  /* Newton's method. This is a straightforward solution of f(alp1) =
855  * lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
856  * root in the interval (0, pi) and its derivative is positive at the
857  * root. Thus f(alp) is positive for alp > alp1 and negative for alp <
858  * alp1. During the course of the iteration, a range (alp1a, alp1b) is
859  * maintained which brackets the root and with each evaluation of
860  * f(alp) the range is shrunk, if possible. Newton's method is
861  * restarted whenever the derivative of f is negative (because the new
862  * value of alp1 is then further from the solution) or if the new
863  * estimate of alp1 lies outside (0,pi); in this case, the new starting
864  * guess is taken to be (alp1a + alp1b) / 2. */
865  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, eps = 0, domg12 = 0;
866  unsigned numit = 0;
867  /* Bracketing range */
868  real salp1a = tiny, calp1a = 1, salp1b = tiny, calp1b = -1;
869  boolx tripn, tripb;
870  for (tripn = FALSE, tripb = FALSE; numit < maxit2; ++numit) {
871  /* the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
872  * WGS84 and random input: mean = 2.85, sd = 0.60 */
873  real dv = 0,
874  v = Lambda12(g, sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
875  slam12, clam12,
876  &salp2, &calp2, &sig12, &ssig1, &csig1, &ssig2, &csig2,
877  &eps, &domg12, numit < maxit1, &dv, Ca);
878  /* 2 * tol0 is approximately 1 ulp for a number in [0, pi]. */
879  /* Reversed test to allow escape with NaNs */
880  if (tripb || !(fabs(v) >= (tripn ? 8 : 1) * tol0)) break;
881  /* Update bracketing values */
882  if (v > 0 && (numit > maxit1 || calp1/salp1 > calp1b/salp1b))
883  { salp1b = salp1; calp1b = calp1; }
884  else if (v < 0 && (numit > maxit1 || calp1/salp1 < calp1a/salp1a))
885  { salp1a = salp1; calp1a = calp1; }
886  if (numit < maxit1 && dv > 0) {
887  real
888  dalp1 = -v/dv;
889  real
890  sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
891  nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
892  if (nsalp1 > 0 && fabs(dalp1) < pi) {
893  calp1 = calp1 * cdalp1 - salp1 * sdalp1;
894  salp1 = nsalp1;
895  norm2(&salp1, &calp1);
896  /* In some regimes we don't get quadratic convergence because
897  * slope -> 0. So use convergence conditions based on epsilon
898  * instead of sqrt(epsilon). */
899  tripn = fabs(v) <= 16 * tol0;
900  continue;
901  }
902  }
903  /* Either dv was not positive or updated value was outside legal
904  * range. Use the midpoint of the bracket as the next estimate.
905  * This mechanism is not needed for the WGS84 ellipsoid, but it does
906  * catch problems with more eccentric ellipsoids. Its efficacy is
907  * such for the WGS84 test set with the starting guess set to alp1 =
908  * 90deg:
909  * the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
910  * WGS84 and random input: mean = 4.74, sd = 0.99 */
911  salp1 = (salp1a + salp1b)/2;
912  calp1 = (calp1a + calp1b)/2;
913  norm2(&salp1, &calp1);
914  tripn = FALSE;
915  tripb = (fabs(salp1a - salp1) + (calp1a - calp1) < tolb ||
916  fabs(salp1 - salp1b) + (calp1 - calp1b) < tolb);
917  }
918  Lengths(g, eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
919  cbet1, cbet2, &s12x, &m12x, 0,
920  outmask & GEOD_GEODESICSCALE ? &M12 : 0,
921  outmask & GEOD_GEODESICSCALE ? &M21 : 0, Ca);
922  m12x *= g->b;
923  s12x *= g->b;
924  a12 = sig12 / degree;
925  if (outmask & GEOD_AREA) {
926  /* omg12 = lam12 - domg12 */
927  real sdomg12 = sin(domg12), cdomg12 = cos(domg12);
928  somg12 = slam12 * cdomg12 - clam12 * sdomg12;
929  comg12 = clam12 * cdomg12 + slam12 * sdomg12;
930  }
931  }
932  }
933 
934  if (outmask & GEOD_DISTANCE)
935  s12 = 0 + s12x; /* Convert -0 to 0 */
936 
937  if (outmask & GEOD_REDUCEDLENGTH)
938  m12 = 0 + m12x; /* Convert -0 to 0 */
939 
940  if (outmask & GEOD_AREA) {
941  real
942  /* From Lambda12: sin(alp1) * cos(bet1) = sin(alp0) */
943  salp0 = salp1 * cbet1,
944  calp0 = hypotx(calp1, salp1 * sbet1); /* calp0 > 0 */
945  real alp12;
946  if (calp0 != 0 && salp0 != 0) {
947  real
948  /* From Lambda12: tan(bet) = tan(sig) * cos(alp) */
949  ssig1 = sbet1, csig1 = calp1 * cbet1,
950  ssig2 = sbet2, csig2 = calp2 * cbet2,
951  k2 = sq(calp0) * g->ep2,
952  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2),
953  /* Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0). */
954  A4 = sq(g->a) * calp0 * salp0 * g->e2;
955  real B41, B42;
956  norm2(&ssig1, &csig1);
957  norm2(&ssig2, &csig2);
958  C4f(g, eps, Ca);
959  B41 = SinCosSeries(FALSE, ssig1, csig1, Ca, nC4);
960  B42 = SinCosSeries(FALSE, ssig2, csig2, Ca, nC4);
961  S12 = A4 * (B42 - B41);
962  } else
963  /* Avoid problems with indeterminate sig1, sig2 on equator */
964  S12 = 0;
965 
966  if (!meridian && somg12 > 1) {
967  somg12 = sin(omg12); comg12 = cos(omg12);
968  }
969 
970  if (!meridian &&
971  /* omg12 < 3/4 * pi */
972  comg12 > -(real)(0.7071) && /* Long difference not too big */
973  sbet2 - sbet1 < (real)(1.75)) { /* Lat difference not too big */
974  /* Use tan(Gamma/2) = tan(omg12/2)
975  * * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
976  * with tan(x/2) = sin(x)/(1+cos(x)) */
977  real
978  domg12 = 1 + comg12, dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
979  alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
980  domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
981  } else {
982  /* alp12 = alp2 - alp1, used in atan2 so no need to normalize */
983  real
984  salp12 = salp2 * calp1 - calp2 * salp1,
985  calp12 = calp2 * calp1 + salp2 * salp1;
986  /* The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
987  * salp12 = -0 and alp12 = -180. However this depends on the sign
988  * being attached to 0 correctly. The following ensures the correct
989  * behavior. */
990  if (salp12 == 0 && calp12 < 0) {
991  salp12 = tiny * calp1;
992  calp12 = -1;
993  }
994  alp12 = atan2(salp12, calp12);
995  }
996  S12 += g->c2 * alp12;
997  S12 *= swapp * lonsign * latsign;
998  /* Convert -0 to 0 */
999  S12 += 0;
1000  }
1001 
1002  /* Convert calp, salp to azimuth accounting for lonsign, swapp, latsign. */
1003  if (swapp < 0) {
1004  swapx(&salp1, &salp2);
1005  swapx(&calp1, &calp2);
1006  if (outmask & GEOD_GEODESICSCALE)
1007  swapx(&M12, &M21);
1008  }
1009 
1010  salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
1011  salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
1012 
1013  if (psalp1) *psalp1 = salp1;
1014  if (pcalp1) *pcalp1 = calp1;
1015  if (psalp2) *psalp2 = salp2;
1016  if (pcalp2) *pcalp2 = calp2;
1017 
1018  if (outmask & GEOD_DISTANCE)
1019  *ps12 = s12;
1020  if (outmask & GEOD_REDUCEDLENGTH)
1021  *pm12 = m12;
1022  if (outmask & GEOD_GEODESICSCALE) {
1023  if (pM12) *pM12 = M12;
1024  if (pM21) *pM21 = M21;
1025  }
1026  if (outmask & GEOD_AREA)
1027  *pS12 = S12;
1028 
1029  /* Returned value in [0, 180] */
1030  return a12;
1031 }
1032 
1033 real geod_geninverse(const struct geod_geodesic* g,
1034  real lat1, real lon1, real lat2, real lon2,
1035  real* ps12, real* pazi1, real* pazi2,
1036  real* pm12, real* pM12, real* pM21, real* pS12) {
1037  real salp1, calp1, salp2, calp2,
1038  a12 = geod_geninverse_int(g, lat1, lon1, lat2, lon2, ps12,
1039  &salp1, &calp1, &salp2, &calp2,
1040  pm12, pM12, pM21, pS12);
1041  if (pazi1) *pazi1 = atan2dx(salp1, calp1);
1042  if (pazi2) *pazi2 = atan2dx(salp2, calp2);
1043  return a12;
1044 }
1045 
1046 void geod_inverseline(struct geod_geodesicline* l,
1047  const struct geod_geodesic* g,
1048  real lat1, real lon1, real lat2, real lon2,
1049  unsigned caps) {
1050  real salp1, calp1,
1051  a12 = geod_geninverse_int(g, lat1, lon1, lat2, lon2, 0,
1052  &salp1, &calp1, 0, 0,
1053  0, 0, 0, 0),
1054  azi1 = atan2dx(salp1, calp1);
1055  caps = caps ? caps : GEOD_DISTANCE_IN | GEOD_LONGITUDE;
1056  /* Ensure that a12 can be converted to a distance */
1057  if (caps & (OUT_ALL & GEOD_DISTANCE_IN)) caps |= GEOD_DISTANCE;
1058  geod_lineinit_int(l, g, lat1, lon1, azi1, salp1, calp1, caps);
1059  geod_setarc(l, a12);
1060 }
1061 
1062 void geod_inverse(const struct geod_geodesic* g,
1063  real lat1, real lon1, real lat2, real lon2,
1064  real* ps12, real* pazi1, real* pazi2) {
1065  geod_geninverse(g, lat1, lon1, lat2, lon2, ps12, pazi1, pazi2, 0, 0, 0, 0);
1066 }
1067 
1068 real SinCosSeries(boolx sinp, real sinx, real cosx, const real c[], int n) {
1069  /* Evaluate
1070  * y = sinp ? sum(c[i] * sin( 2*i * x), i, 1, n) :
1071  * sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
1072  * using Clenshaw summation. N.B. c[0] is unused for sin series
1073  * Approx operation count = (n + 5) mult and (2 * n + 2) add */
1074  real ar, y0, y1;
1075  c += (n + sinp); /* Point to one beyond last element */
1076  ar = 2 * (cosx - sinx) * (cosx + sinx); /* 2 * cos(2 * x) */
1077  y0 = n & 1 ? *--c : 0; y1 = 0; /* accumulators for sum */
1078  /* Now n is even */
1079  n /= 2;
1080  while (n--) {
1081  /* Unroll loop x 2, so accumulators return to their original role */
1082  y1 = ar * y0 - y1 + *--c;
1083  y0 = ar * y1 - y0 + *--c;
1084  }
1085  return sinp
1086  ? 2 * sinx * cosx * y0 /* sin(2 * x) * y0 */
1087  : cosx * (y0 - y1); /* cos(x) * (y0 - y1) */
1088 }
1089 
1090 void Lengths(const struct geod_geodesic* g,
1091  real eps, real sig12,
1092  real ssig1, real csig1, real dn1,
1093  real ssig2, real csig2, real dn2,
1094  real cbet1, real cbet2,
1095  real* ps12b, real* pm12b, real* pm0,
1096  real* pM12, real* pM21,
1097  /* Scratch area of the right size */
1098  real Ca[]) {
1099  real m0 = 0, J12 = 0, A1 = 0, A2 = 0;
1100  real Cb[nC];
1101 
1102  /* Return m12b = (reduced length)/b; also calculate s12b = distance/b,
1103  * and m0 = coefficient of secular term in expression for reduced length. */
1104  boolx redlp = pm12b || pm0 || pM12 || pM21;
1105  if (ps12b || redlp) {
1106  A1 = A1m1f(eps);
1107  C1f(eps, Ca);
1108  if (redlp) {
1109  A2 = A2m1f(eps);
1110  C2f(eps, Cb);
1111  m0 = A1 - A2;
1112  A2 = 1 + A2;
1113  }
1114  A1 = 1 + A1;
1115  }
1116  if (ps12b) {
1117  real B1 = SinCosSeries(TRUE, ssig2, csig2, Ca, nC1) -
1118  SinCosSeries(TRUE, ssig1, csig1, Ca, nC1);
1119  /* Missing a factor of b */
1120  *ps12b = A1 * (sig12 + B1);
1121  if (redlp) {
1122  real B2 = SinCosSeries(TRUE, ssig2, csig2, Cb, nC2) -
1123  SinCosSeries(TRUE, ssig1, csig1, Cb, nC2);
1124  J12 = m0 * sig12 + (A1 * B1 - A2 * B2);
1125  }
1126  } else if (redlp) {
1127  /* Assume here that nC1 >= nC2 */
1128  int l;
1129  for (l = 1; l <= nC2; ++l)
1130  Cb[l] = A1 * Ca[l] - A2 * Cb[l];
1131  J12 = m0 * sig12 + (SinCosSeries(TRUE, ssig2, csig2, Cb, nC2) -
1132  SinCosSeries(TRUE, ssig1, csig1, Cb, nC2));
1133  }
1134  if (pm0) *pm0 = m0;
1135  if (pm12b)
1136  /* Missing a factor of b.
1137  * Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure
1138  * accurate cancellation in the case of coincident points. */
1139  *pm12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) -
1140  csig1 * csig2 * J12;
1141  if (pM12 || pM21) {
1142  real csig12 = csig1 * csig2 + ssig1 * ssig2;
1143  real t = g->ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
1144  if (pM12)
1145  *pM12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
1146  if (pM21)
1147  *pM21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
1148  }
1149 }
1150 
1151 real Astroid(real x, real y) {
1152  /* Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
1153  * This solution is adapted from Geocentric::Reverse. */
1154  real k;
1155  real
1156  p = sq(x),
1157  q = sq(y),
1158  r = (p + q - 1) / 6;
1159  if ( !(q == 0 && r <= 0) ) {
1160  real
1161  /* Avoid possible division by zero when r = 0 by multiplying equations
1162  * for s and t by r^3 and r, resp. */
1163  S = p * q / 4, /* S = r^3 * s */
1164  r2 = sq(r),
1165  r3 = r * r2,
1166  /* The discriminant of the quadratic equation for T3. This is zero on
1167  * the evolute curve p^(1/3)+q^(1/3) = 1 */
1168  disc = S * (S + 2 * r3);
1169  real u = r;
1170  real v, uv, w;
1171  if (disc >= 0) {
1172  real T3 = S + r3, T;
1173  /* Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
1174  * of precision due to cancellation. The result is unchanged because
1175  * of the way the T is used in definition of u. */
1176  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); /* T3 = (r * t)^3 */
1177  /* N.B. cbrtx always returns the real root. cbrtx(-8) = -2. */
1178  T = cbrtx(T3); /* T = r * t */
1179  /* T can be zero; but then r2 / T -> 0. */
1180  u += T + (T != 0 ? r2 / T : 0);
1181  } else {
1182  /* T is complex, but the way u is defined the result is real. */
1183  real ang = atan2(sqrt(-disc), -(S + r3));
1184  /* There are three possible cube roots. We choose the root which
1185  * avoids cancellation. Note that disc < 0 implies that r < 0. */
1186  u += 2 * r * cos(ang / 3);
1187  }
1188  v = sqrt(sq(u) + q); /* guaranteed positive */
1189  /* Avoid loss of accuracy when u < 0. */
1190  uv = u < 0 ? q / (v - u) : u + v; /* u+v, guaranteed positive */
1191  w = (uv - q) / (2 * v); /* positive? */
1192  /* Rearrange expression for k to avoid loss of accuracy due to
1193  * subtraction. Division by 0 not possible because uv > 0, w >= 0. */
1194  k = uv / (sqrt(uv + sq(w)) + w); /* guaranteed positive */
1195  } else { /* q == 0 && r <= 0 */
1196  /* y = 0 with |x| <= 1. Handle this case directly.
1197  * for y small, positive root is k = abs(y)/sqrt(1-x^2) */
1198  k = 0;
1199  }
1200  return k;
1201 }
1202 
1203 real InverseStart(const struct geod_geodesic* g,
1204  real sbet1, real cbet1, real dn1,
1205  real sbet2, real cbet2, real dn2,
1206  real lam12, real slam12, real clam12,
1207  real* psalp1, real* pcalp1,
1208  /* Only updated if return val >= 0 */
1209  real* psalp2, real* pcalp2,
1210  /* Only updated for short lines */
1211  real* pdnm,
1212  /* Scratch area of the right size */
1213  real Ca[]) {
1214  real salp1 = 0, calp1 = 0, salp2 = 0, calp2 = 0, dnm = 0;
1215 
1216  /* Return a starting point for Newton's method in salp1 and calp1 (function
1217  * value is -1). If Newton's method doesn't need to be used, return also
1218  * salp2 and calp2 and function value is sig12. */
1219  real
1220  sig12 = -1, /* Return value */
1221  /* bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0] */
1222  sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
1223  cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
1224  real sbet12a;
1225  boolx shortline = cbet12 >= 0 && sbet12 < (real)(0.5) &&
1226  cbet2 * lam12 < (real)(0.5);
1227  real somg12, comg12, ssig12, csig12;
1228 #if defined(__GNUC__) && __GNUC__ == 4 && \
1229  (__GNUC_MINOR__ < 6 || defined(__MINGW32__))
1230  /* Volatile declaration needed to fix inverse cases
1231  * 88.202499451857 0 -88.202499451857 179.981022032992859592
1232  * 89.262080389218 0 -89.262080389218 179.992207982775375662
1233  * 89.333123580033 0 -89.333123580032997687 179.99295812360148422
1234  * which otherwise fail with g++ 4.4.4 x86 -O3 (Linux)
1235  * and g++ 4.4.0 (mingw) and g++ 4.6.1 (tdm mingw). */
1236  {
1237  volatile real xx1 = sbet2 * cbet1;
1238  volatile real xx2 = cbet2 * sbet1;
1239  sbet12a = xx1 + xx2;
1240  }
1241 #else
1242  sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
1243 #endif
1244  if (shortline) {
1245  real sbetm2 = sq(sbet1 + sbet2), omg12;
1246  /* sin((bet1+bet2)/2)^2
1247  * = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2) */
1248  sbetm2 /= sbetm2 + sq(cbet1 + cbet2);
1249  dnm = sqrt(1 + g->ep2 * sbetm2);
1250  omg12 = lam12 / (g->f1 * dnm);
1251  somg12 = sin(omg12); comg12 = cos(omg12);
1252  } else {
1253  somg12 = slam12; comg12 = clam12;
1254  }
1255 
1256  salp1 = cbet2 * somg12;
1257  calp1 = comg12 >= 0 ?
1258  sbet12 + cbet2 * sbet1 * sq(somg12) / (1 + comg12) :
1259  sbet12a - cbet2 * sbet1 * sq(somg12) / (1 - comg12);
1260 
1261  ssig12 = hypotx(salp1, calp1);
1262  csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
1263 
1264  if (shortline && ssig12 < g->etol2) {
1265  /* really short lines */
1266  salp2 = cbet1 * somg12;
1267  calp2 = sbet12 - cbet1 * sbet2 *
1268  (comg12 >= 0 ? sq(somg12) / (1 + comg12) : 1 - comg12);
1269  norm2(&salp2, &calp2);
1270  /* Set return value */
1271  sig12 = atan2(ssig12, csig12);
1272  } else if (fabs(g->n) > (real)(0.1) || /* No astroid calc if too eccentric */
1273  csig12 >= 0 ||
1274  ssig12 >= 6 * fabs(g->n) * pi * sq(cbet1)) {
1275  /* Nothing to do, zeroth order spherical approximation is OK */
1276  } else {
1277  /* Scale lam12 and bet2 to x, y coordinate system where antipodal point
1278  * is at origin and singular point is at y = 0, x = -1. */
1279  real y, lamscale, betscale;
1280  /* Volatile declaration needed to fix inverse case
1281  * 56.320923501171 0 -56.320923501171 179.664747671772880215
1282  * which otherwise fails with g++ 4.4.4 x86 -O3 */
1283  volatile real x;
1284  real lam12x = atan2(-slam12, -clam12); /* lam12 - pi */
1285  if (g->f >= 0) { /* In fact f == 0 does not get here */
1286  /* x = dlong, y = dlat */
1287  {
1288  real
1289  k2 = sq(sbet1) * g->ep2,
1290  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
1291  lamscale = g->f * cbet1 * A3f(g, eps) * pi;
1292  }
1293  betscale = lamscale * cbet1;
1294 
1295  x = lam12x / lamscale;
1296  y = sbet12a / betscale;
1297  } else { /* f < 0 */
1298  /* x = dlat, y = dlong */
1299  real
1300  cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
1301  bet12a = atan2(sbet12a, cbet12a);
1302  real m12b, m0;
1303  /* In the case of lon12 = 180, this repeats a calculation made in
1304  * Inverse. */
1305  Lengths(g, g->n, pi + bet12a,
1306  sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
1307  cbet1, cbet2, 0, &m12b, &m0, 0, 0, Ca);
1308  x = -1 + m12b / (cbet1 * cbet2 * m0 * pi);
1309  betscale = x < -(real)(0.01) ? sbet12a / x :
1310  -g->f * sq(cbet1) * pi;
1311  lamscale = betscale / cbet1;
1312  y = lam12x / lamscale;
1313  }
1314 
1315  if (y > -tol1 && x > -1 - xthresh) {
1316  /* strip near cut */
1317  if (g->f >= 0) {
1318  salp1 = minx((real)(1), -(real)(x)); calp1 = - sqrt(1 - sq(salp1));
1319  } else {
1320  calp1 = maxx((real)(x > -tol1 ? 0 : -1), (real)(x));
1321  salp1 = sqrt(1 - sq(calp1));
1322  }
1323  } else {
1324  /* Estimate alp1, by solving the astroid problem.
1325  *
1326  * Could estimate alpha1 = theta + pi/2, directly, i.e.,
1327  * calp1 = y/k; salp1 = -x/(1+k); for f >= 0
1328  * calp1 = x/(1+k); salp1 = -y/k; for f < 0 (need to check)
1329  *
1330  * However, it's better to estimate omg12 from astroid and use
1331  * spherical formula to compute alp1. This reduces the mean number of
1332  * Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
1333  * (min 0 max 5). The changes in the number of iterations are as
1334  * follows:
1335  *
1336  * change percent
1337  * 1 5
1338  * 0 78
1339  * -1 16
1340  * -2 0.6
1341  * -3 0.04
1342  * -4 0.002
1343  *
1344  * The histogram of iterations is (m = number of iterations estimating
1345  * alp1 directly, n = number of iterations estimating via omg12, total
1346  * number of trials = 148605):
1347  *
1348  * iter m n
1349  * 0 148 186
1350  * 1 13046 13845
1351  * 2 93315 102225
1352  * 3 36189 32341
1353  * 4 5396 7
1354  * 5 455 1
1355  * 6 56 0
1356  *
1357  * Because omg12 is near pi, estimate work with omg12a = pi - omg12 */
1358  real k = Astroid(x, y);
1359  real
1360  omg12a = lamscale * ( g->f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
1361  somg12 = sin(omg12a); comg12 = -cos(omg12a);
1362  /* Update spherical estimate of alp1 using omg12 instead of lam12 */
1363  salp1 = cbet2 * somg12;
1364  calp1 = sbet12a - cbet2 * sbet1 * sq(somg12) / (1 - comg12);
1365  }
1366  }
1367  /* Sanity check on starting guess. Backwards check allows NaN through. */
1368  if (!(salp1 <= 0))
1369  norm2(&salp1, &calp1);
1370  else {
1371  salp1 = 1; calp1 = 0;
1372  }
1373 
1374  *psalp1 = salp1;
1375  *pcalp1 = calp1;
1376  if (shortline)
1377  *pdnm = dnm;
1378  if (sig12 >= 0) {
1379  *psalp2 = salp2;
1380  *pcalp2 = calp2;
1381  }
1382  return sig12;
1383 }
1384 
1385 real Lambda12(const struct geod_geodesic* g,
1386  real sbet1, real cbet1, real dn1,
1387  real sbet2, real cbet2, real dn2,
1388  real salp1, real calp1,
1389  real slam120, real clam120,
1390  real* psalp2, real* pcalp2,
1391  real* psig12,
1392  real* pssig1, real* pcsig1,
1393  real* pssig2, real* pcsig2,
1394  real* peps,
1395  real* pdomg12,
1396  boolx diffp, real* pdlam12,
1397  /* Scratch area of the right size */
1398  real Ca[]) {
1399  real salp2 = 0, calp2 = 0, sig12 = 0,
1400  ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, eps = 0,
1401  domg12 = 0, dlam12 = 0;
1402  real salp0, calp0;
1403  real somg1, comg1, somg2, comg2, somg12, comg12, lam12;
1404  real B312, eta, k2;
1405 
1406  if (sbet1 == 0 && calp1 == 0)
1407  /* Break degeneracy of equatorial line. This case has already been
1408  * handled. */
1409  calp1 = -tiny;
1410 
1411  /* sin(alp1) * cos(bet1) = sin(alp0) */
1412  salp0 = salp1 * cbet1;
1413  calp0 = hypotx(calp1, salp1 * sbet1); /* calp0 > 0 */
1414 
1415  /* tan(bet1) = tan(sig1) * cos(alp1)
1416  * tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1) */
1417  ssig1 = sbet1; somg1 = salp0 * sbet1;
1418  csig1 = comg1 = calp1 * cbet1;
1419  norm2(&ssig1, &csig1);
1420  /* norm2(&somg1, &comg1); -- don't need to normalize! */
1421 
1422  /* Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
1423  * about this case, since this can yield singularities in the Newton
1424  * iteration.
1425  * sin(alp2) * cos(bet2) = sin(alp0) */
1426  salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
1427  /* calp2 = sqrt(1 - sq(salp2))
1428  * = sqrt(sq(calp0) - sq(sbet2)) / cbet2
1429  * and subst for calp0 and rearrange to give (choose positive sqrt
1430  * to give alp2 in [0, pi/2]). */
1431  calp2 = cbet2 != cbet1 || fabs(sbet2) != -sbet1 ?
1432  sqrt(sq(calp1 * cbet1) +
1433  (cbet1 < -sbet1 ?
1434  (cbet2 - cbet1) * (cbet1 + cbet2) :
1435  (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
1436  fabs(calp1);
1437  /* tan(bet2) = tan(sig2) * cos(alp2)
1438  * tan(omg2) = sin(alp0) * tan(sig2). */
1439  ssig2 = sbet2; somg2 = salp0 * sbet2;
1440  csig2 = comg2 = calp2 * cbet2;
1441  norm2(&ssig2, &csig2);
1442  /* norm2(&somg2, &comg2); -- don't need to normalize! */
1443 
1444  /* sig12 = sig2 - sig1, limit to [0, pi] */
1445  sig12 = atan2(maxx((real)(0), csig1 * ssig2 - ssig1 * csig2),
1446  csig1 * csig2 + ssig1 * ssig2);
1447 
1448  /* omg12 = omg2 - omg1, limit to [0, pi] */
1449  somg12 = maxx((real)(0), comg1 * somg2 - somg1 * comg2);
1450  comg12 = comg1 * comg2 + somg1 * somg2;
1451  /* eta = omg12 - lam120 */
1452  eta = atan2(somg12 * clam120 - comg12 * slam120,
1453  comg12 * clam120 + somg12 * slam120);
1454  k2 = sq(calp0) * g->ep2;
1455  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
1456  C3f(g, eps, Ca);
1457  B312 = (SinCosSeries(TRUE, ssig2, csig2, Ca, nC3-1) -
1458  SinCosSeries(TRUE, ssig1, csig1, Ca, nC3-1));
1459  domg12 = -g->f * A3f(g, eps) * salp0 * (sig12 + B312);
1460  lam12 = eta + domg12;
1461 
1462  if (diffp) {
1463  if (calp2 == 0)
1464  dlam12 = - 2 * g->f1 * dn1 / sbet1;
1465  else {
1466  Lengths(g, eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
1467  cbet1, cbet2, 0, &dlam12, 0, 0, 0, Ca);
1468  dlam12 *= g->f1 / (calp2 * cbet2);
1469  }
1470  }
1471 
1472  *psalp2 = salp2;
1473  *pcalp2 = calp2;
1474  *psig12 = sig12;
1475  *pssig1 = ssig1;
1476  *pcsig1 = csig1;
1477  *pssig2 = ssig2;
1478  *pcsig2 = csig2;
1479  *peps = eps;
1480  *pdomg12 = domg12;
1481  if (diffp)
1482  *pdlam12 = dlam12;
1483 
1484  return lam12;
1485 }
1486 
1487 real A3f(const struct geod_geodesic* g, real eps) {
1488  /* Evaluate A3 */
1489  return polyval(nA3 - 1, g->A3x, eps);
1490 }
1491 
1492 void C3f(const struct geod_geodesic* g, real eps, real c[]) {
1493  /* Evaluate C3 coeffs
1494  * Elements c[1] through c[nC3 - 1] are set */
1495  real mult = 1;
1496  int o = 0, l;
1497  for (l = 1; l < nC3; ++l) { /* l is index of C3[l] */
1498  int m = nC3 - l - 1; /* order of polynomial in eps */
1499  mult *= eps;
1500  c[l] = mult * polyval(m, g->C3x + o, eps);
1501  o += m + 1;
1502  }
1503 }
1504 
1505 void C4f(const struct geod_geodesic* g, real eps, real c[]) {
1506  /* Evaluate C4 coeffs
1507  * Elements c[0] through c[nC4 - 1] are set */
1508  real mult = 1;
1509  int o = 0, l;
1510  for (l = 0; l < nC4; ++l) { /* l is index of C4[l] */
1511  int m = nC4 - l - 1; /* order of polynomial in eps */
1512  c[l] = mult * polyval(m, g->C4x + o, eps);
1513  o += m + 1;
1514  mult *= eps;
1515  }
1516 }
1517 
1518 /* The scale factor A1-1 = mean value of (d/dsigma)I1 - 1 */
1519 real A1m1f(real eps) {
1520  static const real coeff[] = {
1521  /* (1-eps)*A1-1, polynomial in eps2 of order 3 */
1522  1, 4, 64, 0, 256,
1523  };
1524  int m = nA1/2;
1525  real t = polyval(m, coeff, sq(eps)) / coeff[m + 1];
1526  return (t + eps) / (1 - eps);
1527 }
1528 
1529 /* The coefficients C1[l] in the Fourier expansion of B1 */
1530 void C1f(real eps, real c[]) {
1531  static const real coeff[] = {
1532  /* C1[1]/eps^1, polynomial in eps2 of order 2 */
1533  -1, 6, -16, 32,
1534  /* C1[2]/eps^2, polynomial in eps2 of order 2 */
1535  -9, 64, -128, 2048,
1536  /* C1[3]/eps^3, polynomial in eps2 of order 1 */
1537  9, -16, 768,
1538  /* C1[4]/eps^4, polynomial in eps2 of order 1 */
1539  3, -5, 512,
1540  /* C1[5]/eps^5, polynomial in eps2 of order 0 */
1541  -7, 1280,
1542  /* C1[6]/eps^6, polynomial in eps2 of order 0 */
1543  -7, 2048,
1544  };
1545  real
1546  eps2 = sq(eps),
1547  d = eps;
1548  int o = 0, l;
1549  for (l = 1; l <= nC1; ++l) { /* l is index of C1p[l] */
1550  int m = (nC1 - l) / 2; /* order of polynomial in eps^2 */
1551  c[l] = d * polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1552  o += m + 2;
1553  d *= eps;
1554  }
1555 }
1556 
1557 /* The coefficients C1p[l] in the Fourier expansion of B1p */
1558 void C1pf(real eps, real c[]) {
1559  static const real coeff[] = {
1560  /* C1p[1]/eps^1, polynomial in eps2 of order 2 */
1561  205, -432, 768, 1536,
1562  /* C1p[2]/eps^2, polynomial in eps2 of order 2 */
1563  4005, -4736, 3840, 12288,
1564  /* C1p[3]/eps^3, polynomial in eps2 of order 1 */
1565  -225, 116, 384,
1566  /* C1p[4]/eps^4, polynomial in eps2 of order 1 */
1567  -7173, 2695, 7680,
1568  /* C1p[5]/eps^5, polynomial in eps2 of order 0 */
1569  3467, 7680,
1570  /* C1p[6]/eps^6, polynomial in eps2 of order 0 */
1571  38081, 61440,
1572  };
1573  real
1574  eps2 = sq(eps),
1575  d = eps;
1576  int o = 0, l;
1577  for (l = 1; l <= nC1p; ++l) { /* l is index of C1p[l] */
1578  int m = (nC1p - l) / 2; /* order of polynomial in eps^2 */
1579  c[l] = d * polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1580  o += m + 2;
1581  d *= eps;
1582  }
1583 }
1584 
1585 /* The scale factor A2-1 = mean value of (d/dsigma)I2 - 1 */
1586 real A2m1f(real eps) {
1587  static const real coeff[] = {
1588  /* (eps+1)*A2-1, polynomial in eps2 of order 3 */
1589  -11, -28, -192, 0, 256,
1590  };
1591  int m = nA2/2;
1592  real t = polyval(m, coeff, sq(eps)) / coeff[m + 1];
1593  return (t - eps) / (1 + eps);
1594 }
1595 
1596 /* The coefficients C2[l] in the Fourier expansion of B2 */
1597 void C2f(real eps, real c[]) {
1598  static const real coeff[] = {
1599  /* C2[1]/eps^1, polynomial in eps2 of order 2 */
1600  1, 2, 16, 32,
1601  /* C2[2]/eps^2, polynomial in eps2 of order 2 */
1602  35, 64, 384, 2048,
1603  /* C2[3]/eps^3, polynomial in eps2 of order 1 */
1604  15, 80, 768,
1605  /* C2[4]/eps^4, polynomial in eps2 of order 1 */
1606  7, 35, 512,
1607  /* C2[5]/eps^5, polynomial in eps2 of order 0 */
1608  63, 1280,
1609  /* C2[6]/eps^6, polynomial in eps2 of order 0 */
1610  77, 2048,
1611  };
1612  real
1613  eps2 = sq(eps),
1614  d = eps;
1615  int o = 0, l;
1616  for (l = 1; l <= nC2; ++l) { /* l is index of C2[l] */
1617  int m = (nC2 - l) / 2; /* order of polynomial in eps^2 */
1618  c[l] = d * polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1619  o += m + 2;
1620  d *= eps;
1621  }
1622 }
1623 
1624 /* The scale factor A3 = mean value of (d/dsigma)I3 */
1625 void A3coeff(struct geod_geodesic* g) {
1626  static const real coeff[] = {
1627  /* A3, coeff of eps^5, polynomial in n of order 0 */
1628  -3, 128,
1629  /* A3, coeff of eps^4, polynomial in n of order 1 */
1630  -2, -3, 64,
1631  /* A3, coeff of eps^3, polynomial in n of order 2 */
1632  -1, -3, -1, 16,
1633  /* A3, coeff of eps^2, polynomial in n of order 2 */
1634  3, -1, -2, 8,
1635  /* A3, coeff of eps^1, polynomial in n of order 1 */
1636  1, -1, 2,
1637  /* A3, coeff of eps^0, polynomial in n of order 0 */
1638  1, 1,
1639  };
1640  int o = 0, k = 0, j;
1641  for (j = nA3 - 1; j >= 0; --j) { /* coeff of eps^j */
1642  int m = nA3 - j - 1 < j ? nA3 - j - 1 : j; /* order of polynomial in n */
1643  g->A3x[k++] = polyval(m, coeff + o, g->n) / coeff[o + m + 1];
1644  o += m + 2;
1645  }
1646 }
1647 
1648 /* The coefficients C3[l] in the Fourier expansion of B3 */
1649 void C3coeff(struct geod_geodesic* g) {
1650  static const real coeff[] = {
1651  /* C3[1], coeff of eps^5, polynomial in n of order 0 */
1652  3, 128,
1653  /* C3[1], coeff of eps^4, polynomial in n of order 1 */
1654  2, 5, 128,
1655  /* C3[1], coeff of eps^3, polynomial in n of order 2 */
1656  -1, 3, 3, 64,
1657  /* C3[1], coeff of eps^2, polynomial in n of order 2 */
1658  -1, 0, 1, 8,
1659  /* C3[1], coeff of eps^1, polynomial in n of order 1 */
1660  -1, 1, 4,
1661  /* C3[2], coeff of eps^5, polynomial in n of order 0 */
1662  5, 256,
1663  /* C3[2], coeff of eps^4, polynomial in n of order 1 */
1664  1, 3, 128,
1665  /* C3[2], coeff of eps^3, polynomial in n of order 2 */
1666  -3, -2, 3, 64,
1667  /* C3[2], coeff of eps^2, polynomial in n of order 2 */
1668  1, -3, 2, 32,
1669  /* C3[3], coeff of eps^5, polynomial in n of order 0 */
1670  7, 512,
1671  /* C3[3], coeff of eps^4, polynomial in n of order 1 */
1672  -10, 9, 384,
1673  /* C3[3], coeff of eps^3, polynomial in n of order 2 */
1674  5, -9, 5, 192,
1675  /* C3[4], coeff of eps^5, polynomial in n of order 0 */
1676  7, 512,
1677  /* C3[4], coeff of eps^4, polynomial in n of order 1 */
1678  -14, 7, 512,
1679  /* C3[5], coeff of eps^5, polynomial in n of order 0 */
1680  21, 2560,
1681  };
1682  int o = 0, k = 0, l, j;
1683  for (l = 1; l < nC3; ++l) { /* l is index of C3[l] */
1684  for (j = nC3 - 1; j >= l; --j) { /* coeff of eps^j */
1685  int m = nC3 - j - 1 < j ? nC3 - j - 1 : j; /* order of polynomial in n */
1686  g->C3x[k++] = polyval(m, coeff + o, g->n) / coeff[o + m + 1];
1687  o += m + 2;
1688  }
1689  }
1690 }
1691 
1692 /* The coefficients C4[l] in the Fourier expansion of I4 */
1693 void C4coeff(struct geod_geodesic* g) {
1694  static const real coeff[] = {
1695  /* C4[0], coeff of eps^5, polynomial in n of order 0 */
1696  97, 15015,
1697  /* C4[0], coeff of eps^4, polynomial in n of order 1 */
1698  1088, 156, 45045,
1699  /* C4[0], coeff of eps^3, polynomial in n of order 2 */
1700  -224, -4784, 1573, 45045,
1701  /* C4[0], coeff of eps^2, polynomial in n of order 3 */
1702  -10656, 14144, -4576, -858, 45045,
1703  /* C4[0], coeff of eps^1, polynomial in n of order 4 */
1704  64, 624, -4576, 6864, -3003, 15015,
1705  /* C4[0], coeff of eps^0, polynomial in n of order 5 */
1706  100, 208, 572, 3432, -12012, 30030, 45045,
1707  /* C4[1], coeff of eps^5, polynomial in n of order 0 */
1708  1, 9009,
1709  /* C4[1], coeff of eps^4, polynomial in n of order 1 */
1710  -2944, 468, 135135,
1711  /* C4[1], coeff of eps^3, polynomial in n of order 2 */
1712  5792, 1040, -1287, 135135,
1713  /* C4[1], coeff of eps^2, polynomial in n of order 3 */
1714  5952, -11648, 9152, -2574, 135135,
1715  /* C4[1], coeff of eps^1, polynomial in n of order 4 */
1716  -64, -624, 4576, -6864, 3003, 135135,
1717  /* C4[2], coeff of eps^5, polynomial in n of order 0 */
1718  8, 10725,
1719  /* C4[2], coeff of eps^4, polynomial in n of order 1 */
1720  1856, -936, 225225,
1721  /* C4[2], coeff of eps^3, polynomial in n of order 2 */
1722  -8448, 4992, -1144, 225225,
1723  /* C4[2], coeff of eps^2, polynomial in n of order 3 */
1724  -1440, 4160, -4576, 1716, 225225,
1725  /* C4[3], coeff of eps^5, polynomial in n of order 0 */
1726  -136, 63063,
1727  /* C4[3], coeff of eps^4, polynomial in n of order 1 */
1728  1024, -208, 105105,
1729  /* C4[3], coeff of eps^3, polynomial in n of order 2 */
1730  3584, -3328, 1144, 315315,
1731  /* C4[4], coeff of eps^5, polynomial in n of order 0 */
1732  -128, 135135,
1733  /* C4[4], coeff of eps^4, polynomial in n of order 1 */
1734  -2560, 832, 405405,
1735  /* C4[5], coeff of eps^5, polynomial in n of order 0 */
1736  128, 99099,
1737  };
1738  int o = 0, k = 0, l, j;
1739  for (l = 0; l < nC4; ++l) { /* l is index of C4[l] */
1740  for (j = nC4 - 1; j >= l; --j) { /* coeff of eps^j */
1741  int m = nC4 - j - 1; /* order of polynomial in n */
1742  g->C4x[k++] = polyval(m, coeff + o, g->n) / coeff[o + m + 1];
1743  o += m + 2;
1744  }
1745  }
1746 }
1747 
1748 int transit(real lon1, real lon2) {
1749  real lon12;
1750  /* Return 1 or -1 if crossing prime meridian in east or west direction.
1751  * Otherwise return zero. */
1752  /* Compute lon12 the same way as Geodesic::Inverse. */
1753  lon1 = AngNormalize(lon1);
1754  lon2 = AngNormalize(lon2);
1755  lon12 = AngDiff(lon1, lon2, 0);
1756  return lon1 <= 0 && lon2 > 0 && lon12 > 0 ? 1 :
1757  (lon2 <= 0 && lon1 > 0 && lon12 < 0 ? -1 : 0);
1758 }
1759 
1760 int transitdirect(real lon1, real lon2) {
1761  lon1 = fmod(lon1, (real)(720));
1762  lon2 = fmod(lon2, (real)(720));
1763  return ( ((lon2 >= 0 && lon2 < 360) || lon2 < -360 ? 0 : 1) -
1764  ((lon1 >= 0 && lon1 < 360) || lon1 < -360 ? 0 : 1) );
1765 }
1766 
1767 void accini(real s[]) {
1768  /* Initialize an accumulator; this is an array with two elements. */
1769  s[0] = s[1] = 0;
1770 }
1771 
1772 void acccopy(const real s[], real t[]) {
1773  /* Copy an accumulator; t = s. */
1774  t[0] = s[0]; t[1] = s[1];
1775 }
1776 
1777 void accadd(real s[], real y) {
1778  /* Add y to an accumulator. */
1779  real u, z = sumx(y, s[1], &u);
1780  s[0] = sumx(z, s[0], &s[1]);
1781  if (s[0] == 0)
1782  s[0] = u;
1783  else
1784  s[1] = s[1] + u;
1785 }
1786 
1787 real accsum(const real s[], real y) {
1788  /* Return accumulator + y (but don't add to accumulator). */
1789  real t[2];
1790  acccopy(s, t);
1791  accadd(t, y);
1792  return t[0];
1793 }
1794 
1795 void accneg(real s[]) {
1796  /* Negate an accumulator. */
1797  s[0] = -s[0]; s[1] = -s[1];
1798 }
1799 
1800 void geod_polygon_init(struct geod_polygon* p, boolx polylinep) {
1801  p->polyline = (polylinep != 0);
1802  geod_polygon_clear(p);
1803 }
1804 
1805 void geod_polygon_clear(struct geod_polygon* p) {
1806  p->lat0 = p->lon0 = p->lat = p->lon = NaN;
1807  accini(p->P);
1808  accini(p->A);
1809  p->num = p->crossings = 0;
1810 }
1811 
1812 void geod_polygon_addpoint(const struct geod_geodesic* g,
1813  struct geod_polygon* p,
1814  real lat, real lon) {
1815  lon = AngNormalize(lon);
1816  if (p->num == 0) {
1817  p->lat0 = p->lat = lat;
1818  p->lon0 = p->lon = lon;
1819  } else {
1820  real s12, S12 = 0; /* Initialize S12 to stop Visual Studio warning */
1821  geod_geninverse(g, p->lat, p->lon, lat, lon,
1822  &s12, 0, 0, 0, 0, 0, p->polyline ? 0 : &S12);
1823  accadd(p->P, s12);
1824  if (!p->polyline) {
1825  accadd(p->A, S12);
1826  p->crossings += transit(p->lon, lon);
1827  }
1828  p->lat = lat; p->lon = lon;
1829  }
1830  ++p->num;
1831 }
1832 
1833 void geod_polygon_addedge(const struct geod_geodesic* g,
1834  struct geod_polygon* p,
1835  real azi, real s) {
1836  if (p->num) { /* Do nothing is num is zero */
1837  real lat, lon, S12 = 0; /* Initialize S12 to stop Visual Studio warning */
1838  geod_gendirect(g, p->lat, p->lon, azi, GEOD_LONG_UNROLL, s,
1839  &lat, &lon, 0,
1840  0, 0, 0, 0, p->polyline ? 0 : &S12);
1841  accadd(p->P, s);
1842  if (!p->polyline) {
1843  accadd(p->A, S12);
1844  p->crossings += transitdirect(p->lon, lon);
1845  }
1846  p->lat = lat; p->lon = lon;
1847  ++p->num;
1848  }
1849 }
1850 
1851 unsigned geod_polygon_compute(const struct geod_geodesic* g,
1852  const struct geod_polygon* p,
1853  boolx reverse, boolx sign,
1854  real* pA, real* pP) {
1855  real s12, S12, t[2], area0;
1856  int crossings;
1857  if (p->num < 2) {
1858  if (pP) *pP = 0;
1859  if (!p->polyline && pA) *pA = 0;
1860  return p->num;
1861  }
1862  if (p->polyline) {
1863  if (pP) *pP = p->P[0];
1864  return p->num;
1865  }
1866  geod_geninverse(g, p->lat, p->lon, p->lat0, p->lon0,
1867  &s12, 0, 0, 0, 0, 0, &S12);
1868  if (pP) *pP = accsum(p->P, s12);
1869  acccopy(p->A, t);
1870  accadd(t, S12);
1871  crossings = p->crossings + transit(p->lon, p->lon0);
1872  area0 = 4 * pi * g->c2;
1873  if (crossings & 1)
1874  accadd(t, (t[0] < 0 ? 1 : -1) * area0/2);
1875  /* area is with the clockwise sense. If !reverse convert to
1876  * counter-clockwise convention. */
1877  if (!reverse)
1878  accneg(t);
1879  /* If sign put area in (-area0/2, area0/2], else put area in [0, area0) */
1880  if (sign) {
1881  if (t[0] > area0/2)
1882  accadd(t, -area0);
1883  else if (t[0] <= -area0/2)
1884  accadd(t, +area0);
1885  } else {
1886  if (t[0] >= area0)
1887  accadd(t, -area0);
1888  else if (t[0] < 0)
1889  accadd(t, +area0);
1890  }
1891  if (pA) *pA = 0 + t[0];
1892  return p->num;
1893 }
1894 
1895 unsigned geod_polygon_testpoint(const struct geod_geodesic* g,
1896  const struct geod_polygon* p,
1897  real lat, real lon,
1898  boolx reverse, boolx sign,
1899  real* pA, real* pP) {
1900  real perimeter, tempsum, area0;
1901  int crossings, i;
1902  unsigned num = p->num + 1;
1903  if (num == 1) {
1904  if (pP) *pP = 0;
1905  if (!p->polyline && pA) *pA = 0;
1906  return num;
1907  }
1908  perimeter = p->P[0];
1909  tempsum = p->polyline ? 0 : p->A[0];
1910  crossings = p->crossings;
1911  for (i = 0; i < (p->polyline ? 1 : 2); ++i) {
1912  real s12, S12 = 0; /* Initialize S12 to stop Visual Studio warning */
1913  geod_geninverse(g,
1914  i == 0 ? p->lat : lat, i == 0 ? p->lon : lon,
1915  i != 0 ? p->lat0 : lat, i != 0 ? p->lon0 : lon,
1916  &s12, 0, 0, 0, 0, 0, p->polyline ? 0 : &S12);
1917  perimeter += s12;
1918  if (!p->polyline) {
1919  tempsum += S12;
1920  crossings += transit(i == 0 ? p->lon : lon,
1921  i != 0 ? p->lon0 : lon);
1922  }
1923  }
1924 
1925  if (pP) *pP = perimeter;
1926  if (p->polyline)
1927  return num;
1928 
1929  area0 = 4 * pi * g->c2;
1930  if (crossings & 1)
1931  tempsum += (tempsum < 0 ? 1 : -1) * area0/2;
1932  /* area is with the clockwise sense. If !reverse convert to
1933  * counter-clockwise convention. */
1934  if (!reverse)
1935  tempsum *= -1;
1936  /* If sign put area in (-area0/2, area0/2], else put area in [0, area0) */
1937  if (sign) {
1938  if (tempsum > area0/2)
1939  tempsum -= area0;
1940  else if (tempsum <= -area0/2)
1941  tempsum += area0;
1942  } else {
1943  if (tempsum >= area0)
1944  tempsum -= area0;
1945  else if (tempsum < 0)
1946  tempsum += area0;
1947  }
1948  if (pA) *pA = 0 + tempsum;
1949  return num;
1950 }
1951 
1952 unsigned geod_polygon_testedge(const struct geod_geodesic* g,
1953  const struct geod_polygon* p,
1954  real azi, real s,
1955  boolx reverse, boolx sign,
1956  real* pA, real* pP) {
1957  real perimeter, tempsum, area0;
1958  int crossings;
1959  unsigned num = p->num + 1;
1960  if (num == 1) { /* we don't have a starting point! */
1961  if (pP) *pP = NaN;
1962  if (!p->polyline && pA) *pA = NaN;
1963  return 0;
1964  }
1965  perimeter = p->P[0] + s;
1966  if (p->polyline) {
1967  if (pP) *pP = perimeter;
1968  return num;
1969  }
1970 
1971  tempsum = p->A[0];
1972  crossings = p->crossings;
1973  {
1974  real lat, lon, s12, S12;
1975  geod_gendirect(g, p->lat, p->lon, azi, GEOD_LONG_UNROLL, s,
1976  &lat, &lon, 0,
1977  0, 0, 0, 0, &S12);
1978  tempsum += S12;
1979  crossings += transitdirect(p->lon, lon);
1980  geod_geninverse(g, lat, lon, p->lat0, p->lon0,
1981  &s12, 0, 0, 0, 0, 0, &S12);
1982  perimeter += s12;
1983  tempsum += S12;
1984  crossings += transit(lon, p->lon0);
1985  }
1986 
1987  area0 = 4 * pi * g->c2;
1988  if (crossings & 1)
1989  tempsum += (tempsum < 0 ? 1 : -1) * area0/2;
1990  /* area is with the clockwise sense. If !reverse convert to
1991  * counter-clockwise convention. */
1992  if (!reverse)
1993  tempsum *= -1;
1994  /* If sign put area in (-area0/2, area0/2], else put area in [0, area0) */
1995  if (sign) {
1996  if (tempsum > area0/2)
1997  tempsum -= area0;
1998  else if (tempsum <= -area0/2)
1999  tempsum += area0;
2000  } else {
2001  if (tempsum >= area0)
2002  tempsum -= area0;
2003  else if (tempsum < 0)
2004  tempsum += area0;
2005  }
2006  if (pP) *pP = perimeter;
2007  if (pA) *pA = 0 + tempsum;
2008  return num;
2009 }
2010 
2011 void geod_polygonarea(const struct geod_geodesic* g,
2012  real lats[], real lons[], int n,
2013  real* pA, real* pP) {
2014  int i;
2015  struct geod_polygon p;
2016  geod_polygon_init(&p, FALSE);
2017  for (i = 0; i < n; ++i)
2018  geod_polygon_addpoint(g, &p, lats[i], lons[i]);
2019  geod_polygon_compute(g, &p, FALSE, TRUE, pA, pP);
2020 }
2021 
2022 /** @endcond */
unsigned geod_polygon_testedge(const struct geod_geodesic *g, const struct geod_polygon *p, double azi, double s, int reverse, int sign, double *pA, double *pP)
void geod_directline(struct geod_geodesicline *l, const struct geod_geodesic *g, double lat1, double lon1, double azi1, double s12, unsigned caps)
double geod_genposition(const struct geod_geodesicline *l, unsigned flags, double s12_a12, double *plat2, double *plon2, double *pazi2, double *ps12, double *pm12, double *pM12, double *pM21, double *pS12)
void geod_gendirectline(struct geod_geodesicline *l, const struct geod_geodesic *g, double lat1, double lon1, double azi1, unsigned flags, double s12_a12, unsigned caps)
GeographicLib::Math::real real
double lon
Definition: geodesic.h:208
void geod_polygon_addedge(const struct geod_geodesic *g, struct geod_polygon *p, double azi, double s)
unsigned num
Definition: geodesic.h:217
void geod_position(const struct geod_geodesicline *l, double s12, double *plat2, double *plon2, double *pazi2)
double f
Definition: geodesic.h:170
void geod_lineinit(struct geod_geodesicline *l, const struct geod_geodesic *g, double lat1, double lon1, double azi1, unsigned caps)
void geod_setdistance(struct geod_geodesicline *l, double s13)
unsigned caps
Definition: geodesic.h:198
void geod_inverseline(struct geod_geodesicline *l, const struct geod_geodesic *g, double lat1, double lon1, double lat2, double lon2, unsigned caps)
double geod_geninverse(const struct geod_geodesic *g, double lat1, double lon1, double lat2, double lon2, double *ps12, double *pazi1, double *pazi2, double *pm12, double *pM12, double *pM21, double *pS12)
void geod_polygon_addpoint(const struct geod_geodesic *g, struct geod_polygon *p, double lat, double lon)
void geod_polygon_clear(struct geod_polygon *p)
void geod_polygon_init(struct geod_polygon *p, int polylinep)
void geod_direct(const struct geod_geodesic *g, double lat1, double lon1, double azi1, double s12, double *plat2, double *plon2, double *pazi2)
unsigned geod_polygon_compute(const struct geod_geodesic *g, const struct geod_polygon *p, int reverse, int sign, double *pA, double *pP)
void geod_polygonarea(const struct geod_geodesic *g, double lats[], double lons[], int n, double *pA, double *pP)
void geod_gensetdistance(struct geod_geodesicline *l, unsigned flags, double s13_a13)
double a
Definition: geodesic.h:169
double geod_gendirect(const struct geod_geodesic *g, double lat1, double lon1, double azi1, unsigned flags, double s12_a12, double *plat2, double *plon2, double *pazi2, double *ps12, double *pm12, double *pM12, double *pM21, double *pS12)
unsigned geod_polygon_testpoint(const struct geod_geodesic *g, const struct geod_polygon *p, double lat, double lon, int reverse, int sign, double *pA, double *pP)
void geod_inverse(const struct geod_geodesic *g, double lat1, double lon1, double lat2, double lon2, double *ps12, double *pazi1, double *pazi2)
void geod_init(struct geod_geodesic *g, double a, double f)
double lat
Definition: geodesic.h:207
API for the geodesic routines in C.