]> Creatis software - clitk.git/blob - common/clitkCommon.cxx
Merge branch 'master' into OpenGL2
[clitk.git] / common / clitkCommon.cxx
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://www.centreleonberard.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
19 #ifndef CLITKCOMMON_CXX
20 #define CLITKCOMMON_CXX
21
22 // clitk include 
23 #include "clitkCommon.h"
24
25 // std include 
26 #include <fstream>
27 #include <iomanip>
28 #include <sstream>
29 #include <cerrno>
30
31 //------------------------------------------------------------------
32 // skip line which begin with a sharp '#'
33 void clitk::skipComment(std::istream & is)
34 {
35   char c;
36   char line[1024];
37   if (is.eof()) return;
38   is >> c ;
39   while (is && (c == '#')) {
40     is.getline (line, 1024);
41     is >> c;
42     if (is.eof()) return;
43   }
44   if (!(is.fail()) && c != '\n')
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 << std::setw(4) << rounded << '%';
83
84   std::cout << oss.str() << std::flush;
85   for (unsigned 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 // Convert a pixel type without casting
109 template<>
110 double clitk::PixelTypeDownCast(const double & x)
111 {
112   return x;
113 }
114 //------------------------------------------------------------------
115
116 //------------------------------------------------------------------
117 double clitk::rad2deg(const double anglerad)
118 {
119   return (anglerad/M_PI*180.0);
120 }
121 //------------------------------------------------------------------
122
123 //------------------------------------------------------------------
124 double clitk::deg2rad(const double angledeg)
125 {
126   return (angledeg*(M_PI/180.0));
127 }
128 //------------------------------------------------------------------
129
130 //------------------------------------------------------------------
131 int clitk::GetTypeSizeFromString(const std::string & type)
132 {
133 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE)             \
134   if (type == #TYPENAME) return sizeof(TYPE);
135   RETURN_SIZEOF_PIXEL(char, char);
136   RETURN_SIZEOF_PIXEL(uchar, uchar);
137   RETURN_SIZEOF_PIXEL(unsigned char, uchar);
138   RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
139   RETURN_SIZEOF_PIXEL(short, short);
140   RETURN_SIZEOF_PIXEL(ushort, ushort);
141   RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
142   RETURN_SIZEOF_PIXEL(int, int);
143   RETURN_SIZEOF_PIXEL(uint, uint);
144   RETURN_SIZEOF_PIXEL(unsigned_int, uint);
145   RETURN_SIZEOF_PIXEL(float, float);
146   RETURN_SIZEOF_PIXEL(double, double);
147   return 0;
148 }
149 //------------------------------------------------------------------
150
151 //------------------------------------------------------------------
152 template<>
153 bool clitk::IsSameType<signed char>(std::string t)
154 {
155   if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
156   else return false;
157 }
158
159 template<>
160 bool clitk::IsSameType<char>(std::string t)
161 {
162   if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
163   else return false;
164 }
165
166 template<>
167 bool clitk::IsSameType<unsigned char>(std::string t)
168 {
169   if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
170   else return false;
171 }
172
173 template<>
174 bool clitk::IsSameType<unsigned short>(std::string t)
175 {
176   if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
177   else return false;
178 }
179 //------------------------------------------------------------------
180
181 //------------------------------------------------------------------
182 void clitk::FindAndReplace(std::string & line,
183                            const std::string & tofind,
184                            const std::string & replacement)
185 {
186   int pos = line.find(tofind);
187   while (pos!= (int)std::string::npos) {
188     line.replace(pos, tofind.size(), replacement);
189     pos = line.find(tofind, pos+tofind.size()+1);
190   }
191 }
192 //------------------------------------------------------------------
193
194 //------------------------------------------------------------------
195 void clitk::FindAndReplace(std::string & line,
196                            const std::vector<std::string> & tofind,
197                            const std::vector<std::string> & toreplace)
198 {
199   for(unsigned int i=0; i<tofind.size(); i++) {
200     FindAndReplace(line, tofind[i], toreplace[i]);
201   }
202 }
203 //------------------------------------------------------------------
204
205 //------------------------------------------------------------------
206 void clitk::FindAndReplace(std::ifstream & in,
207                            const std::vector<std::string> & tofind,
208                            const std::vector<std::string> & toreplace,
209                            std::ofstream & out)
210 {
211   std::string line;
212   if (tofind.size() != toreplace.size()) {
213     std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
214     std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
215     exit(0);
216   }
217   while (std::getline(in,line)) {
218     FindAndReplace(line, tofind, toreplace);
219     out << line << std::endl;
220   }
221 }
222 //------------------------------------------------------------------
223
224 //------------------------------------------------------------------
225 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
226     const itk::ContinuousIndex<double, 3> pointInPlane,
227     const itk::ContinuousIndex<double, 3> normalPlane)
228 {
229   // http://mathworld.wolfram.com/Plane.html
230   // http://mathworld.wolfram.com/Point-PlaneDistance.html
231   double a = normalPlane[0];
232   double b = normalPlane[1];
233   double c = normalPlane[2];
234   double x0 = pointInPlane[0];
235   double y0 = pointInPlane[1];
236   double z0 = pointInPlane[2];
237   double x = point[0];
238   double y = point[1];
239   double z = point[2];
240
241   double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
242   DD(norm);
243   double d = -a*x0 - b*y0 - c*z0;
244   DD(d);
245   double distance = (a*x + b*y + c*z + d) / norm;
246
247   return distance;
248 }
249 //------------------------------------------------------------------
250
251 //--------------------------------------------------------------------
252 // Open a file for reading
253 void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
254 {
255   is.open(filename.c_str(), std::ios::in | std::ios::binary);
256   if ( is.fail() ) {
257     clitkExceptionMacro("Could not open file for reading: " 
258                         << filename << ". Error is : <" 
259                         << strerror(errno) << ">");
260   }
261 }
262 //--------------------------------------------------------------------
263
264 //--------------------------------------------------------------------
265 // Open a file for writing
266 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
267 {
268   os.open(filename.c_str(), std::ios::out);
269   if ( os.fail() ) {
270     clitkExceptionMacro("Could not open file for writing: " 
271                         << filename << ". Error is : <" 
272                         << strerror(errno) << ">");
273   }
274 }
275 //--------------------------------------------------------------------
276
277 //--------------------------------------------------------------------
278 double clitk::cotan(double i)
279 {
280   return(1.0 / tan(i));
281 }
282 double clitk::invcotan(double x)
283 {
284   //  http://mathworld.wolfram.com/InverseCotangent.html
285   double y;
286   if (x<0) {
287     y = -0.5*M_PI-atan(x);
288   } else {
289     y = 0.5*M_PI-atan(x);
290   }
291   return y;
292 }
293 //--------------------------------------------------------------------
294
295 //--------------------------------------------------------------------
296 std::streambuf * clitk_stdcerr_backup;
297 void clitk::disableStdCerr()
298 {
299   clitk_stdcerr_backup = std::cerr.rdbuf();
300   std::stringstream oss;
301   std::cerr.rdbuf( oss.rdbuf() );
302 }
303 //--------------------------------------------------------------------
304
305 //--------------------------------------------------------------------
306 void clitk::enableStdCerr()
307 {
308   std::cerr.rdbuf(clitk_stdcerr_backup);
309 }
310 //--------------------------------------------------------------------
311
312
313 //--------------------------------------------------------------------
314 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
315 {
316   std::ifstream is;
317   clitk::openFileForReading(is, filename);
318   list.clear();
319   while (is) {
320     clitk::skipComment(is);
321     double d;
322     is >> d;
323     if (is) list.push_back(d);
324   }
325 }
326 //--------------------------------------------------------------------
327
328
329 //--------------------------------------------------------------------
330 void clitk::PrintMemoryUsed()
331 {
332 #if defined(unix) || defined(__APPLE__)
333   rusage usage;  
334   getrusage(RUSAGE_SELF, &usage);
335   DD(usage.ru_maxrss);        /* maximum resident set size */ 
336   // DD(usage.ru_ixrss);         /* integral shared memory size */
337   // DD(usage.ru_idrss);         /* integral unshared data size */
338   // DD(usage.ru_isrss);         /* integral unshared stack size */
339 #endif
340 }
341 //--------------------------------------------------------------------
342
343
344 #endif /* end #define CLITKCOMMON_CXX */
345