]> Creatis software - clitk.git/blob - common/clitkCommon.h
- autotool
[clitk.git] / common / clitkCommon.h
1 #ifndef CLITKCOMMON_H
2 #define CLITKCOMMON_H
3
4 /**
5    -------------------------------------------------------------------
6    * @file   clitkCommon.h
7    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
8    * @date   17 May 2006 07:57:56
9
10    * @brief  
11
12    -------------------------------------------------------------------*/
13
14 // clitk include
15 #include "clitkPortability.h"
16
17 // itk include (include std)
18 #include <itkContinuousIndex.h>
19 #include <itkMacro.h>
20 #include <itkImageRegionConstIterator.h>
21 #include <itkImageRegionIterator.h>
22
23 //--------------------------------------------------------------------
24 namespace clitk {
25
26   typedef unsigned char uchar;
27   typedef unsigned short ushort;
28   typedef unsigned int uint;
29
30   //--------------------------------------------------------------------
31   // display 
32 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
33 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;}
34
35   //--------------------------------------------------------------------
36   // when everything goes wrong
37 #define WHEREAMI "[ " << __FILE__  << " ] line " << __LINE__
38 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
39   
40   //--------------------------------------------------------------------
41   // GGO with modified struct name
42 #define GGO(ggo_filename, args_info)                                    \
43   args_info_##ggo_filename args_info;                                   \
44   cmdline_parser2(argc, argv, &args_info, 1, 1, 0);                     \
45   if (args_info.config_given)                                           \
46     cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
47   else cmdline_parser(argc, argv, &args_info);  
48
49   //--------------------------------------------------------------------
50   // skip line with #
51   void skipComment(std::istream & is);
52
53   //--------------------------------------------------------------------
54   // linear (rough) conversion from Hounsfield Unit to density
55   double HU2density(double HU);
56
57   //--------------------------------------------------------------------
58   // Return filename extension
59   std::string GetExtension(const std::string& filename);
60   
61   //--------------------------------------------------------------------
62   // Convert float, double ... to string
63   template<class T> std::string toString(const T & t);
64   template<class T> std::string toStringVector(const T * t, const int n);
65   template<class T> std::string toStringVector(const T & t, const int n);
66   template<class T> std::string toStringVector(const std::vector<T> & t);
67
68   //--------------------------------------------------------------------
69   // Display a progress %
70   void VerboseInProgress(const int nb, const int current, const int percentage);
71   void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
72
73   //--------------------------------------------------------------------
74   // Convert a pixel type to another (downcast)
75   template<class TPixelUp, class TPixelDown>
76   TPixelDown PixelTypeDownCast(const TPixelUp & x);
77   template<>
78   float PixelTypeDownCast(const double & x);
79
80   //--------------------------------------------------------------------
81   // Return the indexes of sorted values in a vector
82   template<class Type> struct vectorComparisonLowerThan;
83   template<class Type> struct vectorComparisonGreaterThan;
84   template<class Type>
85   void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
86
87   //--------------------------------------------------------------------
88   // Return the name of a type as a string
89   template<class TPixel>
90   std::string GetTypeAsString();
91   
92   //--------------------------------------------------------------------
93   // Convert radian / degree
94   double rad2deg(double anglerad);
95   double deg2rad(double anglerad);
96
97   //--------------------------------------------------------------------
98   int GetTypeSizeFromString(const std::string & type);
99
100   //--------------------------------------------------------------------
101   // Special case to handle "signed_char = schar" ...
102   template<class T>
103   bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
104   template<> bool IsSameType<char>(std::string t);
105   template<> bool IsSameType<signed char>(std::string t);
106   template<> bool IsSameType<unsigned char>(std::string t);
107   template<> bool IsSameType<unsigned short>(std::string t);
108
109   //--------------------------------------------------------------------
110   template<class T1>
111   std::string CreateListOfTypes(bool last=true) {
112     return GetTypeAsString<T1>();
113   }
114   
115   template<class T1, class T2>
116   std::string CreateListOfTypes(bool last=true) {
117     if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
118     else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
119   }
120   
121   template<class T1, class T2, class T3>
122   std::string CreateListOfTypes(bool last=true) {
123     if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
124     else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
125   }
126
127   template<class T1, class T2, class T3, class T4>
128   std::string CreateListOfTypes(bool last=true) {
129     if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
130     else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
131   }
132
133   template<class T1, class T2, class T3, class T4, class T5>
134   std::string CreateListOfTypes(bool last=true) {
135     if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
136     else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
137   }
138
139   template<class T1, class T2, class T3, class T4, class T5, class T6>
140   std::string CreateListOfTypes(bool last=true) {
141     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
142     else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
143   }
144
145   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
146   std::string CreateListOfTypes(bool last=true) {
147     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
148     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
149   }
150
151   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
152   std::string CreateListOfTypes(bool last=true) {
153     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
154     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
155   }
156   //--------------------------------------------------------------------
157   
158   //--------------------------------------------------------------------
159   void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
160   void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
161   void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
162   //--------------------------------------------------------------------
163
164   //--------------------------------------------------------------------
165   double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point, 
166                                                   const itk::ContinuousIndex<double, 3> pointInPlane, 
167                                                   const itk::ContinuousIndex<double, 3> normalPlane);
168
169   //--------------------------------------------------------------------
170   // Open a File for reading/writing
171   void openFileForReading(std::ifstream & is, const std::string & filename);
172   void openFileForWriting(std::ofstream & os, const std::string & filename);
173
174   //--------------------------------------------------------------------
175   double cotan(double i);
176   double invcotan(double i);
177
178   //--------------------------------------------------------------------
179   void disableStdCerr();
180   void enableStdCerr();
181
182   //--------------------------------------------------------------------
183   template<class ImageType>
184   void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
185
186 #include "clitkCommon.txx"
187
188 } // end namespace
189
190 #endif /* end #define CLITKCOMMON_H */
191