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