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