Blame view

src/InternLib/Resampling.hh 27.9 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Resampling.hh
 *
 *  Created on: 31 oct. 2012
 *      Author: AKKA IS
 */

#ifndef Resampling_HH_
#define Resampling_HH_

#include <list>
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "VisitorOfParamData.hh"
#include "Operation.hh"

#include "TimeInterval.hh"

namespace AMDA {
namespace Parameters {

namespace Resampling {

ca31a786   Benjamin Renard   Fix other side ef...
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
class MarkerAbstract {
public:
	MarkerAbstract() : _startTime(0.), _stopTime(0.), _samplingTime(0.), _markerMinTime(0.), _markerMaxTime(0.), _targetTime(0.) {
	}

	virtual bool init(double pStartTime, double pStopTime, int /*currentTimeIntervalIndex*/, double pSamplingTime) {
		_startTime = pStartTime;
		_stopTime = pStopTime;
		_samplingTime = pSamplingTime;
		return true;
	}

	virtual bool nextTarget() = 0;

	double getTargetTime() {
		return _targetTime;
	}

	double getMarkerMinTime() {
		return _markerMinTime;
	}

	double getMarkerMaxTime() {
		return _markerMaxTime;
	}

	virtual bool isFinished() = 0;

protected:
	bool setTargetTime(double pTargetTime) {
		_targetTime = pTargetTime;
		_markerMinTime = _targetTime - _samplingTime / 2.;
		if (_markerMinTime < _startTime) {
			_markerMinTime = _startTime;
		}
		_markerMaxTime = _targetTime + _samplingTime / 2.;
		return !isFinished();
	}

	double _startTime;
	double _stopTime;
	double _samplingTime;
	double _markerMinTime;
        double _markerMaxTime;
	double _targetTime;
};

class MarkerRefParam: public MarkerAbstract {
public:
	MarkerRefParam() : MarkerAbstract(), _refParamDataPtr(NULL), _crtRefIndex(-1) {
	}

	void setRefParamData(ParamData* refParamDataPtr) {
		_refParamDataPtr = refParamDataPtr;
	}

	bool init(double pStartTime, double pStopTime, int currentTimeIntervalIndex, double pSamplingTime) {
		MarkerAbstract::init(pStartTime, pStopTime, currentTimeIntervalIndex, pSamplingTime);
		if (_refParamDataPtr == NULL) {
			return false;
		}
		if (_refParamDataPtr->getIndexInfo()._endTimeIntIndexList.empty() || (currentTimeIntervalIndex == 0)) {
			_crtRefIndex = 0;
		}
		else {
			std::list<unsigned int>::iterator it = _refParamDataPtr->getIndexInfo()._endTimeIntIndexList.begin();
			std::advance(it, currentTimeIntervalIndex-1);
			_crtRefIndex = *it;
		}
		while (pStartTime > _refParamDataPtr->getTime(_crtRefIndex)) {
			++_crtRefIndex;
			if (_crtRefIndex >= (int)_refParamDataPtr->getDataNumber()) {
				_crtRefIndex = -1;
				return false;
			}
		}
		return setTargetTime(_refParamDataPtr->getTime(_crtRefIndex));
	}

	bool nextTarget() {
		if (_crtRefIndex < 0) {
			return false;
		}
		if (_refParamDataPtr == NULL) {
			_crtRefIndex = -1;
			return false;
		}
		++_crtRefIndex;
		if (_crtRefIndex >= (int)_refParamDataPtr->getDataNumber()) {
			_crtRefIndex = -1;
			return false;
		}
		return setTargetTime(_refParamDataPtr->getTime(_crtRefIndex));
	}

	bool isFinished() {
		return (_crtRefIndex == -1);
	}

private:
	ParamData* _refParamDataPtr;

	int _crtRefIndex;
};

class MarkerSampling: public MarkerAbstract {
public:
	MarkerSampling() : MarkerAbstract() {
	}

	bool init(double pStartTime, double pStopTime, int currentTimeIntervalIndex, double pSamplingTime) {
		MarkerAbstract::init(pStartTime, pStopTime, currentTimeIntervalIndex, pSamplingTime);
		return setTargetTime(pStartTime);
	}

	bool nextTarget() {
		return setTargetTime(getTargetTime() + _samplingTime);
	}

