]> Creatis software - clitk.git/blob - common/clitkCommon.h
move clitkConfiguration.h out of clitkCommon to avoid costly recompilations
[clitk.git] / common / clitkCommon.h
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #ifndef CLITKCOMMON_H
20 #define CLITKCOMMON_H
21
22 // clitk include
23 //#include "clitkConfiguration.h"
24 #include "clitkPortability.h"
25 #include "clitkExceptionObject.h"
26
27 // itk include (include std)
28 #include <itkContinuousIndex.h>
29 #include <itkMacro.h>
30 #include <itkImageRegionConstIterator.h>
31 #include <itkImageRegionIterator.h>
32
33 #include <fstream>
34
35 //--------------------------------------------------------------------
36 namespace clitk {
37
38   typedef unsigned char uchar;
39   typedef unsigned short ushort;
40   typedef unsigned int uint;
41
42   //--------------------------------------------------------------------
43   // display 
44 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;std::cout.flush();
45 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;std::cout.flush();}
46 template<class T>
47 void _print_container(T const& a)
48     { for(typename T::const_iterator i=a.begin();i!=a.end();++i) { std::cout << *i << " "; };}
49 #define DDS(a) { std::cout << #a " = [ "; _print_container(a) ; std::cout << " ]" << std::endl;std::cout.flush():}
50
51   //--------------------------------------------------------------------
52   // when everything goes wrong
53 #define WHEREAMI "[ " << __FILE__  << " ] line " << __LINE__
54 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
55   
56   //--------------------------------------------------------------------
57   // GGO with modified struct name
58 #define GGO(ggo_filename, args_info)                                    \
59   args_info_##ggo_filename args_info;                                   \
60   cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0);                    \
61   if (args_info.config_given)                                           \
62     cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
63   else cmdline_parser_##ggo_filename(argc, argv, &args_info);  
64
65   //--------------------------------------------------------------------
66   // skip line with #
67   void skipComment(std::istream & is);
68
69   //--------------------------------------------------------------------
70   // linear (rough) conversion from Hounsfield Unit to density
71   double HU2density(double HU);
72
73   //--------------------------------------------------------------------
74   // Return filename extension
75   std::string GetExtension(const std::string& filename);
76   
77   //--------------------------------------------------------------------
78   // Convert float, double ... to string
79   template<class T> std::string toString(const T & t);
80   template<class T> std::string toStringVector(const T * t, const int n);
81   template<class T> std::string toStringVector(const T & t, const int n);
82   template<class T> std::string toStringVector(const std::vector<T> & t);
83
84   //--------------------------------------------------------------------
85   // Display a progress %
86   void VerboseInProgress(const int nb, const int current, const int percentage);
87   void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
88
89   //--------------------------------------------------------------------
90   // Convert a pixel type to another (downcast)
91   template<class TPixelUp, class TPixelDown>
92   TPixelDown PixelTypeDownCast(const TPixelUp & x);
93   template<>
94   float PixelTypeDownCast(const double & x);
95
96   //--------------------------------------------------------------------
97   // Return the indexes of sorted values in a vector
98   template<class Type> struct vectorComparisonLowerThan;
99   template<class Type> struct vectorComparisonGreaterThan;
100   template<class Type>
101   void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
102
103   //--------------------------------------------------------------------
104   // Return the name of a type as a string
105   template<class TPixel>
106   std::string GetTypeAsString();
107   
108   //--------------------------------------------------------------------
109   // Convert radian / degree
110   double rad2deg(double anglerad);
111   double deg2rad(double anglerad);
112
113   //--------------------------------------------------------------------
114   int GetTypeSizeFromString(const std::string & type);
115
116   //--------------------------------------------------------------------
117   // Special case to handle "signed_char = schar" ...
118   template<class T>
119   bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
120   template<> bool IsSameType<char>(std::string t);
121   template<> bool IsSameType<signed char>(std::string t);
122   template<> bool IsSameType<unsigned char>(std::string t);
123   template<> bool IsSameType<unsigned short>(std::string t);
124
125   //--------------------------------------------------------------------
126   template<class T1>
127   std::string CreateListOfTypes(bool last=true) {
128     return GetTypeAsString<T1>();
129   }
130   
131   template<class T1, class T2>
132   std::string CreateListOfTypes(bool last=true) {
133     if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
134     else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
135   }
136   
137   template<class T1, class T2, class T3>
138   std::string CreateListOfTypes(bool last=true) {
139     if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
140     else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
141   }
142
143   template<class T1, class T2, class T3, class T4>
144   std::string CreateListOfTypes(bool last=true) {
145     if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
146     else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
147   }
148
149   template<class T1, class T2, class T3, class T4, class T5>
150   std::string CreateListOfTypes(bool last=true) {
151     if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
152     else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
153   }
154
155   template<class T1, class T2, class T3, class T4, class T5, class T6>
156   std::string CreateListOfTypes(bool last=true) {
157     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
158     else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
159   }
160
161   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
162   std::string CreateListOfTypes(bool last=true) {
163     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
164     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
165   }
166
167   template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
168   std::string CreateListOfTypes(bool last=true) {
169     if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
170     else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
171   }
172   //--------------------------------------------------------------------
173   
174   //--------------------------------------------------------------------
175   void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
176   void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
177   void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
178   //--------------------------------------------------------------------
179
180   //--------------------------------------------------------------------
181   double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point, 
182                                                   const itk::ContinuousIndex<double, 3> pointInPlane, 
183                                                   const itk::ContinuousIndex<double, 3> normalPlane);
184
185   //--------------------------------------------------------------------
186   // Open a File for reading/writing
187   void openFileForReading(std::ifstream & is, const std::string & filename);
188   void openFileForWriting(std::ofstream & os, const std::string & filename);
189
190   //--------------------------------------------------------------------
191   void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
192
193   //--------------------------------------------------------------------
194   double cotan(double i);
195   double invcotan(double i);
196
197   //--------------------------------------------------------------------
198   void disableStdCerr();
199   void enableStdCerr();
200
201   //--------------------------------------------------------------------
202   template<class ImageType>
203   void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
204
205 #include "clitkCommon.txx"
206
207 } // end namespace
208
209 #endif /* end #define CLITKCOMMON_H */
210