]> Creatis software - clitk.git/blob - common/clitkCommon.txx
Reformatted using new coding style
[clitk.git] / common / clitkCommon.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
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
20 /**
21    -------------------------------------------------
22    * @file   clitkCommon.txx
23    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24    * @date   18 May 2006
25    *
26    -------------------------------------------------*/
27
28 //--------------------------------------------------------------------
29 // Convert float, double ... to string
30 template<class T>
31 std::string toString(const T & t)
32 {
33   std::ostringstream myStream;
34   myStream << t << std::flush;
35   return(myStream.str());
36 }
37 //--------------------------------------------------------------------
38
39 //--------------------------------------------------------------------
40 // Convert float*, double* ... to string
41 template<class T>
42 std::string toStringVector(const T * t, const int n)
43 {
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());
49 }
50 //--------------------------------------------------------------------
51
52 //--------------------------------------------------------------------
53 // Convert float*, double* ... to string
54 template<class T>
55 std::string toStringVector(const T & t, const int n)
56 {
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());
62 }
63 //--------------------------------------------------------------------
64
65 //--------------------------------------------------------------------
66 // Convert float*, double* ... to string
67 template<class T>
68 std::string toStringVector(const std::vector<T> & t)
69 {
70   return toStringVector(&t[0], t.size());
71 }
72 //--------------------------------------------------------------------
73
74 //--------------------------------------------------------------------
75 // Convert a pixel type to another (downcast)
76 template<class TPixelUp, class TPixelDown>
77 TPixelDown PixelTypeDownCast(const TPixelUp & x)
78 {
79   return (TPixelDown)lrint(x);
80 }
81 //--------------------------------------------------------------------
82
83 //--------------------------------------------------------------------
84 template<class Type>
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]);
89   }
90   const std::vector<Type> & vect;
91 };
92 //--------------------------------------------------------------------
93
94 //--------------------------------------------------------------------
95 template<class Type>
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]);
100   }
101   const std::vector<Type> & vect;
102 };
103 //--------------------------------------------------------------------
104
105 //--------------------------------------------------------------------
106 template<class Type>
107 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
108 {
109   index.resize(toSort.size());
110   for(unsigned int i=0; i<index.size(); i++) index[i] = i;
111   if (increasing)
112     std::sort(index.begin(),
113               index.end(),
114               vectorComparisonLowerThan<double>(toSort));
115   else
116     std::sort(index.begin(),
117               index.end(),
118               vectorComparisonGreaterThan<double>(toSort));
119 }
120 //--------------------------------------------------------------------
121
122 //--------------------------------------------------------------------
123 template<class TPixel>
124 std::string GetTypeAsString()
125 {
126   //  http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
127   // and
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();
141   return pixelName;
142 }
143 //--------------------------------------------------------------------
144
145 //--------------------------------------------------------------------
146 template<class ImageType>
147 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
148 {
149   output->SetRegions(input->GetLargestPossibleRegion());
150   output->SetOrigin(input->GetOrigin());
151   output->SetSpacing(input->GetSpacing());
152   output->Allocate();
153   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
154   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
155   pi.GoToBegin();
156   typedef itk::ImageRegionIterator<ImageType> IteratorType;
157   IteratorType po(output,input->GetLargestPossibleRegion());
158   po.GoToBegin();
159   while (!pi.IsAtEnd()) {
160     po.Set(pi.Get());
161     ++pi;
162     ++po;
163   }
164 }
165 //--------------------------------------------------------------------
166
167 #endif /* end #define CLITKCOMMON_TXX */
168