	bool isFinished() {
		return (_targetTime < _startTime || _targetTime > _stopTime);
	}
};


fbe3c2bb   Benjamin Renard   First commit
149
150
151
152
153
154
155
156
/**
 * Define type Resampling.
 */
class ResamplingAbstract: public Operation {
public:
	/**
	 * @brief constructor
	 */
ca31a786   Benjamin Renard   Fix other side ef...
157
158
	ResamplingAbstract(Process& pProcess, bool pUseNearestValue) :
			Operation(pProcess), _isNewInt(true), _useNearestValue(pUseNearestValue) {
fbe3c2bb   Benjamin Renard   First commit
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

	}
	/**
	 * @brief destructor.
	 */
	virtual ~ResamplingAbstract() {
	}
	/**
	 * @brief compute static data
	 */
	virtual void init() = 0;

	/**
	 * @overload Operation::reset(double pStartTime, double pTimeInt)
	 * @brief reset static data to process another TimeInterval
	 */
	virtual void reset() {
		Operation::reset();
a6490f4d   Benjamin Renard   Do not throw an e...
177
		resetResampling();
fbe3c2bb   Benjamin Renard   First commit
178
179
180
181
182
183
184
185
		_isNewInt = true;
	}

	/*
	 * @brief return sampling value
	 */
	virtual double getSampling() = 0;

a6490f4d   Benjamin Renard   Do not throw an e...
186
187
188
189
190
	/*
	 * @brief reset resampling
	 */
	virtual void resetResampling() = 0;

fbe3c2bb   Benjamin Renard   First commit
191
protected:
fbe3c2bb   Benjamin Renard   First commit
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
	/**
	 *@brief  tag to know if a new time interval is set
	 */
	bool _isNewInt;

	/*
	 * @brief  Define the resampling strategy
	 */
	bool _useNearestValue;
};

/**
 * Simple Resampling abstract class, depand of paramData type
 */
template<typename TParamData>
class ResamplingSimpleAbstract: public ResamplingAbstract {

public:
	/**
	 * @brief  Element type of paramData
	 */
	typedef typename TParamData::ElementType ElementType;

	/**
	 * @brief  Constructor
	 */
ca31a786   Benjamin Renard   Fix other side ef...
218
219
220
221
222
	ResamplingSimpleAbstract(Process& pProcess, MarkerAbstract* pMarkerPtr, TimeIntervalListSPtr pTimeIntervalList, double gapSize, TParamData &param, bool pUseNearestValue) :
			ResamplingAbstract(pProcess, pUseNearestValue),
					_markerPtr(pMarkerPtr),
					_timeIntervalList(pTimeIntervalList),
					_currentTimeInterval(_timeIntervalList->begin()), _currentTimeIntervalIndex(0), _samplingMode(INTERPOLATION), _nearestTime(NAN), _lastTime(0),
fbe3c2bb   Benjamin Renard   First commit
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
					_leftEffect(true), _paramOutput(new TParamData()), _paramInput(param),
					_gapSize(gapSize) {
		_paramDataOutput = _paramOutput;
	}

	/**
	 * @brief  Destructor
	 */
	virtual ~ResamplingSimpleAbstract() {
	}
	;

	/**
	 * @brief Type List of Element type of paramData
	 */
	typedef std::list<ElementType> ListValue;

	/**
	 * @overload ResamplingAbstract::ResamplingAbstract::write()
	 */
	void write(ParamDataIndexInfo &pParamDataIndexInfo) {
		if (_isNewInt)
		{
ca31a786   Benjamin Renard   Fix other side ef...
246
			_markerPtr->init((*_currentTimeInterval)._startTime, (*_currentTimeInterval)._stopTime, _currentTimeIntervalIndex, getSampling());
fbe3c2bb   Benjamin Renard   First commit
247
248
249
250
251
			_isNewInt = false;
		}

		if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
			//init _val with the first data value (to init dimensions)
65aa1ad5   Benjamin Renard   Fix a bug in resa...
252
253
254
255
256
257
258
			if (pParamDataIndexInfo._startIndex == 0)
			{
				_val = _paramInput.get(pParamDataIndexInfo._startIndex);
				_nearestValue = _paramInput.get(pParamDataIndexInfo._startIndex);
				_val << NotANumber();
				_nearestValue << NotANumber();
			}
fbe3c2bb   Benjamin Renard   First commit
259
260
261
262
263

			switch (_samplingMode)
			{
			case AVERAGE :
				writeAverage(pParamDataIndexInfo);
fbe3c2bb   Benjamin Renard   First commit
264
265
266
267
268
269
270
271
				break;
			case NEARESTVALUE :
				writeNearestValue(pParamDataIndexInfo);
				break;
			case INTERPOLATION :
				writeInterpolation(pParamDataIndexInfo);
				break;
			}
ca31a786   Benjamin Renard   Fix other side ef...
272
		}
bb672c4c   Benjamin Renard   Fix side effect i...
273
		if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
fbe3c2bb   Benjamin Renard   First commit
274
275
276
277
278
279
280
281
			terminated();
		}
	}

