-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrspltest.cpp
More file actions
241 lines (203 loc) · 4.34 KB
/
rspltest.cpp
File metadata and controls
241 lines (203 loc) · 4.34 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
extern "C" {
#include "rspl/rspl.h"
}
using namespace std;
// struct rspl;
// Base class for 1D interpolation using the rspl library from Argyll. data.
class Interpolate_1D_Private; // We wrap the implementation in here mainly
// so clients don't have to include the C-based rspl headers
class Interpolate_1D
{
public:
Interpolate_1D();
virtual ~Interpolate_1D();
virtual bool Populate(int count,double *in,double *out);
virtual double Interpolate(double input);
virtual double ReverseInterpolate(double output);
protected:
Interpolate_1D_Private *priv;
};
class Interpolate_1D_Private
{
public:
Interpolate_1D_Private() : interp(NULL)
{
interp=new_rspl(0,1,1); // Create a new 1d -> 1d RSPL structure
gres[0]=256;
avgdev[0]=0.0;
low[0]=-0.01;
high[0]=1.01;
low[1]=-0.01;
high[1]=1.01;
}
~Interpolate_1D_Private()
{
if(interp)
interp->del(interp);
}
bool Populate(int count, double *in, double *out)
{
co *myco=new co[count];
for(int i=0;i<count;++i)
{
myco[i].p[0]=in[i];
myco[i].v[0]=out[i];
}
interp->fit_rspl(interp,
0, /* Non-mon and clip flags */
myco, /* Test points */
count, /* Number of test points */
low, high, gres, /* Low, high, resolution of grid */
NULL, NULL, /* Default data scale */
1.0, /* Smoothing */
avgdev, /* Average deviation */
NULL); /* iwidth */
delete[] myco;
return(true);
}
protected:
rspl *interp;
int gres[MXDI];
double avgdev[MXDO];
double low[MXDI];
double high[MXDI];
friend class Interpolate_1D;
};
Interpolate_1D::Interpolate_1D() : priv(NULL)
{
priv=new Interpolate_1D_Private();
}
Interpolate_1D::~Interpolate_1D()
{
if(priv)
delete priv;
}
double Interpolate_1D::Interpolate(double input)
{
co point;
point.p[0]=input;
if(priv->interp->interp(priv->interp,&point))
cerr << "Grid clipped... ";
return(point.v[0]);
}
double Interpolate_1D::ReverseInterpolate(double output)
{
co point;
point.v[0]=output;
int result=priv->interp->rev_interp(priv->interp,0,1,NULL,NULL,&point);
if(result!=1)
cerr << "Got solutions: " << result << " - ";
return(point.p[0]);
}
bool Interpolate_1D::Populate(int count,double *in, double *out)
{
return(priv->Populate(count,in,out));
}
double myin[]=
{
0.0,
0.1,
0.2,
0.3,
0.4,
0.5,
0.6,
0.7,
0.8,
0.9,
1.0
};
double myout[]=
{
0.0,
0.11,
0.19,
0.32,
0.41,
0.56,
0.67,
0.78,
0.86,
0.95,
0.99
};
int main(int argc,char **argv)
{
Interpolate_1D interp;
interp.Populate(11,myin,myout);
cerr << "Forward lookup..." << endl;
for(int i=0;i<100;++i)
{
double o=interp.Interpolate(i/100.0);
cerr << i/100.0 << " -> " << o << endl;
}
cerr << "Reverse lookup..." << endl;
for(int i=0;i<100;++i)
{
double o=interp.ReverseInterpolate(i/100.0);
cerr << i/100.0 << " -> " << o << endl;
}
return(0);
}
#if 0
co mydata[]=
{
{{0.0},{0.0}},
{{0.1},{0.11}},
{{0.2},{0.19}},
{{0.3},{0.32}},
{{0.4},{0.41}},
{{0.5},{0.56}},
{{0.6},{0.67}},
{{0.7},{0.78}},
{{0.8},{0.86}},
{{0.9},{0.95}},
{{1.0},{0.99}},
};
int main(int argc,char **argv)
{
rspl *myrspl=new_rspl(0,1,1); // Create a new 1d -> 1d RSPL structure
int npoints=sizeof(mydata)/sizeof(co);
int gres[MXDI];
double avgdev[MXDO];
double low[MXDI];
double high[MXDI];
gres[0]=3000;
avgdev[0]=0.0;
low[0]=-0.01;
high[0]=1.01;
low[1]=-0.01;
high[1]=1.01;
myrspl->fit_rspl(myrspl,
0, /* Non-mon and clip flags */
mydata, /* Test points */
npoints, /* Number of test points */
low, high, gres, /* Low, high, resolution of grid */
NULL, NULL, /* Default data scale */
1.0, /* Smoothing */
avgdev, /* Average deviation */
NULL); /* iwidth */
cerr << "Forward lookup..." << endl;
for(int i=0;i<100;++i)
{
co point;
point.p[0]=i/100.0;
int result=myrspl->interp(myrspl,&point);
if(result)
cerr << "Grid clipped... ";
cerr << point.p[0] << " -> " << point.v[0] << endl;
}
cerr << "Reverse lookup..." << endl;
for(int i=0;i<100;++i)
{
co point;
point.v[0]=i/100.0;
int result=myrspl->rev_interp(myrspl,0,1,NULL,NULL,&point);
if(result!=1)
cerr << "Got solutions: " << result << " - ";
cerr << point.v[0] << " -> " << point.p[0] << endl;
}
return(0);
}
#endif