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