File: cubicSpline2D.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (521 lines) | stat: -rw-r--r-- 14,856 bytes parent folder | download | duplicates (8)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include <BALL/MATHS/cubicSpline2D.h>
#include <map>
#include <set>

using namespace std;

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

	//	Default constructor.
	CubicSpline2D::CubicSpline2D()
		: verbosity_(VERBOSITY_LEVEL_DEBUG)
	{}	
	
	CubicSpline2D::CubicSpline2D(const std::vector<std::vector<float> >& sample_positions_x,
										const std::vector<float>& sample_positions_y,
										const std::vector<std::vector<float> >& sample_values, 
										bool 	return_average,
										bool 	is_natural,  	
										const std::vector<float>& x_lower_derivatives,  
										const std::vector<float>& x_upper_derivatives,
										float y_lower_derivative, 
										float y_upper_derivative,
										int verbosity)
		:	sample_positions_x_(sample_positions_x),
			sample_positions_y_(sample_positions_y),
			splines_(),
			sample_values_(sample_values),
			return_average_(return_average),	
			x_default_values_(),
			y_default_value_(),
			x_lower_bounds_(),
			x_upper_bounds_(),
			y_lower_bound_(),
			y_upper_bound_(),
			x_is_natural_(),
			y_is_natural_(is_natural),
			x_lower_derivatives_(x_lower_derivatives),
			x_upper_derivatives_(x_upper_derivatives),
			y_lower_derivative_(y_lower_derivative),
			y_upper_derivative_(y_upper_derivative),
			verbosity_(verbosity)
	{	
		x_default_values_.resize(sample_positions_y_.size(), std::numeric_limits<float>::min());
		y_default_value_ = std::numeric_limits<float>::min();
		
		// Store the lower and upper bounds.
		// By default, we assume the first and the last values to be the lower and upper bounds.
		x_lower_bounds_.resize(sample_positions_y_.size());
		x_upper_bounds_.resize(sample_positions_y_.size());
		for (Size i = 0; i < sample_positions_x_.size(); i++)
		{
			x_lower_bounds_[i] = sample_positions_x_[i][0];
			x_upper_bounds_[i] = sample_positions_x_[i][ sample_positions_x_[i].size()-1];
		}
		
		y_lower_bound_ = sample_positions_y_[0];
		y_upper_bound_ = sample_positions_y_[sample_positions_y_.size()-1];
	
		if (x_lower_derivatives.size() == (Size) 0.)
		{
			x_lower_derivatives_.resize(sample_positions_y_.size(), 0.);
			x_upper_derivatives_.resize(sample_positions_y_.size(), 0.);
		}

		x_is_natural_.resize(sample_positions_y_.size(), is_natural);

		//  Finally, compute the splines.	
		createBiCubicSpline();
	}
	
	CubicSpline2D::CubicSpline2D(const std::vector<std::vector<float> >& sample_positions_x,
										const std::vector<float>& sample_positions_y,
										const std::vector<std::vector<float> >& sample_values, 
										const std::vector<float>& x_default_values, 
										float y_default_value, 
										const std::vector<float>& x_lower_bounds,
										const std::vector<float>& x_upper_bounds,
										float y_lower_bound,
										float y_upper_bound,
										bool 	is_natural, 	
										const std::vector<float>& x_lower_derivatives,
										const std::vector<float>& x_upper_derivatives,
										float y_lower_derivative,
										float y_upper_derivative,
										int   verbosity)
		: sample_positions_x_(sample_positions_x),
			sample_positions_y_(sample_positions_y),
			splines_(),
			sample_values_(sample_values),
			return_average_(false),
			x_default_values_(x_default_values),
			y_default_value_(y_default_value),
			x_lower_bounds_(x_lower_bounds),
			x_upper_bounds_(x_upper_bounds),
			y_lower_bound_(y_lower_bound),
			y_upper_bound_(y_upper_bound),
			x_is_natural_(vector<bool>()), 
			y_is_natural_(is_natural),
			x_lower_derivatives_(x_lower_derivatives),
			x_upper_derivatives_(x_upper_derivatives),
			y_lower_derivative_(y_lower_derivative),
			y_upper_derivative_(y_upper_derivative),
			verbosity_(verbosity)
	{
		x_is_natural_.resize(sample_positions_y.size(), is_natural);
		
		if (x_lower_derivatives.size() == (Size) 0.)
		{
			x_lower_derivatives_.resize(sample_positions_y.size(), 0.);
			x_upper_derivatives_.resize(sample_positions_y.size(), 0.);
		}

		//  Finally, compute the splines.	
		createBiCubicSpline();
	}
			
	
