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