5 -------------------------------------------------------------------
7 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
8 * @date 17 May 2006 07:57:56
12 -------------------------------------------------------------------*/
15 #include "clitkPortability.h"
17 // itk include (include std)
18 #include <itkContinuousIndex.h>
20 #include <itkImageRegionConstIterator.h>
21 #include <itkImageRegionIterator.h>
23 //--------------------------------------------------------------------
26 typedef unsigned char uchar;
27 typedef unsigned short ushort;
28 typedef unsigned int uint;
30 //--------------------------------------------------------------------
32 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
33 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;}
35 //--------------------------------------------------------------------
36 // when everything goes wrong
37 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
38 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
40 //--------------------------------------------------------------------
41 // GGO with modified struct name
42 #define GGO(ggo_filename, args_info) \
43 args_info_##ggo_filename args_info; \
44 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
45 if (args_info.config_given) \
46 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
47 else cmdline_parser(argc, argv, &args_info);
49 //--------------------------------------------------------------------
51 void skipComment(std::istream & is);
53 //--------------------------------------------------------------------
54 // linear (rough) conversion from Hounsfield Unit to density
55 double HU2density(double HU);
57 //--------------------------------------------------------------------
58 // Return filename extension
59 std::string GetExtension(const std::string& filename);
61 //--------------------------------------------------------------------
62 // Convert float, double ... to string
63 template<class T> std::string toString(const T & t);
64 template<class T> std::string toStringVector(const T * t, const int n);
65 template<class T> std::string toStringVector(const T & t, const int n);
66 template<class T> std::string toStringVector(const std::vector<T> & t);
68 //--------------------------------------------------------------------
69 // Display a progress %
70 void VerboseInProgress(const int nb, const int current, const int percentage);
71 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
73 //--------------------------------------------------------------------
74 // Convert a pixel type to another (downcast)
75 template<class TPixelUp, class TPixelDown>
76 TPixelDown PixelTypeDownCast(const TPixelUp & x);
78 float PixelTypeDownCast(const double & x);
80 //--------------------------------------------------------------------
81 // Return the indexes of sorted values in a vector
82 template<class Type> struct vectorComparisonLowerThan;
83 template<class Type> struct vectorComparisonGreaterThan;
85 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
87 //--------------------------------------------------------------------
88 // Return the name of a type as a string
89 template<class TPixel>
90 std::string GetTypeAsString();
92 //--------------------------------------------------------------------
93 // Convert radian / degree
94 double rad2deg(double anglerad);
95 double deg2rad(double anglerad);
97 //--------------------------------------------------------------------
98 int GetTypeSizeFromString(const std::string & type);
100 //--------------------------------------------------------------------
101 // Special case to handle "signed_char = schar" ...
103 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
104 template<> bool IsSameType<char>(std::string t);
105 template<> bool IsSameType<signed char>(std::string t);
106 template<> bool IsSameType<unsigned char>(std::string t);
107 template<> bool IsSameType<unsigned short>(std::string t);
109 //--------------------------------------------------------------------
111 std::string CreateListOfTypes(bool last=true) {
112 return GetTypeAsString<T1>();
115 template<class T1, class T2>
116 std::string CreateListOfTypes(bool last=true) {
117 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
118 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
121 template<class T1, class T2, class T3>
122 std::string CreateListOfTypes(bool last=true) {
123 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
124 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
127 template<class T1, class T2, class T3, class T4>
128 std::string CreateListOfTypes(bool last=true) {
129 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
130 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
133 template<class T1, class T2, class T3, class T4, class T5>
134 std::string CreateListOfTypes(bool last=true) {
135 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
136 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
139 template<class T1, class T2, class T3, class T4, class T5, class T6>
140 std::string CreateListOfTypes(bool last=true) {
141 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
142 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
145 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
146 std::string CreateListOfTypes(bool last=true) {
147 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
148 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
151 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
152 std::string CreateListOfTypes(bool last=true) {
153 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
154 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
156 //--------------------------------------------------------------------
158 //--------------------------------------------------------------------
159 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
160 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
161 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
162 //--------------------------------------------------------------------
164 //--------------------------------------------------------------------
165 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
166 const itk::ContinuousIndex<double, 3> pointInPlane,
167 const itk::ContinuousIndex<double, 3> normalPlane);
169 //--------------------------------------------------------------------
170 // Open a File for reading/writing
171 void openFileForReading(std::ifstream & is, const std::string & filename);
172 void openFileForWriting(std::ofstream & os, const std::string & filename);
174 //--------------------------------------------------------------------
175 double cotan(double i);
176 double invcotan(double i);
178 //--------------------------------------------------------------------
179 void disableStdCerr();
180 void enableStdCerr();
182 //--------------------------------------------------------------------
183 template<class ImageType>
184 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
186 #include "clitkCommon.txx"
190 #endif /* end #define CLITKCOMMON_H */