| 1 | // toiinterpolator.cc
 | 
|---|
| 2 | // Eric Aubourg         CEA/DAPNIA/SPP   octobre 1999
 | 
|---|
| 3 | 
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #include "toiinterpolator.h"
 | 
|---|
| 6 | #include "toimanager.h"
 | 
|---|
| 7 | #include "archexc.h"
 | 
|---|
| 8 | 
 | 
|---|
| 9 | TOIInterpolator::TOIInterpolator() {
 | 
|---|
| 10 | }
 | 
|---|
| 11 | 
 | 
|---|
| 12 | 
 | 
|---|
| 13 | string TOIInterpolator::getName() {
 | 
|---|
| 14 |   return("TOIInterpolator 1.0");
 | 
|---|
| 15 | }
 | 
|---|
| 16 | 
 | 
|---|
| 17 | bool TOIInterpolator::canProduce(TOI const& toi) {
 | 
|---|
| 18 |   // 1. Already in cache ?
 | 
|---|
| 19 |   map<TOI, map<TOI, TOIProducer*> >::const_iterator j = neededTOIs.find(toi);
 | 
|---|
| 20 |   if (j != neededTOIs.end()) return true;
 | 
|---|
| 21 | 
 | 
|---|
| 22 |   // 2. Should have interp
 | 
|---|
| 23 |   if (toi.options.find("interp") == toi.options.end()) return false;
 | 
|---|
| 24 |   
 | 
|---|
| 25 |   // 3. Can get non interp
 | 
|---|
| 26 |   TOI toi2 = toi;
 | 
|---|
| 27 |   toi2.options.erase("interp");
 | 
|---|
| 28 |   TOIProducer* prod = TOIManager::findTOIProducer(toi2);
 | 
|---|
| 29 |   set<string> opts = prod->getAvailOptions(toi2);
 | 
|---|
| 30 |   if (opts.find("interp") != opts.end()) return false; // already handled
 | 
|---|
| 31 |   if (opts.find("repet") != opts.end()) return false;  // not compatible !
 | 
|---|
| 32 |   
 | 
|---|
| 33 |   map<TOI, TOIProducer*> fullInputTOI;
 | 
|---|
| 34 |   fullInputTOI[toi2] = prod;
 | 
|---|
| 35 |   neededTOIs[toi] = fullInputTOI;
 | 
|---|
| 36 |   return true;
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | set<TOI> TOIInterpolator::reqTOIFor(TOI const& toi) {
 | 
|---|
| 40 |   set<TOI> x;
 | 
|---|
| 41 |   if (!canProduce(toi)) return x;
 | 
|---|
| 42 |   x.insert((*neededTOIs[toi].begin()).first);
 | 
|---|
| 43 |   return x;
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | bool TOIInterpolator::canGetValue(long sampleNum, TOI const& toi) {
 | 
|---|
| 47 |   map<TOI, TOIProducer*> & inp = neededTOIs[toi];
 | 
|---|
| 48 |   TOIProducer* prod  = (*inp.begin()).second;
 | 
|---|
| 49 |   TOI const&   inTOI = (*inp.begin()).first;
 | 
|---|
| 50 |   
 | 
|---|
| 51 |   if (prod->canGetValue(sampleNum, inTOI)) return true; // direct
 | 
|---|
| 52 |   if (prod->canGetPrevValue(sampleNum, inTOI) &&
 | 
|---|
| 53 |       prod->canGetNextValue(sampleNum, inTOI)) return true;
 | 
|---|
| 54 |   return false;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | bool TOIInterpolator::canGetValueLater(long sampleNum, TOI const& toi) {
 | 
|---|
| 58 |   map<TOI, TOIProducer*> & inp = neededTOIs[toi];
 | 
|---|
| 59 |   TOIProducer* prod  = (*inp.begin()).second;
 | 
|---|
| 60 |   TOI const&   inTOI = (*inp.begin()).first;
 | 
|---|
| 61 |   
 | 
|---|
| 62 |   if (prod->canGetValue(sampleNum, inTOI)) return false; // direct
 | 
|---|
| 63 |   if (prod->canGetValueLater(sampleNum, inTOI)) return true; // direct
 | 
|---|
| 64 |   if (!prod->canGetPrevValue(sampleNum, inTOI)) return false; // no hope
 | 
|---|
| 65 |   if (prod->canGetNextValue(sampleNum, inTOI)) return false; // can get now
 | 
|---|
| 66 |   return true;
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | double TOIInterpolator::getValue(long sampleNum, TOI const& toi) {
 | 
|---|
| 70 |   map<TOI, TOIProducer*> & inp = neededTOIs[toi];
 | 
|---|
| 71 |   TOIProducer* prod  = (*inp.begin()).second;
 | 
|---|
| 72 |   TOI const&   inTOI = (*inp.begin()).first;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   if (prod->canGetValue(sampleNum, inTOI))
 | 
|---|
| 75 |      return prod->getValue(sampleNum, inTOI); // direct
 | 
|---|
| 76 |      
 | 
|---|
| 77 |   long sn1 = sampleNum;
 | 
|---|
| 78 |   double v1 = prod->getPrevValue(sn1, inTOI);
 | 
|---|
| 79 |   
 | 
|---|
| 80 |   long sn2 = sampleNum;
 | 
|---|
| 81 |   double v2 = prod->getNextValue(sn2, inTOI);
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   if (sn1 == sn2) throw ArchExc("Inconsistence in interp, sn1==sn2");
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   return v1 + (v2-v1) * (sampleNum-sn1) / (sn2-sn1);
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | 
 | 
|---|
| 89 | void TOIInterpolator::propagateLowBound(TOI const& toi, long sampleNum) {
 | 
|---|
| 90 |   CHKPROD
 | 
|---|
| 91 |   map<TOI, TOIProducer*> & inp = neededTOIs[toi];
 | 
|---|
| 92 |   TOIProducer* prod  = (*inp.begin()).second;
 | 
|---|
| 93 |   TOI const&   inTOI = (*inp.begin()).first;
 | 
|---|
| 94 |   if (prod->canGetPrevValue(sampleNum,toi)) {
 | 
|---|
| 95 |     prod->getPrevValue(sampleNum, toi);
 | 
|---|
| 96 |     sampleNum--;
 | 
|---|
| 97 |     prod->wontNeedEarlier(toi, this, sampleNum);
 | 
|---|
| 98 |   }
 | 
|---|
| 99 | }
 | 
|---|
| 100 | 
 | 
|---|