]> Creatis software - clitk.git/blob - common/clitkCommon.h
31f28dc40637bd95e91dfec7974f23183edd43ac
[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://www.centreleonberard.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 "clitkPortability.h"
24 #include "clitkDD.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 // std include
34 #include <fstream>
35
36 // Include for "rusage"
37 #include <ctime> 
38 #if defined(unix) || defined(__APPLE__)
39 #  include <sys/time.h>
40 #  include <sys/resource.h>
41 #elif defined(WIN32)
42 #  include <windows.h>
43 #endif
44
45 //--------------------------------------------------------------------
46 namespace clitk {
47
48   typedef unsigned char uchar;
49   typedef unsigned short ushort;
50   typedef unsigned int uint;
51
52   //--------------------------------------------------------------------
53   // when everything goes wrong
54 #define WHEREAMI "[ " << __FILE__  << " ] line " << __LINE__
55 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
56   
57   //--------------------------------------------------------------------
58   // GGO with modified struct name
59 #define GGO(ggo_filename, args_info)                                    \
60   args_info_##ggo_filename args_info;                                   \
61   cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0);                    \
62   if (args_info.config_given)                                           \
63     cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
64   else cmdline_parser_##ggo_filename(argc, argv, &args_info);  
65
66   //--------------------------------------------------------------------
67   // skip line with #
68   void skipComment(std::istream & is);
69
70   //--------------------------------------------------------------------
71   // linear (rough) conversion from Hounsfield Unit to density
72   double HU2density(double HU);
73
74   //--------------------------------------------------------------------
75   // Return filename extension
76   std::string GetExtension(const std::string& filename);
77   
78   //--------------------------------------------------------------------
79   // Convert float, double ... to string
80   template<class T> std::string toString(const T & t);
81   template<class T> std::string toStringVector(const T * t, const int n);
82   template<class T> std::string toStringVector(const T & t, const int n);
83   template<class T> std::string toStringVector(const std::vector<T> & t);
84   template <class T> bool fromString(T& t, 
85                                      const std::string& s, 
86                                      std::ios_base& (*f)(std::ios_base&)=std::dec);
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   void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
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   //--------------------------------------------------------------------
210   void PrintMemoryUsed();
211
212 #include "clitkCommon.txx"
213
214 } // end namespace
215
216 #endif /* end #define CLITKCOMMON_H */
217