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