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