BoundingRectangle-4974cee9.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Matrix2-9e1c22e2', './RuntimeError-4f8ec8a2', './defaultValue-97284df2', './Transforms-273eeb44'], (function (exports, Matrix2, RuntimeError, defaultValue, Transforms) { 'use strict';
  26. /**
  27. * A bounding rectangle given by a corner, width and height.
  28. * @alias BoundingRectangle
  29. * @constructor
  30. *
  31. * @param {Number} [x=0.0] The x coordinate of the rectangle.
  32. * @param {Number} [y=0.0] The y coordinate of the rectangle.
  33. * @param {Number} [width=0.0] The width of the rectangle.
  34. * @param {Number} [height=0.0] The height of the rectangle.
  35. *
  36. * @see BoundingSphere
  37. * @see Packable
  38. */
  39. function BoundingRectangle(x, y, width, height) {
  40. /**
  41. * The x coordinate of the rectangle.
  42. * @type {Number}
  43. * @default 0.0
  44. */
  45. this.x = defaultValue.defaultValue(x, 0.0);
  46. /**
  47. * The y coordinate of the rectangle.
  48. * @type {Number}
  49. * @default 0.0
  50. */
  51. this.y = defaultValue.defaultValue(y, 0.0);
  52. /**
  53. * The width of the rectangle.
  54. * @type {Number}
  55. * @default 0.0
  56. */
  57. this.width = defaultValue.defaultValue(width, 0.0);
  58. /**
  59. * The height of the rectangle.
  60. * @type {Number}
  61. * @default 0.0
  62. */
  63. this.height = defaultValue.defaultValue(height, 0.0);
  64. }
  65. /**
  66. * The number of elements used to pack the object into an array.
  67. * @type {Number}
  68. */
  69. BoundingRectangle.packedLength = 4;
  70. /**
  71. * Stores the provided instance into the provided array.
  72. *
  73. * @param {BoundingRectangle} value The value to pack.
  74. * @param {Number[]} array The array to pack into.
  75. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  76. *
  77. * @returns {Number[]} The array that was packed into
  78. */
  79. BoundingRectangle.pack = function (value, array, startingIndex) {
  80. //>>includeStart('debug', pragmas.debug);
  81. RuntimeError.Check.typeOf.object("value", value);
  82. RuntimeError.Check.defined("array", array);
  83. //>>includeEnd('debug');
  84. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  85. array[startingIndex++] = value.x;
  86. array[startingIndex++] = value.y;
  87. array[startingIndex++] = value.width;
  88. array[startingIndex] = value.height;
  89. return array;
  90. };
  91. /**
  92. * Retrieves an instance from a packed array.
  93. *
  94. * @param {Number[]} array The packed array.
  95. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  96. * @param {BoundingRectangle} [result] The object into which to store the result.
  97. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  98. */
  99. BoundingRectangle.unpack = function (array, startingIndex, result) {
  100. //>>includeStart('debug', pragmas.debug);
  101. RuntimeError.Check.defined("array", array);
  102. //>>includeEnd('debug');
  103. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  104. if (!defaultValue.defined(result)) {
  105. result = new BoundingRectangle();
  106. }
  107. result.x = array[startingIndex++];
  108. result.y = array[startingIndex++];
  109. result.width = array[startingIndex++];
  110. result.height = array[startingIndex];
  111. return result;
  112. };
  113. /**
  114. * Computes a bounding rectangle enclosing the list of 2D points.
  115. * The rectangle is oriented with the corner at the bottom left.
  116. *
  117. * @param {Cartesian2[]} positions List of points that the bounding rectangle will enclose. Each point must have <code>x</code> and <code>y</code> properties.
  118. * @param {BoundingRectangle} [result] The object onto which to store the result.
  119. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  120. */
  121. BoundingRectangle.fromPoints = function (positions, result) {
  122. if (!defaultValue.defined(result)) {
  123. result = new BoundingRectangle();
  124. }
  125. if (!defaultValue.defined(positions) || positions.length === 0) {
  126. result.x = 0;
  127. result.y = 0;
  128. result.width = 0;
  129. result.height = 0;
  130. return result;
  131. }
  132. const length = positions.length;
  133. let minimumX = positions[0].x;
  134. let minimumY = positions[0].y;
  135. let maximumX = positions[0].x;
  136. let maximumY = positions[0].y;
  137. for (let i = 1; i < length; i++) {
  138. const p = positions[i];
  139. const x = p.x;
  140. const y = p.y;
  141. minimumX = Math.min(x, minimumX);
  142. maximumX = Math.max(x, maximumX);
  143. minimumY = Math.min(y, minimumY);
  144. maximumY = Math.max(y, maximumY);
  145. }
  146. result.x = minimumX;
  147. result.y = minimumY;
  148. result.width = maximumX - minimumX;
  149. result.height = maximumY - minimumY;
  150. return result;
  151. };
  152. const defaultProjection = new Transforms.GeographicProjection();
  153. const fromRectangleLowerLeft = new Matrix2.Cartographic();
  154. const fromRectangleUpperRight = new Matrix2.Cartographic();
  155. /**
  156. * Computes a bounding rectangle from a rectangle.
  157. *
  158. * @param {Rectangle} rectangle The valid rectangle used to create a bounding rectangle.
  159. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  160. * @param {BoundingRectangle} [result] The object onto which to store the result.
  161. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  162. */
  163. BoundingRectangle.fromRectangle = function (rectangle, projection, result) {
  164. if (!defaultValue.defined(result)) {
  165. result = new BoundingRectangle();
  166. }
  167. if (!defaultValue.defined(rectangle)) {
  168. result.x = 0;
  169. result.y = 0;
  170. result.width = 0;
  171. result.height = 0;
  172. return result;
  173. }
  174. projection = defaultValue.defaultValue(projection, defaultProjection);
  175. const lowerLeft = projection.project(
  176. Matrix2.Rectangle.southwest(rectangle, fromRectangleLowerLeft)
  177. );
  178. const upperRight = projection.project(
  179. Matrix2.Rectangle.northeast(rectangle, fromRectangleUpperRight)
  180. );
  181. Matrix2.Cartesian2.subtract(upperRight, lowerLeft, upperRight);
  182. result.x = lowerLeft.x;
  183. result.y = lowerLeft.y;
  184. result.width = upperRight.x;
  185. result.height = upperRight.y;
  186. return result;
  187. };
  188. /**
  189. * Duplicates a BoundingRectangle instance.
  190. *
  191. * @param {BoundingRectangle} rectangle The bounding rectangle to duplicate.
  192. * @param {BoundingRectangle} [result] The object onto which to store the result.
  193. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided. (Returns undefined if rectangle is undefined)
  194. */
  195. BoundingRectangle.clone = function (rectangle, result) {
  196. if (!defaultValue.defined(rectangle)) {
  197. return undefined;
  198. }
  199. if (!defaultValue.defined(result)) {
  200. return new BoundingRectangle(
  201. rectangle.x,
  202. rectangle.y,
  203. rectangle.width,
  204. rectangle.height
  205. );
  206. }
  207. result.x = rectangle.x;
  208. result.y = rectangle.y;
  209. result.width = rectangle.width;
  210. result.height = rectangle.height;
  211. return result;
  212. };
  213. /**
  214. * Computes a bounding rectangle that is the union of the left and right bounding rectangles.
  215. *
  216. * @param {BoundingRectangle} left A rectangle to enclose in bounding rectangle.
  217. * @param {BoundingRectangle} right A rectangle to enclose in a bounding rectangle.
  218. * @param {BoundingRectangle} [result] The object onto which to store the result.
  219. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  220. */
  221. BoundingRectangle.union = function (left, right, result) {
  222. //>>includeStart('debug', pragmas.debug);
  223. RuntimeError.Check.typeOf.object("left", left);
  224. RuntimeError.Check.typeOf.object("right", right);
  225. //>>includeEnd('debug');
  226. if (!defaultValue.defined(result)) {
  227. result = new BoundingRectangle();
  228. }
  229. const lowerLeftX = Math.min(left.x, right.x);
  230. const lowerLeftY = Math.min(left.y, right.y);
  231. const upperRightX = Math.max(left.x + left.width, right.x + right.width);
  232. const upperRightY = Math.max(left.y + left.height, right.y + right.height);
  233. result.x = lowerLeftX;
  234. result.y = lowerLeftY;
  235. result.width = upperRightX - lowerLeftX;
  236. result.height = upperRightY - lowerLeftY;
  237. return result;
  238. };
  239. /**
  240. * Computes a bounding rectangle by enlarging the provided rectangle until it contains the provided point.
  241. *
  242. * @param {BoundingRectangle} rectangle A rectangle to expand.
  243. * @param {Cartesian2} point A point to enclose in a bounding rectangle.
  244. * @param {BoundingRectangle} [result] The object onto which to store the result.
  245. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  246. */
  247. BoundingRectangle.expand = function (rectangle, point, result) {
  248. //>>includeStart('debug', pragmas.debug);
  249. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  250. RuntimeError.Check.typeOf.object("point", point);
  251. //>>includeEnd('debug');
  252. result = BoundingRectangle.clone(rectangle, result);
  253. const width = point.x - result.x;
  254. const height = point.y - result.y;
  255. if (width > result.width) {
  256. result.width = width;
  257. } else if (width < 0) {
  258. result.width -= width;
  259. result.x = point.x;
  260. }
  261. if (height > result.height) {
  262. result.height = height;
  263. } else if (height < 0) {
  264. result.height -= height;
  265. result.y = point.y;
  266. }
  267. return result;
  268. };
  269. /**
  270. * Determines if two rectangles intersect.
  271. *
  272. * @param {BoundingRectangle} left A rectangle to check for intersection.
  273. * @param {BoundingRectangle} right The other rectangle to check for intersection.
  274. * @returns {Intersect} <code>Intersect.INTERSECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  275. */
  276. BoundingRectangle.intersect = function (left, right) {
  277. //>>includeStart('debug', pragmas.debug);
  278. RuntimeError.Check.typeOf.object("left", left);
  279. RuntimeError.Check.typeOf.object("right", right);
  280. //>>includeEnd('debug');
  281. const leftX = left.x;
  282. const leftY = left.y;
  283. const rightX = right.x;
  284. const rightY = right.y;
  285. if (
  286. !(
  287. leftX > rightX + right.width ||
  288. leftX + left.width < rightX ||
  289. leftY + left.height < rightY ||
  290. leftY > rightY + right.height
  291. )
  292. ) {
  293. return Transforms.Intersect.INTERSECTING;
  294. }
  295. return Transforms.Intersect.OUTSIDE;
  296. };
  297. /**
  298. * Compares the provided BoundingRectangles componentwise and returns
  299. * <code>true</code> if they are equal, <code>false</code> otherwise.
  300. *
  301. * @param {BoundingRectangle} [left] The first BoundingRectangle.
  302. * @param {BoundingRectangle} [right] The second BoundingRectangle.
  303. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  304. */
  305. BoundingRectangle.equals = function (left, right) {
  306. return (
  307. left === right ||
  308. (defaultValue.defined(left) &&
  309. defaultValue.defined(right) &&
  310. left.x === right.x &&
  311. left.y === right.y &&
  312. left.width === right.width &&
  313. left.height === right.height)
  314. );
  315. };
  316. /**
  317. * Duplicates this BoundingRectangle instance.
  318. *
  319. * @param {BoundingRectangle} [result] The object onto which to store the result.
  320. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  321. */
  322. BoundingRectangle.prototype.clone = function (result) {
  323. return BoundingRectangle.clone(this, result);
  324. };
  325. /**
  326. * Determines if this rectangle intersects with another.
  327. *
  328. * @param {BoundingRectangle} right A rectangle to check for intersection.
  329. * @returns {Intersect} <code>Intersect.INTERSECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  330. */
  331. BoundingRectangle.prototype.intersect = function (right) {
  332. return BoundingRectangle.intersect(this, right);
  333. };
  334. /**
  335. * Compares this BoundingRectangle against the provided BoundingRectangle componentwise and returns
  336. * <code>true</code> if they are equal, <code>false</code> otherwise.
  337. *
  338. * @param {BoundingRectangle} [right] The right hand side BoundingRectangle.
  339. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  340. */
  341. BoundingRectangle.prototype.equals = function (right) {
  342. return BoundingRectangle.equals(this, right);
  343. };
  344. exports.BoundingRectangle = BoundingRectangle;
  345. }));
  346. //# sourceMappingURL=BoundingRectangle-4974cee9.js.map