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