CatmullRomSpline.js 9.4 KB

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