LinearSpline.js 5.1 KB

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