]> Creatis software - clitk.git/blob - common/clitkCommon.cxx
#61 Remove undesired variable declaration in vvBinaryImageOverlayActor
[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 #include <itksys/SystemTools.hxx>
23
24 // clitk include 
25 #include "clitkCommon.h"
26
27 // std include 
28 #include <fstream>
29 #include <iomanip>
30 #include <sstream>
31 #include <cerrno>
32
33 //------------------------------------------------------------------
34 // skip line which begin with a sharp '#'
35 void clitk::skipComment(std::istream & is)
36 {
37   char c;
38   char line[1024];
39   if (is.eof()) return;
40   is >> c ;
41   while (is && (c == '#')) {
42     is.getline (line, 1024);
43     is >> c;
44     if (is.eof()) return;
45   }
46   if (!(is.fail()) && c != '\n')
47     is.unget();
48 } ////
49 //------------------------------------------------------------------
50
51 //------------------------------------------------------------------
52 // linear (rough) conversion from Hounsfield Unit to density
53 double clitk::HU2density(double HU)
54 {
55   return (HU+1000.0)/1000.0;
56 } ////
57 //------------------------------------------------------------------
58
59 //------------------------------------------------------------------
60 // Return filename extension
61 std::string clitk::GetExtension(const std::string& filename)
62 {
63   // This assumes that the final '.' in a file name is the delimiter
64   // for the file's extension type
65   const std::string::size_type it = filename.find_last_of( "." );
66   // This determines the file's type by creating a new string
67   // who's value is the extension of the input filename
68   // eg. "myimage.gif" has an extension of "gif"
69   std::string fileExt( filename, it+1, filename.length() );
70   return( fileExt );
71 } ////
72 //------------------------------------------------------------------
73
74
75 //------------------------------------------------------------------
76 // Return filename splitting in 1 or 2 parts : directory name (if exists) & filename
77 std::vector<std::string> clitk::SplitFilename(const std::string& filename)
78 {
79   std::vector<std::string> dirname;
80   std::string path = itksys::SystemTools::GetFilenamePath(filename);
81   std::vector<std::string> pathComponents;
82   itksys::SystemTools::SplitPath(filename.c_str(), pathComponents);
83   std::string fileName = pathComponents.back();
84   if (path != "")
85     dirname.push_back(path);
86   dirname.push_back(fileName);
87   return( dirname );
88 } ////
89 //------------------------------------------------------------------
90
91
92 //------------------------------------------------------------------
93 // Display progression
94 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
95 {
96   static int previous = -1;
97   const int rounded = (100*current)/nb;
98   if (previous==rounded) return;
99   previous = rounded;
100
101   std::ostringstream oss;
102   oss << std::setw(4) << rounded << '%';
103
104   std::cout << oss.str() << std::flush;
105   for (unsigned int i=0; i<oss.str().length(); ++i)
106     std::cout << "\b" << std::flush;
107 }
108 //------------------------------------------------------------------
109
110 //------------------------------------------------------------------
111 // Display progression
112 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
113 {
114   VerboseInProgress(nb, current, percentage);
115 }
116 //------------------------------------------------------------------
117
118 //------------------------------------------------------------------
119 // Convert a pixel type to another (downcast)
120 template<>
121 float clitk::PixelTypeDownCast(const double & x)
122 {
123   return (float)x;
124 }
125 //------------------------------------------------------------------
126
127 //------------------------------------------------------------------
128 // Convert a pixel type without casting
129 template<>
130 double clitk::PixelTypeDownCast(const double & x)
131 {
132   return x;
133 }
134 //------------------------------------------------------------------
135
136 //------------------------------------------------------------------
137 double clitk::rad2deg(const double anglerad)
138 {
139   return (anglerad/M_PI*180.0);
140 }
141 //------------------------------------------------------------------
142
143 //------------------------------------------------------------------
144 double clitk::deg2rad(const double angledeg)
145 {
146   return (angledeg*(M_PI/180.0));
147 }
148 //------------------------------------------------------------------
149
150 //------------------------------------------------------------------
151 int clitk::GetTypeSizeFromString(const std::string & type)
152 {
153 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE)             \
154   if (type == #TYPENAME) return sizeof(TYPE);
155   RETURN_SIZEOF_PIXEL(char, char);
156   RETURN_SIZEOF_PIXEL(uchar, uchar);
157   RETURN_SIZEOF_PIXEL(unsigned char, uchar);
158   RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
159   RETURN_SIZEOF_PIXEL(short, short);
160   RETURN_SIZEOF_PIXEL(ushort, ushort);
161   RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
162   RETURN_SIZEOF_PIXEL(int, int);
163   RETURN_SIZEOF_PIXEL(uint, uint);
164   RETURN_SIZEOF_PIXEL(unsigned_int, uint);
165   RETURN_SIZEOF_PIXEL(float, float);
166   RETURN_SIZEOF_PIXEL(double, double);
167   return 0;
168 }
169 //------------------------------------------------------------------
170
171 //------------------------------------------------------------------
172 template<>
173 bool clitk::IsSameType<signed char>(std::string t)
174 {
175   if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
176   else return false;
177 }
178
179 template<>
180 bool clitk::IsSameType<char>(std::string t)
181 {
182   if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
183   else return false;
184 }
185
186 template<>
187 bool clitk::IsSameType<unsigned char>(std::string t)
188 {
189   if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
190   else return false;
191 }
192
193 template<>
194 bool clitk::IsSameType<unsigned short>(std::string t)
195 {
196   if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
197   else return false;
198 }
199 //------------------------------------------------------------------
200
201 //------------------------------------------------------------------
202 void clitk::FindAndReplace(std::string & line,
203                            const std::string & tofind,
204                            const std::string & replacement)
205 {
206   int pos = line.find(tofind);
207   while (pos!= (int)std::string::npos) {
208     line.replace(pos, tofind.size(), replacement);
209     pos = line.find(tofind, pos+tofind.size()+1);
210   }
211 }
212 //------------------------------------------------------------------
213
214 //------------------------------------------------------------------
215 void clitk::FindAndReplace(std::string & line,
216                            const std::vector<std::string> & tofind,
217                            const std::vector<std::string> & toreplace)
218 {
219   for(unsigned int i=0; i<tofind.size(); i++) {
220     FindAndReplace(line, tofind[i], toreplace[i]);
221   }
222 }
223 //------------------------------------------------------------------
224
225 //------------------------------------------------------------------
226 void clitk::FindAndReplace(std::ifstream & in,
227                            const std::vector<std::string> & tofind,
228                            const std::vector<std::string> & toreplace,
229                            std::ofstream & out)
230 {
231   std::string line;
232   if (tofind.size() != toreplace.size()) {
233     std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
234     std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
235     exit(0);
236   }
237   while (std::getline(in,line)) {
238     FindAndReplace(line, tofind, toreplace);
239     out << line << std::endl;
240   }
241 }
242 //------------------------------------------------------------------
243
244 //------------------------------------------------------------------
245 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point,
246     const itk::ContinuousIndex<double, 3> pointInPlane,
247     const itk::ContinuousIndex<double, 3> normalPlane)
248 {
249   // http://mathworld.wolfram.com/Plane.html
250   // http://mathworld.wolfram.com/Point-PlaneDistance.html
251   double a = normalPlane[0];
252   double b = normalPlane[1];
253   double c = normalPlane[2];
254   double x0 = pointInPlane[0];
255   double y0 = pointInPlane[1];
256   double z0 = pointInPlane[2];
257   double x = point[0];
258   double y = point[1];
259   double z = point[2];
260
261   double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
262   DD(norm);
263   double d = -a*x0 - b*y0 - c*z0;
264   DD(d);
265   double distance = (a*x + b*y + c*z + d) / norm;
266
267   return distance;
268 }
269 //------------------------------------------------------------------
270
271 //--------------------------------------------------------------------
272 // Open a file for reading
273 void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
274 {
275   is.open(filename.c_str(), std::ios::in | std::ios::binary);
276   if ( is.fail() ) {
277     clitkExceptionMacro("Could not open file for reading: " 
278                         << filename << ". Error is : <" 
279                         << strerror(errno) << ">");
280   }
281 }
282 //--------------------------------------------------------------------
283
284 //--------------------------------------------------------------------
285 // Open a file for writing
286 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
287 {
288   os.open(filename.c_str(), std::ios::out);
289   if ( os.fail() ) {
290     clitkExceptionMacro("Could not open file for writing: " 
291                         << filename << ". Error is : <" 
292                         << strerror(errno) << ">");
293   }
294 }
295 //--------------------------------------------------------------------
296
297 //--------------------------------------------------------------------
298 double clitk::cotan(double i)
299 {
300   return(1.0 / tan(i));
301 }
302 double clitk::invcotan(double x)
303 {
304   //  http://mathworld.wolfram.com/InverseCotangent.html
305   double y;
306   if (x<0) {
307     y = -0.5*M_PI-atan(x);
308   } else {
309     y = 0.5*M_PI-atan(x);
310   }
311   return y;
312 }
313 //--------------------------------------------------------------------
314
315 //--------------------------------------------------------------------
316 std::streambuf * clitk_stdcerr_backup;
317 void clitk::disableStdCerr()
318 {
319   clitk_stdcerr_backup = std::cerr.rdbuf();
320   std::stringstream oss;
321   std::cerr.rdbuf( oss.rdbuf() );
322 }
323 //--------------------------------------------------------------------
324
325 //--------------------------------------------------------------------
326 void clitk::enableStdCerr()
327 {
328   std::cerr.rdbuf(clitk_stdcerr_backup);
329 }
330 //--------------------------------------------------------------------
331
332
333 //--------------------------------------------------------------------
334 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
335 {
336   std::ifstream is;
337   clitk::openFileForReading(is, filename);
338   list.clear();
339   while (is) {
340     clitk::skipComment(is);
341     double d;
342     is >> d;
343     if (is) list.push_back(d);
344   }
345 }
346 //--------------------------------------------------------------------
347
348
349 //--------------------------------------------------------------------
350 void clitk::PrintMemoryUsed()
351 {
352 #if defined(unix) || defined(__APPLE__)
353   rusage usage;  
354   getrusage(RUSAGE_SELF, &usage);
355   DD(usage.ru_maxrss);        /* maximum resident set size */ 
356   // DD(usage.ru_ixrss);         /* integral shared memory size */
357   // DD(usage.ru_idrss);         /* integral unshared data size */
358   // DD(usage.ru_isrss);         /* integral unshared stack size */
359 #endif
360 }
361 //--------------------------------------------------------------------
362
363
364 #endif /* end #define CLITKCOMMON_CXX */
365