4 -------------------------------------------------------------------
6 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
7 * @date 17 May 2006 07:57:56
11 -------------------------------------------------------------------*/
15 #include "clitkConfiguration.h"
16 #include "clitkPortability.h"
18 // itk include (include std)
19 #include <itkContinuousIndex.h>
21 #include <itkImageRegionConstIterator.h>
22 #include <itkImageRegionIterator.h>
24 //--------------------------------------------------------------------
27 typedef unsigned char uchar;
28 typedef unsigned short ushort;
29 typedef unsigned int uint;
31 //--------------------------------------------------------------------
33 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
34 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;}
36 //--------------------------------------------------------------------
37 // when everything goes wrong
38 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
39 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
41 //--------------------------------------------------------------------
42 // GGO with modified struct name
43 #define GGO(ggo_filename, args_info) \
44 args_info_##ggo_filename args_info; \
45 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
46 if (args_info.config_given) \
47 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
48 else cmdline_parser(argc, argv, &args_info);
50 //--------------------------------------------------------------------
52 void skipComment(std::istream & is);
54 //--------------------------------------------------------------------
55 // linear (rough) conversion from Hounsfield Unit to density
56 double HU2density(double HU);
58 //--------------------------------------------------------------------
59 // Return filename extension
60 std::string GetExtension(const std::string& filename);
62 //--------------------------------------------------------------------
63 // Convert float, double ... to string
64 template<class T> std::string toString(const T & t);
65 template<class T> std::string toStringVector(const T * t, const int n);
66 template<class T> std::string toStringVector(const T & t, const int n);
67 template<class T> std::string toStringVector(const std::vector<T> & t);
69 //--------------------------------------------------------------------
70 // Display a progress %
71 void VerboseInProgress(const int nb, const int current, const int percentage);
72 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
74 //--------------------------------------------------------------------
75 // Convert a pixel type to another (downcast)
76 template<class TPixelUp, class TPixelDown>
77 TPixelDown PixelTypeDownCast(const TPixelUp & x);
79 float PixelTypeDownCast(const double & x);
81 //--------------------------------------------------------------------
82 // Return the indexes of sorted values in a vector
83 template<class Type> struct vectorComparisonLowerThan;
84 template<class Type> struct vectorComparisonGreaterThan;
86 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
88 //--------------------------------------------------------------------
89 // Return the name of a type as a string
90 template<class TPixel>
91 std::string GetTypeAsString();
93 //--------------------------------------------------------------------
94 // Convert radian / degree
95 double rad2deg(double anglerad);
96 double deg2rad(double anglerad);
98 //--------------------------------------------------------------------
99 int GetTypeSizeFromString(const std::string & type);
101 //--------------------------------------------------------------------
102 // Special case to handle "signed_char = schar" ...
104 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
105 template<> bool IsSameType<char>(std::string t);
106 template<> bool IsSameType<signed char>(std::string t);
107 template<> bool IsSameType<unsigned char>(std::string t);
108 template<> bool IsSameType<unsigned short>(std::string t);
110 //--------------------------------------------------------------------
112 std::string CreateListOfTypes(bool last=true) {
113 return GetTypeAsString<T1>();
116 template<class T1, class T2>
117 std::string CreateListOfTypes(bool last=true) {
118 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
119 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
122 template<class T1, class T2, class T3>
123 std::string CreateListOfTypes(bool last=true) {
124 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
125 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
128 template<class T1, class T2, class T3, class T4>
129 std::string CreateListOfTypes(bool last=true) {
130 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
131 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
134 template<class T1, class T2, class T3, class T4, class T5>
135 std::string CreateListOfTypes(bool last=true) {
136 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
137 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
140 template<class T1, class T2, class T3, class T4, class T5, class T6>
141 std::string CreateListOfTypes(bool last=true) {
142 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
143 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
146 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
147 std::string CreateListOfTypes(bool last=true) {
148 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
149 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
152 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
153 std::string CreateListOfTypes(bool last=true) {
154 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
155 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
157 //--------------------------------------------------------------------
159 //--------------------------------------------------------------------
160 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
161 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
162 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
163 //--------------------------------------------------------------------
165 //--------------------------------------------------------------------
166 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
167 const itk::ContinuousIndex<double, 3> pointInPlane,
168 const itk::ContinuousIndex<double, 3> normalPlane);
170 //--------------------------------------------------------------------
171 // Open a File for reading/writing
172 void openFileForReading(std::ifstream & is, const std::string & filename);
173 void openFileForWriting(std::ofstream & os, const std::string & filename);
175 //--------------------------------------------------------------------
176 double cotan(double i);
177 double invcotan(double i);
179 //--------------------------------------------------------------------
180 void disableStdCerr();
181 void enableStdCerr();
183 //--------------------------------------------------------------------
184 template<class ImageType>
185 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
187 #include "clitkCommon.txx"
191 #endif /* end #define CLITKCOMMON_H */