5 -------------------------------------------------------------------
7 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
8 * @date 17 May 2006 07:57:56
12 -------------------------------------------------------------------*/
17 //#include <algorithm>
26 //#include <algorithm>
28 // itk include (include std)
29 #include <itkContinuousIndex.h>
31 #include <itkImageRegionConstIterator.h>
32 #include <itkImageRegionIterator.h>
34 //--------------------------------------------------------------------
38 # define lrint(x) (floor(x+(x>0) ? 0.5 : -0.5))
42 # define M_PI 3.141592653589793238462643
46 typedef unsigned char uchar;
47 typedef unsigned short ushort;
48 typedef unsigned int uint;
50 //--------------------------------------------------------------------
52 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
53 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;}
55 //--------------------------------------------------------------------
56 // when everything goes wrong
57 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
58 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
60 //--------------------------------------------------------------------
61 // GGO with modified struct name
62 #define GGO(ggo_filename, args_info) \
63 args_info_##ggo_filename args_info; \
64 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
65 if (args_info.config_given) \
66 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
67 else cmdline_parser(argc, argv, &args_info);
69 //--------------------------------------------------------------------
71 void skipComment(std::istream & is);
73 //--------------------------------------------------------------------
74 // linear (rough) conversion from Hounsfield Unit to density
75 double HU2density(double HU);
77 //--------------------------------------------------------------------
78 // Return filename extension
79 std::string GetExtension(const std::string& filename);
81 //--------------------------------------------------------------------
82 // Convert float, double ... to string
83 template<class T> std::string toString(const T & t);
84 template<class T> std::string toStringVector(const T * t, const int n);
85 template<class T> std::string toStringVector(const T & t, const int n);
86 template<class T> std::string toStringVector(const std::vector<T> & t);
88 //--------------------------------------------------------------------
89 // Display a progress %
90 void VerboseInProgress(const int nb, const int current, const int percentage);
91 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
93 //--------------------------------------------------------------------
94 // Convert a pixel type to another (downcast)
95 template<class TPixelUp, class TPixelDown>
96 TPixelDown PixelTypeDownCast(const TPixelUp & x);
98 float PixelTypeDownCast(const double & x);
100 //--------------------------------------------------------------------
101 // Return the indexes of sorted values in a vector
102 template<class Type> struct vectorComparisonLowerThan;
103 template<class Type> struct vectorComparisonGreaterThan;
105 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
107 //--------------------------------------------------------------------
108 // Return the name of a type as a string
109 template<class TPixel>
110 std::string GetTypeAsString();
112 //--------------------------------------------------------------------
113 // Convert radian / degree
114 double rad2deg(double anglerad);
115 double deg2rad(double anglerad);
117 //--------------------------------------------------------------------
118 int GetTypeSizeFromString(const std::string & type);
120 //--------------------------------------------------------------------
121 // Special case to handle "signed_char = schar" ...
123 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
124 template<> bool IsSameType<char>(std::string t);
125 template<> bool IsSameType<signed char>(std::string t);
126 template<> bool IsSameType<unsigned char>(std::string t);
127 template<> bool IsSameType<unsigned short>(std::string t);
129 //--------------------------------------------------------------------
131 std::string CreateListOfTypes(bool last=true) {
132 return GetTypeAsString<T1>();
135 template<class T1, class T2>
136 std::string CreateListOfTypes(bool last=true) {
137 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
138 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
141 template<class T1, class T2, class T3>
142 std::string CreateListOfTypes(bool last=true) {
143 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
144 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
147 template<class T1, class T2, class T3, class T4>
148 std::string CreateListOfTypes(bool last=true) {
149 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
150 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
153 template<class T1, class T2, class T3, class T4, class T5>
154 std::string CreateListOfTypes(bool last=true) {
155 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
156 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
159 template<class T1, class T2, class T3, class T4, class T5, class T6>
160 std::string CreateListOfTypes(bool last=true) {
161 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
162 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
165 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
166 std::string CreateListOfTypes(bool last=true) {
167 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
168 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
171 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
172 std::string CreateListOfTypes(bool last=true) {
173 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
174 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
176 //--------------------------------------------------------------------
178 //--------------------------------------------------------------------
179 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
180 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
181 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
182 //--------------------------------------------------------------------
184 //--------------------------------------------------------------------
185 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
186 const itk::ContinuousIndex<double, 3> pointInPlane,
187 const itk::ContinuousIndex<double, 3> normalPlane);
189 //--------------------------------------------------------------------
190 // Open a File for reading/writing
191 void openFileForReading(std::ifstream & is, const std::string & filename);
192 void openFileForWriting(std::ofstream & os, const std::string & filename);
194 //--------------------------------------------------------------------
195 double cotan(double i);
196 double invcotan(double i);
198 //--------------------------------------------------------------------
199 void disableStdCerr();
200 void enableStdCerr();
202 //--------------------------------------------------------------------
203 template<class ImageType>
204 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
206 #include "clitkCommon.txx"
210 #endif /* end #define CLITKCOMMON_H */