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