Rotation.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import CesiumMath from "../Core/Math.js";
  5. /**
  6. * Represents a {@link Packable} number that always interpolates values
  7. * towards the shortest angle of rotation. This object is never used directly
  8. * but is instead passed to the constructor of {@link SampledProperty}
  9. * in order to represent a two-dimensional angle of rotation.
  10. *
  11. * @interface Rotation
  12. *
  13. *
  14. * @example
  15. * const time1 = Cesium.JulianDate.fromIso8601('2010-05-07T00:00:00');
  16. * const time2 = Cesium.JulianDate.fromIso8601('2010-05-07T00:01:00');
  17. * const time3 = Cesium.JulianDate.fromIso8601('2010-05-07T00:02:00');
  18. *
  19. * const property = new Cesium.SampledProperty(Cesium.Rotation);
  20. * property.addSample(time1, 0);
  21. * property.addSample(time3, Cesium.Math.toRadians(350));
  22. *
  23. * //Getting the value at time2 will equal 355 degrees instead
  24. * //of 175 degrees (which is what you get if you construct
  25. * //a SampledProperty(Number) instead. Note, the actual
  26. * //return value is in radians, not degrees.
  27. * property.getValue(time2);
  28. *
  29. * @see PackableForInterpolation
  30. */
  31. const Rotation = {
  32. /**
  33. * The number of elements used to pack the object into an array.
  34. * @type {number}
  35. */
  36. packedLength: 1,
  37. /**
  38. * Stores the provided instance into the provided array.
  39. *
  40. * @param {Rotation} value The value to pack.
  41. * @param {number[]} array The array to pack into.
  42. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  43. *
  44. * @returns {number[]} The array that was packed into
  45. */
  46. pack: function (value, array, startingIndex) {
  47. //>>includeStart('debug', pragmas.debug);
  48. if (!defined(value)) {
  49. throw new DeveloperError("value is required");
  50. }
  51. if (!defined(array)) {
  52. throw new DeveloperError("array is required");
  53. }
  54. //>>includeEnd('debug');
  55. startingIndex = defaultValue(startingIndex, 0);
  56. array[startingIndex] = value;
  57. return array;
  58. },
  59. /**
  60. * Retrieves an instance from a packed array.
  61. *
  62. * @param {number[]} array The packed array.
  63. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  64. * @param {Rotation} [result] The object into which to store the result.
  65. * @returns {Rotation} The modified result parameter or a new Rotation instance if one was not provided.
  66. */
  67. unpack: function (array, startingIndex, result) {
  68. //>>includeStart('debug', pragmas.debug);
  69. if (!defined(array)) {
  70. throw new DeveloperError("array is required");
  71. }
  72. //>>includeEnd('debug');
  73. startingIndex = defaultValue(startingIndex, 0);
  74. return array[startingIndex];
  75. },
  76. /**
  77. * Converts a packed array into a form suitable for interpolation.
  78. *
  79. * @param {number[]} packedArray The packed array.
  80. * @param {number} [startingIndex=0] The index of the first element to be converted.
  81. * @param {number} [lastIndex=packedArray.length] The index of the last element to be converted.
  82. * @param {number[]} [result] The object into which to store the result.
  83. */
  84. convertPackedArrayForInterpolation: function (
  85. packedArray,
  86. startingIndex,
  87. lastIndex,
  88. result
  89. ) {
  90. //>>includeStart('debug', pragmas.debug);
  91. if (!defined(packedArray)) {
  92. throw new DeveloperError("packedArray is required");
  93. }
  94. //>>includeEnd('debug');
  95. if (!defined(result)) {
  96. result = [];
  97. }
  98. startingIndex = defaultValue(startingIndex, 0);
  99. lastIndex = defaultValue(lastIndex, packedArray.length);
  100. let previousValue;
  101. for (let i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  102. const value = packedArray[startingIndex + i];
  103. if (i === 0 || Math.abs(previousValue - value) < Math.PI) {
  104. result[i] = value;
  105. } else {
  106. result[i] = value - CesiumMath.TWO_PI;
  107. }
  108. previousValue = value;
  109. }
  110. },
  111. /**
  112. * Retrieves an instance from a packed array converted with {@link Rotation.convertPackedArrayForInterpolation}.
  113. *
  114. * @param {number[]} array The array previously packed for interpolation.
  115. * @param {number[]} sourceArray The original packed array.
  116. * @param {number} [firstIndex=0] The firstIndex used to convert the array.
  117. * @param {number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  118. * @param {Rotation} [result] The object into which to store the result.
  119. * @returns {Rotation} The modified result parameter or a new Rotation instance if one was not provided.
  120. */
  121. unpackInterpolationResult: function (
  122. array,
  123. sourceArray,
  124. firstIndex,
  125. lastIndex,
  126. result
  127. ) {
  128. //>>includeStart('debug', pragmas.debug);
  129. if (!defined(array)) {
  130. throw new DeveloperError("array is required");
  131. }
  132. if (!defined(sourceArray)) {
  133. throw new DeveloperError("sourceArray is required");
  134. }
  135. //>>includeEnd('debug');
  136. result = array[0];
  137. if (result < 0) {
  138. return result + CesiumMath.TWO_PI;
  139. }
  140. return result;
  141. },
  142. };
  143. export default Rotation;