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