25 #ifndef SHKZ_ARRAY_INTERPOLATOR2_H
26 #define SHKZ_ARRAY_INTERPOLATOR2_H
50 static void interpolate_coef(
const shape2 &shape,
const vec2d &p,
vec2i indices[4],
double coef[4] ) {
51 double x = std::max(0.0,std::min(shape.
w-1.,p[0]));
52 double y = std::max(0.0,std::min(shape.
h-1.,p[1]));
53 int i = std::min(x,shape.
w-2.);
54 int j = std::min(y,shape.
h-2.);
55 indices[0] =
vec2i(i,j);
56 indices[1] =
vec2i(i+1,j);
57 indices[2] =
vec2i(i,j+1);
58 indices[3] =
vec2i(i+1,j+1);
59 coef[0] = (i+1-x)*(j+1-y);
60 coef[1] = (x-i)*(j+1-y);
61 coef[2] = (i+1-x)*(y-j);
62 coef[3] = (x-i)*(y-j);
76 template<
class T> T
static interpolate(
const array2<T> &array,
const vec2d &p,
bool only_actives=
false ) {
77 T values[4];
vec2i indices[4];
double coef[4];
78 interpolate_coef(array.
shape(),p,indices,coef);
83 for(
int n=0; n<4; ++n ) w[n] = array.
active(indices[n]) ? coef[n] : 0.0;
84 for(
int n=0; n<4; ++n ) sum += w[n];
86 for(
int n=0; n<4; ++n ) w[n] /= sum;
87 for(
unsigned n=0; n<4; ++n )
if( w[n] ) value += array(indices[n]) * w[n];
90 for(
unsigned n=0; n<4; ++n )
if( coef[n] ) value += array(indices[n]) * coef[n];
110 template<
class T> T
static interpolate(
const array2<T> &array,
const vec2d &origin,
double dx,
const vec2d &p,
bool only_actives=
false ) {
111 return interpolate<T>(array,(p-origin)/dx,only_actives);