	/**
	 * @brief compute and write last value, right effect
	 */
	virtual void terminated() {
ca31a786   Benjamin Renard   Fix other side ef...
282
		if (_markerPtr->isFinished()) {
bb672c4c   Benjamin Renard   Fix side effect i...
283
284
			return;
		}
fbe3c2bb   Benjamin Renard   First commit
285
286
		switch (_samplingMode)
		{
fbe3c2bb   Benjamin Renard   First commit
287
288
289
		case AVERAGE :
			if (!_mem.empty())
			{
c6877b6f   Benjamin Renard   Fix average for v...
290
				_val = average(_mem,_paramInput.getDim1(),_paramInput.getDim2());
fbe3c2bb   Benjamin Renard   First commit
291
				_paramOutput->push(_val);
ca31a786   Benjamin Renard   Fix other side ef...
292
				_paramDataOutput->pushTime(_markerPtr->getTargetTime());
fbe3c2bb   Benjamin Renard   First commit
293
				_mem.clear();
fbe3c2bb   Benjamin Renard   First commit
294
295
			}
			break;
2f932bad   Benjamin Renard   Fix interpolation...
296
		case INTERPOLATION :
fbe3c2bb   Benjamin Renard   First commit
297
		case NEARESTVALUE :
bb672c4c   Benjamin Renard   Fix side effect i...
298
			//Fix  side-off effects - Right boundary - CONSTANTS
ca31a786   Benjamin Renard   Fix other side ef...
299
300
301
302
303
304
305
306
307
			do {
				if ((_markerPtr->getTargetTime() - _lastTime) <= _gapSize)
					_val = _valMoins1;
				else
					_val << NotANumber();
				_paramOutput->pushTime(_markerPtr->getTargetTime());
				_paramOutput->push(_val);
			
			} while(_markerPtr->nextTarget());
bb672c4c   Benjamin Renard   Fix side effect i...
308
			break;
2f932bad   Benjamin Renard   Fix interpolation...
309
		}
fbe3c2bb   Benjamin Renard   First commit
310
311
312
313
314
315
316
317
318
319
320
321
322
	}

