5 -------------------------------------------------------------------
7 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
8 * @date 17 May 2006 07:57:56
12 -------------------------------------------------------------------*/
26 #include <itkContinuousIndex.h>
27 #include <itkImageRegionConstIterator.h>
28 #include <itkImageRegionIterator.h>
30 //--------------------------------------------------------------------
34 #define lrint(x) (floor(x+(x>0) ? 0.5 : -0.5))
37 typedef unsigned char uchar;
38 typedef unsigned short ushort;
39 typedef unsigned int uint;
41 //--------------------------------------------------------------------
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;}
46 //--------------------------------------------------------------------
47 // when everything goes wrong
48 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
49 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
51 //--------------------------------------------------------------------
52 // GGO with modified struct name
53 #define GGO(ggo_filename, args_info) \
54 args_info_##ggo_filename args_info; \
55 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
56 if (args_info.config_given) \
57 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
58 else cmdline_parser(argc, argv, &args_info);
60 //--------------------------------------------------------------------
62 void skipComment(std::istream & is);
64 //--------------------------------------------------------------------
65 // linear (rough) conversion from Hounsfield Unit to density
66 double HU2density(double HU);
68 //--------------------------------------------------------------------
69 // Return filename extension
70 std::string GetExtension(const std::string& filename);
72 //--------------------------------------------------------------------
73 // Convert float, double ... to string
74 template<class T> std::string toString(const T & t);
75 template<class T> std::string toStringVector(const T * t, const int n);
76 template<class T> std::string toStringVector(const T & t, const int n);
77 template<class T> std::string toStringVector(const std::vector<T> & t);
79 //--------------------------------------------------------------------
80 // Display a progress %
81 void VerboseInProgress(const int nb, const int current, const int percentage);
82 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
84 //--------------------------------------------------------------------
85 // Convert a pixel type to another (downcast)
86 template<class TPixelUp, class TPixelDown>
87 TPixelDown PixelTypeDownCast(const TPixelUp & x);
89 float PixelTypeDownCast(const double & x);
91 //--------------------------------------------------------------------
92 // Return the indexes of sorted values in a vector
93 template<class Type> struct vectorComparisonLowerThan;
94 template<class Type> struct vectorComparisonGreaterThan;
96 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
98 //--------------------------------------------------------------------
99 // Return the name of a type as a string
100 template<class TPixel>
101 std::string GetTypeAsString();
103 //--------------------------------------------------------------------
104 // Convert radian / degree
105 double rad2deg(double anglerad);
106 double deg2rad(double anglerad);
108 //--------------------------------------------------------------------
109 int GetTypeSizeFromString(const std::string & type);
111 //--------------------------------------------------------------------
112 // Special case to handle "signed_char = schar" ...
114 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
115 template<> bool IsSameType<char>(std::string t);
116 template<> bool IsSameType<signed char>(std::string t);
117 template<> bool IsSameType<unsigned char>(std::string t);
118 template<> bool IsSameType<unsigned short>(std::string t);
120 //--------------------------------------------------------------------
122 std::string CreateListOfTypes(bool last=true) {
123 return GetTypeAsString<T1>();
126 template<class T1, class T2>
127 std::string CreateListOfTypes(bool last=true) {
128 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
129 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
132 template<class T1, class T2, class T3>
133 std::string CreateListOfTypes(bool last=true) {
134 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
135 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
138 template<class T1, class T2, class T3, class T4>
139 std::string CreateListOfTypes(bool last=true) {
140 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
141 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
144 template<class T1, class T2, class T3, class T4, class T5>
145 std::string CreateListOfTypes(bool last=true) {
146 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
147 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
150 template<class T1, class T2, class T3, class T4, class T5, class T6>
151 std::string CreateListOfTypes(bool last=true) {
152 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
153 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
156 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
157 std::string CreateListOfTypes(bool last=true) {
158 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
159 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
162 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
163 std::string CreateListOfTypes(bool last=true) {
164 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
165 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
167 //--------------------------------------------------------------------
169 //--------------------------------------------------------------------
170 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
171 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
172 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
173 //--------------------------------------------------------------------
175 //--------------------------------------------------------------------
176 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
177 const itk::ContinuousIndex<double, 3> pointInPlane,
178 const itk::ContinuousIndex<double, 3> normalPlane);
180 //--------------------------------------------------------------------
181 // Open a File for reading/writing
182 void openFileForReading(std::ifstream & is, const std::string & filename);
183 void openFileForWriting(std::ofstream & os, const std::string & filename);
185 //--------------------------------------------------------------------
186 double cotan(double i);
187 double invcotan(double i);
189 //--------------------------------------------------------------------
190 void disableStdCerr();
191 void enableStdCerr();
193 //--------------------------------------------------------------------
194 template<class ImageType>
195 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
197 #include "clitkCommon.txx"
201 #endif /* end #define CLITKCOMMON_H */