@ac-kit/math-geodesy 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/LICENSE +57 -0
  3. package/README.md +89 -2
  4. package/dist/_brand.d.ts +14 -0
  5. package/dist/_brand.d.ts.map +1 -0
  6. package/dist/_brand.js +2 -0
  7. package/dist/_brand.js.map +1 -0
  8. package/dist/_ecef-forward-bound.d.ts +7 -0
  9. package/dist/_ecef-forward-bound.d.ts.map +1 -0
  10. package/dist/_ecef-forward-bound.js +7 -0
  11. package/dist/_ecef-forward-bound.js.map +1 -0
  12. package/dist/_ecef-height.d.ts +31 -0
  13. package/dist/_ecef-height.d.ts.map +1 -0
  14. package/dist/_ecef-height.js +99 -0
  15. package/dist/_ecef-height.js.map +1 -0
  16. package/dist/ellipsoid.d.ts +52 -0
  17. package/dist/ellipsoid.d.ts.map +1 -0
  18. package/dist/ellipsoid.js +75 -0
  19. package/dist/ellipsoid.js.map +1 -0
  20. package/dist/geodesic/_angle.d.ts +51 -0
  21. package/dist/geodesic/_angle.d.ts.map +1 -0
  22. package/dist/geodesic/_angle.js +129 -0
  23. package/dist/geodesic/_angle.js.map +1 -0
  24. package/dist/geodesic/_constants.d.ts +38 -0
  25. package/dist/geodesic/_constants.d.ts.map +1 -0
  26. package/dist/geodesic/_constants.js +141 -0
  27. package/dist/geodesic/_constants.js.map +1 -0
  28. package/dist/geodesic/_inverse.d.ts +22 -0
  29. package/dist/geodesic/_inverse.d.ts.map +1 -0
  30. package/dist/geodesic/_inverse.js +714 -0
  31. package/dist/geodesic/_inverse.js.map +1 -0
  32. package/dist/geodesic/_line.d.ts +38 -0
  33. package/dist/geodesic/_line.d.ts.map +1 -0
  34. package/dist/geodesic/_line.js +214 -0
  35. package/dist/geodesic/_line.js.map +1 -0
  36. package/dist/geodesic/_series.d.ts +69 -0
  37. package/dist/geodesic/_series.d.ts.map +1 -0
  38. package/dist/geodesic/_series.js +203 -0
  39. package/dist/geodesic/_series.js.map +1 -0
  40. package/dist/geodesic.d.ts +84 -0
  41. package/dist/geodesic.d.ts.map +1 -0
  42. package/dist/geodesic.js +197 -0
  43. package/dist/geodesic.js.map +1 -0
  44. package/dist/geodetic.d.ts +194 -0
  45. package/dist/geodetic.d.ts.map +1 -0
  46. package/dist/geodetic.js +430 -0
  47. package/dist/geodetic.js.map +1 -0
  48. package/dist/index.d.ts +5 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +5 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/local-frame.d.ts +19 -0
  53. package/dist/local-frame.d.ts.map +1 -0
  54. package/dist/local-frame.js +35 -0
  55. package/dist/local-frame.js.map +1 -0
  56. package/package.json +72 -3
