-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathindex.js
More file actions
299 lines (256 loc) · 10.6 KB
/
index.js
File metadata and controls
299 lines (256 loc) · 10.6 KB
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
import macro from 'vtk.js/Sources/macros';
import vtkSpline1D from 'vtk.js/Sources/Common/DataModel/Spline1D';
import { BoundaryCondition } from 'vtk.js/Sources/Common/DataModel/Spline1D/Constants';
const VTK_EPSILON = 0.0001;
// ----------------------------------------------------------------------------
// vtkCardinalSpline1D methods
// ----------------------------------------------------------------------------
function vtkCardinalSpline1D(publicAPI, model) {
// Set our classname
model.classHierarchy.push('vtkCardinalSpline1D');
// --------------------------------------------------------------------------
publicAPI.computeCloseCoefficients = (size, work, x, y) => {
if (!model.coefficients || model.coefficients.length !== 4 * size) {
model.coefficients = new Float32Array(4 * size);
}
const N = size - 1;
for (let k = 1; k < N; k++) {
const xlk = x[k] - x[k - 1];
const xlkp = x[k + 1] - x[k];
model.coefficients[4 * k + 0] = xlkp;
model.coefficients[4 * k + 1] = 2 * (xlkp + xlk);
model.coefficients[4 * k + 2] = xlk;
work[k] =
3.0 *
((xlkp * (y[k] - y[k - 1])) / xlk + (xlk * (y[k + 1] - y[k])) / xlkp);
}
const xlk = x[N] - x[N - 1];
const xlkp = x[1] - x[0];
model.coefficients[4 * N + 0] = xlkp;
model.coefficients[4 * N + 1] = 2 * (xlkp + xlk);
model.coefficients[4 * N + 2] = xlk;
work[N] =
3 * ((xlkp * (y[N] - y[N - 1])) / xlk + (xlk * (y[1] - y[0])) / xlkp);
const aN = model.coefficients[4 * N + 0];
const bN = model.coefficients[4 * N + 1];
const cN = model.coefficients[4 * N + 2];
const dN = work[N];
// solve resulting set of equations.
model.coefficients[4 * 0 + 2] = 0;
work[0] = 0;
model.coefficients[4 * 0 + 3] = 1;
for (let k = 1; k <= N; k++) {
model.coefficients[4 * k + 1] -=
model.coefficients[4 * k + 0] * model.coefficients[4 * (k - 1) + 2];
model.coefficients[4 * k + 2] =
model.coefficients[4 * k + 2] / model.coefficients[4 * k + 1];
work[k] =
(work[k] - model.coefficients[4 * k + 0] * work[k - 1]) /
model.coefficients[4 * k + 1];
model.coefficients[4 * k + 3] =
(-model.coefficients[4 * k + 0] * model.coefficients[4 * (k - 1) + 3]) /
model.coefficients[4 * k + 1];
}
model.coefficients[4 * N + 0] = 1;
model.coefficients[4 * N + 1] = 0;
for (let k = N - 1; k > 0; k--) {
model.coefficients[4 * k + 0] =
model.coefficients[4 * k + 3] -
model.coefficients[4 * k + 2] * model.coefficients[4 * (k + 1) + 0];
model.coefficients[4 * k + 1] =
work[k] -
model.coefficients[4 * k + 2] * model.coefficients[4 * (k + 1) + 1];
}
work[0] =
(dN -
cN * model.coefficients[4 * 1 + 1] -
aN * model.coefficients[4 * (N - 1) + 1]) /
(bN +
cN * model.coefficients[4 * 1 + 0] +
aN * model.coefficients[4 * (N - 1) + 0]);
work[N] = work[0];
for (let k = 1; k < N; k++) {
work[k] =
model.coefficients[4 * k + 0] * work[N] + model.coefficients[4 * k + 1];
}
// the column vector work now contains the first
// derivative of the spline function at each joint.
// compute the coefficients of the cubic between
// each pair of joints.
for (let k = 0; k < N; k++) {
const b = x[k + 1] - x[k];
model.coefficients[4 * k + 0] = y[k];
model.coefficients[4 * k + 1] = work[k];
model.coefficients[4 * k + 2] =
(3 * (y[k + 1] - y[k])) / (b * b) - (work[k + 1] + 2 * work[k]) / b;
model.coefficients[4 * k + 3] =
(2 * (y[k] - y[k + 1])) / (b * b * b) +
(work[k + 1] + work[k]) / (b * b);
}
// the coefficients of a fictitious nth cubic
// are the same as the coefficients in the first interval
model.coefficients[4 * N + 0] = y[N];
model.coefficients[4 * N + 1] = work[N];
model.coefficients[4 * N + 2] = model.coefficients[4 * 0 + 2];
model.coefficients[4 * N + 3] = model.coefficients[4 * 0 + 3];
};
// --------------------------------------------------------------------------
publicAPI.computeOpenCoefficients = (size, work, x, y, options = {}) => {
if (!model.coefficients || model.coefficients.length !== 4 * size) {
model.coefficients = new Float32Array(4 * size);
}
const N = size - 1;
// develop constraint at leftmost point.
switch (options.leftConstraint) {
case BoundaryCondition.DERIVATIVE:
// desired slope at leftmost point is leftValue.
model.coefficients[4 * 0 + 1] = 1.0;
model.coefficients[4 * 0 + 2] = 0.0;
work[0] = options.leftValue;
break;
case BoundaryCondition.SECOND_DERIVATIVE:
// desired second derivative at leftmost point is leftValue.
model.coefficients[4 * 0 + 1] = 2.0;
model.coefficients[4 * 0 + 2] = 1.0;
work[0] =
3.0 * ((y[1] - y[0]) / (x[1] - x[0])) -
0.5 * (x[1] - x[0]) * options.leftValue;
break;
case BoundaryCondition.SECOND_DERIVATIVE_INTERIOR_POINT:
// desired second derivative at leftmost point is
// leftValue times second derivative at first interior point.
model.coefficients[4 * 0 + 1] = 2.0;
if (Math.abs(options.leftValue + 2) > VTK_EPSILON) {
model.coefficients[4 * 0 + 2] =
4.0 * ((0.5 + options.leftValue) / (2.0 + options.leftValue));
work[0] =
6.0 *
((1.0 + options.leftValue) / (2.0 + options.leftValue)) *
((y[1] - y[0]) / (x[1] - x[0]));
} else {
model.coefficients[4 * 0 + 2] = 0;
work[0] = 0;
}
break;
case BoundaryCondition.DEFAULT:
default:
// desired slope at leftmost point is derivative from two points
model.coefficients[4 * 0 + 1] = 1.0;
model.coefficients[4 * 0 + 2] = 0.0;
work[0] = y[2] - y[0];
break;
}
for (let k = 1; k < N; k++) {
const xlk = x[k] - x[k - 1];
const xlkp = x[k + 1] - x[k];
model.coefficients[4 * k + 0] = xlkp;
model.coefficients[4 * k + 1] = 2 * (xlkp + xlk);
model.coefficients[4 * k + 2] = xlk;
work[k] =
3.0 *
((xlkp * (y[k] - y[k - 1])) / xlk + (xlk * (y[k + 1] - y[k])) / xlkp);
}
// develop constraint at rightmost point.
switch (options.rightConstraint) {
case BoundaryCondition.DERIVATIVE:
// desired slope at rightmost point is rightValue
model.coefficients[4 * N + 0] = 0.0;
model.coefficients[4 * N + 1] = 1.0;
work[N] = options.rightValue;
break;
case BoundaryCondition.SECOND_DERIVATIVE:
// desired second derivative at rightmost point is rightValue.
model.coefficients[4 * N + 0] = 1.0;
model.coefficients[4 * N + 1] = 2.0;
work[N] =
3.0 * ((y[N] - y[N - 1]) / (x[N] - x[N - 1])) +
0.5 * (x[N] - x[N - 1]) * options.rightValue;
break;
case BoundaryCondition.SECOND_DERIVATIVE_INTERIOR_POINT:
// desired second derivative at rightmost point is
// rightValue times second derivative at last interior point.
model.coefficients[4 * N + 1] = 2.0;
if (Math.abs(options.rightValue + 2) > VTK_EPSILON) {
model.coefficients[4 * N + 0] =
4.0 * ((0.5 + options.rightValue) / (2.0 + options.rightValue));
work[N] =
6.0 *
((1.0 + options.rightValue) / (2.0 + options.rightValue)) *
((y[N] - y[size - 2]) / (x[N] - x[size - 2]));
} else {
model.coefficients[4 * N + 0] = 0;
work[N] = 0;
}
break;
case BoundaryCondition.DEFAULT:
default:
// desired slope at rightmost point is derivative from two points
model.coefficients[4 * N + 0] = 0.0;
model.coefficients[4 * N + 1] = 1.0;
work[N] = y[N] - y[N - 2];
break;
}
// solve resulting set of equations.
model.coefficients[4 * 0 + 2] /= model.coefficients[4 * 0 + 1];
work[0] /= model.coefficients[4 * N + 1];
model.coefficients[4 * N + 3] = 1;
for (let k = 1; k <= N; k++) {
model.coefficients[4 * k + 1] -=
model.coefficients[4 * k + 0] * model.coefficients[4 * (k - 1) + 2];
model.coefficients[4 * k + 2] /= model.coefficients[4 * k + 1];
work[k] =
(work[k] - model.coefficients[4 * k + 0] * work[k - 1]) /
model.coefficients[4 * k + 1];
}
for (let k = N - 1; k >= 0; k--) {
work[k] -= model.coefficients[4 * k + 2] * work[k + 1];
}
// the column vector work now contains the first
// derivative of the spline function at each joint.
// compute the coefficients of the cubic between
// each pair of joints.
for (let k = 0; k < N; k++) {
const b = x[k + 1] - x[k];
model.coefficients[4 * k + 0] = y[k];
model.coefficients[4 * k + 1] = work[k];
model.coefficients[4 * k + 2] =
(3 * (y[k + 1] - y[k])) / (b * b) - (work[k + 1] + 2 * work[k]) / b;
model.coefficients[4 * k + 3] =
(2 * (y[k] - y[k + 1])) / (b * b * b) +
(work[k + 1] + work[k]) / (b * b);
}
// the coefficients of a fictitious nth cubic
// are the same as the coefficients in the first interval
model.coefficients[4 * N + 0] = y[N];
model.coefficients[4 * N + 1] = work[N];
model.coefficients[4 * N + 2] = model.coefficients[4 * 0 + 2];
model.coefficients[4 * N + 3] = model.coefficients[4 * 0 + 3];
};
// --------------------------------------------------------------------------
publicAPI.getValue = (intervalIndex, t) => {
const t2 = t * t;
const t3 = t * t * t;
return (
model.coefficients[4 * intervalIndex + 3] * t3 +
model.coefficients[4 * intervalIndex + 2] * t2 +
model.coefficients[4 * intervalIndex + 1] * t +
model.coefficients[4 * intervalIndex + 0]
);
};
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
vtkSpline1D.extend(publicAPI, model, initialValues);
// Build VTK API
macro.obj(publicAPI, model);
vtkCardinalSpline1D(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = macro.newInstance(extend, 'vtkCardinalSpline1D');
// ----------------------------------------------------------------------------
export default { newInstance, extend };