BoundingRectangle-36e6acca.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './defined-26bd4a03', './Check-da037458', './defaultValue-f2e68450', './Cartesian2-2a723276', './Transforms-65aba0a4'], function (exports, defined, Check, defaultValue, Cartesian2, 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. Check.Check.typeOf.object('value', value);
  59. Check.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. Check.Check.defined('array', array);
  79. //>>includeEnd('debug');
  80. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  81. if (!defined.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 (!defined.defined(result)) {
  100. result = new BoundingRectangle();
  101. }
  102. if (!defined.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. var length = positions.length;
  110. var minimumX = positions[0].x;
  111. var minimumY = positions[0].y;
  112. var maximumX = positions[0].x;
  113. var maximumY = positions[0].y;
  114. for ( var i = 1; i < length; i++) {
  115. var p = positions[i];
  116. var x = p.x;
  117. var 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. var defaultProjection = new Transforms.GeographicProjection();
  130. var fromRectangleLowerLeft = new Cartesian2.Cartographic();
  131. var fromRectangleUpperRight = new Cartesian2.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 (!defined.defined(result)) {
  142. result = new BoundingRectangle();
  143. }
  144. if (!defined.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. var lowerLeft = projection.project(Cartesian2.Rectangle.southwest(rectangle, fromRectangleLowerLeft));
  153. var upperRight = projection.project(Cartesian2.Rectangle.northeast(rectangle, fromRectangleUpperRight));
  154. Cartesian2.Cartesian2.subtract(upperRight, lowerLeft, upperRight);
  155. result.x = lowerLeft.x;
  156. result.y = lowerLeft.y;
  157. result.width = upperRight.x;
  158. result.height = upperRight.y;
  159. return result;
  160. };
  161. /**
  162. * Duplicates a BoundingRectangle instance.
  163. *
  164. * @param {BoundingRectangle} rectangle The bounding rectangle to duplicate.
  165. * @param {BoundingRectangle} [result] The object onto which to store the result.
  166. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided. (Returns undefined if rectangle is undefined)
  167. */
  168. BoundingRectangle.clone = function(rectangle, result) {
  169. if (!defined.defined(rectangle)) {
  170. return undefined;
  171. }
  172. if (!defined.defined(result)) {
  173. return new BoundingRectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  174. }
  175. result.x = rectangle.x;
  176. result.y = rectangle.y;
  177. result.width = rectangle.width;
  178. result.height = rectangle.height;
  179. return result;
  180. };
  181. /**
  182. * Computes a bounding rectangle that is the union of the left and right bounding rectangles.
  183. *
  184. * @param {BoundingRectangle} left A rectangle to enclose in bounding rectangle.
  185. * @param {BoundingRectangle} right A rectangle to enclose in a bounding rectangle.
  186. * @param {BoundingRectangle} [result] The object onto which to store the result.
  187. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  188. */
  189. BoundingRectangle.union = function(left, right, result) {
  190. //>>includeStart('debug', pragmas.debug);
  191. Check.Check.typeOf.object('left', left);
  192. Check.Check.typeOf.object('right', right);
  193. //>>includeEnd('debug');
  194. if (!defined.defined(result)) {
  195. result = new BoundingRectangle();
  196. }
  197. var lowerLeftX = Math.min(left.x, right.x);
  198. var lowerLeftY = Math.min(left.y, right.y);
  199. var upperRightX = Math.max(left.x + left.width, right.x + right.width);
  200. var upperRightY = Math.max(left.y + left.height, right.y + right.height);
  201. result.x = lowerLeftX;
  202. result.y = lowerLeftY;
  203. result.width = upperRightX - lowerLeftX;
  204. result.height = upperRightY - lowerLeftY;
  205. return result;
  206. };
  207. /**
  208. * Computes a bounding rectangle by enlarging the provided rectangle until it contains the provided point.
  209. *
  210. * @param {BoundingRectangle} rectangle A rectangle to expand.
  211. * @param {Cartesian2} point A point to enclose in a bounding rectangle.
  212. * @param {BoundingRectangle} [result] The object onto which to store the result.
  213. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  214. */
  215. BoundingRectangle.expand = function(rectangle, point, result) {
  216. //>>includeStart('debug', pragmas.debug);
  217. Check.Check.typeOf.object('rectangle', rectangle);
  218. Check.Check.typeOf.object('point', point);
  219. //>>includeEnd('debug');
  220. result = BoundingRectangle.clone(rectangle, result);
  221. var width = point.x - result.x;
  222. var height = point.y - result.y;
  223. if (width > result.width) {
  224. result.width = width;
  225. } else if (width < 0) {
  226. result.width -= width;
  227. result.x = point.x;
  228. }
  229. if (height > result.height) {
  230. result.height = height;
  231. } else if (height < 0) {
  232. result.height -= height;
  233. result.y = point.y;
  234. }
  235. return result;
  236. };
  237. /**
  238. * Determines if two rectangles intersect.
  239. *
  240. * @param {BoundingRectangle} left A rectangle to check for intersection.
  241. * @param {BoundingRectangle} right The other rectangle to check for intersection.
  242. * @returns {Intersect} <code>Intersect.INTESECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  243. */
  244. BoundingRectangle.intersect = function(left, right) {
  245. //>>includeStart('debug', pragmas.debug);
  246. Check.Check.typeOf.object('left', left);
  247. Check.Check.typeOf.object('right', right);
  248. //>>includeEnd('debug');
  249. var leftX = left.x;
  250. var leftY = left.y;
  251. var rightX = right.x;
  252. var rightY = right.y;
  253. if (!(leftX > rightX + right.width ||
  254. leftX + left.width < rightX ||
  255. leftY + left.height < rightY ||
  256. leftY > rightY + right.height)) {
  257. return Transforms.Intersect.INTERSECTING;
  258. }
  259. return Transforms.Intersect.OUTSIDE;
  260. };
  261. /**
  262. * Compares the provided BoundingRectangles componentwise and returns
  263. * <code>true</code> if they are equal, <code>false</code> otherwise.
  264. *
  265. * @param {BoundingRectangle} [left] The first BoundingRectangle.
  266. * @param {BoundingRectangle} [right] The second BoundingRectangle.
  267. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  268. */
  269. BoundingRectangle.equals = function(left, right) {
  270. return (left === right) ||
  271. ((defined.defined(left)) &&
  272. (defined.defined(right)) &&
  273. (left.x === right.x) &&
  274. (left.y === right.y) &&
  275. (left.width === right.width) &&
  276. (left.height === right.height));
  277. };
  278. /**
  279. * Duplicates this BoundingRectangle instance.
  280. *
  281. * @param {BoundingRectangle} [result] The object onto which to store the result.
  282. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  283. */
  284. BoundingRectangle.prototype.clone = function(result) {
  285. return BoundingRectangle.clone(this, result);
  286. };
  287. /**
  288. * Determines if this rectangle intersects with another.
  289. *
  290. * @param {BoundingRectangle} right A rectangle to check for intersection.
  291. * @returns {Intersect} <code>Intersect.INTESECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  292. */
  293. BoundingRectangle.prototype.intersect = function(right) {
  294. return BoundingRectangle.intersect(this, right);
  295. };
  296. /**
  297. * Compares this BoundingRectangle against the provided BoundingRectangle componentwise and returns
  298. * <code>true</code> if they are equal, <code>false</code> otherwise.
  299. *
  300. * @param {BoundingRectangle} [right] The right hand side BoundingRectangle.
  301. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  302. */
  303. BoundingRectangle.prototype.equals = function(right) {
  304. return BoundingRectangle.equals(this, right);
  305. };
  306. exports.BoundingRectangle = BoundingRectangle;
  307. });