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