CatmullRomSpline.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartesian4 from './Cartesian4.js';
  3. import Check from './Check.js';
  4. import defaultValue from './defaultValue.js';
  5. import defined from './defined.js';
  6. import defineProperties from './defineProperties.js';
  7. import HermiteSpline from './HermiteSpline.js';
  8. import Matrix4 from './Matrix4.js';
  9. import Spline from './Spline.js';
  10. var scratchTimeVec = new Cartesian4();
  11. var scratchTemp0 = new Cartesian3();
  12. var scratchTemp1 = new Cartesian3();
  13. function createEvaluateFunction(spline) {
  14. var points = spline.points;
  15. var times = spline.times;
  16. if (points.length < 3) {
  17. var t0 = times[0];
  18. var invSpan = 1.0 / (times[1] - t0);
  19. var p0 = points[0];
  20. var p1 = points[1];
  21. return function(time, result) {
  22. if (!defined(result)){
  23. result = new Cartesian3();
  24. }
  25. var u = (time - t0) * invSpan;
  26. return Cartesian3.lerp(p0, p1, u, result);
  27. };
  28. }
  29. return function(time, result) {
  30. if (!defined(result)) {
  31. result = new Cartesian3();
  32. }
  33. var i = spline._lastTimeIndex = spline.findTimeInterval(time, spline._lastTimeIndex);
  34. var u = (time - times[i]) / (times[i + 1] - times[i]);
  35. var timeVec = scratchTimeVec;
  36. timeVec.z = u;
  37. timeVec.y = u * u;
  38. timeVec.x = timeVec.y * u;
  39. timeVec.w = 1.0;
  40. var p0;
  41. var p1;
  42. var p2;
  43. var p3;
  44. var coefs;
  45. if (i === 0) {
  46. p0 = points[0];
  47. p1 = points[1];
  48. p2 = spline.firstTangent;
  49. p3 = Cartesian3.subtract(points[2], p0, scratchTemp0);
  50. Cartesian3.multiplyByScalar(p3, 0.5, p3);
  51. coefs = Matrix4.multiplyByVector(HermiteSpline.hermiteCoefficientMatrix, timeVec, timeVec);
  52. } else if (i === points.length - 2) {
  53. p0 = points[i];
  54. p1 = points[i + 1];
  55. p3 = spline.lastTangent;
  56. p2 = Cartesian3.subtract(p1, points[i - 1], scratchTemp0);
  57. Cartesian3.multiplyByScalar(p2, 0.5, p2);
  58. coefs = Matrix4.multiplyByVector(HermiteSpline.hermiteCoefficientMatrix, timeVec, timeVec);
  59. } else {
  60. p0 = points[i - 1];
  61. p1 = points[i];
  62. p2 = points[i + 1];
  63. p3 = points[i + 2];
  64. coefs = Matrix4.multiplyByVector(CatmullRomSpline.catmullRomCoefficientMatrix, timeVec, timeVec);
  65. }
  66. result = Cartesian3.multiplyByScalar(p0, coefs.x, result);
  67. Cartesian3.multiplyByScalar(p1, coefs.y, scratchTemp1);
  68. Cartesian3.add(result, scratchTemp1, result);
  69. Cartesian3.multiplyByScalar(p2, coefs.z, scratchTemp1);
  70. Cartesian3.add(result, scratchTemp1, result);
  71. Cartesian3.multiplyByScalar(p3, coefs.w, scratchTemp1);
  72. return Cartesian3.add(result, scratchTemp1, result);
  73. };
  74. }
  75. var firstTangentScratch = new Cartesian3();
  76. var lastTangentScratch = new Cartesian3();
  77. /**
  78. * A Catmull-Rom spline is a cubic spline where the tangent at control points,
  79. * except the first and last, are computed using the previous and next control points.
  80. * Catmull-Rom splines are in the class C<sup>1</sup>.
  81. *
  82. * @alias CatmullRomSpline
  83. * @constructor
  84. *
  85. * @param {Object} options Object with the following properties:
  86. * @param {Number[]} options.times An array of strictly increasing, unit-less, floating-point times at each point.
  87. * The values are in no way connected to the clock time. They are the parameterization for the curve.
  88. * @param {Cartesian3[]} options.points The array of {@link Cartesian3} control points.
  89. * @param {Cartesian3} [options.firstTangent] The tangent of the curve at the first control point.
  90. * If the tangent is not given, it will be estimated.
  91. * @param {Cartesian3} [options.lastTangent] The tangent of the curve at the last control point.
  92. * If the tangent is not given, it will be estimated.
  93. *
  94. * @exception {DeveloperError} points.length must be greater than or equal to 2.
  95. * @exception {DeveloperError} times.length must be equal to points.length.
  96. *
  97. *
  98. * @example
  99. * // spline above the earth from Philadelphia to Los Angeles
  100. * var spline = new Cesium.CatmullRomSpline({
  101. * times : [ 0.0, 1.5, 3.0, 4.5, 6.0 ],
  102. * points : [
  103. * new Cesium.Cartesian3(1235398.0, -4810983.0, 4146266.0),
  104. * new Cesium.Cartesian3(1372574.0, -5345182.0, 4606657.0),
  105. * new Cesium.Cartesian3(-757983.0, -5542796.0, 4514323.0),
  106. * new Cesium.Cartesian3(-2821260.0, -5248423.0, 4021290.0),
  107. * new Cesium.Cartesian3(-2539788.0, -4724797.0, 3620093.0)
  108. * ]
  109. * });
  110. *
  111. * var p0 = spline.evaluate(times[i]); // equal to positions[i]
  112. * var p1 = spline.evaluate(times[i] + delta); // interpolated value when delta < times[i + 1] - times[i]
  113. *
  114. * @see HermiteSpline
  115. * @see LinearSpline
  116. * @see QuaternionSpline
  117. * @see WeightSpline
  118. */
  119. function CatmullRomSpline(options) {
  120. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  121. var points = options.points;
  122. var times = options.times;
  123. var firstTangent = options.firstTangent;
  124. var lastTangent = options.lastTangent;
  125. //>>includeStart('debug', pragmas.debug);
  126. Check.defined('points', points);
  127. Check.defined('times', times);
  128. Check.typeOf.number.greaterThanOrEquals('points.length', points.length, 2);
  129. Check.typeOf.number.equals('times.length', 'points.length', times.length, points.length);
  130. //>>includeEnd('debug');
  131. if (points.length > 2) {
  132. if (!defined(firstTangent)) {
  133. firstTangent = firstTangentScratch;
  134. Cartesian3.multiplyByScalar(points[1], 2.0, firstTangent);
  135. Cartesian3.subtract(firstTangent, points[2], firstTangent);
  136. Cartesian3.subtract(firstTangent, points[0], firstTangent);
  137. Cartesian3.multiplyByScalar(firstTangent, 0.5, firstTangent);
  138. }
  139. if (!defined(lastTangent)) {
  140. var n = points.length - 1;
  141. lastTangent = lastTangentScratch;
  142. Cartesian3.multiplyByScalar(points[n - 1], 2.0, lastTangent);
  143. Cartesian3.subtract(points[n], lastTangent, lastTangent);
  144. Cartesian3.add(lastTangent, points[n - 2], lastTangent);
  145. Cartesian3.multiplyByScalar(lastTangent, 0.5, lastTangent);
  146. }
  147. }
  148. this._times = times;
  149. this._points = points;
  150. this._firstTangent = Cartesian3.clone(firstTangent);
  151. this._lastTangent = Cartesian3.clone(lastTangent);
  152. this._evaluateFunction = createEvaluateFunction(this);
  153. this._lastTimeIndex = 0;
  154. }
  155. defineProperties(CatmullRomSpline.prototype, {
  156. /**
  157. * An array of times for the control points.
  158. *
  159. * @memberof CatmullRomSpline.prototype
  160. *
  161. * @type {Number[]}
  162. * @readonly
  163. */
  164. times : {
  165. get : function() {
  166. return this._times;
  167. }
  168. },
  169. /**
  170. * An array of {@link Cartesian3} control points.
  171. *
  172. * @memberof CatmullRomSpline.prototype
  173. *
  174. * @type {Cartesian3[]}
  175. * @readonly
  176. */
  177. points : {
  178. get : function() {
  179. return this._points;
  180. }
  181. },
  182. /**
  183. * The tangent at the first control point.
  184. *
  185. * @memberof CatmullRomSpline.prototype
  186. *
  187. * @type {Cartesian3}
  188. * @readonly
  189. */
  190. firstTangent : {
  191. get : function() {
  192. return this._firstTangent;
  193. }
  194. },
  195. /**
  196. * The tangent at the last control point.
  197. *
  198. * @memberof CatmullRomSpline.prototype
  199. *
  200. * @type {Cartesian3}
  201. * @readonly
  202. */
  203. lastTangent : {
  204. get : function() {
  205. return this._lastTangent;
  206. }
  207. }
  208. });
  209. /**
  210. * @private
  211. */
  212. CatmullRomSpline.catmullRomCoefficientMatrix = new Matrix4(
  213. -0.5, 1.0, -0.5, 0.0,
  214. 1.5, -2.5, 0.0, 1.0,
  215. -1.5, 2.0, 0.5, 0.0,
  216. 0.5, -0.5, 0.0, 0.0);
  217. /**
  218. * Finds an index <code>i</code> in <code>times</code> such that the parameter
  219. * <code>time</code> is in the interval <code>[times[i], times[i + 1]]</code>.
  220. * @function
  221. *
  222. * @param {Number} time The time.
  223. * @returns {Number} The index for the element at the start of the interval.
  224. *
  225. * @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>
  226. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  227. * in the array <code>times</code>.
  228. */
  229. CatmullRomSpline.prototype.findTimeInterval = Spline.prototype.findTimeInterval;
  230. /**
  231. * Wraps the given time to the period covered by the spline.
  232. * @function
  233. *
  234. * @param {Number} time The time.
  235. * @return {Number} The time, wrapped around to the updated animation.
  236. */
  237. CatmullRomSpline.prototype.wrapTime = Spline.prototype.wrapTime;
  238. /**
  239. * Clamps the given time to the period covered by the spline.
  240. * @function
  241. *
  242. * @param {Number} time The time.
  243. * @return {Number} The time, clamped to the animation period.
  244. */
  245. CatmullRomSpline.prototype.clampTime = Spline.prototype.clampTime;
  246. /**
  247. * Evaluates the curve at a given time.
  248. *
  249. * @param {Number} time The time at which to evaluate the curve.
  250. * @param {Cartesian3} [result] The object onto which to store the result.
  251. * @returns {Cartesian3} The modified result parameter or a new instance of the point on the curve at the given time.
  252. *
  253. * @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>
  254. * is the first element in the array <code>times</code> and <code>t<sub>n</sub></code> is the last element
  255. * in the array <code>times</code>.
  256. */
  257. CatmullRomSpline.prototype.evaluate = function(time, result) {
  258. return this._evaluateFunction(time, result);
  259. };
  260. export default CatmullRomSpline;