5 -------------------------------------------------------------------
7 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
8 * @date 17 May 2006 07:57:56
12 -------------------------------------------------------------------*/
16 #include "clitkConfiguration.h"
17 #include "clitkPortability.h"
19 // itk include (include std)
20 #include <itkContinuousIndex.h>
22 #include <itkImageRegionConstIterator.h>
23 #include <itkImageRegionIterator.h>
25 //--------------------------------------------------------------------
28 typedef unsigned char uchar;
29 typedef unsigned short ushort;
30 typedef unsigned int uint;
32 //--------------------------------------------------------------------
34 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
35 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;}
37 //--------------------------------------------------------------------
38 // when everything goes wrong
39 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
40 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
42 //--------------------------------------------------------------------
43 // GGO with modified struct name
44 #define GGO(ggo_filename, args_info) \
45 args_info_##ggo_filename args_info; \
46 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
47 if (args_info.config_given) \
48 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
49 else cmdline_parser(argc, argv, &args_info);
51 //--------------------------------------------------------------------
53 void skipComment(std::istream & is);
55 //--------------------------------------------------------------------
56 // linear (rough) conversion from Hounsfield Unit to density
57 double HU2density(double HU);
59 //--------------------------------------------------------------------
60 // Return filename extension
61 std::string GetExtension(const std::string& filename);
63 //--------------------------------------------------------------------
64 // Convert float, double ... to string
65 template<class T> std::string toString(const T & t);
66 template<class T> std::string toStringVector(const T * t, const int n);
67 template<class T> std::string toStringVector(const T & t, const int n);
68 template<class T> std::string toStringVector(const std::vector<T> & t);
70 //--------------------------------------------------------------------
71 // Display a progress %
72 void VerboseInProgress(const int nb, const int current, const int percentage);
73 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
75 //--------------------------------------------------------------------
76 // Convert a pixel type to another (downcast)
77 template<class TPixelUp, class TPixelDown>
78 TPixelDown PixelTypeDownCast(const TPixelUp & x);
80 float PixelTypeDownCast(const double & x);
82 //--------------------------------------------------------------------
83 // Return the indexes of sorted values in a vector
84 template<class Type> struct vectorComparisonLowerThan;
85 template<class Type> struct vectorComparisonGreaterThan;
87 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
89 //--------------------------------------------------------------------
90 // Return the name of a type as a string
91 template<class TPixel>
92 std::string GetTypeAsString();
94 //--------------------------------------------------------------------
95 // Convert radian / degree
96 double rad2deg(double anglerad);
97 double deg2rad(double anglerad);
99 //--------------------------------------------------------------------
100 int GetTypeSizeFromString(const std::string & type);
102 //--------------------------------------------------------------------
103 // Special case to handle "signed_char = schar" ...
105 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
106 template<> bool IsSameType<char>(std::string t);
107 template<> bool IsSameType<signed char>(std::string t);
108 template<> bool IsSameType<unsigned char>(std::string t);
109 template<> bool IsSameType<unsigned short>(std::string t);
111 //--------------------------------------------------------------------
113 std::string CreateListOfTypes(bool last=true) {
114 return GetTypeAsString<T1>();
117 template<class T1, class T2>
118 std::string CreateListOfTypes(bool last=true) {
119 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
120 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
123 template<class T1, class T2, class T3>
124 std::string CreateListOfTypes(bool last=true) {
125 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
126 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
129 template<class T1, class T2, class T3, class T4>
130 std::string CreateListOfTypes(bool last=true) {
131 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
132 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
135 template<class T1, class T2, class T3, class T4, class T5>
136 std::string CreateListOfTypes(bool last=true) {
137 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
138 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
141 template<class T1, class T2, class T3, class T4, class T5, class T6>
142 std::string CreateListOfTypes(bool last=true) {
143 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
144 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
147 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
148 std::string CreateListOfTypes(bool last=true) {
149 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
150 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
153 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
154 std::string CreateListOfTypes(bool last=true) {
155 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
156 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
158 //--------------------------------------------------------------------
160 //--------------------------------------------------------------------
161 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
162 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
163 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
164 //--------------------------------------------------------------------
166 //--------------------------------------------------------------------
167 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
168 const itk::ContinuousIndex<double, 3> pointInPlane,
169 const itk::ContinuousIndex<double, 3> normalPlane);
171 //--------------------------------------------------------------------
172 // Open a File for reading/writing
173 void openFileForReading(std::ifstream & is, const std::string & filename);
174 void openFileForWriting(std::ofstream & os, const std::string & filename);
176 //--------------------------------------------------------------------
177 double cotan(double i);
178 double invcotan(double i);
180 //--------------------------------------------------------------------
181 void disableStdCerr();
182 void enableStdCerr();
184 //--------------------------------------------------------------------
185 template<class ImageType>
186 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
188 #include "clitkCommon.txx"
192 #endif /* end #define CLITKCOMMON_H */