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 gengetopt_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);
61 /* //--------------------------------------------------------------------
63 #define GGO(ggo_filename, args_info) \
64 gengetopt_args_info_##ggo_filename args_info; \
65 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
66 if (args_info.config_given) \
67 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
68 else cmdline_parser(argc, argv, &args_info);
70 //--------------------------------------------------------------------
72 //--------------------------------------------------------------------
74 void skipComment(std::istream & is);
76 //--------------------------------------------------------------------
77 // linear (rough) conversion from Hounsfield Unit to density
78 double HU2density(double HU);
80 //--------------------------------------------------------------------
81 // Return filename extension
82 std::string GetExtension(const std::string& filename);
84 //--------------------------------------------------------------------
85 // Convert float, double ... to string
86 template<class T> std::string toString(const T & t);
87 template<class T> std::string toStringVector(const T * t, const int n);
88 template<class T> std::string toStringVector(const T & t, const int n);
89 template<class T> std::string toStringVector(const std::vector<T> & t);
91 //--------------------------------------------------------------------
92 // Display a progress %
93 void VerboseInProgress(const int nb, const int current, const int percentage);
94 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
96 //--------------------------------------------------------------------
97 // Convert a pixel type to another (downcast)
98 template<class TPixelUp, class TPixelDown>
99 TPixelDown PixelTypeDownCast(const TPixelUp & x);
101 float PixelTypeDownCast(const double & x);
103 //--------------------------------------------------------------------
104 // Return the indexes of sorted values in a vector
105 template<class Type> struct vectorComparisonLowerThan;
106 template<class Type> struct vectorComparisonGreaterThan;
108 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
110 //--------------------------------------------------------------------
111 // Return the name of a type as a string
112 template<class TPixel>
113 std::string GetTypeAsString();
115 //--------------------------------------------------------------------
116 // Convert radian / degree
117 double rad2deg(double anglerad);
118 double deg2rad(double anglerad);
120 //--------------------------------------------------------------------
121 int GetTypeSizeFromString(const std::string & type);
123 //--------------------------------------------------------------------
124 // Special case to handle "signed_char = schar" ...
126 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
127 template<> bool IsSameType<char>(std::string t);
128 template<> bool IsSameType<signed char>(std::string t);
129 template<> bool IsSameType<unsigned char>(std::string t);
130 template<> bool IsSameType<unsigned short>(std::string t);
132 //--------------------------------------------------------------------
134 std::string CreateListOfTypes(bool last=true) {
135 return GetTypeAsString<T1>();
138 template<class T1, class T2>
139 std::string CreateListOfTypes(bool last=true) {
140 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
141 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
144 template<class T1, class T2, class T3>
145 std::string CreateListOfTypes(bool last=true) {
146 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
147 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
150 template<class T1, class T2, class T3, class T4>
151 std::string CreateListOfTypes(bool last=true) {
152 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
153 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
156 template<class T1, class T2, class T3, class T4, class T5>
157 std::string CreateListOfTypes(bool last=true) {
158 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
159 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
162 template<class T1, class T2, class T3, class T4, class T5, class T6>
163 std::string CreateListOfTypes(bool last=true) {
164 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
165 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
168 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
169 std::string CreateListOfTypes(bool last=true) {
170 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
171 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
174 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
175 std::string CreateListOfTypes(bool last=true) {
176 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
177 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
179 //--------------------------------------------------------------------
181 //--------------------------------------------------------------------
182 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
183 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
184 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
185 //--------------------------------------------------------------------
187 //--------------------------------------------------------------------
188 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
189 const itk::ContinuousIndex<double, 3> pointInPlane,
190 const itk::ContinuousIndex<double, 3> normalPlane);
192 //--------------------------------------------------------------------
193 // Open a File for reading/writing
194 void openFileForReading(std::ifstream & is, const std::string & filename);
195 void openFileForWriting(std::ofstream & os, const std::string & filename);
197 //--------------------------------------------------------------------
198 double cotan(double i);
199 double invcotan(double i);
201 //--------------------------------------------------------------------
202 void disableStdCerr();
203 void enableStdCerr();
205 //--------------------------------------------------------------------
206 template<class ImageType>
207 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
209 #include "clitkCommon.txx"
213 #endif /* end #define CLITKCOMMON_H */