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