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