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
|
double goblinInterpolate(double const p[2][2][2], double const x, double const y, double const z)
{
double const c00 ( x*(p[1][0][0]-p[0][0][0]) + p[0][0][0] );
double const c10 ( x*(p[1][1][0]-p[0][1][0]) + p[0][1][0] );
double const c11 ( x*(p[1][1][1]-p[0][1][1]) + p[0][1][1] );
double const c01 ( x*(p[1][0][1]-p[0][0][1]) + p[0][0][1] );
double const c0 ( y*(c10-c00) + c00 );
double const c1 ( y*(c11-c01) + c01 );
return z*(c1-c0) + c0;
}
double linearInterpolate(double const p[2], double const x)
{
return x*(p[1] - p[0]) + p[0];
}
double bilinearInterpolate(double const p[2][2], double const x, double const y)
{
double const arr[2] = { linearInterpolate(p[0], y), linearInterpolate(p[1], y) };
return linearInterpolate(arr, x);
}
double trilinearInterpolate(double const p[2][2][2], double const x, double const y, double const z)
{
double const arr[2] = { bilinearInterpolate(p[0], y, z), bilinearInterpolate(p[1], y, z) };
return linearInterpolate(arr, x);
}
int main()
{
double p[2][2][2] = { { {0, 1}, {1, 2} }, { {0, 0}, {1, 2} } };
double const first = trilinearInterpolate(p, 0.5, 0.5, 0.5);
double const second = goblinInterpolate(p, 0.5, 0.5, 0.5);
assert(first == second);
} |