-
Notifications
You must be signed in to change notification settings - Fork 5
/
geodesic.go
2230 lines (2062 loc) · 59.1 KB
/
geodesic.go
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
// API for the geodesic routines in Go
//
// This an implementation in Go of the geodesic algorithms described in
// - C. F. F. Karney, Algorithms for geodesics,
// J. Geodesy 87, 43--55 (2013);
// DOI: 10.1007/s00190-012-0578-z;
// addenda: https://geographiclib.sourceforge.io/geod-addenda.html;
// link: https://doi.org/10.1007/s00190-012-0578-z;
//
// Copyright (c) Charles Karney (2012-2021) <charles@karney.com> and licensed
// under the MIT/X11 License. For more information, see
// https://geographiclib.sourceforge.io/
//
// Ported to Go by Joshua Baker <joshbaker77@gmail.com> and licensed
// under the MIT License.
package geodesy
import "math"
const (
pi = float64(math.Pi)
degree = pi / 180
epsilon = float64(7.)/3 - float64(4.)/3 - float64(1.)
digits = 53
geographicLibGeodesicOrder = 6
nA1 = geographicLibGeodesicOrder
nC1 = geographicLibGeodesicOrder
nC1p = geographicLibGeodesicOrder
nA2 = geographicLibGeodesicOrder
nC2 = geographicLibGeodesicOrder
nA3 = geographicLibGeodesicOrder
nA3x = nA3
nC3 = geographicLibGeodesicOrder
nC3x = ((nC3 * (nC3 - 1)) / 2)
nC4 = geographicLibGeodesicOrder
nC4x = ((nC4 * (nC4 + 1)) / 2)
nC = (geographicLibGeodesicOrder + 1)
tol0 = epsilon
realmin = math.SmallestNonzeroFloat64
)
const (
geodNone = 0 /**< Calculate nothing */
geodLatitude = 1 << 7 /**< Calculate latitude */
geodLongitude = 1<<8 | 1<<3 /**< Calculate longitude */
geodAzimuth = 1 << 9 /**< Calculate azimuth */
geosDistance = 1<<10 | 1<<0 /**< Calculate distance */
geodDistanceIn = 1<<11 | 1<<0 | 1<<1 /**< Allow distance as input */
geodReducedLength = 1<<12 | 1<<0 | 1<<2 /**< Calculate reduced length */
geodGeodesicScale = 1<<13 | 1<<0 | 1<<2 /**< Calculate geodesic scale */
geodArea = 1<<14 | 1<<4 /**< Calculate reduced length */
geodAll = 0x7F80 | 0x1F /**< Calculate everything */
)
/**
* flag values for the \e flags argument to geod_gendirect() and
* geod_genposition()
**********************************************************************/
const (
geodNoFlags = 0 /**< No flags */
geodArcMode = 1 << 0 /**< Position given in terms of arc distance */
geodLongUnroll = 1 << 15 /**< Unroll the longitude */
)
const (
capNone = 0
capC1 = 1 << 0
capC1p = 1 << 1
capC2 = 1 << 2
capC3 = 1 << 3
capC4 = 1 << 4
capAll = 0x1F
outAll = 0x7F80
)
var (
tiny = sqrt(realmin)
tol1 = 200 * tol0
tol2 = sqrt(tol0)
xthresh = 1000 * tol2
maxit1 uint = 20
maxit2 uint = maxit1 + digits + 10
tolb = tol0 * tol2
)
type geodGeodesic struct {
a float64 /**< the equatorial radius */
f float64 /**< the flattening */
/**< @cond SKIP */
f1, e2, ep2, n, b, c2, etol2 float64
A3x [6]float64
C3x [15]float64
C4x [21]float64
/**< @endcond */
}
func geodInverse(g *geodGeodesic,
lat1 float64, lon1 float64,
lat2 float64, lon2 float64,
ps12 *float64, pazi1 *float64, pazi2 *float64) {
geodGenInverse(g, lat1, lon1, lat2, lon2, ps12, pazi1, pazi2,
nil, nil, nil, nil)
}
func geodGenInverse(g *geodGeodesic,
lat1 float64, lon1 float64, lat2 float64, lon2 float64,
ps12 *float64, pazi1 *float64, pazi2 *float64,
pm12 *float64, pM12 *float64, pM21 *float64, pS12 *float64,
) float64 {
var salp1, calp1, salp2, calp2 float64
a12 := geodGenInverseInt(g, lat1, lon1, lat2, lon2, ps12,
&salp1, &calp1, &salp2, &calp2,
pm12, pM12, pM21, pS12)
if pazi1 != nil {
*pazi1 = atan2dx(salp1, calp1)
}
if pazi2 != nil {
*pazi2 = atan2dx(salp2, calp2)
}
return a12
}
func atan2dx(y float64, x float64) float64 {
/* 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. */
var q = 0
var ang float64
if fabs(y) > fabs(x) {
x, y = y, x
q = 2
}
if x < 0 {
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 {
/* Note that atan2d(-0.0, 1.0) will return -0. However, we expect that
* atan2d will not be called with y = -0. If need be, include
*
* case 0: ang = 0 + ang; break;
*/
case 1:
var v float64
if y >= 0 {
v = 180
} else {
v = -180
}
ang = v - ang
case 2:
ang = 90 - ang
case 3:
ang = -90 + ang
}
return ang
}
func geodGenInverseInt(g *geodGeodesic,
lat1 float64, lon1 float64, lat2 float64, lon2 float64,
ps12 *float64,
psalp1 *float64, pcalp1 *float64,
psalp2 *float64, pcalp2 *float64,
pm12 *float64, pM12 *float64, pM21 *float64,
pS12 *float64,
) float64 {
var s12, m12, M12, M21, S12 float64
var lon12, lon12s float64
var latsign, lonsign, swapp int
var sbet1, cbet1, sbet2, cbet2, s12x, m12x float64
var dn1, dn2, lam12, slam12, clam12 float64
var a12, sig12, calp1, salp1, calp2, salp2 float64
var Ca [nC]float64
var meridian bool
/* somg12 > 1 marks that it needs to be calculated */
var omg12, somg12, comg12 float64
somg12 = 2
var outmask uint
if ps12 != nil {
outmask |= geosDistance
}
if pm12 != nil {
outmask |= geodReducedLength
}
if pM12 != nil || pM21 != nil {
outmask |= geodGeodesicScale
}
if pS12 != nil {
outmask |= geodArea
}
outmask &= outAll
/* 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. */
if lon12 >= 0 {
lonsign = 1
} else {
lonsign = -1
}
/* If very close to being on the same half-meridian, then make it so. */
lon12 = float64(lonsign) * angRound(lon12)
lon12s = angRound((180 - lon12) - float64(lonsign)*lon12s)
lam12 = lon12 * degree
if lon12 > 90 {
sincosdx(lon12s, &slam12, &clam12)
clam12 = -clam12
} else {
sincosdx(lon12, &slam12, &clam12)
}
/* 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. */
if fabs(lat1) < fabs(lat2) {
swapp = -1
} else {
swapp = 1
}
if swapp < 0 {
lonsign *= -1
lat1, lat2 = lat2, lat1
}
/* Make lat1 <= 0 */
if lat1 < 0 {
latsign = 1
} else {
latsign = -1
}
lat1 *= float64(latsign)
lat2 *= float64(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 = maxx(tiny, cbet1)
sincosdx(lat2, &sbet2, &cbet2)
sbet2 *= g.f1
/* Ensure cbet2 = +epsilon at poles */
norm2(&sbet2, &cbet2)
cbet2 = maxx(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 {
if sbet2 < 0 {
sbet2 = sbet1
} else {
sbet2 = -sbet1
}
}
} else {
if fabs(sbet2) == -sbet1 {
cbet2 = cbet1
}
}
dn1 = sqrt(1 + g.ep2*sq(sbet1))
dn2 = sqrt(1 + g.ep2*sq(sbet2))
meridian = lat1 == -90 || slam12 == 0
if meridian {
/* Endpoints are on a single full meridian, so the geodesic might lie on
* a meridian. */
var ssig1, csig1, ssig2, csig2 float64
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(maxx(0, csig1*ssig2-ssig1*csig2),
csig1*csig2+ssig1*ssig2)
var a, b *float64
if outmask&geodGeodesicScale != 0 {
a = &M12
b = &M21
}
lengths(g, g.n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
cbet1, cbet2, &s12x, &m12x, nil, a, b, 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, 0, 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*180) {
/* Geodesic runs along equator */
calp1, calp2 = 0, 0
salp1, salp2 = 1, 1
s12x = g.a * lam12
omg12 = lam12 / g.f1
sig12 = omg12
m12x = g.b * sin(sig12)
if outmask&geodGeodesicScale != 0 {
M21 = cos(sig12)
M12 = M21
}
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 */
var dnm float64
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&geodGeodesicScale != 0 {
M21 = cos(sig12 / dnm)
M12 = M21
}
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. */
var ssig1, csig1, ssig2, csig2, eps, domg12 float64
var numit uint
/* Bracketing range */
var salp1a, calp1a, salp1b, calp1b = tiny, 1.0, tiny, -1.0
var tripn bool
var tripb bool
for ; numit < maxit2; numit++ {
/* the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
* WGS84 and random input: mean = 2.85, sd = 0.60 */
var dv float64
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[:])
/* Reversed test to allow escape with NaNs */
var vv float64
if tripn {
vv = 8
} else {
vv = 1
}
if tripb || !(fabs(v) >= vv*tol0) {
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 {
dalp1 := -v / dv
sdalp1, cdalp1 := sincos(dalp1)
nsalp1 := salp1*cdalp1 + calp1*sdalp1
if nsalp1 > 0 && fabs(dalp1) < pi {
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)
}
var v1 *float64
var v2 *float64
if outmask&geodGeodesicScale != 0 {
v1 = &M12
v2 = &M21
}
lengths(g, eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
cbet1, cbet2, &s12x, &m12x, nil,
v1,
v2, Ca[:])
m12x *= g.b
s12x *= g.b
a12 = sig12 / degree
if outmask&geodArea != 0 {
/* omg12 = lam12 - domg12 */
sdomg12, cdomg12 := sincos(domg12)
somg12 = slam12*cdomg12 - clam12*sdomg12
comg12 = clam12*cdomg12 + slam12*sdomg12
}
}
}
if outmask&geosDistance != 0 {
s12 = 0 + s12x /* Convert -0 to 0 */
}
if outmask&geodReducedLength != 0 {
m12 = 0 + m12x /* Convert -0 to 0 */
}
if outmask&geodArea != 0 {
/* From Lambda12: sin(alp1) * cos(bet1) = sin(alp0) */
salp0 := salp1 * cbet1
calp0 := hypot(calp1, salp1*sbet1) /* calp0 > 0 */
var alp12 float64
if calp0 != 0 && salp0 != 0 {
/* 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
var B41, B42 float64
norm2(&ssig1, &csig1)
norm2(&ssig2, &csig2)
c4f(g, eps, Ca[:])
B41 = sinCosSeries(iFalse, ssig1, csig1, Ca[:], nC4)
B42 = sinCosSeries(iFalse, ssig2, csig2, Ca[:], nC4)
S12 = A4 * (B42 - B41)
} else {
/* Avoid problems with indeterminate sig1, sig2 on equator */
S12 = 0
}
if !meridian && somg12 > 1 {
somg12, comg12 = sincos(omg12)
}
if !meridian &&
/* omg12 < 3/4 * pi */
comg12 > -(real)(0.7071) && /* Long difference not too big */
sbet2-sbet1 < (real)(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)) */
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 */
salp12 := salp2*calp1 - calp2*salp1
calp12 := calp2*calp1 + salp2*salp1
/* The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
* salp12 = -0 and alp12 = -180. However this depends on the sign
* being attached to 0 correctly. The following ensures the correct
* behavior. */
if salp12 == 0 && calp12 < 0 {
salp12 = tiny * calp1
calp12 = -1
}
alp12 = atan2(salp12, calp12)
}
S12 += g.c2 * alp12
S12 *= float64(swapp * lonsign * latsign)
/* Convert -0 to 0 */
S12 += 0
}
/* Convert calp, salp to azimuth accounting for lonsign, swapp, latsign. */
if swapp < 0 {
salp1, salp2 = salp2, salp1
calp1, calp2 = calp2, calp1
if outmask&geodGeodesicScale != 0 {
M12, M21 = M21, M12
}
}
salp1 *= float64(swapp * lonsign)
calp1 *= float64(swapp * latsign)
salp2 *= float64(swapp * lonsign)
calp2 *= float64(swapp * latsign)
if psalp1 != nil {
*psalp1 = salp1
}
if pcalp1 != nil {
*pcalp1 = calp1
}
if psalp2 != nil {
*psalp2 = salp2
}
if pcalp2 != nil {
*pcalp2 = calp2
}
if outmask&geosDistance != 0 {
*ps12 = s12
}
if outmask&geodReducedLength != 0 {
*pm12 = m12
}
if outmask&geodGeodesicScale != 0 {
if pM12 != nil {
*pM12 = M12
}
if pM21 != nil {
*pM21 = M21
}
}
if outmask&geodArea != 0 {
*pS12 = S12
}
/* Returned value in [0, 180] */
return a12
}
func angDiff(x float64, y float64, e *float64) float64 {
var t float64
var d = angNormalize(sumx(angNormalize(-x), angNormalize(y), &t))
/* Here y - x = d + t (mod 360), exactly, where d is in (-180,180] and
* abs(t) <= eps (eps = 2^-45 for doubles). The only case where the
* addition of t takes the result outside the range (-180,180] is d = 180
* and t > 0. The case, d = -180 + eps, t = -eps, can't happen, since
* sum would have returned the exact result in such a case (i.e., given t
* = 0). */
var v float64
if d == 180 && t > 0 {
v = -180
} else {
v = d
}
return sumx(v, t, e)
}
func sumx(u float64, v float64, t *float64) float64 {
var s = u + v
var up = s - v
var vpp = s - up
up -= u
vpp -= v
if t != nil {
*t = -(up + vpp)
}
/* error-free sum:
* u + v = s + t
* = round(u + v) + t */
return s
}
func angNormalize(x float64) float64 {
x = remainder(x, 360)
if x != -180 {
return x
}
return 180
}
func angRound(x float64) float64 {
const z = 1.0 / 16.0
var y float64
if x == 0 {
return 0
}
y = fabs(x)
/* The compiler mustn't "simplify" z - (z - y) to y */
if y < z {
y = z - (z - y)
}
if x < 0 {
return -y
}
return y
}
func remquo(x float64, y float64, q *float64) float64 {
*q = x / y
return mod(x, y)
}
func sincosdx(x float64, sinx *float64, cosx *float64) {
/* In order to minimize round-off errors, this function exactly reduces
* the argument to the range [-45, 45] before converting it to radians. */
var r, s, c float64
var q float64
r = remquo(x, 90, &q)
/* now abs(r) <= 45 */
r *= degree
/* Possibly could call the gnu extension sincos */
s, c = sincos(r)
switch uint(q) & 3 {
case 0:
*sinx = s
*cosx = c
case 1:
*sinx = c
*cosx = -s
case 2:
*sinx = -s
*cosx = -c
default:
*sinx = -c
*cosx = s
/* case 3U */
}
if x != 0 {
*sinx += 0
*cosx += 0
}
}
func latFix(x float64) float64 {
if fabs(x) > 90 {
return math.NaN()
}
return x
}
func norm2(sinx *float64, cosx *float64) {
r := hypot(*sinx, *cosx)
*sinx /= r
*cosx /= r
}
func sq(x float64) float64 { return x * x }
func lengths(g *geodGeodesic,
eps float64, sig12 float64,
ssig1 float64, csig1 float64, dn1 float64,
ssig2 float64, csig2 float64, dn2 float64,
cbet1 float64, cbet2 float64,
ps12b *float64, pm12b *float64, pm0 *float64,
pM12 *float64, pM21 *float64,
/* Scratch area of the right size */
Ca []float64,
) {
var m0, J12, A1, A2 float64
var Cb [nC]float64
/* Return m12b = (reduced length)/b; also calculate s12b = distance/b,
* and m0 = coefficient of secular term in expression for reduced length. */
redlp := pm12b != nil || pm0 != nil || pM12 != nil || pM21 != nil
if ps12b != nil || redlp {
A1 = a1m1f(eps)
c1f(eps, Ca)
if redlp {
A2 = a2m1f(eps)
c2f(eps, Cb[:])
m0 = A1 - A2
A2 = 1 + A2
}
A1 = 1 + A1
}
if ps12b != nil {
B1 := sinCosSeries(iTrue, ssig2, csig2, Ca, nC1) -
sinCosSeries(iTrue, ssig1, csig1, Ca, nC1)
/* Missing a factor of b */
*ps12b = A1 * (sig12 + B1)
if redlp {
B2 := sinCosSeries(iTrue, ssig2, csig2, Cb[:], nC2) -
sinCosSeries(iTrue, ssig1, csig1, Cb[:], nC2)
J12 = m0*sig12 + (A1*B1 - A2*B2)
}
} else if redlp {
/* Assume here that nC1 >= nC2 */
var l int
for l = 1; l <= nC2; l++ {
Cb[l] = A1*Ca[l] - A2*Cb[l]
}
J12 = m0*sig12 + (sinCosSeries(iTrue, ssig2, csig2, Cb[:], nC2) -
sinCosSeries(iTrue, ssig1, csig1, Cb[:], nC2))
}
if pm0 != nil {
*pm0 = m0
}
if pm12b != nil {
/* Missing a factor of b.
* Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure
* accurate cancellation in the case of coincident points. */
*pm12b = dn2*(csig1*ssig2) - dn1*(ssig1*csig2) -
csig1*csig2*J12
}
if pM12 != nil || pM21 != nil {
csig12 := csig1*csig2 + ssig1*ssig2
t := g.ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2)
if pM12 != nil {
*pM12 = csig12 + (t*ssig2-csig2*J12)*ssig1/dn1
}
if pM21 != nil {
*pM21 = csig12 - (t*ssig1-csig1*J12)*ssig2/dn2
}
}
}
/* The scale factor A1-1 = mean value of (d/dsigma)I1 - 1 */
func a1m1f(eps float64) float64 {
var coeff = [...]float64{
/* (1-eps)*A1-1, polynomial in eps2 of order 3 */
1, 4, 64, 0, 256,
}
m := nA1 / 2
t := polyval(m, coeff[:], sq(eps)) / coeff[m+1]
return (t + eps) / (1 - eps)
}
func polyval(N int, p []float64, x float64) (y float64) {
for i := 0; i <= N; i++ {
y = y*x + p[i]
}
return y
}
var coeffC1f = [...]float64{
/* C1[1]/eps^1, polynomial in eps2 of order 2 */
-1, 6, -16, 32,
/* C1[2]/eps^2, polynomial in eps2 of order 2 */
-9, 64, -128, 2048,
/* C1[3]/eps^3, polynomial in eps2 of order 1 */
9, -16, 768,
/* C1[4]/eps^4, polynomial in eps2 of order 1 */
3, -5, 512,
/* C1[5]/eps^5, polynomial in eps2 of order 0 */
-7, 1280,
/* C1[6]/eps^6, polynomial in eps2 of order 0 */
-7, 2048,
}
/* The coefficients C1[l] in the Fourier expansion of B1 */
func c1f(eps float64, c []float64) {
var eps2 = sq(eps)
var d = eps
var o int
for l := 1; l <= nC1; l++ { /* l is index of C1p[l] */
m := (nC1 - l) / 2 /* order of polynomial in eps^2 */
c[l] = d * polyval(m, coeffC1f[o:], eps2) / coeffC1f[o+m+1]
o += m + 2
d *= eps
}
}
/* The scale factor A2-1 = mean value of (d/dsigma)I2 - 1 */
func a2m1f(eps float64) float64 {
var coeff = [...]float64{
/* (eps+1)*A2-1, polynomial in eps2 of order 3 */
-11, -28, -192, 0, 256,
}
m := nA2 / 2
t := polyval(m, coeff[:], sq(eps)) / coeff[m+1]
return (t - eps) / (1 + eps)
}
var coeffC2f = [...]float64{
/* C2[1]/eps^1, polynomial in eps2 of order 2 */
1, 2, 16, 32,
/* C2[2]/eps^2, polynomial in eps2 of order 2 */
35, 64, 384, 2048,
/* C2[3]/eps^3, polynomial in eps2 of order 1 */
15, 80, 768,
/* C2[4]/eps^4, polynomial in eps2 of order 1 */
7, 35, 512,
/* C2[5]/eps^5, polynomial in eps2 of order 0 */
63, 1280,
/* C2[6]/eps^6, polynomial in eps2 of order 0 */
77, 2048,
}
/* The coefficients C2[l] in the Fourier expansion of B2 */
func c2f(eps float64, c []float64) {
var eps2 = eps * eps //sq(eps)
var d = eps
var o int
for l := 1; l <= nC2; l++ { /* l is index of C2[l] */
m := (nC2 - l) / 2 /* order of polynomial in eps^2 */
c[l] = d * polyval(m, coeffC2f[o:], eps2) / coeffC2f[o+m+1]
o += m + 2
d *= eps
}
}
type ibool int
const (
iFalse ibool = 0
iTrue ibool = 1
)
func sinCosSeries(sinpb ibool, sinx, cosx float64, c []float64, n int) float64 {
/* Evaluate
* y = sinp ? sum(c[i] * sin( 2*i * x), i, 1, n) :
* sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
* using Clenshaw summation. N.B. c[0] is unused for sin series
* Approx operation count = (n + 5) mult and (2 * n + 2) add */
ci := n + int(sinpb) /* Point to one beyond last element */
ar := 2 * (cosx - sinx) * (cosx + sinx) /* 2 * cos(2 * x) */
y0 := 0.0
if n&1 != 0 {
ci--
y0 = c[0]
}
y1 := 0.0 /* accumulators for sum */
/* Now n is even */
n /= 2
for n != 0 {
n--
/* Unroll loop x 2, so accumulators return to their original role */
ci--
y1 = ar*y0 - y1 + c[ci]
ci--
y0 = ar*y1 - y0 + c[ci]
}
if sinpb == iTrue {
return 2 * sinx * cosx * y0 /* sin(2 * x) * y0 */
}
return cosx * (y0 - y1) /* cos(x) * (y0 - y1) */
}
func inverseStart(g *geodGeodesic,
sbet1 float64, cbet1 float64, dn1 float64,
sbet2 float64, cbet2 float64, dn2 float64,
lam12 float64, slam12 float64, clam12 float64,
psalp1 *float64, pcalp1 *float64,
/* Only updated if return val >= 0 */
psalp2 *float64, pcalp2 *float64,
/* Only updated for short lines */
pdnm *float64,
/* Scratch area of the right size */
Ca []float64,
) float64 {
var salp1, calp1, salp2, calp2, dnm float64
/* Return a starting point for Newton's method in salp1 and calp1 (function
* value is -1). If Newton's method doesn't need to be used, return also
* salp2 and calp2 and function value is sig12. */
var sig12 = -1.0 /* Return value */
/* bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0] */
var sbet12 = sbet2*cbet1 - cbet2*sbet1
var cbet12 = cbet2*cbet1 + sbet2*sbet1
var sbet12a float64
var shortline = cbet12 >= 0 && sbet12 < 0.5 &&
cbet2*lam12 < 0.5
var somg12, comg12, ssig12, csig12 float64
sbet12a = sbet2*cbet1 + cbet2*sbet1
if shortline {
var sbetm2 = sq(sbet1 + sbet2)
var omg12 float64
/* sin((bet1+bet2)/2)^2
* = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2) */
sbetm2 /= sbetm2 + sq(cbet1+cbet2)
dnm = sqrt(1 + g.ep2*sbetm2)
omg12 = lam12 / (g.f1 * dnm)
somg12, comg12 = sincos(omg12)
} else {
somg12 = slam12
comg12 = clam12
}
salp1 = cbet2 * somg12
if comg12 >= 0 {
calp1 = sbet12 + cbet2*sbet1*sq(somg12)/(1+comg12)
} else {
calp1 = sbet12a - cbet2*sbet1*sq(somg12)/(1-comg12)
}
ssig12 = hypot(salp1, calp1)
csig12 = sbet1*sbet2 + cbet1*cbet2*comg12
if shortline && ssig12 < g.etol2 {
// /* really short lines */
salp2 = cbet1 * somg12
var v float64
if comg12 >= 0 {
v = sq(somg12) / (1 + comg12)
} else {
v = 1 - comg12
}
calp2 = sbet12 - cbet1*sbet2*v
norm2(&salp2, &calp2)
/* Set return value */
sig12 = atan2(ssig12, csig12)
} else if fabs(g.n) > 0.1 || /* No astroid calc if too eccentric */
csig12 >= 0 ||
ssig12 >= 6*fabs(g.n)*pi*sq(cbet1) {
/* Nothing to do, zeroth order spherical approximation is OK */
} else {
/* Scale lam12 and bet2 to x, y coordinate system where antipodal point
* is at origin and singular point is at y = 0, x = -1. */
var y, lamscale, betscale float64
/* Volatile declaration needed to fix inverse case
* 56.320923501171 0 -56.320923501171 179.664747671772880215
* which otherwise fails with g++ 4.4.4 x86 -O3 */
var x float64
lam12x := atan2(-slam12, -clam12) /* lam12 - pi */
if g.f >= 0 { /* In fact f == 0 does not get here */
/* x = dlong, y = dlat */
{
k2 := sq(sbet1) * g.ep2
eps := k2 / (2*(1+sqrt(1+k2)) + k2)
lamscale = g.f * cbet1 * a3f(g, eps) * pi
}
betscale = lamscale * cbet1
x = lam12x / lamscale
y = sbet12a / betscale
} else { /* f < 0 */
/* x = dlat, y = dlong */
cbet12a := cbet2*cbet1 - sbet2*sbet1
bet12a := atan2(sbet12a, cbet12a)
var m12b, m0 float64
/* In the case of lon12 = 180, this repeats a calculation made in
* Inverse. */
lengths(g, g.n, pi+bet12a,
sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
cbet1, cbet2, nil, &m12b, &m0, nil, nil, Ca)