-
Notifications
You must be signed in to change notification settings - Fork 791
/
geodesic.c
2028 lines (1880 loc) · 71.5 KB
/
geodesic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file geodesic.c
* \brief Implementation of the geodesic routines in C
*
* For the full documentation see geodesic.h.
**********************************************************************/
/** @cond SKIP */
/*
* This is a C implementation of the geodesic algorithms described in
*
* C. F. F. Karney,
* Algorithms for geodesics,
* J. Geodesy <b>87</b>, 43--55 (2013);
* https://doi.org/10.1007/s00190-012-0578-z
* Addenda: https://geographiclib.sourceforge.io/geod-addenda.html
*
* See the comments in geodesic.h for documentation.
*
* Copyright (c) Charles Karney (2012-2022) <charles@karney.com> and licensed
* under the MIT/X11 License. For more information, see
* https://geographiclib.sourceforge.io/
*/
#include "geodesic.h"
#include <math.h>
#include <float.h>
#if !defined(__cplusplus)
#define nullptr 0
#endif
#define GEOGRAPHICLIB_GEODESIC_ORDER 6
#define nA1 GEOGRAPHICLIB_GEODESIC_ORDER
#define nC1 GEOGRAPHICLIB_GEODESIC_ORDER
#define nC1p GEOGRAPHICLIB_GEODESIC_ORDER
#define nA2 GEOGRAPHICLIB_GEODESIC_ORDER
#define nC2 GEOGRAPHICLIB_GEODESIC_ORDER
#define nA3 GEOGRAPHICLIB_GEODESIC_ORDER
#define nA3x nA3
#define nC3 GEOGRAPHICLIB_GEODESIC_ORDER
#define nC3x ((nC3 * (nC3 - 1)) / 2)
#define nC4 GEOGRAPHICLIB_GEODESIC_ORDER
#define nC4x ((nC4 * (nC4 + 1)) / 2)
#define nC (GEOGRAPHICLIB_GEODESIC_ORDER + 1)
typedef int boolx;
enum booly { FALSE = 0, TRUE = 1 };
/* qd = quarter turn / degree
* hd = half turn / degree
* td = full turn / degree */
enum dms { qd = 90, hd = 2 * qd, td = 2 * hd };
static unsigned init = 0;
static unsigned digits, maxit1, maxit2;
static double epsilon, realmin, pi, degree, NaN,
tiny, tol0, tol1, tol2, tolb, xthresh;
static void Init(void) {
if (!init) {
digits = DBL_MANT_DIG;
epsilon = DBL_EPSILON;
realmin = DBL_MIN;
#if defined(M_PI)
pi = M_PI;
#else
pi = atan2(0.0, -1.0);
#endif
maxit1 = 20;
maxit2 = maxit1 + digits + 10;
tiny = sqrt(realmin);
tol0 = epsilon;
/* Increase multiplier in defn of tol1 from 100 to 200 to fix inverse case
* 52.784459512564 0 -52.784459512563990912 179.634407464943777557
* which otherwise failed for Visual Studio 10 (Release and Debug) */
tol1 = 200 * tol0;
tol2 = sqrt(tol0);
/* Check on bisection interval */
tolb = tol0;
xthresh = 1000 * tol2;
degree = pi/hd;
NaN = nan("0");
init = 1;
}
}
enum captype {
CAP_NONE = 0U,
CAP_C1 = 1U<<0,
CAP_C1p = 1U<<1,
CAP_C2 = 1U<<2,
CAP_C3 = 1U<<3,
CAP_C4 = 1U<<4,
CAP_ALL = 0x1FU,
OUT_ALL = 0x7F80U
};
static double sq(double x) { return x * x; }
static double sumx(double u, double v, double* t) {
volatile double s = u + v;
volatile double up = s - v;
volatile double vpp = s - up;
up -= u;
vpp -= v;
if (t) *t = s != 0 ? 0 - (up + vpp) : s;
/* error-free sum:
* u + v = s + t
* = round(u + v) + t */
return s;
}
static double polyvalx(int N, const double p[], double x) {
double y = N < 0 ? 0 : *p++;
while (--N >= 0) y = y * x + *p++;
return y;
}
static void swapx(double* x, double* y)
{ double t = *x; *x = *y; *y = t; }
static void norm2(double* sinx, double* cosx) {
#if defined(_MSC_VER) && defined(_M_IX86)
/* hypot for Visual Studio (A=win32) fails monotonicity, e.g., with
* x = 0.6102683302836215
* y1 = 0.7906090004346522
* y2 = y1 + 1e-16
* the test
* hypot(x, y2) >= hypot(x, y1)
* fails. See also
* https://bugs.python.org/issue43088 */
double r = sqrt(*sinx * *sinx + *cosx * *cosx);
#else
double r = hypot(*sinx, *cosx);
#endif
*sinx /= r;
*cosx /= r;
}
static double AngNormalize(double x) {
double y = remainder(x, (double)td);
return fabs(y) == hd ? copysign((double)hd, x) : y;
}
static double LatFix(double x)
{ return fabs(x) > qd ? NaN : x; }
static double AngDiff(double x, double y, double* e) {
/* Use remainder instead of AngNormalize, since we treat boundary cases
* later taking account of the error */
double t, d = sumx(remainder(-x, (double)td), remainder( y, (double)td), &t);
/* This second sum can only change d if abs(d) < 128, so don't need to
* apply remainder yet again. */
d = sumx(remainder(d, (double)td), t, &t);
/* Fix the sign if d = -180, 0, 180. */
if (d == 0 || fabs(d) == hd)
/* If t == 0, take sign from y - x
* else (t != 0, implies d = +/-180), d and t must have opposite signs */
d = copysign(d, t == 0 ? y - x : -t);
if (e) *e = t;
return d;
}
static double AngRound(double x) {
/* False positive in cppcheck requires "1.0" instead of "1" */
const double z = 1.0/16.0;
volatile double y = fabs(x);
volatile double w = z - y;
/* The compiler mustn't "simplify" z - (z - y) to y */
y = w > 0 ? z - w : y;
return copysign(y, x);
}
static void sincosdx(double x, double* sinx, double* cosx) {
/* In order to minimize round-off errors, this function exactly reduces
* the argument to the range [-45, 45] before converting it to radians. */
double r, s, c; int q = 0;
r = remquo(x, (double)qd, &q);
/* now abs(r) <= 45 */
r *= degree;
/* Possibly could call the gnu extension sincos */
s = sin(r); c = cos(r);
switch ((unsigned)q & 3U) {
case 0U: *sinx = s; *cosx = c; break;
case 1U: *sinx = c; *cosx = -s; break;
case 2U: *sinx = -s; *cosx = -c; break;
default: *sinx = -c; *cosx = s; break; /* case 3U */
}
/* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1950.pdf */
*cosx += 0; /* special values from F.10.1.12 */
/* special values from F.10.1.13 */
if (*sinx == 0) *sinx = copysign(*sinx, x);
}
static void sincosde(double x, double t, double* sinx, double* cosx) {
/* In order to minimize round-off errors, this function exactly reduces
* the argument to the range [-45, 45] before converting it to radians. */
double r, s, c; int q = 0;
r = AngRound(remquo(x, (double)qd, &q) + t);
/* now abs(r) <= 45 */
r *= degree;
/* Possibly could call the gnu extension sincos */
s = sin(r); c = cos(r);
switch ((unsigned)q & 3U) {
case 0U: *sinx = s; *cosx = c; break;
case 1U: *sinx = c; *cosx = -s; break;
case 2U: *sinx = -s; *cosx = -c; break;
default: *sinx = -c; *cosx = s; break; /* case 3U */
}
/* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1950.pdf */
*cosx += 0; /* special values from F.10.1.12 */
/* special values from F.10.1.13 */
if (*sinx == 0) *sinx = copysign(*sinx, x);
}
static double atan2dx(double y, double x) {
/* In order to minimize round-off errors, this function rearranges the
* arguments so that result of atan2 is in the range [-pi/4, pi/4] before
* converting it to degrees and mapping the result to the correct
* quadrant. */
int q = 0; double ang;
if (fabs(y) > fabs(x)) { swapx(&x, &y); q = 2; }
if (signbit(x)) { x = -x; ++q; }
/* here x >= 0 and x >= abs(y), so angle is in [-pi/4, pi/4] */
ang = atan2(y, x) / degree;
switch (q) {
case 1: ang = copysign((double)hd, y) - ang; break;
case 2: ang = qd - ang; break;
case 3: ang = -qd + ang; break;
default: break;
}
return ang;
}
static void A3coeff(struct geod_geodesic* g);
static void C3coeff(struct geod_geodesic* g);
static void C4coeff(struct geod_geodesic* g);
static double SinCosSeries(boolx sinp,
double sinx, double cosx,
const double c[], int n);
static void Lengths(const struct geod_geodesic* g,
double eps, double sig12,
double ssig1, double csig1, double dn1,
double ssig2, double csig2, double dn2,
double cbet1, double cbet2,
double* ps12b, double* pm12b, double* pm0,
double* pM12, double* pM21,
/* Scratch area of the right size */
double Ca[]);
static double Astroid(double x, double y);
static double InverseStart(const struct geod_geodesic* g,
double sbet1, double cbet1, double dn1,
double sbet2, double cbet2, double dn2,
double lam12, double slam12, double clam12,
double* psalp1, double* pcalp1,
/* Only updated if return val >= 0 */
double* psalp2, double* pcalp2,
/* Only updated for short lines */
double* pdnm,
/* Scratch area of the right size */
double Ca[]);
static double Lambda12(const struct geod_geodesic* g,
double sbet1, double cbet1, double dn1,
double sbet2, double cbet2, double dn2,
double salp1, double calp1,
double slam120, double clam120,
double* psalp2, double* pcalp2,
double* psig12,
double* pssig1, double* pcsig1,
double* pssig2, double* pcsig2,
double* peps,
double* pdomg12,
boolx diffp, double* pdlam12,
/* Scratch area of the right size */
double Ca[]);
static double A3f(const struct geod_geodesic* g, double eps);
static void C3f(const struct geod_geodesic* g, double eps, double c[]);
static void C4f(const struct geod_geodesic* g, double eps, double c[]);
static double A1m1f(double eps);
static void C1f(double eps, double c[]);
static void C1pf(double eps, double c[]);
static double A2m1f(double eps);
static void C2f(double eps, double c[]);
static int transit(double lon1, double lon2);
static int transitdirect(double lon1, double lon2);
static void accini(double s[]);
static void acccopy(const double s[], double t[]);
static void accadd(double s[], double y);
static double accsum(const double s[], double y);
static void accneg(double s[]);
static void accrem(double s[], double y);
static double areareduceA(double area[], double area0,
int crossings, boolx reverse, boolx sign);
static double areareduceB(double area, double area0,
int crossings, boolx reverse, boolx sign);
void geod_init(struct geod_geodesic* g, double a, double f) {
if (!init) Init();
g->a = a;
g->f = f;
g->f1 = 1 - g->f;
g->e2 = g->f * (2 - g->f);
g->ep2 = g->e2 / sq(g->f1); /* e2 / (1 - e2) */
g->n = g->f / ( 2 - g->f);
g->b = g->a * g->f1;
g->c2 = (sq(g->a) + sq(g->b) *
(g->e2 == 0 ? 1 :
(g->e2 > 0 ? atanh(sqrt(g->e2)) : atan(sqrt(-g->e2))) /
sqrt(fabs(g->e2))))/2; /* authalic radius squared */
/* The sig12 threshold for "really short". Using the auxiliary sphere
* solution with dnm computed at (bet1 + bet2) / 2, the relative error in the
* azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2. (Error
* measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a given f and
* sig12, the max error occurs for lines near the pole. If the old rule for
* computing dnm = (dn1 + dn2)/2 is used, then the error increases by a
* factor of 2.) Setting this equal to epsilon gives sig12 = etol2. Here
* 0.1 is a safety factor (error decreased by 100) and max(0.001, abs(f))
* stops etol2 getting too large in the nearly spherical case. */
g->etol2 = 0.1 * tol2 /
sqrt( fmax(0.001, fabs(g->f)) * fmin(1.0, 1 - g->f/2) / 2 );
A3coeff(g);
C3coeff(g);
C4coeff(g);
}
static void geod_lineinit_int(struct geod_geodesicline* l,
const struct geod_geodesic* g,
double lat1, double lon1,
double azi1, double salp1, double calp1,
unsigned caps) {
double cbet1, sbet1, eps;
l->a = g->a;
l->f = g->f;
l->b = g->b;
l->c2 = g->c2;
l->f1 = g->f1;
/* If caps is 0 assume the standard direct calculation */
l->caps = (caps ? caps : GEOD_DISTANCE_IN | GEOD_LONGITUDE) |
/* always allow latitude and azimuth and unrolling of longitude */
GEOD_LATITUDE | GEOD_AZIMUTH | GEOD_LONG_UNROLL;
l->lat1 = LatFix(lat1);
l->lon1 = lon1;
l->azi1 = azi1;
l->salp1 = salp1;
l->calp1 = calp1;
sincosdx(AngRound(l->lat1), &sbet1, &cbet1); sbet1 *= l->f1;
/* Ensure cbet1 = +epsilon at poles */
norm2(&sbet1, &cbet1); cbet1 = fmax(tiny, cbet1);
l->dn1 = sqrt(1 + g->ep2 * sq(sbet1));
/* Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0), */
l->salp0 = l->salp1 * cbet1; /* alp0 in [0, pi/2 - |bet1|] */
/* Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
* is slightly better (consider the case salp1 = 0). */
l->calp0 = hypot(l->calp1, l->salp1 * sbet1);
/* Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
* sig = 0 is nearest northward crossing of equator.
* With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
* With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
* With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
* Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
* With alp0 in (0, pi/2], quadrants for sig and omg coincide.
* No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
* With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi. */
l->ssig1 = sbet1; l->somg1 = l->salp0 * sbet1;
l->csig1 = l->comg1 = sbet1 != 0 || l->calp1 != 0 ? cbet1 * l->calp1 : 1;
norm2(&l->ssig1, &l->csig1); /* sig1 in (-pi, pi] */
/* norm2(somg1, comg1); -- don't need to normalize! */
l->k2 = sq(l->calp0) * g->ep2;
eps = l->k2 / (2 * (1 + sqrt(1 + l->k2)) + l->k2);
if (l->caps & CAP_C1) {
double s, c;
l->A1m1 = A1m1f(eps);
C1f(eps, l->C1a);
l->B11 = SinCosSeries(TRUE, l->ssig1, l->csig1, l->C1a, nC1);
s = sin(l->B11); c = cos(l->B11);
/* tau1 = sig1 + B11 */
l->stau1 = l->ssig1 * c + l->csig1 * s;
l->ctau1 = l->csig1 * c - l->ssig1 * s;
/* Not necessary because C1pa reverts C1a
* B11 = -SinCosSeries(TRUE, stau1, ctau1, C1pa, nC1p); */
}
if (l->caps & CAP_C1p)
C1pf(eps, l->C1pa);
if (l->caps & CAP_C2) {
l->A2m1 = A2m1f(eps);
C2f(eps, l->C2a);
l->B21 = SinCosSeries(TRUE, l->ssig1, l->csig1, l->C2a, nC2);
}
if (l->caps & CAP_C3) {
C3f(g, eps, l->C3a);
l->A3c = -l->f * l->salp0 * A3f(g, eps);
l->B31 = SinCosSeries(TRUE, l->ssig1, l->csig1, l->C3a, nC3-1);
}
if (l->caps & CAP_C4) {
C4f(g, eps, l->C4a);
/* Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0) */
l->A4 = sq(l->a) * l->calp0 * l->salp0 * g->e2;
l->B41 = SinCosSeries(FALSE, l->ssig1, l->csig1, l->C4a, nC4);
}
l->a13 = l->s13 = NaN;
}
void geod_lineinit(struct geod_geodesicline* l,
const struct geod_geodesic* g,
double lat1, double lon1, double azi1, unsigned caps) {
double salp1, calp1;
azi1 = AngNormalize(azi1);
/* Guard against underflow in salp0 */
sincosdx(AngRound(azi1), &salp1, &calp1);
geod_lineinit_int(l, g, lat1, lon1, azi1, salp1, calp1, caps);
}
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) {
geod_lineinit(l, g, lat1, lon1, azi1, caps);
geod_gensetdistance(l, flags, s12_a12);
}
void geod_directline(struct geod_geodesicline* l,
const struct geod_geodesic* g,
double lat1, double lon1, double azi1,
double s12, unsigned caps) {
geod_gendirectline(l, g, lat1, lon1, azi1, GEOD_NOFLAGS, s12, 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) {
double lat2 = 0, lon2 = 0, azi2 = 0, s12 = 0,
m12 = 0, M12 = 0, M21 = 0, S12 = 0;
/* Avoid warning about uninitialized B12. */
double sig12, ssig12, csig12, B12 = 0, AB1 = 0;
double omg12, lam12, lon12;
double ssig2, csig2, sbet2, cbet2, somg2, comg2, salp2, calp2, dn2;
unsigned outmask =
(plat2 ? GEOD_LATITUDE : GEOD_NONE) |
(plon2 ? GEOD_LONGITUDE : GEOD_NONE) |
(pazi2 ? GEOD_AZIMUTH : GEOD_NONE) |
(ps12 ? GEOD_DISTANCE : GEOD_NONE) |
(pm12 ? GEOD_REDUCEDLENGTH : GEOD_NONE) |
(pM12 || pM21 ? GEOD_GEODESICSCALE : GEOD_NONE) |
(pS12 ? GEOD_AREA : GEOD_NONE);
outmask &= l->caps & OUT_ALL;
if (!( (flags & GEOD_ARCMODE || (l->caps & (GEOD_DISTANCE_IN & OUT_ALL))) ))
/* Impossible distance calculation requested */
return NaN;
if (flags & GEOD_ARCMODE) {
/* Interpret s12_a12 as spherical arc length */
sig12 = s12_a12 * degree;
sincosdx(s12_a12, &ssig12, &csig12);
} else {
/* Interpret s12_a12 as distance */
double
tau12 = s12_a12 / (l->b * (1 + l->A1m1)),
s = sin(tau12),
c = cos(tau12);
/* tau2 = tau1 + tau12 */
B12 = - SinCosSeries(TRUE,
l->stau1 * c + l->ctau1 * s,
l->ctau1 * c - l->stau1 * s,
l->C1pa, nC1p);
sig12 = tau12 - (B12 - l->B11);
ssig12 = sin(sig12); csig12 = cos(sig12);
if (fabs(l->f) > 0.01) {
/* Reverted distance series is inaccurate for |f| > 1/100, so correct
* sig12 with 1 Newton iteration. The following table shows the
* approximate maximum error for a = WGS_a() and various f relative to
* GeodesicExact.
* erri = the error in the inverse solution (nm)
* errd = the error in the direct solution (series only) (nm)
* errda = the error in the direct solution (series + 1 Newton) (nm)
*
* f erri errd errda
* -1/5 12e6 1.2e9 69e6
* -1/10 123e3 12e6 765e3
* -1/20 1110 108e3 7155
* -1/50 18.63 200.9 27.12
* -1/100 18.63 23.78 23.37
* -1/150 18.63 21.05 20.26
* 1/150 22.35 24.73 25.83
* 1/100 22.35 25.03 25.31
* 1/50 29.80 231.9 30.44
* 1/20 5376 146e3 10e3
* 1/10 829e3 22e6 1.5e6
* 1/5 157e6 3.8e9 280e6 */
double serr;
ssig2 = l->ssig1 * csig12 + l->csig1 * ssig12;
csig2 = l->csig1 * csig12 - l->ssig1 * ssig12;
B12 = SinCosSeries(TRUE, ssig2, csig2, l->C1a, nC1);
serr = (1 + l->A1m1) * (sig12 + (B12 - l->B11)) - s12_a12 / l->b;
sig12 = sig12 - serr / sqrt(1 + l->k2 * sq(ssig2));
ssig12 = sin(sig12); csig12 = cos(sig12);
/* Update B12 below */
}
}
/* sig2 = sig1 + sig12 */
ssig2 = l->ssig1 * csig12 + l->csig1 * ssig12;
csig2 = l->csig1 * csig12 - l->ssig1 * ssig12;
dn2 = sqrt(1 + l->k2 * sq(ssig2));
if (outmask & (GEOD_DISTANCE | GEOD_REDUCEDLENGTH | GEOD_GEODESICSCALE)) {
if (flags & GEOD_ARCMODE || fabs(l->f) > 0.01)
B12 = SinCosSeries(TRUE, ssig2, csig2, l->C1a, nC1);
AB1 = (1 + l->A1m1) * (B12 - l->B11);
}
/* sin(bet2) = cos(alp0) * sin(sig2) */
sbet2 = l->calp0 * ssig2;
/* Alt: cbet2 = hypot(csig2, salp0 * ssig2); */
cbet2 = hypot(l->salp0, l->calp0 * csig2);
if (cbet2 == 0)
/* I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case */
cbet2 = csig2 = tiny;
/* tan(alp0) = cos(sig2)*tan(alp2) */
salp2 = l->salp0; calp2 = l->calp0 * csig2; /* No need to normalize */
if (outmask & GEOD_DISTANCE)
s12 = (flags & GEOD_ARCMODE) ?
l->b * ((1 + l->A1m1) * sig12 + AB1) :
s12_a12;
if (outmask & GEOD_LONGITUDE) {
double E = copysign(1, l->salp0); /* east or west going? */
/* tan(omg2) = sin(alp0) * tan(sig2) */
somg2 = l->salp0 * ssig2; comg2 = csig2; /* No need to normalize */
/* omg12 = omg2 - omg1 */
omg12 = (flags & GEOD_LONG_UNROLL)
? E * (sig12
- (atan2( ssig2, csig2) - atan2( l->ssig1, l->csig1))
+ (atan2(E * somg2, comg2) - atan2(E * l->somg1, l->comg1)))
: atan2(somg2 * l->comg1 - comg2 * l->somg1,
comg2 * l->comg1 + somg2 * l->somg1);
lam12 = omg12 + l->A3c *
( sig12 + (SinCosSeries(TRUE, ssig2, csig2, l->C3a, nC3-1)
- l->B31));
lon12 = lam12 / degree;
lon2 = (flags & GEOD_LONG_UNROLL) ? l->lon1 + lon12 :
AngNormalize(AngNormalize(l->lon1) + AngNormalize(lon12));
}
if (outmask & GEOD_LATITUDE)
lat2 = atan2dx(sbet2, l->f1 * cbet2);
if (outmask & GEOD_AZIMUTH)
azi2 = atan2dx(salp2, calp2);
if (outmask & (GEOD_REDUCEDLENGTH | GEOD_GEODESICSCALE)) {
double
B22 = SinCosSeries(TRUE, ssig2, csig2, l->C2a, nC2),
AB2 = (1 + l->A2m1) * (B22 - l->B21),
J12 = (l->A1m1 - l->A2m1) * sig12 + (AB1 - AB2);
if (outmask & GEOD_REDUCEDLENGTH)
/* Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure
* accurate cancellation in the case of coincident points. */
m12 = l->b * ((dn2 * (l->csig1 * ssig2) - l->dn1 * (l->ssig1 * csig2))
- l->csig1 * csig2 * J12);
if (outmask & GEOD_GEODESICSCALE) {
double t = l->k2 * (ssig2 - l->ssig1) * (ssig2 + l->ssig1) /
(l->dn1 + dn2);
M12 = csig12 + (t * ssig2 - csig2 * J12) * l->ssig1 / l->dn1;
M21 = csig12 - (t * l->ssig1 - l->csig1 * J12) * ssig2 / dn2;
}
}
if (outmask & GEOD_AREA) {
double
B42 = SinCosSeries(FALSE, ssig2, csig2, l->C4a, nC4);
double salp12, calp12;
if (l->calp0 == 0 || l->salp0 == 0) {
/* alp12 = alp2 - alp1, used in atan2 so no need to normalize */
salp12 = salp2 * l->calp1 - calp2 * l->salp1;
calp12 = calp2 * l->calp1 + salp2 * l->salp1;
} else {
/* tan(alp) = tan(alp0) * sec(sig)
* tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
* = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
* If csig12 > 0, write
* csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
* else
* csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
* No need to normalize */
salp12 = l->calp0 * l->salp0 *
(csig12 <= 0 ? l->csig1 * (1 - csig12) + ssig12 * l->ssig1 :
ssig12 * (l->csig1 * ssig12 / (1 + csig12) + l->ssig1));
calp12 = sq(l->salp0) + sq(l->calp0) * l->csig1 * csig2;
}
S12 = l->c2 * atan2(salp12, calp12) + l->A4 * (B42 - l->B41);
}
/* In the pattern
*
* if ((outmask & GEOD_XX) && pYY)
* *pYY = YY;
*
* the second check "&& pYY" is redundant. It's there to make the CLang
* static analyzer happy.
*/
if ((outmask & GEOD_LATITUDE) && plat2)
*plat2 = lat2;
if ((outmask & GEOD_LONGITUDE) && plon2)
*plon2 = lon2;
if ((outmask & GEOD_AZIMUTH) && pazi2)
*pazi2 = azi2;
if ((outmask & GEOD_DISTANCE) && ps12)
*ps12 = s12;
if ((outmask & GEOD_REDUCEDLENGTH) && pm12)
*pm12 = m12;
if (outmask & GEOD_GEODESICSCALE) {
if (pM12) *pM12 = M12;
if (pM21) *pM21 = M21;
}
if ((outmask & GEOD_AREA) && pS12)
*pS12 = S12;
return (flags & GEOD_ARCMODE) ? s12_a12 : sig12 / degree;
}
void geod_setdistance(struct geod_geodesicline* l, double s13) {
l->s13 = s13;
l->a13 = geod_genposition(l, GEOD_NOFLAGS, l->s13, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr);
}
static void geod_setarc(struct geod_geodesicline* l, double a13) {
l->a13 = a13; l->s13 = NaN;
geod_genposition(l, GEOD_ARCMODE, l->a13, nullptr, nullptr, nullptr, &l->s13,
nullptr, nullptr, nullptr, nullptr);
}
void geod_gensetdistance(struct geod_geodesicline* l,
unsigned flags, double s13_a13) {
(flags & GEOD_ARCMODE) ?
geod_setarc(l, s13_a13) :
geod_setdistance(l, s13_a13);
}
void geod_position(const struct geod_geodesicline* l, double s12,
double* plat2, double* plon2, double* pazi2) {
geod_genposition(l, FALSE, s12, plat2, plon2, pazi2,
nullptr, nullptr, nullptr, nullptr, nullptr);
}
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) {
struct geod_geodesicline l;
unsigned outmask =
(plat2 ? GEOD_LATITUDE : GEOD_NONE) |
(plon2 ? GEOD_LONGITUDE : GEOD_NONE) |
(pazi2 ? GEOD_AZIMUTH : GEOD_NONE) |
(ps12 ? GEOD_DISTANCE : GEOD_NONE) |
(pm12 ? GEOD_REDUCEDLENGTH : GEOD_NONE) |
(pM12 || pM21 ? GEOD_GEODESICSCALE : GEOD_NONE) |
(pS12 ? GEOD_AREA : GEOD_NONE);
geod_lineinit(&l, g, lat1, lon1, azi1,
/* Automatically supply GEOD_DISTANCE_IN if necessary */
outmask |
((flags & GEOD_ARCMODE) ? GEOD_NONE : GEOD_DISTANCE_IN));
return geod_genposition(&l, flags, s12_a12,
plat2, plon2, pazi2, ps12, pm12, pM12, pM21, pS12);
}
void geod_direct(const struct geod_geodesic* g,
double lat1, double lon1, double azi1,
double s12,
double* plat2, double* plon2, double* pazi2) {
geod_gendirect(g, lat1, lon1, azi1, GEOD_NOFLAGS, s12, plat2, plon2, pazi2,
nullptr, nullptr, nullptr, nullptr, nullptr);
}
static double geod_geninverse_int(const struct geod_geodesic* g,
double lat1, double lon1,
double lat2, double lon2,
double* ps12,
double* psalp1, double* pcalp1,
double* psalp2, double* pcalp2,
double* pm12, double* pM12, double* pM21,
double* pS12) {
double s12 = 0, m12 = 0, M12 = 0, M21 = 0, S12 = 0;
double lon12, lon12s;
int latsign, lonsign, swapp;
double sbet1, cbet1, sbet2, cbet2, s12x = 0, m12x = 0;
double dn1, dn2, lam12, slam12, clam12;
double a12 = 0, sig12, calp1 = 0, salp1 = 0, calp2 = 0, salp2 = 0;
double Ca[nC];
boolx meridian;
/* somg12 == 2 marks that it needs to be calculated */
double omg12 = 0, somg12 = 2, comg12 = 0;
unsigned outmask =
(ps12 ? GEOD_DISTANCE : GEOD_NONE) |
(pm12 ? GEOD_REDUCEDLENGTH : GEOD_NONE) |
(pM12 || pM21 ? GEOD_GEODESICSCALE : GEOD_NONE) |
(pS12 ? GEOD_AREA : GEOD_NONE);
outmask &= OUT_ALL;
/* Compute longitude difference (AngDiff does this carefully). Result is
* in [-180, 180] but -180 is only for west-going geodesics. 180 is for
* east-going and meridional geodesics. */
lon12 = AngDiff(lon1, lon2, &lon12s);
/* Make longitude difference positive. */
lonsign = signbit(lon12) ? -1 : 1;
lon12 *= lonsign; lon12s *= lonsign;
lam12 = lon12 * degree;
/* Calculate sincos of lon12 + error (this applies AngRound internally). */
sincosde(lon12, lon12s, &slam12, &clam12);
lon12s = (hd - lon12) - lon12s; /* the supplementary longitude difference */
/* If really close to the equator, treat as on equator. */
lat1 = AngRound(LatFix(lat1));
lat2 = AngRound(LatFix(lat2));
/* Swap points so that point with higher (abs) latitude is point 1
* If one latitude is a nan, then it becomes lat1. */
swapp = fabs(lat1) < fabs(lat2) || lat2 != lat2 ? -1 : 1;
if (swapp < 0) {
lonsign *= -1;
swapx(&lat1, &lat2);
}
/* Make lat1 <= -0 */
latsign = signbit(lat1) ? 1 : -1;
lat1 *= latsign;
lat2 *= latsign;
/* Now we have
*
* 0 <= lon12 <= 180
* -90 <= lat1 <= -0
* lat1 <= lat2 <= -lat1
*
* longsign, swapp, latsign register the transformation to bring the
* coordinates to this canonical form. In all cases, 1 means no change was
* made. We make these transformations so that there are few cases to
* check, e.g., on verifying quadrants in atan2. In addition, this
* enforces some symmetries in the results returned. */
sincosdx(lat1, &sbet1, &cbet1); sbet1 *= g->f1;
/* Ensure cbet1 = +epsilon at poles */
norm2(&sbet1, &cbet1); cbet1 = fmax(tiny, cbet1);
sincosdx(lat2, &sbet2, &cbet2); sbet2 *= g->f1;
/* Ensure cbet2 = +epsilon at poles */
norm2(&sbet2, &cbet2); cbet2 = fmax(tiny, cbet2);
/* If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
* |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
* a better measure. This logic is used in assigning calp2 in Lambda12.
* Sometimes these quantities vanish and in that case we force bet2 = +/-
* bet1 exactly. An example where is is necessary is the inverse problem
* 48.522876735459 0 -48.52287673545898293 179.599720456223079643
* which failed with Visual Studio 10 (Release and Debug) */
if (cbet1 < -sbet1) {
if (cbet2 == cbet1)
sbet2 = copysign(sbet1, sbet2);
} else {
if (fabs(sbet2) == -sbet1)
cbet2 = cbet1;
}
dn1 = sqrt(1 + g->ep2 * sq(sbet1));
dn2 = sqrt(1 + g->ep2 * sq(sbet2));
meridian = lat1 == -qd || slam12 == 0;
if (meridian) {
/* Endpoints are on a single full meridian, so the geodesic might lie on
* a meridian. */
double ssig1, csig1, ssig2, csig2;
calp1 = clam12; salp1 = slam12; /* Head to the target longitude */
calp2 = 1; salp2 = 0; /* At the target we're heading north */
/* tan(bet) = tan(sig) * cos(alp) */
ssig1 = sbet1; csig1 = calp1 * cbet1;
ssig2 = sbet2; csig2 = calp2 * cbet2;
/* sig12 = sig2 - sig1 */
sig12 = atan2(fmax(0.0, csig1 * ssig2 - ssig1 * csig2) + 0,
csig1 * csig2 + ssig1 * ssig2);
Lengths(g, g->n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
cbet1, cbet2, &s12x, &m12x, nullptr,
(outmask & GEOD_GEODESICSCALE) ? &M12 : nullptr,
(outmask & GEOD_GEODESICSCALE) ? &M21 : nullptr,
Ca);
/* Add the check for sig12 since zero length geodesics might yield m12 <
* 0. Test case was
*
* echo 20.001 0 20.001 0 | GeodSolve -i
*
* In fact, we will have sig12 > pi/2 for meridional geodesic which is
* not a shortest path. */
if (sig12 < 1 || m12x >= 0) {
/* Need at least 2, to handle 90 0 90 180 */
if (sig12 < 3 * tiny ||
/* Prevent negative s12 or m12 for short lines */
(sig12 < tol0 && (s12x < 0 || m12x < 0)))
sig12 = m12x = s12x = 0;
m12x *= g->b;
s12x *= g->b;
a12 = sig12 / degree;
} else
/* m12 < 0, i.e., prolate and too close to anti-podal */
meridian = FALSE;
}
if (!meridian &&
sbet1 == 0 && /* and sbet2 == 0 */
/* Mimic the way Lambda12 works with calp1 = 0 */
(g->f <= 0 || lon12s >= g->f * hd)) {
/* Geodesic runs along equator */
calp1 = calp2 = 0; salp1 = salp2 = 1;
s12x = g->a * lam12;
sig12 = omg12 = lam12 / g->f1;
m12x = g->b * sin(sig12);
if (outmask & GEOD_GEODESICSCALE)
M12 = M21 = cos(sig12);
a12 = lon12 / g->f1;
} else if (!meridian) {
/* Now point1 and point2 belong within a hemisphere bounded by a
* meridian and geodesic is neither meridional or equatorial. */
/* Figure a starting point for Newton's method */
double dnm = 0;
sig12 = InverseStart(g, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
lam12, slam12, clam12,
&salp1, &calp1, &salp2, &calp2, &dnm,
Ca);
if (sig12 >= 0) {
/* Short lines (InverseStart sets salp2, calp2, dnm) */
s12x = sig12 * g->b * dnm;
m12x = sq(dnm) * g->b * sin(sig12 / dnm);
if (outmask & GEOD_GEODESICSCALE)
M12 = M21 = cos(sig12 / dnm);
a12 = sig12 / degree;
omg12 = lam12 / (g->f1 * dnm);
} else {
/* Newton's method. This is a straightforward solution of f(alp1) =
* lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
* root in the interval (0, pi) and its derivative is positive at the
* root. Thus f(alp) is positive for alp > alp1 and negative for alp <
* alp1. During the course of the iteration, a range (alp1a, alp1b) is
* maintained which brackets the root and with each evaluation of
* f(alp) the range is shrunk, if possible. Newton's method is
* restarted whenever the derivative of f is negative (because the new
* value of alp1 is then further from the solution) or if the new
* estimate of alp1 lies outside (0,pi); in this case, the new starting
* guess is taken to be (alp1a + alp1b) / 2. */
double ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, eps = 0, domg12 = 0;
unsigned numit = 0;
/* Bracketing range */
double salp1a = tiny, calp1a = 1, salp1b = tiny, calp1b = -1;
boolx tripn = FALSE;
boolx tripb = FALSE;
for (;; ++numit) {
/* the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
* WGS84 and random input: mean = 2.85, sd = 0.60 */
double dv = 0,
v = Lambda12(g, sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
slam12, clam12,
&salp2, &calp2, &sig12, &ssig1, &csig1, &ssig2, &csig2,
&eps, &domg12, numit < maxit1, &dv, Ca);
if (tripb ||
/* Reversed test to allow escape with NaNs */
!(fabs(v) >= (tripn ? 8 : 1) * tol0) ||
/* Enough bisections to get accurate result */
numit == maxit2)
break;
/* Update bracketing values */
if (v > 0 && (numit > maxit1 || calp1/salp1 > calp1b/salp1b))
{ salp1b = salp1; calp1b = calp1; }
else if (v < 0 && (numit > maxit1 || calp1/salp1 < calp1a/salp1a))
{ salp1a = salp1; calp1a = calp1; }
if (numit < maxit1 && dv > 0) {
double
dalp1 = -v/dv;
if (fabs(dalp1) < pi) {
double
sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
if (nsalp1 > 0) {
calp1 = calp1 * cdalp1 - salp1 * sdalp1;
salp1 = nsalp1;
norm2(&salp1, &calp1);
/* In some regimes we don't get quadratic convergence because
* slope -> 0. So use convergence conditions based on epsilon
* instead of sqrt(epsilon). */
tripn = fabs(v) <= 16 * tol0;
continue;
}
}
}
/* Either dv was not positive or updated value was outside legal
* range. Use the midpoint of the bracket as the next estimate.
* This mechanism is not needed for the WGS84 ellipsoid, but it does
* catch problems with more eccentric ellipsoids. Its efficacy is
* such for the WGS84 test set with the starting guess set to alp1 =
* 90deg:
* the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
* WGS84 and random input: mean = 4.74, sd = 0.99 */
salp1 = (salp1a + salp1b)/2;
calp1 = (calp1a + calp1b)/2;
norm2(&salp1, &calp1);
tripn = FALSE;
tripb = (fabs(salp1a - salp1) + (calp1a - calp1) < tolb ||
fabs(salp1 - salp1b) + (calp1 - calp1b) < tolb);
}
Lengths(g, eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
cbet1, cbet2, &s12x, &m12x, nullptr,
(outmask & GEOD_GEODESICSCALE) ? &M12 : nullptr,
(outmask & GEOD_GEODESICSCALE) ? &M21 : nullptr, Ca);
m12x *= g->b;
s12x *= g->b;
a12 = sig12 / degree;
if (outmask & GEOD_AREA) {
/* omg12 = lam12 - domg12 */
double sdomg12 = sin(domg12), cdomg12 = cos(domg12);
somg12 = slam12 * cdomg12 - clam12 * sdomg12;
comg12 = clam12 * cdomg12 + slam12 * sdomg12;
}
}
}
if (outmask & GEOD_DISTANCE)
s12 = 0 + s12x; /* Convert -0 to 0 */
if (outmask & GEOD_REDUCEDLENGTH)
m12 = 0 + m12x; /* Convert -0 to 0 */
if (outmask & GEOD_AREA) {
double
/* From Lambda12: sin(alp1) * cos(bet1) = sin(alp0) */
salp0 = salp1 * cbet1,
calp0 = hypot(calp1, salp1 * sbet1); /* calp0 > 0 */
double alp12;
if (calp0 != 0 && salp0 != 0) {
double
/* From Lambda12: tan(bet) = tan(sig) * cos(alp) */
ssig1 = sbet1, csig1 = calp1 * cbet1,
ssig2 = sbet2, csig2 = calp2 * cbet2,
k2 = sq(calp0) * g->ep2,
eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2),
/* Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0). */
A4 = sq(g->a) * calp0 * salp0 * g->e2;
double B41, B42;
norm2(&ssig1, &csig1);
norm2(&ssig2, &csig2);
C4f(g, eps, Ca);
B41 = SinCosSeries(FALSE, ssig1, csig1, Ca, nC4);
B42 = SinCosSeries(FALSE, ssig2, csig2, Ca, nC4);
S12 = A4 * (B42 - B41);
} else
/* Avoid problems with indeterminate sig1, sig2 on equator */
S12 = 0;
if (!meridian && somg12 == 2) {
somg12 = sin(omg12); comg12 = cos(omg12);
}
if (!meridian &&
/* omg12 < 3/4 * pi */
comg12 > -0.7071 && /* Long difference not too big */
sbet2 - sbet1 < 1.75) { /* Lat difference not too big */
/* Use tan(Gamma/2) = tan(omg12/2)
* * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
* with tan(x/2) = sin(x)/(1+cos(x)) */
double
domg12 = 1 + comg12, dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
} else {
/* alp12 = alp2 - alp1, used in atan2 so no need to normalize */
double