1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
23 #include "clitkPortability.h"
25 #include "clitkExceptionObject.h"
27 // itk include (include std)
28 #include <itkContinuousIndex.h>
30 #include <itkImageRegionConstIterator.h>
31 #include <itkImageRegionIterator.h>
36 // Include for "rusage"
38 #if defined(unix) || defined(__APPLE__)
39 # include <sys/time.h>
40 # include <sys/resource.h>
45 #if ITK_VERSION_MAJOR < 4
48 typedef unsigned int ThreadIdType;
52 //--------------------------------------------------------------------
55 typedef unsigned char uchar;
56 typedef unsigned short ushort;
57 typedef unsigned int uint;
59 #define CLITK_TRY_CATCH_EXIT(func) \
63 catch (const itk::ExceptionObject& e) { \
67 catch (const std::exception& e) { \
68 std::cout << e.what() << std::endl; \
72 std::cout << "Unknown excpetion" << std::endl; \
77 //--------------------------------------------------------------------
78 // when everything goes wrong
79 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
80 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
82 //--------------------------------------------------------------------
83 // GGO with modified struct name
84 #define GGO(ggo_filename, args_info) \
85 args_info_##ggo_filename args_info; \
86 cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0); \
87 if (args_info.config_given) \
88 cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
89 else cmdline_parser_##ggo_filename(argc, argv, &args_info);
91 //--------------------------------------------------------------------
93 void skipComment(std::istream & is);
95 //--------------------------------------------------------------------
96 // linear (rough) conversion from Hounsfield Unit to density
97 double HU2density(double HU);
99 //--------------------------------------------------------------------
100 // Return filename extension
101 std::string GetExtension(const std::string& filename);
103 //--------------------------------------------------------------------
104 // Convert float, double ... to string
105 template<class T> std::string toString(const T & t);
106 template<class T> std::string toStringVector(const T * t, const int n);
107 template<class T> std::string toStringVector(const T & t, const int n);
108 template<class T> std::string toStringVector(const std::vector<T> & t);
109 template <class T> bool fromString(T& t,
110 const std::string& s,
111 std::ios_base& (*f)(std::ios_base&)=std::dec);
113 //--------------------------------------------------------------------
114 // Display a progress %
115 void VerboseInProgress(const int nb, const int current, const int percentage);
116 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
118 //--------------------------------------------------------------------
119 // Convert a pixel type to another (downcast)
120 template<class TPixelUp, class TPixelDown>
121 TPixelDown PixelTypeDownCast(const TPixelUp & x);
123 float PixelTypeDownCast(const double & x);
125 //--------------------------------------------------------------------
126 // Return the indexes of sorted values in a vector
127 template<class Type> struct vectorComparisonLowerThan;
128 template<class Type> struct vectorComparisonGreaterThan;
130 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
132 //--------------------------------------------------------------------
133 // Return the name of a type as a string
134 template<class TPixel>
135 std::string GetTypeAsString();
137 //--------------------------------------------------------------------
138 // Convert radian / degree
139 double rad2deg(double anglerad);
140 double deg2rad(double anglerad);
142 //--------------------------------------------------------------------
143 int GetTypeSizeFromString(const std::string & type);
145 //--------------------------------------------------------------------
146 // Special case to handle "signed_char = schar" ...
148 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
149 template<> bool IsSameType<char>(std::string t);
150 template<> bool IsSameType<signed char>(std::string t);
151 template<> bool IsSameType<unsigned char>(std::string t);
152 template<> bool IsSameType<unsigned short>(std::string t);
154 //--------------------------------------------------------------------
156 std::string CreateListOfTypes(bool last=true) {
157 return GetTypeAsString<T1>();
160 template<class T1, class T2>
161 std::string CreateListOfTypes(bool last=true) {
162 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
163 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
166 template<class T1, class T2, class T3>
167 std::string CreateListOfTypes(bool last=true) {
168 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
169 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
172 template<class T1, class T2, class T3, class T4>
173 std::string CreateListOfTypes(bool last=true) {
174 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
175 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
178 template<class T1, class T2, class T3, class T4, class T5>
179 std::string CreateListOfTypes(bool last=true) {
180 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
181 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
184 template<class T1, class T2, class T3, class T4, class T5, class T6>
185 std::string CreateListOfTypes(bool last=true) {
186 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
187 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
190 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
191 std::string CreateListOfTypes(bool last=true) {
192 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
193 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
196 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
197 std::string CreateListOfTypes(bool last=true) {
198 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
199 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
201 //--------------------------------------------------------------------
203 //--------------------------------------------------------------------
204 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
205 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
206 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
207 //--------------------------------------------------------------------
209 //--------------------------------------------------------------------
210 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
211 const itk::ContinuousIndex<double, 3> pointInPlane,
212 const itk::ContinuousIndex<double, 3> normalPlane);
214 //--------------------------------------------------------------------
215 // Open a File for reading/writing
216 void openFileForReading(std::ifstream & is, const std::string & filename);
217 void openFileForWriting(std::ofstream & os, const std::string & filename);
219 //--------------------------------------------------------------------
220 void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
222 //--------------------------------------------------------------------
223 double cotan(double i);
224 double invcotan(double i);
226 //--------------------------------------------------------------------
227 void disableStdCerr();
228 void enableStdCerr();
230 //--------------------------------------------------------------------
231 template<class ImageType>
232 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
234 //--------------------------------------------------------------------
235 void PrintMemoryUsed();
237 //--------------------------------------------------------------------
238 // Convert a map to a vector
239 template <typename M, typename V>
240 void MapToVecFirst(const M & m, V & v);
241 template <typename M, typename V>
242 void MapToVecSecond(const M & m, V & v);
244 #include "clitkCommon.txx"
248 #endif /* end #define CLITKCOMMON_H */