C++ function to Interpolate 2D Scattered Data on to 2D grid

I am looking for a C++ program/function for Interpolation for 2-D gridded data in meshgrid format similar to interp2(X,Y,V,Xq,Yq) in Matlab or scipy.interpolate.griddata in Python

Vq = interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points.




Last edited on
Well assuming 4 bytes per element, then an array of 782,711 x 782,711 needs
2,450,546,038,084 bytes which is approx 2.2 Terra bytes of memory!

For 106,650 points at 4 bytes per point is approx 1/2 megabyte.

So you're looking for a sparse matrix C++ library.

Have a look at http://jefftrull.github.io/c++/eigen/csparse/suitesparse/2017/02/09/sparse-matrices-for-cplusplus.html

Last edited on
You mention 2D matrices, but your griddata pseudo-declaration doesn't seem to need them. Are you asking for this?
1
2
3
4
5
6
7
8
9
10
11
12
struct Point {
    double x,y;  // coordinates
    double value;  // value at the point
};

// Compute and store the "value" members of the "result" vector by interpolating the
// data vector. The interpolation method is given in "method" and can be 0 for linear.
// No other methods are currently supported.
bool
interpolate(const vector<Point> &data,
            vector<Point> &result,
            int method);

Hai dhayden


Thank you for the reply. The following is what I am looking for:
Interpolation for 2-D gridded data in meshgrid format (C++ equivalent for the following MATLAB function )

Vq = interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points.

But I have not understood the above interpolate method in your post. Which header do I need to include for using Interpolate method ?
Last edited on
C++ does not have a built in one, you can find a library or, since this is simple, just write it. Its probably < 10 lines... you find the % difference, effectively. that is if you have data at point 5 (eg y=f(5) ) and at point 10 and want it at 9... what do you need to do? shift it, and its 0-5 so 5 is 100%. 9-5 is 4. so you want the value at 4 on a scale of 0-5: you want 80% of the way between the points. Then you do the data side, same thing, (high-low)*.8 + low. Extend that idea to 2-d and you have it (you can do it component wise, do x then do y).
Last edited on
Topic archived. No new replies allowed.