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