#ifndef GRID_2D_INTERPOLATOR_DECL #define GRID_2D_INTERPOLATOR_DECL #include #include #include #include #include #include using namespace std; #ifndef EPSILON #define EPSILON 1e-15 #endif //implements a bilinear interpolator on rectangular 2D grids //interpolation is not very efficient (linear in row_counts+col_counts), so this is meant for cases where only a few interpolations are needed //no extrapolation is done, i.e. only requests for points inside the grid are accepted. The rest returns NaN. class Grid2DInterpolator{ private: vector r_values, c_values; vector z_values; //matrix in row-major format of size NR x NC static void string2numvector(const string &s, vector &v); static string vector2string(const vector &v, string separator); void explodeString(vector &parts, const string &haystack, const string &separator, int maxPartsCount); public: //setup grid from a string //string should be of format ,, //where each of the three parts is a space-separated list of real numbers (of length NR, NC and NR*NC, respectively) //z-values are in row-major format bool load(const string &s); //append a row to the existing grid //z_values must be of size get_c_count() //Assumes tat setCvalues(..) has been called already bool appendRow(double r_value, const vector &new_z_values); //define grid c-values //this will delete the entire pre-existing grid and start a new empty grid //required if you are going to build the grid row-by-row using appendRow(..) void setCvalues(const vector &_c_values); //generate a string representation of the grid, in the same format as used by load(..) string getGridAsString() const; //print grid z_values in matrix format void printGridMatrix(ostream &ss) const; //print grid r,c and z_values in scan format (compatible with gnuplot) void printGridScann(ostream &ss) const; //Returns NaN if point is not within grid double interpolate(double r_value, double c_value) const; //returns default_z_value if point is not within grid double interpolate(double r_value, double c_value, double default_z_value) const; //returns a one-line summary of the grid setup string getShortDescription() const; bool isWithinGrid(double r_value, double c_value) const; long get_r_count() const{ return r_values.size(); } long get_c_count() const{ return c_values.size(); } double get_r_value(long r) const{ return r_values[r]; } double get_c_value(long c) const{ return c_values[c]; } double get_z_value(long r, long c) const{ return z_values[r*c_values.size()+c]; } }; #endif