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