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>
46 #define VTK_EXCLUDE_STRSTREAM_HEADERS
48 //--------------------------------------------------------------------
51 typedef unsigned char uchar;
52 typedef unsigned short ushort;
53 typedef unsigned int uint;
55 #define CLITK_TRY_CATCH_EXIT(func) \
59 catch (const itk::ExceptionObject& e) { \
63 catch (const std::exception& e) { \
64 std::cout << e.what() << std::endl; \
68 std::cout << "Unknown excpetion" << std::endl; \
76 //--------------------------------------------------------------------
77 // when everything goes wrong
78 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
79 #define FATAL(a) { std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0); }
81 //--------------------------------------------------------------------
82 // GGO with modified struct name
83 #define GGO(ggo_filename, args_info) \
84 args_info_##ggo_filename args_info; \
85 cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0); \
86 if (args_info.config_given) \
87 cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
88 else cmdline_parser_##ggo_filename(argc, argv, &args_info);
90 //--------------------------------------------------------------------
92 void skipComment(std::istream & is);
94 //--------------------------------------------------------------------
95 // linear (rough) conversion from Hounsfield Unit to density
96 double HU2density(double HU);
98 //--------------------------------------------------------------------
99 // Return filename extension
100 std::string GetExtension(const std::string& filename);
102 // Return filename splitting in 1 or 2 parts : directory name (if exists) & filename
103 std::vector<std::string> SplitFilename(const std::string& filename);
105 //--------------------------------------------------------------------
106 // Convert float, double ... to string
107 template<class T> std::string toString(const T & t);
108 template<class T> std::string toStringVector(const T * t, const int n);
109 template<class T> std::string toStringVector(const T & t, const int n);
110 template<class T> std::string toStringVector(const std::vector<T> & t);
111 template <class T> bool fromString(T& t,
112 const std::string& s,
113 std::ios_base& (*f)(std::ios_base&)=std::dec);
115 //--------------------------------------------------------------------
116 // Display a progress %
117 void VerboseInProgress(const int nb, const int current, const int percentage);
118 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
120 //--------------------------------------------------------------------
121 // Convert a pixel type to another (downcast)
122 template<class TPixelUp, class TPixelDown>
123 TPixelDown PixelTypeDownCast(const TPixelUp & x);
125 float PixelTypeDownCast(const double & x);
127 double PixelTypeDownCast(const double & x);
129 //--------------------------------------------------------------------
130 // Return the indexes of sorted values in a vector
131 template<class Type> struct vectorComparisonLowerThan;
132 template<class Type> struct vectorComparisonGreaterThan;
134 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
136 //--------------------------------------------------------------------
137 // Return the name of a type as a string
138 template<class TPixel>
139 std::string GetTypeAsString();
141 //--------------------------------------------------------------------
142 // Convert radian / degree
143 double rad2deg(double anglerad);
144 double deg2rad(double anglerad);
146 //--------------------------------------------------------------------
147 int GetTypeSizeFromString(const std::string & type);
149 //--------------------------------------------------------------------
150 // Special case to handle "signed_char = schar" ...
152 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
153 template<> bool IsSameType<char>(std::string t);
154 template<> bool IsSameType<signed char>(std::string t);
155 template<> bool IsSameType<unsigned char>(std::string t);
156 template<> bool IsSameType<unsigned short>(std::string t);
158 //--------------------------------------------------------------------
160 std::string CreateListOfTypes(bool last=true) {
161 return GetTypeAsString<T1>();
164 template<class T1, class T2>
165 std::string CreateListOfTypes(bool last=true) {
166 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
167 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
170 template<class T1, class T2, class T3>
171 std::string CreateListOfTypes(bool last=true) {
172 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
173 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
176 template<class T1, class T2, class T3, class T4>
177 std::string CreateListOfTypes(bool last=true) {
178 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
179 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
182 template<class T1, class T2, class T3, class T4, class T5>
183 std::string CreateListOfTypes(bool last=true) {
184 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
185 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
188 template<class T1, class T2, class T3, class T4, class T5, class T6>
189 std::string CreateListOfTypes(bool last=true) {
190 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
191 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
194 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
195 std::string CreateListOfTypes(bool last=true) {
196 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
197 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
200 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
201 std::string CreateListOfTypes(bool last=true) {
202 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
203 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
205 //--------------------------------------------------------------------
207 //--------------------------------------------------------------------
208 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
209 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
210 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
211 //--------------------------------------------------------------------
213 //--------------------------------------------------------------------
214 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
215 const itk::ContinuousIndex<double, 3> pointInPlane,
216 const itk::ContinuousIndex<double, 3> normalPlane);
218 //--------------------------------------------------------------------
219 // Open a File for reading/writing
220 void openFileForReading(std::ifstream & is, const std::string & filename);
221 void openFileForWriting(std::ofstream & os, const std::string & filename);
223 //--------------------------------------------------------------------
224 void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
226 //--------------------------------------------------------------------
227 double cotan(double i);
228 double invcotan(double i);
230 //--------------------------------------------------------------------
231 void disableStdCerr();
232 void enableStdCerr();
234 //--------------------------------------------------------------------
235 template<class ImageType>
236 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
238 //--------------------------------------------------------------------
239 void PrintMemoryUsed();
241 //--------------------------------------------------------------------
242 // Convert a map to a vector
243 template <typename M, typename V>
244 void MapToVecFirst(const M & m, V & v);
245 template <typename M, typename V>
246 void MapToVecSecond(const M & m, V & v);
248 //--------------------------------------------------------------------
249 // Find/replace string
251 int inline findAndReplace(T& source, const T& find, const T& replace);
253 #include "clitkCommon.txx"
257 #endif /* end #define CLITKCOMMON_H */