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