	/**
	 * @brief compute and write new value in average mode
	 */
	void writeNearestValue(ParamDataIndexInfo &pParamDataIndexInfo)
	{
		for (unsigned int index = pParamDataIndexInfo._startIndex;
				index < pParamDataIndexInfo._startIndex
					+ pParamDataIndexInfo._nbDataToProcess; index++)
		{
			double timeCurrent = _paramInput.getTime(index);

ca31a786   Benjamin Renard   Fix other side ef...
323
			if (timeCurrent < _markerPtr->getMarkerMaxTime())
fbe3c2bb   Benjamin Renard   First commit
324
			{
ca31a786   Benjamin Renard   Fix other side ef...
325
326
327
				if (timeCurrent < _markerPtr->getMarkerMinTime()) {
					continue;
				}
fbe3c2bb   Benjamin Renard   First commit
328
329
330
331
332
				if (isNAN(_nearestTime))
				{
					_nearestValue = _paramInput.get(index);
					_nearestTime = timeCurrent;
				}
ca31a786   Benjamin Renard   Fix other side ef...
333
				else if (abs(_nearestTime-_markerPtr->getTargetTime()) > abs(timeCurrent-_markerPtr->getTargetTime()))
fbe3c2bb   Benjamin Renard   First commit
334
335
336
337
338
339
				{
					_nearestValue = _paramInput.get(index);
					_nearestTime = timeCurrent;
				}
			} else {
				_paramOutput->push(_nearestValue);
ca31a786   Benjamin Renard   Fix other side ef...
340
341
				_paramDataOutput->pushTime(_markerPtr->getTargetTime());
				if(!_markerPtr->nextTarget())
fbe3c2bb   Benjamin Renard   First commit
342
					break;
ca31a786   Benjamin Renard   Fix other side ef...
343
				while (_markerPtr->getMarkerMaxTime() < timeCurrent)
fbe3c2bb   Benjamin Renard   First commit
344
				{
2f932bad   Benjamin Renard   Fix interpolation...
345
					if (timeCurrent - _lastTime < _gapSize) {
80265e6a   Benjamin Renard   Compute interpola...
346
						//Interpolation
ca31a786   Benjamin Renard   Fix other side ef...
347
						double coefT = (float) (_markerPtr->getTargetTime() - _lastTime) / (timeCurrent - _lastTime);
2f932bad   Benjamin Renard   Fix interpolation...
348
						_val = interpolation(_valMoins1, _paramInput.get(index), coefT, _paramInput.getDim1(),_paramInput.getDim2());
80265e6a   Benjamin Renard   Compute interpola...
349
					}
2f932bad   Benjamin Renard   Fix interpolation...
350
					else {
80265e6a   Benjamin Renard   Compute interpola...
351
						_val << NotANumber();
2f932bad   Benjamin Renard   Fix interpolation...
352
					}
fbe3c2bb   Benjamin Renard   First commit
353
					_paramOutput->push(_val);
ca31a786   Benjamin Renard   Fix other side ef...
354
					_paramDataOutput->pushTime(_markerPtr->getTargetTime());
fbe3c2bb   Benjamin Renard   First commit
355
					_nearestTime << NotANumber();
ca31a786   Benjamin Renard   Fix other side ef...
356
					if(!_markerPtr->nextTarget())
fbe3c2bb   Benjamin Renard   First commit
357
						break;
fbe3c2bb   Benjamin Renard   First commit
358
				}
fbe3c2bb   Benjamin Renard   First commit
359
360
361
				_nearestValue = _paramInput.get(index);
				_nearestTime = timeCurrent;
			}
80265e6a   Benjamin Renard   Compute interpola...
362
363
364

			_lastTime = _paramInput.getTime(index);
			_valMoins1 = _paramInput.get(index);
fbe3c2bb   Benjamin Renard   First commit
365
366
367
368
369
370
371
		}
	}

	/**
	 * @brief compute and write new value in average mode
	 */
	void writeAverage(ParamDataIndexInfo &pParamDataIndexInfo) {
fbe3c2bb   Benjamin Renard   First commit
372
373
374
375
376
377
378
379
		for (unsigned int index = pParamDataIndexInfo._startIndex;
				index
						< pParamDataIndexInfo._startIndex
								+ pParamDataIndexInfo._nbDataToProcess;
				index++) {

			double timeCurrent = _paramInput.getTime(index);

ca31a786   Benjamin Renard   Fix other side ef...
380
381
382
383
			if (timeCurrent < _markerPtr->getMarkerMaxTime()) {
				if (timeCurrent >= _markerPtr->getMarkerMinTime()) { 
					_mem.push_back(_paramInput.get(index));
				}
fbe3c2bb   Benjamin Renard   First commit
384
			} else {
ca31a786   Benjamin Renard   Fix other side ef...
385
				if (!_mem.empty()) {
c6877b6f   Benjamin Renard   Fix average for v...
386
					_val = average(_mem,_paramInput.getDim1(),_paramInput.getDim2());
ca31a786   Benjamin Renard   Fix other side ef...
387
				}
2f932bad   Benjamin Renard   Fix interpolation...
388
				else if (timeCurrent - _lastTime < _gapSize) {
80265e6a   Benjamin Renard   Compute interpola...
389
					//Interpolation
ca31a786   Benjamin Renard   Fix other side ef...
390
					double coefT = (float) (_markerPtr->getTargetTime() - _lastTime) / (timeCurrent - _lastTime);
2f932bad   Benjamin Renard   Fix interpolation...
391
					_val = interpolation(_valMoins1, _paramInput.get(index), coefT, _paramInput.getDim1(),_paramInput.getDim2());
80265e6a   Benjamin Renard   Compute interpola...
392
				}
ca31a786   Benjamin Renard   Fix other side ef...
393
				else {
fbe3c2bb   Benjamin Renard   First commit
394
					_val << NotANumber();
ca31a786   Benjamin Renard   Fix other side ef...
395
				}
fbe3c2bb   Benjamin Renard   First commit
396
				_paramOutput->push(_val);
ca31a786   Benjamin Renard   Fix other side ef...
397
398
				_paramDataOutput->pushTime(_markerPtr->getTargetTime());
				_mem.clear();
fbe3c2bb   Benjamin Renard   First commit
399

ca31a786   Benjamin Renard   Fix other side ef...
400
				if(!_markerPtr->nextTarget())
fbe3c2bb   Benjamin Renard   First commit
401
					break;
fbe3c2bb   Benjamin Renard   First commit
402

ca31a786   Benjamin Renard   Fix other side ef...
403
				while (_markerPtr->getMarkerMaxTime() < timeCurrent)
fbe3c2bb   Benjamin Renard   First commit
404
				{
2f932bad   Benjamin Renard   Fix interpolation...
405
					if (timeCurrent - _lastTime < _gapSize) {
80265e6a   Benjamin Renard   Compute interpola...
406
						//Interpolation
ca31a786   Benjamin Renard   Fix other side ef...
407
						double coefT = (float) (_markerPtr->getTargetTime() - _lastTime) / (timeCurrent - _lastTime);
2f932bad   Benjamin Renard   Fix interpolation...
408
						_val = interpolation(_valMoins1, _paramInput.get(index), coefT, _paramInput.getDim1(),_paramInput.getDim2());
80265e6a   Benjamin Renard   Compute interpola...
409
410
411
					}
					else
						_val << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
412
					_paramOutput->push(_val);
ca31a786   Benjamin Renard   Fix other side ef...
413
414
					_paramDataOutput->pushTime(_markerPtr->getTargetTime());
					if(!_markerPtr->nextTarget())
fbe3c2bb   Benjamin Renard   First commit
415
						break;
fbe3c2bb   Benjamin Renard   First commit
416
				}
ca31a786   Benjamin Renard   Fix other side ef...
417
418
419
				if ((timeCurrent >= _markerPtr->getMarkerMinTime()) && (timeCurrent < _markerPtr->getMarkerMaxTime())) {
					_mem.push_back(_paramInput.get(index));
				}
fbe3c2bb   Benjamin Renard   First commit
420
			}
80265e6a   Benjamin Renard   Compute interpola...
421
422
			_lastTime = _paramInput.getTime(index);
			_valMoins1 = _paramInput.get(index);
fbe3c2bb   Benjamin Renard   First commit
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
		}
	}