//------------------------------simple constructors-----------------------------------------------------

	
	CubicSpline2D::CubicSpline2D(const std::vector<float>& sample_positions_x,
										const std::vector<float>& sample_positions_y,
										const std::vector<std::vector<float> >& sample_values, 
										bool 	return_average,
										bool 	is_natural,  	
										const std::vector<float>& x_lower_derivatives,  
										const std::vector<float>& x_upper_derivatives,
										float y_lower_derivative, 
										float y_upper_derivative,
										int 	verbosity)
		:	sample_positions_x_(),
			sample_positions_y_(sample_positions_y),
			splines_(),
			sample_values_(sample_values),
			return_average_(return_average),	
			x_default_values_(),
			y_default_value_(),
			x_lower_bounds_(),
			x_upper_bounds_(),
			y_lower_bound_(),
			y_upper_bound_(),
			x_is_natural_(),
			y_is_natural_(is_natural),
			x_lower_derivatives_(x_lower_derivatives),
			x_upper_derivatives_(x_upper_derivatives),
			y_lower_derivative_(y_lower_derivative),
			y_upper_derivative_(y_upper_derivative),
			verbosity_(verbosity)
	{	
		// Assuming that all rows have the same x-positions, 
		// we have to create a full set of x-positions
		std::vector<std::vector<float> > complete_x_positions(sample_positions_y.size());
		for (Position i=0; i<complete_x_positions.size(); i++)
		{
			complete_x_positions[i] = sample_positions_x;
		}
		
		sample_positions_x_ = complete_x_positions;
	
		x_default_values_.resize(sample_positions_y_.size(), std::numeric_limits<float>::min());
		y_default_value_ = std::numeric_limits<float>::min();
		
		// Store the lower and upper bounds.
		// By default, we assume the first and the last values to be the lower and upper bounds.
		x_lower_bounds_.resize(sample_positions_y.size());
		x_upper_bounds_.resize(sample_positions_y.size());
		for (Size i = 0; i < sample_positions_x.size(); i++)
		{
			x_lower_bounds_[i] = sample_positions_x_[i][0];
			x_upper_bounds_[i] = sample_positions_x_[i][sample_positions_x_[i].size()-1];
		}
		
		y_lower_bound_ = sample_positions_y_[0];
		y_upper_bound_ = sample_positions_y_[sample_positions_y.size()-1];
	
		if (x_lower_derivatives.size() == (Size) 0.)
		{
			x_lower_derivatives_.resize(sample_positions_y_.size(), 0.);
			x_upper_derivatives_.resize(sample_positions_y_.size(), 0.);
		}

		x_is_natural_.resize(sample_positions_y_.size(), is_natural);

		//  Finally, compute the splines.	
		createBiCubicSpline();
	}
	
	CubicSpline2D::CubicSpline2D(const std::vector<float>& sample_positions_x,
										const std::vector<float>& sample_positions_y,
										const std::vector<std::vector<float> >& sample_values, 
										const std::vector<float>& x_default_values, 
										float y_default_value, 
										const std::vector<float>& x_lower_bounds,
										const std::vector<float>& x_upper_bounds,
										float y_lower_bound,
										float y_upper_bound,
										bool 	is_natural, 	
										const std::vector<float>& x_lower_derivatives,
										const std::vector<float>& x_upper_derivatives,
										float y_lower_derivative,
										float y_upper_derivative,
										int		verbosity)
		: sample_positions_x_(),
			sample_positions_y_(sample_positions_y),
			splines_(),
			sample_values_(sample_values),
			return_average_(false),
			x_default_values_(x_default_values),
			y_default_value_(y_default_value),
			x_lower_bounds_(x_lower_bounds),
			x_upper_bounds_(x_upper_bounds),
			y_lower_bound_(y_lower_bound),
			y_upper_bound_(y_upper_bound),
			x_is_natural_(vector<bool>()), 
			y_is_natural_(is_natural),
			x_lower_derivatives_(x_lower_derivatives),
			x_upper_derivatives_(x_upper_derivatives),
			y_lower_derivative_(y_lower_derivative),
			y_upper_derivative_(y_upper_derivative),
			verbosity_(verbosity)
	{
		// Assuming that all rows have the same x-positions, 
		// we have to create a full set of x-positions
		std::vector<std::vector<float> > complete_x_positions(sample_positions_y.size());
		for (Position i=0; i<complete_x_positions.size(); i++)
		{
			complete_x_positions[i] = sample_positions_x;
		}
		
		sample_positions_x_ = complete_x_positions;
		
		x_is_natural_.resize(sample_positions_y_.size(), is_natural);
		
		if (x_lower_derivatives.size() == (Size) 0.)
		{
			x_lower_derivatives_.resize(sample_positions_y_.size(), 0.);
			x_upper_derivatives_.resize(sample_positions_y_.size(), 0.);
		}

		//  Finally, compute the splines.	
		createBiCubicSpline();
	}
	
	// Copy  constructor
	CubicSpline2D::CubicSpline2D(const CubicSpline2D& cs2D)
		: sample_positions_x_(cs2D.sample_positions_x_),
			sample_positions_y_(cs2D.sample_positions_y_),
			splines_(cs2D.splines_),
			sample_values_(cs2D.sample_values_),
			return_average_(cs2D.return_average_),
			x_default_values_(cs2D.x_default_values_), 
			y_default_value_(cs2D.y_default_value_),
			default_value_(cs2D.default_value_),
			x_lower_bounds_(cs2D.x_lower_bounds_),
			x_upper_bounds_(cs2D.x_upper_bounds_),
			y_lower_bound_(cs2D.y_lower_bound_),
			y_upper_bound_(cs2D.y_upper_bound_),
			x_is_natural_(cs2D.x_is_natural_),
			y_is_natural_(cs2D.y_is_natural_),
			x_lower_derivatives_(cs2D.x_lower_derivatives_),
			x_upper_derivatives_(cs2D.x_upper_derivatives_),
			y_lower_derivative_(cs2D.y_lower_derivative_),
			y_upper_derivative_(cs2D.y_upper_derivative_),
			verbosity_(cs2D.verbosity_)
	{
	}
			
 	// Destructor
	CubicSpline2D::~CubicSpline2D()
	{}

	// A complex version to create a 2D Cubic spline. 
	void CubicSpline2D::createBiCubicSpline()
	{
		// For each y sample position
		// a 1D spline upon the corresponding x sample positions 
		// and the x sample values is created and stored.
		int m = sample_positions_x_.size();

		// If neccessary determine the default value.
		if (return_average_)
		{
			default_value_ = 0.;
		}
		for (int j = 0; j < m; j++)
		{
			// compute a 1D spline
			CubicSpline1D cs(sample_positions_x_[j], sample_values_[j], x_lower_bounds_[j], x_upper_bounds_[j], 
											 return_average_, x_default_values_[j], x_is_natural_[j], x_lower_derivatives_[j], x_upper_derivatives_[j]);
			cs.setVerbosity(verbosity_);

			if (return_average_)
			{
				default_value_ += cs.getDefaultValue()/m;
			}
			// store the 1D splines
			splines_.push_back(cs);	
		}
	}

	void CubicSpline2D::setVerbosity(int verbosity)
	{
		verbosity_ = verbosity;
		for (Position i=0; i<splines_.size(); i++)
			splines_[i].setVerbosity(verbosity_);
	}

	float CubicSpline2D::operator () (float x, float y)
	{ 
		// We are looking for the interpolation of the 2D spline
		// at (x,y). 
		// So, we need to evaluate all 1D splines in x direction at value x, 
		// create a temporary 1D spline upon these values and the y positions, and
		// evaluate the temporary spline at value y.
		
		// For the evaluation spline we need
	  // sample values...
		std::vector<float> values;
		
		// The number of 1D splines.
		int n = sample_positions_y_.size();  

		// First, the sample values of our temporary
		// 1D spline are determined by evaluating 
		// for each y position the 1D splines at x.
		for (int i=0; i < n; i++)
		{
			values.push_back(splines_[i](x));
		}

		// Set the default value to a correct value.
		float default_value = y_default_value_;
		if (return_average_)
			default_value = default_value_;	
		
		// Then, a new 1D spline is constructed. 
		CubicSpline1D cs(sample_positions_y_, values, 
										 y_lower_bound_, y_upper_bound_, 
										 return_average_, default_value, 
										 y_is_natural_, y_lower_derivative_, y_upper_derivative_);
		cs.setVerbosity(verbosity_);
		// Evaluate the new spline at y.
		return cs(y);
	}
	
	
	float CubicSpline2D::getXDefaultValue(Index x) const
	{
		if (x >= (Index)x_default_values_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			return x_default_values_[x];
		}
	}
	
	float CubicSpline2D::getXLowerBounds(Index x) const
	{
		if (x >= (Index)x_lower_bounds_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			return x_lower_bounds_[x];
		}
	}
	
	float CubicSpline2D::getXUpperBounds(Index x) const
	{
		if (x >= (Index)x_upper_bounds_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			return x_upper_bounds_[x];
		}
	}
	
	bool CubicSpline2D::isXNatural(Index x)
	{
		if (x > (Index)x_is_natural_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			return x_is_natural_[x];
		}
	}
	
	/** Sets the flag {\tt is_natural_} for the x-th spline to true. 
	 *  By default the method recomputes the spline. 
	 *  If the argument is false, no recomputation is done.*/
	void CubicSpline2D::makeXNatural(Index x, bool recompute)
	{
		if (x > (Index)x_is_natural_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			x_is_natural_[x] = true;
		}
		
		if (recompute)
		{
			createBiCubicSpline();
		}
	}
	
	
	void CubicSpline2D::makeAllXNatural(bool recompute)
	{
		for (Size i = 0; i < x_is_natural_.size(); i++)
		{
			x_is_natural_[i] = true;
		}
		if (recompute)
		{
			createBiCubicSpline();
		}
	}
		
	void CubicSpline2D::makeYNatural(bool y_is_natural, bool recompute)
	{ 
		y_is_natural_ = y_is_natural;	
		
		if (recompute)
		{
			createBiCubicSpline();
		}
	}
	
	void CubicSpline2D::setXLowerDerivatives(vector<float> ld, bool recompute)
	{
		x_lower_derivatives_ = ld;
		if (recompute)
		{
			createBiCubicSpline();
		}

	}
	
	void CubicSpline2D::setXUpperDerivatives(vector<float> ud, bool recompute) 
	{
		x_upper_derivatives_ = ud;	
		if (recompute)
		{
			createBiCubicSpline();
		}
	}	
	
	
	void CubicSpline2D::setYLowerDerivative (float ld, bool recompute)
	{
		y_lower_derivative_ = ld;
		if (recompute)
		{
			createBiCubicSpline();
		}
	}
	
	void CubicSpline2D::setYUpperDerivative (float ud, bool recompute)
	{
		y_upper_derivative_ = ud;
		if (recompute)
		{
			createBiCubicSpline();
		}
	}

	
	float CubicSpline2D::getXLowerDerivatives(Index x)
	{
		if (x >= (Index)x_lower_derivatives_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			return x_lower_derivatives_[x];
		}
	}
	
	float CubicSpline2D::getXUpperDerivatives(Index x)
	{
		if (x >= (Index)x_upper_derivatives_.size())
		{	
			throw Exception::OutOfRange(__FILE__, __LINE__);
		}
		else
		{
			return x_upper_derivatives_[x];
		}
	}
	
	CubicSpline1D& CubicSpline2D::getSpline(Position i) 
	{
		if (i < (Position)splines_.size()) 
			return splines_[i]; 
		else 
			throw Exception::OutOfRange(__FILE__, __LINE__);
	} 
	
	const CubicSpline1D& CubicSpline2D::getSpline(Position i) const 
	{
		if (i < (Position)splines_.size()) 
			return splines_[i]; 
		else 
			throw Exception::OutOfRange(__FILE__, __LINE__);
	}

}