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