BoundingRectangle.js 13 KB

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