LinearSpline.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 {Cartesian3[]} options.points The array of {@link Cartesian3} 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. * var times = [ 0.0, 1.5, 3.0, 4.5, 6.0 ];
  23. * var 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. * var p0 = spline.evaluate(times[0]);
  35. *
  36. * @see HermiteSpline
  37. * @see CatmullRomSpline
  38. * @see QuaternionSpline
  39. * @see WeightSpline
  40. */
  41. function LinearSpline(options) {
  42. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  43. var points = options.points;
  44. var times = options.times;
  45. //>>includeStart('debug', pragmas.debug);
  46. if (!defined(points) || !defined(times)) {
  47. throw new DeveloperError("points and times are required.");
  48. }
  49. if (points.length < 2) {
  50. throw new DeveloperError(
  51. "points.length must be greater than or equal to 2."
  52. );
  53. }
  54. if (times.length !== points.length) {
  55. throw new DeveloperError("times.length must be equal to points.length.");
  56. }
  57. //>>includeEnd('debug');
  58. this._times = times;
  59. this._points = points;
  60. this._lastTimeIndex = 0;
  61. }
  62. Object.defineProperties(LinearSpline.prototype, {
  63. /**
  64. * An array of times for the control points.
  65. *
  66. * @memberof LinearSpline.prototype
  67. *
  68. * @type {Number[]}
  69. * @readonly
  70. */
  71. times: {
  72. get: function () {
  73. return this._times;
  74. },
  75. },
  76. /**
  77. * An array of {@link Cartesian3} control points.
  78. *
  79. * @memberof LinearSpline.prototype
  80. *
  81. * @type {Cartesian3[]}
  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. * @returns {Number} The index for the element at the start of the interval.
  97. *
  98. * @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>
  99. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  100. * in the array <code>times</code>.
  101. */
  102. LinearSpline.prototype.findTimeInterval = Spline.prototype.findTimeInterval;
  103. /**
  104. * Wraps the given time to the period covered by the spline.
  105. * @function
  106. *
  107. * @param {Number} time The time.
  108. * @return {Number} The time, wrapped around to the updated animation.
  109. */
  110. LinearSpline.prototype.wrapTime = Spline.prototype.wrapTime;
  111. /**
  112. * Clamps the given time to the period covered by the spline.
  113. * @function
  114. *
  115. * @param {Number} time The time.
  116. * @return {Number} The time, clamped to the animation period.
  117. */
  118. LinearSpline.prototype.clampTime = Spline.prototype.clampTime;
  119. /**
  120. * Evaluates the curve at a given time.
  121. *
  122. * @param {Number} time The time at which to evaluate the curve.
  123. * @param {Cartesian3} [result] The object onto which to store the result.
  124. * @returns {Cartesian3} The modified result parameter or a new instance of the point on the curve at the given time.
  125. *
  126. * @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>
  127. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  128. * in the array <code>times</code>.
  129. */
  130. LinearSpline.prototype.evaluate = function (time, result) {
  131. var points = this.points;
  132. var times = this.times;
  133. var i = (this._lastTimeIndex = this.findTimeInterval(
  134. time,
  135. this._lastTimeIndex
  136. ));
  137. var u = (time - times[i]) / (times[i + 1] - times[i]);
  138. if (!defined(result)) {
  139. result = new Cartesian3();
  140. }
  141. return Cartesian3.lerp(points[i], points[i + 1], u, result);
  142. };
  143. export default LinearSpline;