Rotation.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * @exports Rotation
  12. *
  13. *
  14. * @example
  15. * var time1 = Cesium.JulianDate.fromIso8601('2010-05-07T00:00:00');
  16. * var time2 = Cesium.JulianDate.fromIso8601('2010-05-07T00:01:00');
  17. * var time3 = Cesium.JulianDate.fromIso8601('2010-05-07T00:02:00');
  18. *
  19. * var 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. var 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(packedArray, startingIndex, lastIndex, result) {
  85. //>>includeStart('debug', pragmas.debug);
  86. if (!defined(packedArray)) {
  87. throw new DeveloperError('packedArray is required');
  88. }
  89. //>>includeEnd('debug');
  90. startingIndex = defaultValue(startingIndex, 0);
  91. lastIndex = defaultValue(lastIndex, packedArray.length);
  92. var previousValue;
  93. for (var i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
  94. var value = packedArray[startingIndex + i];
  95. if (i === 0 || Math.abs(previousValue - value) < Math.PI) {
  96. result[i] = value;
  97. } else {
  98. result[i] = value - CesiumMath.TWO_PI;
  99. }
  100. previousValue = value;
  101. }
  102. },
  103. /**
  104. * Retrieves an instance from a packed array converted with {@link Rotation.convertPackedArrayForInterpolation}.
  105. *
  106. * @param {Number[]} array The array previously packed for interpolation.
  107. * @param {Number[]} sourceArray The original packed array.
  108. * @param {Number} [firstIndex=0] The firstIndex used to convert the array.
  109. * @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
  110. * @param {Rotation} [result] The object into which to store the result.
  111. * @returns {Rotation} The modified result parameter or a new Rotation instance if one was not provided.
  112. */
  113. unpackInterpolationResult : function(array, sourceArray, firstIndex, lastIndex, result) {
  114. //>>includeStart('debug', pragmas.debug);
  115. if (!defined(array)) {
  116. throw new DeveloperError('array is required');
  117. }
  118. if (!defined(sourceArray)) {
  119. throw new DeveloperError('sourceArray is required');
  120. }
  121. //>>includeEnd('debug');
  122. result = array[0];
  123. if (result < 0) {
  124. return result + CesiumMath.TWO_PI;
  125. }
  126. return result;
  127. }
  128. };
  129. export default Rotation;