ConstantSpline.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import Check from "./Check.js";
  2. import DeveloperError from "./DeveloperError.js";
  3. import Spline from "./Spline.js";
  4. /**
  5. * A spline that evaluates to a constant value. Although this follows the {@link Spline} interface,
  6. * it does not maintain an internal array of times since its value never changes.
  7. *
  8. * @alias ConstantSpline
  9. * @constructor
  10. *
  11. * @param {number|Cartesian3|Quaternion} value The constant value that the spline evaluates to.
  12. *
  13. * @example
  14. * const position = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  15. * const spline = new Cesium.ConstantSpline(position);
  16. *
  17. * const p0 = spline.evaluate(0.0);
  18. *
  19. * @see LinearSpline
  20. * @see HermiteSpline
  21. * @see CatmullRomSpline
  22. * @see QuaternionSpline
  23. * @see MorphWeightSpline
  24. */
  25. function ConstantSpline(value) {
  26. this._value = value;
  27. this._valueType = Spline.getPointType(value);
  28. }
  29. Object.defineProperties(ConstantSpline.prototype, {
  30. /**
  31. * The constant value that the spline evaluates to.
  32. *
  33. * @memberof ConstantSpline.prototype
  34. *
  35. * @type {number|Cartesian3|Quaternion}
  36. * @readonly
  37. */
  38. value: {
  39. get: function () {
  40. return this._value;
  41. },
  42. },
  43. });
  44. /**
  45. * Finds an index <code>i</code> in <code>times</code> such that the parameter
  46. * <code>time</code> is in the interval <code>[times[i], times[i + 1]]</code>.
  47. *
  48. * Since a constant spline has no internal times array, this will throw an error.
  49. * @function
  50. *
  51. * @param {number} time The time.
  52. *
  53. * @exception {DeveloperError} findTimeInterval cannot be called on a ConstantSpline.
  54. */
  55. ConstantSpline.prototype.findTimeInterval = function (time) {
  56. //>>includeStart('debug', pragmas.debug);
  57. throw new DeveloperError(
  58. "findTimeInterval cannot be called on a ConstantSpline."
  59. );
  60. //>>includeEnd('debug');
  61. };
  62. /**
  63. * Wraps the given time to the period covered by the spline.
  64. * @function
  65. *
  66. * @param {number} time The time.
  67. * @return {number} The time, wrapped around to the updated animation.
  68. */
  69. ConstantSpline.prototype.wrapTime = function (time) {
  70. //>>includeStart('debug', pragmas.debug);
  71. Check.typeOf.number("time", time);
  72. //>>includeEnd('debug');
  73. return 0.0;
  74. };
  75. /**
  76. * Clamps the given time to the period covered by the spline.
  77. * @function
  78. *
  79. * @param {number} time The time.
  80. * @return {number} The time, clamped to the animation period.
  81. */
  82. ConstantSpline.prototype.clampTime = function (time) {
  83. //>>includeStart('debug', pragmas.debug);
  84. Check.typeOf.number("time", time);
  85. //>>includeEnd('debug');
  86. return 0.0;
  87. };
  88. /**
  89. * Evaluates the curve at a given time.
  90. * @function
  91. *
  92. * @param {number} time The time at which to evaluate the curve.
  93. * @param {Cartesian3|Quaternion} [result] The object onto which to store the result.
  94. * @returns {number|Cartesian3|Quaternion} The modified result parameter or the value that the constant spline represents.
  95. */
  96. ConstantSpline.prototype.evaluate = function (time, result) {
  97. //>>includeStart('debug', pragmas.debug);
  98. Check.typeOf.number("time", time);
  99. //>>includeEnd('debug');
  100. const value = this._value;
  101. const ValueType = this._valueType;
  102. if (ValueType === Number) {
  103. return value;
  104. }
  105. return ValueType.clone(value, result);
  106. };
  107. export default ConstantSpline;