BoundingRectangle-dc808c42.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788'], function (exports, when, Check, Cartographic, Cartesian2, BoundingSphere) { 'use strict';
  24. /**
  25. * A bounding rectangle given by a corner, width and height.
  26. * @alias BoundingRectangle
  27. * @constructor
  28. *
  29. * @param {Number} [x=0.0] The x coordinate of the rectangle.
  30. * @param {Number} [y=0.0] The y coordinate of the rectangle.
  31. * @param {Number} [width=0.0] The width of the rectangle.
  32. * @param {Number} [height=0.0] The height of the rectangle.
  33. *
  34. * @see BoundingSphere
  35. * @see Packable
  36. */
  37. function BoundingRectangle(x, y, width, height) {
  38. /**
  39. * The x coordinate of the rectangle.
  40. * @type {Number}
  41. * @default 0.0
  42. */
  43. this.x = when.defaultValue(x, 0.0);
  44. /**
  45. * The y coordinate of the rectangle.
  46. * @type {Number}
  47. * @default 0.0
  48. */
  49. this.y = when.defaultValue(y, 0.0);
  50. /**
  51. * The width of the rectangle.
  52. * @type {Number}
  53. * @default 0.0
  54. */
  55. this.width = when.defaultValue(width, 0.0);
  56. /**
  57. * The height of the rectangle.
  58. * @type {Number}
  59. * @default 0.0
  60. */
  61. this.height = when.defaultValue(height, 0.0);
  62. }
  63. /**
  64. * The number of elements used to pack the object into an array.
  65. * @type {Number}
  66. */
  67. BoundingRectangle.packedLength = 4;
  68. /**
  69. * Stores the provided instance into the provided array.
  70. *
  71. * @param {BoundingRectangle} value The value to pack.
  72. * @param {Number[]} array The array to pack into.
  73. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  74. *
  75. * @returns {Number[]} The array that was packed into
  76. */
  77. BoundingRectangle.pack = function(value, array, startingIndex) {
  78. //>>includeStart('debug', pragmas.debug);
  79. Check.Check.typeOf.object('value', value);
  80. Check.Check.defined('array', array);
  81. //>>includeEnd('debug');
  82. startingIndex = when.defaultValue(startingIndex, 0);
  83. array[startingIndex++] = value.x;
  84. array[startingIndex++] = value.y;
  85. array[startingIndex++] = value.width;
  86. array[startingIndex] = value.height;
  87. return array;
  88. };
  89. /**
  90. * Retrieves an instance from a packed array.
  91. *
  92. * @param {Number[]} array The packed array.
  93. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  94. * @param {BoundingRectangle} [result] The object into 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.unpack = function(array, startingIndex, result) {
  98. //>>includeStart('debug', pragmas.debug);
  99. Check.Check.defined('array', array);
  100. //>>includeEnd('debug');
  101. startingIndex = when.defaultValue(startingIndex, 0);
  102. if (!when.defined(result)) {
  103. result = new BoundingRectangle();
  104. }
  105. result.x = array[startingIndex++];
  106. result.y = array[startingIndex++];
  107. result.width = array[startingIndex++];
  108. result.height = array[startingIndex];
  109. return result;
  110. };
  111. /**
  112. * Computes a bounding rectangle enclosing the list of 2D points.
  113. * The rectangle is oriented with the corner at the bottom left.
  114. *
  115. * @param {Cartesian2[]} positions List of points that the bounding rectangle will enclose. Each point must have <code>x</code> and <code>y</code> properties.
  116. * @param {BoundingRectangle} [result] The object onto which to store the result.
  117. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  118. */
  119. BoundingRectangle.fromPoints = function(positions, result) {
  120. if (!when.defined(result)) {
  121. result = new BoundingRectangle();
  122. }
  123. if (!when.defined(positions) || positions.length === 0) {
  124. result.x = 0;
  125. result.y = 0;
  126. result.width = 0;
  127. result.height = 0;
  128. return result;
  129. }
  130. var length = positions.length;
  131. var minimumX = positions[0].x;
  132. var minimumY = positions[0].y;
  133. var maximumX = positions[0].x;
  134. var maximumY = positions[0].y;
  135. for ( var i = 1; i < length; i++) {
  136. var p = positions[i];
  137. var x = p.x;
  138. var y = p.y;
  139. minimumX = Math.min(x, minimumX);
  140. maximumX = Math.max(x, maximumX);
  141. minimumY = Math.min(y, minimumY);
  142. maximumY = Math.max(y, maximumY);
  143. }
  144. result.x = minimumX;
  145. result.y = minimumY;
  146. result.width = maximumX - minimumX;
  147. result.height = maximumY - minimumY;
  148. return result;
  149. };
  150. var defaultProjection = new BoundingSphere.GeographicProjection();
  151. var fromRectangleLowerLeft = new Cartographic.Cartographic();
  152. var fromRectangleUpperRight = new Cartographic.Cartographic();
  153. /**
  154. * Computes a bounding rectangle from a rectangle.
  155. *
  156. * @param {Rectangle} rectangle The valid rectangle used to create a bounding rectangle.
  157. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  158. * @param {BoundingRectangle} [result] The object onto which to store the result.
  159. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  160. */
  161. BoundingRectangle.fromRectangle = function(rectangle, projection, result) {
  162. if (!when.defined(result)) {
  163. result = new BoundingRectangle();
  164. }
  165. if (!when.defined(rectangle)) {
  166. result.x = 0;
  167. result.y = 0;
  168. result.width = 0;
  169. result.height = 0;
  170. return result;
  171. }
  172. projection = when.defaultValue(projection, defaultProjection);
  173. var lowerLeft = projection.project(Cartesian2.Rectangle.southwest(rectangle, fromRectangleLowerLeft));
  174. var upperRight = projection.project(Cartesian2.Rectangle.northeast(rectangle, fromRectangleUpperRight));
  175. Cartesian2.Cartesian2.subtract(upperRight, lowerLeft, upperRight);
  176. result.x = lowerLeft.x;
  177. result.y = lowerLeft.y;
  178. result.width = upperRight.x;
  179. result.height = upperRight.y;
  180. return result;
  181. };
  182. /**
  183. * Duplicates a BoundingRectangle instance.
  184. *
  185. * @param {BoundingRectangle} rectangle The bounding rectangle to duplicate.
  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. (Returns undefined if rectangle is undefined)
  188. */
  189. BoundingRectangle.clone = function(rectangle, result) {
  190. if (!when.defined(rectangle)) {
  191. return undefined;
  192. }
  193. if (!when.defined(result)) {
  194. return new BoundingRectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  195. }
  196. result.x = rectangle.x;
  197. result.y = rectangle.y;
  198. result.width = rectangle.width;
  199. result.height = rectangle.height;
  200. return result;
  201. };
  202. /**
  203. * Computes a bounding rectangle that is the union of the left and right bounding rectangles.
  204. *
  205. * @param {BoundingRectangle} left A rectangle to enclose in bounding rectangle.
  206. * @param {BoundingRectangle} right A rectangle to enclose in a bounding rectangle.
  207. * @param {BoundingRectangle} [result] The object onto which to store the result.
  208. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  209. */
  210. BoundingRectangle.union = function(left, right, result) {
  211. //>>includeStart('debug', pragmas.debug);
  212. Check.Check.typeOf.object('left', left);
  213. Check.Check.typeOf.object('right', right);
  214. //>>includeEnd('debug');
  215. if (!when.defined(result)) {
  216. result = new BoundingRectangle();
  217. }
  218. var lowerLeftX = Math.min(left.x, right.x);
  219. var lowerLeftY = Math.min(left.y, right.y);
  220. var upperRightX = Math.max(left.x + left.width, right.x + right.width);
  221. var upperRightY = Math.max(left.y + left.height, right.y + right.height);
  222. result.x = lowerLeftX;
  223. result.y = lowerLeftY;
  224. result.width = upperRightX - lowerLeftX;
  225. result.height = upperRightY - lowerLeftY;
  226. return result;
  227. };
  228. /**
  229. * Computes a bounding rectangle by enlarging the provided rectangle until it contains the provided point.
  230. *
  231. * @param {BoundingRectangle} rectangle A rectangle to expand.
  232. * @param {Cartesian2} point A point to enclose in a bounding rectangle.
  233. * @param {BoundingRectangle} [result] The object onto which to store the result.
  234. * @returns {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
  235. */
  236. BoundingRectangle.expand = function(rectangle, point, result) {
  237. //>>includeStart('debug', pragmas.debug);
  238. Check.Check.typeOf.object('rectangle', rectangle);
  239. Check.Check.typeOf.object('point', point);
  240. //>>includeEnd('debug');
  241. result = BoundingRectangle.clone(rectangle, result);
  242. var width = point.x - result.x;
  243. var height = point.y - result.y;
  244. if (width > result.width) {
  245. result.width = width;
  246. } else if (width < 0) {
  247. result.width -= width;
  248. result.x = point.x;
  249. }
  250. if (height > result.height) {
  251. result.height = height;
  252. } else if (height < 0) {
  253. result.height -= height;
  254. result.y = point.y;
  255. }
  256. return result;
  257. };
  258. /**
  259. * Determines if two rectangles intersect.
  260. *
  261. * @param {BoundingRectangle} left A rectangle to check for intersection.
  262. * @param {BoundingRectangle} right The other rectangle to check for intersection.
  263. * @returns {Intersect} <code>Intersect.INTESECTING</code> if the rectangles intersect, <code>Intersect.OUTSIDE</code> otherwise.
  264. */
  265. BoundingRectangle.intersect = function(left, right) {
  266. //>>includeStart('debug', pragmas.debug);
  267. Check.Check.typeOf.object('left', left);
  268. Check.Check.typeOf.object('right', right);
  269. //>>includeEnd('debug');
  270. var leftX = left.x;
  271. var leftY = left.y;
  272. var rightX = right.x;
  273. var rightY = right.y;
  274. if (!(leftX > rightX + right.width ||
  275. leftX + left.width < rightX ||
  276. leftY + left.height < rightY ||
  277. leftY > rightY + right.height)) {
  278. return BoundingSphere.Intersect.INTERSECTING;
  279. }
  280. return BoundingSphere.Intersect.OUTSIDE;
  281. };
  282. /**
  283. * Compares the provided BoundingRectangles componentwise and returns
  284. * <code>true</code> if they are equal, <code>false</code> otherwise.
  285. *
  286. * @param {BoundingRectangle} [left] The first BoundingRectangle.
  287. * @param {BoundingRectangle} [right] The second BoundingRectangle.
  288. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  289. */
  290. BoundingRectangle.equals = function(left, right) {
  291. return (left === right) ||
  292. ((when.defined(left)) &&
  293. (when.defined(right)) &&
  294. (left.x === right.x) &&
  295. (left.y === right.y) &&
  296. (left.width === right.width) &&
  297. (left.height === right.height));
  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.INTESECTING</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. exports.BoundingRectangle = BoundingRectangle;
  328. });