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