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 ======================================================================-====*/
18 #ifndef CLITKCOMMON_TXX
19 #define CLITKCOMMON_TXX
21 -------------------------------------------------
22 * @file clitkCommon.txx
23 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
26 -------------------------------------------------*/
28 //--------------------------------------------------------------------
29 // Convert float, double ... to string
31 std::string toString(const T & t)
33 std::ostringstream myStream;
34 myStream << t << std::flush;
35 return(myStream.str());
37 //--------------------------------------------------------------------
39 //--------------------------------------------------------------------
40 // Convert float*, double* ... to string
42 std::string toStringVector(const T * t, const int n)
44 std::ostringstream myStream;
45 for(int i=0; i<n-1; i++)
46 myStream << clitk::toString<T>(t[i]) << " ";
47 myStream << clitk::toString<T>(t[n-1]) << std::flush;
48 return(myStream.str());
50 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 // Convert float*, double* ... to string
55 std::string toStringVector(const T & t, const int n)
57 std::ostringstream myStream;
58 for(int i=0; i<n-1; i++)
59 myStream << t[i] << " ";
60 myStream << t[n-1] << std::flush;
61 return(myStream.str());
63 //--------------------------------------------------------------------
65 //--------------------------------------------------------------------
66 // Convert float*, double* ... to string
68 std::string toStringVector(const std::vector<T> & t)
70 return toStringVector(&t[0], t.size());
72 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 // Convert a pixel type to another (downcast)
76 template<class TPixelUp, class TPixelDown>
77 TPixelDown PixelTypeDownCast(const TPixelUp & x)
79 return (TPixelDown)lrint(x);
81 //--------------------------------------------------------------------
83 //--------------------------------------------------------------------
85 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
86 vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
87 bool operator()(int x, int y) {
88 return (vect[x] < vect[y]);
90 const std::vector<Type> & vect;
92 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
96 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
97 vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
98 bool operator()(int x, int y) {
99 return (vect[x] > vect[y]);
101 const std::vector<Type> & vect;
103 //--------------------------------------------------------------------
105 //--------------------------------------------------------------------
107 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
109 index.resize(toSort.size());
110 for(unsigned int i=0; i<index.size(); i++) index[i] = i;
112 std::sort(index.begin(),
114 vectorComparisonLowerThan<double>(toSort));
116 std::sort(index.begin(),
118 vectorComparisonGreaterThan<double>(toSort));
120 //--------------------------------------------------------------------
122 //--------------------------------------------------------------------
123 template<class TPixel>
124 std::string GetTypeAsString()
126 // http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
128 // itkImageIOBase.cxx
129 const std::type_info & PixType = typeid(TPixel);
130 std::string pixelName;
131 if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
132 else if (PixType == typeid(signed char)) pixelName = "signed_char";
133 else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
134 else if (PixType == typeid(short)) pixelName = "short";
135 else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
136 else if (PixType == typeid(int)) pixelName = "int";
137 else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
138 else if (PixType == typeid(float)) pixelName = "float";
139 else if (PixType == typeid(double)) pixelName = "double";
140 else pixelName = PixType.name();
143 //--------------------------------------------------------------------
145 //--------------------------------------------------------------------
146 template<class ImageType>
147 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
149 output->SetRegions(input->GetLargestPossibleRegion());
150 output->SetOrigin(input->GetOrigin());
151 output->SetSpacing(input->GetSpacing());
153 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
154 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
156 typedef itk::ImageRegionIterator<ImageType> IteratorType;
157 IteratorType po(output,input->GetLargestPossibleRegion());
159 while (!pi.IsAtEnd()) {
165 //--------------------------------------------------------------------
167 #endif /* end #define CLITKCOMMON_TXX */