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 <map>
20
21extern "C" // order matters
22{
23#include "config.h"
24//
25#include "nn.h"
26//
27#include "nncommon.h"
28//
29#include "delaunay.h"
30}
31
32#include "highmap/array.hpp"
33
34namespace hmap
35{
36
53
55{
56public:
59
60 void build(const std::vector<float> &xin, const std::vector<float> &yin);
61
62 void setup_output_points(const std::vector<float> &x,
63 const std::vector<float> &y);
64
65 void interpolate(const std::vector<float> &values_in,
66 std::vector<float> &values_out) const;
67
68private:
69 nnai *handle = nullptr;
70 delaunay *d = nullptr;
71 std::vector<double> xout;
72 std::vector<double> yout;
73 size_t nout = 0;
74};
75
92inline float bilinear_interp(float f00,
93 float f10,
94 float f01,
95 float f11,
96 float u,
97 float v)
98{
99 float a10 = f10 - f00;
100 float a01 = f01 - f00;
101 float a11 = f11 - f10 - f01 + f00;
102 return f00 + a10 * u + a01 * v + a11 * u * v;
103}
104
105inline float cubic_interpolate(float p[4], float x)
106{
107 return p[1] + 0.5 * x *
108 (p[2] - p[0] +
109 x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] +
110 x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
111}
112
146Array harmonic_interpolation(const Array &array,
147 const Array &mask_fixed_values,
148 int iterations_max = 500,
149 float tolerance = 1e-5f,
150 float omega = 1.8f);
151
179Array interpolate2d(glm::ivec2 shape,
180 const std::vector<float> &x,
181 const std::vector<float> &y,
182 const std::vector<float> &values,
183 InterpolationMethod2D interpolation_method,
184 const Array *p_noise_x = nullptr,
185 const Array *p_noise_y = nullptr,
186 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
187
206Array interpolate2d_delaunay(glm::ivec2 shape,
207 const std::vector<float> &x,
208 const std::vector<float> &y,
209 const std::vector<float> &values,
210 const Array *p_noise_x = nullptr,
211 const Array *p_noise_y = nullptr,
212 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
213 float fill_value = 0.f);
214
219Array interpolate2d_delaunay_gradient(glm::ivec2 shape,
220 const std::vector<float> &x,
221 const std::vector<float> &y,
222 const std::vector<float> &values,
223 const Array *p_noise_x = nullptr,
224 const Array *p_noise_y = nullptr,
225 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
226 float fill_value = 0.f,
227 float gradient_scaling = 1.f);
228
232Array interpolate2d_gaussian(glm::ivec2 shape,
233 const std::vector<float> &x,
234 const std::vector<float> &y,
235 const std::vector<float> &values,
236 const Array *p_noise_x = nullptr,
237 const Array *p_noise_y = nullptr,
238 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
239 float sigma = 0.05f,
240 float radius = 0.f);
241
245Array interpolate2d_idw(glm::ivec2 shape,
246 const std::vector<float> &x,
247 const std::vector<float> &y,
248 const std::vector<float> &values,
249 const Array *p_noise_x = nullptr,
250 const Array *p_noise_y = nullptr,
251 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f},
252 float distance_exp = 2.f,
253 float radius = 0.f);
254
272Array interpolate2d_nearest(glm::ivec2 shape,
273 const std::vector<float> &x,
274 const std::vector<float> &y,
275 const std::vector<float> &values,
276 const Array *p_noise_x = nullptr,
277 const Array *p_noise_y = nullptr,
278 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
279
283Array interpolate2d_nni(glm::ivec2 shape,
284 const std::vector<float> &x,
285 const std::vector<float> &y,
286 const std::vector<float> &values,
287 const Array *p_noise_x = nullptr,
288 const Array *p_noise_y = nullptr,
289 glm::vec4 bbox = {0.f, 1.f, 0.f, 1.f});
290
291} // namespace hmap
292
293namespace hmap::gpu
294{
295
297Array harmonic_interpolation(const Array &array,
298 const Array &mask_fixed_values,
299 int iterations_max = 500);
300
301} // namespace hmap::gpu
Declaration of the Array class for 2D floating-point arrays with various mathematical operations and ...
Definition interpolate2d.hpp:55
void build(const std::vector< float > &xin, const std::vector< float > &yin)
Definition natural_neighbor_interpolator.cpp:19
void interpolate(const std::vector< float > &values_in, std::vector< float > &values_out) const
Definition natural_neighbor_interpolator.cpp:40
~NaturalNeighborInterpolator()
Definition natural_neighbor_interpolator.cpp:13
void setup_output_points(const std::vector< float > &x, const std::vector< float > &y)
Definition natural_neighbor_interpolator.cpp:60
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:11
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:13
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:11
InterpolationMethod2D
Enumeration of 2D interpolation methods.
Definition interpolate2d.hpp:45
@ ITP2D_IDW
Inverse Distance Weighting.
Definition interpolate2d.hpp:48
@ ITP2D_DELAUNAY
Delaunay triangulation method for 2D interpolation.
Definition interpolate2d.hpp:46
@ ITP2D_NNI
Natural Neighbor Interpolation.
Definition interpolate2d.hpp:50
@ ITP2D_DELAUNAY_GRADIENT
Delaunay triangulation + linear gradient.
Definition interpolate2d.hpp:51
@ ITP2D_GAUSSIAN
Gaussian Distance Weighting.
Definition interpolate2d.hpp:49
@ ITP2D_NEAREST
Nearest point method for 2D interpolation.
Definition interpolate2d.hpp:47
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:11
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:15
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:11
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:15
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:92
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:15
float cubic_interpolate(float p[4], float x)
Definition interpolate2d.hpp:105