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