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