LinearSpline.js 5.5 KB

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