Skip to content

Commit 1265fdb

Browse files
author
Vincent van der Wal
committed
add arrows for wind chart
1 parent 303d9a2 commit 1265fdb

File tree

2 files changed

+188
-80
lines changed

2 files changed

+188
-80
lines changed

src/utils/math.ts

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ export const tileToBBOX = (tile: [x: number, y: number, z: number]) => {
1717
return [w, s, e, n];
1818
};
1919

20-
// const interpolateLinear = (
21-
// data: Float32Array<ArrayBufferLike>,
22-
// index: number,
23-
// xFraction: number,
24-
// yFraction: number,
25-
// nx: number
26-
// ): number => {
27-
// const p0 = data[index];
28-
// const p1 = data[index + 1];
29-
// const p2 = data[index + nx];
30-
// const p3 = data[index + 1 + nx];
31-
// return (
32-
// p0 * (1 - xFraction) * (1 - yFraction) +
33-
// p1 * xFraction * (1 - yFraction) +
34-
// p2 * (1 - xFraction) * yFraction +
35-
// p3 * xFraction * yFraction
36-
// );
37-
// };
20+
export const interpolateLinear = (
21+
values: TypedArray,
22+
nx: number,
23+
index: number,
24+
xFraction: number,
25+
yFraction: number
26+
): number => {
27+
const p0 = values[index];
28+
const p1 = values[index + 1];
29+
const p2 = values[index + nx];
30+
const p3 = values[index + 1 + nx];
31+
return (
32+
p0 * (1 - xFraction) * (1 - yFraction) +
33+
p1 * xFraction * (1 - yFraction) +
34+
p2 * (1 - xFraction) * yFraction +
35+
p3 * xFraction * yFraction
36+
);
37+
};
3838

