BoundingRectangle-41078ce4.js 12 KB

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