]> Creatis software - clitk.git/blob - common/clitkCommon.txx
error are now handled by exception
[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 // Utility functions for text file parsing (author: joel schaerer)
23
24 //--------------------------------------------------------------------
25 template<class ElementType>
26 ElementType parse_value(std::string str)
27 {
28   std::istringstream parser(str);
29   ElementType value;
30   parser >> value;
31   assert(!parser.fail());
32   return value;
33 }
34 //--------------------------------------------------------------------
35
36
37 //--------------------------------------------------------------------
38 template<class ElementType>
39 std::vector<ElementType> parse_string(std::string str,char delim)
40 {
41   std::istringstream ss(str);
42   std::string token;
43   std::vector<ElementType> result;
44   while (std::getline(ss,token,delim)) {
45     result.push_back(parse_value<ElementType>(token));
46   }
47   return result;
48 }
49 //--------------------------------------------------------------------
50
51
52 //--------------------------------------------------------------------
53 template<class ElementType>
54 std::vector<std::vector<ElementType> > parse_file(const char* filename,char delim)
55 {
56   std::ifstream fs(filename);
57   std::string line;
58   std::vector<std::vector<ElementType> > result;
59   while (std::getline(fs,line)) {
60     if (line[0] != '#') //skip comments
61       result.push_back(parse_string<ElementType>(line,delim));
62   }
63   return result;
64 }
65 //--------------------------------------------------------------------
66
67
68 //--------------------------------------------------------------------
69 // Convert float, double ... to string
70 template<class T>
71 std::string toString(const T & t)
72 {
73   std::ostringstream myStream;
74   myStream << t << std::flush;
75   return(myStream.str());
76 }
77 //--------------------------------------------------------------------
78
79 //--------------------------------------------------------------------
80 // Convert float*, double* ... to string
81 template<class T>
82 std::string toStringVector(const T * t, const int n)
83 {
84   std::ostringstream myStream;
85   for(int i=0; i<n-1; i++)
86     myStream << clitk::toString<T>(t[i]) << " ";
87   myStream << clitk::toString<T>(t[n-1]) << std::flush;
88   return(myStream.str());
89 }
90 //--------------------------------------------------------------------
91
92 //--------------------------------------------------------------------
93 // Convert float*, double* ... to string
94 template<class T>
95 std::string toStringVector(const T & t, const int n)
96 {
97   std::ostringstream myStream;
98   for(int i=0; i<n-1; i++)
99     myStream << t[i] << " ";
100   myStream << t[n-1] << std::flush;
101   return(myStream.str());
102 }
103 //--------------------------------------------------------------------
104
105 //--------------------------------------------------------------------
106 // Convert float*, double* ... to string
107 template<class T>
108 std::string toStringVector(const std::vector<T> & t)
109 {
110   return toStringVector(&t[0], t.size());
111 }
112 //--------------------------------------------------------------------
113
114 //--------------------------------------------------------------------
115 // Convert a pixel type to another (downcast)
116 template<class TPixelUp, class TPixelDown>
117 TPixelDown PixelTypeDownCast(const TPixelUp & x)
118 {
119   return (TPixelDown)lrint(x);
120 }
121 //--------------------------------------------------------------------
122
123 //--------------------------------------------------------------------
124 template<class Type>
125 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
126   vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
127   bool operator()(int x, int y) {
128     return (vect[x] < vect[y]);
129   }
130   const std::vector<Type> & vect;
131 };
132 //--------------------------------------------------------------------
133
134 //--------------------------------------------------------------------
135 template<class Type>
136 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
137   vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
138   bool operator()(int x, int y) {
139     return (vect[x] > vect[y]);
140   }
141   const std::vector<Type> & vect;
142 };
143 //--------------------------------------------------------------------
144
145 //--------------------------------------------------------------------
146 template<class Type>
147 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
148 {
149   index.resize(toSort.size());
150   for(unsigned int i=0; i<index.size(); i++) index[i] = i;
151   if (increasing)
152     std::sort(index.begin(),
153               index.end(),
154               vectorComparisonLowerThan<double>(toSort));
155   else
156     std::sort(index.begin(),
157               index.end(),
158               vectorComparisonGreaterThan<double>(toSort));
159 }
160 //--------------------------------------------------------------------
161
162 //--------------------------------------------------------------------
163 template<class TPixel>
164 std::string GetTypeAsString()
165 {
166   //  http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
167   // and
168   // itkImageIOBase.cxx
169   const std::type_info & PixType = typeid(TPixel);
170   std::string pixelName;
171   if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
172   else if (PixType == typeid(signed char)) pixelName = "signed_char";
173   else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
174   else if (PixType == typeid(short)) pixelName = "short";
175   else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
176   else if (PixType == typeid(int)) pixelName = "int";
177   else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
178   else if (PixType == typeid(float)) pixelName = "float";
179   else if (PixType == typeid(double)) pixelName = "double";
180   else pixelName = PixType.name();
181   return pixelName;
182 }
183 //--------------------------------------------------------------------
184
185 //--------------------------------------------------------------------
186 template<class ImageType>
187 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
188 {
189   output->SetRegions(input->GetLargestPossibleRegion());
190   output->SetOrigin(input->GetOrigin());
191   output->SetSpacing(input->GetSpacing());
192   output->Allocate();
193   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
194   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
195   pi.GoToBegin();
196   typedef itk::ImageRegionIterator<ImageType> IteratorType;
197   IteratorType po(output,input->GetLargestPossibleRegion());
198   po.GoToBegin();
199   while (!pi.IsAtEnd()) {
200     po.Set(pi.Get());
201     ++pi;
202     ++po;
203   }
204 }
205 //--------------------------------------------------------------------
206
207 #endif /* end #define CLITKCOMMON_TXX */
208