yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
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
import { Cartesian3 } from "../../Source/Cesium.js";
import { CatmullRomSpline } from "../../Source/Cesium.js";
import { HermiteSpline } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
 
describe("Core/CatmullRomSpline", 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 or times", function () {
    expect(function () {
      return new CatmullRomSpline();
    }).toThrowDeveloperError();
  });
 
  it("constructor throws when control points length is less than 2", function () {
    expect(function () {
      return new CatmullRomSpline({
        points: [Cartesian3.ZERO],
      });
    }).toThrowDeveloperError();
  });
 
  it("constructor throws when times.length is not equal to points.length", function () {
    expect(function () {
      return new CatmullRomSpline({
        points: points,
        times: [0.0, 1.0],
      });
    }).toThrowDeveloperError();
  });
 
  it("sets start and end tangents", function () {
    var start = Cartesian3.subtract(points[1], points[0], new Cartesian3());
    var end = Cartesian3.subtract(
      points[points.length - 1],
      points[points.length - 2],
      new Cartesian3()
    );
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
      firstTangent: start,
      lastTangent: end,
    });
 
    expect(start).toEqual(crs.firstTangent);
    expect(end).toEqual(crs.lastTangent);
  });
 
  it("computes start and end tangents", function () {
    var controlPoint0 = Cartesian3.clone(points[0]);
    var controlPoint1 = Cartesian3.clone(points[1]);
    var controlPoint2 = Cartesian3.clone(points[2]);
 
    var start = new Cartesian3();
    start = Cartesian3.multiplyByScalar(
      Cartesian3.subtract(
        Cartesian3.subtract(
          Cartesian3.multiplyByScalar(controlPoint1, 2.0, start),
          controlPoint2,
          start
        ),
        controlPoint0,
        start
      ),
      0.5,
      start
    );
 
    var controlPointn0 = Cartesian3.clone(points[points.length - 1]);
    var controlPointn1 = Cartesian3.clone(points[points.length - 2]);
    var controlPointn2 = Cartesian3.clone(points[points.length - 3]);
 
    var end = new Cartesian3();
    end = Cartesian3.multiplyByScalar(
      Cartesian3.add(
        Cartesian3.subtract(
          controlPointn0,
          Cartesian3.multiplyByScalar(controlPointn1, 2.0, end),
          end
        ),
        controlPointn2,
        end
      ),
      0.5,
      end
    );
 
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
 
    expect(start).toEqual(crs.firstTangent);
    expect(end).toEqual(crs.lastTangent);
  });
 
  it("evaluate throws without time", function () {
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
 
    expect(function () {
      crs.evaluate();
    }).toThrowDeveloperError();
  });
 
  it("evaluate throws when time is out of range", function () {
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
 
    expect(function () {
      crs.evaluate(times[0] - 1.0);
    }).toThrowDeveloperError();
  });
 
  it("check Catmull-Rom spline against a Hermite spline", function () {
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
 
    var tangents = [crs.firstTangent];
    for (var i = 1; i < points.length - 1; ++i) {
      tangents.push(
        Cartesian3.multiplyByScalar(
          Cartesian3.subtract(points[i + 1], points[i - 1], new Cartesian3()),
          0.5,
          new Cartesian3()
        )
      );
    }
    tangents.push(crs.lastTangent);
 
    var hs = HermiteSpline.createC1({
      points: points,
      tangents: tangents,
      times: times,
    });
 
    var granularity = 0.5;
    for (var j = times[0]; j <= times[points.length - 1]; j = j + granularity) {
      expect(hs.evaluate(j)).toEqualEpsilon(
        crs.evaluate(j),
        CesiumMath.EPSILON4
      );
    }
  });
 
  it("evaluate with result parameter", function () {
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
    var result = new Cartesian3();
 
    var point = crs.evaluate(times[0], result);
    expect(point).toBe(result);
    expect(result).toEqual(points[0]);
  });
 
  it("spline with 2 control points defaults to lerp", function () {
    points = points.slice(0, 2);
    times = times.slice(0, 2);
 
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
 
    var t = (times[0] + times[1]) * 0.5;
    expect(crs.evaluate(t)).toEqual(
      Cartesian3.lerp(points[0], points[1], t, new Cartesian3())
    );
  });
 
  it("spline with 2 control points defaults to lerp and result parameter", function () {
    points = points.slice(0, 2);
    times = times.slice(0, 2);
 
    var crs = new CatmullRomSpline({
      points: points,
      times: times,
    });
 
    var t = (times[0] + times[1]) * 0.5;
    var result = new Cartesian3();
    var actual = crs.evaluate(t, result);
    expect(actual).toBe(result);
    expect(actual).toEqual(
      Cartesian3.lerp(points[0], points[1], t, new Cartesian3())
    );
  });
});