اسكريبت جافا آخر

 

Source: geographiclib-geodesic/src/Geodesic.js

  1. /*
  2. * Geodesic.js
  3. * Transcription of Geodesic.[ch]pp into JavaScript.
  4. *
  5. * See the documentation for the C++ class. The conversion is a literal
  6. * conversion from C++.
  7. *
  8. * The algorithms are derived in
  9. *
  10. * Charles F. F. Karney,
  11. * Algorithms for geodesics, J. Geodesy 87, 43-55 (2013);
  12. * https://doi.org/10.1007/s00190-012-0578-z
  13. * Addenda: https://geographiclib.sourceforge.io/geod-addenda.html
  14. *
  15. * Copyright (c) Charles Karney (2011-2022) <karney@alum.mit.edu> and licensed
  16. * under the MIT/X11 License. For more information, see
  17. * https://geographiclib.sourceforge.io/
  18. */
  19. // Load AFTER Math.js
  20. // To allow swap via [y, x] = [x, y]
  21. /* jshint esversion: 6 */
  22. geodesic.Geodesic = {};
  23. geodesic.GeodesicLine = {};
  24. geodesic.PolygonArea = {};
  25. (function(
  26. /**
  27. * @exports geodesic/Geodesic
  28. * @description Solve geodesic problems via the
  29. * {@link module:geodesic/Geodesic.Geodesic Geodesic} class.
  30. */
  31. g, l, p, m, c) {
  32. "use strict";
  33. var GEOGRAPHICLIB_GEODESIC_ORDER = 6,
  34. nA1_ = GEOGRAPHICLIB_GEODESIC_ORDER,
  35. nA2_ = GEOGRAPHICLIB_GEODESIC_ORDER,
  36. nA3_ = GEOGRAPHICLIB_GEODESIC_ORDER,
  37. nA3x_ = nA3_,
  38. nC3x_, nC4x_,
  39. maxit1_ = 20,
  40. maxit2_ = maxit1_ + m.digits + 10,
  41. tol0_ = m.epsilon,
  42. tol1_ = 200 * tol0_,
  43. tol2_ = Math.sqrt(tol0_),
  44. tolb_ = tol0_,
  45. xthresh_ = 1000 * tol2_,
  46. CAP_NONE = 0,
  47. CAP_ALL = 0x1F,
  48. OUT_ALL = 0x7F80,
  49. astroid,
  50. A1m1f_coeff, C1f_coeff, C1pf_coeff,
  51. A2m1f_coeff, C2f_coeff,
  52. A3_coeff, C3_coeff, C4_coeff;
  53. // N.B. Number.MIN_VALUE is denormalized; divide by Number.EPSILON to get min
  54. // normalized positive number.
  55. g.tiny_ = Math.sqrt(Number.MIN_VALUE/Number.EPSILON);
  56. g.nC1_ = GEOGRAPHICLIB_GEODESIC_ORDER;
  57. g.nC1p_ = GEOGRAPHICLIB_GEODESIC_ORDER;
  58. g.nC2_ = GEOGRAPHICLIB_GEODESIC_ORDER;
  59. g.nC3_ = GEOGRAPHICLIB_GEODESIC_ORDER;
  60. g.nC4_ = GEOGRAPHICLIB_GEODESIC_ORDER;
  61. nC3x_ = (g.nC3_ * (g.nC3_ - 1)) / 2;
  62. nC4x_ = (g.nC4_ * (g.nC4_ + 1)) / 2;
  63. g.CAP_C1 = 1<<0;
  64. g.CAP_C1p = 1<<1;
  65. g.CAP_C2 = 1<<2;
  66. g.CAP_C3 = 1<<3;
  67. g.CAP_C4 = 1<<4;
  68. g.NONE = 0;
  69. g.ARC = 1<<6;
  70. g.LATITUDE = 1<<7 | CAP_NONE;
  71. g.LONGITUDE = 1<<8 | g.CAP_C3;
  72. g.AZIMUTH = 1<<9 | CAP_NONE;
  73. g.DISTANCE = 1<<10 | g.CAP_C1;
  74. g.STANDARD = g.LATITUDE | g.LONGITUDE | g.AZIMUTH | g.DISTANCE;
  75. g.DISTANCE_IN = 1<<11 | g.CAP_C1 | g.CAP_C1p;
  76. g.REDUCEDLENGTH = 1<<12 | g.CAP_C1 | g.CAP_C2;
  77. g.GEODESICSCALE = 1<<13 | g.CAP_C1 | g.CAP_C2;
  78. g.AREA = 1<<14 | g.CAP_C4;
  79. g.ALL = OUT_ALL| CAP_ALL;
  80. g.LONG_UNROLL = 1<<15;
  81. g.OUT_MASK = OUT_ALL| g.LONG_UNROLL;
  82. g.SinCosSeries = function(sinp, sinx, cosx, c) {
  83. // Evaluate
  84. // y = sinp ? sum(c[i] * sin( 2*i * x), i, 1, n) :
  85. // sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
  86. // using Clenshaw summation. N.B. c[0] is unused for sin series
  87. // Approx operation count = (n + 5) mult and (2 * n + 2) add
  88. var k = c.length, // Point to one beyond last element
  89. n = k - (sinp ? 1 : 0),
  90. ar = 2 * (cosx - sinx) * (cosx + sinx), // 2 * cos(2 * x)
  91. y0 = n & 1 ? c[--k] : 0, y1 = 0; // accumulators for sum
  92. // Now n is even
  93. n = Math.floor(n/2);
  94. while (n--) {
  95. // Unroll loop x 2, so accumulators return to their original role
  96. y1 = ar * y0 - y1 + c[--k];
  97. y0 = ar * y1 - y0 + c[--k];
  98. }
  99. return (sinp ? 2 * sinx * cosx * y0 : // sin(2 * x) * y0
  100. cosx * (y0 - y1)); // cos(x) * (y0 - y1)
  101. };
  102. astroid = function(x, y) {
  103. // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive
  104. // root k. This solution is adapted from Geocentric::Reverse.
  105. var k,
  106. p = m.sq(x),
  107. q = m.sq(y),
  108. r = (p + q - 1) / 6,
  109. S, r2, r3, disc, u, T3, T, ang, v, uv, w;
  110. if ( !(q === 0 && r <= 0) ) {
  111. // Avoid possible division by zero when r = 0 by multiplying
  112. // equations for s and t by r^3 and r, resp.
  113. S = p * q / 4; // S = r^3 * s
  114. r2 = m.sq(r);
  115. r3 = r * r2;
  116. // The discriminant of the quadratic equation for T3. This is
  117. // zero on the evolute curve p^(1/3)+q^(1/3) = 1
  118. disc = S * (S + 2 * r3);
  119. u = r;
  120. if (disc >= 0) {
  121. T3 = S + r3;
  122. // Pick the sign on the sqrt to maximize abs(T3). This
  123. // minimizes loss of precision due to cancellation. The
  124. // result is unchanged because of the way the T is used
  125. // in definition of u.
  126. T3 += T3 < 0 ? -Math.sqrt(disc) : Math.sqrt(disc); // T3 = (r * t)^3
  127. // N.B. cbrt always returns the real root. cbrt(-8) = -2.
  128. T = m.cbrt(T3); // T = r * t
  129. // T can be zero; but then r2 / T -> 0.
  130. u += T + (T !== 0 ? r2 / T : 0);
  131. } else {
  132. // T is complex, but the way u is defined the result is real.
  133. ang = Math.atan2(Math.sqrt(-disc), -(S + r3));
  134. // There are three possible cube roots. We choose the
  135. // root which avoids cancellation. Note that disc < 0
  136. // implies that r < 0.
  137. u += 2 * r * Math.cos(ang / 3);
  138. }
  139. v = Math.sqrt(m.sq(u) + q); // guaranteed positive
  140. // Avoid loss of accuracy when u < 0.
  141. uv = u < 0 ? q / (v - u) : u + v; // u+v, guaranteed positive
  142. w = (uv - q) / (2 * v); // positive?
  143. // Rearrange expression for k to avoid loss of accuracy due to
  144. // subtraction. Division by 0 not possible because uv > 0, w >= 0.
  145. k = uv / (Math.sqrt(uv + m.sq(w)) + w); // guaranteed positive
  146. } else { // q == 0 && r <= 0
  147. // y = 0 with |x| <= 1. Handle this case directly.
  148. // for y small, positive root is k = abs(y)/sqrt(1-x^2)
  149. k = 0;
  150. }
  151. return k;
  152. };
  153. A1m1f_coeff = [
  154. // (1-eps)*A1-1, polynomial in eps2 of order 3
  155. +1, 4, 64, 0, 256
  156. ];
  157. // The scale factor A1-1 = mean value of (d/dsigma)I1 - 1
  158. g.A1m1f = function(eps) {
  159. var p = Math.floor(nA1_/2),
  160. t = m.polyval(p, A1m1f_coeff, 0, m.sq(eps)) / A1m1f_coeff[p + 1];
  161. return (t + eps) / (1 - eps);
  162. };
  163. C1f_coeff = [
  164. // C1[1]/eps^1, polynomial in eps2 of order 2
  165. -1, 6, -16, 32,
  166. // C1[2]/eps^2, polynomial in eps2 of order 2
  167. -9, 64, -128, 2048,
  168. // C1[3]/eps^3, polynomial in eps2 of order 1
  169. +9, -16, 768,
  170. // C1[4]/eps^4, polynomial in eps2 of order 1
  171. +3, -5, 512,
  172. // C1[5]/eps^5, polynomial in eps2 of order 0
  173. -7, 1280,
  174. // C1[6]/eps^6, polynomial in eps2 of order 0
  175. -7, 2048
  176. ];
  177. // The coefficients C1[l] in the Fourier expansion of B1
  178. g.C1f = function(eps, c) {
  179. var eps2 = m.sq(eps),
  180. d = eps,
  181. o = 0,
  182. l, p;
  183. for (l = 1; l <= g.nC1_; ++l) { // l is index of C1p[l]
  184. p = Math.floor((g.nC1_ - l) / 2); // order of polynomial in eps^2
  185. c[l] = d * m.polyval(p, C1f_coeff, o, eps2) / C1f_coeff[o + p + 1];
  186. o += p + 2;
  187. d *= eps;
  188. }
  189. };
  190. C1pf_coeff = [
  191. // C1p[1]/eps^1, polynomial in eps2 of order 2
  192. +205, -432, 768, 1536,
  193. // C1p[2]/eps^2, polynomial in eps2 of order 2
  194. +4005, -4736, 3840, 12288,
  195. // C1p[3]/eps^3, polynomial in eps2 of order 1
  196. -225, 116, 384,
  197. // C1p[4]/eps^4, polynomial in eps2 of order 1
  198. -7173, 2695, 7680,
  199. // C1p[5]/eps^5, polynomial in eps2 of order 0
  200. +3467, 7680,
  201. // C1p[6]/eps^6, polynomial in eps2 of order 0
  202. +38081, 61440
  203. ];
  204. // The coefficients C1p[l] in the Fourier expansion of B1p
  205. g.C1pf = function(eps, c) {
  206. var eps2 = m.sq(eps),
  207. d = eps,
  208. o = 0,
  209. l, p;
  210. for (l = 1; l <= g.nC1p_; ++l) { // l is index of C1p[l]
  211. p = Math.floor((g.nC1p_ - l) / 2); // order of polynomial in eps^2
  212. c[l] = d * m.polyval(p, C1pf_coeff, o, eps2) / C1pf_coeff[o + p + 1];
  213. o += p + 2;
  214. d *= eps;
  215. }
  216. };
  217. A2m1f_coeff = [
  218. // (eps+1)*A2-1, polynomial in eps2 of order 3
  219. -11, -28, -192, 0, 256
  220. ];
  221. // The scale factor A2-1 = mean value of (d/dsigma)I2 - 1
  222. g.A2m1f = function(eps) {
  223. var p = Math.floor(nA2_/2),
  224. t = m.polyval(p, A2m1f_coeff, 0, m.sq(eps)) / A2m1f_coeff[p + 1];
  225. return (t - eps) / (1 + eps);
  226. };
  227. C2f_coeff = [
  228. // C2[1]/eps^1, polynomial in eps2 of order 2
  229. +1, 2, 16, 32,
  230. // C2[2]/eps^2, polynomial in eps2 of order 2
  231. +35, 64, 384, 2048,
  232. // C2[3]/eps^3, polynomial in eps2 of order 1
  233. +15, 80, 768,
  234. // C2[4]/eps^4, polynomial in eps2 of order 1
  235. +7, 35, 512,
  236. // C2[5]/eps^5, polynomial in eps2 of order 0
  237. +63, 1280,
  238. // C2[6]/eps^6, polynomial in eps2 of order 0
  239. +77, 2048
  240. ];
  241. // The coefficients C2[l] in the Fourier expansion of B2
  242. g.C2f = function(eps, c) {
  243. var eps2 = m.sq(eps),
  244. d = eps,
  245. o = 0,
  246. l, p;
  247. for (l = 1; l <= g.nC2_; ++l) { // l is index of C2[l]
  248. p = Math.floor((g.nC2_ - l) / 2); // order of polynomial in eps^2
  249. c[l] = d * m.polyval(p, C2f_coeff, o, eps2) / C2f_coeff[o + p + 1];
  250. o += p + 2;
  251. d *= eps;
  252. }
  253. };
  254. /**
  255. * @class
  256. * @property {number} a the equatorial radius (meters).
  257. * @property {number} f the flattening.
  258. * @summary Initialize a Geodesic object for a specific ellipsoid.
  259. * @classdesc Performs geodesic calculations on an ellipsoid of revolution.
  260. * The routines for solving the direct and inverse problems return an
  261. * object with some of the following fields set: lat1, lon1, azi1, lat2,
  262. * lon2, azi2, s12, a12, m12, M12, M21, S12. See {@tutorial 2-interface},
  263. * section "The results".
  264. * @example
  265. * var geodesic = require("geographiclib-geodesic"),
  266. * geod = geodesic.Geodesic.WGS84;
  267. * var inv = geod.Inverse(1,2,3,4);
  268. * console.log("lat1 = " + inv.lat1 + ", lon1 = " + inv.lon1 +
  269. * ", lat2 = " + inv.lat2 + ", lon2 = " + inv.lon2 +
  270. * ",\nazi1 = " + inv.azi1 + ", azi2 = " + inv.azi2 +
  271. * ", s12 = " + inv.s12);
  272. * @param {number} a the equatorial radius of the ellipsoid (meters).
  273. * @param {number} f the flattening of the ellipsoid. Setting f = 0 gives
  274. * a sphere (on which geodesics are great circles). Negative f gives a
  275. * prolate ellipsoid.
  276. * @throws an error if the parameters are illegal.
  277. */
  278. g.Geodesic = function(a, f) {
  279. this.a = a;
  280. this.f = f;
  281. this._f1 = 1 - this.f;
  282. this._e2 = this.f * (2 - this.f);
  283. this._ep2 = this._e2 / m.sq(this._f1); // e2 / (1 - e2)
  284. this._n = this.f / ( 2 - this.f);
  285. this._b = this.a * this._f1;
  286. // authalic radius squared
  287. this._c2 = (m.sq(this.a) + m.sq(this._b) *
  288. (this._e2 === 0 ? 1 :
  289. (this._e2 > 0 ? m.atanh(Math.sqrt(this._e2)) :
  290. Math.atan(Math.sqrt(-this._e2))) /
  291. Math.sqrt(Math.abs(this._e2))))/2;
  292. // The sig12 threshold for "really short". Using the auxiliary sphere
  293. // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
  294. // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
  295. // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a given
  296. // f and sig12, the max error occurs for lines near the pole. If the old
  297. // rule for computing dnm = (dn1 + dn2)/2 is used, then the error increases
  298. // by a factor of 2.) Setting this equal to epsilon gives sig12 = etol2.
  299. // Here 0.1 is a safety factor (error decreased by 100) and max(0.001,
  300. // abs(f)) stops etol2 getting too large in the nearly spherical case.
  301. this._etol2 = 0.1 * tol2_ /
  302. Math.sqrt( Math.max(0.001, Math.abs(this.f)) *
  303. Math.min(1, 1 - this.f/2) / 2 );
  304. if (!(isFinite(this.a) && this.a > 0))
  305. throw new Error("Equatorial radius is not positive");
  306. if (!(isFinite(this._b) && this._b > 0))
  307. throw new Error("Polar semi-axis is not positive");
  308. this._A3x = new Array(nA3x_);
  309. this._C3x = new Array(nC3x_);
  310. this._C4x = new Array(nC4x_);
  311. this.A3coeff();
  312. this.C3coeff();
  313. this.C4coeff();
  314. };
  315. A3_coeff = [
  316. // A3, coeff of eps^5, polynomial in n of order 0
  317. -3, 128,
  318. // A3, coeff of eps^4, polynomial in n of order 1
  319. -2, -3, 64,
  320. // A3, coeff of eps^3, polynomial in n of order 2
  321. -1, -3, -1, 16,
  322. // A3, coeff of eps^2, polynomial in n of order 2
  323. +3, -1, -2, 8,
  324. // A3, coeff of eps^1, polynomial in n of order 1
  325. +1, -1, 2,
  326. // A3, coeff of eps^0, polynomial in n of order 0
  327. +1, 1
  328. ];
  329. // The scale factor A3 = mean value of (d/dsigma)I3
  330. g.Geodesic.prototype.A3coeff = function() {
  331. var o = 0, k = 0,
  332. j, p;
  333. for (j = nA3_ - 1; j >= 0; --j) { // coeff of eps^j
  334. p = Math.min(nA3_ - j - 1, j); // order of polynomial in n
  335. this._A3x[k++] = m.polyval(p, A3_coeff, o, this._n) /
  336. A3_coeff[o + p + 1];
  337. o += p + 2;
  338. }
  339. };
  340. C3_coeff = [
  341. // C3[1], coeff of eps^5, polynomial in n of order 0
  342. +3, 128,
  343. // C3[1], coeff of eps^4, polynomial in n of order 1
  344. +2, 5, 128,
  345. // C3[1], coeff of eps^3, polynomial in n of order 2
  346. -1, 3, 3, 64,
  347. // C3[1], coeff of eps^2, polynomial in n of order 2
  348. -1, 0, 1, 8,
  349. // C3[1], coeff of eps^1, polynomial in n of order 1
  350. -1, 1, 4,
  351. // C3[2], coeff of eps^5, polynomial in n of order 0
  352. +5, 256,
  353. // C3[2], coeff of eps^4, polynomial in n of order 1
  354. +1, 3, 128,
  355. // C3[2], coeff of eps^3, polynomial in n of order 2
  356. -3, -2, 3, 64,
  357. // C3[2], coeff of eps^2, polynomial in n of order 2
  358. +1, -3, 2, 32,
  359. // C3[3], coeff of eps^5, polynomial in n of order 0
  360. +7, 512,
  361. // C3[3], coeff of eps^4, polynomial in n of order 1
  362. -10, 9, 384,
  363. // C3[3], coeff of eps^3, polynomial in n of order 2
  364. +5, -9, 5, 192,
  365. // C3[4], coeff of eps^5, polynomial in n of order 0
  366. +7, 512,
  367. // C3[4], coeff of eps^4, polynomial in n of order 1
  368. -14, 7, 512,
  369. // C3[5], coeff of eps^5, polynomial in n of order 0
  370. +21, 2560
  371. ];
  372. // The coefficients C3[l] in the Fourier expansion of B3
  373. g.Geodesic.prototype.C3coeff = function() {
  374. var o = 0, k = 0,
  375. l, j, p;
  376. for (l = 1; l < g.nC3_; ++l) { // l is index of C3[l]
  377. for (j = g.nC3_ - 1; j >= l; --j) { // coeff of eps^j
  378. p = Math.min(g.nC3_ - j - 1, j); // order of polynomial in n
  379. this._C3x[k++] = m.polyval(p, C3_coeff, o, this._n) /
  380. C3_coeff[o + p + 1];
  381. o += p + 2;
  382. }
  383. }
  384. };
  385. C4_coeff = [
  386. // C4[0], coeff of eps^5, polynomial in n of order 0
  387. +97, 15015,
  388. // C4[0], coeff of eps^4, polynomial in n of order 1
  389. +1088, 156, 45045,
  390. // C4[0], coeff of eps^3, polynomial in n of order 2
  391. -224, -4784, 1573, 45045,
  392. // C4[0], coeff of eps^2, polynomial in n of order 3
  393. -10656, 14144, -4576, -858, 45045,
  394. // C4[0], coeff of eps^1, polynomial in n of order 4
  395. +64, 624, -4576, 6864, -3003, 15015,
  396. // C4[0], coeff of eps^0, polynomial in n of order 5
  397. +100, 208, 572, 3432, -12012, 30030, 45045,
  398. // C4[1], coeff of eps^5, polynomial in n of order 0
  399. +1, 9009,
  400. // C4[1], coeff of eps^4, polynomial in n of order 1
  401. -2944, 468, 135135,
  402. // C4[1], coeff of eps^3, polynomial in n of order 2
  403. +5792, 1040, -1287, 135135,
  404. // C4[1], coeff of eps^2, polynomial in n of order 3
  405. +5952, -11648, 9152, -2574, 135135,
  406. // C4[1], coeff of eps^1, polynomial in n of order 4
  407. -64, -624, 4576, -6864, 3003, 135135,
  408. // C4[2], coeff of eps^5, polynomial in n of order 0
  409. +8, 10725,
  410. // C4[2], coeff of eps^4, polynomial in n of order 1
  411. +1856, -936, 225225,
  412. // C4[2], coeff of eps^3, polynomial in n of order 2
  413. -8448, 4992, -1144, 225225,
  414. // C4[2], coeff of eps^2, polynomial in n of order 3
  415. -1440, 4160, -4576, 1716, 225225,
  416. // C4[3], coeff of eps^5, polynomial in n of order 0
  417. -136, 63063,
  418. // C4[3], coeff of eps^4, polynomial in n of order 1
  419. +1024, -208, 105105,
  420. // C4[3], coeff of eps^3, polynomial in n of order 2
  421. +3584, -3328, 1144, 315315,
  422. // C4[4], coeff of eps^5, polynomial in n of order 0
  423. -128, 135135,
  424. // C4[4], coeff of eps^4, polynomial in n of order 1
  425. -2560, 832, 405405,
  426. // C4[5], coeff of eps^5, polynomial in n of order 0
  427. +128, 99099
  428. ];
  429. g.Geodesic.prototype.C4coeff = function() {
  430. var o = 0, k = 0,
  431. l, j, p;
  432. for (l = 0; l < g.nC4_; ++l) { // l is index of C4[l]
  433. for (j = g.nC4_ - 1; j >= l; --j) { // coeff of eps^j
  434. p = g.nC4_ - j - 1; // order of polynomial in n
  435. this._C4x[k++] = m.polyval(p, C4_coeff, o, this._n) /
  436. C4_coeff[o + p + 1];
  437. o += p + 2;
  438. }
  439. }
  440. };
  441. g.Geodesic.prototype.A3f = function(eps) {
  442. // Evaluate A3
  443. return m.polyval(nA3x_ - 1, this._A3x, 0, eps);
  444. };
  445. g.Geodesic.prototype.C3f = function(eps, c) {
  446. // Evaluate C3 coeffs
  447. // Elements c[1] thru c[nC3_ - 1] are set
  448. var mult = 1,
  449. o = 0,
  450. l, p;
  451. for (l = 1; l < g.nC3_; ++l) { // l is index of C3[l]
  452. p = g.nC3_ - l - 1; // order of polynomial in eps
  453. mult *= eps;
  454. c[l] = mult * m.polyval(p, this._C3x, o, eps);
  455. o += p + 1;
  456. }
  457. };
  458. g.Geodesic.prototype.C4f = function(eps, c) {
  459. // Evaluate C4 coeffs
  460. // Elements c[0] thru c[g.nC4_ - 1] are set
  461. var mult = 1,
  462. o = 0,
  463. l, p;
  464. for (l = 0; l < g.nC4_; ++l) { // l is index of C4[l]
  465. p = g.nC4_ - l - 1; // order of polynomial in eps
  466. c[l] = mult * m.polyval(p, this._C4x, o, eps);
  467. o += p + 1;
  468. mult *= eps;
  469. }
  470. };
  471. // return s12b, m12b, m0, M12, M21
  472. g.Geodesic.prototype.Lengths = function(eps, sig12,
  473. ssig1, csig1, dn1, ssig2, csig2, dn2,
  474. cbet1, cbet2, outmask,
  475. C1a, C2a) {
  476. // Return m12b = (reduced length)/_b; also calculate s12b =
  477. // distance/_b, and m0 = coefficient of secular term in
  478. // expression for reduced length.
  479. outmask &= g.OUT_MASK;
  480. var vals = {},
  481. m0x = 0, J12 = 0, A1 = 0, A2 = 0,
  482. B1, B2, l, csig12, t;
  483. if (outmask & (g.DISTANCE | g.REDUCEDLENGTH | g.GEODESICSCALE)) {
  484. A1 = g.A1m1f(eps);
  485. g.C1f(eps, C1a);
  486. if (outmask & (g.REDUCEDLENGTH | g.GEODESICSCALE)) {
  487. A2 = g.A2m1f(eps);
  488. g.C2f(eps, C2a);
  489. m0x = A1 - A2;
  490. A2 = 1 + A2;
  491. }
  492. A1 = 1 + A1;
  493. }
  494. if (outmask & g.DISTANCE) {
  495. B1 = g.SinCosSeries(true, ssig2, csig2, C1a) -
  496. g.SinCosSeries(true, ssig1, csig1, C1a);
  497. // Missing a factor of _b
  498. vals.s12b = A1 * (sig12 + B1);
  499. if (outmask & (g.REDUCEDLENGTH | g.GEODESICSCALE)) {
  500. B2 = g.SinCosSeries(true, ssig2, csig2, C2a) -
  501. g.SinCosSeries(true, ssig1, csig1, C2a);
  502. J12 = m0x * sig12 + (A1 * B1 - A2 * B2);
  503. }
  504. } else if (outmask & (g.REDUCEDLENGTH | g.GEODESICSCALE)) {
  505. // Assume here that nC1_ >= nC2_
  506. for (l = 1; l <= g.nC2_; ++l)
  507. C2a[l] = A1 * C1a[l] - A2 * C2a[l];
  508. J12 = m0x * sig12 + (g.SinCosSeries(true, ssig2, csig2, C2a) -
  509. g.SinCosSeries(true, ssig1, csig1, C2a));
  510. }
  511. if (outmask & g.REDUCEDLENGTH) {
  512. vals.m0 = m0x;
  513. // Missing a factor of _b.
  514. // Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure
  515. // accurate cancellation in the case of coincident points.
  516. vals.m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) -
  517. csig1 * csig2 * J12;
  518. }
  519. if (outmask & g.GEODESICSCALE) {
  520. csig12 = csig1 * csig2 + ssig1 * ssig2;
  521. t = this._ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
  522. vals.M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
  523. vals.M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
  524. }
  525. return vals;
  526. };
  527. // return sig12, salp1, calp1, salp2, calp2, dnm
  528. g.Geodesic.prototype.InverseStart = function(sbet1, cbet1, dn1,
  529. sbet2, cbet2, dn2,
  530. lam12, slam12, clam12,
  531. C1a, C2a) {
  532. // Return a starting point for Newton's method in salp1 and calp1
  533. // (function value is -1). If Newton's method doesn't need to be
  534. // used, return also salp2 and calp2 and function value is sig12.
  535. // salp2, calp2 only updated if return val >= 0.
  536. var vals = {},
  537. // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
  538. sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
  539. cbet12 = cbet2 * cbet1 + sbet2 * sbet1,
  540. sbet12a, shortline, omg12, sbetm2, somg12, comg12, t, ssig12, csig12,
  541. x, y, lamscale, betscale, k2, eps, cbet12a, bet12a, m12b, m0, nvals,
  542. k, omg12a, lam12x;
  543. vals.sig12 = -1; // Return value
  544. // Volatile declaration needed to fix inverse cases
  545. // 88.202499451857 0 -88.202499451857 179.981022032992859592
  546. // 89.262080389218 0 -89.262080389218 179.992207982775375662
  547. // 89.333123580033 0 -89.333123580032997687 179.99295812360148422
  548. // which otherwise fail with g++ 4.4.4 x86 -O3
  549. sbet12a = sbet2 * cbet1;
  550. sbet12a += cbet2 * sbet1;
  551. shortline = cbet12 >= 0 && sbet12 < 0.5 && cbet2 * lam12 < 0.5;
  552. if (shortline) {
  553. sbetm2 = m.sq(sbet1 + sbet2);
  554. // sin((bet1+bet2)/2)^2
  555. // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
  556. sbetm2 /= sbetm2 + m.sq(cbet1 + cbet2);
  557. vals.dnm = Math.sqrt(1 + this._ep2 * sbetm2);
  558. omg12 = lam12 / (this._f1 * vals.dnm);
  559. somg12 = Math.sin(omg12); comg12 = Math.cos(omg12);
  560. } else {
  561. somg12 = slam12; comg12 = clam12;
  562. }
  563. vals.salp1 = cbet2 * somg12;
  564. vals.calp1 = comg12 >= 0 ?
  565. sbet12 + cbet2 * sbet1 * m.sq(somg12) / (1 + comg12) :
  566. sbet12a - cbet2 * sbet1 * m.sq(somg12) / (1 - comg12);
  567. ssig12 = m.hypot(vals.salp1, vals.calp1);
  568. csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
  569. if (shortline && ssig12 < this._etol2) {
  570. // really short lines
  571. vals.salp2 = cbet1 * somg12;
  572. vals.calp2 = sbet12 - cbet1 * sbet2 *
  573. (comg12 >= 0 ? m.sq(somg12) / (1 + comg12) : 1 - comg12);
  574. // norm(vals.salp2, vals.calp2);
  575. t = m.hypot(vals.salp2, vals.calp2); vals.salp2 /= t; vals.calp2 /= t;
  576. // Set return value
  577. vals.sig12 = Math.atan2(ssig12, csig12);
  578. } else if (Math.abs(this._n) > 0.1 || // Skip astroid calc if too eccentric
  579. csig12 >= 0 ||
  580. ssig12 >= 6 * Math.abs(this._n) * Math.PI * m.sq(cbet1)) {
  581. // Nothing to do, zeroth order spherical approximation is OK
  582. } else {
  583. // Scale lam12 and bet2 to x, y coordinate system where antipodal
  584. // point is at origin and singular point is at y = 0, x = -1.
  585. lam12x = Math.atan2(-slam12, -clam12); // lam12 - pi
  586. if (this.f >= 0) { // In fact f == 0 does not get here
  587. // x = dlong, y = dlat
  588. k2 = m.sq(sbet1) * this._ep2;
  589. eps = k2 / (2 * (1 + Math.sqrt(1 + k2)) + k2);
  590. lamscale = this.f * cbet1 * this.A3f(eps) * Math.PI;
  591. betscale = lamscale * cbet1;
  592. x = lam12x / lamscale;
  593. y = sbet12a / betscale;
  594. } else { // f < 0
  595. // x = dlat, y = dlong
  596. cbet12a = cbet2 * cbet1 - sbet2 * sbet1;
  597. bet12a = Math.atan2(sbet12a, cbet12a);
  598. // In the case of lon12 = 180, this repeats a calculation made
  599. // in Inverse.
  600. nvals = this.Lengths(this._n, Math.PI + bet12a,
  601. sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
  602. cbet1, cbet2, g.REDUCEDLENGTH, C1a, C2a);
  603. m12b = nvals.m12b; m0 = nvals.m0;
  604. x = -1 + m12b / (cbet1 * cbet2 * m0 * Math.PI);
  605. betscale = x < -0.01 ? sbet12a / x :
  606. -this.f * m.sq(cbet1) * Math.PI;
  607. lamscale = betscale / cbet1;
  608. y = lam12 / lamscale;
  609. }
  610. if (y > -tol1_ && x > -1 - xthresh_) {
  611. // strip near cut
  612. if (this.f >= 0) {
  613. vals.salp1 = Math.min(1, -x);
  614. vals.calp1 = -Math.sqrt(1 - m.sq(vals.salp1));
  615. } else {
  616. vals.calp1 = Math.max(x > -tol1_ ? 0 : -1, x);
  617. vals.salp1 = Math.sqrt(1 - m.sq(vals.calp1));
  618. }
  619. } else {
  620. // Estimate alp1, by solving the astroid problem.
  621. //
  622. // Could estimate alpha1 = theta + pi/2, directly, i.e.,
  623. // calp1 = y/k; salp1 = -x/(1+k); for f >= 0
  624. // calp1 = x/(1+k); salp1 = -y/k; for f < 0 (need to check)
  625. //
  626. // However, it's better to estimate omg12 from astroid and use
  627. // spherical formula to compute alp1. This reduces the mean number of
  628. // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
  629. // (min 0 max 5). The changes in the number of iterations are as
  630. // follows:
  631. //
  632. // change percent
  633. // 1 5
  634. // 0 78
  635. // -1 16
  636. // -2 0.6
  637. // -3 0.04
  638. // -4 0.002
  639. //
  640. // The histogram of iterations is (m = number of iterations estimating
  641. // alp1 directly, n = number of iterations estimating via omg12, total
  642. // number of trials = 148605):
  643. //
  644. // iter m n
  645. // 0 148 186
  646. // 1 13046 13845
  647. // 2 93315 102225
  648. // 3 36189 32341
  649. // 4 5396 7
  650. // 5 455 1
  651. // 6 56 0
  652. //
  653. // Because omg12 is near pi, estimate work with omg12a = pi - omg12
  654. k = astroid(x, y);
  655. omg12a = lamscale * ( this.f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
  656. somg12 = Math.sin(omg12a); comg12 = -Math.cos(omg12a);
  657. // Update spherical estimate of alp1 using omg12 instead of
  658. // lam12
  659. vals.salp1 = cbet2 * somg12;
  660. vals.calp1 = sbet12a -
  661. cbet2 * sbet1 * m.sq(somg12) / (1 - comg12);
  662. }
  663. }
  664. // Sanity check on starting guess. Backwards check allows NaN through.
  665. // jshint -W018
  666. if (!(vals.salp1 <= 0)) {
  667. // norm(vals.salp1, vals.calp1);
  668. t = m.hypot(vals.salp1, vals.calp1); vals.salp1 /= t; vals.calp1 /= t;
  669. } else {
  670. vals.salp1 = 1; vals.calp1 = 0;
  671. }
  672. return vals;
  673. };
  674. // return lam12, salp2, calp2, sig12, ssig1, csig1, ssig2, csig2, eps,
  675. // domg12, dlam12,
  676. g.Geodesic.prototype.Lambda12 = function(sbet1, cbet1, dn1,
  677. sbet2, cbet2, dn2,
  678. salp1, calp1, slam120, clam120,
  679. diffp, C1a, C2a, C3a) {
  680. var vals = {},
  681. t, salp0, calp0,
  682. somg1, comg1, somg2, comg2, somg12, comg12, B312, eta, k2, nvals;
  683. if (sbet1 === 0 && calp1 === 0)
  684. // Break degeneracy of equatorial line. This case has already been
  685. // handled.
  686. calp1 = -g.tiny_;
  687. // sin(alp1) * cos(bet1) = sin(alp0)
  688. salp0 = salp1 * cbet1;
  689. calp0 = m.hypot(calp1, salp1 * sbet1); // calp0 > 0
  690. // tan(bet1) = tan(sig1) * cos(alp1)
  691. // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
  692. vals.ssig1 = sbet1; somg1 = salp0 * sbet1;
  693. vals.csig1 = comg1 = calp1 * cbet1;
  694. // norm(vals.ssig1, vals.csig1);
  695. t = m.hypot(vals.ssig1, vals.csig1); vals.ssig1 /= t; vals.csig1 /= t;
  696. // norm(somg1, comg1); -- don't need to normalize!
  697. // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
  698. // about this case, since this can yield singularities in the Newton
  699. // iteration.
  700. // sin(alp2) * cos(bet2) = sin(alp0)
  701. vals.salp2 = cbet2 !== cbet1 ? salp0 / cbet2 : salp1;
  702. // calp2 = sqrt(1 - sq(salp2))
  703. // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
  704. // and subst for calp0 and rearrange to give (choose positive sqrt
  705. // to give alp2 in [0, pi/2]).
  706. vals.calp2 = cbet2 !== cbet1 || Math.abs(sbet2) !== -sbet1 ?
  707. Math.sqrt(m.sq(calp1 * cbet1) + (cbet1 < -sbet1 ?
  708. (cbet2 - cbet1) * (cbet1 + cbet2) :
  709. (sbet1 - sbet2) * (sbet1 + sbet2))) /
  710. cbet2 : Math.abs(calp1);
  711. // tan(bet2) = tan(sig2) * cos(alp2)
  712. // tan(omg2) = sin(alp0) * tan(sig2).
  713. vals.ssig2 = sbet2; somg2 = salp0 * sbet2;
  714. vals.csig2 = comg2 = vals.calp2 * cbet2;
  715. // norm(vals.ssig2, vals.csig2);
  716. t = m.hypot(vals.ssig2, vals.csig2); vals.ssig2 /= t; vals.csig2 /= t;
  717. // norm(somg2, comg2); -- don't need to normalize!
  718. // sig12 = sig2 - sig1, limit to [0, pi]
  719. vals.sig12 = Math.atan2(Math.max(0, vals.csig1 * vals.ssig2 -
  720. vals.ssig1 * vals.csig2),
  721. vals.csig1 * vals.csig2 +
  722. vals.ssig1 * vals.ssig2);
  723. // omg12 = omg2 - omg1, limit to [0, pi]
  724. somg12 = Math.max(0, comg1 * somg2 - somg1 * comg2);
  725. comg12 = comg1 * comg2 + somg1 * somg2;
  726. // eta = omg12 - lam120
  727. eta = Math.atan2(somg12 * clam120 - comg12 * slam120,
  728. comg12 * clam120 + somg12 * slam120);
  729. k2 = m.sq(calp0) * this._ep2;
  730. vals.eps = k2 / (2 * (1 + Math.sqrt(1 + k2)) + k2);
  731. this.C3f(vals.eps, C3a);
  732. B312 = (g.SinCosSeries(true, vals.ssig2, vals.csig2, C3a) -
  733. g.SinCosSeries(true, vals.ssig1, vals.csig1, C3a));
  734. vals.domg12 = -this.f * this.A3f(vals.eps) * salp0 * (vals.sig12 + B312);
  735. vals.lam12 = eta + vals.domg12;
  736. if (diffp) {
  737. if (vals.calp2 === 0)
  738. vals.dlam12 = -2 * this._f1 * dn1 / sbet1;
  739. else {
  740. nvals = this.Lengths(vals.eps, vals.sig12,
  741. vals.ssig1, vals.csig1, dn1,
  742. vals.ssig2, vals.csig2, dn2,
  743. cbet1, cbet2, g.REDUCEDLENGTH, C1a, C2a);
  744. vals.dlam12 = nvals.m12b;
  745. vals.dlam12 *= this._f1 / (vals.calp2 * cbet2);
  746. }
  747. }
  748. return vals;
  749. };
  750. /**
  751. * @summary Solve the inverse geodesic problem.
  752. * @param {number} lat1 the latitude of the first point in degrees.
  753. * @param {number} lon1 the longitude of the first point in degrees.
  754. * @param {number} lat2 the latitude of the second point in degrees.
  755. * @param {number} lon2 the longitude of the second point in degrees.
  756. * @param {bitmask} [outmask = STANDARD] which results to include.
  757. * @return {object} the requested results
  758. * @description The lat1, lon1, lat2, lon2, and a12 fields of the result are
  759. * always set. For details on the outmask parameter, see {@tutorial
  760. * 2-interface}, "The outmask and caps parameters".
  761. */
  762. g.Geodesic.prototype.Inverse = function(lat1, lon1, lat2, lon2, outmask) {
  763. var r, vals;
  764. if (!outmask) outmask = g.STANDARD;
  765. if (outmask === g.LONG_UNROLL) outmask |= g.STANDARD;
  766. outmask &= g.OUT_MASK;
  767. r = this.InverseInt(lat1, lon1, lat2, lon2, outmask);
  768. vals = r.vals;
  769. if (outmask & g.AZIMUTH) {
  770. vals.azi1 = m.atan2d(r.salp1, r.calp1);
  771. vals.azi2 = m.atan2d(r.salp2, r.calp2);
  772. }
  773. return vals;
  774. };
  775. g.Geodesic.prototype.InverseInt = function(lat1, lon1, lat2, lon2, outmask) {
  776. var vals = {},
  777. lon12, lon12s, lonsign, t, swapp, latsign,
  778. sbet1, cbet1, sbet2, cbet2, s12x, m12x,
  779. dn1, dn2, lam12, slam12, clam12,
  780. sig12, calp1, salp1, calp2, salp2, C1a, C2a, C3a, meridian, nvals,
  781. ssig1, csig1, ssig2, csig2, eps, omg12, dnm,
  782. numit, salp1a, calp1a, salp1b, calp1b,
  783. tripn, tripb, v, dv, dalp1, sdalp1, cdalp1, nsalp1,
  784. lengthmask, salp0, calp0, alp12, k2, A4, C4a, B41, B42,
  785. somg12, comg12, domg12, dbet1, dbet2, salp12, calp12, sdomg12, cdomg12;
  786. // Compute longitude difference (AngDiff does this carefully). Result is
  787. // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
  788. // east-going and meridional geodesics.
  789. vals.lat1 = lat1 = m.LatFix(lat1); vals.lat2 = lat2 = m.LatFix(lat2);
  790. // If really close to the equator, treat as on equator.
  791. lat1 = m.AngRound(lat1);
  792. lat2 = m.AngRound(lat2);
  793. lon12 = m.AngDiff(lon1, lon2); lon12s = lon12.e; lon12 = lon12.d;
  794. if (outmask & g.LONG_UNROLL) {
  795. vals.lon1 = lon1; vals.lon2 = (lon1 + lon12) + lon12s;
  796. } else {
  797. vals.lon1 = m.AngNormalize(lon1); vals.lon2 = m.AngNormalize(lon2);
  798. }
  799. // Make longitude difference positive.
  800. lonsign = m.copysign(1, lon12);
  801. lon12 *= lonsign; lon12s *= lonsign;
  802. lam12 = lon12 * m.degree;
  803. // Calculate sincos of lon12 + error (this applies AngRound internally).
  804. t = m.sincosde(lon12, lon12s); slam12 = t.s; clam12 = t.c;
  805. lon12s = (180 - lon12) - lon12s; // the supplementary longitude difference
  806. // Swap points so that point with higher (abs) latitude is point 1
  807. // If one latitude is a nan, then it becomes lat1.
  808. swapp = Math.abs(lat1) < Math.abs(lat2) || isNaN(lat2) ? -1 : 1;
  809. if (swapp < 0) {
  810. lonsign *= -1;
  811. [lat2, lat1] = [lat1, lat2]; // swap(lat1, lat2);
  812. }
  813. // Make lat1 <= 0
  814. latsign = m.copysign(1, -lat1);
  815. lat1 *= latsign;
  816. lat2 *= latsign;
  817. // Now we have
  818. //
  819. // 0 <= lon12 <= 180
  820. // -90 <= lat1 <= 0
  821. // lat1 <= lat2 <= -lat1
  822. //
  823. // longsign, swapp, latsign register the transformation to bring the
  824. // coordinates to this canonical form. In all cases, 1 means no change was
  825. // made. We make these transformations so that there are few cases to
  826. // check, e.g., on verifying quadrants in atan2. In addition, this
  827. // enforces some symmetries in the results returned.
  828. t = m.sincosd(lat1); sbet1 = this._f1 * t.s; cbet1 = t.c;
  829. // norm(sbet1, cbet1);
  830. t = m.hypot(sbet1, cbet1); sbet1 /= t; cbet1 /= t;
  831. // Ensure cbet1 = +epsilon at poles
  832. cbet1 = Math.max(g.tiny_, cbet1);
  833. t = m.sincosd(lat2); sbet2 = this._f1 * t.s; cbet2 = t.c;
  834. // norm(sbet2, cbet2);
  835. t = m.hypot(sbet2, cbet2); sbet2 /= t; cbet2 /= t;
  836. // Ensure cbet2 = +epsilon at poles
  837. cbet2 = Math.max(g.tiny_, cbet2);
  838. // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
  839. // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
  840. // a better measure. This logic is used in assigning calp2 in Lambda12.
  841. // Sometimes these quantities vanish and in that case we force bet2 = +/-
  842. // bet1 exactly. An example where is is necessary is the inverse problem
  843. // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
  844. // which failed with Visual Studio 10 (Release and Debug)
  845. if (cbet1 < -sbet1) {
  846. if (cbet2 === cbet1)
  847. sbet2 = m.copysign(sbet1, sbet2);
  848. } else {
  849. if (Math.abs(sbet2) === -sbet1)
  850. cbet2 = cbet1;
  851. }
  852. dn1 = Math.sqrt(1 + this._ep2 * m.sq(sbet1));
  853. dn2 = Math.sqrt(1 + this._ep2 * m.sq(sbet2));
  854. // index zero elements of these arrays are unused
  855. C1a = new Array(g.nC1_ + 1);
  856. C2a = new Array(g.nC2_ + 1);
  857. C3a = new Array(g.nC3_);
  858. meridian = lat1 === -90 || slam12 === 0;
  859. if (meridian) {
  860. // Endpoints are on a single full meridian, so the geodesic might
  861. // lie on a meridian.
  862. calp1 = clam12; salp1 = slam12; // Head to the target longitude
  863. calp2 = 1; salp2 = 0; // At the target we're heading north
  864. // tan(bet) = tan(sig) * cos(alp)
  865. ssig1 = sbet1; csig1 = calp1 * cbet1;
  866. ssig2 = sbet2; csig2 = calp2 * cbet2;
  867. // sig12 = sig2 - sig1
  868. sig12 = Math.atan2(Math.max(0, csig1 * ssig2 - ssig1 * csig2),
  869. csig1 * csig2 + ssig1 * ssig2);
  870. nvals = this.Lengths(this._n, sig12,
  871. ssig1, csig1, dn1, ssig2, csig2, dn2, cbet1, cbet2,
  872. outmask | g.DISTANCE | g.REDUCEDLENGTH,
  873. C1a, C2a);
  874. s12x = nvals.s12b;
  875. m12x = nvals.m12b;
  876. // Ignore m0
  877. if (outmask & g.GEODESICSCALE) {
  878. vals.M12 = nvals.M12;
  879. vals.M21 = nvals.M21;
  880. }
  881. // Add the check for sig12 since zero length geodesics might yield
  882. // m12 < 0. Test case was
  883. //
  884. // echo 20.001 0 20.001 0 | GeodSolve -i
  885. //
  886. // In fact, we will have sig12 > pi/2 for meridional geodesic
  887. // which is not a shortest path.
  888. if (sig12 < 1 || m12x >= 0) {
  889. // Need at least 2, to handle 90 0 90 180
  890. if (sig12 < 3 * g.tiny_ ||
  891. // Prevent negative s12 or m12 for short lines
  892. (sig12 < tol0_ && (s12x < 0 || m12x < 0)))
  893. sig12 = m12x = s12x = 0;
  894. m12x *= this._b;
  895. s12x *= this._b;
  896. vals.a12 = sig12 / m.degree;
  897. } else
  898. // m12 < 0, i.e., prolate and too close to anti-podal
  899. meridian = false;
  900. }
  901. somg12 = 2;
  902. if (!meridian &&
  903. sbet1 === 0 && // and sbet2 == 0
  904. (this.f <= 0 || lon12s >= this.f * 180)) {
  905. // Geodesic runs along equator
  906. calp1 = calp2 = 0; salp1 = salp2 = 1;
  907. s12x = this.a * lam12;
  908. sig12 = omg12 = lam12 / this._f1;
  909. m12x = this._b * Math.sin(sig12);
  910. if (outmask & g.GEODESICSCALE)
  911. vals.M12 = vals.M21 = Math.cos(sig12);
  912. vals.a12 = lon12 / this._f1;
  913. } else if (!meridian) {
  914. // Now point1 and point2 belong within a hemisphere bounded by a
  915. // meridian and geodesic is neither meridional or equatorial.
  916. // Figure a starting point for Newton's method
  917. nvals = this.InverseStart(sbet1, cbet1, dn1, sbet2, cbet2, dn2,
  918. lam12, slam12, clam12, C1a, C2a);
  919. sig12 = nvals.sig12;
  920. salp1 = nvals.salp1;
  921. calp1 = nvals.calp1;
  922. if (sig12 >= 0) {
  923. salp2 = nvals.salp2;
  924. calp2 = nvals.calp2;
  925. // Short lines (InverseStart sets salp2, calp2, dnm)
  926. dnm = nvals.dnm;
  927. s12x = sig12 * this._b * dnm;
  928. m12x = m.sq(dnm) * this._b * Math.sin(sig12 / dnm);
  929. if (outmask & g.GEODESICSCALE)
  930. vals.M12 = vals.M21 = Math.cos(sig12 / dnm);
  931. vals.a12 = sig12 / m.degree;
  932. omg12 = lam12 / (this._f1 * dnm);
  933. } else {
  934. // Newton's method. This is a straightforward solution of f(alp1) =
  935. // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
  936. // root in the interval (0, pi) and its derivative is positive at the
  937. // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
  938. // alp1. During the course of the iteration, a range (alp1a, alp1b) is
  939. // maintained which brackets the root and with each evaluation of
  940. // f(alp) the range is shrunk if possible. Newton's method is
  941. // restarted whenever the derivative of f is negative (because the new
  942. // value of alp1 is then further from the solution) or if the new
  943. // estimate of alp1 lies outside (0,pi); in this case, the new starting
  944. // guess is taken to be (alp1a + alp1b) / 2.
  945. numit = 0;
  946. // Bracketing range
  947. salp1a = g.tiny_; calp1a = 1; salp1b = g.tiny_; calp1b = -1;
  948. for (tripn = false, tripb = false;; ++numit) {
  949. // the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
  950. // WGS84 and random input: mean = 2.85, sd = 0.60
  951. nvals = this.Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2,
  952. salp1, calp1, slam12, clam12, numit < maxit1_,
  953. C1a, C2a, C3a);
  954. v = nvals.lam12;
  955. salp2 = nvals.salp2;
  956. calp2 = nvals.calp2;
  957. sig12 = nvals.sig12;
  958. ssig1 = nvals.ssig1;
  959. csig1 = nvals.csig1;
  960. ssig2 = nvals.ssig2;
  961. csig2 = nvals.csig2;
  962. eps = nvals.eps;
  963. domg12 = nvals.domg12;
  964. dv = nvals.dlam12;
  965. // Reversed test to allow escape with NaNs
  966. // jshint -W018
  967. if (tripb || !(Math.abs(v) >= (tripn ? 8 : 1) * tol0_) ||
  968. numit == maxit2_)
  969. break;
  970. // Update bracketing values
  971. if (v > 0 && (numit < maxit1_ || calp1/salp1 > calp1b/salp1b)) {
  972. salp1b = salp1; calp1b = calp1;
  973. } else if (v < 0 &&
  974. (numit < maxit1_ || calp1/salp1 < calp1a/salp1a)) {
  975. salp1a = salp1; calp1a = calp1;
  976. }
  977. if (numit < maxit1_ && dv > 0) {
  978. dalp1 = -v/dv;
  979. if (Math.abs(dalp1) < Math.PI) {
  980. sdalp1 = Math.sin(dalp1); cdalp1 = Math.cos(dalp1);
  981. nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
  982. if (nsalp1 > 0) {
  983. calp1 = calp1 * cdalp1 - salp1 * sdalp1;
  984. salp1 = nsalp1;
  985. // norm(salp1, calp1);
  986. t = m.hypot(salp1, calp1); salp1 /= t; calp1 /= t;
  987. // In some regimes we don't get quadratic convergence because
  988. // slope -> 0. So use convergence conditions based on epsilon
  989. // instead of sqrt(epsilon).
  990. tripn = Math.abs(v) <= 16 * tol0_;
  991. continue;
  992. }
  993. }
  994. }
  995. // Either dv was not positive or updated value was outside legal
  996. // range. Use the midpoint of the bracket as the next estimate.
  997. // This mechanism is not needed for the WGS84 ellipsoid, but it does
  998. // catch problems with more eccentric ellipsoids. Its efficacy is
  999. // such for the WGS84 test set with the starting guess set to alp1 =
  1000. // 90deg:
  1001. // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
  1002. // WGS84 and random input: mean = 4.74, sd = 0.99
  1003. salp1 = (salp1a + salp1b)/2;
  1004. calp1 = (calp1a + calp1b)/2;
  1005. // norm(salp1, calp1);
  1006. t = m.hypot(salp1, calp1); salp1 /= t; calp1 /= t;
  1007. tripn = false;
  1008. tripb = (Math.abs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
  1009. Math.abs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
  1010. }
  1011. lengthmask = outmask |
  1012. (outmask & (g.REDUCEDLENGTH | g.GEODESICSCALE) ?
  1013. g.DISTANCE : g.NONE);
  1014. nvals = this.Lengths(eps, sig12,
  1015. ssig1, csig1, dn1, ssig2, csig2, dn2,
  1016. cbet1, cbet2,
  1017. lengthmask, C1a, C2a);
  1018. s12x = nvals.s12b;
  1019. m12x = nvals.m12b;
  1020. // Ignore m0
  1021. if (outmask & g.GEODESICSCALE) {
  1022. vals.M12 = nvals.M12;
  1023. vals.M21 = nvals.M21;
  1024. }
  1025. m12x *= this._b;
  1026. s12x *= this._b;
  1027. vals.a12 = sig12 / m.degree;
  1028. if (outmask & g.AREA) {
  1029. // omg12 = lam12 - domg12
  1030. sdomg12 = Math.sin(domg12); cdomg12 = Math.cos(domg12);
  1031. somg12 = slam12 * cdomg12 - clam12 * sdomg12;
  1032. comg12 = clam12 * cdomg12 + slam12 * sdomg12;
  1033. }
  1034. }
  1035. }
  1036. if (outmask & g.DISTANCE)
  1037. vals.s12 = 0 + s12x; // Convert -0 to 0
  1038. if (outmask & g.REDUCEDLENGTH)
  1039. vals.m12 = 0 + m12x; // Convert -0 to 0
  1040. if (outmask & g.AREA) {
  1041. // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
  1042. salp0 = salp1 * cbet1;
  1043. calp0 = m.hypot(calp1, salp1 * sbet1); // calp0 > 0
  1044. if (calp0 !== 0 && salp0 !== 0) {
  1045. // From Lambda12: tan(bet) = tan(sig) * cos(alp)
  1046. ssig1 = sbet1; csig1 = calp1 * cbet1;
  1047. ssig2 = sbet2; csig2 = calp2 * cbet2;
  1048. k2 = m.sq(calp0) * this._ep2;
  1049. eps = k2 / (2 * (1 + Math.sqrt(1 + k2)) + k2);
  1050. // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
  1051. A4 = m.sq(this.a) * calp0 * salp0 * this._e2;
  1052. // norm(ssig1, csig1);
  1053. t = m.hypot(ssig1, csig1); ssig1 /= t; csig1 /= t;
  1054. // norm(ssig2, csig2);
  1055. t = m.hypot(ssig2, csig2); ssig2 /= t; csig2 /= t;
  1056. C4a = new Array(g.nC4_);
  1057. this.C4f(eps, C4a);
  1058. B41 = g.SinCosSeries(false, ssig1, csig1, C4a);
  1059. B42 = g.SinCosSeries(false, ssig2, csig2, C4a);
  1060. vals.S12 = A4 * (B42 - B41);
  1061. } else
  1062. // Avoid problems with indeterminate sig1, sig2 on equator
  1063. vals.S12 = 0;
  1064. if (!meridian && somg12 == 2) {
  1065. somg12 = Math.sin(omg12); comg12 = Math.cos(omg12);
  1066. }
  1067. if (!meridian &&
  1068. comg12 > -0.7071 && // Long difference not too big
  1069. sbet2 - sbet1 < 1.75) { // Lat difference not too big
  1070. // Use tan(Gamma/2) = tan(omg12/2)
  1071. // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
  1072. // with tan(x/2) = sin(x)/(1+cos(x))
  1073. domg12 = 1 + comg12; dbet1 = 1 + cbet1; dbet2 = 1 + cbet2;
  1074. alp12 = 2 * Math.atan2( somg12 * (sbet1*dbet2 + sbet2*dbet1),
  1075. domg12 * (sbet1*sbet2 + dbet1*dbet2) );
  1076. } else {
  1077. // alp12 = alp2 - alp1, used in atan2 so no need to normalize
  1078. salp12 = salp2 * calp1 - calp2 * salp1;
  1079. calp12 = calp2 * calp1 + salp2 * salp1;
  1080. // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
  1081. // salp12 = -0 and alp12 = -180. However this depends on the sign
  1082. // being attached to 0 correctly. The following ensures the correct
  1083. // behavior.
  1084. if (salp12 === 0 && calp12 < 0) {
  1085. salp12 = g.tiny_ * calp1;
  1086. calp12 = -1;
  1087. }
  1088. alp12 = Math.atan2(salp12, calp12);
  1089. }
  1090. vals.S12 += this._c2 * alp12;
  1091. vals.S12 *= swapp * lonsign * latsign;
  1092. // Convert -0 to 0
  1093. vals.S12 += 0;
  1094. }
  1095. // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
  1096. if (swapp < 0) {
  1097. [salp2, salp1] = [salp1, salp2]; // swap(salp1, salp2);
  1098. [calp2, calp1] = [calp1, calp2]; // swap(calp1, calp2);
  1099. if (outmask & g.GEODESICSCALE) {
  1100. [vals.M21, vals.M12] = [vals.M12, vals.M21]; //swap(vals.M12, vals.M21);
  1101. }
  1102. }
  1103. salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
  1104. salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
  1105. return {vals: vals,
  1106. salp1: salp1, calp1: calp1,
  1107. salp2: salp2, calp2: calp2};
  1108. };
  1109. /**
  1110. * @summary Solve the general direct geodesic problem.
  1111. * @param {number} lat1 the latitude of the first point in degrees.
  1112. * @param {number} lon1 the longitude of the first point in degrees.
  1113. * @param {number} azi1 the azimuth at the first point in degrees.
  1114. * @param {bool} arcmode is the next parameter an arc length?
  1115. * @param {number} s12_a12 the (arcmode ? arc length : distance) from the
  1116. * first point to the second in (arcmode ? degrees : meters).
  1117. * @param {bitmask} [outmask = STANDARD] which results to include.
  1118. * @return {object} the requested results.
  1119. * @description The lat1, lon1, azi1, and a12 fields of the result are always
  1120. * set; s12 is included if arcmode is false. For details on the outmask
  1121. * parameter, see {@tutorial 2-interface}, "The outmask and caps
  1122. * parameters".
  1123. */
  1124. g.Geodesic.prototype.GenDirect = function(lat1, lon1, azi1,
  1125. arcmode, s12_a12, outmask) {
  1126. var line;
  1127. if (!outmask) outmask = g.STANDARD;
  1128. else if (outmask === g.LONG_UNROLL) outmask |= g.STANDARD;
  1129. // Automatically supply DISTANCE_IN if necessary
  1130. if (!arcmode) outmask |= g.DISTANCE_IN;
  1131. line = new l.GeodesicLine(this, lat1, lon1, azi1, outmask);
  1132. return line.GenPosition(arcmode, s12_a12, outmask);
  1133. };
  1134. /**
  1135. * @summary Solve the direct geodesic problem.
  1136. * @param {number} lat1 the latitude of the first point in degrees.
  1137. * @param {number} lon1 the longitude of the first point in degrees.
  1138. * @param {number} azi1 the azimuth at the first point in degrees.
  1139. * @param {number} s12 the distance from the first point to the second in
  1140. * meters.
  1141. * @param {bitmask} [outmask = STANDARD] which results to include.
  1142. * @return {object} the requested results.
  1143. * @description The lat1, lon1, azi1, s12, and a12 fields of the result are
  1144. * always set. For details on the outmask parameter, see {@tutorial
  1145. * 2-interface}, "The outmask and caps parameters".
  1146. */
  1147. g.Geodesic.prototype.Direct = function(lat1, lon1, azi1, s12, outmask) {
  1148. return this.GenDirect(lat1, lon1, azi1, false, s12, outmask);
  1149. };
  1150. /**
  1151. * @summary Solve the direct geodesic problem with arc length.
  1152. * @param {number} lat1 the latitude of the first point in degrees.
  1153. * @param {number} lon1 the longitude of the first point in degrees.
  1154. * @param {number} azi1 the azimuth at the first point in degrees.
  1155. * @param {number} a12 the arc length from the first point to the second in
  1156. * degrees.
  1157. * @param {bitmask} [outmask = STANDARD] which results to include.
  1158. * @return {object} the requested results.
  1159. * @description The lat1, lon1, azi1, and a12 fields of the result are
  1160. * always set. For details on the outmask parameter, see {@tutorial
  1161. * 2-interface}, "The outmask and caps parameters".
  1162. */
  1163. g.Geodesic.prototype.ArcDirect = function(lat1, lon1, azi1, a12, outmask) {
  1164. return this.GenDirect(lat1, lon1, azi1, true, a12, outmask);
  1165. };
  1166. /**
  1167. * @summary Create a {@link module:geodesic/GeodesicLine.GeodesicLine
  1168. * GeodesicLine} object.
  1169. * @param {number} lat1 the latitude of the first point in degrees.
  1170. * @param {number} lon1 the longitude of the first point in degrees.
  1171. * @param {number} azi1 the azimuth at the first point in degrees.
  1172. * degrees.
  1173. * @param {bitmask} [caps = STANDARD | DISTANCE_IN] which capabilities to
  1174. * include.
  1175. * @return {object} the
  1176. * {@link module:geodesic/GeodesicLine.GeodesicLine
  1177. * GeodesicLine} object
  1178. * @description For details on the caps parameter, see {@tutorial
  1179. * 2-interface}, "The outmask and caps parameters".
  1180. */
  1181. g.Geodesic.prototype.Line = function(lat1, lon1, azi1, caps) {
  1182. return new l.GeodesicLine(this, lat1, lon1, azi1, caps);
  1183. };
  1184. /**
  1185. * @summary Define a {@link module:geodesic/GeodesicLine.GeodesicLine
  1186. * GeodesicLine} in terms of the direct geodesic problem specified in terms
  1187. * of distance.
  1188. * @param {number} lat1 the latitude of the first point in degrees.
  1189. * @param {number} lon1 the longitude of the first point in degrees.
  1190. * @param {number} azi1 the azimuth at the first point in degrees.
  1191. * degrees.
  1192. * @param {number} s12 the distance between point 1 and point 2 (meters); it
  1193. * can be negative.
  1194. * @param {bitmask} [caps = STANDARD | DISTANCE_IN] which capabilities to
  1195. * include.
  1196. * @return {object} the
  1197. * {@link module:geodesic/GeodesicLine.GeodesicLine
  1198. * GeodesicLine} object
  1199. * @description This function sets point 3 of the GeodesicLine to correspond
  1200. * to point 2 of the direct geodesic problem. For details on the caps
  1201. * parameter, see {@tutorial 2-interface}, "The outmask and caps
  1202. * parameters".
  1203. */
  1204. g.Geodesic.prototype.DirectLine = function(lat1, lon1, azi1, s12, caps) {
  1205. return this.GenDirectLine(lat1, lon1, azi1, false, s12, caps);
  1206. };
  1207. /**
  1208. * @summary Define a {@link module:geodesic/GeodesicLine.GeodesicLine
  1209. * GeodesicLine} in terms of the direct geodesic problem specified in terms
  1210. * of arc length.
  1211. * @param {number} lat1 the latitude of the first point in degrees.
  1212. * @param {number} lon1 the longitude of the first point in degrees.
  1213. * @param {number} azi1 the azimuth at the first point in degrees.
  1214. * degrees.
  1215. * @param {number} a12 the arc length between point 1 and point 2 (degrees);
  1216. * it can be negative.
  1217. * @param {bitmask} [caps = STANDARD | DISTANCE_IN] which capabilities to
  1218. * include.
  1219. * @return {object} the
  1220. * {@link module:geodesic/GeodesicLine.GeodesicLine
  1221. * GeodesicLine} object
  1222. * @description This function sets point 3 of the GeodesicLine to correspond
  1223. * to point 2 of the direct geodesic problem. For details on the caps
  1224. * parameter, see {@tutorial 2-interface}, "The outmask and caps
  1225. * parameters".
  1226. */
  1227. g.Geodesic.prototype.ArcDirectLine = function(lat1, lon1, azi1, a12, caps) {
  1228. return this.GenDirectLine(lat1, lon1, azi1, true, a12, caps);
  1229. };
  1230. /**
  1231. * @summary Define a {@link module:geodesic/GeodesicLine.GeodesicLine
  1232. * GeodesicLine} in terms of the direct geodesic problem specified in terms
  1233. * of either distance or arc length.
  1234. * @param {number} lat1 the latitude of the first point in degrees.
  1235. * @param {number} lon1 the longitude of the first point in degrees.
  1236. * @param {number} azi1 the azimuth at the first point in degrees.
  1237. * degrees.
  1238. * @param {bool} arcmode boolean flag determining the meaning of the
  1239. * s12_a12.
  1240. * @param {number} s12_a12 if arcmode is false, this is the distance between
  1241. * point 1 and point 2 (meters); otherwise it is the arc length between
  1242. * point 1 and point 2 (degrees); it can be negative.
  1243. * @param {bitmask} [caps = STANDARD | DISTANCE_IN] which capabilities to
  1244. * include.
  1245. * @return {object} the
  1246. * {@link module:geodesic/GeodesicLine.GeodesicLine
  1247. * GeodesicLine} object
  1248. * @description This function sets point 3 of the GeodesicLine to correspond
  1249. * to point 2 of the direct geodesic problem. For details on the caps
  1250. * parameter, see {@tutorial 2-interface}, "The outmask and caps
  1251. * parameters".
  1252. */
  1253. g.Geodesic.prototype.GenDirectLine = function(lat1, lon1, azi1,
  1254. arcmode, s12_a12, caps) {
  1255. var t;
  1256. if (!caps) caps = g.STANDARD | g.DISTANCE_IN;
  1257. // Automatically supply DISTANCE_IN if necessary
  1258. if (!arcmode) caps |= g.DISTANCE_IN;
  1259. t = new l.GeodesicLine(this, lat1, lon1, azi1, caps);
  1260. t.GenSetDistance(arcmode, s12_a12);
  1261. return t;
  1262. };
  1263. /**
  1264. * @summary Define a {@link module:geodesic/GeodesicLine.GeodesicLine
  1265. * GeodesicLine} in terms of the inverse geodesic problem.
  1266. * @param {number} lat1 the latitude of the first point in degrees.
  1267. * @param {number} lon1 the longitude of the first point in degrees.
  1268. * @param {number} lat2 the latitude of the second point in degrees.
  1269. * @param {number} lon2 the longitude of the second point in degrees.
  1270. * @param {bitmask} [caps = STANDARD | DISTANCE_IN] which capabilities to
  1271. * include.
  1272. * @return {object} the
  1273. * {@link module:geodesic/GeodesicLine.GeodesicLine
  1274. * GeodesicLine} object
  1275. * @description This function sets point 3 of the GeodesicLine to correspond
  1276. * to point 2 of the inverse geodesic problem. For details on the caps
  1277. * parameter, see {@tutorial 2-interface}, "The outmask and caps
  1278. * parameters".
  1279. */
  1280. g.Geodesic.prototype.InverseLine = function(lat1, lon1, lat2, lon2, caps) {
  1281. var r, t, azi1;
  1282. if (!caps) caps = g.STANDARD | g.DISTANCE_IN;
  1283. r = this.InverseInt(lat1, lon1, lat2, lon2, g.ARC);
  1284. azi1 = m.atan2d(r.salp1, r.calp1);
  1285. // Ensure that a12 can be converted to a distance
  1286. if (caps & (g.OUT_MASK & g.DISTANCE_IN)) caps |= g.DISTANCE;
  1287. t = new l.GeodesicLine(this, lat1, lon1, azi1, caps, r.salp1, r.calp1);
  1288. t.SetArc(r.vals.a12);
  1289. return t;
  1290. };
  1291. /**
  1292. * @summary Create a {@link module:geodesic/PolygonArea.PolygonArea
  1293. * PolygonArea} object.
  1294. * @param {bool} [polyline = false] if true the new PolygonArea object
  1295. * describes a polyline instead of a polygon.
  1296. * @return {object} the
  1297. * {@link module:geodesic/PolygonArea.PolygonArea
  1298. * PolygonArea} object
  1299. */
  1300. g.Geodesic.prototype.Polygon = function(polyline) {
  1301. return new p.PolygonArea(this, polyline);
  1302. };
  1303. /**
  1304. * @summary a {@link module:geodesic/Geodesic.Geodesic Geodesic} object
  1305. * initialized for the WGS84 ellipsoid.
  1306. * @constant {object}
  1307. */
  1308. g.WGS84 = new g.Geodesic(c.WGS84.a, c.WGS84.f);
  1309. })(geodesic.Geodesic, geodesic.GeodesicLine,
  1310. geodesic.PolygonArea, geodesic.Math, geodesic.Constants);

تعليقات

المشاركات الشائعة من هذه المدونة

ترجمة متواضعة لورقة كارني العلمية كبديل عن تقريب فينسنتي إلى اللغة العربية

المعادلات والمتسلسلات الهامة والكاملة والروابط الهامة