]> Creatis software - clitk.git/blob - common/clitkCommon.h
Merge branch 'master' into clitkImage2Dicom
[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 #ifndef ITK_OVERRIDE
73 # define ITK_OVERRIDE
74 #endif
75
76   //--------------------------------------------------------------------
77   // when everything goes wrong
78 #define WHEREAMI "[ " << __FILE__  << " ] line " << __LINE__
79 #define FATAL(a) { std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0); }
80
81   //--------------------------------------------------------------------
82   // GGO with modified struct name
83 #define GGO(ggo_filename, args_info)                                    \
84   args_info_##ggo_filename args_info;                                   \
85   cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0);                    \
86   if (args_info.config_given)                                           \
87     cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
88   else cmdline_parser_##ggo_filename(argc, argv, &args_info);
89
90   //--------------------------------------------------------------------
91   // skip line with #
92   void skipComment(std::istream & is);
93
94   //--------------------------------------------------------------------
95   // linear (rough) conversion from Hounsfield Unit to density
96   double HU2density(double HU);
97
98   //--------------------------------------------------------------------
99   // Return filename extension
100   std::string GetExtension(const std::string& filename);
101
102   //--------------------------------------------------------------------
103   // Convert float, double ... to string
104   template<class T> std::string toString(const T & t);
105   template<class T> std::string toStringVector(const T * t, const int n);
106   template<class T> std::string toStringVector(const T & t, const int n);
107   template<class T> std::string toStringVector(const std::vector<T> & t);
108   template <class T> bool fromString(T& t,
109                                      const std::string& s,
110                                      std::ios_base& (*f)(std::ios_base&)=std::dec);
111
112   //--------------------------------------------------------------------
113   // Display a progress %
114   void VerboseInProgress(const int nb, const int current, const int percentage);
115   void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
116
117   //--------------------------------------------------------------------
118   // Convert a pixel type to another (downcast)
119   template<class TPixelUp, class TPixelDown>
120   TPixelDown PixelTypeDownCast(const TPixelUp & x);
121   template<>
122   float PixelTypeDownCast(const double & x);
123   template<>
124   double PixelTypeDownCast(const double & x);
125
126   //--------------------------------------------------------------------
127   // Return the indexes of sorted values in a vector
128   template<class Type> struct vectorComparisonLowerThan;
129   template<class Type> struct vectorComparisonGreaterThan;
130   template<class Type>
131   void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
132
133   //--------------------------------------------------------------------
134   // Return the name of a type as a string
135   template<class TPixel>
136   std::string GetTypeAsString();
137
138   //--------------------------------------------------------------------
139   // Convert radian / degree
140   double rad2deg(double anglerad);
141   double deg2rad(double anglerad);
142
143   //--------------------------------------------------------------------
144   int GetTypeSizeFromString(const std::string & type);
145
146   //--------------------------------------------------------------------
147   // Special case to handle "signed_char = schar" ...
148   template<class T>
149   bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
150   template<> bool IsSameType<char>(std::string t);
151   template<> bool IsSameType<signed char>(std::string t);
152   template<> bool IsSameType<unsigned char>(std::string t);
153   template<> bool IsSameType<unsigned short>(std::string t);
154
155   //--------------------------------------------------------------------
156   template<class T1>
157   std::string CreateListOfTypes(bool last=true) {
158     return GetTypeAsString<T1>();
159   }
160
161   template<class T1, class T2>
162   std::string CreateListOfTypes(bool last=true) {
163     if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
164     else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
165   }
166
167   template<class T1, class T2, class T3>
168   std::string CreateListOfTypes(bool last=true) {
169     if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
170     else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
171   }
172
173   template<class T1, class T2, class T3, class T4>
174   std::string CreateListOfTypes(bool last=true) {
175     if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
176     else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
177   }
178
179   template<class T1, class T2, class T3, class T4, class T5>
180   std::string CreateListOfTypes(bool last=true) {
181     if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
182     else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
183   }
184
185   template<class T1, class T2, class T3, class T4, class T5, class T6>
186   std::string CreateListOfTypes(bool last=true) {
187     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
188     else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
189   }
190
191   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
192   std::string CreateListOfTypes(bool last=true) {
193     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
194     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
195   }
196
197   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
198   std::string CreateListOfTypes(bool last=true) {
199     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
200     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
201   }
202   //--------------------------------------------------------------------
203
204   //--------------------------------------------------------------------
205   void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
206   void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
207   void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
208   //--------------------------------------------------------------------
209
210   //--------------------------------------------------------------------
211   double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
212                                                   const itk::ContinuousIndex<double, 3> pointInPlane,
213                                                   const itk::ContinuousIndex<double, 3> normalPlane);
214
215   //--------------------------------------------------------------------
216   // Open a File for reading/writing
217   void openFileForReading(std::ifstream & is, const std::string & filename);
218   void openFileForWriting(std::ofstream & os, const std::string & filename);
219
220   //--------------------------------------------------------------------
221   void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
222
223   //--------------------------------------------------------------------
224   double cotan(double i);
225   double invcotan(double i);
226
227   //--------------------------------------------------------------------
228   void disableStdCerr();
229   void enableStdCerr();
230
231   //--------------------------------------------------------------------
232   template<class ImageType>
233   void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
234
235   //--------------------------------------------------------------------
236   void PrintMemoryUsed();
237
238   //--------------------------------------------------------------------
239   // Convert a map to a vector
240   template <typename M, typename V>
241   void MapToVecFirst(const M & m, V & v);
242   template <typename M, typename V>
243   void MapToVecSecond(const M & m, V & v);
244
245   //--------------------------------------------------------------------
246   // Find/replace string
247   template<class T>
248   int inline findAndReplace(T& source, const T& find, const T& replace);
249
250 #include "clitkCommon.txx"
251
252 } // end namespace
253
254 #endif /* end #define CLITKCOMMON_H */