BoundingRectangle-995f0079.js 12 KB

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