]> Creatis software - clitk.git/blob - common/clitkCommon.h
upport
[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   gengetopt_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   /*   //--------------------------------------------------------------------
62   // GGO default
63 #define GGO(ggo_filename, args_info)                                    \
64   gengetopt_args_info_##ggo_filename args_info;                         \
65   cmdline_parser2(argc, argv, &args_info, 1, 1, 0);                     \
66   if (args_info.config_given)                                           \
67     cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
68   else cmdline_parser(argc, argv, &args_info);
69   
70  //--------------------------------------------------------------------
71  */
72   //--------------------------------------------------------------------
73   // skip line with #
74   void skipComment(std::istream & is);
75
76   //--------------------------------------------------------------------
77   // linear (rough) conversion from Hounsfield Unit to density
78   double HU2density(double HU);
79
80   //--------------------------------------------------------------------
81   // Return filename extension
82   std::string GetExtension(const std::string& filename);
83   
84   //--------------------------------------------------------------------
85   // Convert float, double ... to string
86   template<class T> std::string toString(const T & t);
87   template<class T> std::string toStringVector(const T * t, const int n);
88   template<class T> std::string toStringVector(const T & t, const int n);
89   template<class T> std::string toStringVector(const std::vector<T> & t);
90
91   //--------------------------------------------------------------------
92   // Display a progress %
93   void VerboseInProgress(const int nb, const int current, const int percentage);
94   void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
95
96   //--------------------------------------------------------------------
97   // Convert a pixel type to another (downcast)
98   template<class TPixelUp, class TPixelDown>
99   TPixelDown PixelTypeDownCast(const TPixelUp & x);
100   template<>
101   float PixelTypeDownCast(const double & x);
102
103   //--------------------------------------------------------------------
104   // Return the indexes of sorted values in a vector
105   template<class Type> struct vectorComparisonLowerThan;
106   template<class Type> struct vectorComparisonGreaterThan;
107   template<class Type>
108   void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
109
110   //--------------------------------------------------------------------
111   // Return the name of a type as a string
112   template<class TPixel>
113   std::string GetTypeAsString();
114   
115   //--------------------------------------------------------------------
116   // Convert radian / degree
117   double rad2deg(double anglerad);
118   double deg2rad(double anglerad);
119
120   //--------------------------------------------------------------------
121   int GetTypeSizeFromString(const std::string & type);
122
123   //--------------------------------------------------------------------
124   // Special case to handle "signed_char = schar" ...
125   template<class T>
126   bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
127   template<> bool IsSameType<char>(std::string t);
128   template<> bool IsSameType<signed char>(std::string t);
129   template<> bool IsSameType<unsigned char>(std::string t);
130   template<> bool IsSameType<unsigned short>(std::string t);
131
132   //--------------------------------------------------------------------
133   template<class T1>
134   std::string CreateListOfTypes(bool last=true) {
135     return GetTypeAsString<T1>();
136   }
137   
138   template<class T1, class T2>
139   std::string CreateListOfTypes(bool last=true) {
140     if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
141     else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
142   }
143   
144   template<class T1, class T2, class T3>
145   std::string CreateListOfTypes(bool last=true) {
146     if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
147     else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
148   }
149
150   template<class T1, class T2, class T3, class T4>
151   std::string CreateListOfTypes(bool last=true) {
152     if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
153     else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
154   }
155
156   template<class T1, class T2, class T3, class T4, class T5>
157   std::string CreateListOfTypes(bool last=true) {
158     if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
159     else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
160   }
161
162   template<class T1, class T2, class T3, class T4, class T5, class T6>
163   std::string CreateListOfTypes(bool last=true) {
164     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
165     else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
166   }
167
168   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
169   std::string CreateListOfTypes(bool last=true) {
170     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
171     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
172   }
173
174   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
175   std::string CreateListOfTypes(bool last=true) {
176     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
177     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
178   }
179   //--------------------------------------------------------------------
180   
181   //--------------------------------------------------------------------
182   void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
183   void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
184   void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
185   //--------------------------------------------------------------------
186
187   //--------------------------------------------------------------------
188   double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point, 
189                                                   const itk::ContinuousIndex<double, 3> pointInPlane, 
190                                                   const itk::ContinuousIndex<double, 3> normalPlane);
191
192   //--------------------------------------------------------------------
193   // Open a File for reading/writing
194   void openFileForReading(std::ifstream & is, const std::string & filename);
195   void openFileForWriting(std::ofstream & os, const std::string & filename);
196
197   //--------------------------------------------------------------------
198   double cotan(double i);
199   double invcotan(double i);
200
201   //--------------------------------------------------------------------
202   void disableStdCerr();
203   void enableStdCerr();
204
205   //--------------------------------------------------------------------
206   template<class ImageType>
207   void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
208
209 #include "clitkCommon.txx"
210
211 } // end namespace
212
213 #endif /* end #define CLITKCOMMON_H */
214