EllipsoidGeodesic.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartographic from './Cartographic.js';
  3. import Check from './Check.js';
  4. import defaultValue from './defaultValue.js';
  5. import defined from './defined.js';
  6. import defineProperties from './defineProperties.js';
  7. import Ellipsoid from './Ellipsoid.js';
  8. import CesiumMath from './Math.js';
  9. function setConstants(ellipsoidGeodesic) {
  10. var uSquared = ellipsoidGeodesic._uSquared;
  11. var a = ellipsoidGeodesic._ellipsoid.maximumRadius;
  12. var b = ellipsoidGeodesic._ellipsoid.minimumRadius;
  13. var f = (a - b) / a;
  14. var cosineHeading = Math.cos(ellipsoidGeodesic._startHeading);
  15. var sineHeading = Math.sin(ellipsoidGeodesic._startHeading);
  16. var tanU = (1 - f) * Math.tan(ellipsoidGeodesic._start.latitude);
  17. var cosineU = 1.0 / Math.sqrt(1.0 + tanU * tanU);
  18. var sineU = cosineU * tanU;
  19. var sigma = Math.atan2(tanU, cosineHeading);
  20. var sineAlpha = cosineU * sineHeading;
  21. var sineSquaredAlpha = sineAlpha * sineAlpha;
  22. var cosineSquaredAlpha = 1.0 - sineSquaredAlpha;
  23. var cosineAlpha = Math.sqrt(cosineSquaredAlpha);
  24. var u2Over4 = uSquared / 4.0;
  25. var u4Over16 = u2Over4 * u2Over4;
  26. var u6Over64 = u4Over16 * u2Over4;
  27. var u8Over256 = u4Over16 * u4Over16;
  28. var a0 = (1.0 + u2Over4 - 3.0 * u4Over16 / 4.0 + 5.0 * u6Over64 / 4.0 - 175.0 * u8Over256 / 64.0);
  29. var a1 = (1.0 - u2Over4 + 15.0 * u4Over16 / 8.0 - 35.0 * u6Over64 / 8.0);
  30. var a2 = (1.0 - 3.0 * u2Over4 + 35.0 * u4Over16 / 4.0);
  31. var a3 = (1.0 - 5.0 * u2Over4);
  32. var distanceRatio = a0 * sigma - a1 * Math.sin(2.0 * sigma) * u2Over4 / 2.0 - a2 * Math.sin(4.0 * sigma) * u4Over16 / 16.0 -
  33. a3 * Math.sin(6.0 * sigma) * u6Over64 / 48.0 - Math.sin(8.0 * sigma) * 5.0 * u8Over256 / 512;
  34. var constants = ellipsoidGeodesic._constants;
  35. constants.a = a;
  36. constants.b = b;
  37. constants.f = f;
  38. constants.cosineHeading = cosineHeading;
  39. constants.sineHeading = sineHeading;
  40. constants.tanU = tanU;
  41. constants.cosineU = cosineU;
  42. constants.sineU = sineU;
  43. constants.sigma = sigma;
  44. constants.sineAlpha = sineAlpha;
  45. constants.sineSquaredAlpha = sineSquaredAlpha;
  46. constants.cosineSquaredAlpha = cosineSquaredAlpha;
  47. constants.cosineAlpha = cosineAlpha;
  48. constants.u2Over4 = u2Over4;
  49. constants.u4Over16 = u4Over16;
  50. constants.u6Over64 = u6Over64;
  51. constants.u8Over256 = u8Over256;
  52. constants.a0 = a0;
  53. constants.a1 = a1;
  54. constants.a2 = a2;
  55. constants.a3 = a3;
  56. constants.distanceRatio = distanceRatio;
  57. }
  58. function computeC(f, cosineSquaredAlpha) {
  59. return f * cosineSquaredAlpha * (4.0 + f * (4.0 - 3.0 * cosineSquaredAlpha)) / 16.0;
  60. }
  61. function computeDeltaLambda(f, sineAlpha, cosineSquaredAlpha, sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint) {
  62. var C = computeC(f, cosineSquaredAlpha);
  63. return (1.0 - C) * f * sineAlpha * (sigma + C * sineSigma * (cosineTwiceSigmaMidpoint +
  64. C * cosineSigma * (2.0 * cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint - 1.0)));
  65. }
  66. function vincentyInverseFormula(ellipsoidGeodesic, major, minor, firstLongitude, firstLatitude, secondLongitude, secondLatitude) {
  67. var eff = (major - minor) / major;
  68. var l = secondLongitude - firstLongitude;
  69. var u1 = Math.atan((1 - eff) * Math.tan(firstLatitude));
  70. var u2 = Math.atan((1 - eff) * Math.tan(secondLatitude));
  71. var cosineU1 = Math.cos(u1);
  72. var sineU1 = Math.sin(u1);
  73. var cosineU2 = Math.cos(u2);
  74. var sineU2 = Math.sin(u2);
  75. var cc = cosineU1 * cosineU2;
  76. var cs = cosineU1 * sineU2;
  77. var ss = sineU1 * sineU2;
  78. var sc = sineU1 * cosineU2;
  79. var lambda = l;
  80. var lambdaDot = CesiumMath.TWO_PI;
  81. var cosineLambda = Math.cos(lambda);
  82. var sineLambda = Math.sin(lambda);
  83. var sigma;
  84. var cosineSigma;
  85. var sineSigma;
  86. var cosineSquaredAlpha;
  87. var cosineTwiceSigmaMidpoint;
  88. do {
  89. cosineLambda = Math.cos(lambda);
  90. sineLambda = Math.sin(lambda);
  91. var temp = cs - sc * cosineLambda;
  92. sineSigma = Math.sqrt(cosineU2 * cosineU2 * sineLambda * sineLambda + temp * temp);
  93. cosineSigma = ss + cc * cosineLambda;
  94. sigma = Math.atan2(sineSigma, cosineSigma);
  95. var sineAlpha;
  96. if (sineSigma === 0.0) {
  97. sineAlpha = 0.0;
  98. cosineSquaredAlpha = 1.0;
  99. } else {
  100. sineAlpha = cc * sineLambda / sineSigma;
  101. cosineSquaredAlpha = 1.0 - sineAlpha * sineAlpha;
  102. }
  103. lambdaDot = lambda;
  104. cosineTwiceSigmaMidpoint = cosineSigma - 2.0 * ss / cosineSquaredAlpha;
  105. if (isNaN(cosineTwiceSigmaMidpoint)) {
  106. cosineTwiceSigmaMidpoint = 0.0;
  107. }
  108. lambda = l + computeDeltaLambda(eff, sineAlpha, cosineSquaredAlpha,
  109. sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint);
  110. } while (Math.abs(lambda - lambdaDot) > CesiumMath.EPSILON12);
  111. var uSquared = cosineSquaredAlpha * (major * major - minor * minor) / (minor * minor);
  112. var A = 1.0 + uSquared * (4096.0 + uSquared * (uSquared * (320.0 - 175.0 * uSquared) - 768.0)) / 16384.0;
  113. var B = uSquared * (256.0 + uSquared * (uSquared * (74.0 - 47.0 * uSquared) - 128.0)) / 1024.0;
  114. var cosineSquaredTwiceSigmaMidpoint = cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint;
  115. var deltaSigma = B * sineSigma * (cosineTwiceSigmaMidpoint + B * (cosineSigma *
  116. (2.0 * cosineSquaredTwiceSigmaMidpoint - 1.0) - B * cosineTwiceSigmaMidpoint *
  117. (4.0 * sineSigma * sineSigma - 3.0) * (4.0 * cosineSquaredTwiceSigmaMidpoint - 3.0) / 6.0) / 4.0);
  118. var distance = minor * A * (sigma - deltaSigma);
  119. var startHeading = Math.atan2(cosineU2 * sineLambda, cs - sc * cosineLambda);
  120. var endHeading = Math.atan2(cosineU1 * sineLambda, cs * cosineLambda - sc);
  121. ellipsoidGeodesic._distance = distance;
  122. ellipsoidGeodesic._startHeading = startHeading;
  123. ellipsoidGeodesic._endHeading = endHeading;
  124. ellipsoidGeodesic._uSquared = uSquared;
  125. }
  126. var scratchCart1 = new Cartesian3();
  127. var scratchCart2 = new Cartesian3();
  128. function computeProperties(ellipsoidGeodesic, start, end, ellipsoid) {
  129. var firstCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(start, scratchCart2), scratchCart1);
  130. var lastCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(end, scratchCart2), scratchCart2);
  131. //>>includeStart('debug', pragmas.debug);
  132. Check.typeOf.number.greaterThanOrEquals('value', Math.abs(Math.abs(Cartesian3.angleBetween(firstCartesian, lastCartesian)) - Math.PI), 0.0125);
  133. //>>includeEnd('debug');
  134. vincentyInverseFormula(ellipsoidGeodesic, ellipsoid.maximumRadius, ellipsoid.minimumRadius,
  135. start.longitude, start.latitude, end.longitude, end.latitude);
  136. ellipsoidGeodesic._start = Cartographic.clone(start, ellipsoidGeodesic._start);
  137. ellipsoidGeodesic._end = Cartographic.clone(end, ellipsoidGeodesic._end);
  138. ellipsoidGeodesic._start.height = 0;
  139. ellipsoidGeodesic._end.height = 0;
  140. setConstants(ellipsoidGeodesic);
  141. }
  142. /**
  143. * Initializes a geodesic on the ellipsoid connecting the two provided planetodetic points.
  144. *
  145. * @alias EllipsoidGeodesic
  146. * @constructor
  147. *
  148. * @param {Cartographic} [start] The initial planetodetic point on the path.
  149. * @param {Cartographic} [end] The final planetodetic point on the path.
  150. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the geodesic lies.
  151. */
  152. function EllipsoidGeodesic(start, end, ellipsoid) {
  153. var e = defaultValue(ellipsoid, Ellipsoid.WGS84);
  154. this._ellipsoid = e;
  155. this._start = new Cartographic();
  156. this._end = new Cartographic();
  157. this._constants = {};
  158. this._startHeading = undefined;
  159. this._endHeading = undefined;
  160. this._distance = undefined;
  161. this._uSquared = undefined;
  162. if (defined(start) && defined(end)) {
  163. computeProperties(this, start, end, e);
  164. }
  165. }
  166. defineProperties(EllipsoidGeodesic.prototype, {
  167. /**
  168. * Gets the ellipsoid.
  169. * @memberof EllipsoidGeodesic.prototype
  170. * @type {Ellipsoid}
  171. * @readonly
  172. */
  173. ellipsoid : {
  174. get : function() {
  175. return this._ellipsoid;
  176. }
  177. },
  178. /**
  179. * Gets the surface distance between the start and end point
  180. * @memberof EllipsoidGeodesic.prototype
  181. * @type {Number}
  182. * @readonly
  183. */
  184. surfaceDistance : {
  185. get : function() {
  186. //>>includeStart('debug', pragmas.debug);
  187. Check.defined('distance', this._distance);
  188. //>>includeEnd('debug');
  189. return this._distance;
  190. }
  191. },
  192. /**
  193. * Gets the initial planetodetic point on the path.
  194. * @memberof EllipsoidGeodesic.prototype
  195. * @type {Cartographic}
  196. * @readonly
  197. */
  198. start : {
  199. get : function() {
  200. return this._start;
  201. }
  202. },
  203. /**
  204. * Gets the final planetodetic point on the path.
  205. * @memberof EllipsoidGeodesic.prototype
  206. * @type {Cartographic}
  207. * @readonly
  208. */
  209. end : {
  210. get : function() {
  211. return this._end;
  212. }
  213. },
  214. /**
  215. * Gets the heading at the initial point.
  216. * @memberof EllipsoidGeodesic.prototype
  217. * @type {Number}
  218. * @readonly
  219. */
  220. startHeading : {
  221. get : function() {
  222. //>>includeStart('debug', pragmas.debug);
  223. Check.defined('distance', this._distance);
  224. //>>includeEnd('debug');
  225. return this._startHeading;
  226. }
  227. },
  228. /**
  229. * Gets the heading at the final point.
  230. * @memberof EllipsoidGeodesic.prototype
  231. * @type {Number}
  232. * @readonly
  233. */
  234. endHeading : {
  235. get : function() {
  236. //>>includeStart('debug', pragmas.debug);
  237. Check.defined('distance', this._distance);
  238. //>>includeEnd('debug');
  239. return this._endHeading;
  240. }
  241. }
  242. });
  243. /**
  244. * Sets the start and end points of the geodesic
  245. *
  246. * @param {Cartographic} start The initial planetodetic point on the path.
  247. * @param {Cartographic} end The final planetodetic point on the path.
  248. */
  249. EllipsoidGeodesic.prototype.setEndPoints = function(start, end) {
  250. //>>includeStart('debug', pragmas.debug);
  251. Check.defined('start', start);
  252. Check.defined('end', end);
  253. //>>includeEnd('debug');
  254. computeProperties(this, start, end, this._ellipsoid);
  255. };
  256. /**
  257. * Provides the location of a point at the indicated portion along the geodesic.
  258. *
  259. * @param {Number} fraction The portion of the distance between the initial and final points.
  260. * @param {Cartographic} result The object in which to store the result.
  261. * @returns {Cartographic} The location of the point along the geodesic.
  262. */
  263. EllipsoidGeodesic.prototype.interpolateUsingFraction = function(fraction, result) {
  264. return this.interpolateUsingSurfaceDistance(this._distance * fraction, result);
  265. };
  266. /**
  267. * Provides the location of a point at the indicated distance along the geodesic.
  268. *
  269. * @param {Number} distance The distance from the inital point to the point of interest along the geodesic
  270. * @param {Cartographic} result The object in which to store the result.
  271. * @returns {Cartographic} The location of the point along the geodesic.
  272. *
  273. * @exception {DeveloperError} start and end must be set before calling function interpolateUsingSurfaceDistance
  274. */
  275. EllipsoidGeodesic.prototype.interpolateUsingSurfaceDistance = function(distance, result) {
  276. //>>includeStart('debug', pragmas.debug);
  277. Check.defined('distance', this._distance);
  278. //>>includeEnd('debug');
  279. var constants = this._constants;
  280. var s = constants.distanceRatio + distance / constants.b;
  281. var cosine2S = Math.cos(2.0 * s);
  282. var cosine4S = Math.cos(4.0 * s);
  283. var cosine6S = Math.cos(6.0 * s);
  284. var sine2S = Math.sin(2.0 * s);
  285. var sine4S = Math.sin(4.0 * s);
  286. var sine6S = Math.sin(6.0 * s);
  287. var sine8S = Math.sin(8.0 * s);
  288. var s2 = s * s;
  289. var s3 = s * s2;
  290. var u8Over256 = constants.u8Over256;
  291. var u2Over4 = constants.u2Over4;
  292. var u6Over64 = constants.u6Over64;
  293. var u4Over16 = constants.u4Over16;
  294. var sigma = 2.0 * s3 * u8Over256 * cosine2S / 3.0 +
  295. s * (1.0 - u2Over4 + 7.0 * u4Over16 / 4.0 - 15.0 * u6Over64 / 4.0 + 579.0 * u8Over256 / 64.0 -
  296. (u4Over16 - 15.0 * u6Over64 / 4.0 + 187.0 * u8Over256 / 16.0) * cosine2S -
  297. (5.0 * u6Over64 / 4.0 - 115.0 * u8Over256 / 16.0) * cosine4S -
  298. 29.0 * u8Over256 * cosine6S / 16.0) +
  299. (u2Over4 / 2.0 - u4Over16 + 71.0 * u6Over64 / 32.0 - 85.0 * u8Over256 / 16.0) * sine2S +
  300. (5.0 * u4Over16 / 16.0 - 5.0 * u6Over64 / 4.0 + 383.0 * u8Over256 / 96.0) * sine4S -
  301. s2 * ((u6Over64 - 11.0 * u8Over256 / 2.0) * sine2S + 5.0 * u8Over256 * sine4S / 2.0) +
  302. (29.0 * u6Over64 / 96.0 - 29.0 * u8Over256 / 16.0) * sine6S +
  303. 539.0 * u8Over256 * sine8S / 1536.0;
  304. var theta = Math.asin(Math.sin(sigma) * constants.cosineAlpha);
  305. var latitude = Math.atan(constants.a / constants.b * Math.tan(theta));
  306. // Redefine in terms of relative argument of latitude.
  307. sigma = sigma - constants.sigma;
  308. var cosineTwiceSigmaMidpoint = Math.cos(2.0 * constants.sigma + sigma);
  309. var sineSigma = Math.sin(sigma);
  310. var cosineSigma = Math.cos(sigma);
  311. var cc = constants.cosineU * cosineSigma;
  312. var ss = constants.sineU * sineSigma;
  313. var lambda = Math.atan2(sineSigma * constants.sineHeading, cc - ss * constants.cosineHeading);
  314. var l = lambda - computeDeltaLambda(constants.f, constants.sineAlpha, constants.cosineSquaredAlpha,
  315. sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint);
  316. if (defined(result)) {
  317. result.longitude = this._start.longitude + l;
  318. result.latitude = latitude;
  319. result.height = 0.0;
  320. return result;
  321. }
  322. return new Cartographic(this._start.longitude + l, latitude, 0.0);
  323. };
  324. export default EllipsoidGeodesic;