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 //--------------------------------------------------------------------
79 //--------------------------------------------------------------------
80 // Convert float*, double* ... to string
82 std::string toStringVector(const T * t, const int n)
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());
90 //--------------------------------------------------------------------
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 << t[i] << " ";
100 myStream << t[n-1] << std::flush;
101 return(myStream.str());
103 //--------------------------------------------------------------------
105 //--------------------------------------------------------------------
106 // Convert float*, double* ... to string
108 std::string toStringVector(const std::vector<T> & t)
110 return toStringVector(&t[0], t.size());
112 //--------------------------------------------------------------------
114 //--------------------------------------------------------------------
115 // Convert a pixel type to another (downcast)
116 template<class TPixelUp, class TPixelDown>
117 TPixelDown PixelTypeDownCast(const TPixelUp & x)
119 return (TPixelDown)lrint(x);
121 //--------------------------------------------------------------------
123 //--------------------------------------------------------------------
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]);
130 const std::vector<Type> & vect;
132 //--------------------------------------------------------------------
134 //--------------------------------------------------------------------
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]);
141 const std::vector<Type> & vect;
143 //--------------------------------------------------------------------
145 //--------------------------------------------------------------------
147 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
149 index.resize(toSort.size());
150 for(unsigned int i=0; i<index.size(); i++) index[i] = i;
152 std::sort(index.begin(),
154 vectorComparisonLowerThan<double>(toSort));
156 std::sort(index.begin(),
158 vectorComparisonGreaterThan<double>(toSort));
160 //--------------------------------------------------------------------
162 //--------------------------------------------------------------------
163 template<class TPixel>
164 std::string GetTypeAsString()
166 // http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
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();
183 //--------------------------------------------------------------------
185 //--------------------------------------------------------------------
186 template<class ImageType>
187 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
189 output->SetRegions(input->GetLargestPossibleRegion());
190 output->SetOrigin(input->GetOrigin());
191 output->SetSpacing(input->GetSpacing());
193 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
194 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
196 typedef itk::ImageRegionIterator<ImageType> IteratorType;
197 IteratorType po(output,input->GetLargestPossibleRegion());
199 while (!pi.IsAtEnd()) {
205 //--------------------------------------------------------------------
207 #endif /* end #define CLITKCOMMON_TXX */