File: cubicSpline1D.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (349 lines) | stat: -rw-r--r-- 9,967 bytes parent folder | download | duplicates (7)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <BALL/MATHS/cubicSpline1D.h>
#include <map>
#include <set>

using namespace std;

namespace BALL 
{
	const int CubicSpline1D::VERBOSITY_LEVEL_CRITICAL = 5;
	const int CubicSpline1D::VERBOSITY_LEVEL_DEBUG = 10;

	CubicSpline1D::CubicSpline1D()
		: sample_positions_(),
			sample_values_(),
			curvature_(),
			return_average_(),
			default_value_(),
			lower_bound_(),
			upper_bound_(), 
			is_natural_(),
			lower_derivative_(),
			upper_derivative_(),
			verbosity_(VERBOSITY_LEVEL_DEBUG)
	{
	}	
	
	CubicSpline1D::CubicSpline1D(const std::vector<float>& sample_positions, 
															 const std::vector<float>& sample_values, bool return_average, 
															 bool is_natural, float lower_derivative, float upper_derivative,
															 int verbosity)
		: sample_positions_(sample_positions),
			sample_values_(sample_values),
			curvature_(),
			return_average_(return_average),
			default_value_(std::numeric_limits<float>::min()),
			lower_bound_(sample_positions[0]),
			upper_bound_(sample_positions[sample_positions.size()-1]),
			is_natural_(is_natural),
			lower_derivative_(lower_derivative),
			upper_derivative_(upper_derivative),
			verbosity_(verbosity)
	{
		// Compute the spline.
		createSpline();	
	}

	CubicSpline1D::CubicSpline1D(const std::vector<float>& sample_positions, 
															 const std::vector<float>& sample_values, float default_value, 
															 bool is_natural, float lower_derivative, float upper_derivative,
															 int verbosity)
		: sample_positions_(sample_positions),
			sample_values_(sample_values),
			curvature_(),
			return_average_(false),
			default_value_(default_value),	
			lower_bound_(sample_positions[0]),
			upper_bound_(sample_positions[sample_positions.size()-1]),
			is_natural_(is_natural),
			lower_derivative_(lower_derivative),
			upper_derivative_(upper_derivative),
			verbosity_(verbosity)
	{
		// Compute the spline.
		createSpline();
	}
	

	CubicSpline1D::CubicSpline1D(const std::vector<float>& sample_positions, 
															 const std::vector<float>& sample_values, float default_value, 
															 float lower_bound, float upper_bound, 
															 bool is_natural, float lower_derivative, float upper_derivative,
															 int verbosity)
		: sample_positions_(sample_positions),
			sample_values_(sample_values),
			curvature_(),
			return_average_(false),
			default_value_(default_value),	
			lower_bound_(lower_bound),
			upper_bound_(upper_bound),
			is_natural_(is_natural),
			lower_derivative_(lower_derivative),
			upper_derivative_(upper_derivative),
			verbosity_(verbosity)
	{
		// Compute the spline.
		createSpline();
	}
	
	
	CubicSpline1D::CubicSpline1D(const std::vector<float>& sample_positions, 
										           const std::vector<float>& sample_values, 
															 float lower_bound, float upper_bound,  
															 bool return_average, float default_value,
															 bool is_natural, float lower_derivative, float upper_derivative,
															 int verbosity)
		: sample_positions_(sample_positions),
			sample_values_(sample_values),
			curvature_(),
			return_average_(return_average),
			default_value_(default_value),	
			lower_bound_(lower_bound),
			upper_bound_(upper_bound),
			is_natural_(is_natural),
			lower_derivative_(lower_derivative),
			upper_derivative_(upper_derivative),
			verbosity_(verbosity)

	{
		// Compute the spline.
		createSpline();			
	}
	
	CubicSpline1D::CubicSpline1D(const CubicSpline1D& cs1D)
		: sample_positions_(cs1D.sample_positions_),
			sample_values_(cs1D.sample_values_),
			curvature_(cs1D.curvature_),
			return_average_(cs1D.return_average_),
			default_value_(cs1D.default_value_),
			lower_bound_(cs1D.lower_bound_),
			upper_bound_(cs1D.upper_bound_), 
			is_natural_(cs1D.is_natural_),
			lower_derivative_(cs1D.lower_derivative_),
			upper_derivative_(cs1D.upper_derivative_),
			verbosity_(cs1D.verbosity_)
	{
	}
			
	CubicSpline1D::~CubicSpline1D()
	{}
			
