-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRBFInterpolator.cpp
More file actions
149 lines (114 loc) · 2.91 KB
/
RBFInterpolator.cpp
File metadata and controls
149 lines (114 loc) · 2.91 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//////////////////////////////////////////////////////////////////////////
//
// RBFInterpolator : interpolation by radial basis functions
//
// 2009 Karsten Noe
//
// Read the blog at cg.alexandra.dk for more information
//
//////////////////////////////////////////////////////////////////////////
#include "RBFInterpolator.h"
#include <math.h>
RBFInterpolator::RBFInterpolator()
{
successfullyInitialized = false;
}
RBFInterpolator::RBFInterpolator(vector<real> x, vector<real> y, vector<real> z, vector<real> f)
{
successfullyInitialized = false; // default value for if we end init prematurely.
M = f.size();
// all four input vectors must have the same length.
if ( x.size() != M || y.size() != M || z.size() != M )
return;
ColumnVector F = ColumnVector(M + 4);
P = Matrix(M, 3);
Matrix G(M + 4,M + 4);
// copy function values
for (unsigned int i = 1; i <= M; i++)
F(i) = f[i-1];
F(M+1) = 0; F(M+2) = 0; F(M+3) = 0; F(M+4) = 0;
// fill xyz coordinates into P
for (unsigned int i = 1; i <= M; i++)
{
P(i,1) = x[i-1];
P(i,2) = y[i-1];
P(i,3) = z[i-1];
}
// the matrix below is symmetric, so I could save some calculations Hmmm. must be a todo
for (unsigned int i = 1; i <= M; i++)
for (unsigned int j = 1; j <= M; j++)
{
real dx = x[i-1] - x[j-1];
real dy = y[i-1] - y[j-1];
real dz = z[i-1] - z[j-1];
real distance_squared = dx*dx + dy*dy + dz*dz;
G(i,j) = g(distance_squared);
}
//Set last 4 columns of G
for (unsigned int i = 1; i <= M; i++)
{
G( i, M+1 ) = 1;
G( i, M+2 ) = x[i-1];
G( i, M+3 ) = y[i-1];
G( i, M+4 ) = z[i-1];
}
for (unsigned int i = M+1; i <= M+4; i++)
for (unsigned int j = M+1; j <= M+4; j++)
G( i, j ) = 0;
//Set last 4 rows of G
for (unsigned int j = 1; j <= M; j++)
{
G( M+1, j ) = 1;
G( M+2, j ) = x[j-1];
G( M+3, j ) = y[j-1];
G( M+4, j ) = z[j-1];
}
Try
{
Ginv = G.i();
A = Ginv*F;
successfullyInitialized = true;
}
CatchAll { cout << BaseException::what() << endl; }
}
RBFInterpolator::~RBFInterpolator()
{
}
real RBFInterpolator::interpolate(real x, real y, real z)
{
if (!successfullyInitialized)
return 0.0f;
real sum = 0.0f;
// RBF part
for (unsigned int i = 1; i <= M; i++)
{
real dx = x - P(i,1);
real dy = y - P(i,2);
real dz = z - P(i,3);
real distance_squared = dx*dx + dy*dy + dz*dz;
sum += A(i) * g(distance_squared);
}
//affine part
sum += A(M+1) + A(M+2)*x + A(M+3)*y + A(M+4)*z;
return sum;
}
//note: assuming the input is t squared
real RBFInterpolator::g(real t_squared)
{
return sqrt(log10(t_squared + 1.0f));
}
void RBFInterpolator::UpdateFunctionValues(vector<real> f)
{
successfullyInitialized = false;
ColumnVector F(M+4);
// copy function values
for (unsigned int i = 1; i <= M; i++)
F(i) = f[i-1];
F(M+1) = 0; F(M+2) = 0; F(M+3) = 0; F(M+4) = 0;
Try
{
A = Ginv*F;
successfullyInitialized = true;
}
CatchAll { cout << BaseException::what() << endl; }
}