15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { Cartesian3 } from "../../Source/Cesium.js";
import { HermiteSpline } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
 
describe("Core/HermiteSpline", function () {
  var points;
  var times;
 
  beforeEach(function () {
    points = [
      new Cartesian3(-1.0, -1.0, 0.0),
      new Cartesian3(-0.5, -0.125, 0.0),
      new Cartesian3(0.5, 0.125, 0.0),
      new Cartesian3(1.0, 1.0, 0.0),
    ];
    times = [0.0, 1.0, 2.0, 3.0];
  });
 
  it("constructor throws without points, times or tangents", function () {
    expect(function () {
      return new HermiteSpline();
    }).toThrowDeveloperError();
  });
 
  it("constructor throws when control points length is less than 2", function () {
    expect(function () {
      return new HermiteSpline({
        points: [Cartesian3.ZERO],
      });
    }).toThrowDeveloperError();
  });
 
  it("constructor throws when times.length is not equal to points.length", function () {
    expect(function () {
      return new HermiteSpline({
        points: points,
        times: [0.0, 1.0],
      });
    }).toThrowDeveloperError();
  });
 
  it("constructor throws when inTangents or outTangents length is not equal to points.length - 1", function () {
    expect(function () {
      return new HermiteSpline({
        points: points,
        times: times,
        inTangents: [Cartesian3.ZERO],
        outTangents: [Cartesian3.UNIT_X],
      });
    }).toThrowDeveloperError();
  });
 
  // returns a function for a hermite curve between points p and q
  // with tangents pT and qT respectively on the interval [0, 1]
  function createHermiteBasis(p, pT, q, qT) {
    return function (u) {
      var a = 2.0 * u * u * u - 3.0 * u * u + 1.0;
      var b = -2.0 * u * u * u + 3.0 * u * u;
      var c = u * u * u - 2.0 * u * u + u;
      var d = u * u * u - u * u;
 
      var p0 = Cartesian3.multiplyByScalar(p, a, new Cartesian3());
      var p1 = Cartesian3.multiplyByScalar(q, b, new Cartesian3());
      var p2 = Cartesian3.multiplyByScalar(pT, c, new Cartesian3());
      var p3 = Cartesian3.multiplyByScalar(qT, d, new Cartesian3());
 
      return Cartesian3.add(
        Cartesian3.add(Cartesian3.add(p0, p1, p0), p2, p0),
        p3,
        p0
      );
    };
  }
 
  it("create spline", function () {
    var hs = new HermiteSpline({
      times: [0.0, 1.0, 3.0, 4.5, 6.0],
      points: [
        new Cartesian3(1235398.0, -4810983.0, 4146266.0),
        new Cartesian3(1372574.0, -5345182.0, 4606657.0),
        new Cartesian3(-757983.0, -5542796.0, 4514323.0),
        new Cartesian3(-2821260.0, -5248423.0, 4021290.0),
        new Cartesian3(-2539788.0, -4724797.0, 3620093.0),
      ],
      outTangents: [
        new Cartesian3(1125196, -161816, 270551),
        new Cartesian3(-996690.5, -365906.5, 184028.5),
        new Cartesian3(-2096917, 48379.5, -292683.5),
        new Cartesian3(-890902.5, 408999.5, -447115),
      ],
      inTangents: [
        new Cartesian3(-1993381, -731813, 368057),
        new Cartesian3(-4193834, 96759, -585367),
        new Cartesian3(-1781805, 817999, -894230),
        new Cartesian3(1165345, 112641, 47281),
      ],
    });
 
    var p0 = hs.points[0];
    var p1 = hs.points[1];
    var pT0 = hs.outTangents[0];
    var pT1 = hs.inTangents[0];
    var interpolate = createHermiteBasis(p0, pT0, p1, pT1);
 
    var granularity = 0.1;
    for (var i = 0.0; i < 1.0; i = i + granularity) {
      expect(hs.evaluate(i)).toEqualEpsilon(
        interpolate(i),
        CesiumMath.EPSILON3
      );
    }
  });
 
  it("createC1 throws without points", function () {
    expect(function () {
      return HermiteSpline.createC1();
    }).toThrowDeveloperError();
  });
 
  it("createC1 throws when control points length is less than 2", function () {
    expect(function () {
      return HermiteSpline.createC1({
        points: [Cartesian3.ZERO],
      });
    }).toThrowDeveloperError();
  });
 
  it("createC1 throws without times", function () {
    expect(function () {
      return HermiteSpline.createC1({
        points: points,
      });
    }).toThrowDeveloperError();
  });
 
  it("createC1 throws when times.length is not equal to points.length", function () {
    expect(function () {
      return HermiteSpline.createC1({
        points: points,
        times: [0.0, 1.0],
      });
    }).toThrowDeveloperError();
  });
 
  it("createC1 throws without tangents", function () {
    expect(function () {
      return HermiteSpline.createC1({
        points: points,
        times: times,
      });
    }).toThrowDeveloperError();
  });
 
  it("createC1 throws when tangents.length is not equal to times.length", function () {
    expect(function () {
      return HermiteSpline.createC1({
        points: points,
        times: times,
        tangents: [Cartesian3.ZERO],
      });
    }).toThrowDeveloperError();
  });
 
  it("C1 spline", function () {
    var times = [0.0, 1.0, 3.0, 4.5, 6.0];
    var points = [
      new Cartesian3(1235398.0, -4810983.0, 4146266.0),
      new Cartesian3(1372574.0, -5345182.0, 4606657.0),
      new Cartesian3(-757983.0, -5542796.0, 4514323.0),
      new Cartesian3(-2821260.0, -5248423.0, 4021290.0),
      new Cartesian3(-2539788.0, -4724797.0, 3620093.0),
    ];
 
    var tangents = new Array(points.length);
    tangents[0] = new Cartesian3(1125196, -161816, 270551);
    for (var i = 1; i < tangents.length - 1; ++i) {
      tangents[i] = Cartesian3.multiplyByScalar(
        Cartesian3.subtract(points[i + 1], points[i - 1], new Cartesian3()),
        0.5,
        new Cartesian3()
      );
    }
    tangents[tangents.length - 1] = new Cartesian3(1165345, 112641, 47281);
 
    var interpolate = createHermiteBasis(
      points[0],
      tangents[0],
      points[1],
      tangents[1]
    );
    var hs = HermiteSpline.createC1({
      times: times,
      points: points,
      tangents: tangents,
    });
 
    var granularity = 0.1;
    for (var j = times[0]; j < times[1]; j = j + granularity) {
      expect(hs.evaluate(j)).toEqualEpsilon(
        interpolate(j),
        CesiumMath.EPSILON3
      );
    }
  });
 
  it("createNaturalCubic throws without points", function () {
    expect(function () {
      return HermiteSpline.createNaturalCubic();
    }).toThrowDeveloperError();
  });
 
  it("createNaturalCubic throws when control points length is less than 2", function () {
    expect(function () {
      return HermiteSpline.createNaturalCubic({
        points: [Cartesian3.ZERO],
      });
    }).toThrowDeveloperError();
  });
 
  it("createNaturalCubic throws without times", function () {
    expect(function () {
      return HermiteSpline.createNaturalCubic({
        points: points,
      });
    }).toThrowDeveloperError();
  });
 
  it("createNaturalCubic throws when times.length is not equal to points.length", function () {
    expect(function () {
      return HermiteSpline.createNaturalCubic({
        points: points,
        times: [0.0, 1.0],
      });
    }).toThrowDeveloperError();
  });
 
  it("natural cubic spline", function () {
    points = [
      new Cartesian3(1.0, 0.0, 0.0),
      new Cartesian3(0.0, 1.0, CesiumMath.PI_OVER_TWO),
      new Cartesian3(-1.0, 0.0, Math.PI),
      new Cartesian3(0.0, -1.0, CesiumMath.THREE_PI_OVER_TWO),
    ];
 
    var p0Tangent = new Cartesian3(-0.87, 1.53, 1.57);
    var p1Tangent = new Cartesian3(-1.27, -0.07, 1.57);
 
    var interpolate = createHermiteBasis(
      points[0],
      p0Tangent,
      points[1],
      p1Tangent
    );
    var hs = HermiteSpline.createNaturalCubic({
      points: points,
      times: times,
    });
 
    var granularity = 0.1;
    for (var i = times[0]; i < times[1]; i = i + granularity) {
      expect(hs.evaluate(i)).toEqualEpsilon(
        interpolate(i),
        CesiumMath.EPSILON3
      );
    }
  });
 
  it("createClampedCubic throws without points", function () {
    expect(function () {
      return HermiteSpline.createClampedCubic();
    }).toThrowDeveloperError();
  });
 
  it("createClampedCubic throws when control points length is less than 2", function () {
    expect(function () {
      return HermiteSpline.createClampedCubic({
        points: [Cartesian3.ZERO],
      });
    }).toThrowDeveloperError();
  });
 
  it("createClampedCubic throws without times", function () {
    expect(function () {
      return HermiteSpline.createClampedCubic({
        points: points,
      });
    }).toThrowDeveloperError();
  });
 
  it("createClampedCubic throws when times.length is not equal to points.length", function () {
    expect(function () {
      return HermiteSpline.createClampedCubic({
        points: points,
        times: [0.0, 1.0],
      });
    }).toThrowDeveloperError();
  });
 
  it("createClampedCubic throws without firstTangent", function () {
    expect(function () {
      return HermiteSpline.createClampedCubic({
        points: points,
        times: times,
      });
    }).toThrowDeveloperError();
  });
 
  it("createClampedCubic throws without lastTangent", function () {
    expect(function () {
      return HermiteSpline.createClampedCubic({
        points: points,
        times: times,
        firstTangent: Cartesian3.ZERO,
      });
    }).toThrowDeveloperError();
  });
 
  it("clamped cubic spline", function () {
    points = [
      new Cartesian3(1.0, 0.0, 0.0),
      new Cartesian3(0.0, 1.0, CesiumMath.PI_OVER_TWO),
      new Cartesian3(-1.0, 0.0, Math.PI),
      new Cartesian3(0.0, -1.0, CesiumMath.THREE_PI_OVER_TWO),
    ];
 
    var firstTangent = new Cartesian3(0.0, 1.0, 0.0);
    var lastTangent = new Cartesian3(1.0, 0.0, 0.0);
 
    var p0Tangent = firstTangent;
    var p1Tangent = new Cartesian3(-1.53, 0.13, 1.88);
 
    var interpolate = createHermiteBasis(
      points[0],
      p0Tangent,
      points[1],
      p1Tangent
    );
    var hs = HermiteSpline.createClampedCubic({
      points: points,
      times: times,
      firstTangent: firstTangent,
      lastTangent: lastTangent,
    });
 
    var granularity = 0.1;
    for (var i = points[0].time; i < points[1].time; i = i + granularity) {
      expect(hs.evaluate(i)).toEqualEpsilon(
        interpolate(i),
        CesiumMath.EPSILON3
      );
    }
  });
 
  it("evaluate fails with undefined time", function () {
    var hs = HermiteSpline.createNaturalCubic({
      points: points,
      times: times,
    });
    expect(function () {
      hs.evaluate();
    }).toThrowDeveloperError();
  });
 
  it("evaluate fails with time out of range", function () {
    var hs = HermiteSpline.createNaturalCubic({
      points: points,
      times: times,
    });
    expect(function () {
      hs.evaluate(times[0] - 1.0);
    }).toThrowDeveloperError();
  });
 
  it("evaluate with result parameter", function () {
    var hs = HermiteSpline.createNaturalCubic({
      points: points,
      times: times,
    });
    var result = new Cartesian3();
    var point = hs.evaluate(times[0], result);
    expect(point).toBe(result);
    expect(result).toEqual(points[0]);
  });
 
  it("createNaturalCubic with 2 control points defaults to lerp", function () {
    points = points.slice(0, 2);
    times = times.slice(0, 2);
 
    var hs = HermiteSpline.createNaturalCubic({
      points: points,
      times: times,
    });
 
    var t = (times[0] + times[1]) * 0.5;
    expect(hs.evaluate(t)).toEqual(
      Cartesian3.lerp(points[0], points[1], t, new Cartesian3())
    );
  });
 
  it("createClampedCubic with 2 control points defaults to lerp", function () {
    points = points.slice(0, 2);
    times = times.slice(0, 2);
 
    var hs = HermiteSpline.createNaturalCubic({
      points: points,
      times: times,
    });
 
    var t = (times[0] + times[1]) * 0.5;
    expect(hs.evaluate(t)).toEqual(
      Cartesian3.lerp(points[0], points[1], t, new Cartesian3())
    );
  });
});