	void CubicSpline1D::createSpline()
	{
		// Do we have reasonable data?
		if (	 (sample_values_.size() != sample_positions_.size()) 
				&& (verbosity_ >= VERBOSITY_LEVEL_CRITICAL))
		{	
			Log.error() << "CubicSpline1D_::createSpline: number of sample positions != number of sample values" << std::endl;
			return;
		}		
		
		if (return_average_)
		{
			default_value_ = 0.;
			// In case we have too less values or the access-value is out of bound
			// we want to return the average -> compute the average.
			for (Position i=0; i < sample_values_.size(); i++)
			{
				default_value_  += sample_values_[i];
			}
			default_value_ /= sample_values_.size();
		}
			
		//
		// Now we compute the spline.
		// 
		float p, qn, sig, un;
		std::vector<float> u;

		// Set the positions and values.
		int n							= sample_positions_.size();

		// Initialize the vectors.
		curvature_.resize(n,0.);
		u.resize(n,0.); 
		
		if (is_natural_)
		{
			// Natural spline -> second derivative is set to zero 
			// 								-> first derivative does not change 
			curvature_[0] = 0.;        
			u[0] = 0.0;                
		}
		else
		{
			// Use the stored first derivatives of the boundaries.
			curvature_[0] = -0.5;
			u[0] =  (3.0 / (sample_positions_[1] - sample_positions_[0])) * 
							((sample_values_[1]-sample_values_[0]) / (sample_positions_[1]-sample_positions_[0]) - lower_derivative_ );
		}
		
		for (int i=1; i < n-1; i++) 
		{
			// This is the decomposition loop of the tridiagonal algorithm.
			// Curvature_ and u are used for temporary
			// storage of the decomposed factors.
			sig = (sample_positions_[i]-sample_positions_[i-1]) / (sample_positions_[i+1]-sample_positions_[i-1]);
			p 	=  sig * curvature_[i-1] + 2.0;
			curvature_[i] = (sig-1.0)/p;
			u[i] =  (sample_values_[i+1]-sample_values_[i]) / (sample_positions_[i+1]-sample_positions_[i]) 
				    - (sample_values_[i]-sample_values_[i-1]) / (sample_positions_[i]-sample_positions_[i-1]);
			u[i] =  (6.0*u[i] / (sample_positions_[i+1] - sample_positions_[i-1]) - sig*u[i-1])/p;
		}

		if (is_natural_)
		{ // For natural splines, the second derivative is zero
			qn = 0.0;
			un = 0.0; 
		}
		else
		{
		  // The first derivative of the upper bound is given to compute the second derivative! 
		 	qn = 0.5;
		 	un = (3.0/(sample_positions_[n]-sample_positions_[n-1]))*
			    (upper_derivative_ -(sample_values_[n]-sample_values_[n-1])/(sample_positions_[n]-sample_positions_[n-1]));
		}

		curvature_[n-1] = (un - qn*u[n-2])/(qn*curvature_[n-2] + 1.0);

		// Backsubstitution loop of the tridiagonal algorithm.
		for (int k = n-2; k >= 0; k--) 
		{	
			curvature_[k] = curvature_[k]*curvature_[k+1] + u[k]; 
		}	
		return;	
	}

	void CubicSpline1D::setCurvature(std::vector<float> curvature)
	{
		// Do we have enough curvature values?
		if (   (curvature.size() != sample_positions_.size()) 
				&& (verbosity_ >= VERBOSITY_LEVEL_CRITICAL))
		{
			Log.warn()<< "CubicSpline1D_::setCurvature: number of curvature values != number of sample values" << std::endl;
		}
		else
		{
			curvature_ = curvature;
		}

		// NOTE: a subsequent access(es)  will consider the new curvature, since 
		// the operator () evaluates a spline with equation
		// y = A_y_j + B_y_j+1 + C_curv_j + D_curv_j+1
		// A recomputation will overwrite the new curvature.
	}
	
	void CubicSpline1D::setValues(std::vector<float> values, bool recompute)
	{
		sample_values_ = values;
		if (recompute)
		{
			createSpline();
		}
	}
	
	void CubicSpline1D::setPositions(std::vector<float> positions, bool recompute)
	{
		sample_positions_= positions; 
		if (recompute)
		{
			createSpline();
		}
	}

	void CubicSpline1D::setLowerDerivative(float derivative, bool recompute)
	{
		lower_derivative_ = derivative;
		is_natural_ = false;
		if (recompute)
		{
			createSpline();
		}
	}
	
	void CubicSpline1D::setUpperDerivative(float derivative, bool recompute)
	{
		upper_derivative_ = derivative;
		is_natural_ = false;
		if (recompute)
		{
			createSpline();
		}
	}

	void CubicSpline1D::makeNatural(bool recompute)
	{
		is_natural_ = true;
		if (recompute)
		{
			createSpline();
		}
	}

	void CubicSpline1D::setBoudaryDerivatives(float lower_derivative, float upper_derivative, bool recompute)
	{
		is_natural_ = false;
		lower_derivative_ = lower_derivative;
		upper_derivative_ = upper_derivative;
		if (recompute)
		{
			createSpline();
		}
	}

	float CubicSpline1D::operator() (float x)
	{
		unsigned int n = sample_positions_.size();
		// Is this access-value inside the boundaries?
		if (!sample_positions_.empty() && ((x < lower_bound_) || (x > upper_bound_)))
		{
			// Something _really_ bad happened.
			if (		(!return_average_) 
					 && (verbosity_ >= VERBOSITY_LEVEL_CRITICAL))
			{
				Log.warn() << "invalid : access value " << x << " not between "<< sample_positions_[0] << " and " 
									<< sample_positions_[n-1]<< std::endl;
			}
			return default_value_; 
		}

		// Do we have enough points ?
		if (sample_positions_.size() < 3)
		{
			return default_value_; 
		}
			
		// First, we find the indices bracketing the access value x. 
		// We use bisection here.
		int lower_index = 0, upper_index = n-1;
		int index;
		while (upper_index - lower_index > 1) 
		{
			index = (upper_index + lower_index)/2;
			if (sample_positions_[index] > x)
			{
				upper_index = index;
			}
			else
			{
				lower_index = index;
			}
		} 
		
		float spacing = sample_positions_[upper_index] - sample_positions_[lower_index];
		if (	 (spacing == 0.0) 
				&& (verbosity_ >= VERBOSITY_LEVEL_CRITICAL))
		{
			Log.warn() << "Zero length interval" << std::endl;
			return std::numeric_limits<float>::min();
		}

		float a = (sample_positions_[upper_index]-x)/spacing; 
		float b = (x-sample_positions_[lower_index])/spacing;

		float result =  a*sample_values_[lower_index]      + b*sample_values_[upper_index]
						      + ((a*a*a-a)*curvature_[lower_index] + (b*b*b-b)*curvature_[upper_index])*(spacing*spacing)/6.0;

		return result;
	}
}