]> Creatis software - clitk.git/blob - common/clitkCommon.txx
Debug RTStruct conversion with empty struc
[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://www.centreleonberard.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 //--------------------------------------------------------------------
81 // http://www.codeguru.com/forum/showthread.php?t=231054
82 template <class T>
83 bool fromString(T& t, const std::string& s, 
84                 std::ios_base& (*f)(std::ios_base&))
85 {
86   std::istringstream iss(s);
87   return !(iss >> f >> t).fail();
88 }
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 << clitk::toString<T>(t[i]) << " ";
100   myStream << clitk::toString<T>(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 T & t, const int n)
109 {
110   std::ostringstream myStream;
111   for(int i=0; i<n-1; i++)
112     myStream << t[i] << " ";
113   myStream << t[n-1] << std::flush;
114   return(myStream.str());
115 }
116 //--------------------------------------------------------------------
117
118 //--------------------------------------------------------------------
119 // Convert float*, double* ... to string
120 template<class T>
121 std::string toStringVector(const std::vector<T> & t)
122 {
123   return toStringVector(&t[0], t.size());
124 }
125 //--------------------------------------------------------------------
126
127 //--------------------------------------------------------------------
128 // Convert a pixel type to another (downcast)
129 template<class TPixelUp, class TPixelDown>
130 TPixelDown PixelTypeDownCast(const TPixelUp & x)
131 {
132   return (TPixelDown)lrint(x);
133 }
134 //--------------------------------------------------------------------
135
136 //--------------------------------------------------------------------
137 template<class Type>
138 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
139   vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
140   bool operator()(int x, int y) {
141     return (vect[x] < vect[y]);
142   }
143   const std::vector<Type> & vect;
144 };
145 //--------------------------------------------------------------------
146
147 //--------------------------------------------------------------------
148 template<class Type>
149 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
150   vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
151   bool operator()(int x, int y) {
152     return (vect[x] > vect[y]);
153   }
154   const std::vector<Type> & vect;
155 };
156 //--------------------------------------------------------------------
157
158 //--------------------------------------------------------------------
159 template<class Type>
160 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
161 {
162   index.resize(toSort.size());
163   for(unsigned int i=0; i<index.size(); i++) index[i] = i;
164   if (increasing)
165     std::sort(index.begin(),
166               index.end(),
167               vectorComparisonLowerThan<double>(toSort));
168   else
169     std::sort(index.begin(),
170               index.end(),
171               vectorComparisonGreaterThan<double>(toSort));
172 }
173 //--------------------------------------------------------------------
174
175 //--------------------------------------------------------------------
176 template<class TPixel>
177 std::string GetTypeAsString()
178 {
179   //  http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
180   // and
181   // itkImageIOBase.cxx
182   const std::type_info & PixType = typeid(TPixel);
183   std::string pixelName;
184   if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
185   else if (PixType == typeid(signed char)) pixelName = "signed_char";
186   else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
187   else if (PixType == typeid(short)) pixelName = "short";
188   else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
189   else if (PixType == typeid(int)) pixelName = "int";
190   else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
191   else if (PixType == typeid(float)) pixelName = "float";
192   else if (PixType == typeid(double)) pixelName = "double";
193   else pixelName = PixType.name();
194   return pixelName;
195 }
196 //--------------------------------------------------------------------
197
198
199 //--------------------------------------------------------------------
200 template<class ImageType>
201 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
202 {
203   output->SetRegions(input->GetLargestPossibleRegion());
204   output->SetOrigin(input->GetOrigin());
205   output->SetSpacing(input->GetSpacing());
206   output->Allocate();
207   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
208   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
209   pi.GoToBegin();
210   typedef itk::ImageRegionIterator<ImageType> IteratorType;
211   IteratorType po(output,input->GetLargestPossibleRegion());
212   po.GoToBegin();
213   while (!pi.IsAtEnd()) {
214     po.Set(pi.Get());
215     ++pi;
216     ++po;
217   }
218 }
219 //--------------------------------------------------------------------
220
221
222 //--------------------------------------------------------------------
223 // http://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl
224 template <typename M, typename V> 
225 void MapToVecFirst(const  M & m, V & v) {
226   for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) {
227     v.push_back( it->first );
228   }
229 }
230 template <typename M, typename V> 
231 void MapToVecSecond(const  M & m, V & v) {
232   for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) {
233     v.push_back( it->second );
234   }
235 }
236 //--------------------------------------------------------------------
237
238
239 //--------------------------------------------------------------------
240 //http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string
241 template<class T>
242 int inline findAndReplace(T& source, const T& find, const T& replace)
243 {
244   int num=0;
245   int fLen = find.size();
246   int rLen = replace.size();
247   for (int pos=0; (pos=source.find(find, pos))!=T::npos; pos+=rLen) {
248     num++;
249     source.replace(pos, fLen, replace);
250   }
251   return num;
252 }
253 //--------------------------------------------------------------------
254
255 #endif /* end #define CLITKCOMMON_TXX */
256