| 1 | // toiinterpolator.cc | 
|---|
| 2 | // Eric Aubourg         CEA/DAPNIA/SPP   juillet 1999 | 
|---|
| 3 |  | 
|---|
| 4 |  | 
|---|
| 5 | #include "toiinterpolator.h" | 
|---|
| 6 |  | 
|---|
| 7 | void TOIInterpolator::enterValue(double value, long sample) | 
|---|
| 8 | { | 
|---|
| 9 | // $CHECK$ sample > back.sample | 
|---|
| 10 | if (!hist.empty() && sample == hist.back().t) return; // pour eviter /0 | 
|---|
| 11 | hist.push_back(pair(value, sample)); | 
|---|
| 12 | } | 
|---|
| 13 |  | 
|---|
| 14 | bool TOIInterpolator::needMoreDataFor(long sample) | 
|---|
| 15 | { | 
|---|
| 16 | if (hist.empty()) return true; | 
|---|
| 17 | int xx = hist.back().t; | 
|---|
| 18 | return (hist.empty() || sample > xx); | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | bool TOIInterpolator::canGet(long sample) | 
|---|
| 22 | { | 
|---|
| 23 | return (!hist.empty() && (sample >= hist.front().t && sample <= hist.back().t)); | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | double TOIInterpolator::getEValue(long sample) // can forget before sample | 
|---|
| 27 | { | 
|---|
| 28 | if (!canGet(sample)) return -9.e99;   // user should have checked before | 
|---|
| 29 | deque<pair>::iterator i = hist.begin(); | 
|---|
| 30 | while ((*i).t < sample) i++; | 
|---|
| 31 | if ((*i).t > sample) i--; | 
|---|
| 32 | double value = (*i).val; | 
|---|
| 33 | if (i != hist.begin()) { | 
|---|
| 34 | i--; | 
|---|
| 35 | hist.erase(hist.begin(), i); | 
|---|
| 36 | } | 
|---|
| 37 | return value; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | double TOIInterpolator::getIValue(long sample) // can forget before sample | 
|---|
| 41 | { | 
|---|
| 42 | if (!canGet(sample)) return -9.e99;   // user should have checked before | 
|---|
| 43 |  | 
|---|
| 44 | deque<pair>::iterator i = hist.begin(); | 
|---|
| 45 | while ((*i).t < sample) i++; | 
|---|
| 46 | double value; | 
|---|
| 47 | if ((*i).t == sample) { | 
|---|
| 48 | value = (*i).val; | 
|---|
| 49 | } else { | 
|---|
| 50 | long samp2 = (*i).t; | 
|---|
| 51 | double val2 = (*i).val; | 
|---|
| 52 | i--; | 
|---|
| 53 | long samp1 = (*i).t; | 
|---|
| 54 | double val1 = (*i).val; | 
|---|
| 55 | value = val1 + (val2-val1)*(sample-samp1)/(samp2-samp1); | 
|---|
| 56 | } | 
|---|
| 57 | if (i != hist.begin()) { | 
|---|
| 58 | i--; | 
|---|
| 59 | hist.erase(hist.begin(), i); | 
|---|
| 60 | } | 
|---|
| 61 | return value; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | bool TOIInterpolator::isNewValue(long sample) | 
|---|
| 65 | { | 
|---|
| 66 | deque<pair>::iterator i = hist.begin(); | 
|---|
| 67 | while (i!=hist.end()) { | 
|---|
| 68 | if ((*i).t == sample) return true; | 
|---|
| 69 | if ((*i).t > sample) return false; | 
|---|
| 70 | i++; | 
|---|
| 71 | } | 
|---|
| 72 | return false; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | long TOIInterpolator::nextSample(long sampleMin) | 
|---|
| 77 | { | 
|---|
| 78 | for (deque<pair>::iterator i = hist.begin(); i<hist.end(); i++) | 
|---|
| 79 | if ((*i).t >= sampleMin) return (*i).t; | 
|---|
| 80 | return -1; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|