QuaternionSpline.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import defaultValue from './defaultValue.js';
  2. import defined from './defined.js';
  3. import defineProperties from './defineProperties.js';
  4. import DeveloperError from './DeveloperError.js';
  5. import Quaternion from './Quaternion.js';
  6. import Spline from './Spline.js';
  7. function createEvaluateFunction(spline) {
  8. var points = spline.points;
  9. var times = spline.times;
  10. // use slerp interpolation
  11. return function(time, result) {
  12. if (!defined(result)){
  13. result = new Quaternion();
  14. }
  15. var i = spline._lastTimeIndex = spline.findTimeInterval(time, spline._lastTimeIndex);
  16. var u = (time - times[i]) / (times[i + 1] - times[i]);
  17. var q0 = points[i];
  18. var q1 = points[i + 1];
  19. return Quaternion.fastSlerp(q0, q1, u, result);
  20. };
  21. }
  22. /**
  23. * A spline that uses spherical linear (slerp) interpolation to create a quaternion curve.
  24. * The generated curve is in the class C<sup>1</sup>.
  25. *
  26. * @alias QuaternionSpline
  27. * @constructor
  28. *
  29. * @param {Object} options Object with the following properties:
  30. * @param {Number[]} options.times An array of strictly increasing, unit-less, floating-point times at each point.
  31. * The values are in no way connected to the clock time. They are the parameterization for the curve.
  32. * @param {Quaternion[]} options.points The array of {@link Quaternion} control points.
  33. *
  34. * @exception {DeveloperError} points.length must be greater than or equal to 2.
  35. * @exception {DeveloperError} times.length must be equal to points.length.
  36. *
  37. * @see HermiteSpline
  38. * @see CatmullRomSpline
  39. * @see LinearSpline
  40. * @see WeightSpline
  41. */
  42. function QuaternionSpline(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._evaluateFunction = createEvaluateFunction(this);
  60. this._lastTimeIndex = 0;
  61. }
  62. defineProperties(QuaternionSpline.prototype, {
  63. /**
  64. * An array of times for the control points.
  65. *
  66. * @memberof QuaternionSpline.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 Quaternion} control points.
  78. *
  79. * @memberof QuaternionSpline.prototype
  80. *
  81. * @type {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. * @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. QuaternionSpline.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. QuaternionSpline.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. QuaternionSpline.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 {Quaternion} [result] The object onto which to store the result.
  124. * @returns {Quaternion} 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. QuaternionSpline.prototype.evaluate = function(time, result) {
  131. return this._evaluateFunction(time, result);
  132. };
  133. export default QuaternionSpline;