Ticket #4218: interpolationSplit2016_9_16.diff

File interpolationSplit2016_9_16.diff, 3.6 KB (added by FeXoR, 8 years ago)

Splits 1D and 2D interpolation, fixes x/y swaping

  • binaries/data/mods/public/globalscripts/BicubicInterpolation.js

     
    11/**
    2  * From https://github.com/hughsk/bicubic licensed under MIT 2013 Hugh Kennedy - see /license_mit.txt
     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 */
     8function 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/**
    317 * Two dimensional interpolation within a square grid using polynomials of 3rd degree.
    418 * @param {float} xf - X coordinate within the subgrid (see below) of the point to interpolate the value for
    519 * @param {float} yf - y coordinate within the subgrid (see below) of the point to interpolate the value for
    620 * @param {float} p00 - Value of the property to calculate at the origin of the naighbor grid
    721 * @param {float} pxy - Value at naighbor grid coordinates x/y, x/y in {0, 1, 2, 3}
    822 * @return {float} - Value at the given float coordinates in the small grid interpolated from it's values
    9   */
     23 */
    1024function bicubicInterpolation(
    1125    xf, yf,
    1226    p00, p01, p02, p03,
     
    1529    p30, p31, p32, p33
    1630)
    1731{
    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;
     32    let x0 = cubicInterpolation(yf, p00, p01, p02, p03);
     33    let x1 = cubicInterpolation(yf, p10, p11, p12, p13);
     34    let x2 = cubicInterpolation(yf, p20, p21, p22, p23);
     35    let x3 = cubicInterpolation(yf, p30, p31, p32, p33);
     36    return cubicInterpolation(xf, x0, x1, x2, x3);
    4737}
  • binaries/data/mods/public/maps/random/rmgen2/gaia.js

     
    13041304                shift.y = - 1;
    13051305
    13061306            let neighbors = [];
    1307             for (let localYi = 0; localYi < 4; ++localYi)
    1308                 for (let localXi = 0; localXi < 4; ++localXi)
     1307            for (let localXi = 0; localXi < 4; ++localXi)
     1308                for (let localYi = 0; localYi < 4; ++localYi)
    13091309                    neighbors.push(heightmap[(hmTile.x + localXi + shift.x - 1) * hmSize + (hmTile.y + localYi + shift.y - 1)]);
    13101310
    13111311            setHeight(x, y, bicubicInterpolation(hmPoint.x - hmTile.x - shift.x, hmPoint.y - hmTile.y - shift.y, ...neighbors) / scale);