	/**
	 * @brief compute and write new value in interpolation mode
	 */
	void writeInterpolation(ParamDataIndexInfo &pParamDataIndexInfo) {
		for (unsigned int index = pParamDataIndexInfo._startIndex;
				index
						< pParamDataIndexInfo._startIndex
								+ pParamDataIndexInfo._nbDataToProcess;
				index++) {

			double timeCurrent = _paramInput.getTime(index);

fbe3c2bb   Benjamin Renard   First commit
438
439
			//Fix  side-off effects - Left boundary - CONSTANTS
			if (_leftEffect) {
ca31a786   Benjamin Renard   Fix other side ef...
440
441
				while (_markerPtr->getTargetTime() <= timeCurrent) {
					if (abs(timeCurrent - _markerPtr->getTargetTime()) <= _gapSize) {
fbe3c2bb   Benjamin Renard   First commit
442
443
444
445
						_val = _paramInput.get(index);
					} else {
						_val << NotANumber();
					}
ca31a786   Benjamin Renard   Fix other side ef...
446
					_paramDataOutput->pushTime(_markerPtr->getTargetTime());
fbe3c2bb   Benjamin Renard   First commit
447
					_paramOutput->push(_val);
ca31a786   Benjamin Renard   Fix other side ef...
448
					if (!_markerPtr->nextTarget())
fbe3c2bb   Benjamin Renard   First commit
449
450
451
452
						break;
				}
				_leftEffect = false;
			} else {
ca31a786   Benjamin Renard   Fix other side ef...
453
454
				if (_markerPtr->getTargetTime() <= timeCurrent) {
					while (_markerPtr->getTargetTime() <= timeCurrent) {
fbe3c2bb   Benjamin Renard   First commit
455
456
						double deltaT = (float) (timeCurrent - _lastTime);
						if (deltaT <= _gapSize) {
ca31a786   Benjamin Renard   Fix other side ef...
457
							double coefT = (float) (_markerPtr->getTargetTime() - _lastTime) / deltaT;
2f932bad   Benjamin Renard   Fix interpolation...
458
							_val = interpolation(_valMoins1, _paramInput.get(index), coefT, _paramInput.getDim1(),_paramInput.getDim2());
fbe3c2bb   Benjamin Renard   First commit
459
460
461
						} else {
							_val << NotANumber();
						}
ca31a786   Benjamin Renard   Fix other side ef...
462
						_paramDataOutput->pushTime(_markerPtr->getTargetTime());
fbe3c2bb   Benjamin Renard   First commit
463
						_paramOutput->push(_val);
ca31a786   Benjamin Renard   Fix other side ef...
464
						if (!_markerPtr->nextTarget())
fbe3c2bb   Benjamin Renard   First commit
465
466
467
468
							break;
					}
				}
			}
fbe3c2bb   Benjamin Renard   First commit
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
			_lastTime = _paramInput.getTime(index);
			_valMoins1 = _paramInput.get(index);
		}
	}

