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