Split the 1D and 2D interpolation into 2 Functions. Renamed BicubicInterpolation.js to interpolation.js. Original patch by Vladislav. Reviewed by fatherbushido. Fixes #4218

This was SVN commit r18925.
This commit is contained in:
FeXoR 2016-11-13 17:34:08 +00:00
parent 2ec3f3ebaa
commit 93aefe0787
4 changed files with 43 additions and 52 deletions

View File

@ -79,6 +79,3 @@ in particular, let us know and we can try to clarify it.
/source/tools/atlas
GPL version 2 (or later) - see license_gpl-2.0.txt
/binaries/data/mods/public/globalscripts/BicubicInterpolation.js
MIT 2013 Hugh Kennedy - see license_mit.txt

View File

@ -1,47 +0,0 @@
/**
* From https://github.com/hughsk/bicubic licensed under MIT 2013 Hugh Kennedy - see /license_mit.txt
* Two dimensional interpolation within a square grid using polynomials of 3rd degree.
* @param {float} xf - X coordinate within the subgrid (see below) of the point to interpolate the value for
* @param {float} yf - y coordinate within the subgrid (see below) of the point to interpolate the value for
* @param {float} p00 - Value of the property to calculate at the origin of the naighbor grid
* @param {float} pxy - Value at naighbor grid coordinates x/y, x/y in {0, 1, 2, 3}
* @return {float} - Value at the given float coordinates in the small grid interpolated from it's values
*/
function bicubicInterpolation(
xf, yf,
p00, p01, p02, p03,
p10, p11, p12, p13,
p20, p21, p22, p23,
p30, p31, p32, p33
)
{
var yf2 = yf * yf;
var xf2 = xf * xf;
var xf3 = xf * xf2;
var x00 = p03 - p02 - p00 + p01;
var x01 = p00 - p01 - x00;
var x02 = p02 - p00;
var x0 = x00*xf3 + x01*xf2 + x02*xf + p01;
var x10 = p13 - p12 - p10 + p11;
var x11 = p10 - p11 - x10;
var x12 = p12 - p10;
var x1 = x10*xf3 + x11*xf2 + x12*xf + p11;
var x20 = p23 - p22 - p20 + p21;
var x21 = p20 - p21 - x20;
var x22 = p22 - p20;
var x2 = x20*xf3 + x21*xf2 + x22*xf + p21;
var x30 = p33 - p32 - p30 + p31;
var x31 = p30 - p31 - x30;
var x32 = p32 - p30;
var x3 = x30*xf3 + x31*xf2 + x32*xf + p31;
var y0 = x3 - x2 - x0 + x1;
var y1 = x0 - x1 - y0;
var y2 = x2 - x0;
return y0*yf*yf2 + y1*yf2 + y2*yf + x1;
}

View File

@ -0,0 +1,41 @@
/**
* One dimensional interpolation within four uniformly spaced points using polynomials of 3rd degree.
* A uniform CatmullRom spline implementation.
* @param {float} xf - Float coordinate relative to p1 to interpolate the property for
* @param {float} p0 - Value of the property to calculate at the origin of four point scale
* @param {float} px - Value at the property of the corresponding point on the scale
* @return {float} - Value at the given float position in the 4-point scale interpolated from it's property values
*/
function cubicInterpolation(xf, p0, p1, p2, p3)
{
return p1 +
(-0.5 * p0 + 0.5 * p2) * xf +
(p0 - 2.5 * p1 + 2.0 * p2 - 0.5 * p3) * xf * xf +
(-0.5 * p0 + 1.5 * p1 - 1.5 * p2 + 0.5 * p3) * xf * xf * xf;
}
/**
* Two dimensional interpolation within a square grid using polynomials of 3rd degree.
* @param {float} xf - X coordinate relative to p1y of the point to interpolate the value for
* @param {float} yf - Y coordinate relative to px1 of the point to interpolate the value for
* @param {float} p00 - Value of the property to calculate at the origin of the neighbor grid
* @param {float} pxy - Value at neighbor grid coordinates x/y, x/y in {0, 1, 2, 3}
* @return {float} - Value at the given float coordinates in the small grid interpolated from it's values
*/
function bicubicInterpolation
(
xf, yf,
p00, p01, p02, p03,
p10, p11, p12, p13,
p20, p21, p22, p23,
p30, p31, p32, p33
)
{
return cubicInterpolation(
xf,
cubicInterpolation(yf, p00, p01, p02, p03),
cubicInterpolation(yf, p10, p11, p12, p13),
cubicInterpolation(yf, p20, p21, p22, p23),
cubicInterpolation(yf, p30, p31, p32, p33)
);
}

View File

@ -1304,8 +1304,8 @@ function paintHeightmap(heightmap, tilemap, pallet, func = undefined)
shift.y = - 1;
let neighbors = [];
for (let localYi = 0; localYi < 4; ++localYi)
for (let localXi = 0; localXi < 4; ++localXi)
for (let localXi = 0; localXi < 4; ++localXi)
for (let localYi = 0; localYi < 4; ++localYi)
neighbors.push(heightmap[(hmTile.x + localXi + shift.x - 1) * hmSize + (hmTile.y + localYi + shift.y - 1)]);
setHeight(x, y, bicubicInterpolation(hmPoint.x - hmTile.x - shift.x, hmPoint.y - hmTile.y - shift.y, ...neighbors) / scale);