	/**
	 * @overload ResamplingAbstract::ResamplingAbstract::init()
	 */
	virtual void init() {
		//Compute Sampling mode
		if (_useNearestValue)
		{
			_samplingMode = NEARESTVALUE;
			_nearestValue = ElementType();
			_nearestTime << NotANumber();
		}
		else
		{
			float minSampling = _paramInput.getMinSampling();

			if ((minSampling * 2) < getSampling()) {
				_samplingMode = AVERAGE;
			}
		}

		_isNewInt = true;
	}

	/**
	 * @overload ResamplingAbstract::reset(double pStartTime, double pTimeInt)
	 */
a6490f4d   Benjamin Renard   Do not throw an e...
500
	virtual void resetResampling() {
fbe3c2bb   Benjamin Renard   First commit
501
502
503
		if (_currentTimeInterval == _timeIntervalList->end())
			return;

fbe3c2bb   Benjamin Renard   First commit
504
505
		// Get next TimeInteval;
		++_currentTimeInterval;
ca31a786   Benjamin Renard   Fix other side ef...
506
		++_currentTimeIntervalIndex;
fbe3c2bb   Benjamin Renard   First commit
507
508
509
510
511
512
513
514
515
516
		_samplingMode = INTERPOLATION;
		_lastTime = 0;
		_leftEffect = true;
		_valMoins1 = ElementType();
		_val = ElementType();
		_mem.clear();

		init();
	}

ca31a786   Benjamin Renard   Fix other side ef...
517
	virtual double getSampling() = 0;
fbe3c2bb   Benjamin Renard   First commit
518
519
520
521
522
523
524
525

	/**
	 * @brief sampling mode
	 */
	enum SamplingMode {
		INTERPOLATION, AVERAGE, NEARESTVALUE
	};

fbe3c2bb   Benjamin Renard   First commit
526
527
528
529
530
531
532
private:

	/**
	 * @brief stored val to compute average in average mode
	 */
	ListValue _mem;

ca31a786   Benjamin Renard   Fix other side ef...
533
534
	MarkerAbstract* _markerPtr;
	
fbe3c2bb   Benjamin Renard   First commit
535
536
537
538
	TimeIntervalListSPtr _timeIntervalList;

	TimeIntervalList::iterator _currentTimeInterval;

ca31a786   Benjamin Renard   Fix other side ef...
539
	int _currentTimeIntervalIndex;
fbe3c2bb   Benjamin Renard   First commit
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

	/**<
	 * @brief current sampling mode
	 */
	SamplingMode _samplingMode;

	/**<
	 * @brief last computed value
	 */
	ElementType _val;

	/**
	 * @brief nearest time
	 */
	double _nearestTime;

	/**
	 * @brief nearest value
	 */
	ElementType _nearestValue;

	/**<
	 * @brief last input value
	 */
	ElementType _valMoins1;
	/**<
	 * @brief time of  last input value
	 */
	double _lastTime;

	/**
	 * @brief left effect indicator in interpolation mode
	 */
	bool _leftEffect;

	/**
	 * @brief real ParamData Output
	 */
	TParamData *_paramOutput;

	/**
	 * @brief real ParamData Input
	 */
	TParamData& _paramInput;

	/**
	 * @brief gap size for input data
	 */
	double _gapSize;
fbe3c2bb   Benjamin Renard   First commit
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
}
;

/**
 * Resampling implementation with a given sampling time , depand of paramData type
 */
template<typename TParamData>
class ResamplingWithSamplingTime : public ResamplingSimpleAbstract<TParamData> {

public:
	/**
	 * @brief  Constructor
	 */
	ResamplingWithSamplingTime(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList,
			double sampling, double gapSize, TParamData &param, bool  pUseNearestValue) :
ca31a786   Benjamin Renard   Fix other side ef...
604
605
			ResamplingSimpleAbstract<TParamData>(pProcess, &_marker, pTimeIntervalList,
						gapSize, param, pUseNearestValue), _samplingTime(sampling) {
fbe3c2bb   Benjamin Renard   First commit
606
607
608
609
610
611
612
613
	}

