SteppedSpline.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import defaultValue from "./defaultValue.js";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. import Spline from "./Spline.js";
  5. /**
  6. * A spline that is composed of piecewise constants representing a step function.
  7. *
  8. * @alias SteppedSpline
  9. * @constructor
  10. *
  11. * @param {object} options Object with the following properties:
  12. * @param {number[]} options.times An array of strictly increasing, unit-less, floating-point times at each point. The values are in no way connected to the clock time. They are the parameterization for the curve.
  13. * @param {number[]|Cartesian3[]|Quaternion[]} options.points The array of control points.
  14. *
  15. * @exception {DeveloperError} points.length must be greater than or equal to 2.
  16. * @exception {DeveloperError} times.length must be equal to points.length.
  17. *
  18. * @example
  19. * const times = [ 0.0, 1.5, 3.0, 4.5, 6.0 ];
  20. * const spline = new Cesium.SteppedSpline({
  21. * times : times,
  22. * points : [
  23. * new Cesium.Cartesian3(1235398.0, -4810983.0, 4146266.0),
  24. * new Cesium.Cartesian3(1372574.0, -5345182.0, 4606657.0),
  25. * new Cesium.Cartesian3(-757983.0, -5542796.0, 4514323.0),
  26. * new Cesium.Cartesian3(-2821260.0, -5248423.0, 4021290.0),
  27. * new Cesium.Cartesian3(-2539788.0, -4724797.0, 3620093.0)
  28. * ]
  29. * });
  30. *
  31. * const p0 = spline.evaluate(times[0]);
  32. *
  33. * @see ConstantSpline
  34. * @see CatmullRomSpline
  35. * @see HermiteSpline
  36. * @see LinearSpline
  37. * @see QuaternionSpline
  38. * @see MorphWeightSpline
  39. */
  40. function SteppedSpline(options) {
  41. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  42. const points = options.points;
  43. const times = options.times;
  44. //>>includeStart('debug', pragmas.debug);
  45. if (!defined(points) || !defined(times)) {
  46. throw new DeveloperError("points and times are required.");
  47. }
  48. if (points.length < 2) {
  49. throw new DeveloperError(
  50. "points.length must be greater than or equal to 2."
  51. );
  52. }
  53. if (times.length !== points.length) {
  54. throw new DeveloperError("times.length must be equal to points.length.");
  55. }
  56. //>>includeEnd('debug');
  57. this._times = times;
  58. this._points = points;
  59. this._pointType = Spline.getPointType(points[0]);
  60. this._lastTimeIndex = 0;
  61. }
  62. Object.defineProperties(SteppedSpline.prototype, {
  63. /**
  64. * An array of times for the control points.
  65. *
  66. * @memberof SteppedSpline.prototype
  67. *
  68. * @type {number[]}
  69. * @readonly
  70. */
  71. times: {
  72. get: function () {
  73. return this._times;
  74. },
  75. },
  76. /**
  77. * An array of control points.
  78. *
  79. * @memberof SteppedSpline.prototype
  80. *
  81. * @type {number[]|Cartesian3[]|Quaternion[]}
  82. * @readonly
  83. */
  84. points: {
  85. get: function () {
  86. return this._points;
  87. },
  88. },
  89. });
  90. /**
  91. * Finds an index <code>i</code> in <code>times</code> such that the parameter
  92. * <code>time</code> is in the interval <code>[times[i], times[i + 1]]</code>.
  93. * @function
  94. *
  95. * @param {number} time The time.
  96. * @param {number} startIndex The index from which to start the search.
  97. * @returns {number} The index for the element at the start of the interval.
  98. *
  99. * @exception {DeveloperError} time must be in the range <code>[t<sub>0</sub>, t<sub>n</sub>]</code>, where <code>t<sub>0</sub></code>
  100. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  101. * in the array <code>times</code>.
  102. */
  103. SteppedSpline.prototype.findTimeInterval = Spline.prototype.findTimeInterval;
  104. /**
  105. * Wraps the given time to the period covered by the spline.
  106. * @function
  107. *
  108. * @param {number} time The time.
  109. * @return {number} The time, wrapped around to the updated animation.
  110. */
  111. SteppedSpline.prototype.wrapTime = Spline.prototype.wrapTime;
  112. /**
  113. * Clamps the given time to the period covered by the spline.
  114. * @function
  115. *
  116. * @param {number} time The time.
  117. * @return {number} The time, clamped to the animation period.
  118. */
  119. SteppedSpline.prototype.clampTime = Spline.prototype.clampTime;
  120. /**
  121. * Evaluates the curve at a given time.
  122. *
  123. * @param {number} time The time at which to evaluate the curve.
  124. * @param {Cartesian3|Quaternion} [result] The object onto which to store the result.
  125. * @returns {number|Cartesian3|Quaternion} The modified result parameter or a new instance of the point on the curve at the given time.
  126. *
  127. * @exception {DeveloperError} time must be in the range <code>[t<sub>0</sub>, t<sub>n</sub>]</code>, where <code>t<sub>0</sub></code>
  128. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  129. * in the array <code>times</code>.
  130. */
  131. SteppedSpline.prototype.evaluate = function (time, result) {
  132. const points = this.points;
  133. this._lastTimeIndex = this.findTimeInterval(time, this._lastTimeIndex);
  134. const i = this._lastTimeIndex;
  135. const PointType = this._pointType;
  136. if (PointType === Number) {
  137. return points[i];
  138. }
  139. if (!defined(result)) {
  140. result = new PointType();
  141. }
  142. return PointType.clone(points[i], result);
  143. };
  144. export default SteppedSpline;