8 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
13 #include "../../shared/shared.h"
14 #include "../../shared/io/io.h"
17 int InterpFromGridToMeshx(
IssmSeqVec<IssmPDouble>** pdata_mesh,
double* x_in,
int x_rows,
double* y_in,
int y_rows,
double* data,
int M,
int N,
double* x_mesh,
double* y_mesh,
int nods,
double default_value,
const char* interptype){
28 if ((M<2) || (N<2) || (nods<=0)){
29 _error_(
"nothing to be done according to the dimensions of input matrices and vectors.");
31 if (x_in[1]-x_in[0]<0){
32 _error_(
"x coordinate vector should be increasing.\n use Matlab's command x=flipud(x), also flip the data matrix data=fliplr(data)");
34 if (y_in[1]-y_in[0]<0){
35 _error_(
"y coordinate vector should be increasing.\n use Matlab's command y=flipud(y), also flip the data matrix data=flipud(data)");
42 if(N==(x_rows-1) && M==(y_rows-1)){
47 for(i=0;i<N;i++) x[i]=(x_in[i]+x_in[i+1])/2.;
48 for(i=0;i<M;i++) y[i]=(y_in[i]+y_in[i+1])/2.;
52 else if (N==x_rows && M==y_rows){
57 for(i=0;i<N;i++) x[i]=x_in[i];
58 for(i=0;i<M;i++) y[i]=y_in[i];
61 _error_(
"x and y vectors length should be 1 or 0 more than data number of rows.");
85 *pdata_mesh=data_mesh;
105 double Q11,Q12,Q21,Q22;
110 my_thread=handle->
id;
111 num_threads=handle->
num;
114 double *x_mesh = gate->
x_mesh;
115 double *y_mesh = gate->
y_mesh;
116 int x_rows = gate->
x_rows;
117 int y_rows = gate->
y_rows;
120 int nods = gate->
nods;
122 double *data = gate->
data;
124 const char* interptype = gate->
interp;
128 bool debug = M*N>1?
true:
false;
132 for (i=i0;i<i1;i++) {
139 if(
findindices(&n,&m,x,x_rows, y,y_rows, x_grid,y_grid)){
157 Q22=data[(m+1)*N+n+1];
159 if(strcmp(interptype,
"triangle")==0){
160 data_value=
triangleinterp(x1,x2,y1,y2,Q11,Q12,Q21,Q22,x_grid,y_grid);
162 else if(strcmp(interptype,
"bilinear")==0){
163 data_value=
bilinearinterp(x1,x2,y1,y2,Q11,Q12,Q21,Q22,x_grid,y_grid);
165 else if(strcmp(interptype,
"nearest")==0){
166 data_value=
nearestinterp(x1,x2,y1,y2, Q11,Q12,Q21,Q22,x_grid,y_grid);
169 _printf_(
"Interpolation " << interptype <<
" not supported yet (supported intepolations are: triangle, bilinear and nearest)\n");
172 if(xIsNan<IssmPDouble>(data_value)) data_value=default_value;
175 data_value=default_value;
178 data_mesh->SetValue(i,data_value,
INS_VAL);
185 bool findindices(
int* pn,
int* pm,
double* x,
int x_rows,
double* y,
int y_rows,
double xgrid,
double ygrid){
187 bool foundx=
false,foundy=
false;
191 for (i=0;i<x_rows-1;i++){
192 if ((x[i]<=xgrid) && (xgrid<x[i+1])){
198 if(xgrid==x[x_rows-1]){
203 for (i=0;i<y_rows-1;i++){
204 if ((y[i]<=ygrid) && (ygrid<y[i+1])){
210 if(ygrid==y[y_rows-1]){
217 return (foundx && foundy);
220 double triangleinterp(
double x1,
double x2,
double y1,
double y2,
double Q11,
double Q12,
double Q21,
double Q22,
double x,
double y){
233 double area,area_1,area_2,area_3;
237 _assert_(x<=x2 && x>=x1 && y<=y2 && y>=y1);
240 area=(x2-x1)*(y2-y1);
243 if ((x-x1)/(x2-x1)<(y-y1)/(y2-y1)){
245 area_1=((y2-y)*(x2-x1))/area;
246 area_2=((x-x1)*(y2-y1))/area;
247 area_3=1-area_1-area_2;
249 return area_1*Q11+area_2*Q22+area_3*Q12;
253 area_1=((y-y1)*(x2-x1))/area;
254 area_2=((x2-x)*(y2-y1))/area;
255 area_3=1-area_1-area_2;
257 return area_1*Q22+area_2*Q11+area_3*Q21;
261 double bilinearinterp(
double x1,
double x2,
double y1,
double y2,
double Q11,
double Q12,
double Q21,
double Q22,
double x,
double y){
278 _assert_(x<=x2 && x>=x1 && y<=y2 && y>=y1);
281 +Q11*(x2-x)*(y2-y)/((x2-x1)*(y2-y1))
282 +Q21*(x-x1)*(y2-y)/((x2-x1)*(y2-y1))
283 +Q12*(x2-x)*(y-y1)/((x2-x1)*(y2-y1))
284 +Q22*(x-x1)*(y-y1)/((x2-x1)*(y2-y1));
288 double nearestinterp(
double x1,
double x2,
double y1,
double y2,
double Q11,
double Q12,
double Q21,
double Q22,
double x,
double y){
304 _assert_(x<=x2 && x>=x1 && y<=y2 && y>=y1);
309 if (x<xm && y<ym)
return Q11;
310 if (x<xm && y>ym)
return Q12;
311 if (x>xm && y<ym)
return Q21;