	/**
	 * @brief  Destructor
	 */
	virtual ~ResamplingWithSamplingTime() {
	}

ca31a786   Benjamin Renard   Fix other side ef...
614
615
	double getSampling() {
		return _samplingTime;
fbe3c2bb   Benjamin Renard   First commit
616
617
618
	}

private:
ca31a786   Benjamin Renard   Fix other side ef...
619
620
621
622
	MarkerSampling _marker;

	double _samplingTime;
};
fbe3c2bb   Benjamin Renard   First commit
623
624
625
626
627
628
629
630
631
632
633
634
635
636


/**
 * Resampling implementation with a given reference parameter for time definition , depand of paramData type
 */
template<typename TParamData>
class ResamplingWithRefParam: public ResamplingSimpleAbstract<TParamData> {

public:
	/**
	 * @brief  Constructor
	 */
	ResamplingWithRefParam(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList,
			TParamData &param, ParamData &refParamData, double gapSize, bool  pUseNearestValue) :
ca31a786   Benjamin Renard   Fix other side ef...
637
638
639
			ResamplingSimpleAbstract<TParamData>(pProcess, &_marker, pTimeIntervalList,
					gapSize, param, pUseNearestValue), _refParamData(refParamData) {
		_marker.setRefParamData(&refParamData);
fbe3c2bb   Benjamin Renard   First commit
640
641
642
643
644
645
646
647
	}

	/**
	 * @brief  Destructor
	 */
	virtual ~ResamplingWithRefParam() {
	}

ca31a786   Benjamin Renard   Fix other side ef...
648
	double getSampling() {
fbe3c2bb   Benjamin Renard   First commit
649
		return _refParamData.getMinSampling();
ca31a786   Benjamin Renard   Fix other side ef...
650
        }
fbe3c2bb   Benjamin Renard   First commit
651
652

private:
ca31a786   Benjamin Renard   Fix other side ef...
653
	MarkerRefParam _marker;
fbe3c2bb   Benjamin Renard   First commit
654

fbe3c2bb   Benjamin Renard   First commit
655
	ParamData& _refParamData;
ca31a786   Benjamin Renard   Fix other side ef...
656
};
fbe3c2bb   Benjamin Renard   First commit
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056

/**
 * create the real Resampling implementation with visitor design pattern
 */
class CreateResampling: public VisitorOfParamData {
public:
	enum ResamplingType {
		RT_WITHSAMPLINGTIME,
		RT_WITHREFTIMELIST
	};

	/**
	 * @brief constructor
	 */
	CreateResampling(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList,
			double sampling, double gapSize, ParamData &paramData, bool useNearestValue) :
			_type(ResamplingType::RT_WITHSAMPLINGTIME),
			_process(pProcess), _paramData(paramData), _resampling(NULL), _timeIntervalList(
					pTimeIntervalList), _sampling(sampling), _gapSize(
					gapSize), _refParamData(paramData), _useNearestValue(useNearestValue) {
		_paramData.accept(*this);
	}

