1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #ifndef CLITKCOMMON_CXX
20 #define CLITKCOMMON_CXX
22 #include "clitkCommon.h"
27 //------------------------------------------------------------------
28 // skip line which begin with a sharp '#'
29 void clitk::skipComment(std::istream & is)
35 while (is && (c == '#')) {
36 is.getline (line, 1024);
42 //------------------------------------------------------------------
44 //------------------------------------------------------------------
45 // linear (rough) conversion from Hounsfield Unit to density
46 double clitk::HU2density(double HU)
48 return (HU+1000.0)/1000.0;
50 //------------------------------------------------------------------
52 //------------------------------------------------------------------
53 // Return filename extension
54 std::string clitk::GetExtension(const std::string& filename)
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() );
65 //------------------------------------------------------------------
67 //------------------------------------------------------------------
68 // Display progression
69 void clitk::VerboseInProgress(const int nb, const int current, const int percentage)
71 static int previous = -1;
72 const int rounded = (100*current)/nb;
73 if (previous==rounded) return;
76 std::ostringstream oss;
77 oss << std::setw(4) << rounded << '%';
79 std::cout << oss.str() << std::flush;
80 for (unsigned int i=0; i<oss.str().length(); ++i)
81 std::cout << "\b" << std::flush;
83 //------------------------------------------------------------------
85 //------------------------------------------------------------------
86 // Display progression
87 void clitk::VerboseInProgressInPercentage(const int nb, const int current, const int percentage)
89 VerboseInProgress(nb, current, percentage);
91 //------------------------------------------------------------------
93 //------------------------------------------------------------------
94 // Convert a pixel type to another (downcast)
96 float clitk::PixelTypeDownCast(const double & x)
100 //------------------------------------------------------------------
102 //------------------------------------------------------------------
103 double clitk::rad2deg(const double anglerad)
105 return (anglerad/M_PI*180.0);
107 //------------------------------------------------------------------
109 //------------------------------------------------------------------
110 double clitk::deg2rad(const double angledeg)
112 return (angledeg*(M_PI/180.0));
114 //------------------------------------------------------------------
116 //------------------------------------------------------------------
117 int clitk::GetTypeSizeFromString(const std::string & type)
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);
135 //------------------------------------------------------------------
137 //------------------------------------------------------------------
139 bool clitk::IsSameType<signed char>(std::string t)
141 if ((t==GetTypeAsString<signed char>()) || (t == "schar")) return true;
146 bool clitk::IsSameType<char>(std::string t)
148 if ((t==GetTypeAsString<char>()) || (t == "char")) return true;
153 bool clitk::IsSameType<unsigned char>(std::string t)
155 if ((t==GetTypeAsString<unsigned char>()) || (t == "uchar")) return true;
160 bool clitk::IsSameType<unsigned short>(std::string t)
162 if ((t==GetTypeAsString<unsigned short>()) || (t == "ushort")) return true;
165 //------------------------------------------------------------------
167 //------------------------------------------------------------------
168 void clitk::FindAndReplace(std::string & line,
169 const std::string & tofind,
170 const std::string & replacement)
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);
178 //------------------------------------------------------------------
180 //------------------------------------------------------------------
181 void clitk::FindAndReplace(std::string & line,
182 const std::vector<std::string> & tofind,
183 const std::vector<std::string> & toreplace)
185 for(unsigned int i=0; i<tofind.size(); i++) {
186 FindAndReplace(line, tofind[i], toreplace[i]);
189 //------------------------------------------------------------------
191 //------------------------------------------------------------------
192 void clitk::FindAndReplace(std::ifstream & in,
193 const std::vector<std::string> & tofind,
194 const std::vector<std::string> & toreplace,
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;
203 while (std::getline(in,line)) {
204 FindAndReplace(line, tofind, toreplace);
205 out << line << std::endl;
208 //------------------------------------------------------------------
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)
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];
227 double norm = sqrt(x0*x0 + y0*y0 + z0*z0);
229 double d = -a*x0 - b*y0 - c*z0;
231 double distance = (a*x + b*y + c*z + d) / norm;
235 //------------------------------------------------------------------
237 //--------------------------------------------------------------------
238 // Open a file for reading
239 void clitk::openFileForReading(std::ifstream & is, const std::string & filename)
241 is.open(filename.c_str(), std::ios::in);
243 clitkExceptionMacro("Could not open file (for reading): " << filename);
246 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 // Open a file for writing
250 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)
252 os.open(filename.c_str(), std::ios::out);
254 clitkExceptionMacro("Could not open file (for writing): " << filename);
257 //--------------------------------------------------------------------
259 //--------------------------------------------------------------------
260 double clitk::cotan(double i)
262 return(1.0 / tan(i));
264 double clitk::invcotan(double x)
266 // http://mathworld.wolfram.com/InverseCotangent.html
269 y = -0.5*M_PI-atan(x);
271 y = 0.5*M_PI-atan(x);
275 //--------------------------------------------------------------------
277 //--------------------------------------------------------------------
278 std::streambuf * clitk_stdcerr_backup;
279 void clitk::disableStdCerr()
281 clitk_stdcerr_backup = std::cerr.rdbuf();
282 std::stringstream oss;
283 std::cerr.rdbuf( oss.rdbuf() );
285 //--------------------------------------------------------------------
287 //--------------------------------------------------------------------
288 void clitk::enableStdCerr()
290 std::cerr.rdbuf(clitk_stdcerr_backup);
292 //--------------------------------------------------------------------
295 //--------------------------------------------------------------------
296 void clitk::readDoubleFromFile(const std::string & filename, std::vector<double> & list)
299 clitk::openFileForReading(is, filename);
302 clitk::skipComment(is);
305 if (is) list.push_back(d);
308 //--------------------------------------------------------------------
311 #endif /* end #define CLITKCOMMON_CXX */