1 #ifndef CLITKCOMMON_TXX
2 #define CLITKCOMMON_TXX
4 -------------------------------------------------
5 * @file clitkCommon.txx
6 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
9 -------------------------------------------------*/
11 //--------------------------------------------------------------------
12 // Convert float, double ... to string
14 std::string toString(const T & t) {
15 std::ostringstream myStream;
16 myStream << t << std::flush;
17 return(myStream.str());
19 //--------------------------------------------------------------------
21 //--------------------------------------------------------------------
22 // Convert float*, double* ... to string
24 std::string toStringVector(const T * t, const int n) {
25 std::ostringstream myStream;
26 for(int i=0; i<n-1; i++)
27 myStream << clitk::toString<T>(t[i]) << " ";
28 myStream << clitk::toString<T>(t[n-1]) << std::flush;
29 return(myStream.str());
31 //--------------------------------------------------------------------
33 //--------------------------------------------------------------------
34 // Convert float*, double* ... to string
36 std::string toStringVector(const T & t, const int n) {
37 std::ostringstream myStream;
38 for(int i=0; i<n-1; i++)
39 myStream << t[i] << " ";
40 myStream << t[n-1] << std::flush;
41 return(myStream.str());
43 //--------------------------------------------------------------------
45 //--------------------------------------------------------------------
46 // Convert float*, double* ... to string
48 std::string toStringVector(const std::vector<T> & t) {
49 return toStringVector(&t[0], t.size());
51 //--------------------------------------------------------------------
53 //--------------------------------------------------------------------
54 // Convert a pixel type to another (downcast)
55 template<class TPixelUp, class TPixelDown>
56 TPixelDown PixelTypeDownCast(const TPixelUp & x) {
57 return (TPixelDown)lrint(x);
59 //--------------------------------------------------------------------
61 //--------------------------------------------------------------------
63 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
64 vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
65 bool operator()(int x, int y) {
66 return (vect[x] < vect[y]);
68 const std::vector<Type> & vect;
70 //--------------------------------------------------------------------
72 //--------------------------------------------------------------------
74 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
75 vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
76 bool operator()(int x, int y) {
77 return (vect[x] > vect[y]);
79 const std::vector<Type> & vect;
81 //--------------------------------------------------------------------
83 //--------------------------------------------------------------------
85 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing) {
86 index.resize(toSort.size());
87 for(unsigned int i=0; i<index.size(); i++) index[i] = i;
89 std::sort(index.begin(),
91 vectorComparisonLowerThan<double>(toSort));
93 std::sort(index.begin(),
95 vectorComparisonGreaterThan<double>(toSort));
97 //--------------------------------------------------------------------
99 //--------------------------------------------------------------------
100 template<class TPixel>
101 std::string GetTypeAsString() {
102 // http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
104 // itkImageIOBase.cxx
105 const std::type_info & PixType = typeid(TPixel);
106 std::string pixelName;
107 if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
108 else if (PixType == typeid(signed char)) pixelName = "signed_char";
109 else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
110 else if (PixType == typeid(short)) pixelName = "short";
111 else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
112 else if (PixType == typeid(int)) pixelName = "int";
113 else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
114 else if (PixType == typeid(float)) pixelName = "float";
115 else if (PixType == typeid(double)) pixelName = "double";
116 else pixelName = PixType.name();
119 //--------------------------------------------------------------------
121 //--------------------------------------------------------------------
122 template<class ImageType>
123 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) {
124 output->SetRegions(input->GetLargestPossibleRegion());
125 output->SetOrigin(input->GetOrigin());
126 output->SetSpacing(input->GetSpacing());
128 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
129 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
131 typedef itk::ImageRegionIterator<ImageType> IteratorType;
132 IteratorType po(output,input->GetLargestPossibleRegion());
134 while (!pi.IsAtEnd()) {
140 //--------------------------------------------------------------------
142 #endif /* end #define CLITKCOMMON_TXX */