TimeInterval.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import Check from "./Check.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defined from "./defined.js";
  4. import DeveloperError from "./DeveloperError.js";
  5. import JulianDate from "./JulianDate.js";
  6. /**
  7. * An interval defined by a start and a stop time; optionally including those times as part of the interval.
  8. * Arbitrary data can optionally be associated with each instance for used with {@link TimeIntervalCollection}.
  9. *
  10. * @alias TimeInterval
  11. * @constructor
  12. *
  13. * @param {Object} [options] Object with the following properties:
  14. * @param {JulianDate} [options.start=new JulianDate()] The start time of the interval.
  15. * @param {JulianDate} [options.stop=new JulianDate()] The stop time of the interval.
  16. * @param {Boolean} [options.isStartIncluded=true] <code>true</code> if <code>options.start</code> is included in the interval, <code>false</code> otherwise.
  17. * @param {Boolean} [options.isStopIncluded=true] <code>true</code> if <code>options.stop</code> is included in the interval, <code>false</code> otherwise.
  18. * @param {Object} [options.data] Arbitrary data associated with this interval.
  19. *
  20. * @example
  21. * // Create an instance that spans August 1st, 1980 and is associated
  22. * // with a Cartesian position.
  23. * var timeInterval = new Cesium.TimeInterval({
  24. * start : Cesium.JulianDate.fromIso8601('1980-08-01T00:00:00Z'),
  25. * stop : Cesium.JulianDate.fromIso8601('1980-08-02T00:00:00Z'),
  26. * isStartIncluded : true,
  27. * isStopIncluded : false,
  28. * data : Cesium.Cartesian3.fromDegrees(39.921037, -75.170082)
  29. * });
  30. *
  31. * @example
  32. * // Create two instances from ISO 8601 intervals with associated numeric data
  33. * // then compute their intersection, summing the data they contain.
  34. * var left = Cesium.TimeInterval.fromIso8601({
  35. * iso8601 : '2000/2010',
  36. * data : 2
  37. * });
  38. *
  39. * var right = Cesium.TimeInterval.fromIso8601({
  40. * iso8601 : '1995/2005',
  41. * data : 3
  42. * });
  43. *
  44. * //The result of the below intersection will be an interval equivalent to
  45. * //var intersection = Cesium.TimeInterval.fromIso8601({
  46. * // iso8601 : '2000/2005',
  47. * // data : 5
  48. * //});
  49. * var intersection = new Cesium.TimeInterval();
  50. * Cesium.TimeInterval.intersect(left, right, intersection, function(leftData, rightData) {
  51. * return leftData + rightData;
  52. * });
  53. *
  54. * @example
  55. * // Check if an interval contains a specific time.
  56. * var dateToCheck = Cesium.JulianDate.fromIso8601('1982-09-08T11:30:00Z');
  57. * var containsDate = Cesium.TimeInterval.contains(timeInterval, dateToCheck);
  58. */
  59. function TimeInterval(options) {
  60. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  61. /**
  62. * Gets or sets the start time of this interval.
  63. * @type {JulianDate}
  64. */
  65. this.start = defined(options.start)
  66. ? JulianDate.clone(options.start)
  67. : new JulianDate();
  68. /**
  69. * Gets or sets the stop time of this interval.
  70. * @type {JulianDate}
  71. */
  72. this.stop = defined(options.stop)
  73. ? JulianDate.clone(options.stop)
  74. : new JulianDate();
  75. /**
  76. * Gets or sets the data associated with this interval.
  77. * @type {*}
  78. */
  79. this.data = options.data;
  80. /**
  81. * Gets or sets whether or not the start time is included in this interval.
  82. * @type {Boolean}
  83. * @default true
  84. */
  85. this.isStartIncluded = defaultValue(options.isStartIncluded, true);
  86. /**
  87. * Gets or sets whether or not the stop time is included in this interval.
  88. * @type {Boolean}
  89. * @default true
  90. */
  91. this.isStopIncluded = defaultValue(options.isStopIncluded, true);
  92. }
  93. Object.defineProperties(TimeInterval.prototype, {
  94. /**
  95. * Gets whether or not this interval is empty.
  96. * @memberof TimeInterval.prototype
  97. * @type {Boolean}
  98. * @readonly
  99. */
  100. isEmpty: {
  101. get: function () {
  102. var stopComparedToStart = JulianDate.compare(this.stop, this.start);
  103. return (
  104. stopComparedToStart < 0 ||
  105. (stopComparedToStart === 0 &&
  106. (!this.isStartIncluded || !this.isStopIncluded))
  107. );
  108. },
  109. },
  110. });
  111. var scratchInterval = {
  112. start: undefined,
  113. stop: undefined,
  114. isStartIncluded: undefined,
  115. isStopIncluded: undefined,
  116. data: undefined,
  117. };
  118. /**
  119. * Creates a new instance from a {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} interval.
  120. *
  121. * @throws DeveloperError if options.iso8601 does not match proper formatting.
  122. *
  123. * @param {Object} options Object with the following properties:
  124. * @param {String} options.iso8601 An ISO 8601 interval.
  125. * @param {Boolean} [options.isStartIncluded=true] <code>true</code> if <code>options.start</code> is included in the interval, <code>false</code> otherwise.
  126. * @param {Boolean} [options.isStopIncluded=true] <code>true</code> if <code>options.stop</code> is included in the interval, <code>false</code> otherwise.
  127. * @param {Object} [options.data] Arbitrary data associated with this interval.
  128. * @param {TimeInterval} [result] An existing instance to use for the result.
  129. * @returns {TimeInterval} The modified result parameter or a new instance if none was provided.
  130. */
  131. TimeInterval.fromIso8601 = function (options, result) {
  132. //>>includeStart('debug', pragmas.debug);
  133. Check.typeOf.object("options", options);
  134. Check.typeOf.string("options.iso8601", options.iso8601);
  135. //>>includeEnd('debug');
  136. var dates = options.iso8601.split("/");
  137. if (dates.length !== 2) {
  138. throw new DeveloperError(
  139. "options.iso8601 is an invalid ISO 8601 interval."
  140. );
  141. }
  142. var start = JulianDate.fromIso8601(dates[0]);
  143. var stop = JulianDate.fromIso8601(dates[1]);
  144. var isStartIncluded = defaultValue(options.isStartIncluded, true);
  145. var isStopIncluded = defaultValue(options.isStopIncluded, true);
  146. var data = options.data;
  147. if (!defined(result)) {
  148. scratchInterval.start = start;
  149. scratchInterval.stop = stop;
  150. scratchInterval.isStartIncluded = isStartIncluded;
  151. scratchInterval.isStopIncluded = isStopIncluded;
  152. scratchInterval.data = data;
  153. return new TimeInterval(scratchInterval);
  154. }
  155. result.start = start;
  156. result.stop = stop;
  157. result.isStartIncluded = isStartIncluded;
  158. result.isStopIncluded = isStopIncluded;
  159. result.data = data;
  160. return result;
  161. };
  162. /**
  163. * Creates an ISO8601 representation of the provided interval.
  164. *
  165. * @param {TimeInterval} timeInterval The interval to be converted.
  166. * @param {Number} [precision] The number of fractional digits used to represent the seconds component. By default, the most precise representation is used.
  167. * @returns {String} The ISO8601 representation of the provided interval.
  168. */
  169. TimeInterval.toIso8601 = function (timeInterval, precision) {
  170. //>>includeStart('debug', pragmas.debug);
  171. Check.typeOf.object("timeInterval", timeInterval);
  172. //>>includeEnd('debug');
  173. return (
  174. JulianDate.toIso8601(timeInterval.start, precision) +
  175. "/" +
  176. JulianDate.toIso8601(timeInterval.stop, precision)
  177. );
  178. };
  179. /**
  180. * Duplicates the provided instance.
  181. *
  182. * @param {TimeInterval} [timeInterval] The instance to clone.
  183. * @param {TimeInterval} [result] An existing instance to use for the result.
  184. * @returns {TimeInterval} The modified result parameter or a new instance if none was provided.
  185. */
  186. TimeInterval.clone = function (timeInterval, result) {
  187. if (!defined(timeInterval)) {
  188. return undefined;
  189. }
  190. if (!defined(result)) {
  191. return new TimeInterval(timeInterval);
  192. }
  193. result.start = timeInterval.start;
  194. result.stop = timeInterval.stop;
  195. result.isStartIncluded = timeInterval.isStartIncluded;
  196. result.isStopIncluded = timeInterval.isStopIncluded;
  197. result.data = timeInterval.data;
  198. return result;
  199. };
  200. /**
  201. * Compares two instances and returns <code>true</code> if they are equal, <code>false</code> otherwise.
  202. *
  203. * @param {TimeInterval} [left] The first instance.
  204. * @param {TimeInterval} [right] The second instance.
  205. * @param {TimeInterval.DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  206. * @returns {Boolean} <code>true</code> if the dates are equal; otherwise, <code>false</code>.
  207. */
  208. TimeInterval.equals = function (left, right, dataComparer) {
  209. return (
  210. left === right ||
  211. (defined(left) &&
  212. defined(right) &&
  213. ((left.isEmpty && right.isEmpty) ||
  214. (left.isStartIncluded === right.isStartIncluded &&
  215. left.isStopIncluded === right.isStopIncluded &&
  216. JulianDate.equals(left.start, right.start) &&
  217. JulianDate.equals(left.stop, right.stop) &&
  218. (left.data === right.data ||
  219. (defined(dataComparer) && dataComparer(left.data, right.data))))))
  220. );
  221. };
  222. /**
  223. * Compares two instances and returns <code>true</code> if they are within <code>epsilon</code> seconds of
  224. * each other. That is, in order for the dates to be considered equal (and for
  225. * this function to return <code>true</code>), the absolute value of the difference between them, in
  226. * seconds, must be less than <code>epsilon</code>.
  227. *
  228. * @param {TimeInterval} [left] The first instance.
  229. * @param {TimeInterval} [right] The second instance.
  230. * @param {Number} [epsilon=0] The maximum number of seconds that should separate the two instances.
  231. * @param {TimeInterval.DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  232. * @returns {Boolean} <code>true</code> if the two dates are within <code>epsilon</code> seconds of each other; otherwise <code>false</code>.
  233. */
  234. TimeInterval.equalsEpsilon = function (left, right, epsilon, dataComparer) {
  235. epsilon = defaultValue(epsilon, 0);
  236. return (
  237. left === right ||
  238. (defined(left) &&
  239. defined(right) &&
  240. ((left.isEmpty && right.isEmpty) ||
  241. (left.isStartIncluded === right.isStartIncluded &&
  242. left.isStopIncluded === right.isStopIncluded &&
  243. JulianDate.equalsEpsilon(left.start, right.start, epsilon) &&
  244. JulianDate.equalsEpsilon(left.stop, right.stop, epsilon) &&
  245. (left.data === right.data ||
  246. (defined(dataComparer) && dataComparer(left.data, right.data))))))
  247. );
  248. };
  249. /**
  250. * Computes the intersection of two intervals, optionally merging their data.
  251. *
  252. * @param {TimeInterval} left The first interval.
  253. * @param {TimeInterval} [right] The second interval.
  254. * @param {TimeInterval} [result] An existing instance to use for the result.
  255. * @param {TimeInterval.MergeCallback} [mergeCallback] A function which merges the data of the two intervals. If omitted, the data from the left interval will be used.
  256. * @returns {TimeInterval} The modified result parameter.
  257. */
  258. TimeInterval.intersect = function (left, right, result, mergeCallback) {
  259. //>>includeStart('debug', pragmas.debug);
  260. Check.typeOf.object("left", left);
  261. //>>includeEnd('debug');
  262. if (!defined(right)) {
  263. return TimeInterval.clone(TimeInterval.EMPTY, result);
  264. }
  265. var leftStart = left.start;
  266. var leftStop = left.stop;
  267. var rightStart = right.start;
  268. var rightStop = right.stop;
  269. var intersectsStartRight =
  270. JulianDate.greaterThanOrEquals(rightStart, leftStart) &&
  271. JulianDate.greaterThanOrEquals(leftStop, rightStart);
  272. var intersectsStartLeft =
  273. !intersectsStartRight &&
  274. JulianDate.lessThanOrEquals(rightStart, leftStart) &&
  275. JulianDate.lessThanOrEquals(leftStart, rightStop);
  276. if (!intersectsStartRight && !intersectsStartLeft) {
  277. return TimeInterval.clone(TimeInterval.EMPTY, result);
  278. }
  279. var leftIsStartIncluded = left.isStartIncluded;
  280. var leftIsStopIncluded = left.isStopIncluded;
  281. var rightIsStartIncluded = right.isStartIncluded;
  282. var rightIsStopIncluded = right.isStopIncluded;
  283. var leftLessThanRight = JulianDate.lessThan(leftStop, rightStop);
  284. if (!defined(result)) {
  285. result = new TimeInterval();
  286. }
  287. result.start = intersectsStartRight ? rightStart : leftStart;
  288. result.isStartIncluded =
  289. (leftIsStartIncluded && rightIsStartIncluded) ||
  290. (!JulianDate.equals(rightStart, leftStart) &&
  291. ((intersectsStartRight && rightIsStartIncluded) ||
  292. (intersectsStartLeft && leftIsStartIncluded)));
  293. result.stop = leftLessThanRight ? leftStop : rightStop;
  294. result.isStopIncluded = leftLessThanRight
  295. ? leftIsStopIncluded
  296. : (leftIsStopIncluded && rightIsStopIncluded) ||
  297. (!JulianDate.equals(rightStop, leftStop) && rightIsStopIncluded);
  298. result.data = defined(mergeCallback)
  299. ? mergeCallback(left.data, right.data)
  300. : left.data;
  301. return result;
  302. };
  303. /**
  304. * Checks if the specified date is inside the provided interval.
  305. *
  306. * @param {TimeInterval} timeInterval The interval.
  307. * @param {JulianDate} julianDate The date to check.
  308. * @returns {Boolean} <code>true</code> if the interval contains the specified date, <code>false</code> otherwise.
  309. */
  310. TimeInterval.contains = function (timeInterval, julianDate) {
  311. //>>includeStart('debug', pragmas.debug);
  312. Check.typeOf.object("timeInterval", timeInterval);
  313. Check.typeOf.object("julianDate", julianDate);
  314. //>>includeEnd('debug');
  315. if (timeInterval.isEmpty) {
  316. return false;
  317. }
  318. var startComparedToDate = JulianDate.compare(timeInterval.start, julianDate);
  319. if (startComparedToDate === 0) {
  320. return timeInterval.isStartIncluded;
  321. }
  322. var dateComparedToStop = JulianDate.compare(julianDate, timeInterval.stop);
  323. if (dateComparedToStop === 0) {
  324. return timeInterval.isStopIncluded;
  325. }
  326. return startComparedToDate < 0 && dateComparedToStop < 0;
  327. };
  328. /**
  329. * Duplicates this instance.
  330. *
  331. * @param {TimeInterval} [result] An existing instance to use for the result.
  332. * @returns {TimeInterval} The modified result parameter or a new instance if none was provided.
  333. */
  334. TimeInterval.prototype.clone = function (result) {
  335. return TimeInterval.clone(this, result);
  336. };
  337. /**
  338. * Compares this instance against the provided instance componentwise and returns
  339. * <code>true</code> if they are equal, <code>false</code> otherwise.
  340. *
  341. * @param {TimeInterval} [right] The right hand side interval.
  342. * @param {TimeInterval.DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  343. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  344. */
  345. TimeInterval.prototype.equals = function (right, dataComparer) {
  346. return TimeInterval.equals(this, right, dataComparer);
  347. };
  348. /**
  349. * Compares this instance against the provided instance componentwise and returns
  350. * <code>true</code> if they are within the provided epsilon,
  351. * <code>false</code> otherwise.
  352. *
  353. * @param {TimeInterval} [right] The right hand side interval.
  354. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  355. * @param {TimeInterval.DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  356. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  357. */
  358. TimeInterval.prototype.equalsEpsilon = function (right, epsilon, dataComparer) {
  359. return TimeInterval.equalsEpsilon(this, right, epsilon, dataComparer);
  360. };
  361. /**
  362. * Creates a string representing this TimeInterval in ISO8601 format.
  363. *
  364. * @returns {String} A string representing this TimeInterval in ISO8601 format.
  365. */
  366. TimeInterval.prototype.toString = function () {
  367. return TimeInterval.toIso8601(this);
  368. };
  369. /**
  370. * An immutable empty interval.
  371. *
  372. * @type {TimeInterval}
  373. * @constant
  374. */
  375. TimeInterval.EMPTY = Object.freeze(
  376. new TimeInterval({
  377. start: new JulianDate(),
  378. stop: new JulianDate(),
  379. isStartIncluded: false,
  380. isStopIncluded: false,
  381. })
  382. );
  383. /**
  384. * Function interface for merging interval data.
  385. * @callback TimeInterval.MergeCallback
  386. *
  387. * @param {*} leftData The first data instance.
  388. * @param {*} rightData The second data instance.
  389. * @returns {*} The result of merging the two data instances.
  390. */
  391. /**
  392. * Function interface for comparing interval data.
  393. * @callback TimeInterval.DataComparer
  394. * @param {*} leftData The first data instance.
  395. * @param {*} rightData The second data instance.
  396. * @returns {Boolean} <code>true</code> if the provided instances are equal, <code>false</code> otherwise.
  397. */
  398. export default TimeInterval;