HighMap library (C++)
Loading...
Searching...
No Matches
interpolate2d.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
18#pragma once
19#include <cstddef>
20#include <map>
21#include <vector>
22
23extern "C" // order matters
24{
25#include "config.h"
26//
27#include "nn.h"
28//
29#include "nncommon.h"
30//
31#include "delaunay.h"
32}
33
34#include "highmap/array.hpp"
35
36namespace hmap
37{
38
55
57{
58public:
61
62 void build(const std::vector<float> &xin, const std::vector<float> &yin);
63
64 void setup_output_points(const std::vector<float> &x,
65 const std::vector<float> &y);
66
67 void interpolate(const std::vector<float> &values_in,
68 std::vector<float> &values_out) const;
69
70private:
71 nnai *handle = nullptr;
72 delaunay *d = nullptr;
73 std::vector<double> xout;
74 std::vector<double> yout;
75 size_t nout = 0;
76};
77
94inline float bilinear_interp(float f00,
95 float f10,
96 float f01,
97 float f11,
98 float u,
99 float v)
100{
101 float a10 = f10 - f00;
102 float a01 = f01 - f00;
103 float a11 = f11 - f10 - f01 + f00;
104 return f00 + a10 * u + a01 * v + a11 * u * v;
105}
106
107inline float cubic_interpolate(float p[4], float x)
108{
109 return p[1] + 0.5 * x *
110 (p[2] - p[0] +
111 x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] +
112 x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
113}
114
148Array harmonic_interpolation(const Array &array,
149 const Array &mask_fixed_values,
150 int iterations_max = 500,
151 float tolerance = 1e-5f,
152 float omega = 1.8f);
153
181Array interpolate2d(glm::ivec2 shape,
182 const std::vector<float> &x,
183 const std::vector<float> &y,
184 const std::vector<float> &values,
185 InterpolationMethod2D interpolation_method,
186 const Array *p_noise_x = nullptr,
187 const Array *p_noise_y = nullptr,
188 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
189
208Array interpolate2d_delaunay(glm::ivec2 shape,
209 const std::vector<float> &x,
210 const std::vector<float> &y,
211 const std::vector<float> &values,
212 const Array *p_noise_x = nullptr,
213 const Array *p_noise_y = nullptr,
214 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
215 float fill_value = 0.f);
216
221Array interpolate2d_delaunay_gradient(glm::ivec2 shape,
222 const std::vector<float> &x,
223 const std::vector<float> &y,
224 const std::vector<float> &values,
225 const Array *p_noise_x = nullptr,
226 const Array *p_noise_y = nullptr,
227 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
228 float fill_value = 0.f,
229 float gradient_scaling = 1.f);
230
234Array interpolate2d_gaussian(glm::ivec2 shape,
235 const std::vector<float> &x,
236 const std::vector<float> &y,
237 const std::vector<float> &values,
238 const Array *p_noise_x = nullptr,
239 const Array *p_noise_y = nullptr,
240 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
241 float sigma = 0.05f,
242 float radius = 0.f);
243
247Array interpolate2d_idw(glm::ivec2 shape,
248 const std::vector<float> &x,
249 const std::vector<float> &y,
250 const std::vector<float> &values,
251 const Array *p_noise_x = nullptr,
252 const Array *p_noise_y = nullptr,
253 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
254 float distance_exp = 2.f,
255 float radius = 0.f);
256
274Array interpolate2d_nearest(glm::ivec2 shape,
275 const std::vector<float> &x,
276 const std::vector<float> &y,
277 const std::vector<float> &values,
278 const Array *p_noise_x = nullptr,
279 const Array *p_noise_y = nullptr,
280 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
281
285Array interpolate2d_nni(glm::ivec2 shape,
286 const std::vector<float> &x,
287 const std::vector<float> &y,
288 const std::vector<float> &values,
289 const Array *p_noise_x = nullptr,
290 const Array *p_noise_y = nullptr,
291 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
292
293} // namespace hmap
294
295namespace hmap::gpu
296{
297
299Array harmonic_interpolation(const Array &array,
300 const Array &mask_fixed_values,
301 int iterations_max = 500);
302
303} // namespace hmap::gpu
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Definition interpolate2d.hpp:57
void build(const std::vector< float > &xin, const std::vector< float > &yin)
Definition natural_neighbor_interpolator.cpp:21
void interpolate(const std::vector< float > &values_in, std::vector< float > &values_out) const
Definition natural_neighbor_interpolator.cpp:42
~NaturalNeighborInterpolator()
Definition natural_neighbor_interpolator.cpp:15
void setup_output_points(const std::vector< float > &x, const std::vector< float > &y)
Definition natural_neighbor_interpolator.cpp:62
Definition blending.hpp:186
Definition algebra.hpp:23
Array interpolate2d_gaussian(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, float sigma=0.05f, float radius=0.f)
2D interpolation using the Gaussian kernel method.
Definition interpolate2d_gaussian.cpp:17
Array interpolate2d_idw(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, float distance_exp=2.f, float radius=0.f)
2D interpolation using the IDW method.
Definition interpolate2d_idw.cpp:18
Array interpolate2d_nearest(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
2D interpolation using the nearest neighbor method.
Definition interpolate2d_nearest.cpp:14
InterpolationMethod2D
Enumeration of 2D interpolation methods.
Definition interpolate2d.hpp:47
@ ITP2D_IDW
Inverse Distance Weighting.
Definition interpolate2d.hpp:50
@ ITP2D_DELAUNAY
Delaunay triangulation method for 2D interpolation.
Definition interpolate2d.hpp:48
@ ITP2D_NNI
Natural Neighbor Interpolation.
Definition interpolate2d.hpp:52
@ ITP2D_DELAUNAY_GRADIENT
Delaunay triangulation + linear gradient.
Definition interpolate2d.hpp:53
@ ITP2D_GAUSSIAN
Gaussian Distance Weighting.
Definition interpolate2d.hpp:51
@ ITP2D_NEAREST
Nearest point method for 2D interpolation.
Definition interpolate2d.hpp:49
Array harmonic_interpolation(const Array &array, const Array &mask_fixed_values, int iterations_max=500, float tolerance=1e-5f, float omega=1.8f)
Perform harmonic interpolation on a 2D array using the Successive Over-Relaxation (SOR) method.
Definition harmonic_interpolation.cpp:12
Array interpolate2d_delaunay_gradient(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, float fill_value=0.f, float gradient_scaling=1.f)
2D interpolation using a smoother version of the Delaunay triangulation method.
Definition interpolate2d_delaunay_gradient.cpp:13
Array interpolate2d_nni(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
2D interpolation using the Natural Neighbor Interpolation method.
Definition interpolate2d_nni.cpp:15
Array interpolate2d_delaunay(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f}, float fill_value=0.f)
2D interpolation using the Delaunay triangulation method.
Definition interpolate2d_delaunay.cpp:13
float bilinear_interp(float f00, float f10, float f01, float f11, float u, float v)
Compute the bilinear interpolated value from four input values.
Definition interpolate2d.hpp:94
Array interpolate2d(glm::ivec2 shape, const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &values, InterpolationMethod2D interpolation_method, const Array *p_noise_x=nullptr, const Array *p_noise_y=nullptr, glm::vec4 bbox={0.f, 1.f, 0.f, 1.f})
Generic 2D interpolation function.
Definition interpolate2d.cpp:13
float cubic_interpolate(float p[4], float x)
Definition interpolate2d.hpp:107