]> Creatis software - clitk.git/blob - common/clitkCommon.txx
- correct bug: string pixeltype is different in ITK and VTK
[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   //  http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
116   // and
117   // itkImageIOBase.cxx
118   const std::type_info & PixType = typeid(TPixel);
119   std::string pixelName;
120   if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
121   else if (PixType == typeid(signed char)) pixelName = "signed_char";
122   else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
123   else if (PixType == typeid(short)) pixelName = "short";
124   else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
125   else if (PixType == typeid(int)) pixelName = "int";
126   else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
127   else if (PixType == typeid(float)) pixelName = "float";
128   else if (PixType == typeid(double)) pixelName = "double";
129   else pixelName = PixType.name();
130   return pixelName;
131 }
132 //--------------------------------------------------------------------
133
134 //--------------------------------------------------------------------
135 template<class ImageType>
136 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) {
137   output->SetRegions(input->GetLargestPossibleRegion());
138   output->SetOrigin(input->GetOrigin());
139   output->SetSpacing(input->GetSpacing());
140   output->Allocate();
141   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType; 
142   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
143   pi.GoToBegin();  
144   typedef itk::ImageRegionIterator<ImageType> IteratorType; 
145   IteratorType po(output,input->GetLargestPossibleRegion());
146   po.GoToBegin(); 
147   while (!pi.IsAtEnd()) {
148     po.Set(pi.Get());
149     ++pi;
150     ++po;
151   }
152 }
153 //--------------------------------------------------------------------
154
155 #endif /* end #define CLITKCOMMON_TXX */
156