]> Creatis software - clitk.git/blob - common/clitkListOfPair.cxx
9b7bb77244cf81c3258a5df335d1ccad89e5da76
[clitk.git] / common / clitkListOfPair.cxx
1 /*------------------------------------------------------------------------=
2                                                                                 
3   Program:   clitk
4   Language:  C++
5                                                                                 
6   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
7   l'Image). All rights reserved. See Doc/License.txt or
8   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
9                                                                                 
10   This software is distributed WITHOUT ANY WARRANTY; without even
11   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   PURPOSE.  See the above copyright notices for more information.
13                                                                              
14   ------------------------------------------------------------------------=*/
15
16
17 #ifndef CLITKLISTOFPAIR_CXX
18 #define CLITKLISTOFPAIR_CXX
19
20 /**
21    ------------------------------------------------=
22    * @file   clitkListOfPair.cxx
23    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24    * @date   27 Feb 2007 09:55:56
25    * 
26    * @brief  
27    * 
28    * 
29    ------------------------------------------------=*/
30
31 #include "clitkListOfPair.h"
32
33 //--------------------------------------------------------------------
34 double clitk::convertValue(double v, 
35                            const std::multimap<double, double> & conversionTable, 
36                            bool linear) {
37   std::multimap<double, double>::const_iterator i;
38   i = conversionTable.lower_bound(v);  
39   if (i == conversionTable.end()) {
40     std::cerr << "The value " << v << " is out of the table" << std::endl;
41     exit(0);
42   }
43   
44   double v2 = i->first;
45   double p2 = i->second;
46   if (i != conversionTable.begin()) i--;
47   double v1 = i->first;
48   double p1 = i->second;
49
50   // interpol
51   if (!linear) {
52     if ((v-v1) > (v2-v)) return p2;
53     else return p1;
54   }
55   else {
56     double w = (v-v1)/(v2-v1);
57     return p1*(1.0-w)+w*p2;
58   }
59 }
60 //--------------------------------------------------------------------
61
62 #endif /* end #define CLITKLISTOFPAIR_CXX */
63