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://oncora1.lyon.fnclcc.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>
35 //--------------------------------------------------------------------
38 typedef unsigned char uchar;
39 typedef unsigned short ushort;
40 typedef unsigned int uint;
42 //--------------------------------------------------------------------
43 // when everything goes wrong
44 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
45 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
47 //--------------------------------------------------------------------
48 // GGO with modified struct name
49 #define GGO(ggo_filename, args_info) \
50 args_info_##ggo_filename args_info; \
51 cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0); \
52 if (args_info.config_given) \
53 cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
54 else cmdline_parser_##ggo_filename(argc, argv, &args_info);
56 //--------------------------------------------------------------------
58 void skipComment(std::istream & is);
60 //--------------------------------------------------------------------
61 // linear (rough) conversion from Hounsfield Unit to density
62 double HU2density(double HU);
64 //--------------------------------------------------------------------
65 // Return filename extension
66 std::string GetExtension(const std::string& filename);
68 //--------------------------------------------------------------------
69 // Convert float, double ... to string
70 template<class T> std::string toString(const T & t);
71 template<class T> std::string toStringVector(const T * t, const int n);
72 template<class T> std::string toStringVector(const T & t, const int n);
73 template<class T> std::string toStringVector(const std::vector<T> & t);
75 //--------------------------------------------------------------------
76 // Display a progress %
77 void VerboseInProgress(const int nb, const int current, const int percentage);
78 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
80 //--------------------------------------------------------------------
81 // Convert a pixel type to another (downcast)
82 template<class TPixelUp, class TPixelDown>
83 TPixelDown PixelTypeDownCast(const TPixelUp & x);
85 float PixelTypeDownCast(const double & x);
87 //--------------------------------------------------------------------
88 // Return the indexes of sorted values in a vector
89 template<class Type> struct vectorComparisonLowerThan;
90 template<class Type> struct vectorComparisonGreaterThan;
92 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
94 //--------------------------------------------------------------------
95 // Return the name of a type as a string
96 template<class TPixel>
97 std::string GetTypeAsString();
99 //--------------------------------------------------------------------
100 // Convert radian / degree
101 double rad2deg(double anglerad);
102 double deg2rad(double anglerad);
104 //--------------------------------------------------------------------
105 int GetTypeSizeFromString(const std::string & type);
107 //--------------------------------------------------------------------
108 // Special case to handle "signed_char = schar" ...
110 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
111 template<> bool IsSameType<char>(std::string t);
112 template<> bool IsSameType<signed char>(std::string t);
113 template<> bool IsSameType<unsigned char>(std::string t);
114 template<> bool IsSameType<unsigned short>(std::string t);
116 //--------------------------------------------------------------------
118 std::string CreateListOfTypes(bool last=true) {
119 return GetTypeAsString<T1>();
122 template<class T1, class T2>
123 std::string CreateListOfTypes(bool last=true) {
124 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
125 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
128 template<class T1, class T2, class T3>
129 std::string CreateListOfTypes(bool last=true) {
130 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
131 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
134 template<class T1, class T2, class T3, class T4>
135 std::string CreateListOfTypes(bool last=true) {
136 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
137 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
140 template<class T1, class T2, class T3, class T4, class T5>
141 std::string CreateListOfTypes(bool last=true) {
142 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
143 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
146 template<class T1, class T2, class T3, class T4, class T5, class T6>
147 std::string CreateListOfTypes(bool last=true) {
148 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
149 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
152 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
153 std::string CreateListOfTypes(bool last=true) {
154 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
155 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
158 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
159 std::string CreateListOfTypes(bool last=true) {
160 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
161 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
163 //--------------------------------------------------------------------
165 //--------------------------------------------------------------------
166 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
167 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
168 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
169 //--------------------------------------------------------------------
171 //--------------------------------------------------------------------
172 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
173 const itk::ContinuousIndex<double, 3> pointInPlane,
174 const itk::ContinuousIndex<double, 3> normalPlane);
176 //--------------------------------------------------------------------
177 // Open a File for reading/writing
178 void openFileForReading(std::ifstream & is, const std::string & filename);
179 void openFileForWriting(std::ofstream & os, const std::string & filename);
181 //--------------------------------------------------------------------
182 void readDoubleFromFile(const std::string & filename, std::vector<double> & list);
184 //--------------------------------------------------------------------
185 double cotan(double i);
186 double invcotan(double i);
188 //--------------------------------------------------------------------
189 void disableStdCerr();
190 void enableStdCerr();
192 //--------------------------------------------------------------------
193 template<class ImageType>
194 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
196 #include "clitkCommon.txx"
200 #endif /* end #define CLITKCOMMON_H */