From 99d95334532ff5afc65c586e1594226931cf883f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 28 Oct 2024 01:38:34 +0300 Subject: [PATCH] reimplement bicubic interpolation --- src/maths/Heightmap.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/maths/Heightmap.cpp b/src/maths/Heightmap.cpp index 8db33f6b..99c27098 100644 --- a/src/maths/Heightmap.cpp +++ b/src/maths/Heightmap.cpp @@ -5,10 +5,6 @@ #include #include -static inline float smootherstep(float x) { - return glm::smoothstep(std::floor(x), std::floor(x)+1, x); -} - static inline float sample_at( const float* buffer, uint width, @@ -17,6 +13,27 @@ static inline float sample_at( return buffer[y*width+x]; } +static inline float sample_at( + const float* buffer, + uint width, uint height, + uint x, uint y +) { + return buffer[(y >= height ? height-1 : y)*width+(x >= width ? width-1 : x)]; +} + +static inline float interpolate_cubic(float p[4], float x) { + return p[1] + 0.5 * x*(p[2] - p[0] + x*(2.0*p[0] - 5.0*p[1] + 4.0*p[2] - + p[3] + x*(3.0*(p[1] - p[2]) + p[3] - p[0]))); +} + +static inline float interpolate_bicubic(float p[4][4], float x, float y) { + float q[4]; + for (int i = 0; i < 4; i++) { + q[i] = interpolate_cubic(p[i], y); + } + return interpolate_cubic(q, x); +} + static inline float sample_at( const float* buffer, uint width, uint height, @@ -50,7 +67,17 @@ static inline float sample_at( return a00 + a10*tx + a01*ty + a11*tx*ty; } - // TODO: implement CUBIC (Bicubic) interpolation + case InterpolationType::CUBIC: { + float p[4][4]; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + p[i][j] = sample_at( + buffer, width, height, ix + j - 1, iy + i - 1 + ); + } + } + return interpolate_bicubic(p, ty, tx); + } default: throw std::runtime_error("interpolation type is not implemented"); }