3939
export const hermite = (t: number, p0: number, p1: number, m0: number, m1: number) => {
4040
const t2 = t * t;
@@ -53,7 +53,7 @@ export const getDerivative = (fPrev: number, fNext: number) => {
5353
};
5454

5555
export const interpolate2DHermite = (
56-
data: TypedArray,
56+
values: TypedArray,
5757
nx: number,
5858
index: number,
5959
xFraction: number,
@@ -75,29 +75,29 @@ export const interpolate2DHermite = (
7575
}
7676
// tension = 0 is Hermite with Catmull-Rom. Tension = 1 is bilinear interpolation
7777
// 0.5 is somewhat in the middle
78-
return interpolateCardinal2D(data, nx, index, xFraction, yFraction, 0.3);
79-
//return interpolateRBF3x3(data, nx, index, xFraction, yFraction)
80-
//return interpolateRBF4x4(data, nx, index, xFraction, yFraction)
81-
//return interpolateSmoothBilinear(data, index, xFraction, yFraction, nx)
82-
//return interpolateMonotonicHermite(data, nx, index, xFraction, yFraction)
83-
//return interpolateGaussianBilinear(data, index, xFraction, yFraction, nx)
84-
//return interpolateLinear(data, index, xFraction, yFraction, nx)
85-
//return quinticHermite2D(data, nx, index, xFraction, yFraction)
78+
return interpolateCardinal2D(values, nx, index, xFraction, yFraction, 0.3);
79+
//return interpolateRBF3x3(values, nx, index, xFraction, yFraction)
80+
//return interpolateRBF4x4(values, nx, index, xFraction, yFraction)
81+
//return interpolateSmoothBilinear(values, index, xFraction, yFraction, nx)
82+
//return interpolateMonotonicHermite(values, nx, index, xFraction, yFraction)
83+
//return interpolateGaussianBilinear(values, index, xFraction, yFraction, nx)
84+
//return interpolateLinear(values, index, xFraction, yFraction, nx)
85+
//return quinticHermite2D(values, nx, index, xFraction, yFraction)
8686

8787
/*let x = index % nx;
8888
let y = index / nx;
89-
let ny = data.length / nx;
89+
let ny = values.length / nx;
9090
if (x <= 1 || y <= 1 || x >= nx - 3 || y >= ny - 3) {
91-
return interpolateLinear(data, index, xFraction, yFraction, nx);
91+
return interpolateLinear(values, index, xFraction, yFraction, nx);
9292
}
9393
9494
// Interpolate along X for each of the 4 rows
9595
const interpRow = [];
9696
for (let j = -1; j < 3; j++) {
97-
const p0 = data[index + j * nx];
98-
const p1 = data[index + j * nx + 1];
99-
const m0 = getDerivative(data[index + j * nx - 1], data[index + j * nx + 1]);
100-
const m1 = getDerivative(data[index + j * nx + 0], data[index + j * nx + 2]);
97+
const p0 = values[index + j * nx];
98+
const p1 = values[index + j * nx + 1];
99+
const m0 = getDerivative(values[index + j * nx - 1], values[index + j * nx + 1]);
100+
const m1 = getDerivative(values[index + j * nx + 0], values[index + j * nx + 2]);
101101
interpRow[j + 1] = hermite(xFraction, p0, p1, m0, m1);
102102
}
103103
@@ -146,7 +146,7 @@ export const secondDerivative = (fm1: number, f0: number, fp1: number): number =
146146

147147
// 2D Quintic Hermite Interpolation on a 6x6 or larger grid
148148
export const quinticHermite2D = (
149-
data: Float32Array<ArrayBufferLike>,
149+
values: Float32Array<ArrayBufferLike>,
150150
nx: number,
151151
index: number,
152152
xFraction: number,
@@ -156,12 +156,12 @@ export const quinticHermite2D = (
156156
const colValues = [];
157157

158158
for (let j = -2; j <= 3; j++) {
159-
const f0 = data[index + j * nx];
160-
const f1 = data[index + j * nx + 1];
161-
const m0 = derivative(data[index + j * nx - 1], data[index + j * nx + 1]);
162-
const m1 = derivative(data[index + j * nx], data[index + j * nx + 2]);
163-
const c0 = secondDerivative(data[index + j * nx - 1], f0, f1);
164-
const c1 = secondDerivative(f0, f1, data[index + j * nx + 2]);
159+
const f0 = values[index + j * nx];
160+
const f1 = values[index + j * nx + 1];
161+
const m0 = derivative(values[index + j * nx - 1], values[index + j * nx + 1]);
162+
const m1 = derivative(values[index + j * nx], values[index + j * nx + 2]);
163+
const c0 = secondDerivative(values[index + j * nx - 1], f0, f1);
164+
const c1 = secondDerivative(f0, f1, values[index + j * nx + 2]);
165165

166166
const interpolatedX = quinticHermite(xFraction, f0, f1, m0, m1, c0, c1);
167167
colValues.push(interpolatedX);
@@ -222,7 +222,7 @@ const cardinalSpline = (
222222
};
223223

224224
const interpolateCardinal2D = (
225-
data: TypedArray,
225+
values: TypedArray,
226226
nx: number,
227227
index: number,
228228
xFraction: number,
@@ -232,34 +232,34 @@ const interpolateCardinal2D = (
232232
// Interpolate 4 rows in X
233233
const r0 = cardinalSpline(
234234
xFraction,
235-
data[index + -1 * nx - 1],
236-
data[index + -1 * nx + 0],
237-
data[index + -1 * nx + 1],
238-
data[index + -1 * nx + 2],
235+
values[index + -1 * nx - 1],
236+
values[index + -1 * nx + 0],
237+
values[index + -1 * nx + 1],
238+
values[index + -1 * nx + 2],
239239
tension
240240
);
241241
const r1 = cardinalSpline(
242242
xFraction,
243-
data[index + +0 * nx - 1],
244-
data[index + +0 * nx + 0],
245-
data[index + +0 * nx + 1],
246-
data[index + +0 * nx + 2],
243+
values[index + +0 * nx - 1],
244+
values[index + +0 * nx + 0],
245+
values[index + +0 * nx + 1],
246+
values[index + +0 * nx + 2],
247247
tension
248248
);
249249
const r2 = cardinalSpline(
250250
xFraction,
251-
data[index + +1 * nx - 1],
252-
data[index + +1 * nx + 0],
253-
data[index + +1 * nx + 1],
254-
data[index + +1 * nx + 2],
251+
values[index + +1 * nx - 1],
252+
values[index + +1 * nx + 0],
253+
values[index + +1 * nx + 1],
254+
values[index + +1 * nx + 2],
255255
tension
256256
);
257257
const r3 = cardinalSpline(
258258
xFraction,
259-
data[index + +2 * nx - 1],
260-
data[index + +2 * nx + 0],
261-
data[index + +2 * nx + 1],
262-
data[index + +2 * nx + 2],
259+
values[index + +2 * nx - 1],
260+
values[index + +2 * nx + 0],
261+
values[index + +2 * nx + 1],
262+
values[index + +2 * nx + 2],
263263
tension
264264
);
265265

@@ -276,7 +276,7 @@ export const radiansToDegrees = (rad: number) => {
276276
};
277277

278278
// const interpolateRBF4x4 = (
279-
// data: Float32Array | (Float32Array & ArrayBufferLike),
279+
// values: Float32Array | (Float32Array & ArrayBufferLike),
280280
// nx: number,
281281
// index: number,
282282
// xFraction: number,
@@ -285,7 +285,7 @@ export const radiansToDegrees = (rad: number) => {
285285
// const sigma = 0.65;
286286
// const denom = 2 * sigma * sigma;
287287

288-
// const ny = data.length / nx;
288+
// const ny = values.length / nx;
289289
// const x = (index % nx) + xFraction;
290290
// const y = Math.floor(index / nx) + yFraction;
291291

@@ -308,7 +308,7 @@ export const radiansToDegrees = (rad: number) => {
308308
// const weight = Math.exp(-dist2 / denom);
309309

310310
// const sampleIndex = py * nx + px;
311-
// const value = data[sampleIndex];
311+
// const value = values[sampleIndex];
312312

313313
// sum += value * weight;
314314
// weightSum += weight;
@@ -319,7 +319,7 @@ export const radiansToDegrees = (rad: number) => {
319319
// };
320320

321321
// const interpolateRBF3x3 = (
322-
// data: Float32Array | (Float32Array & ArrayBufferLike),
322+
// values: Float32Array | (Float32Array & ArrayBufferLike),
323323
// nx: number,
324324
// index: number,
325325
// xFraction: number,
@@ -328,7 +328,7 @@ export const radiansToDegrees = (rad: number) => {
328328
// const sigma = 0.4;
329329
// const denom = 2 * sigma * sigma;
330330

331-
// const ny = data.length / nx;
331+
// const ny = values.length / nx;
332332
// const x = (index % nx) + xFraction;
333333
// const y = Math.floor(index / nx) + yFraction;
334334

@@ -351,7 +351,7 @@ export const radiansToDegrees = (rad: number) => {
351351
// const weight = Math.exp(-dist2 / denom);
352352

353353
// const sampleIndex = py * nx + px;
354-
// const value = data[sampleIndex];
354+
// const value = values[sampleIndex];
355355

356356
// sum += value * weight;
357357
// weightSum += weight;

0 commit comments

Comments
 (0)