]> Creatis software - clitk.git/blob - common/clitkListOfPair.cxx
00f4d54867fc3c9b4de1cf72135bc1fb8f3dddbe
[clitk.git] / common / clitkListOfPair.cxx
1 #ifndef CLITKLISTOFPAIR_CXX
2 #define CLITKLISTOFPAIR_CXX
3 /**
4    ------------------------------------------------=
5    * @file   clitkListOfPair.cxx
6    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
7    * @date   27 Feb 2007 09:55:56
8    * 
9    * @brief  
10    * 
11    * 
12    ------------------------------------------------=*/
13
14 #include "clitkListOfPair.h"
15
16 //--------------------------------------------------------------------
17 double clitk::convertValue(double v, 
18                            const std::multimap<double, double> & conversionTable, 
19                            bool linear) {
20   std::multimap<double, double>::const_iterator i;
21   i = conversionTable.lower_bound(v);  
22   if (i == conversionTable.end()) {
23     std::cerr << "The value " << v << " is out of the table" << std::endl;
24     exit(0);
25   }
26   
27   double v2 = i->first;
28   double p2 = i->second;
29   if (i != conversionTable.begin()) i--;
30   double v1 = i->first;
31   double p1 = i->second;
32
33   // interpol
34   if (!linear) {
35     if ((v-v1) > (v2-v)) return p2;
36     else return p1;
37   }
38   else {
39     double w = (v-v1)/(v2-v1);
40     return p1*(1.0-w)+w*p2;
41   }
42 }
43 //--------------------------------------------------------------------
44
45 #endif /* end #define CLITKLISTOFPAIR_CXX */
46