@@ -0,0 +1,714 @@
1
+ /*! Derived from GeographicLib Geodesic.js (geographiclib-geodesic 2.2.0), Copyright (c) 2011-2022, Charles Karney, MIT License — see LICENSE. */
2
+ // The inverse problem (Karney 2013, §4): `Lengths`, `Astroid`, `InverseStart`,
3
+ // `Lambda12` and `GenInverse` of the upstream file, restricted to the distance
4
+ // and the azimuths. Upstream's symbols are spelled out: beta is the reduced
5
+ // latitude (`bet`), alpha the azimuth (`alp`), sigma the arc on the auxiliary
6
+ // sphere (`sig`), omega the longitude on it (`omg`), lambda the longitude
7
+ // difference (`lam`), "scale" the `dn` factor, `ellipsoid constants` the
8
+ // object's fields. Results and the arguments of the large functions go through
9
+ // module-level buffers, so a call allocates no object (V8 still boxes a few
10
+ // heap numbers for locals).
11
+ import { copySign, DEG_TO_RAD, sinCosDegInPlace } from "@ac-kit/math-scalar";
12
+ import { angleDifference_, angleRound_, scratchCreate_, sineCosineCorrected_, TINY_, TINY_DOUBLE_, } from "./_angle.js";
13
+ import { CONSTANT_EQUATORIAL_RADIUS_, CONSTANT_FLATTENING_, CONSTANT_POLAR_RADIUS_, CONSTANT_POLAR_RATIO_, CONSTANT_SECOND_ECCENTRICITY_SQUARED_, CONSTANT_SHORT_ARC_, CONSTANT_THIRD_FLATTENING_, SERIES_ORDER_, } from "./_constants.js";
14
+ import { a1Minus1_, a2Minus1_, a3_, c1_, c2_, c3_, SERIES_COS2_, SERIES_COS_, SERIES_DIFFERENCE_, SERIES_EPSILON_, SERIES_MEAN_, SERIES_SIN2_, SERIES_SIN_, SERIES_SLOTS_, seriesRegisters_, sinSeriesDifference_, } from "./_series.js";
15
+ const MAX_NEWTON_ITERATIONS_ = 20;
16
+ const MAX_BISECTION_ITERATIONS_ = MAX_NEWTON_ITERATIONS_ + 53 + 10;
17
+ const TOLERANCE_ = Number.EPSILON;
18
+ const TOLERANCE_COARSE_ = 200 * TOLERANCE_;
19
+ const TOLERANCE_SQRT_ = Math.sqrt(TOLERANCE_);
20
+ const TOLERANCE_X_ = 1000 * TOLERANCE_SQRT_;
21
+ // `Math.hypot` is wrong for some GeodSolve cases and slow; the operands here
22
+ // are sines and cosines, so the plain form is exact enough (plan S12).
23
+ function hypot_(x, y) {
24
+ return Math.sqrt(x * x + y * y);
25
+ }
26
+ const c1Scratch_ = scratchCreate_(SERIES_SLOTS_);
27
+ const c2Scratch_ = scratchCreate_(SERIES_SLOTS_);
28
+ const c3Scratch_ = scratchCreate_(SERIES_ORDER_);
29
+ const angleScratch_ = scratchCreate_(2);
30
+ // A call that passes a double to a function V8 does not inline boxes it in a
31
+ // heap number, so the arguments of the large functions below travel in
32
+ // module-level buffers instead ("registers"), which the callee reads first.
33
+ const LENGTHS_EPSILON_ = 0;
34
+ const LENGTHS_SIGMA12_ = 1;
35
+ const LENGTHS_SIN_SIGMA1_ = 2;
36
+ const LENGTHS_COS_SIGMA1_ = 3;
37
+ const LENGTHS_SCALE1_ = 4;
38
+ const LENGTHS_SIN_SIGMA2_ = 5;
39
+ const LENGTHS_COS_SIGMA2_ = 6;
40
+ const LENGTHS_SCALE2_ = 7;
41
+ const lengthsInput_ = scratchCreate_(8);
42
+ const LENGTH_DISTANCE_ = 0;
43
+ const LENGTH_REDUCED_ = 1;
44
+ const LENGTH_COEFFICIENT_ = 2;
45
+ const lengthsScratch_ = scratchCreate_(3);
46
+ /**
47
+ * The distance divided by `b` (`lengthsScratch_[0]`, when `wantDistance`) and
48
+ * the reduced length divided by `b` and the secular coefficient of it (`[1]`
49
+ * and `[2]`, when `wantReduced`) between the arcs `sigma1` and `sigma2` of the
50
+ * auxiliary sphere, whose parameters are in `lengthsInput_`.
51
+ */
52
+ function lengths_(wantDistance, wantReduced) {
53
+ const epsilon = lengthsInput_[LENGTHS_EPSILON_];
54
+ const sigma12 = lengthsInput_[LENGTHS_SIGMA12_];
55
+ const sinSigma1 = lengthsInput_[LENGTHS_SIN_SIGMA1_];
56
+ const cosSigma1 = lengthsInput_[LENGTHS_COS_SIGMA1_];
57
+ const scale1 = lengthsInput_[LENGTHS_SCALE1_];
58
+ const sinSigma2 = lengthsInput_[LENGTHS_SIN_SIGMA2_];
59
+ const cosSigma2 = lengthsInput_[LENGTHS_COS_SIGMA2_];
60
+ const scale2 = lengthsInput_[LENGTHS_SCALE2_];
61
+ let secular = Number.NaN;
62
+ let jacobi = Number.NaN;
63
+ let a2 = Number.NaN;
64
+ seriesRegisters_[SERIES_EPSILON_] = epsilon;
65
+ a1Minus1_();
66
+ let a1 = seriesRegisters_[SERIES_MEAN_];
67
+ c1_(c1Scratch_, 0);
68
+ if (wantReduced) {
69
+ a2Minus1_();
70
+ a2 = seriesRegisters_[SERIES_MEAN_];
71
+ c2_(c2Scratch_, 0);
72
+ secular = a1 - a2;
73
+ a2 = 1 + a2;
74
+ }
75
+ a1 = 1 + a1;
76
+ seriesRegisters_[SERIES_SIN_] = sinSigma2;
77
+ seriesRegisters_[SERIES_COS_] = cosSigma2;
78
+ seriesRegisters_[SERIES_SIN2_] = sinSigma1;
79
+ seriesRegisters_[SERIES_COS2_] = cosSigma1;
80
+ if (wantDistance) {
81
+ sinSeriesDifference_(c1Scratch_, 0, SERIES_SLOTS_);
82
+ const b1 = seriesRegisters_[SERIES_DIFFERENCE_];
83
+ lengthsScratch_[LENGTH_DISTANCE_] = a1 * (sigma12 + b1);
84
+ if (wantReduced) {
85
+ sinSeriesDifference_(c2Scratch_, 0, SERIES_SLOTS_);
86
+ const b2 = seriesRegisters_[SERIES_DIFFERENCE_];
87
+ jacobi = secular * sigma12 + (a1 * b1 - a2 * b2);
88
+ }
89
+ }
90
+ else if (wantReduced) {
91
+ for (let order = 1; order <= SERIES_ORDER_; order++) {
92
+ c2Scratch_[order] = a1 * c1Scratch_[order] - a2 * c2Scratch_[order];
93
+ }
94
+ sinSeriesDifference_(c2Scratch_, 0, SERIES_SLOTS_);
95
+ jacobi = secular * sigma12 + seriesRegisters_[SERIES_DIFFERENCE_];
96
+ }
97
+ if (wantReduced) {
98
+ lengthsScratch_[LENGTH_COEFFICIENT_] = secular;
99
+ // The parentheses keep the cancellation accurate for coincident points.
100
+ lengthsScratch_[LENGTH_REDUCED_] =
101
+ scale2 * (cosSigma1 * sinSigma2) -
102
+ scale1 * (sinSigma1 * cosSigma2) -
103
+ cosSigma1 * cosSigma2 * jacobi;
104
+ }
105
+ else {
106
+ lengthsScratch_[LENGTH_REDUCED_] = Number.NaN;
107
+ }
108
+ }
109
+ const ASTROID_X_ = 0;
110
+ const ASTROID_Y_ = 1;
111
+ const ASTROID_ROOT_ = 2;
112
+ const astroidRegisters_ = scratchCreate_(3);
113
+ /**
114
+ * The positive root of `k⁴ + 2k³ − (x² + y² − 1)k² − 2y²k − y² = 0`, the
115
+ * astroid of the nearly antipodal problem (adapted from Karney's geocentric
116
+ * conversion), for `x` and `y` in `astroidRegisters_`, into it.
117
+ */
118
+ function astroid_() {
119
+ const x = astroidRegisters_[ASTROID_X_];
120
+ const y = astroidRegisters_[ASTROID_Y_];
121
+ const p = x * x;
122
+ const q = y * y;
123
+ const r = (p + q - 1) / 6;
124
+ if (q === 0 && r <= 0) {
125
+ // y = 0 with |x| <= 1.
126
+ astroidRegisters_[ASTROID_ROOT_] = 0;
127
+ return;
128
+ }
129
+ // S = r³·s
130
+ const s = (p * q) / 4;
131
+ const rSquared = r * r;
132
+ const rCubed = r * rSquared;
133
+ const discriminant = s * (s + 2 * rCubed);
134
+ let u = r;
135
+ if (discriminant >= 0) {
136
+ let t3 = s + rCubed;
137
+ // The sign of the root that maximises |t3| avoids cancellation.
138
+ t3 += t3 < 0 ? -Math.sqrt(discriminant) : Math.sqrt(discriminant);
139
+ const t = Math.cbrt(t3);
140
+ u += t + (t !== 0 ? rSquared / t : 0);
141
+ }
142
+ else {
143
+ // A complex root whose combination is real; discriminant < 0 means r < 0.
144
+ const angle = Math.atan2(Math.sqrt(-discriminant), -(s + rCubed));
145
+ u += 2 * r * Math.cos(angle / 3);
146
+ }
147
+ const v = Math.sqrt(u * u + q);
148
+ const uv = u < 0 ? q / (v - u) : u + v;
149
+ const w = (uv - q) / (2 * v);
150
+ astroidRegisters_[ASTROID_ROOT_] = uv / (Math.sqrt(uv + w * w) + w);
151
+ }
152
+ const START_SIGMA12_ = 0;
153
+ const START_SIN_ALPHA1_ = 1;
154
+ const START_COS_ALPHA1_ = 2;
155
+ const START_SIN_ALPHA2_ = 3;
156
+ const START_COS_ALPHA2_ = 4;
157
+ const START_SCALE_ = 5;
158
+ const startScratch_ = scratchCreate_(6);
159
+ // The problem being solved, shared by `inverseStart_` and `lambda12_`.
160
+ const PROBLEM_SIN_BETA1_ = 0;
161
+ const PROBLEM_COS_BETA1_ = 1;
162
+ const PROBLEM_SCALE1_ = 2;
163
+ const PROBLEM_SIN_BETA2_ = 3;
164
+ const PROBLEM_COS_BETA2_ = 4;
165
+ const PROBLEM_SCALE2_ = 5;
166
+ const PROBLEM_LAMBDA12_ = 6;
167
+ const PROBLEM_SIN_LAMBDA12_ = 7;
168
+ const PROBLEM_COS_LAMBDA12_ = 8;
169
+ const PROBLEM_SIN_ALPHA1_ = 9;
170
+ const PROBLEM_COS_ALPHA1_ = 10;
171
+ const problem_ = scratchCreate_(11);
172
+ /**
173
+ * A starting azimuth at point 1 for Newton's method in `startScratch_`
174
+ * (`sigma12` is −1); for a really short line the whole solution, with `sigma12
175
+ * ≥ 0`. Reads the problem from `problem_`.
176
+ */
177
+ function inverseStart_(constants) {
178
+ const sinBeta1 = problem_[PROBLEM_SIN_BETA1_];
179
+ const cosBeta1 = problem_[PROBLEM_COS_BETA1_];
180
+ const scale1 = problem_[PROBLEM_SCALE1_];
181
+ const sinBeta2 = problem_[PROBLEM_SIN_BETA2_];
182
+ const cosBeta2 = problem_[PROBLEM_COS_BETA2_];
183
+ const scale2 = problem_[PROBLEM_SCALE2_];
184
+ const lambda12 = problem_[PROBLEM_LAMBDA12_];
185
+ const sinLambda12 = problem_[PROBLEM_SIN_LAMBDA12_];
186
+ const cosLambda12 = problem_[PROBLEM_COS_LAMBDA12_];
187
+ const flattening = constants[CONSTANT_FLATTENING_];
188
+ const polarRatio = constants[CONSTANT_POLAR_RATIO_];
189
+ const secondEccentricitySquared = constants[CONSTANT_SECOND_ECCENTRICITY_SQUARED_];
190
+ const thirdFlattening = constants[CONSTANT_THIRD_FLATTENING_];
191
+ const shortArc = constants[CONSTANT_SHORT_ARC_];
192
+ // beta12 = beta2 − beta1 in [0, π); beta12a = beta2 + beta1 in (−π, 0]
193
+ const sinBeta12 = sinBeta2 * cosBeta1 - cosBeta2 * sinBeta1;
194
+ const cosBeta12 = cosBeta2 * cosBeta1 + sinBeta2 * sinBeta1;
195
+ let sinBeta12a = sinBeta2 * cosBeta1;
196
+ sinBeta12a += cosBeta2 * sinBeta1;
197
+ const shortLine = cosBeta12 >= 0 && sinBeta12 < 0.5 && cosBeta2 * lambda12 < 0.5;
198
+ let sinOmega12;
199
+ let cosOmega12;
200
+ startScratch_[START_SIGMA12_] = -1;
201
+ if (shortLine) {
202
+ let sinBetaMean2 = (sinBeta1 + sinBeta2) * (sinBeta1 + sinBeta2);
203
+ // sin²((β1 + β2) / 2)
204
+ sinBetaMean2 /=
205
+ sinBetaMean2 + (cosBeta1 + cosBeta2) * (cosBeta1 + cosBeta2);
206
+ const scaleMean = Math.sqrt(1 + secondEccentricitySquared * sinBetaMean2);
207
+ startScratch_[START_SCALE_] = scaleMean;
208
+ const omega12 = lambda12 / (polarRatio * scaleMean);
209
+ sinOmega12 = Math.sin(omega12);
210
+ cosOmega12 = Math.cos(omega12);
211
+ }
212
+ else {
213
+ sinOmega12 = sinLambda12;
214
+ cosOmega12 = cosLambda12;
215
+ }
216
+ let sinAlpha1 = cosBeta2 * sinOmega12;
217
+ let cosAlpha1 = cosOmega12 >= 0
218
+ ? sinBeta12 +
219
+ (cosBeta2 * sinBeta1 * (sinOmega12 * sinOmega12)) / (1 + cosOmega12)
220
+ : sinBeta12a -
221
+ (cosBeta2 * sinBeta1 * (sinOmega12 * sinOmega12)) / (1 - cosOmega12);
222
+ const sinSigma12 = hypot_(sinAlpha1, cosAlpha1);
223
+ const cosSigma12 = sinBeta1 * sinBeta2 + cosBeta1 * cosBeta2 * cosOmega12;
224
+ if (shortLine && sinSigma12 < shortArc) {
225
+ // A really short line.
226
+ let sinAlpha2 = cosBeta1 * sinOmega12;
227
+ let cosAlpha2 = sinBeta12 -
228
+ cosBeta1 *
229
+ sinBeta2 *
230
+ (cosOmega12 >= 0
231
+ ? (sinOmega12 * sinOmega12) / (1 + cosOmega12)
232
+ : 1 - cosOmega12);
233
+ const norm = hypot_(sinAlpha2, cosAlpha2);
234
+ sinAlpha2 /= norm;
235
+ cosAlpha2 /= norm;
236
+ startScratch_[START_SIN_ALPHA2_] = sinAlpha2;
237
+ startScratch_[START_COS_ALPHA2_] = cosAlpha2;
238
+ startScratch_[START_SIGMA12_] = Math.atan2(sinSigma12, cosSigma12);
239
+ }
240
+ else if (Math.abs(thirdFlattening) > 0.1 ||
241
+ cosSigma12 >= 0 ||
242
+ sinSigma12 >=
243
+ 6 * Math.abs(thirdFlattening) * Math.PI * (cosBeta1 * cosBeta1)) {
244
+ // The zeroth-order spherical approximation is enough.
245
+ }
246
+ else {
247
+ // Scale lambda12 and beta2 to the (x, y) system whose origin is the
248
+ // antipodal point and whose singular point is y = 0, x = −1.
249
+ const lambda12x = Math.atan2(-sinLambda12, -cosLambda12);
250
+ let x;
251
+ let y;
252
+ let lambdaScale;
253
+ let betaScale;
254
+ if (flattening >= 0) {
255
+ const kSquared = sinBeta1 * sinBeta1 * secondEccentricitySquared;
256
+ const epsilon = kSquared / (2 * (1 + Math.sqrt(1 + kSquared)) + kSquared);
257
+ seriesRegisters_[SERIES_EPSILON_] = epsilon;
258
+ a3_(constants);
259
+ lambdaScale =
260
+ flattening * cosBeta1 * seriesRegisters_[SERIES_MEAN_] * Math.PI;
261
+ betaScale = lambdaScale * cosBeta1;
262
+ x = lambda12x / lambdaScale;
263
+ y = sinBeta12a / betaScale;
264
+ }
265
+ else {
266
+ const cosBeta12a = cosBeta2 * cosBeta1 - sinBeta2 * sinBeta1;
267
+ const beta12a = Math.atan2(sinBeta12a, cosBeta12a);
268
+ lengthsInput_[LENGTHS_EPSILON_] = thirdFlattening;
269
+ lengthsInput_[LENGTHS_SIGMA12_] = Math.PI + beta12a;
270
+ lengthsInput_[LENGTHS_SIN_SIGMA1_] = sinBeta1;
271
+ lengthsInput_[LENGTHS_COS_SIGMA1_] = -cosBeta1;
272
+ lengthsInput_[LENGTHS_SCALE1_] = scale1;
273
+ lengthsInput_[LENGTHS_SIN_SIGMA2_] = sinBeta2;
274
+ lengthsInput_[LENGTHS_COS_SIGMA2_] = cosBeta2;
275
+ lengthsInput_[LENGTHS_SCALE2_] = scale2;
276
+ lengths_(false, true);
277
+ const reduced = lengthsScratch_[LENGTH_REDUCED_];
278
+ const secular = lengthsScratch_[LENGTH_COEFFICIENT_];
279
+ x = -1 + reduced / (cosBeta1 * cosBeta2 * secular * Math.PI);
280
+ betaScale =
281
+ x < -0.01
282
+ ? sinBeta12a / x
283
+ : -flattening * (cosBeta1 * cosBeta1) * Math.PI;
284
+ lambdaScale = betaScale / cosBeta1;
285
+ y = lambda12 / lambdaScale;
286
+ }
287
+ if (y > -TOLERANCE_COARSE_ && x > -1 - TOLERANCE_X_) {
288
+ // Strip near the cut.
289
+ if (flattening >= 0) {
290
+ sinAlpha1 = Math.min(1, -x);
291
+ cosAlpha1 = -Math.sqrt(1 - sinAlpha1 * sinAlpha1);
292
+ }
293
+ else {
294
+ cosAlpha1 = Math.max(x > -TOLERANCE_COARSE_ ? 0 : -1, x);
295
+ sinAlpha1 = Math.sqrt(1 - cosAlpha1 * cosAlpha1);
296
+ }
297
+ }
298
+ else {
299
+ // Solve the astroid for omega12 (near π, so through its supplement)
300
+ // and update the spherical estimate of alpha1 with it.
301
+ astroidRegisters_[ASTROID_X_] = x;
302
+ astroidRegisters_[ASTROID_Y_] = y;
303
+ astroid_();
304
+ const k = astroidRegisters_[ASTROID_ROOT_];
305
+ const omega12a = lambdaScale *
306
+ (flattening >= 0 ? (-x * k) / (1 + k) : (-y * (1 + k)) / k);
307
+ sinOmega12 = Math.sin(omega12a);
308
+ cosOmega12 = -Math.cos(omega12a);
309
+ sinAlpha1 = cosBeta2 * sinOmega12;
310
+ cosAlpha1 =
311
+ sinBeta12a -
312
+ (cosBeta2 * sinBeta1 * (sinOmega12 * sinOmega12)) / (1 - cosOmega12);
313
+ }
314
+ }
315
+ // A backwards test lets NaN through to the second branch's opposite.
316
+ if (!(sinAlpha1 <= 0)) {
317
+ const norm = hypot_(sinAlpha1, cosAlpha1);
318
+ sinAlpha1 /= norm;
319
+ cosAlpha1 /= norm;
320
+ }
321
+ else {
322
+ sinAlpha1 = 1;
323
+ cosAlpha1 = 0;
324
+ }
325
+ startScratch_[START_SIN_ALPHA1_] = sinAlpha1;
326
+ startScratch_[START_COS_ALPHA1_] = cosAlpha1;
327
+ }
328
+ const LAMBDA_LAMBDA12_ = 0;
329
+ const LAMBDA_SIN_ALPHA2_ = 1;
330
+ const LAMBDA_COS_ALPHA2_ = 2;
331
+ const LAMBDA_SIGMA12_ = 3;
332
+ const LAMBDA_SIN_SIGMA1_ = 4;
333
+ const LAMBDA_COS_SIGMA1_ = 5;
334
+ const LAMBDA_SIN_SIGMA2_ = 6;
335
+ const LAMBDA_COS_SIGMA2_ = 7;
336
+ const LAMBDA_EPSILON_ = 8;
337
+ const LAMBDA_DERIVATIVE_ = 9;
338
+ const lambdaScratch_ = scratchCreate_(10);
339
+ /**
340
+ * The longitude difference reached by the geodesic leaving point 1 at azimuth
341
+ * `alpha1`, less the wanted one, and its derivative in `alpha1` when
342
+ * `wantDerivative`, with the quantities the final length needs, in
343
+ * `lambdaScratch_`. Reads the problem and `alpha1` from `problem_`.
344
+ */
345
+ function lambda12_(constants, wantDerivative) {
346
+ const sinBeta1 = problem_[PROBLEM_SIN_BETA1_];
347
+ const cosBeta1 = problem_[PROBLEM_COS_BETA1_];
348
+ const scale1 = problem_[PROBLEM_SCALE1_];
349
+ const sinBeta2 = problem_[PROBLEM_SIN_BETA2_];
350
+ const cosBeta2 = problem_[PROBLEM_COS_BETA2_];
351
+ const scale2 = problem_[PROBLEM_SCALE2_];
352
+ const sinAlpha1 = problem_[PROBLEM_SIN_ALPHA1_];
353
+ let cosAlpha1 = problem_[PROBLEM_COS_ALPHA1_];
354
+ const sinLambda120 = problem_[PROBLEM_SIN_LAMBDA12_];
355
+ const cosLambda120 = problem_[PROBLEM_COS_LAMBDA12_];
356
+ const flattening = constants[CONSTANT_FLATTENING_];
357
+ const polarRatio = constants[CONSTANT_POLAR_RATIO_];
358
+ const secondEccentricitySquared = constants[CONSTANT_SECOND_ECCENTRICITY_SQUARED_];
359
+ if (sinBeta1 === 0 && cosAlpha1 === 0) {
360
+ // Breaks the degeneracy of the equatorial line (handled before).
361
+ cosAlpha1 = -TINY_DOUBLE_[0];
362
+ }
363
+ // sin(alpha1)·cos(beta1) = sin(alpha0)
364
+ const sinAlpha0 = sinAlpha1 * cosBeta1;
365
+ const cosAlpha0 = hypot_(cosAlpha1, sinAlpha1 * sinBeta1);
366
+ let sinSigma1 = sinBeta1;
367
+ const sinOmega1 = sinAlpha0 * sinBeta1;
368
+ let cosSigma1 = cosAlpha1 * cosBeta1;
369
+ const cosOmega1 = cosSigma1;
370
+ let norm = hypot_(sinSigma1, cosSigma1);
371
+ sinSigma1 /= norm;
372
+ cosSigma1 /= norm;
373
+ // Symmetries for |beta2| = −beta1 keep the Newton iteration regular.
374
+ const sinAlpha2 = cosBeta2 !== cosBeta1 ? sinAlpha0 / cosBeta2 : sinAlpha1;
375
+ const cosAlpha2 = cosBeta2 !== cosBeta1 || Math.abs(sinBeta2) !== -sinBeta1
376
+ ? Math.sqrt(cosAlpha1 * cosBeta1 * (cosAlpha1 * cosBeta1) +
377
+ (cosBeta1 < -sinBeta1
378
+ ? (cosBeta2 - cosBeta1) * (cosBeta1 + cosBeta2)
379
+ : (sinBeta1 - sinBeta2) * (sinBeta1 + sinBeta2))) / cosBeta2
380
+ : Math.abs(cosAlpha1);
381
+ let sinSigma2 = sinBeta2;
382
+ const sinOmega2 = sinAlpha0 * sinBeta2;
383
+ let cosSigma2 = cosAlpha2 * cosBeta2;
384
+ const cosOmega2 = cosSigma2;
385
+ norm = hypot_(sinSigma2, cosSigma2);
386
+ sinSigma2 /= norm;
387
+ cosSigma2 /= norm;
388
+ // sigma12 = sigma2 − sigma1, in [0, π]
389
+ const sigma12 = Math.atan2(Math.max(0, cosSigma1 * sinSigma2 - sinSigma1 * cosSigma2), cosSigma1 * cosSigma2 + sinSigma1 * sinSigma2);
390
+ // omega12 = omega2 − omega1, in [0, π]
391
+ const sinOmega12 = Math.max(0, cosOmega1 * sinOmega2 - sinOmega1 * cosOmega2);
392
+ const cosOmega12 = cosOmega1 * cosOmega2 + sinOmega1 * sinOmega2;
393
+ // eta = omega12 − lambda120
394
+ const eta = Math.atan2(sinOmega12 * cosLambda120 - cosOmega12 * sinLambda120, cosOmega12 * cosLambda120 + sinOmega12 * sinLambda120);
395
+ const kSquared = cosAlpha0 * cosAlpha0 * secondEccentricitySquared;
396
+ const epsilon = kSquared / (2 * (1 + Math.sqrt(1 + kSquared)) + kSquared);
397
+ seriesRegisters_[SERIES_EPSILON_] = epsilon;
398
+ c3_(constants, c3Scratch_, 0);
399
+ a3_(constants);
400
+ const a3 = seriesRegisters_[SERIES_MEAN_];
401
+ seriesRegisters_[SERIES_SIN_] = sinSigma2;
402
+ seriesRegisters_[SERIES_COS_] = cosSigma2;
403
+ seriesRegisters_[SERIES_SIN2_] = sinSigma1;
404
+ seriesRegisters_[SERIES_COS2_] = cosSigma1;
405
+ sinSeriesDifference_(c3Scratch_, 0, SERIES_ORDER_);
406
+ const b312 = seriesRegisters_[SERIES_DIFFERENCE_];
407
+ const domega12 = -flattening * a3 * sinAlpha0 * (sigma12 + b312);
408
+ lambdaScratch_[LAMBDA_LAMBDA12_] = eta + domega12;
409
+ lambdaScratch_[LAMBDA_SIN_ALPHA2_] = sinAlpha2;
410
+ lambdaScratch_[LAMBDA_COS_ALPHA2_] = cosAlpha2;
411
+ lambdaScratch_[LAMBDA_SIGMA12_] = sigma12;
412
+ lambdaScratch_[LAMBDA_SIN_SIGMA1_] = sinSigma1;
413
+ lambdaScratch_[LAMBDA_COS_SIGMA1_] = cosSigma1;
414
+ lambdaScratch_[LAMBDA_SIN_SIGMA2_] = sinSigma2;
415
+ lambdaScratch_[LAMBDA_COS_SIGMA2_] = cosSigma2;
416
+ lambdaScratch_[LAMBDA_EPSILON_] = epsilon;
417
+ if (wantDerivative) {
418
+ if (cosAlpha2 === 0) {
419
+ lambdaScratch_[LAMBDA_DERIVATIVE_] =
420
+ (-2 * polarRatio * scale1) / sinBeta1;
421
+ }
422
+ else {
423
+ lengthsInput_[LENGTHS_EPSILON_] = epsilon;
424
+ lengthsInput_[LENGTHS_SIGMA12_] = sigma12;
425
+ lengthsInput_[LENGTHS_SIN_SIGMA1_] = sinSigma1;
426
+ lengthsInput_[LENGTHS_COS_SIGMA1_] = cosSigma1;
427
+ lengthsInput_[LENGTHS_SCALE1_] = scale1;
428
+ lengthsInput_[LENGTHS_SIN_SIGMA2_] = sinSigma2;
429
+ lengthsInput_[LENGTHS_COS_SIGMA2_] = cosSigma2;
430
+ lengthsInput_[LENGTHS_SCALE2_] = scale2;
431
+ lengths_(false, true);
432
+ lambdaScratch_[LAMBDA_DERIVATIVE_] =
433
+ lengthsScratch_[LENGTH_REDUCED_] *
434
+ (polarRatio / (cosAlpha2 * cosBeta2));
435
+ }
436
+ }
437
+ }
438
+ /** The indices of the input and the result of {@link inverse_}. */
439
+ export const INVERSE_LATITUDE1_ = 0;
440
+ export const INVERSE_LONGITUDE1_ = 1;
441
+ export const INVERSE_LATITUDE2_ = 2;
442
+ export const INVERSE_LONGITUDE2_ = 3;
443
+ export const INVERSE_DISTANCE_ = 0;
444
+ export const INVERSE_SIN_ALPHA1_ = 1;
445
+ export const INVERSE_COS_ALPHA1_ = 2;
446
+ export const INVERSE_SIN_ALPHA2_ = 3;
447
+ export const INVERSE_COS_ALPHA2_ = 4;
448
+ export const INVERSE_LENGTH_ = 5;
449
+ /**
450
+ * Solves the inverse problem between `(latitude1, longitude1)` and `(latitude2,
451
+ * longitude2)`, degrees, read from `io[0 … 3]`; `io` then holds the distance in
452
+ * metres and the sine and cosine of both azimuths. A latitude beyond ±90° and
453
+ * non-finite input give `NaN`s. (One buffer in and out: a call passing doubles
454
+ * as arguments would box them.)
455
+ */
456
+ export function inverse_(constants, io) {
457
+ const latitude1 = io[INVERSE_LATITUDE1_];
458
+ const longitude1 = io[INVERSE_LONGITUDE1_];
459
+ const latitude2 = io[INVERSE_LATITUDE2_];
460
+ const longitude2 = io[INVERSE_LONGITUDE2_];
461
+ const equatorialRadius = constants[CONSTANT_EQUATORIAL_RADIUS_];
462
+ const flattening = constants[CONSTANT_FLATTENING_];
463
+ const polarRatio = constants[CONSTANT_POLAR_RATIO_];
464
+ const secondEccentricitySquared = constants[CONSTANT_SECOND_ECCENTRICITY_SQUARED_];
465
+ const thirdFlattening = constants[CONSTANT_THIRD_FLATTENING_];
466
+ const polarRadius = constants[CONSTANT_POLAR_RADIUS_];
467
+ // Latitudes within 1/16° of the equator count as on it.
468
+ let lat1 = angleRound_(Math.abs(latitude1) > 90 ? Number.NaN : latitude1);
469
+ let lat2 = angleRound_(Math.abs(latitude2) > 90 ? Number.NaN : latitude2);
470
+ // The longitude difference, exactly, in [−180, 180] (−180 only for
471
+ // west-going geodesics).
472
+ angleScratch_[0] = longitude1;
473
+ angleScratch_[1] = longitude2;
474
+ angleDifference_(angleScratch_);
475
+ let lon12 = angleScratch_[0];
476
+ let lon12s = angleScratch_[1];
477
+ let lonSign = copySign(1, lon12);
478
+ lon12 *= lonSign;
479
+ lon12s *= lonSign;
480
+ const lambda12 = lon12 * DEG_TO_RAD;
481
+ angleScratch_[0] = lon12;
482
+ angleScratch_[1] = lon12s;
483
+ sineCosineCorrected_(angleScratch_);
484
+ const sinLambda12 = angleScratch_[0];
485
+ const cosLambda12 = angleScratch_[1];
486
+ // The supplementary longitude difference.
487
+ lon12s = 180 - lon12 - lon12s;
488
+ // Put the point with the larger |latitude| first (a NaN latitude first), and
489
+ // the first in the southern hemisphere, so 0 ≤ lon12 ≤ 180 and
490
+ // lat1 ≤ lat2 ≤ −lat1 whatever the input.
491
+ const swapped = Math.abs(lat1) < Math.abs(lat2) || Number.isNaN(lat2);
492
+ const swapSign = swapped ? -1 : 1;
493
+ if (swapped) {
494
+ lonSign = -lonSign;
495
+ const latitude = lat1;
496
+ lat1 = lat2;
497
+ lat2 = latitude;
498
+ }
499
+ const latSign = copySign(1, -lat1);
500
+ lat1 *= latSign;
501
+ lat2 *= latSign;
502
+ angleScratch_[0] = lat1;
503
+ sinCosDegInPlace(angleScratch_);
504
+ let sinBeta1 = polarRatio * angleScratch_[0];
505
+ let cosBeta1 = angleScratch_[1];
506
+ let norm = hypot_(sinBeta1, cosBeta1);
507
+ sinBeta1 /= norm;
508
+ cosBeta1 /= norm;
509
+ // cos(beta) stays positive at the poles.
510
+ cosBeta1 = Math.max(TINY_, cosBeta1);
511
+ angleScratch_[0] = lat2;
512
+ sinCosDegInPlace(angleScratch_);
513
+ let sinBeta2 = polarRatio * angleScratch_[0];
514
+ let cosBeta2 = angleScratch_[1];
515
+ norm = hypot_(sinBeta2, cosBeta2);
516
+ sinBeta2 /= norm;
517
+ cosBeta2 /= norm;
518
+ cosBeta2 = Math.max(TINY_, cosBeta2);
519
+ // When the points are mirror images, force |beta2| = −beta1 exactly; the
520
+ // Newton iteration is singular otherwise. (`mirrored` is computed on every
521
+ // path: see angleNormalize_.)
522
+ const mirrored = copySign(sinBeta1, sinBeta2);
523
+ if (cosBeta1 < -sinBeta1) {
524
+ if (cosBeta2 === cosBeta1) {
525
+ sinBeta2 = mirrored;
526
+ }
527
+ }
528
+ else if (Math.abs(sinBeta2) === -sinBeta1) {
529
+ cosBeta2 = cosBeta1;
530
+ }
531
+ const scale1 = Math.sqrt(1 + secondEccentricitySquared * (sinBeta1 * sinBeta1));
532
+ const scale2 = Math.sqrt(1 + secondEccentricitySquared * (sinBeta2 * sinBeta2));
533
+ let sinAlpha1 = Number.NaN;
534
+ let cosAlpha1 = Number.NaN;
535
+ let sinAlpha2 = Number.NaN;
536
+ let cosAlpha2 = Number.NaN;
537
+ let distanceBase = Number.NaN;
538
+ // Endpoints on one full meridian: the geodesic may lie on it.
539
+ let meridian = lat1 === -90 || sinLambda12 === 0;
540
+ if (meridian) {
541
+ cosAlpha1 = cosLambda12;
542
+ sinAlpha1 = sinLambda12;
543
+ cosAlpha2 = 1;
544
+ sinAlpha2 = 0;
545
+ const sinSigma1 = sinBeta1;
546
+ const cosSigma1 = cosAlpha1 * cosBeta1;
547
+ const sinSigma2 = sinBeta2;
548
+ const cosSigma2 = cosAlpha2 * cosBeta2;
549
+ const sigma12 = Math.atan2(Math.max(0, cosSigma1 * sinSigma2 - sinSigma1 * cosSigma2), cosSigma1 * cosSigma2 + sinSigma1 * sinSigma2);
550
+ lengthsInput_[LENGTHS_EPSILON_] = thirdFlattening;
551
+ lengthsInput_[LENGTHS_SIGMA12_] = sigma12;
552
+ lengthsInput_[LENGTHS_SIN_SIGMA1_] = sinSigma1;
553
+ lengthsInput_[LENGTHS_COS_SIGMA1_] = cosSigma1;
554
+ lengthsInput_[LENGTHS_SCALE1_] = scale1;
555
+ lengthsInput_[LENGTHS_SIN_SIGMA2_] = sinSigma2;
556
+ lengthsInput_[LENGTHS_COS_SIGMA2_] = cosSigma2;
557
+ lengthsInput_[LENGTHS_SCALE2_] = scale2;
558
+ lengths_(true, true);
559
+ distanceBase = lengthsScratch_[LENGTH_DISTANCE_];
560
+ const reducedBase = lengthsScratch_[LENGTH_REDUCED_];
561
+ // A zero-length geodesic can give a negative reduced length.
562
+ if (sigma12 < TOLERANCE_SQRT_ || reducedBase >= 0) {
563
+ if (sigma12 < 3 * TINY_ ||
564
+ (sigma12 < TOLERANCE_ && (distanceBase < 0 || reducedBase < 0))) {
565
+ distanceBase = 0;
566
+ }
567
+ distanceBase *= polarRadius;
568
+ }
569
+ else {
570
+ // Prolate and too close to antipodal: not a meridian after all.
571
+ meridian = false;
572
+ }
573
+ }
574
+ if (!meridian &&
575
+ sinBeta1 === 0 &&
576
+ (flattening <= 0 || lon12s >= flattening * 180)) {
577
+ // The geodesic runs along the equator.
578
+ cosAlpha1 = 0;
579
+ cosAlpha2 = 0;
580
+ sinAlpha1 = 1;
581
+ sinAlpha2 = 1;
582
+ distanceBase = equatorialRadius * lambda12;
583
+ }
584
+ else if (!meridian) {
585
+ // Neither meridional nor equatorial: Newton's method on lambda12, from a
586
+ // starting azimuth.
587
+ problem_[PROBLEM_SIN_BETA1_] = sinBeta1;
588
+ problem_[PROBLEM_COS_BETA1_] = cosBeta1;
589
+ problem_[PROBLEM_SCALE1_] = scale1;
590
+ problem_[PROBLEM_SIN_BETA2_] = sinBeta2;
591
+ problem_[PROBLEM_COS_BETA2_] = cosBeta2;
592
+ problem_[PROBLEM_SCALE2_] = scale2;
593
+ problem_[PROBLEM_LAMBDA12_] = lambda12;
594
+ problem_[PROBLEM_SIN_LAMBDA12_] = sinLambda12;
595
+ problem_[PROBLEM_COS_LAMBDA12_] = cosLambda12;
596
+ inverseStart_(constants);
597
+ let sigma12 = startScratch_[START_SIGMA12_];
598
+ sinAlpha1 = startScratch_[START_SIN_ALPHA1_];
599
+ cosAlpha1 = startScratch_[START_COS_ALPHA1_];
600
+ if (sigma12 >= 0) {
601
+ // A short line, solved by the start.
602
+ sinAlpha2 = startScratch_[START_SIN_ALPHA2_];
603
+ cosAlpha2 = startScratch_[START_COS_ALPHA2_];
604
+ distanceBase = sigma12 * polarRadius * startScratch_[START_SCALE_];
605
+ }
606
+ else {
607
+ // f(alpha) = lambda12(alpha) − lambda120 has one root in (0, π) with a
608
+ // positive slope; a bracket (alpha1a, alpha1b) around it shrinks with
609
+ // every evaluation, and Newton's method restarts from its middle when
610
+ // the slope is not positive or a step leaves (0, π).
611
+ let epsilon = Number.NaN;
612
+ let sinSigma1 = Number.NaN;
613
+ let cosSigma1 = Number.NaN;
614
+ let sinSigma2 = Number.NaN;
615
+ let cosSigma2 = Number.NaN;
616
+ let sinAlpha1Low = TINY_DOUBLE_[0];
617
+ let cosAlpha1Low = 1;
618
+ let sinAlpha1High = TINY_DOUBLE_[0];
619
+ let cosAlpha1High = -1;
620
+ let converging = false;
621
+ let bracketClosed = false;
622
+ for (let iteration = 0;; iteration++) {
623
+ problem_[PROBLEM_SIN_ALPHA1_] = sinAlpha1;
624
+ problem_[PROBLEM_COS_ALPHA1_] = cosAlpha1;
625
+ lambda12_(constants, iteration < MAX_NEWTON_ITERATIONS_);
626
+ const value = lambdaScratch_[LAMBDA_LAMBDA12_];
627
+ const derivative = lambdaScratch_[LAMBDA_DERIVATIVE_];
628
+ sinAlpha2 = lambdaScratch_[LAMBDA_SIN_ALPHA2_];
629
+ cosAlpha2 = lambdaScratch_[LAMBDA_COS_ALPHA2_];
630
+ sigma12 = lambdaScratch_[LAMBDA_SIGMA12_];
631
+ sinSigma1 = lambdaScratch_[LAMBDA_SIN_SIGMA1_];
632
+ cosSigma1 = lambdaScratch_[LAMBDA_COS_SIGMA1_];
633
+ sinSigma2 = lambdaScratch_[LAMBDA_SIN_SIGMA2_];
634
+ cosSigma2 = lambdaScratch_[LAMBDA_COS_SIGMA2_];
635
+ epsilon = lambdaScratch_[LAMBDA_EPSILON_];
636
+ // A reversed test, so NaN escapes the loop.
637
+ if (bracketClosed ||
638
+ !(Math.abs(value) >= (converging ? 8 : 1) * TOLERANCE_) ||
639
+ iteration === MAX_BISECTION_ITERATIONS_) {
640
+ break;
641
+ }
642
+ if (value > 0 &&
643
+ (iteration < MAX_NEWTON_ITERATIONS_ ||
644
+ cosAlpha1 / sinAlpha1 > cosAlpha1High / sinAlpha1High)) {
645
+ sinAlpha1High = sinAlpha1;
646
+ cosAlpha1High = cosAlpha1;
647
+ }
648
+ else if (value < 0 &&
649
+ (iteration < MAX_NEWTON_ITERATIONS_ ||
650
+ cosAlpha1 / sinAlpha1 < cosAlpha1Low / sinAlpha1Low)) {
651
+ sinAlpha1Low = sinAlpha1;
652
+ cosAlpha1Low = cosAlpha1;
653
+ }
654
+ if (iteration < MAX_NEWTON_ITERATIONS_ && derivative > 0) {
655
+ const step = -value / derivative;
656
+ if (Math.abs(step) < Math.PI) {
657
+ const sinStep = Math.sin(step);
658
+ const cosStep = Math.cos(step);
659
+ const nextSinAlpha1 = sinAlpha1 * cosStep + cosAlpha1 * sinStep;
660
+ if (nextSinAlpha1 > 0) {
661
+ cosAlpha1 = cosAlpha1 * cosStep - sinAlpha1 * sinStep;
662
+ sinAlpha1 = nextSinAlpha1;
663
+ norm = hypot_(sinAlpha1, cosAlpha1);
664
+ sinAlpha1 /= norm;
665
+ cosAlpha1 /= norm;
666
+ // Convergence is not always quadratic (the slope can vanish):
667
+ // use conditions on epsilon rather than its square root.
668
+ converging = Math.abs(value) <= 16 * TOLERANCE_;
669
+ continue;
670
+ }
671
+ }
672
+ }
673
+ // No usable slope or a step out of range: the middle of the bracket.
674
+ sinAlpha1 = (sinAlpha1Low + sinAlpha1High) / 2;
675
+ cosAlpha1 = (cosAlpha1Low + cosAlpha1High) / 2;
676
+ norm = hypot_(sinAlpha1, cosAlpha1);
677
+ sinAlpha1 /= norm;
678
+ cosAlpha1 /= norm;
679
+ converging = false;
680
+ bracketClosed =
681
+ Math.abs(sinAlpha1Low - sinAlpha1) + (cosAlpha1Low - cosAlpha1) <
682
+ TOLERANCE_ ||
683
+ Math.abs(sinAlpha1 - sinAlpha1High) + (cosAlpha1 - cosAlpha1High) <
684
+ TOLERANCE_;
685
+ }
686
+ lengthsInput_[LENGTHS_EPSILON_] = epsilon;
687
+ lengthsInput_[LENGTHS_SIGMA12_] = sigma12;
688
+ lengthsInput_[LENGTHS_SIN_SIGMA1_] = sinSigma1;
689
+ lengthsInput_[LENGTHS_COS_SIGMA1_] = cosSigma1;
690
+ lengthsInput_[LENGTHS_SCALE1_] = scale1;
691
+ lengthsInput_[LENGTHS_SIN_SIGMA2_] = sinSigma2;
692
+ lengthsInput_[LENGTHS_COS_SIGMA2_] = cosSigma2;
693
+ lengthsInput_[LENGTHS_SCALE2_] = scale2;
694
+ lengths_(true, false);
695
+ distanceBase = lengthsScratch_[LENGTH_DISTANCE_] * polarRadius;
696
+ }
697
+ }
698
+ // Back to the caller's orientation: the swap, the longitude sign and the
699
+ // hemisphere sign.
700
+ if (swapped) {
701
+ let swap = sinAlpha1;
702
+ sinAlpha1 = sinAlpha2;
703
+ sinAlpha2 = swap;
704
+ swap = cosAlpha1;
705
+ cosAlpha1 = cosAlpha2;
706
+ cosAlpha2 = swap;
707
+ }
708
+ io[INVERSE_DISTANCE_] = 0 + distanceBase;
709
+ io[INVERSE_SIN_ALPHA1_] = sinAlpha1 * swapSign * lonSign;
710
+ io[INVERSE_COS_ALPHA1_] = cosAlpha1 * swapSign * latSign;
711
+ io[INVERSE_SIN_ALPHA2_] = sinAlpha2 * swapSign * lonSign;
712
+ io[INVERSE_COS_ALPHA2_] = cosAlpha2 * swapSign * latSign;
713
+ }
714
+ //# sourceMappingURL=_inverse.js.map