]> Creatis software - clitk.git/blob - common/clitkCommon.txx
upport
[clitk.git] / common / clitkCommon.txx
1 /*-------------------------------------------------------------------------
2                                                                                 
3   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
4   l'Image). All rights reserved. See Doc/License.txt or
5   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
6   
7   This software is distributed WITHOUT ANY WARRANTY; without even
8   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9   PURPOSE.  See the above copyright notices for more information.
10   
11   -------------------------------------------------------------------------*/
12
13 #ifndef CLITKCOMMON_TXX
14 #define CLITKCOMMON_TXX
15
16 /**
17    -------------------------------------------------
18    * @file   clitkCommon.txx
19    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
20    * @date   18 May 2006
21    * 
22    -------------------------------------------------*/
23
24 //--------------------------------------------------------------------
25 // Convert float, double ... to string
26 template<class T>
27 std::string toString(const T & t) {
28   std::ostringstream myStream;
29   myStream << t << std::flush;
30   return(myStream.str());
31 }
32 //--------------------------------------------------------------------
33
34 //--------------------------------------------------------------------
35 // Convert float*, double* ... to string
36 template<class T>
37 std::string toStringVector(const T * t, const int n) {
38   std::ostringstream myStream;
39   for(int i=0; i<n-1; i++)
40     myStream << clitk::toString<T>(t[i]) << " ";
41   myStream << clitk::toString<T>(t[n-1]) << std::flush;
42   return(myStream.str());
43 }
44 //--------------------------------------------------------------------
45
46 //--------------------------------------------------------------------
47 // Convert float*, double* ... to string
48 template<class T>
49 std::string toStringVector(const T & t, const int n) {
50   std::ostringstream myStream;
51   for(int i=0; i<n-1; i++)
52     myStream << t[i] << " ";
53   myStream << t[n-1] << std::flush;
54   return(myStream.str());
55 }
56 //--------------------------------------------------------------------
57
58 //--------------------------------------------------------------------
59 // Convert float*, double* ... to string
60 template<class T>
61 std::string toStringVector(const std::vector<T> & t) {
62   return toStringVector(&t[0], t.size());
63 }
64 //--------------------------------------------------------------------
65
66 //--------------------------------------------------------------------
67 // Convert a pixel type to another (downcast)
68 template<class TPixelUp, class TPixelDown>
69 TPixelDown PixelTypeDownCast(const TPixelUp & x) {
70   return (TPixelDown)lrint(x);
71 }
72 //--------------------------------------------------------------------
73
74 //--------------------------------------------------------------------
75 template<class Type>
76 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
77   vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
78   bool operator()(int x, int y) { 
79     return (vect[x] < vect[y]);
80   }
81   const std::vector<Type> & vect;
82 };
83 //--------------------------------------------------------------------
84
85 //--------------------------------------------------------------------
86 template<class Type>
87 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
88   vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
89   bool operator()(int x, int y) { 
90     return (vect[x] > vect[y]);
91   }
92   const std::vector<Type> & vect;
93 };
94 //--------------------------------------------------------------------
95
96 //--------------------------------------------------------------------
97 template<class Type>
98 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing) {
99   index.resize(toSort.size());  
100   for(unsigned int i=0; i<index.size(); i++) index[i] = i;
101   if (increasing) 
102     std::sort(index.begin(), 
103               index.end(), 
104               vectorComparisonLowerThan<double>(toSort));
105   else 
106     std::sort(index.begin(), 
107               index.end(), 
108               vectorComparisonGreaterThan<double>(toSort));
109 }
110 //--------------------------------------------------------------------
111
112 //--------------------------------------------------------------------
113 template<class TPixel>
114 std::string GetTypeAsString() {
115   const std::type_info & PixType = typeid(TPixel);
116   std::string pixelName;
117   if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
118   else if (PixType == typeid(signed char)) pixelName = "signed_char";
119   else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
120   else if (PixType == typeid(short)) pixelName = "short";
121   else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
122   else if (PixType == typeid(int)) pixelName = "int";
123   else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
124   else if (PixType == typeid(float)) pixelName = "float";
125   else if (PixType == typeid(double)) pixelName = "double";
126   else pixelName = PixType.name();
127   return pixelName;
128 }
129 //--------------------------------------------------------------------
130
131 //--------------------------------------------------------------------
132 template<class ImageType>
133 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) {
134   output->SetRegions(input->GetLargestPossibleRegion());
135   output->SetSpacing(input->GetSpacing());
136   output->Allocate();
137   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType; 
138   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
139   pi.GoToBegin();  
140   typedef itk::ImageRegionIterator<ImageType> IteratorType; 
141   IteratorType po(output,input->GetLargestPossibleRegion());
142   po.GoToBegin(); 
143   while (!pi.IsAtEnd()) {
144     po.Set(pi.Get());
145     ++pi;
146     ++po;
147   }
148 }
149 //--------------------------------------------------------------------
150
151 #endif /* end #define CLITKCOMMON_TXX */
152