EllipsoidTangentPlane.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import AxisAlignedBoundingBox from "./AxisAlignedBoundingBox.js";
  2. import Cartesian2 from "./Cartesian2.js";
  3. import Cartesian3 from "./Cartesian3.js";
  4. import Cartesian4 from "./Cartesian4.js";
  5. import Check from "./Check.js";
  6. import defaultValue from "./defaultValue.js";
  7. import defined from "./defined.js";
  8. import DeveloperError from "./DeveloperError.js";
  9. import Ellipsoid from "./Ellipsoid.js";
  10. import IntersectionTests from "./IntersectionTests.js";
  11. import Matrix4 from "./Matrix4.js";
  12. import Plane from "./Plane.js";
  13. import Ray from "./Ray.js";
  14. import Transforms from "./Transforms.js";
  15. var scratchCart4 = new Cartesian4();
  16. /**
  17. * A plane tangent to the provided ellipsoid at the provided origin.
  18. * If origin is not on the surface of the ellipsoid, it's surface projection will be used.
  19. * If origin is at the center of the ellipsoid, an exception will be thrown.
  20. * @alias EllipsoidTangentPlane
  21. * @constructor
  22. *
  23. * @param {Cartesian3} origin The point on the surface of the ellipsoid where the tangent plane touches.
  24. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  25. *
  26. * @exception {DeveloperError} origin must not be at the center of the ellipsoid.
  27. */
  28. function EllipsoidTangentPlane(origin, ellipsoid) {
  29. //>>includeStart('debug', pragmas.debug);
  30. Check.defined("origin", origin);
  31. //>>includeEnd('debug');
  32. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  33. origin = ellipsoid.scaleToGeodeticSurface(origin);
  34. //>>includeStart('debug', pragmas.debug);
  35. if (!defined(origin)) {
  36. throw new DeveloperError(
  37. "origin must not be at the center of the ellipsoid."
  38. );
  39. }
  40. //>>includeEnd('debug');
  41. var eastNorthUp = Transforms.eastNorthUpToFixedFrame(origin, ellipsoid);
  42. this._ellipsoid = ellipsoid;
  43. this._origin = origin;
  44. this._xAxis = Cartesian3.fromCartesian4(
  45. Matrix4.getColumn(eastNorthUp, 0, scratchCart4)
  46. );
  47. this._yAxis = Cartesian3.fromCartesian4(
  48. Matrix4.getColumn(eastNorthUp, 1, scratchCart4)
  49. );
  50. var normal = Cartesian3.fromCartesian4(
  51. Matrix4.getColumn(eastNorthUp, 2, scratchCart4)
  52. );
  53. this._plane = Plane.fromPointNormal(origin, normal);
  54. }
  55. Object.defineProperties(EllipsoidTangentPlane.prototype, {
  56. /**
  57. * Gets the ellipsoid.
  58. * @memberof EllipsoidTangentPlane.prototype
  59. * @type {Ellipsoid}
  60. */
  61. ellipsoid: {
  62. get: function () {
  63. return this._ellipsoid;
  64. },
  65. },
  66. /**
  67. * Gets the origin.
  68. * @memberof EllipsoidTangentPlane.prototype
  69. * @type {Cartesian3}
  70. */
  71. origin: {
  72. get: function () {
  73. return this._origin;
  74. },
  75. },
  76. /**
  77. * Gets the plane which is tangent to the ellipsoid.
  78. * @memberof EllipsoidTangentPlane.prototype
  79. * @readonly
  80. * @type {Plane}
  81. */
  82. plane: {
  83. get: function () {
  84. return this._plane;
  85. },
  86. },
  87. /**
  88. * Gets the local X-axis (east) of the tangent plane.
  89. * @memberof EllipsoidTangentPlane.prototype
  90. * @readonly
  91. * @type {Cartesian3}
  92. */
  93. xAxis: {
  94. get: function () {
  95. return this._xAxis;
  96. },
  97. },
  98. /**
  99. * Gets the local Y-axis (north) of the tangent plane.
  100. * @memberof EllipsoidTangentPlane.prototype
  101. * @readonly
  102. * @type {Cartesian3}
  103. */
  104. yAxis: {
  105. get: function () {
  106. return this._yAxis;
  107. },
  108. },
  109. /**
  110. * Gets the local Z-axis (up) of the tangent plane.
  111. * @memberof EllipsoidTangentPlane.prototype
  112. * @readonly
  113. * @type {Cartesian3}
  114. */
  115. zAxis: {
  116. get: function () {
  117. return this._plane.normal;
  118. },
  119. },
  120. });
  121. var tmp = new AxisAlignedBoundingBox();
  122. /**
  123. * Creates a new instance from the provided ellipsoid and the center
  124. * point of the provided Cartesians.
  125. *
  126. * @param {Cartesian3[]} cartesians The list of positions surrounding the center point.
  127. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  128. */
  129. EllipsoidTangentPlane.fromPoints = function (cartesians, ellipsoid) {
  130. //>>includeStart('debug', pragmas.debug);
  131. Check.defined("cartesians", cartesians);
  132. //>>includeEnd('debug');
  133. var box = AxisAlignedBoundingBox.fromPoints(cartesians, tmp);
  134. return new EllipsoidTangentPlane(box.center, ellipsoid);
  135. };
  136. var scratchProjectPointOntoPlaneRay = new Ray();
  137. var scratchProjectPointOntoPlaneCartesian3 = new Cartesian3();
  138. /**
  139. * Computes the projection of the provided 3D position onto the 2D plane, radially outward from the {@link EllipsoidTangentPlane.ellipsoid} coordinate system origin.
  140. *
  141. * @param {Cartesian3} cartesian The point to project.
  142. * @param {Cartesian2} [result] The object onto which to store the result.
  143. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided. Undefined if there is no intersection point
  144. */
  145. EllipsoidTangentPlane.prototype.projectPointOntoPlane = function (
  146. cartesian,
  147. result
  148. ) {
  149. //>>includeStart('debug', pragmas.debug);
  150. Check.defined("cartesian", cartesian);
  151. //>>includeEnd('debug');
  152. var ray = scratchProjectPointOntoPlaneRay;
  153. ray.origin = cartesian;
  154. Cartesian3.normalize(cartesian, ray.direction);
  155. var intersectionPoint = IntersectionTests.rayPlane(
  156. ray,
  157. this._plane,
  158. scratchProjectPointOntoPlaneCartesian3
  159. );
  160. if (!defined(intersectionPoint)) {
  161. Cartesian3.negate(ray.direction, ray.direction);
  162. intersectionPoint = IntersectionTests.rayPlane(
  163. ray,
  164. this._plane,
  165. scratchProjectPointOntoPlaneCartesian3
  166. );
  167. }
  168. if (defined(intersectionPoint)) {
  169. var v = Cartesian3.subtract(
  170. intersectionPoint,
  171. this._origin,
  172. intersectionPoint
  173. );
  174. var x = Cartesian3.dot(this._xAxis, v);
  175. var y = Cartesian3.dot(this._yAxis, v);
  176. if (!defined(result)) {
  177. return new Cartesian2(x, y);
  178. }
  179. result.x = x;
  180. result.y = y;
  181. return result;
  182. }
  183. return undefined;
  184. };
  185. /**
  186. * Computes the projection of the provided 3D positions onto the 2D plane (where possible), radially outward from the global origin.
  187. * The resulting array may be shorter than the input array - if a single projection is impossible it will not be included.
  188. *
  189. * @see EllipsoidTangentPlane.projectPointOntoPlane
  190. *
  191. * @param {Cartesian3[]} cartesians The array of points to project.
  192. * @param {Cartesian2[]} [result] The array of Cartesian2 instances onto which to store results.
  193. * @returns {Cartesian2[]} The modified result parameter or a new array of Cartesian2 instances if none was provided.
  194. */
  195. EllipsoidTangentPlane.prototype.projectPointsOntoPlane = function (
  196. cartesians,
  197. result
  198. ) {
  199. //>>includeStart('debug', pragmas.debug);
  200. Check.defined("cartesians", cartesians);
  201. //>>includeEnd('debug');
  202. if (!defined(result)) {
  203. result = [];
  204. }
  205. var count = 0;
  206. var length = cartesians.length;
  207. for (var i = 0; i < length; i++) {
  208. var p = this.projectPointOntoPlane(cartesians[i], result[count]);
  209. if (defined(p)) {
  210. result[count] = p;
  211. count++;
  212. }
  213. }
  214. result.length = count;
  215. return result;
  216. };
  217. /**
  218. * Computes the projection of the provided 3D position onto the 2D plane, along the plane normal.
  219. *
  220. * @param {Cartesian3} cartesian The point to project.
  221. * @param {Cartesian2} [result] The object onto which to store the result.
  222. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if none was provided.
  223. */
  224. EllipsoidTangentPlane.prototype.projectPointToNearestOnPlane = function (
  225. cartesian,
  226. result
  227. ) {
  228. //>>includeStart('debug', pragmas.debug);
  229. Check.defined("cartesian", cartesian);
  230. //>>includeEnd('debug');
  231. if (!defined(result)) {
  232. result = new Cartesian2();
  233. }
  234. var ray = scratchProjectPointOntoPlaneRay;
  235. ray.origin = cartesian;
  236. Cartesian3.clone(this._plane.normal, ray.direction);
  237. var intersectionPoint = IntersectionTests.rayPlane(
  238. ray,
  239. this._plane,
  240. scratchProjectPointOntoPlaneCartesian3
  241. );
  242. if (!defined(intersectionPoint)) {
  243. Cartesian3.negate(ray.direction, ray.direction);
  244. intersectionPoint = IntersectionTests.rayPlane(
  245. ray,
  246. this._plane,
  247. scratchProjectPointOntoPlaneCartesian3
  248. );
  249. }
  250. var v = Cartesian3.subtract(
  251. intersectionPoint,
  252. this._origin,
  253. intersectionPoint
  254. );
  255. var x = Cartesian3.dot(this._xAxis, v);
  256. var y = Cartesian3.dot(this._yAxis, v);
  257. result.x = x;
  258. result.y = y;
  259. return result;
  260. };
  261. /**
  262. * Computes the projection of the provided 3D positions onto the 2D plane, along the plane normal.
  263. *
  264. * @see EllipsoidTangentPlane.projectPointToNearestOnPlane
  265. *
  266. * @param {Cartesian3[]} cartesians The array of points to project.
  267. * @param {Cartesian2[]} [result] The array of Cartesian2 instances onto which to store results.
  268. * @returns {Cartesian2[]} The modified result parameter or a new array of Cartesian2 instances if none was provided. This will have the same length as <code>cartesians</code>.
  269. */
  270. EllipsoidTangentPlane.prototype.projectPointsToNearestOnPlane = function (
  271. cartesians,
  272. result
  273. ) {
  274. //>>includeStart('debug', pragmas.debug);
  275. Check.defined("cartesians", cartesians);
  276. //>>includeEnd('debug');
  277. if (!defined(result)) {
  278. result = [];
  279. }
  280. var length = cartesians.length;
  281. result.length = length;
  282. for (var i = 0; i < length; i++) {
  283. result[i] = this.projectPointToNearestOnPlane(cartesians[i], result[i]);
  284. }
  285. return result;
  286. };
  287. var projectPointsOntoEllipsoidScratch = new Cartesian3();
  288. /**
  289. * Computes the projection of the provided 2D position onto the 3D ellipsoid.
  290. *
  291. * @param {Cartesian2} cartesian The points to project.
  292. * @param {Cartesian3} [result] The Cartesian3 instance to store result.
  293. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  294. */
  295. EllipsoidTangentPlane.prototype.projectPointOntoEllipsoid = function (
  296. cartesian,
  297. result
  298. ) {
  299. //>>includeStart('debug', pragmas.debug);
  300. Check.defined("cartesian", cartesian);
  301. //>>includeEnd('debug');
  302. if (!defined(result)) {
  303. result = new Cartesian3();
  304. }
  305. var ellipsoid = this._ellipsoid;
  306. var origin = this._origin;
  307. var xAxis = this._xAxis;
  308. var yAxis = this._yAxis;
  309. var tmp = projectPointsOntoEllipsoidScratch;
  310. Cartesian3.multiplyByScalar(xAxis, cartesian.x, tmp);
  311. result = Cartesian3.add(origin, tmp, result);
  312. Cartesian3.multiplyByScalar(yAxis, cartesian.y, tmp);
  313. Cartesian3.add(result, tmp, result);
  314. ellipsoid.scaleToGeocentricSurface(result, result);
  315. return result;
  316. };
  317. /**
  318. * Computes the projection of the provided 2D positions onto the 3D ellipsoid.
  319. *
  320. * @param {Cartesian2[]} cartesians The array of points to project.
  321. * @param {Cartesian3[]} [result] The array of Cartesian3 instances onto which to store results.
  322. * @returns {Cartesian3[]} The modified result parameter or a new array of Cartesian3 instances if none was provided.
  323. */
  324. EllipsoidTangentPlane.prototype.projectPointsOntoEllipsoid = function (
  325. cartesians,
  326. result
  327. ) {
  328. //>>includeStart('debug', pragmas.debug);
  329. Check.defined("cartesians", cartesians);
  330. //>>includeEnd('debug');
  331. var length = cartesians.length;
  332. if (!defined(result)) {
  333. result = new Array(length);
  334. } else {
  335. result.length = length;
  336. }
  337. for (var i = 0; i < length; ++i) {
  338. result[i] = this.projectPointOntoEllipsoid(cartesians[i], result[i]);
  339. }
  340. return result;
  341. };
  342. export default EllipsoidTangentPlane;