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