BoundingRectangle.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. const length = positions.length;
  116. let minimumX = positions[0].x;
  117. let minimumY = positions[0].y;
  118. let maximumX = positions[0].x;
  119. let maximumY = positions[0].y;
  120. for (let i = 1; i < length; i++) {
  121. const p = positions[i];
  122. const x = p.x;
  123. const 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. const defaultProjection = new GeographicProjection();
  136. const fromRectangleLowerLeft = new Cartographic();
  137. const 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. const lowerLeft = projection.project(
  159. Rectangle.southwest(rectangle, fromRectangleLowerLeft)
  160. );
  161. const upperRight = projection.project(
  162. Rectangle.northeast(rectangle, fromRectangleUpperRight)
  163. );
  164. Cartesian2.subtract(upperRight, lowerLeft, upperRight);
  165. result.x = lowerLeft.x;
  166. result.y = lowerLeft.y;
  167. result.width = upperRight.x;
  168. result.height = upperRight.y;
  169. return result;
  170. };
  171. /**
  172. * Duplicates a BoundingRectangle instance.
  173. *
  174. * @param {BoundingRectangle} rectangle The bounding rectangle to duplicate.
  175. * @param {BoundingRectangle} [result] The object onto which to store the result.
  176. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided. (Returns undefined if rectangle is undefined)
  177. */
  178. BoundingRectangle.clone = function (rectangle, result) {
  179. if (!defined(rectangle)) {
  180. return undefined;
  181. }
  182. if (!defined(result)) {
  183. return new BoundingRectangle(
  184. rectangle.x,
  185. rectangle.y,
  186. rectangle.width,
  187. rectangle.height
  188. );
  189. }
  190. result.x = rectangle.x;
  191. result.y = rectangle.y;
  192. result.width = rectangle.width;
  193. result.height = rectangle.height;
  194. return result;
  195. };
  196. /**
  197. * Computes a bounding rectangle that is the union of the left and right bounding rectangles.
  198. *
  199. * @param {BoundingRectangle} left A rectangle to enclose in bounding rectangle.
  200. * @param {BoundingRectangle} right A rectangle to enclose in a bounding rectangle.
  201. * @param {BoundingRectangle} [result] The object onto which to store the result.
  202. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  203. */
  204. BoundingRectangle.union = function (left, right, result) {
  205. //>>includeStart('debug', pragmas.debug);
  206. Check.typeOf.object("left", left);
  207. Check.typeOf.object("right", right);
  208. //>>includeEnd('debug');
  209. if (!defined(result)) {
  210. result = new BoundingRectangle();
  211. }
  212. const lowerLeftX = Math.min(left.x, right.x);
  213. const lowerLeftY = Math.min(left.y, right.y);
  214. const upperRightX = Math.max(left.x + left.width, right.x + right.width);
  215. const upperRightY = Math.max(left.y + left.height, right.y + right.height);
  216. result.x = lowerLeftX;
  217. result.y = lowerLeftY;
  218. result.width = upperRightX - lowerLeftX;
  219. result.height = upperRightY - lowerLeftY;
  220. return result;
  221. };
  222. /**
  223. * Computes a bounding rectangle by enlarging the provided rectangle until it contains the provided point.
  224. *
  225. * @param {BoundingRectangle} rectangle A rectangle to expand.
  226. * @param {Cartesian2} point A point to enclose in a bounding rectangle.
  227. * @param {BoundingRectangle} [result] The object onto which to store the result.
  228. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  229. */
  230. BoundingRectangle.expand = function (rectangle, point, result) {
  231. //>>includeStart('debug', pragmas.debug);
  232. Check.typeOf.object("rectangle", rectangle);
  233. Check.typeOf.object("point", point);
  234. //>>includeEnd('debug');
  235. result = BoundingRectangle.clone(rectangle, result);
  236. const width = point.x - result.x;
  237. const height = point.y - result.y;
  238. if (width > result.width) {
  239. result.width = width;
  240. } else if (width < 0) {
  241. result.width -= width;
  242. result.x = point.x;
  243. }
  244. if (height > result.height) {
  245. result.height = height;
  246. } else if (height < 0) {
  247. result.height -= height;
  248. result.y = point.y;
  249. }
  250. return result;
  251. };
  252. /**
  253. * Determines if two rectangles intersect.
  254. *
  255. * @param {BoundingRectangle} left A rectangle to check for intersection.
  256. * @param {BoundingRectangle} right The other rectangle to check for intersection.
  257. * @returns {Intersect} <code>Intersect.INTERSECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  258. */
  259. BoundingRectangle.intersect = function (left, right) {
  260. //>>includeStart('debug', pragmas.debug);
  261. Check.typeOf.object("left", left);
  262. Check.typeOf.object("right", right);
  263. //>>includeEnd('debug');
  264. const leftX = left.x;
  265. const leftY = left.y;
  266. const rightX = right.x;
  267. const rightY = right.y;
  268. if (
  269. !(
  270. leftX > rightX + right.width ||
  271. leftX + left.width < rightX ||
  272. leftY + left.height < rightY ||
  273. leftY > rightY + right.height
  274. )
  275. ) {
  276. return Intersect.INTERSECTING;
  277. }
  278. return Intersect.OUTSIDE;
  279. };
  280. /**
  281. * Compares the provided BoundingRectangles componentwise and returns
  282. * <code>true</code> if they are equal, <code>false</code> otherwise.
  283. *
  284. * @param {BoundingRectangle} [left] The first BoundingRectangle.
  285. * @param {BoundingRectangle} [right] The second BoundingRectangle.
  286. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  287. */
  288. BoundingRectangle.equals = function (left, right) {
  289. return (
  290. left === right ||
  291. (defined(left) &&
  292. defined(right) &&
  293. left.x === right.x &&
  294. left.y === right.y &&
  295. left.width === right.width &&
  296. left.height === right.height)
  297. );
  298. };
  299. /**
  300. * Duplicates this BoundingRectangle instance.
  301. *
  302. * @param {BoundingRectangle} [result] The object onto which to store the result.
  303. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  304. */
  305. BoundingRectangle.prototype.clone = function (result) {
  306. return BoundingRectangle.clone(this, result);
  307. };
  308. /**
  309. * Determines if this rectangle intersects with another.
  310. *
  311. * @param {BoundingRectangle} right A rectangle to check for intersection.
  312. * @returns {Intersect} <code>Intersect.INTERSECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  313. */
  314. BoundingRectangle.prototype.intersect = function (right) {
  315. return BoundingRectangle.intersect(this, right);
  316. };
  317. /**
  318. * Compares this BoundingRectangle against the provided BoundingRectangle componentwise and returns
  319. * <code>true</code> if they are equal, <code>false</code> otherwise.
  320. *
  321. * @param {BoundingRectangle} [right] The right hand side BoundingRectangle.
  322. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  323. */
  324. BoundingRectangle.prototype.equals = function (right) {
  325. return BoundingRectangle.equals(this, right);
  326. };
  327. export default BoundingRectangle;