]> Creatis software - clitk.git/blob - common/clitkCommon.h
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / common / clitkCommon.h
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
19 #ifndef CLITKCOMMON_H
20 #define CLITKCOMMON_H
21
22 // clitk include
23 #include "clitkPortability.h"
24 #include "clitkDD.h"
25 #include "clitkExceptionObject.h"
26
27 // itk include (include std)
28 #include <itkContinuousIndex.h>
29 #include <itkMacro.h>
30 #include <itkImageRegionConstIterator.h>
31 #include <itkImageRegionIterator.h>
32
33 // std include
34 #include <fstream>
35
36 // Include for "rusage"
37 #include <ctime>
38 #if defined(unix) || defined(__APPLE__)
39 #  include <sys/time.h>
40 #  include <sys/resource.h>
41 #elif defined(_WIN32)
42 #  include <windows.h>
43 #  include <stdint.h>
44 #endif
45
46 #define VTK_EXCLUDE_STRSTREAM_HEADERS
47
48 //--------------------------------------------------------------------
49 namespace clitk {
50
51   typedef unsigned char uchar;
52   typedef unsigned short ushort;
53   typedef unsigned int uint;
54
55 #define CLITK_TRY_CATCH_EXIT(func) \
56   try { \
57     func; \
58   } \
59   catch (const itk::ExceptionObject& e) { \
60     e.Print(std::cout); \
61     exit(-1);\
62   } \
63   catch (const std::exception& e) { \
64     std::cout << e.what() << std::endl; \
65     exit(-2);\
66   } \
67   catch (...) { \
68     std::cout << "Unknown excpetion" << std::endl; \
69     exit(-3); \
70   }
71
72
73   //--------------------------------------------------------------------
74   // when everything goes wrong
75 #define WHEREAMI "[ " << __FILE__  << " ] line " << __LINE__
76 #define FATAL(a) { std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0); }
77
78   //--------------------------------------------------------------------
79   // GGO with modified struct name
80 #define GGO(ggo_filename, args_info)                                    \
81   args_info_##ggo_filename args_info;                                   \
82   cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0);                    \
83   if (args_info.config_given)                                           \
84     cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
85   else cmdline_parser_##ggo_filename(argc, argv, &args_info);
86
87   //--------------------------------------------------------------------
88   // skip line with #
89   void skipComment(std::istream & is);
90
91   //--------------------------------------------------------------------
92   // linear (rough) conversion from Hounsfield Unit to density
93   double HU2density(double HU);
94
95   //--------------------------------------------------------------------
96   // Return filename extension
97   std::string GetExtension(const std::string& filename);
98
99   //--------------------------------------------------------------------
100   // Convert float, double ... to string
101   template<class T> std::string toString(const T & t);
102   template<class T> std::string toStringVector(const T * t, const int n);
103   template<class T> std::string toStringVector(const T & t, const int n);
104   template<class T> std::string toStringVector(const std::vector<T> & t);
105   template <class T> bool fromString(T& t,
106                                      const std::string& s,
107                                      std::ios_base& (*f)(std::ios_base&)=std::dec);
108
109   //--------------------------------------------------------------------
110   // Display a progress %
111   void VerboseInProgress(const int nb, const int current, const int percentage);
112   void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
113
114   //--------------------------------------------------------------------
115   // Convert a pixel type to another (downcast)
116   template<class TPixelUp, class TPixelDown>
117   TPixelDown PixelTypeDownCast(const TPixelUp & x);
118   template<>
119   float PixelTypeDownCast(const double & x);
120   template<>
121   double PixelTypeDownCast(const double & x);
122
123   //--------------------------------------------------------------------
124   // Return the indexes of sorted values in a vector
125   template<class Type> struct vectorComparisonLowerThan;
126   template<class Type> struct vectorComparisonGreaterThan;
127   template<class Type>
128   void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
129
130   //--------------------------------------------------------------------
131   // Return the name of a type as a string
132   template<class TPixel>
133   std::string GetTypeAsString();
134
135   //--------------------------------------------------------------------
136   // Convert radian / degree
137   double rad2deg(double anglerad);
138   double deg2rad(double anglerad);
139
140   //--------------------------------------------------------------------
141   int GetTypeSizeFromString(const std::string & type);
142
143   //--------------------------------------------------------------------
144   // Special case to handle "signed_char = schar" ...
145   template<class T>
146   bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
147   template<> bool IsSameType<char>(std::string t);
148   template<> bool IsSameType<signed char>(std::string t);
149   template<> bool IsSameType<unsigned char>(std::string t);
150   template<> bool IsSameType<unsigned short>(std::string t);
151
152   //--------------------------------------------------------------------
153   template<class T1>
154   std::string CreateListOfTypes(bool last=true) {
155     return GetTypeAsString<T1>();
156   }
157
158   template<class T1, class T2>
159   std::string CreateListOfTypes(bool last=true) {
160     if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
161     else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
162   }
163
164   template<class T1, class T2, class T3>
165   std::string CreateListOfTypes(bool last=true) {
166     if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
167     else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
168   }
169
170   template<class T1, class T2, class T3, class T4>
171   std::string CreateListOfTypes(bool last=true) {
172     if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
173     else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
174   }
175
176   template<class T1, class T2, class T3, class T4, class T5>
177   std::string CreateListOfTypes(bool last=true) {
178     if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
179     else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
180   }
181
182   template<class T1, class T2, class T3, class T4, class T5, class T6>
183   std::string CreateListOfTypes(bool last=true) {
184     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
185     else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
186   }
187
188   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
189   std::string CreateListOfTypes(bool last=true) {
190     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
191     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
192   }
193
194   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
195   std::string CreateListOfTypes(bool last=true) {
196     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
197     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
198   }
199   //--------------------------------------------------------------------
200
201   //--------------------------------------------------------------------
202   void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
203   void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
204   void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
205   //--------------------------------------------------------------------
206
207   //--------------------------------------------------------------------
208   double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
209                                                   const itk::ContinuousIndex<double, 3> pointInPlane,
210                                                   const itk::ContinuousIndex<double, 3> normalPlane);
211
212   //--------------------------------------------------------------------
213   // Open a File for reading/writing
214   void openFileForReading(std::ifstream & is, const std::string & filename);
215   void openFileForWriting(std::ofstream & os, const std::string & filename);
216
217   //--------------------------------------------------------------------
218   void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
219
220   //--------------------------------------------------------------------
221   double cotan(double i);
222   double invcotan(double i);
223
224   //--------------------------------------------------------------------
225   void disableStdCerr();
226   void enableStdCerr();
227
228   //--------------------------------------------------------------------
229   template<class ImageType>
230   void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
231
232   //--------------------------------------------------------------------
233   void PrintMemoryUsed();
234
235   //--------------------------------------------------------------------
236   // Convert a map to a vector
237   template <typename M, typename V>
238   void MapToVecFirst(const M & m, V & v);
239   template <typename M, typename V>
240   void MapToVecSecond(const M & m, V & v);
241
242   //--------------------------------------------------------------------
243   // Find/replace string
244   template<class T>
245   int inline findAndReplace(T& source, const T& find, const T& replace);
246
247 #include "clitkCommon.txx"
248
249 } // end namespace
250
251 #endif /* end #define CLITKCOMMON_H */