58 void setAxis(
double origin,
double dx,
int N);
65 void setAxis(std::vector<double> &x_in);
79 void findIIT(
double x_in,
int &i1,
int &i2,
double &t)
const;
99 void dataFromFile(std::string filename);
105 double operator()(
int i)
const;
113 std::vector<double>
x;
126 void findCell(
int &i,
int &ii,
const double x)
const;
145 for(
int i = 0; i < n; i++){
146 x.push_back(origin_in + static_cast<double>(i)*dx_in);
164 i =
static_cast<int>((x_in - origin)/dx);
170 int mid = (i + ii)/2;
179 findCell(i, ii, x_in);
200 findCell(i1, i2, x_in);
201 t = (x_in - x[i1])/(x[i2] - x[i1]);
206 std::ifstream datafile(filename.c_str());
207 if (!datafile.good()) {
208 std::cout <<
"Can't open the file: " << filename << std::endl;
212 std::string line, tmp;
213 getline(datafile, line);
215 std::istringstream inp(line.c_str());
218 if (tmp.compare(
"CONST") == 0)
227 getline(datafile, line);
228 std::istringstream inp(line.c_str());
231 setAxis(origin, dx, Nticks);
236 for (
int i = 0; i < Nticks; i++){
237 getline(datafile, line);
238 std::istringstream inp(line.c_str());
261 double operator()(
int l,
int r,
int c)
const;
263 double operator()(
int r,
int c)
const;
265 double operator()(
int c)
const;
269 void resize(
int nl,
int nr,
int nc);
271 void set(
int l,
int r,
int c,
double v_in);
282 std::vector<std::vector<std::vector<double>>>
v;
298 v.resize(nl,std::vector<std::vector<double>>(nr, std::vector<double>(nc,0)));
309 return v[0][0].size();
339 double interpolate(
double x,
double y = 0,
double z = 0)
const;
349 void setAxis(
int idim, std::vector<double> &x_in);
358 void setAxis(
int idim,
double origin_in,
double dx_in,
int n);
365 void setMethod(
METHOD method_in);
396 void getDataFromFile(std::string filename);
413 void setValues(
int l,
int r,
int c,
double v_in);
416 void setElevation(
int l,
int r,
int c,
double v_in);
431 double interp1D(
double x)
const;
433 double interp2D(
double x,
double y)
const;
435 double interp3D(
double x,
double y,
double z)
const;
439 double translateX = 0.0;
440 double translateY = 0.0;
444 bool bPreTransform =
false;
456 for(
int idim = 0; idim < dim; ++ idim)
467 a[idim].setAxis(x_in);
473 a[idim].setAxis(origin_in, dx_in, n);
483 elev.set(l, r, c, v_in);
494 std::ifstream datafile(filename.c_str());
495 if (!datafile.good()) {
496 std::cout <<
"Can't open the file: " << filename << std::endl;
501 std::string line, tmp;
502 std::vector<int> dims;
504 getline(datafile, line);
506 std::istringstream inp(line.c_str());
508 if (tmp.compare(
"GRIDDED") == 0){
510 getline(datafile, line);
512 inp.str(line.c_str());
516 if (tmp.compare(
"LINEAR") == 0)
518 else if (tmp.compare(
"NEAREST") == 0)
523 for(
int idim = 0; idim < dim; ++idim){
532 if (std::abs(translateX) > 0.000001 || std::abs(translateY) > 0.000001 || std::abs(rotate) > 0.000001){
533 bPreTransform =
true;
535 double pi = 4*std::atan(1);
536 cosTH = std::cos(rotate * pi/180.0);
537 sinTH = std::sin(rotate * pi/180.0);
542 v.resize(1,1,dims[0]);
544 v.resize(1,dims[1],dims[0]);
546 v.resize(dims[2], dims[1], dims[0]);
549 getline(datafile, line);
551 std::istringstream inp(line.c_str());
552 for(
int i = 0; i < dim; ++i){
554 a[i].dataFromFile(tmp);
559 for (
int i = 0; i < dims[0]; ++i){
560 getline(datafile, line);
561 std::istringstream inp(line.c_str());
580 for (
int k = 0; k < nz; ++k)
581 for (
int i = 0; i < ny; ++i){
582 getline(datafile, line);
583 std::istringstream inp(line.c_str());
584 for (
int j = 0; j < nx; ++j){
600 x = x * cosTH - y * sinTH;
601 x = x * sinTH + y * cosTH;
607 outcome = interp1D(x);
610 outcome = interp2D(x,y);
613 outcome = interp3D(x,y,z);
616 outcome = std::numeric_limits<double>::quiet_NaN();
627 a[0].findIIT(x,i1, i2, t);
631 outcome = v(i1)*(1-t) + v(i2)*t;
634 if (std::abs(a[0](i1) - x) <= std::abs(a[0](i2) - x))
640 outcome = std::numeric_limits<double>::quiet_NaN();
652 a[0].findIIT(x, j1, j2, tx);
653 a[1].findIIT(y, i1, i2, ty);
659 double y1 = v(i1, j1)*(1-tx) + v(i1, j2)*tx;
660 double y2 = v(i2, j1)*(1-tx) + v(i2, j2)*tx;
661 outcome = y1*(1-ty) + y2*ty;
668 if (std::abs(a[0](j1) - x) <= std::abs(a[0](j2) - x))
673 if (std::abs(a[1](i1) - y) <= std::abs(a[1](i2) - y))
688 int i1, i2, j1, j2, k1, k2;
690 a[0].findIIT(x, j1, j2, tx);
691 a[1].findIIT(y, i1, i2, ty);
692 a[2].findIIT(z, k1, k2, tz);
698 double z1y1 = v(k1, i1, j1)*(1-tx) + v(k1, i1, j2)*tx;
699 double z1y2 = v(k1, i2, j1)*(1-tx) + v(k1, i2, j2)*tx;
700 double z1 = z1y1*(1-ty) + z1y2*ty;
702 double z2y1 = v(k2, i1, j1)*(1-tx) + v(k2, i1, j2)*tx;
703 double z2y2 = v(k2, i2, j1)*(1-tx) + v(k2, i2, j2)*tx;
704 double z2 = z2y1*(1-ty) + z2y2*ty;
706 return z1*(1-tz) + z2*tz;
712 if (std::abs(a[0](j1) - x) <= std::abs(a[0](j2) - x))
717 if (std::abs(a[1](i1) - y) <= std::abs(a[1](i2) - y))
721 if (std::abs(a[2](k1) - y) <= std::abs(a[2](k2) - y))
738 void writeCoordsValues(std::string filename, std::vector<std::vector<double>> &coords, std::vector<double> &v){
739 std::ofstream out_file;
740 out_file.open(filename.c_str());
741 for (
unsigned int i = 0; i < coords.size(); i++){
742 for (
unsigned j = 0; j < coords[i].size(); j++){
743 out_file << coords[i][j] <<
" ";
745 out_file << v[i] << std::endl;
void getDataFromFile(std::string filename)
Get the Data From File object.
Definition: gridInterp.h:493
int nx() const
returns the number of columns or values in the x direction
Definition: gridInterp.h:308
int last() const
Definition: gridInterp.h:139
GridValues()
Empty constructor.
Definition: gridInterp.h:259
std::vector< std::vector< std::vector< double > > > v
Definition: gridInterp.h:282
void reset()
Resets the class to the empty state.
Definition: gridInterp.h:455
The axis class is a container for axis tick values.
Definition: gridInterp.h:47
std::vector< axis > a
A vector of axis objects.
Definition: gridInterp.h:423
void writeCoordsValues(std::string filename, std::vector< std::vector< double >> &coords, std::vector< double > &v)
This is a utility function which is used to print the results of the interpolation to a file so that ...
Definition: gridInterp.h:738
Definition: gridInterp.h:10
double interp3D(double x, double y, double z) const
The interpolate method will call this when dim is 3.
Definition: gridInterp.h:687
double operator()(int l, int r, int c) const
Access operator in 3D.
Definition: gridInterp.h:285
void setValues(int l, int r, int c, double v_in)
Set the values of the iterpolation.
Definition: gridInterp.h:477
axis()
The constructor does absolutely nothing.
Definition: gridInterp.h:50
void findIIT(double x_in, int &i1, int &i2, double &t) const
Finds the lower and upper ids that enclose the input value #x_in. Also returns the parametric value t...
Definition: gridInterp.h:185
GridValues v
The container for the values that are used in the interpolation.
Definition: gridInterp.h:425
void set(int l, int r, int c, double v_in)
Definition: gridInterp.h:300
double interp1D(double x) const
The interpolate method will call this when dim is 1.
Definition: gridInterp.h:623
void setMethod(METHOD method_in)
sets the method
Definition: gridInterp.h:487
GridValues elev
In 3D this holds the layer elevation information.
Definition: gridInterp.h:427
double cosTH
Definition: gridInterp.h:442
double interpolate(double x, double y=0, double z=0) const
This is the method to call to do the interpolation.
Definition: gridInterp.h:596
void reset()
Definition: gridInterp.h:304
void dataFromFile(std::string filename)
Reads the axis data from file.
Definition: gridInterp.h:205
METHOD
Enumeration for the interpolation methods.
Definition: gridInterp.h:18
TYPE
The interpolation type it is used only in 3D. For 1 and 2 D it is ignored. The normal type is when in...
Definition: gridInterp.h:31
void setAxis(double origin, double dx, int N)
Set the Axis tick values when the space between the ticks is constant.
Definition: gridInterp.h:143
void setElevation(int l, int r, int c, double v_in)
Definition: gridInterp.h:482
int nz() const
returns the number of layers or values in the z direction
Definition: gridInterp.h:314
void resize(int nl, int nr, int nc)
Definition: gridInterp.h:294
std::vector< double > x
is the vector that containts the tick values
Definition: gridInterp.h:113
void reset()
Delets all data and resest the axis object to its original state.
Definition: gridInterp.h:129
interp()
Definition: gridInterp.h:449
void findCell(int &i, int &ii, const double x) const
Definition: gridInterp.h:159
double operator()(int i) const
accessor for the i element of the axis
Definition: gridInterp.h:136
void setAxis(int idim, std::vector< double > &x_in)
Set the Axis object.
Definition: gridInterp.h:465
Main interpolation class.
Definition: gridInterp.h:326
int ny() const
returns the number of rows or values in the y direction
Definition: gridInterp.h:311
double interp2D(double x, double y) const
The interpolate method will call this when dim is 2.
Definition: gridInterp.h:648
double sinTH
Definition: gridInterp.h:443
This is a container class for the values.
Definition: gridInterp.h:256