	/**
	 * @brief constructor
	 */
	CreateResampling(Process& pProcess, TimeIntervalListSPtr pTimeIntervalList,
			ParamData &paramData, ParamData& refParamData, double gapSize, bool useNearestValue) :
			_type(ResamplingType::RT_WITHREFTIMELIST),
			_process(pProcess), _paramData(paramData), _resampling(NULL), _timeIntervalList(
					pTimeIntervalList), _sampling(0.), _gapSize(
					gapSize),_refParamData(refParamData), _useNearestValue(useNearestValue) {
		_paramData.accept(*this);
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireShort *)
	 */
	virtual void visit(ParamDataScalaireShort *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataScalaireShort>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataScalaireShort &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataScalaireShort>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataScalaireShort &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireFloat *)
	 */
	virtual void visit(ParamDataScalaireFloat *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataScalaireFloat>(_process,
							_timeIntervalList, _sampling, _gapSize,
							dynamic_cast<ParamDataScalaireFloat &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataScalaireFloat>(_process,
							_timeIntervalList,
							dynamic_cast<ParamDataScalaireFloat &>(_paramData),
							_refParamData,
							_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireDouble *)
	 */
	virtual void visit(ParamDataScalaireDouble *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataScalaireDouble>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataScalaireDouble &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataScalaireDouble>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataScalaireDouble &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireLongDouble *)
	 */
	virtual void visit(ParamDataScalaireLongDouble *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataScalaireLongDouble>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataScalaireLongDouble &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataScalaireLongDouble>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataScalaireLongDouble &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataScalaireInt *)
	 */
	virtual void visit(ParamDataScalaireInt *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataScalaireInt>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataScalaireInt &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataScalaireInt>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataScalaireInt &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataLogicalData *)
	 */
	virtual void visit(ParamDataLogicalData *) {
		BOOST_THROW_EXCEPTION(
				AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN)
						<< AMDA::ex_msg(
								"CreateResampling operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DShort *)
	 */
	virtual void visit(ParamDataTab1DShort *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab1DShort>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataTab1DShort &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab1DShort>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab1DShort &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DFloat *)
	 */
	virtual void visit(ParamDataTab1DFloat *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab1DFloat>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataTab1DFloat &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab1DFloat>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab1DFloat &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DDouble *)
	 */
	virtual void visit(ParamDataTab1DDouble *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab1DDouble>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataTab1DDouble &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab1DDouble>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab1DDouble &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DLongDouble *)
	 */
	virtual void visit(ParamDataTab1DLongDouble *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab1DLongDouble>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataTab1DLongDouble &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab1DLongDouble>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab1DLongDouble &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DInt *)
	 */
	virtual void visit(ParamDataTab1DInt *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab1DInt>(_process,
				_timeIntervalList, _sampling, _gapSize,
				dynamic_cast<ParamDataTab1DInt &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab1DInt>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab1DInt &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab1DLogicalData *)
	 */
	virtual void visit(ParamDataTab1DLogicalData *) {
		BOOST_THROW_EXCEPTION(
				AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN)
						<< AMDA::ex_msg(
								"CreateResampling operation not supported"));
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DShort *)
	 */
	virtual void visit(ParamDataTab2DShort *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab2DShort>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataTab2DShort &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab2DShort>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab2DShort &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DFloat *)
	 */
	virtual void visit(ParamDataTab2DFloat *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab2DFloat>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataTab2DFloat &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab2DFloat>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab2DFloat &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DDouble *)
	 */
	virtual void visit(ParamDataTab2DDouble *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab2DDouble>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataTab2DDouble &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab2DDouble>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab2DDouble &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DLongDouble *)
	 */
	virtual void visit(ParamDataTab2DLongDouble *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab2DLongDouble>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataTab2DLongDouble &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab2DLongDouble>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab2DLongDouble &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DInt *)
	 */
	virtual void visit(ParamDataTab2DInt *) {
		switch (_type)
		{
		case ResamplingType::RT_WITHSAMPLINGTIME :
			_resampling = new ResamplingWithSamplingTime<ParamDataTab2DInt>(_process,
					_timeIntervalList, _sampling, _gapSize,
					dynamic_cast<ParamDataTab2DInt &>(_paramData), _useNearestValue);
			break;
		case ResamplingType::RT_WITHREFTIMELIST :
			_resampling = new ResamplingWithRefParam<ParamDataTab2DInt>(_process,
				_timeIntervalList,
				dynamic_cast<ParamDataTab2DInt &>(_paramData),
				_refParamData,
				_gapSize, _useNearestValue);
			break;
		}
	}

	/**
	 * @overload VisitorOfParamData::visit(ParamDataTab2DLogicalData *)
	 */
	virtual void visit(ParamDataTab2DLogicalData *) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("ParamDataTab2DLogicalData data not supported"));
	}

	/**
	 * @brief Good resampling getter
	 */
	ResamplingAbstract* getResampling() const {
		return _resampling;
	}
private:
	ResamplingType _type;
	Process &_process;
	ParamData &_paramData;
	ResamplingAbstract *_resampling;
	TimeIntervalListSPtr _timeIntervalList;
	double _sampling;
	double _gapSize;
	ParamData& _refParamData;
	bool _useNearestValue;
};

} /* Resampling */
} /* AMDA */
} /* Parameters */

#endif /* Resampling_HH_ */