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 ======================================================================-====*/
21 -------------------------------------------------------------------
23 * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
24 * @date 17 May 2006 07:57:56
28 -------------------------------------------------------------------*/
32 #include "clitkConfiguration.h"
33 #include "clitkPortability.h"
35 // itk include (include std)
36 #include <itkContinuousIndex.h>
38 #include <itkImageRegionConstIterator.h>
39 #include <itkImageRegionIterator.h>
41 //--------------------------------------------------------------------
44 typedef unsigned char uchar;
45 typedef unsigned short ushort;
46 typedef unsigned int uint;
48 //--------------------------------------------------------------------
50 #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;
51 #define DDV(a,n) { std::cout << #a " = [ "; for(unsigned int _i_=0; _i_<n; _i_++) { std::cout << a[_i_] << " "; }; std::cout << " ]" << std::endl;}
53 //--------------------------------------------------------------------
54 // when everything goes wrong
55 #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
56 #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0);
58 //--------------------------------------------------------------------
59 // GGO with modified struct name
60 #define GGO(ggo_filename, args_info) \
61 args_info_##ggo_filename args_info; \
62 cmdline_parser2(argc, argv, &args_info, 1, 1, 0); \
63 if (args_info.config_given) \
64 cmdline_parser_configfile (args_info.config_arg, &args_info, 0, 0, 1); \
65 else cmdline_parser(argc, argv, &args_info);
67 //--------------------------------------------------------------------
69 void skipComment(std::istream & is);
71 //--------------------------------------------------------------------
72 // linear (rough) conversion from Hounsfield Unit to density
73 double HU2density(double HU);
75 //--------------------------------------------------------------------
76 // Return filename extension
77 std::string GetExtension(const std::string& filename);
79 //--------------------------------------------------------------------
80 // Convert float, double ... to string
81 template<class T> std::string toString(const T & t);
82 template<class T> std::string toStringVector(const T * t, const int n);
83 template<class T> std::string toStringVector(const T & t, const int n);
84 template<class T> std::string toStringVector(const std::vector<T> & t);
86 //--------------------------------------------------------------------
87 // Display a progress %
88 void VerboseInProgress(const int nb, const int current, const int percentage);
89 void VerboseInProgressInPercentage(const int nb, const int current, const int percentage);
91 //--------------------------------------------------------------------
92 // Convert a pixel type to another (downcast)
93 template<class TPixelUp, class TPixelDown>
94 TPixelDown PixelTypeDownCast(const TPixelUp & x);
96 float PixelTypeDownCast(const double & x);
98 //--------------------------------------------------------------------
99 // Return the indexes of sorted values in a vector
100 template<class Type> struct vectorComparisonLowerThan;
101 template<class Type> struct vectorComparisonGreaterThan;
103 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing=true);
105 //--------------------------------------------------------------------
106 // Return the name of a type as a string
107 template<class TPixel>
108 std::string GetTypeAsString();
110 //--------------------------------------------------------------------
111 // Convert radian / degree
112 double rad2deg(double anglerad);
113 double deg2rad(double anglerad);
115 //--------------------------------------------------------------------
116 int GetTypeSizeFromString(const std::string & type);
118 //--------------------------------------------------------------------
119 // Special case to handle "signed_char = schar" ...
121 bool IsSameType(std::string t) { return (t==GetTypeAsString<T>()); }
122 template<> bool IsSameType<char>(std::string t);
123 template<> bool IsSameType<signed char>(std::string t);
124 template<> bool IsSameType<unsigned char>(std::string t);
125 template<> bool IsSameType<unsigned short>(std::string t);
127 //--------------------------------------------------------------------
129 std::string CreateListOfTypes(bool last=true) {
130 return GetTypeAsString<T1>();
133 template<class T1, class T2>
134 std::string CreateListOfTypes(bool last=true) {
135 if (last) return CreateListOfTypes<T1>()+" and "+CreateListOfTypes<T2>();
136 else return CreateListOfTypes<T1>()+", "+CreateListOfTypes<T2>();
139 template<class T1, class T2, class T3>
140 std::string CreateListOfTypes(bool last=true) {
141 if (last) return CreateListOfTypes<T1,T2>(false)+" and "+CreateListOfTypes<T3>();
142 else return CreateListOfTypes<T1,T2>(false)+", "+CreateListOfTypes<T3>();
145 template<class T1, class T2, class T3, class T4>
146 std::string CreateListOfTypes(bool last=true) {
147 if (last) return CreateListOfTypes<T1,T2,T3>(false)+" and "+CreateListOfTypes<T4>();
148 else return CreateListOfTypes<T1,T2,T3>(false)+", "+CreateListOfTypes<T4>();
151 template<class T1, class T2, class T3, class T4, class T5>
152 std::string CreateListOfTypes(bool last=true) {
153 if (last) return CreateListOfTypes<T1,T2,T3,T4>(false)+" and "+CreateListOfTypes<T5>();
154 else return CreateListOfTypes<T1,T2,T3,T4>(false)+", "+CreateListOfTypes<T5>();
157 template<class T1, class T2, class T3, class T4, class T5, class T6>
158 std::string CreateListOfTypes(bool last=true) {
159 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+" and "+CreateListOfTypes<T6>();
160 else return CreateListOfTypes<T1,T2,T3,T4,T5>(false)+", "+CreateListOfTypes<T6>();
163 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
164 std::string CreateListOfTypes(bool last=true) {
165 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+" and "+CreateListOfTypes<T7>();
166 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6>(false)+", "+CreateListOfTypes<T7>();
169 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
170 std::string CreateListOfTypes(bool last=true) {
171 if (last) return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+" and "+CreateListOfTypes<T8>();
172 else return CreateListOfTypes<T1,T2,T3,T4,T5,T6,T7>(false)+", "+CreateListOfTypes<T8>();
174 //--------------------------------------------------------------------
176 //--------------------------------------------------------------------
177 void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement);
178 void FindAndReplace(std::string & line, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace);
179 void FindAndReplace(std::ifstream & in, const std::vector<std::string> & tofind, const std::vector<std::string> & toreplace, std::ofstream & out);
180 //--------------------------------------------------------------------
182 //--------------------------------------------------------------------
183 double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
184 const itk::ContinuousIndex<double, 3> pointInPlane,
185 const itk::ContinuousIndex<double, 3> normalPlane);
187 //--------------------------------------------------------------------
188 // Open a File for reading/writing
189 void openFileForReading(std::ifstream & is, const std::string & filename);
190 void openFileForWriting(std::ofstream & os, const std::string & filename);
192 //--------------------------------------------------------------------
193 double cotan(double i);
194 double invcotan(double i);
196 //--------------------------------------------------------------------
197 void disableStdCerr();
198 void enableStdCerr();
200 //--------------------------------------------------------------------
201 template<class ImageType>
202 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output);
204 #include "clitkCommon.txx"
208 #endif /* end #define CLITKCOMMON_H */