1 | | /** |
2 | | * From https://github.com/hughsk/bicubic licensed under MIT 2013 Hugh Kennedy - see /license_mit.txt |
3 | | * Two dimensional interpolation within a square grid using polynomials of 3rd degree. |
4 | | * @param {float} xf - X coordinate within the subgrid (see below) of the point to interpolate the value for |
5 | | * @param {float} yf - y coordinate within the subgrid (see below) of the point to interpolate the value for |
6 | | * @param {float} p00 - Value of the property to calculate at the origin of the naighbor grid |
7 | | * @param {float} pxy - Value at naighbor grid coordinates x/y, x/y in {0, 1, 2, 3} |
8 | | * @return {float} - Value at the given float coordinates in the small grid interpolated from it's values |
9 | | */ |
10 | | function bicubicInterpolation( |
11 | | xf, yf, |
12 | | p00, p01, p02, p03, |
13 | | p10, p11, p12, p13, |
14 | | p20, p21, p22, p23, |
15 | | p30, p31, p32, p33 |
16 | | ) |
17 | | { |
18 | | var yf2 = yf * yf; |
19 | | var xf2 = xf * xf; |
20 | | var xf3 = xf * xf2; |
21 | | |
22 | | var x00 = p03 - p02 - p00 + p01; |
23 | | var x01 = p00 - p01 - x00; |
24 | | var x02 = p02 - p00; |
25 | | var x0 = x00*xf3 + x01*xf2 + x02*xf + p01; |
26 | | |
27 | | var x10 = p13 - p12 - p10 + p11; |
28 | | var x11 = p10 - p11 - x10; |
29 | | var x12 = p12 - p10; |
30 | | var x1 = x10*xf3 + x11*xf2 + x12*xf + p11; |
31 | | |
32 | | var x20 = p23 - p22 - p20 + p21; |
33 | | var x21 = p20 - p21 - x20; |
34 | | var x22 = p22 - p20; |
35 | | var x2 = x20*xf3 + x21*xf2 + x22*xf + p21; |
36 | | |
37 | | var x30 = p33 - p32 - p30 + p31; |
38 | | var x31 = p30 - p31 - x30; |
39 | | var x32 = p32 - p30; |
40 | | var x3 = x30*xf3 + x31*xf2 + x32*xf + p31; |
41 | | |
42 | | var y0 = x3 - x2 - x0 + x1; |
43 | | var y1 = x0 - x1 - y0; |
44 | | var y2 = x2 - x0; |
45 | | |
46 | | return y0*yf*yf2 + y1*yf2 + y2*yf + x1; |
47 | | } |
| 1 | /** |
| 2 | * Two dimensional interpolation within four equidistant points using polynomials of 3rd degree. |
| 3 | * @param {float} xf - Float value between the points to interpolate the property for |
| 4 | * @param {float} p0 - Value of the property to calculate at the origin of four point scale |
| 5 | * @param {float} px - Value at the property on the points |
| 6 | * @return {float} - Value at the given float position in the 4-point scale interpolated from it's property values |
| 7 | */ |
| 8 | function cubicInterpolation(xf, p0, p1, p2, p3) |
| 9 | { |
| 10 | return p1 |
| 11 | + (-0.5 * p0 + 0.5 * p2) * xf |
| 12 | + (p0 - 2.5 * p1 + 2.0 * p2 - 0.5 * p3) * xf * xf |
| 13 | + (-0.5 * p0 + 1.5 * p1 - 1.5 * p2 + 0.5 * p3) * xf * xf * xf; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Two dimensional interpolation within a square grid using polynomials of 3rd degree. |
| 18 | * @param {float} xf - X coordinate within the subgrid (see below) of the point to interpolate the value for |
| 19 | * @param {float} yf - y coordinate within the subgrid (see below) of the point to interpolate the value for |
| 20 | * @param {float} p00 - Value of the property to calculate at the origin of the naighbor grid |
| 21 | * @param {float} pxy - Value at naighbor grid coordinates x/y, x/y in {0, 1, 2, 3} |
| 22 | * @return {float} - Value at the given float coordinates in the small grid interpolated from it's values |
| 23 | */ |
| 24 | function bicubicInterpolation |
| 25 | ( |
| 26 | xf, yf, |
| 27 | p00, p01, p02, p03, |
| 28 | p10, p11, p12, p13, |
| 29 | p20, p21, p22, p23, |
| 30 | p30, p31, p32, p33 |
| 31 | ) |
| 32 | { |
| 33 | return cubicInterpolation( |
| 34 | xf, |
| 35 | cubicInterpolation(yf, p00, p01, p02, p03), |
| 36 | cubicInterpolation(yf, p10, p11, p12, p13), |
| 37 | cubicInterpolation(yf, p20, p21, p22, p23), |
| 38 | cubicInterpolation(yf, p30, p31, p32, p33) |
| 39 | ); |
| 40 | } |