1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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
21 //-------------------------------------------------------
22 // Utility functions for text file parsing (author: joel schaerer)
24 //--------------------------------------------------------------------
25 template<class ElementType>
26 ElementType parse_value(std::string str)
28 std::istringstream parser(str);
31 assert(!parser.fail());
34 //--------------------------------------------------------------------
37 //--------------------------------------------------------------------
38 template<class ElementType>
39 std::vector<ElementType> parse_string(std::string str,char delim)
41 std::istringstream ss(str);
43 std::vector<ElementType> result;
44 while (std::getline(ss,token,delim)) {
45 result.push_back(parse_value<ElementType>(token));
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 template<class ElementType>
54 std::vector<std::vector<ElementType> > parse_file(const char* filename,char delim)
56 std::ifstream fs(filename);
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));
65 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 // Convert float, double ... to string
71 std::string toString(const T & t)
73 std::ostringstream myStream;
74 myStream << t << std::flush;
75 return(myStream.str());
77 //--------------------------------------------------------------------
80 //--------------------------------------------------------------------
81 // http://www.codeguru.com/forum/showthread.php?t=231054
83 bool fromString(T& t, const std::string& s,
84 std::ios_base& (*f)(std::ios_base&))
86 std::istringstream iss(s);
87 return !(iss >> f >> t).fail();
89 //--------------------------------------------------------------------
92 //--------------------------------------------------------------------
93 // Convert float*, double* ... to string
95 std::string toStringVector(const T * t, const int n)
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());
103 //--------------------------------------------------------------------
105 //--------------------------------------------------------------------
106 // Convert float*, double* ... to string
108 std::string toStringVector(const T & t, const int n)
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());
116 //--------------------------------------------------------------------
118 //--------------------------------------------------------------------
119 // Convert float*, double* ... to string
121 std::string toStringVector(const std::vector<T> & t)
123 return toStringVector(&t[0], t.size());
125 //--------------------------------------------------------------------
127 //--------------------------------------------------------------------
128 // Convert a pixel type to another (downcast)
129 template<class TPixelUp, class TPixelDown>
130 TPixelDown PixelTypeDownCast(const TPixelUp & x)
132 return (TPixelDown)lrint(x);
134 //--------------------------------------------------------------------
136 //--------------------------------------------------------------------
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]);
143 const std::vector<Type> & vect;
145 //--------------------------------------------------------------------
147 //--------------------------------------------------------------------
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]);
154 const std::vector<Type> & vect;
156 //--------------------------------------------------------------------
158 //--------------------------------------------------------------------
160 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
162 index.resize(toSort.size());
163 for(unsigned int i=0; i<index.size(); i++) index[i] = i;
165 std::sort(index.begin(),
167 vectorComparisonLowerThan<double>(toSort));
169 std::sort(index.begin(),
171 vectorComparisonGreaterThan<double>(toSort));
173 //--------------------------------------------------------------------
175 //--------------------------------------------------------------------
176 template<class TPixel>
177 std::string GetTypeAsString()
179 // http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
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();
196 //--------------------------------------------------------------------
198 //--------------------------------------------------------------------
199 template<class ImageType>
200 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
202 output->SetRegions(input->GetLargestPossibleRegion());
203 output->SetOrigin(input->GetOrigin());
204 output->SetSpacing(input->GetSpacing());
206 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
207 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
209 typedef itk::ImageRegionIterator<ImageType> IteratorType;
210 IteratorType po(output,input->GetLargestPossibleRegion());
212 while (!pi.IsAtEnd()) {
218 //--------------------------------------------------------------------
220 #endif /* end #define CLITKCOMMON_TXX */