-
Notifications
You must be signed in to change notification settings - Fork 2
/
array2_utils.h
63 lines (51 loc) · 1.42 KB
/
array2_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef ARRAY2_UTILS_H
#define ARRAY2_UTILS_H
#include "vec.h"
#include "array2.h"
#include "util.h"
template<class S, class T>
T interpolate_value(const Vec<2,S>& point, const Array2<T, Array1<T> >& grid) {
int i,j;
S fx,fy;
get_barycentric(point[0], i, fx, 0, grid.ni);
get_barycentric(point[1], j, fy, 0, grid.nj);
return bilerp(
grid(i,j), grid(i+1,j),
grid(i,j+1), grid(i+1,j+1),
fx, fy);
}
template<class S, class T>
float interpolate_gradient(Vec<2,T>& gradient, const Vec<2,S>& point, const Array2<T, Array1<float> >& grid) {
int i,j;
S fx,fy;
get_barycentric(point[0], i, fx, 0, grid.ni);
get_barycentric(point[1], j, fy, 0, grid.nj);
T v00 = grid(i,j);
T v01 = grid(i,j+1);
T v10 = grid(i+1,j);
T v11 = grid(i+1,j+1);
T ddy0 = (v01 - v00);
T ddy1 = (v11 - v10);
T ddx0 = (v10 - v00);
T ddx1 = (v11 - v01);
gradient[0] = lerp(ddx0,ddx1,fy);
gradient[1] = lerp(ddy0,ddy1,fx);
//may as well return value too
return bilerp(v00, v10, v01, v11, fx, fy);
}
template<class T>
void write_matlab_array(std::ostream &output, Array2<T, Array1<T> >&a, const char *variable_name, bool transpose=false)
{
output<<variable_name<<"=[";
for(int j = 0; j < a.nj; ++j) {
for(int i = 0; i < a.ni; ++i) {
output<<a(i,j)<<" ";
}
output<<";";
}
output<<"]";
if(transpose)
output<<"'";
output<<";"<<std::endl;
}
#endif