1 /*-------------------------------------------------------------------------
3 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
4 l'Image). All rights reserved. See Doc/License.txt or
5 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
7 This software is distributed WITHOUT ANY WARRANTY; without even
8 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 PURPOSE. See the above copyright notices for more information.
11 -------------------------------------------------------------------------*/
13 #ifndef CLITKCOMMON_TXX
14 #define CLITKCOMMON_TXX
17 -------------------------------------------------
18 * @file clitkCommon.txx
19 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
22 -------------------------------------------------*/
24 //--------------------------------------------------------------------
25 // Convert float, double ... to string
27 std::string toString(const T & t) {
28 std::ostringstream myStream;
29 myStream << t << std::flush;
30 return(myStream.str());
32 //--------------------------------------------------------------------
34 //--------------------------------------------------------------------
35 // Convert float*, double* ... to string
37 std::string toStringVector(const T * t, const int n) {
38 std::ostringstream myStream;
39 for(int i=0; i<n-1; i++)
40 myStream << clitk::toString<T>(t[i]) << " ";
41 myStream << clitk::toString<T>(t[n-1]) << std::flush;
42 return(myStream.str());
44 //--------------------------------------------------------------------
46 //--------------------------------------------------------------------
47 // Convert float*, double* ... to string
49 std::string toStringVector(const T & t, const int n) {
50 std::ostringstream myStream;
51 for(int i=0; i<n-1; i++)
52 myStream << t[i] << " ";
53 myStream << t[n-1] << std::flush;
54 return(myStream.str());
56 //--------------------------------------------------------------------
58 //--------------------------------------------------------------------
59 // Convert float*, double* ... to string
61 std::string toStringVector(const std::vector<T> & t) {
62 return toStringVector(&t[0], t.size());
64 //--------------------------------------------------------------------
66 //--------------------------------------------------------------------
67 // Convert a pixel type to another (downcast)
68 template<class TPixelUp, class TPixelDown>
69 TPixelDown PixelTypeDownCast(const TPixelUp & x) {
70 return (TPixelDown)lrint(x);
72 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
76 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
77 vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
78 bool operator()(int x, int y) {
79 return (vect[x] < vect[y]);
81 const std::vector<Type> & vect;
83 //--------------------------------------------------------------------
85 //--------------------------------------------------------------------
87 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
88 vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
89 bool operator()(int x, int y) {
90 return (vect[x] > vect[y]);
92 const std::vector<Type> & vect;
94 //--------------------------------------------------------------------
96 //--------------------------------------------------------------------
98 void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing) {
99 index.resize(toSort.size());
100 for(unsigned int i=0; i<index.size(); i++) index[i] = i;
102 std::sort(index.begin(),
104 vectorComparisonLowerThan<double>(toSort));
106 std::sort(index.begin(),
108 vectorComparisonGreaterThan<double>(toSort));
110 //--------------------------------------------------------------------
112 //--------------------------------------------------------------------
113 template<class TPixel>
114 std::string GetTypeAsString() {
115 const std::type_info & PixType = typeid(TPixel);
116 std::string pixelName;
117 if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
118 else if (PixType == typeid(signed char)) pixelName = "signed_char";
119 else if (PixType == typeid(unsigned char)) pixelName = "unsigned_char";
120 else if (PixType == typeid(short)) pixelName = "short";
121 else if (PixType == typeid(unsigned short)) pixelName = "unsigned_short";
122 else if (PixType == typeid(int)) pixelName = "int";
123 else if (PixType == typeid(unsigned int)) pixelName = "unsigned_int";
124 else if (PixType == typeid(float)) pixelName = "float";
125 else if (PixType == typeid(double)) pixelName = "double";
126 else pixelName = PixType.name();
129 //--------------------------------------------------------------------
131 //--------------------------------------------------------------------
132 template<class ImageType>
133 void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) {
134 output->SetRegions(input->GetLargestPossibleRegion());
135 output->SetSpacing(input->GetSpacing());
137 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
138 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
140 typedef itk::ImageRegionIterator<ImageType> IteratorType;
141 IteratorType po(output,input->GetLargestPossibleRegion());
143 while (!pi.IsAtEnd()) {
149 //--------------------------------------------------------------------
151 #endif /* end #define CLITKCOMMON_TXX */