]> Creatis software - clitk.git/blob - common/clitkCommon.cxx
VerboseInProgress and VerboseInProgressInPercentage were extremely strange functions...
[clitk.git] / common / clitkCommon.cxx
1 /*-------------------------------------------------------------------------
2
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.
6                                                                                 
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.
10                                                                              
11 -------------------------------------------------------------------------*/
12
13 #ifndef CLITKCOMMON_CXX
14 #define CLITKCOMMON_CXX
15
16 /**
17    -------------------------------------------------
18    * @file   clitkCommon.cxx
19    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
20    * @date   17 May 2006 07:59:06
21    * 
22    * @brief  
23    * 
24    * 
25    -------------------------------------------------*/
26
27 #include "clitkCommon.h"
28 #include <fstream>
29 #include <iomanip>
30 #include <sstream>
31
32 //------------------------------------------------------------------
33 // skip line which begin with a sharp '#'
34 void clitk::skipComment(std::istream & is) 
35 {
36   char c;
37   char line[1024];
38   if (is.eof()) return;
39   is >> c ; 
40   while (is && (c == '#')) {
41         is.getline (line, 1024); 
42         is >> c;
43         if (is.eof()) return;
44   }
45   is.unget();
46 } ////
47 //------------------------------------------------------------------
48   
49 //------------------------------------------------------------------
50 // linear (rough) conversion from Hounsfield Unit to density
51 double clitk::HU2density(double HU) 
52 {
53   return (HU+1000.0)/1000.0;
54 } ////
55 //------------------------------------------------------------------
56
57 //------------------------------------------------------------------
58 // Return filename extension
59 std::string clitk::GetExtension(const std::string& filename) 
60 {
61   // This assumes that the final '.' in a file name is the delimiter
62   // for the file's extension type
63   const std::string::size_type it = filename.find_last_of( "." );       
64   // This determines the file's type by creating a new string
65   // who's value is the extension of the input filename
66   // eg. "myimage.gif" has an extension of "gif"
67   std::string fileExt( filename, it+1, filename.length() );     
68   return( fileExt );
69 } ////
70 //------------------------------------------------------------------
71
72 //------------------------------------------------------------------
73 // Display progression
74 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
75 {
76   static int previous = -1;
77   const int rounded = (100*current)/nb;
78   if (previous==rounded) return; 
79   previous = rounded;
80
81   std::ostringstream oss;
82   oss << rounded << '%';
83
84   std::cout << oss.str() << std::flush;
85   for (int i=0; i<oss.str().length(); ++i) 
86         std::cout << "\b" << std::flush;
87 }
88 //------------------------------------------------------------------
89
90 //------------------------------------------------------------------
91 // Display progression
92 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
93 {
94   VerboseInProgress(nb, current, percentage);
95 }
96 //------------------------------------------------------------------
97
98 //------------------------------------------------------------------
99 // Convert a pixel type to another (downcast)
100 template<>
101 float clitk::PixelTypeDownCast(const double & x)
102 {
103   return (float)x;
104 }
105 //------------------------------------------------------------------
106
107 //------------------------------------------------------------------
108 double clitk::rad2deg(const double anglerad) {
109   return (anglerad/M_PI*180.0);
110 }
111 //------------------------------------------------------------------
112
113 //------------------------------------------------------------------
114 double clitk::deg2rad(const double angledeg) {
115   return (angledeg*(M_PI/180.0));
116 }
117 //------------------------------------------------------------------
118
119 //------------------------------------------------------------------
120 int clitk::GetTypeSizeFromString(const std::string & type) {
121 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE)             \
122   if (type == #TYPENAME) return sizeof(TYPE);
123   RETURN_SIZEOF_PIXEL(char, char);
124   RETURN_SIZEOF_PIXEL(uchar, uchar);
125   RETURN_SIZEOF_PIXEL(unsigned char, uchar);
126   RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
127   RETURN_SIZEOF_PIXEL(short, short);
128   RETURN_SIZEOF_PIXEL(ushort, ushort);
129   RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
130   RETURN_SIZEOF_PIXEL(int, int);
131   RETURN_SIZEOF_PIXEL(uint, uint);
132   RETURN_SIZEOF_PIXEL(unsigned_int, uint);
133   RETURN_SIZEOF_PIXEL(float, float);
134   RETURN_SIZEOF_PIXEL(double, double);
135   return 0;
136 }
137 //------------------------------------------------------------------
138
139 //------------------------------------------------------------------
140 template<>
141 bool clitk::IsSameType<signed char>(std::string t) { 
142   if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false; 
143 }
144
145 template<>
146 bool clitk::IsSameType<char>(std::string t) { 
147   if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false; 
148 }
149
150 template<>
151 bool clitk::IsSameType<unsigned char>(std::string t) { 
152   if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false; 
153 }
154
155 template<>
156 bool clitk::IsSameType<unsigned short>(std::string t) { 
157   if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false; 
158 }
159 //------------------------------------------------------------------
160
161 //------------------------------------------------------------------
162 void clitk::FindAndReplace(std::string & line, 
163                     const std::string & tofind, 
164                     const std::string & replacement) {
165   int pos = line.find(tofind);
166   while (pos!= (int)std::string::npos) {
167     line.replace(pos, tofind.size(), replacement);
168     pos = line.find(tofind, pos+tofind.size()+1);
169   }
170 }
171 //------------------------------------------------------------------
172
173 //------------------------------------------------------------------
174 void clitk::FindAndReplace(std::string & line, 
175                     const std::vector<std::string> & tofind, 
176                     const std::vector<std::string> & toreplace) {
177   for(unsigned int i=0; i<tofind.size(); i++) {
178     FindAndReplace(line, tofind[i], toreplace[i]);
179   }
180 }
181 //------------------------------------------------------------------
182
183 //------------------------------------------------------------------
184 void clitk::FindAndReplace(std::ifstream & in, 
185                     const std::vector<std::string> & tofind, 
186                     const std::vector<std::string> & toreplace,
187                     std::ofstream & out) {
188   std::string line;
189   if (tofind.size() != toreplace.size()) {
190     std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
191     std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
192     exit(0);
193   }
194   while (std::getline(in,line)) {
195     FindAndReplace(line, tofind, toreplace);
196     out << line << std::endl;
197   }
198 }
199 //------------------------------------------------------------------
200
201 //------------------------------------------------------------------
202 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point, 
203                                                        const itk::ContinuousIndex<double, 3> pointInPlane, 
204                                                        const itk::ContinuousIndex<double, 3> normalPlane) {
205   // http://mathworld.wolfram.com/Plane.html
206   // http://mathworld.wolfram.com/Point-PlaneDistance.html
207   double a = normalPlane[0];
208   double b = normalPlane[1];
209   double c = normalPlane[2];
210   double x0 = pointInPlane[0];
211   double y0 = pointInPlane[1];
212   double z0 = pointInPlane[2];
213   double x = point[0];
214   double y = point[1];
215   double z = point[2];
216
217   double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
218   DD(norm);
219   double d = -a*x0 - b*y0 - c*z0;
220   DD(d);
221   double distance = (a*x + b*y + c*z + d) / norm;
222   
223   return distance;
224 }
225 //------------------------------------------------------------------
226
227 //--------------------------------------------------------------------
228 // Open a file for reading
229 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
230   is.open(filename.c_str(), std::ios::in);
231   if ( is.fail() ) {
232     itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
233   }
234 }
235 //--------------------------------------------------------------------
236   
237 //--------------------------------------------------------------------
238 // Open a file for writing
239 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)  {
240   os.open(filename.c_str(), std::ios::out);
241   if ( os.fail() ) {
242     itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
243   }
244 }
245 //--------------------------------------------------------------------
246
247 //--------------------------------------------------------------------
248 double clitk::cotan(double i) { return(1.0 / tan(i)); }
249 double clitk::invcotan(double x) { 
250   //  http://mathworld.wolfram.com/InverseCotangent.html
251   double y;
252   if (x<0) {
253     y = -0.5*M_PI-atan(x);
254   }
255   else {
256     y = 0.5*M_PI-atan(x);
257   }
258   return y;
259 }
260 //--------------------------------------------------------------------
261
262 //--------------------------------------------------------------------
263 std::streambuf * clitk_stdcerr_backup;
264 void clitk::disableStdCerr() {
265   clitk_stdcerr_backup = std::cerr.rdbuf();
266   std::stringstream oss;
267   std::cerr.rdbuf( oss.rdbuf() );
268 }
269 //--------------------------------------------------------------------
270
271 //--------------------------------------------------------------------
272 void clitk::enableStdCerr() {
273   std::cerr.rdbuf(clitk_stdcerr_backup);
274 }
275 //--------------------------------------------------------------------
276
277 #endif /* end #define CLITKCOMMON_CXX */
278