arrayRemoveDuplicates-2869246d.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 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/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240'], function (exports, when, Check, _Math) { 'use strict';
  24. var removeDuplicatesEpsilon = _Math.CesiumMath.EPSILON10;
  25. /**
  26. * Removes adjacent duplicate values in an array of values.
  27. *
  28. * @param {Array.<*>} [values] The array of values.
  29. * @param {Function} equalsEpsilon Function to compare values with an epsilon. Boolean equalsEpsilon(left, right, epsilon).
  30. * @param {Boolean} [wrapAround=false] Compare the last value in the array against the first value.
  31. * @returns {Array.<*>|undefined} A new array of values with no adjacent duplicate values or the input array if no duplicates were found.
  32. *
  33. * @example
  34. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0), (1.0, 1.0, 1.0)]
  35. * var values = [
  36. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  37. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  38. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  39. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  40. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  41. * var nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon);
  42. *
  43. * @example
  44. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
  45. * var values = [
  46. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  47. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  48. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  49. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  50. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  51. * var nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon, true);
  52. *
  53. * @private
  54. */
  55. function arrayRemoveDuplicates(values, equalsEpsilon, wrapAround, epsilon) {
  56. //>>includeStart('debug', pragmas.debug);
  57. Check.Check.defined('equalsEpsilon', equalsEpsilon);
  58. //>>includeEnd('debug');
  59. if (!when.defined(values)) {
  60. return undefined;
  61. }
  62. epsilon = when.defaultValue(epsilon, removeDuplicatesEpsilon);
  63. wrapAround = when.defaultValue(wrapAround, false);
  64. var length = values.length;
  65. if (length < 2) {
  66. return values;
  67. }
  68. var i;
  69. var v0;
  70. var v1;
  71. for (i = 1; i < length; ++i) {
  72. v0 = values[i - 1];
  73. v1 = values[i];
  74. if (equalsEpsilon(v0, v1, epsilon)) {
  75. break;
  76. }
  77. }
  78. if (i === length) {
  79. if (wrapAround && equalsEpsilon(values[0], values[values.length - 1], epsilon)) {
  80. return values.slice(1);
  81. }
  82. return values;
  83. }
  84. var cleanedvalues = values.slice(0, i);
  85. for (; i < length; ++i) {
  86. // v0 is set by either the previous loop, or the previous clean point.
  87. v1 = values[i];
  88. if (!equalsEpsilon(v0, v1, epsilon)) {
  89. cleanedvalues.push(v1);
  90. v0 = v1;
  91. }
  92. }
  93. if (wrapAround && cleanedvalues.length > 1 && equalsEpsilon(cleanedvalues[0], cleanedvalues[cleanedvalues.length - 1], epsilon)) {
  94. cleanedvalues.shift();
  95. }
  96. return cleanedvalues;
  97. }
  98. exports.arrayRemoveDuplicates = arrayRemoveDuplicates;
  99. });