]> Creatis software - clitk.git/blob - common/clitkCommon.cxx
- correct crop 2D bug
[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   return (anglerad/M_PI*180.0);
105 }
106 //------------------------------------------------------------------
107
108 //------------------------------------------------------------------
109 double clitk::deg2rad(const double angledeg) {
110   return (angledeg*(M_PI/180.0));
111 }
112 //------------------------------------------------------------------
113
114 //------------------------------------------------------------------
115 int clitk::GetTypeSizeFromString(const std::string & type) {
116 #define RETURN_SIZEOF_PIXEL(TYPENAME, TYPE)             \
117   if (type == #TYPENAME) return sizeof(TYPE);
118   RETURN_SIZEOF_PIXEL(char, char);
119   RETURN_SIZEOF_PIXEL(uchar, uchar);
120   RETURN_SIZEOF_PIXEL(unsigned char, uchar);
121   RETURN_SIZEOF_PIXEL(unsigned_char, uchar);
122   RETURN_SIZEOF_PIXEL(short, short);
123   RETURN_SIZEOF_PIXEL(ushort, ushort);
124   RETURN_SIZEOF_PIXEL(unsigned_short, ushort);
125   RETURN_SIZEOF_PIXEL(int, int);
126   RETURN_SIZEOF_PIXEL(uint, uint);
127   RETURN_SIZEOF_PIXEL(unsigned_int, uint);
128   RETURN_SIZEOF_PIXEL(float, float);
129   RETURN_SIZEOF_PIXEL(double, double);
130   return 0;
131 }
132 //------------------------------------------------------------------
133
134 //------------------------------------------------------------------
135 template<>
136 bool clitk::IsSameType<signed char>(std::string t) { 
137   if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true; else return false; 
138 }
139
140 template<>
141 bool clitk::IsSameType<char>(std::string t) { 
142   if ((t==GetTypeAsString<char>()) || (t == "char")) return true; else return false; 
143 }
144
145 template<>
146 bool clitk::IsSameType<unsigned char>(std::string t) { 
147   if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true; else return false; 
148 }
149
150 template<>
151 bool clitk::IsSameType<unsigned short>(std::string t) { 
152   if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true; else return false; 
153 }
154 //------------------------------------------------------------------
155
156 //------------------------------------------------------------------
157 void clitk::FindAndReplace(std::string & line, 
158                     const std::string & tofind, 
159                     const std::string & replacement) {
160   int pos = line.find(tofind);
161   while (pos!= (int)std::string::npos) {
162     line.replace(pos, tofind.size(), replacement);
163     pos = line.find(tofind, pos+tofind.size()+1);
164   }
165 }
166 //------------------------------------------------------------------
167
168 //------------------------------------------------------------------
169 void clitk::FindAndReplace(std::string & line, 
170                     const std::vector<std::string> & tofind, 
171                     const std::vector<std::string> & toreplace) {
172   for(unsigned int i=0; i<tofind.size(); i++) {
173     FindAndReplace(line, tofind[i], toreplace[i]);
174   }
175 }
176 //------------------------------------------------------------------
177
178 //------------------------------------------------------------------
179 void clitk::FindAndReplace(std::ifstream & in, 
180                     const std::vector<std::string> & tofind, 
181                     const std::vector<std::string> & toreplace,
182                     std::ofstream & out) {
183   std::string line;
184   if (tofind.size() != toreplace.size()) {
185     std::cerr << "Error' tofind' is size=" << tofind.size() << std::endl;
186     std::cerr << "... while 'toreplace' is size=" << toreplace.size() << std::endl;
187     exit(0);
188   }
189   while (std::getline(in,line)) {
190     FindAndReplace(line, tofind, toreplace);
191     out << line << std::endl;
192   }
193 }
194 //------------------------------------------------------------------
195
196 //------------------------------------------------------------------
197 double clitk::ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex<double, 3> point, 
198                                                        const itk::ContinuousIndex<double, 3> pointInPlane, 
199                                                        const itk::ContinuousIndex<double, 3> normalPlane) {
200   // http://mathworld.wolfram.com/Plane.html
201   // http://mathworld.wolfram.com/Point-PlaneDistance.html
202   double a = normalPlane[0];
203   double b = normalPlane[1];
204   double c = normalPlane[2];
205   double x0 = pointInPlane[0];
206   double y0 = pointInPlane[1];
207   double z0 = pointInPlane[2];
208   double x = point[0];
209   double y = point[1];
210   double z = point[2];
211
212   double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
213   DD(norm);
214   double d = -a*x0 - b*y0 - c*z0;
215   DD(d);
216   double distance = (a*x + b*y + c*z + d) / norm;
217   
218   return distance;
219 }
220 //------------------------------------------------------------------
221
222 //--------------------------------------------------------------------
223 // Open a file for reading
224 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
225   is.open(filename.c_str(), std::ios::in);
226   if ( is.fail() ) {
227     itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
228   }
229 }
230 //--------------------------------------------------------------------
231   
232 //--------------------------------------------------------------------
233 // Open a file for writing
234 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)  {
235   os.open(filename.c_str(), std::ios::out);
236   if ( os.fail() ) {
237     itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
238   }
239 }
240 //--------------------------------------------------------------------
241
242 //--------------------------------------------------------------------
243 double clitk::cotan(double i) { return(1.0 / tan(i)); }
244 double clitk::invcotan(double x) { 
245   //  http://mathworld.wolfram.com/InverseCotangent.html
246   double y;
247   if (x<0) {
248     y = -0.5*M_PI-atan(x);
249   }
250   else {
251     y = 0.5*M_PI-atan(x);
252   }
253   return y;
254 }
255 //--------------------------------------------------------------------
256
257 //--------------------------------------------------------------------
258 std::streambuf * clitk_stdcerr_backup;
259 void clitk::disableStdCerr() {
260   clitk_stdcerr_backup = std::cerr.rdbuf();
261   std::stringstream oss;
262   std::cerr.rdbuf( oss.rdbuf() );
263 }
264 //--------------------------------------------------------------------
265
266 //--------------------------------------------------------------------
267 void clitk::enableStdCerr() {
268   std::cerr.rdbuf(clitk_stdcerr_backup);
269 }
270 //--------------------------------------------------------------------
271
272
273 //--------------------------------------------------------------------
274 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list) {
275   std::ifstream is;
276   clitk::openFileForReading(is, filename);
277   list.clear();
278   while (is) {
279     clitk::skipComment(is);
280     double d;
281     is >> d;
282     if (is) list.push_back(d);
283   }
284 }
285 //--------------------------------------------------------------------
286
287
288 #endif /* end #define CLITKCOMMON_CXX */
289