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) {
32 std::ostringstream myStream;
33 myStream << t << std::flush;
34 return(myStream.str());
36 //--------------------------------------------------------------------
38 //--------------------------------------------------------------------
39 // Convert float*, double* ... to string
41 std::string toStringVector(const T * t, const int n) {
42 std::ostringstream myStream;
43 for(int i=0; i<n-1; i++)
44 myStream << clitk::toString<T>(t[i]) << " ";
45 myStream << clitk::toString<T>(t[n-1]) << std::flush;
46 return(myStream.str());
48 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
51 // Convert float*, double* ... to string
53 std::string toStringVector(const T & t, const int n) {
54 std::ostringstream myStream;
55 for(int i=0; i<n-1; i++)
56 myStream << t[i] << " ";
57 myStream << t[n-1] << std::flush;
58 return(myStream.str());
60 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 // Convert float*, double* ... to string
65 std::string toStringVector(const std::vector<T> & t) {
66 return toStringVector(&t[0], t.size());
68 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 // Convert a pixel type to another (downcast)
72 template<class TPixelUp, class TPixelDown>
73 TPixelDown PixelTypeDownCast(const TPixelUp & x) {
74 return (TPixelDown)lrint(x);
76 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
80 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
81 vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
82 bool operator()(int x, int y) {
83 return (vect[x] < vect[y]);
85 const std::vector<Type> & vect;
87 //--------------------------------------------------------------------
89 //--------------------------------------------------------------------
91 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
92 vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
93 bool operator()(int x, int y) {
94 return (vect[x] > vect[y]);
96 const std::vector<Type> & vect;
98 //--------------------------------------------------------------------
100 //--------------------------------------------------------------------
102 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing) {
103 index.resize(toSort.size());
104 for(unsigned int i=0; i<index.size(); i++) index[i] = i;
106 std::sort(index.begin(),
108 vectorComparisonLowerThan<double>(toSort));
110 std::sort(index.begin(),
112 vectorComparisonGreaterThan<double>(toSort));
114 //--------------------------------------------------------------------
116 //--------------------------------------------------------------------
117 template<class TPixel>
118 std::string GetTypeAsString() {
119 // http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
121 // itkImageIOBase.cxx
122 const std::type_info & PixType = typeid(TPixel);
123 std::string pixelName;
124 if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
125 else if (PixType == typeid(signed char)) pixelName = "signed_char";
126 else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
127 else if (PixType == typeid(short)) pixelName = "short";
128 else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
129 else if (PixType == typeid(int)) pixelName = "int";
130 else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
131 else if (PixType == typeid(float)) pixelName = "float";
132 else if (PixType == typeid(double)) pixelName = "double";
133 else pixelName = PixType.name();
136 //--------------------------------------------------------------------
138 //--------------------------------------------------------------------
139 template<class ImageType>
140 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) {
141 output->SetRegions(input->GetLargestPossibleRegion());
142 output->SetOrigin(input->GetOrigin());
143 output->SetSpacing(input->GetSpacing());
145 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
146 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
148 typedef itk::ImageRegionIterator<ImageType> IteratorType;
149 IteratorType po(output,input->GetLargestPossibleRegion());
151 while (!pi.IsAtEnd()) {
157 //--------------------------------------------------------------------
159 #endif /* end #define CLITKCOMMON_TXX */