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